Starting and stopping timers in FreeRTOS - Time & Space Complexity
When working with timers in FreeRTOS, it's important to understand how the time to start and stop timers changes as you use more timers.
We want to know how the work grows when starting or stopping timers as the number of timers increases.
Analyze the time complexity of the following code snippet.
// Create a timer
TimerHandle_t timer = xTimerCreate("Timer", pdMS_TO_TICKS(1000), pdFALSE, NULL, TimerCallback);
// Start the timer
xTimerStart(timer, 0);
// Stop the timer
xTimerStop(timer, 0);
This code creates a single timer, then starts and stops it once.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Starting or stopping a single timer involves checking and updating timer lists internally.
- How many times: Each start or stop call runs once per timer operation.
When you have more timers, starting or stopping one timer requires searching or updating internal lists that hold all timers.
| Input Size (number of timers) | Approx. Operations |
|---|---|
| 10 | About 10 checks to find or update the timer |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows roughly in direct proportion to the number of timers you have.
Time Complexity: O(n)
This means starting or stopping a timer takes longer as you have more timers, growing in a straight line with the number of timers.
[X] Wrong: "Starting or stopping a timer always takes the same time, no matter how many timers exist."
[OK] Correct: Internally, FreeRTOS must find and update the timer in a list, so more timers mean more work.
Understanding how timer operations scale helps you design efficient real-time systems and shows you can think about performance in embedded programming.
"What if FreeRTOS used a different data structure for timers that allowed faster lookup? How would the time complexity change?"
Practice
xTimerCreate do in FreeRTOS?Solution
Step 1: Understand the purpose of
This function is used to create a software timer in FreeRTOS but it does not start the timer automatically.xTimerCreateStep 2: Differentiate from other timer functions
Starting a timer requiresxTimerStart, stopping requiresxTimerStop, and deleting requiresxTimerDelete.Final Answer:
Creates a timer but does not start it automatically -> Option AQuick Check:
xTimerCreateonly creates timer [OK]
- Thinking
xTimerCreatestarts the timer automatically - Confusing
xTimerStartwith creation - Assuming timer is running immediately after creation
xTimer?Solution
Step 1: Identify the function to start a timer
The functionxTimerStartis used to start a timer in FreeRTOS.Step 2: Check the syntax
The correct syntax isxTimerStart(xTimer, 0);where the second parameter is the block time.Final Answer:
xTimerStart(xTimer, 0);-> Option DQuick Check:
Start timer =xTimerStartcall [OK]
xTimerStart, not create or stop [OK]- Using
xTimerCreateto start a timer - Confusing stop and delete functions with start
- Missing the block time parameter
int counter = 0;
TimerHandle_t xTimer = xTimerCreate("Timer", pdMS_TO_TICKS(1000), pdTRUE, NULL, timerCallback);
xTimerStart(xTimer, 0);
xTimerStop(xTimer, 0);
// Assume timerCallback increments counterSolution
Step 1: Analyze timer start and stop sequence
The timer is started and then immediately stopped before it can expire and call the callback.Step 2: Understand timer callback execution
Since the timer period is 1000 ms and it is stopped immediately, the callback does not get a chance to run, so the counter stays 0.Final Answer:
counter will remain 0 -> Option AQuick Check:
Immediate stop prevents callback = counter 0 [OK]
- Assuming callback runs immediately on start
- Ignoring timer period delay
- Confusing start and stop timing effects
TimerHandle_t xTimer = xTimerCreate("Timer", pdMS_TO_TICKS(500), pdFALSE, NULL, timerCallback);
xTimerStop(xTimer, 1000);Solution
Step 1: Check timer lifecycle
The timer is created but never started before callingxTimerStop.Step 2: Understand stopping a timer
Stopping a timer that is not running has no effect and may cause unexpected behavior.Final Answer:
The timer was never started before stopping -> Option BQuick Check:
Stop requires running timer first [OK]
- Stopping timer without starting it
- Assuming block time causes error
- Misunderstanding timer handle validity
Solution
Step 1: Understand timer creation parameters
SettingpdTRUEmakes the timer periodic (auto-reload), but it does not start automatically; you must callxTimerStart.Step 2: Implement stopping after 5 executions
Use a counter inside the callback function to track executions and callxTimerStopafter the 5th execution.Final Answer:
Create the timer withpdTRUEfor auto-reload, start it manually, and in the callback count executions and stop after 5 -> Option CQuick Check:
Periodic timer + manual start + callback stop after 5 [OK]
- Assuming
xTimerCreatestarts timer automatically - Using one-shot timer for periodic behavior
- Not counting executions in callback
Ready to prove it?
Pass the test and this pattern moves unknown
