What if your program could handle waiting and repeating tasks all by itself, perfectly timed every time?
One-shot vs auto-reload timers in FreeRTOS - When to Use Which
Imagine you want to turn on a light for exactly 5 seconds and then turn it off automatically. You try to do this by checking the time repeatedly in your program and turning the light off when 5 seconds pass.
This manual way means your program is busy checking the time all the time. It wastes effort and can miss other important tasks. Also, if you want the light to turn on and off repeatedly every 5 seconds, you have to write extra code to reset the timer again and again, which is easy to mess up.
One-shot and auto-reload timers in FreeRTOS handle this for you. A one-shot timer runs once and stops, perfect for single delays. An auto-reload timer runs repeatedly at set intervals without extra code. This frees your program to do other things while the timer works in the background.
start_time = get_time() while (get_time() - start_time < 5000) { // wait } turn_off_light();
TimerHandle_t timer = xTimerCreate("OneShot", pdMS_TO_TICKS(5000), pdFALSE, 0, turnOffLightCallback); xTimerStart(timer, 0);
Timers let your program handle delays and repeated actions easily and reliably, without wasting effort or missing other tasks.
In a smart home, you can use a one-shot timer to turn off a heater after 10 minutes, or an auto-reload timer to check the temperature every minute automatically.
Manual timing wastes effort and can miss tasks.
One-shot timers run once; auto-reload timers run repeatedly.
Timers free your program to do more while waiting.
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
