Idle mode behavior in Embedded C - Time & Space Complexity
When a program enters idle mode, it waits without doing much work. We want to understand how this waiting affects the program's running time.
How does the program's time change when it spends time idling?
Analyze the time complexity of the following code snippet.
void idle_mode(int wait_cycles) {
while(wait_cycles > 0) {
// CPU does nothing but waits
wait_cycles--;
}
}
This code makes the CPU wait by counting down from a number until zero, simulating idle time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that decreases
wait_cyclesby one each time. - How many times: Exactly as many times as the initial value of
wait_cycles.
As the number of wait cycles increases, the program spends more time looping.
| Input Size (wait_cycles) | Approx. Operations |
|---|---|
| 10 | 10 loop steps |
| 100 | 100 loop steps |
| 1000 | 1000 loop steps |
Pattern observation: The time grows directly with the number of wait cycles; doubling wait cycles doubles the time.
Time Complexity: O(n)
This means the time the program spends idling grows in a straight line with the number of wait cycles.
[X] Wrong: "Idle mode takes constant time no matter how long we wait."
[OK] Correct: Even though the CPU is not doing useful work, the loop still runs once per wait cycle, so time grows with the wait length.
Understanding idle mode time helps you reason about how waiting affects program speed, a useful skill when working with embedded systems or low-power devices.
"What if we replaced the while loop with a hardware timer interrupt that pauses the CPU without looping? How would the time complexity change?"
Practice
Solution
Step 1: Understand idle mode function
Idle mode pauses the CPU to save power when it is not performing tasks.Step 2: Compare options with idle mode purpose
Only To save power by pausing the CPU when it is not busy correctly states that idle mode saves power by pausing the CPU.Final Answer:
To save power by pausing the CPU when it is not busy -> Option CQuick Check:
Idle mode = power saving by pausing CPU [OK]
- Thinking idle mode speeds up CPU
- Confusing idle mode with reset
- Believing interrupts are disabled permanently
Solution
Step 1: Recall instruction for idle mode
The WFI (Wait For Interrupt) instruction is used to enter idle mode.Step 2: Verify other instructions
NOP does nothing, SVC triggers supervisor call, MOV moves data; none enter idle mode.Final Answer:
WFI -> Option AQuick Check:
Idle mode instruction = WFI [OK]
- Choosing NOP thinking it pauses CPU
- Confusing SVC with idle mode instruction
- Using MOV as idle mode command
__asm__("WFI");
// CPU is idle here until interrupt occursSolution
Step 1: Understand WFI instruction effect
WFI causes CPU to enter idle mode and wait for an interrupt to wake it.Step 2: Analyze other options
CPU does not continue running instructions without pause, reset immediately, or disable all interrupts on WFI.Final Answer:
CPU enters idle mode and waits for an interrupt -> Option DQuick Check:
WFI = idle mode wait for interrupt [OK]
- Assuming CPU runs instructions after WFI immediately
- Thinking WFI resets CPU
- Believing WFI disables interrupts
void enter_idle() {
__asm__("WFI");
__disable_irq();
}Solution
Step 1: Understand WFI and interrupt disabling order
WFI waits for interrupt to wake CPU, but interrupts are disabled after WFI.Step 2: Analyze impact of disabling interrupts after WFI
If interrupts are disabled after WFI, CPU may not wake properly because interrupts are blocked.Final Answer:
Interrupts are disabled after WFI, so CPU may never wake -> Option BQuick Check:
Disabling interrupts after WFI blocks wake-up [OK]
- Disabling interrupts after WFI instead of before
- Thinking WFI syntax is wrong
- Expecting function to return value unnecessarily
if (no_tasks_pending()) {
__disable_irq();
if (no_tasks_pending()) {
__asm__("WFI");
}
__enable_irq();
}Solution
Step 1: Analyze double check of no_tasks_pending()
Disabling interrupts then checking again ensures no tasks arrived during interrupt disable.Step 2: Confirm interrupts are re-enabled after WFI
Interrupts are enabled after WFI, so CPU can wake properly.Final Answer:
This code safely enters idle mode only if no tasks are pending -> Option AQuick Check:
Double check + disable/enable IRQ = safe idle entry [OK]
- Not re-enabling interrupts after WFI
- Calling WFI before disabling interrupts
- Checking no_tasks_pending only once
Ready to prove it?
Pass the test and this pattern moves unknown
