Low-power design patterns in Embedded C - Time & Space Complexity
When designing low-power embedded systems, it's important to understand how the program's running time changes as input or workload grows.
We want to see how the power-saving patterns affect the number of operations as the system runs longer or handles more tasks.
Analyze the time complexity of the following code snippet.
void low_power_loop(int n) {
for (int i = 0; i < n; i++) {
// Enter low power mode
enter_low_power_mode();
// Wake up and do a quick task
perform_task();
}
}
This code runs a loop n times, each time entering a low power mode and then performing a small task after waking up.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop running from 0 to n-1.
- How many times: Exactly n times, each iteration calls low power mode and task functions.
As n increases, the number of times the loop runs grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 low power cycles and tasks |
| 100 | About 100 low power cycles and tasks |
| 1000 | About 1000 low power cycles and tasks |
Pattern observation: The work grows steadily and directly with the input size n.
Time Complexity: O(n)
This means the time to complete the loop grows in a straight line as n gets bigger.
[X] Wrong: "Entering low power mode reduces the number of loop iterations or operations."
[OK] Correct: Low power mode saves energy but does not reduce how many times the loop runs or tasks execute.
Understanding how loops and power-saving calls scale helps you explain embedded system efficiency clearly and confidently.
"What if the perform_task() function itself contains a loop that runs m times? How would the overall time complexity change?"
Practice
Solution
Step 1: Understand low-power design patterns
These patterns focus on saving battery by reducing power use in the CPU and peripherals.Step 2: Compare options to the purpose
Only To reduce battery consumption by putting the CPU and peripherals into sleep modes talks about reducing battery consumption by sleep modes, which matches the purpose.Final Answer:
To reduce battery consumption by putting the CPU and peripherals into sleep modes -> Option AQuick Check:
Low-power design = save battery by sleep modes [OK]
- Confusing low-power with performance improvement
- Thinking it adds features instead of saving power
- Assuming it improves UI responsiveness
Solution
Step 1: Identify the standard CPU sleep instruction
The __WFI() instruction is a common CPU instruction to wait for interrupt and enter sleep mode safely.Step 2: Check other options
Other options like sleep(), power_down(), enter_sleep_mode() are not standard embedded C instructions.Final Answer:
__WFI(); -> Option DQuick Check:
Use __WFI() to sleep CPU [OK]
- Using non-standard function names
- Forgetting the double underscore in __WFI()
- Confusing sleep() with standard CPU instructions
void enter_low_power() {
disable_peripherals();
__WFI();
enable_peripherals();
}
What is the expected behavior when this function runs?Solution
Step 1: Analyze the function steps
First, peripherals are disabled to save power, then __WFI() puts CPU to sleep waiting for interrupt, finally peripherals are re-enabled after waking.Step 2: Understand expected behavior
The CPU sleeps until an interrupt wakes it, peripherals are off during sleep to save power, then turned back on.Final Answer:
The CPU sleeps until an interrupt occurs, peripherals are disabled before sleep and enabled after wake-up -> Option AQuick Check:
Disable peripherals + __WFI() = sleep with low power [OK]
- Assuming peripherals stay enabled during sleep
- Thinking CPU does not sleep after __WFI()
- Confusing function behavior with syntax errors
void sleep_mode() {
__WFI;
}
What is the error and how to fix it?Solution
Step 1: Identify syntax error in __WFI usage
The code uses __WFI without parentheses, which is incorrect syntax for a function-like instruction.Step 2: Correct the syntax
Add parentheses to call __WFI as a function: __WFI(); fixes the error.Final Answer:
__WFI is missing parentheses; fix by writing __WFI(); -> Option CQuick Check:
__WFI() needs parentheses to call [OK]
- Omitting parentheses on __WFI
- Replacing __WFI with non-standard functions
- Ignoring peripheral disable step (not syntax error here)
are_peripherals_active() returning 1 if active, 0 if not, and __WFI() to sleep, which code snippet correctly implements this logic?Solution
Step 1: Understand the condition to sleep
The CPU should sleep only if no peripherals are active, so condition is peripherals inactive (0).Step 2: Match condition with code
Using if (!are_peripherals_active()) means if peripherals are not active, then call __WFI() to sleep.Final Answer:
if (!are_peripherals_active()) { __WFI(); } -> Option BQuick Check:
Sleep only if peripherals inactive = if (!active) [OK]
- Sleeping when peripherals are active
- Using loops causing repeated sleep calls
- Confusing condition negation
Ready to prove it?
Pass the test and this pattern moves unknown
