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.