One-shot vs auto-reload timers in FreeRTOS - Performance Comparison
When using timers in FreeRTOS, it is important to understand how their execution time changes as the number of timers grows.
We want to know how the timer type affects the cost of running timer callbacks repeatedly.
Analyze the time complexity of these timer callback executions.
// Create a one-shot timer
TimerHandle_t oneShotTimer = xTimerCreate("OneShot", pdMS_TO_TICKS(1000), pdFALSE, NULL, callback);
// Create an auto-reload timer
TimerHandle_t autoReloadTimer = xTimerCreate("AutoReload", pdMS_TO_TICKS(1000), pdTRUE, NULL, callback);
// Start timers
xTimerStart(oneShotTimer, 0);
xTimerStart(autoReloadTimer, 0);
// Timer callback function
void callback(TimerHandle_t xTimer) {
// Code to run when timer expires
}
This code creates two timers: one that runs once and one that runs repeatedly every interval.
Look at what repeats when timers run.
- Primary operation: The callback function execution each time a timer expires.
- How many times: One-shot timer runs once; auto-reload timer runs repeatedly at fixed intervals.
Imagine you have more timers running at once.
| Number of Timers (n) | Approx. Callback Executions per Interval |
|---|---|
| 10 | 10 (if all auto-reload) |
| 100 | 100 |
| 1000 | 1000 |
Each timer callback runs once per interval, so total executions grow directly with the number of timers.
Time Complexity: O(n)
This means the total work grows linearly with the number of timers running their callbacks.
[X] Wrong: "One-shot timers are always faster because they run only once."
[OK] Correct: While one-shot timers run once, if you have many timers or restart them often, the total callback executions can still add up and affect performance.
Understanding how timer callbacks scale helps you design responsive and efficient real-time systems, a key skill in embedded programming.
What if we changed all timers from auto-reload to one-shot timers that restart themselves manually? How would the time complexity change?
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
