Timer for periodic status reporting in FreeRTOS - Time & Space Complexity
We want to understand how the time cost changes when using a timer for periodic status reporting in FreeRTOS.
Specifically, how often the timer callback runs affects the total work done over time.
Analyze the time complexity of the following code snippet.
// Create a timer that calls StatusReport every 1000 ms
TimerHandle_t xTimer = xTimerCreate("StatusTimer", pdMS_TO_TICKS(1000), pdTRUE, NULL, StatusReport);
// Start the timer
xTimerStart(xTimer, 0);
// Timer callback function
void StatusReport(TimerHandle_t xTimer) {
// Code to send status update
}
This code sets up a timer that calls a status reporting function every second.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The timer callback function
StatusReportruns periodically. - How many times: It runs once every 1000 milliseconds, repeating indefinitely.
The total work done grows linearly with the number of timer periods that pass.
| Input Size (n seconds) | Approx. Operations (calls to StatusReport) |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The number of operations grows directly with time passed, increasing steadily.
Time Complexity: O(n)
This means the total work grows in direct proportion to the time the timer runs.
[X] Wrong: "The timer callback runs only once, so time cost is constant."
[OK] Correct: The timer is set to repeat, so the callback runs many times over time, increasing total work.
Understanding how periodic timers affect execution time helps you design responsive and efficient embedded systems.
"What if the timer period changed from 1000 ms to 500 ms? How would the time complexity change?"
Practice
Solution
Step 1: Understand timer functionality in FreeRTOS
Timers in FreeRTOS are designed to call a function repeatedly or once after a delay.Step 2: Relate to periodic status reporting
Periodic status reporting requires running code repeatedly at fixed intervals, which timers enable.Final Answer:
To run a function repeatedly at fixed time intervals -> Option CQuick Check:
Timer = repeated function calls [OK]
- Confusing timers with tasks
- Thinking timers pause the system
- Assuming timers allocate memory
statusTimer with a 1000 ms period and auto-reload enabled?Solution
Step 1: Identify correct period conversion
FreeRTOS timers use ticks, so pdMS_TO_TICKS(1000) converts 1000 ms to ticks correctly.Step 2: Check auto-reload parameter
pdTRUE enables auto-reload for periodic timers, which is needed here.Final Answer:
xTimerCreate("statusTimer", pdMS_TO_TICKS(1000), pdTRUE, NULL, TimerCallback); -> Option AQuick Check:
Use pdMS_TO_TICKS and pdTRUE for periodic timers [OK]
- Using raw milliseconds instead of ticks
- Setting auto-reload to pdFALSE for periodic timer
- Confusing pdTRUE and pdFALSE values
void TimerCallback(TimerHandle_t xTimer) {
printf("Status: OK\n");
}
int main() {
TimerHandle_t statusTimer = xTimerCreate("statusTimer", pdMS_TO_TICKS(1000), pdTRUE, NULL, TimerCallback);
xTimerStart(statusTimer, 0);
vTaskStartScheduler();
return 0;
}Solution
Step 1: Analyze timer creation and start
The timer is created with pdTRUE for auto-reload and started immediately, so it runs repeatedly.Step 2: Understand callback behavior
The callback prints "Status: OK" each time the timer expires, which is every 1000 ms (1 second).Final Answer:
Status: OK printed every second repeatedly -> Option AQuick Check:
Auto-reload timer prints every second [OK]
- Assuming timer runs only once
- Forgetting to start the timer
- Confusing callback parameters
TimerHandle_t statusTimer = xTimerCreate("statusTimer", 1000, pdTRUE, NULL, TimerCallback);
xTimerStart(statusTimer, 0);Solution
Step 1: Check timer period parameter
The period is given as 1000 (milliseconds) but FreeRTOS expects ticks, so pdMS_TO_TICKS is needed.Step 2: Verify other parameters
The timer name as string is allowed, auto-reload pdTRUE is correct, and callback is assumed defined.Final Answer:
Timer period should be converted to ticks using pdMS_TO_TICKS -> Option DQuick Check:
Always convert ms to ticks with pdMS_TO_TICKS [OK]
- Passing raw milliseconds instead of ticks
- Confusing auto-reload true/false
- Assuming timer name must be NULL
Solution
Step 1: Understand timer types and requirements
Auto-reload timers run repeatedly; one-shot timers run once. We need repeated calls every 2 seconds.Step 2: Implement counting and stopping logic
Use an auto-reload timer with 2-second period, count callback calls, and stop timer after 5 executions.Final Answer:
Create an auto-reload timer with 2-second period and count executions in callback, then stop timer after 5 times -> Option BQuick Check:
Auto-reload + count + stop after 5 [OK]
- Using one-shot timer without manual restarts
- Incorrect timer period causing wrong intervals
- Not stopping timer after desired count
Ready to prove it?
Pass the test and this pattern moves unknown
