Arduino-ESP32 documantation & Timer API

sevketk
Posts: 2
Joined: Fri Dec 08, 2023 9:57 am

Arduino-ESP32 documantation & Timer API

Postby sevketk » Fri Dec 08, 2023 10:11 am

I am using the sample code in link:
https://espressif-docs.readthedocs-host ... timer.html
my platform Arduino 2.2.1
my Board library : Esp32 by Espressif systems 2.0.11
And I get erros for examle :
C:\Users\leno\AppData\Local\Temp\.arduinoIDE-unsaved2023118-15208-5agu7p.d9ara\sketch_dec8a\sketch_dec8a.ino: In function 'void setup()':
C:\Users\leno\AppData\Local\Temp\.arduinoIDE-unsaved2023118-15208-5agu7p.d9ara\sketch_dec8a\sketch_dec8a.ino:7:29: error: too few arguments to function 'hw_timer_t* timerBegin(uint8_t, uint16_t, bool)'
timer = timerBegin(1000000);
^
In file included from C:\Users\leno\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.11\cores\esp32/esp32-hal.h:92,
from C:\Users\leno\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.11\cores\esp32/Arduino.h:36,
from C:\Users\leno\AppData\Local\Temp\.arduinoIDE-unsaved2023118-15208-5agu7p.d9ara\sketch_dec8a\sketch_dec8a.ino:1:
C:\Users\leno\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.11\cores\esp32/esp32-hal-timer.h:33:14: note: declared here
hw_timer_t * timerBegin(uint8_t timer, uint16_t divider, bool countUp);
Etc...
Why?

ESP_Minatel
Posts: 364
Joined: Mon Jan 04, 2021 2:06 pm

Re: Arduino-ESP32 documantation & Timer API

Postby ESP_Minatel » Fri Dec 08, 2023 10:29 am

Hi sevketk,

The timer library, and many other are being refactored in order to increase the compatibility with the Arduino. The documentation reflects the master branch and not the latest release version, today is the 2.0.14.

You can use the migration guide to see all the refactored APIs.

If you are using the 2.0.11, the example you should try is here: timer examples for 2.0.11 version.
Alternatively, you can use the 2.0.11 version and use the link I've shared, or you can use the alpha version of the 3.0.0.

sevketk
Posts: 2
Joined: Fri Dec 08, 2023 9:57 am

Re: Arduino-ESP32 documantation & Timer API

Postby sevketk » Fri Dec 08, 2023 11:16 am

Ok. Thank you.
As far as I understand, the operations I want to do with the timer are
It is wrong to use it inside "void ARDUINO_ISR_ATTR onTimer(){ } ". I have to use it inside the "loop". Otherwise, when my operations are delayed a little, I get the error "Guru Meditation Error: Core 1 panic'ed (Interrupt wdt timeout on CPU1). " Did I understand correctly?
for example
Wrong:
  1. void ARDUINO_ISR_ATTR onTimer(){
  2.    dosomething();//*********** what i want to do *******
  3. }
correct:
  1. void ARDUINO_ISR_ATTR onTimer(){
  2.   // Increment the counter and set the time of ISR
  3.   portENTER_CRITICAL_ISR(&timerMux);
  4.   isrCounter++;
  5.   lastIsrAt = millis();
  6.   portEXIT_CRITICAL_ISR(&timerMux);
  7.   // Give a semaphore that we can check in the loop
  8.   xSemaphoreGiveFromISR(timerSemaphore, NULL);
  9.   // It is safe to use digitalRead/Write here if you want to toggle an output
  10. }
  11. void loop() {
  12.   // If Timer has fired
  13.   if (xSemaphoreTake(timerSemaphore, 0) == pdTRUE){
  14.     uint32_t isrCount = 0, isrTime = 0;
  15.     // Read the interrupt count and time
  16.     portENTER_CRITICAL(&timerMux);
  17.     isrCount = isrCounter;
  18.     isrTime = lastIsrAt;
  19.     portEXIT_CRITICAL(&timerMux);
  20.     // Print it
  21.    
  22.       dosomething();//*********** what i want to do *******
  23.   }
  24. }
>>>>>
In its simplest form, what kind of code should I write for an operation that I want to perform that takes 2000 ms and will occur every 500 ms?

ESP_Minatel
Posts: 364
Joined: Mon Jan 04, 2021 2:06 pm

Re: Arduino-ESP32 documantation & Timer API

Postby ESP_Minatel » Fri Dec 08, 2023 12:00 pm

I think you need a task, not a timer.
examples/FreeRTOS

lbernstone
Posts: 829
Joined: Mon Jul 22, 2019 3:20 pm

Re: Arduino-ESP32 documantation & Timer API

Postby lbernstone » Fri Dec 08, 2023 5:44 pm

If the code you have posted is actually what you are trying to do, and you having something that takes 2000ms running every 500ms, then your problem is very likely race conditions.
You can use a timer (or Ticker if you don't need an interrupt) to start the process, but you shouldn't be using the critical mutex. Make your own mutex specific to this task, and put a realistic timeout on it, or else you will get the processes backed up and exhaust your memory.

Who is online

Users browsing this forum: No registered users and 140 guests