Standby mode behavior in Embedded C - Time & Space Complexity
When working with standby mode in embedded systems, it is important to understand how the program's execution time changes as it waits or checks for events.
We want to know how the time spent in standby grows as the system waits for input or conditions.
Analyze the time complexity of the following code snippet.
void standby_mode(int timeout) {
int counter = 0;
while(counter < timeout) {
if(check_event()) {
break;
}
counter++;
}
}
This code waits in standby, checking for an event repeatedly until a timeout or event occurs.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that checks for an event repeatedly.
- How many times: Up to
timeouttimes, or fewer if the event occurs early.
As the timeout value increases, the number of checks grows roughly the same amount.
| Input Size (timeout) | Approx. Operations (event checks) |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The number of operations grows directly with the timeout value.
Time Complexity: O(n)
This means the time spent waiting grows linearly with the timeout value.
[X] Wrong: "The standby loop runs instantly and does not depend on timeout."
[OK] Correct: The loop actually runs once per check until timeout or event, so more timeout means more checks and more time.
Understanding how loops behave in standby or waiting code helps you reason about system responsiveness and efficiency in real embedded projects.
"What if we replaced the while loop with an interrupt-driven event check? How would the time complexity change?"
Practice
standby mode?Solution
Step 1: Understand standby mode function
Standby mode is designed to reduce power consumption by putting the device into a very low power state.Step 2: Compare options with standby purpose
Only To save power by entering a deep sleep state correctly describes saving power by deep sleep. Other options describe unrelated functions.Final Answer:
To save power by entering a deep sleep state -> Option AQuick Check:
Standby mode = power saving [OK]
- Confusing standby with reset or speed boost
- Thinking standby enables all peripherals
- Assuming standby clears registers
Solution
Step 1: Identify correct register settings for standby
Setting PWR_CR_PDDS bit and SLEEPDEEP bit enables standby mode deep sleep.Step 2: Check the wait instruction
__WFI() (Wait For Interrupt) is used to enter sleep mode properly.Final Answer:
PWR->CR |= PWR_CR_PDDS; SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; __WFI(); -> Option DQuick Check:
Set PDDS + SLEEPDEEP + __WFI() = standby [OK]
- Clearing PDDS or SLEEPDEEP bits disables standby
- Using __WFE() instead of __WFI() incorrectly
- Not setting SLEEPDEEP bit before sleep
volatile int flag = 0;
void enter_standby() {
PWR->CR |= PWR_CR_PDDS;
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
__WFI();
flag = 1;
}
int main() {
enter_standby();
if(flag) {
printf("Woke up from standby\n");
} else {
printf("Still running\n");
}
return 0;
}Solution
Step 1: Understand standby mode effect on program flow
Standby mode resets the device on wake-up, so code after __WFI() is not executed.Step 2: Analyze flag variable and output
Since flag is set after __WFI(), but device resets, flag remains 0 and main restarts, so output will be "Still running" because flag is 0 after reset.Final Answer:
Still running -> Option CQuick Check:
Standby resets device, code after __WFI() not run, flag remains 0 [OK]
- Assuming code after __WFI() runs after wake-up
- Ignoring device reset on standby wake-up
- Confusing volatile with persistence across reset
void enter_standby() {
PWR->CR |= PWR_CR_PDDS;
SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
__WFI();
}Solution
Step 1: Check SLEEPDEEP bit usage
SLEEPDEEP bit must be set to enter deep sleep (standby). Clearing it disables deep sleep.Step 2: Verify other settings
PDDS bit is set correctly; __WFI() is correct; power clock enabling is not shown but not always mandatory here.Final Answer:
SLEEPDEEP bit is cleared, so device does not enter deep sleep -> Option BQuick Check:
SLEEPDEEP cleared = no deep sleep [OK]
- Clearing SLEEPDEEP bit disables standby
- Confusing __WFI() with __WFE()
- Forgetting to set PDDS bit
Solution
Step 1: Configure wake-up source before standby
External interrupt line and wake-up pin must be enabled before entering standby.Step 2: Set standby mode bits and enter sleep
Set PDDS and SLEEPDEEP bits to enable standby, then call __WFI() to enter standby mode.Final Answer:
Enable external interrupt line; configure wake-up pin; set PDDS and SLEEPDEEP bits; call __WFI() -> Option AQuick Check:
Setup wake-up + set bits + __WFI() = standby with wake-up [OK]
- Enabling interrupts after entering standby
- Clearing PDDS bit disables standby
- Using __WFE() without proper wake-up config
Ready to prove it?
Pass the test and this pattern moves unknown
