hw timer

debugger3056
Posts: 4
Joined: Tue Jan 26, 2021 8:21 pm

hw timer

Postby debugger3056 » Tue Jan 26, 2021 8:35 pm

Dear colleagues,

I'm not as savvy as everyone else on this forum because of that
Programming MC's is "just" a hobby of mine. That's why I would be
grateful for a useful tip!
I have the following problem:

I initialize a hwTimer with:
  1. hw_timer_t *timer = NULL;
  2. portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
  3. volatile int interruptCounter;
  4. volatile int totalInterruptCounter;
  5. void IRAM_ATTR onTimer();
the Setup
  1. // Timer
  2.   timer = timerBegin(0, 80, true); // Teiler 80MHz / 80 = 1MHz
  3.   timerLed = timerBegin(1, 800, true); // Teiler 80MHz / 80 = 100kHz
  4.   timerAttachInterrupt(timerLed, &onTimerLed, true);
  5.   timerAlarmWrite(timerLed, 20000, true);
  6.   timerAlarmEnable(timerLed);
the interrupt routine
  1. void IRAM_ATTR onTimer() {
  2.   portENTER_CRITICAL_ISR(&timerMux);
  3.   interruptCounter++;
  4.   totalInterruptCounter++;
  5.   portEXIT_CRITICAL_ISR(&timerMux);
  6. }
and the loop
  1. if (stateCounter == 4) {
  2.     stateCounter = 5;
  3.     LedState = 0;
  4.        
  5.     display.clearDisplay();
  6.     display.setCursor(0,0);
  7.     display.println("measuring... ");
  8.     display.display();
  9.    
  10.     myFile = SD.open(fileString, FILE_WRITE);
  11.     if(!myFile){
  12.       DEBUG_PRINTLN("file dose not exist");
  13.     } else {
  14.       myFile.println(dataString);
  15.     }
  16.     runtime = 0;
  17.     timestamp = 0;
  18.     interruptCounter = 0;
  19.     totalInterruptCounter = 0;
  20.     timerAttachInterrupt(timer, &onTimer, true);
  21.     timerAlarmWrite(timer, 5000, true);
  22.     timerAlarmEnable(timer);
  23.     millisTimer = millis();
  24.    
  25.   } // end of measuring
  26.   // Interrupt completed 25 Jan 2021
  27.   if (interruptCounter > 0) {
  28.     portENTER_CRITICAL(&timerMux);
  29.     int sensorWert = 0;
  30.     timestamp += 0.005;
  31.      
  32.     dataString = String(timestamp, 3) + ","; // drei Dezimalstellen
  33.     // read sensors and append to the string:
  34.     for (byte i = 0; i < AnalogInAnzahl - 1; i++) {
  35.       sensorWert = analogRead(AnalogIn[i]) - AnalogInInit[i];
  36.       dataString += String(sensorWert, DEC) + ",";
  37.     }
  38.     sensorWert = analogRead(AnalogIn[AnalogInAnzahl - 1]) - AnalogInInit[AnalogInAnzahl - 1];
  39.     dataString += String(sensorWert, DEC); // the last data without komma
  40.      
  41.     if (myFile) {
  42.       myFile.println(dataString);
  43.     } else {
  44.       SDerror("error opening filestring");
  45.     }
  46.        
  47.     if (totalInterruptCounter >= 1201) {
  48.       timerAlarmDisable(timer);
  49.       runtime = millis() - millisTimer;
  50.       if (myFile) {
  51.         myFile.close();
  52.       }
  53.       DEBUG_PRINT("Messdauer [ms]: ");
  54.       DEBUG_PRINTLN(runtime);
  55.       LedState = 1;
  56.       stateCounter = 6;
  57.     }
  58.     interruptCounter--;
  59.     portEXIT_CRITICAL(&timerMux);
  60.   }

if I now fade out in the loop,
  1. //myFile.println(dataString);
I get a runtime of exactly 6000ms.
But if I transfer the data (approx. 40Byte per string) to the SD card
write results in a runtime (different) of approx. 6600ms!
At first I thought the SD routine couldn't do that in 5ms per
Record and tried 10ms, but inaccurate with the same
Result ?
Please please what can be the reason, what do I not understand?

Thanks in advance !!!

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: hw timer

Postby ESP_Sprite » Wed Jan 27, 2021 12:47 am

Your SD-card is a complicated bit of hardware, and any write to it can potentially take a fairly long while as it may need to do things like re-shuffle sector content for wear levelling.

debugger3056
Posts: 4
Joined: Tue Jan 26, 2021 8:21 pm

Re: hw timer

Postby debugger3056 » Wed Jan 27, 2021 8:20 am

Dear,

so how should I save 50Byte per 5ms (without BLE or WLan) ???

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: hw timer

Postby ESP_Sprite » Wed Jan 27, 2021 11:03 am

The easiest way in traditional ESP-IDF would be to use a separate thread for writing, and a big buffer between the two threads to hold any data when writing is slow. Not sure how to solve it in Arduino, I'm afraid.

debugger3056
Posts: 4
Joined: Tue Jan 26, 2021 8:21 pm

Re: hw timer

Postby debugger3056 » Wed Jan 27, 2021 2:15 pm

thank you very much,

could you tell me at last how you would realize a big buffer with ESP-IDF ?

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: hw timer

Postby ESP_Sprite » Thu Jan 28, 2021 3:13 am

The simplest way to do that is to make a large FreeRTOS queue. This can function both to shuttle the data from the sensor reading task to the writing task, as well as as a buffer to smooth out SD-card writing delay times.

debugger3056
Posts: 4
Joined: Tue Jan 26, 2021 8:21 pm

Re: hw timer

Postby debugger3056 » Thu Jan 28, 2021 9:12 am

thank you very much !

Who is online

Users browsing this forum: No registered users and 34 guests