Time starts sporadic

OneTwo
Posts: 20
Joined: Mon Jan 15, 2018 9:24 am

Time starts sporadic

Postby OneTwo » Mon Jan 15, 2018 9:39 am

Hallo community,

for my current project i need a Timer.
Currently I use the following code for the timer https://github.com/michaelbecker/freert ... /timer.hpp, with some changes in code.

In order to make it possible to call a Member-Function, I removed the
virtual function run

and added a void notify member function to Timer which should call the callback function given as a parameter to the constructor of Timer.
The Callback-Function is defined like:

Code: Select all

 std::function<void(Timer*)> CallbackFunc;

Example of construct the Timer:

Code: Select all

Timer* t1 = new Timer("", 1000, std::bind(&Class::Func, this, std::placeholders_1));
Now the Problem:
The Timer does not start reliably. In most cases when i press the reboot button of the esp32 the timer starts.

Do i need the IRAM_ATTR for the called member-function or do I forget something else?

EDIT:
btw i dont get any error or warning when the timer does not run/start.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Time starts sporadic

Postby ESP_Angus » Mon Jan 15, 2018 9:30 pm

Hi OneTwo,
OneTwo wrote: The Timer does not start reliably. In most cases when i press the reboot button of the esp32 the timer starts.
Can you explain what you mean? Do you mean, after ESP-IDFs resets the timer triggers once but doesn't trigger a second time?
OneTwo wrote: Do i need the IRAM_ATTR for the called member-function or do I forget something else?
No. FreeRTOS timers run in the context of the "timer task" which is the same as any other RTOS task (only low priority).
btw i dont get any error or warning when the timer does not run/start.
Without seeing your code and your modifications to the linked class it's hard to make any diagnosis. Maybe you could post these somewhere (gist.github.com or similar)? If it's complex code, try to make a simplified test case (this can be a good debugging technique by itself).

Something to remember is that the FreeRTOS Timer Task is a low priority task in FreeRTOS. So if you have higher priority tasks running, these will starve the timer callback from running. Make sure all your tasks spend at least some time asleep or blocked on another task. The Task Watchdog can be used to tell you if any task is monopolising the CPU.

If you want a more hardware-level timer that can run at higher priority, check out the ESP High Resolution Timer.

OneTwo
Posts: 20
Joined: Mon Jan 15, 2018 9:24 am

Re: Time starts sporadic

Postby OneTwo » Tue Jan 16, 2018 6:41 am

ESP_Angus wrote:Hi OneTwo,
Can you explain what you mean? Do you mean, after ESP-IDFs resets the timer triggers once but doesn't trigger a second time?
No, the timer does not start at all.
For debugging i added a log message inside the static function

Code: Select all

void Timer::TimerCallbackFunctionAdapter(TimerHandle_t xTimer)
which should be called from the FreeRTOS Timer Task if its triggerd. but as I said before sometimes it doenst get triggerd once.

ESP_Angus wrote: No. FreeRTOS timers run in the context of the "timer task" which is the same as any other RTOS task (only low priority).
ESP_Angus wrote: Something to remember is that the FreeRTOS Timer Task is a low priority task in FreeRTOS. So if you have higher priority tasks running, these will starve the timer callback from running. Make sure all your tasks spend at least some time asleep or blocked on another task. The Task Watchdog can be used to tell you if any task is monopolising the CPU.
Maybe this is the answer to the problem.
Currently there are two tasks in the project which are responsible for reading out sensors and one task for drawing/update the user interface and furthermore a task for read/write on uart.

That means a total of five tasks which all running at priority of 5 including the main thread.

I have often wondered how many tasks freertos/esp32 can handle.
ESP_Angus wrote: If you want a more hardware-level timer that can run at higher priority, check out the ESP High Resolution Timer
Thank you! I will check first the priority stuff.

Who is online

Users browsing this forum: No registered users and 82 guests