Timers help you run code after some time. One-shot timers run once, auto-reload timers run repeatedly.
One-shot vs auto-reload timers in FreeRTOS
xTimerCreate( const char * const pcTimerName, const TickType_t xTimerPeriodInTicks, const UBaseType_t uxAutoReload, void * const pvTimerID, TimerCallbackFunction_t pxCallbackFunction );
uxAutoReload is 0 for one-shot, 1 for auto-reload timers.
The callback function runs when the timer expires.
/* One-shot timer example */ xTimerCreate("OneShot", pdMS_TO_TICKS(1000), pdFALSE, NULL, vTimerCallback);
/* Auto-reload timer example */ xTimerCreate("AutoReload", pdMS_TO_TICKS(500), pdTRUE, NULL, vTimerCallback);
This program creates one one-shot timer and one auto-reload timer. The one-shot timer callback runs once after 1 second. The auto-reload timer callback runs every 0.5 seconds. The output shows how many times the callback was called.
#include "FreeRTOS.h" #include "timers.h" #include <stdio.h> void vTimerCallback(TimerHandle_t xTimer) { static int count = 0; count++; printf("Timer callback called %d times\n", count); } void vTaskDelay(const TickType_t xTicksToDelay); int main(void) { TimerHandle_t oneShotTimer = xTimerCreate("OneShot", pdMS_TO_TICKS(1000), pdFALSE, NULL, vTimerCallback); TimerHandle_t autoReloadTimer = xTimerCreate("AutoReload", pdMS_TO_TICKS(500), pdTRUE, NULL, vTimerCallback); if (oneShotTimer != NULL && autoReloadTimer != NULL) { xTimerStart(oneShotTimer, 0); xTimerStart(autoReloadTimer, 0); } // Simulate scheduler running for 3 seconds for (int i = 0; i < 6; i++) { vTaskDelay(pdMS_TO_TICKS(500)); } return 0; } // Dummy vTaskDelay for simulation void vTaskDelay(const TickType_t xTicksToDelay) { // Just a placeholder to simulate delay }
One-shot timers stop automatically after running once.
Auto-reload timers keep running until stopped manually.
Timers run in the context of the timer service task, so keep callbacks short.
One-shot timers run once after a delay.
Auto-reload timers run repeatedly at fixed intervals.
Choose timer type based on whether you want a single or repeated action.
Practice
Solution
Step 1: Understand timer behavior
One-shot timers execute their callback function only once after the specified delay.Step 2: Compare with auto-reload timers
Auto-reload timers automatically restart after each expiration, running repeatedly at fixed intervals.Final Answer:
One-shot timers run once after a delay; auto-reload timers run repeatedly at fixed intervals. -> Option AQuick Check:
One-shot = single run, Auto-reload = repeated runs [OK]
- Confusing which timer runs repeatedly
- Thinking one-shot timers repeat automatically
- Assuming auto-reload timers cannot be stopped
xTimerCreate?Solution
Step 1: Identify auto-reload parameter
The third parameter ofxTimerCreateisuxAutoReload, which should bepdTRUEfor auto-reload timers.Step 2: Check time parameter format
UsepdMS_TO_TICKS(1000)to convert milliseconds to ticks correctly.Final Answer:
xTimerCreate("Timer", pdMS_TO_TICKS(1000), pdTRUE, NULL, TimerCallback); -> Option DQuick Check:
Auto-reload needs pdTRUE and tick conversion [OK]
- Using pdFALSE for auto-reload timers
- Passing raw milliseconds instead of ticks
- Confusing parameter order
TimerHandle_t timer = xTimerCreate("MyTimer", pdMS_TO_TICKS(500), pdFALSE, NULL, TimerCallback);
xTimerStart(timer, 0);What will happen when this timer expires?
Solution
Step 1: Analyze timer creation parameters
The timer is created withpdFALSEfor auto-reload, meaning it is a one-shot timer.Step 2: Understand timer start and expiration
After starting, the timer waits 500 ms, then callsTimerCallbackonce and stops.Final Answer:
The TimerCallback function will be called once after 500 ms. -> Option AQuick Check:
pdFALSE means one-shot = single callback [OK]
- Thinking pdFALSE means repeated calls
- Assuming timer starts immediately
- Confusing pdFALSE with invalid parameter
TimerHandle_t timer = xTimerCreate("Timer", pdMS_TO_TICKS(1000), pdFALSE, NULL, TimerCallback);
xTimerStart(timer, 0);What is the likely error?
Solution
Step 1: Check auto-reload parameter
The timer was created withpdFALSE, which means it is a one-shot timer.Step 2: Understand effect on timer behavior
Because ofpdFALSE, the timer runs only once and does not auto-reload.Final Answer:
The auto-reload parameter is set to pdFALSE instead of pdTRUE. -> Option CQuick Check:
pdFALSE disables auto-reload, causing single run [OK]
- Ignoring auto-reload parameter
- Thinking block time affects reload
- Assuming callback absence causes single run
Solution
Step 1: Identify requirement for repeated action
Toggling LED every 2 seconds indefinitely means the action repeats automatically.Step 2: Choose timer type and period
An auto-reload timer with 2000 ms period will call the toggle function every 2 seconds without manual restart.Final Answer:
Create an auto-reload timer with 2000 ms period to toggle LED automatically. -> Option BQuick Check:
Repeated action = auto-reload timer with correct period [OK]
- Using one-shot timer and forgetting to restart
- Setting wrong timer period
- Confusing toggle frequency with timer period
Ready to prove it?
Pass the test and this pattern moves unknown
