Software Timer using xTimerCreate runs 33% slow
Posted: 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;
(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;
I recalculate the required duration immediately before starting the timer;
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.
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;
- channel[channelId]->xCountdownTimer = xTimerCreate(
- "CountdownTimer",
- 1,
- pdFALSE,
- (void*) channelId,
- vCountdownTimerCallback
- );
I use 4th arg TimerID to store the index into the channel[]
With callback function;
- void vCountdownTimerCallback(TimerHandle_t xTimer) {
- int channelId = (int) pvTimerGetTimerID( xTimer );
- xSemaphoreGive( channel[channelId]->xStopSemaphore );
- }
- bool startCountdown() {
- return ( (pdPASS == xTimerChangePeriod(
- xCountdownTimer,
- pdMS_TO_TICKS( calcDuration() ),
- pdMS_TO_TICKS(TIMER_SET_BLOCK_TIME) ))
- && (pdPASS == xTimerStart(
- xCountdownTimer,
- pdMS_TO_TICKS(TIMER_SET_BLOCK_TIME) )));
- }
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.