Consider a FreeRTOS timer created as one-shot with a 1000 ms period. The timer callback increments a global counter each time it runs. The timer is started once. What will be the value of the counter after 3 seconds?
volatile int counter = 0; void TimerCallback(TimerHandle_t xTimer) { counter++; } int main() { TimerHandle_t timer = xTimerCreate("OneShotTimer", pdMS_TO_TICKS(1000), pdFALSE, NULL, TimerCallback); xTimerStart(timer, 0); vTaskDelay(pdMS_TO_TICKS(3000)); // What is counter now? return 0; }
One-shot timers run only once after the period expires.
A one-shot timer triggers its callback only once after the set period. Since the timer is started once and the period is 1000 ms, after 3 seconds the callback runs exactly once, so counter is 1.
A FreeRTOS auto-reload timer is created with a 500 ms period. The callback increments a global variable each time it runs. The timer is started once. After 2 seconds, what is the expected value of the variable?
volatile int count = 0; void TimerCallback(TimerHandle_t xTimer) { count++; } int main() { TimerHandle_t timer = xTimerCreate("AutoReloadTimer", pdMS_TO_TICKS(500), pdTRUE, NULL, TimerCallback); xTimerStart(timer, 0); vTaskDelay(pdMS_TO_TICKS(2000)); // What is count now? return 0; }
Auto-reload timers run repeatedly every period.
The timer period is 500 ms and it auto-reloads. After 2 seconds, the callback runs approximately 4 times (2000 ms / 500 ms = 4), so count is 4.
Which statement correctly describes the difference between one-shot and auto-reload timers in FreeRTOS?
Think about how many times the callback runs for each timer type.
One-shot timers run their callback once after the timer period expires. Auto-reload timers run their callback repeatedly at each timer period until stopped.
An auto-reload timer with a 1000 ms period is started. After 2500 ms, the timer is stopped. How many times has the callback run?
volatile int callbackCount = 0; void TimerCallback(TimerHandle_t xTimer) { callbackCount++; } int main() { TimerHandle_t timer = xTimerCreate("AutoReload", pdMS_TO_TICKS(1000), pdTRUE, NULL, TimerCallback); xTimerStart(timer, 0); vTaskDelay(pdMS_TO_TICKS(2500)); xTimerStop(timer, 0); // What is callbackCount now? return 0; }
Count how many full periods fit in 2500 ms before stopping.
The timer period is 1000 ms. After 2500 ms, the callback runs twice (at 1000 ms and 2000 ms). The timer is stopped before the third callback at 3000 ms, so callbackCount is 2.
A one-shot timer with a 1000 ms period is started. Inside its callback, the timer is restarted. What will be the behavior of the callback executions?
volatile int execCount = 0; void TimerCallback(TimerHandle_t xTimer) { execCount++; xTimerStart(xTimer, 0); } int main() { TimerHandle_t timer = xTimerCreate("OneShot", pdMS_TO_TICKS(1000), pdFALSE, NULL, TimerCallback); xTimerStart(timer, 0); vTaskDelay(pdMS_TO_TICKS(3500)); // What is execCount now? return 0; }
Consider that restarting a one-shot timer inside its callback schedules it again.
Each time the callback runs, it restarts the one-shot timer. So the timer triggers every 1000 ms repeatedly. After 3.5 seconds, the callback runs 3 times (at ~1000 ms, ~2000 ms, ~3000 ms), so execCount is 3.
