Home / Learn Chip Programming / RTOS Software Timers and Tick Accuracy
RTOS Software Timers and Tick Accuracy
Run functions after a delay or periodically without a task per timer.
Theory in plain words
What it is
Software timers call a callback after a period (one-shot or auto-reload). They run in a timer service task using the tick as time base.
Why we use it
Timeouts, retries, debouncing and periodic sampling are easy with timers and save tasks (and RAM).
How it works
xTimerCreate with period in ticks, xTimerStart, callback runs in the timer task so it must not block. Tick resolution is configTICK_RATE_HZ (usually 1000).
Where it is used
- Button debounce
- Retry after failed connection
- Blinking status LED pattern
Tick resolution
vTaskDelay(1) can last anywhere between 0 and 1 tick. Use a hardware timer if you need microsecond accuracy.
static void blink_cb(TimerHandle_t t) { toggle_led(); } // runs in the timer task: keep it short, never block
TimerHandle_t blink = xTimerCreate("blink", pdMS_TO_TICKS(500), pdTRUE, NULL, blink_cb); // pdTRUE = auto reload
xTimerStart(blink, 0);
TimerHandle_t once = xTimerCreate("timeout", pdMS_TO_TICKS(3000), pdFALSE, NULL, on_timeout);
xTimerReset(once, 0); // restart: use as a watchdog for a protocolQuick quiz
1. Which runs software timer callbacks?
So callbacks must not block.
2. vTaskDelayUntil prevents...
It uses an absolute schedule.