Software Timer using xTimerCreate runs 33% slow

Posts: 1
Joined: Wed Sep 04, 2019 5:15 pm

Software Timer using xTimerCreate runs 33% slow

Postby vannoo67 » Wed Sep 04, 2019 8:56 pm

Hi Folks,

I am having trouble using FreeRTOS's Software timer. It works, but the duration of the timer is consistently 33% too long. To get the correct time, I need to scale my duration by 0.75.

I'm wondering if I have something wrong with my clock frequency setting?
Note: I am using Arduino IDE 1.8.9 with esp32 package 1.0.2
CPU Freq: 240 MHz (WiFi/BT)

My esp32 is an esp32 DevKit.

I setup my timer (initially with 1 tick duration) like so;
  1. channel[channelId]->xCountdownTimer = xTimerCreate(
  2.   "CountdownTimer",
  3.   1,
  4.   pdFALSE,
  5.   (void*) channelId,
  6.   vCountdownTimerCallback
  7. );
(Note: I have a number of separate 'Channels', each with a xCountdownTimer and associated xStopSemaphore)
I use 4th arg TimerID to store the index into the channel[]

With callback function;
  1. void vCountdownTimerCallback(TimerHandle_t xTimer) {
  2.   int channelId =  (int) pvTimerGetTimerID( xTimer );
  3.   xSemaphoreGive( channel[channelId]->xStopSemaphore );
  4. }
I recalculate the required duration immediately before starting the timer;
  1. bool startCountdown() {
  2.   return ( (pdPASS == xTimerChangePeriod(
  3.                                   xCountdownTimer,
  4.                                   pdMS_TO_TICKS( calcDuration() ),  
  5.                                   pdMS_TO_TICKS(TIMER_SET_BLOCK_TIME) ))
  7.                && (pdPASS == xTimerStart(
  8.                                   xCountdownTimer,
  9.                                   pdMS_TO_TICKS(TIMER_SET_BLOCK_TIME) )));
  10. }
Everything works perfectly, except that the duration is 33% longer than expected. As a work-around I am scaling the result of calcDuration() by 0.75

I have tried various sizes of delays, from seconds to minutes, but if I ask for 30 seconds, I get 40 seconds, ask for 60 seconds, I get 80 seconds.

I'm confident that my calcDuration function is working correctly (returns number of milliseconds as an unsigned long), but something is going pear-shaped in pdMS_TO_TICKS or deeper in the system.

Note: I've simplified my code a little, so if the above doesn't make complete sense, something was probably lost in translation.
eg; startCountdown() is actually a method of the Channel object stored in the channel[] so references to xCountdownTimer are local

Any suggestions will be gratefully received.

Posts: 136
Joined: Fri Jun 02, 2017 6:50 am

Re: Software Timer using xTimerCreate runs 33% slow

Postby ESP_Dazz » Thu Sep 12, 2019 2:27 pm

Some points to consider
  • How are you actually measuring the duration of your timer? The timer doesn't start until xTimerStart() returns, and xTimerStart() has a possibility of being blocked if the timer's internal command queue is full.
  • The timer callback could be delayed in running if there is something a higher priority task preventing the timer daemon task form running. Check your timer task's priority in your project configuration (should be priority 1 by default).

Who is online

Users browsing this forum: No registered users and 10 guests