Wake-up sources configuration in Embedded C - Time & Space Complexity
When configuring wake-up sources in embedded systems, it's important to know how the time to set them up changes as the number of sources grows.
We want to understand how the setup time scales when more wake-up sources are added.
Analyze the time complexity of the following code snippet.
void configureWakeupSources(int *sources, int count) {
for (int i = 0; i < count; i++) {
enableWakeupSource(sources[i]);
}
}
// Enables a single wake-up source
void enableWakeupSource(int source) {
// Hardware register setup here
}
This code sets up each wake-up source one by one by calling a function for each source.
- Primary operation: Loop over the array of wake-up sources.
- How many times: Exactly once for each source, so
counttimes.
As the number of wake-up sources increases, the time to configure them grows in a straight line.
| Input Size (count) | Approx. Operations |
|---|---|
| 10 | 10 calls to enableWakeupSource |
| 100 | 100 calls to enableWakeupSource |
| 1000 | 1000 calls to enableWakeupSource |
Pattern observation: The setup time grows directly with the number of sources; doubling sources doubles the work.
Time Complexity: O(n)
This means the time to configure wake-up sources grows linearly with how many sources you have.
[X] Wrong: "Configuring multiple wake-up sources takes the same time no matter how many there are."
[OK] Correct: Each source requires a separate setup step, so more sources mean more work and more time.
Understanding how setup time grows with input size helps you explain system behavior clearly and shows you can think about efficiency in embedded programming.
"What if we batch configure all wake-up sources in one hardware call instead of individually? How would the time complexity change?"
Practice
Solution
Step 1: Understand wake-up sources
Wake-up sources are hardware events that can wake a device from sleep mode, such as GPIO interrupts or timers.Step 2: Identify valid wake-up sources
GPIO pin interrupts are commonly used as wake-up sources because they detect external signals.Final Answer:
GPIO pin interrupt -> Option AQuick Check:
Wake-up source = GPIO pin interrupt [OK]
- Confusing software operations with hardware wake-up sources
- Thinking file operations can wake embedded devices
- Assuming calculations can trigger wake-up
Solution
Step 1: Recognize common embedded C macros
Macros often use uppercase and underscores, like GPIO_WAKEUP_ENABLE(pin).Step 2: Match syntax style
GPIO_WAKEUP_ENABLE(5); matches typical embedded C macro style for enabling wake-up on GPIO pin 5.Final Answer:
GPIO_WAKEUP_ENABLE(5); -> Option BQuick Check:
Macro style = GPIO_WAKEUP_ENABLE(5) [OK]
- Using lowercase function names not defined in embedded C
- Confusing parameter formats
- Missing parentheses or semicolons
void configure_wakeup() {
WAKEUP_ENABLE_TIMER(1000); // 1000 ms
WAKEUP_ENABLE_GPIO(3);
}
int main() {
configure_wakeup();
if (is_wakeup_source(GPIO_3)) {
printf("Wakeup by GPIO 3\n");
} else if (is_wakeup_source(TIMER)) {
printf("Wakeup by Timer\n");
} else {
printf("No wakeup source\n");
}
return 0;
}Solution
Step 1: Analyze function and macros usage
The code uses WAKEUP_ENABLE_TIMER and WAKEUP_ENABLE_GPIO macros but does not define or include them, causing compilation errors.Step 2: Check function calls and conditions
Functions like is_wakeup_source and constants GPIO_3, TIMER are undefined, leading to compilation failure.Final Answer:
Compilation error -> Option DQuick Check:
Undefined macros/functions cause compilation error [OK]
- Assuming macros are predefined without headers
- Ignoring missing function definitions
- Confusing runtime output with compile errors
void setup_wakeup() {
WAKEUP_ENABLE_GPIO(2);
WAKEUP_ENABLE_GPIO(2);
WAKEUP_ENABLE_TIMER(500);
}Solution
Step 1: Review GPIO wake-up configuration
The code enables GPIO pin 2 twice, which can cause conflicts or redundant configuration.Step 2: Check timer configuration and function structure
Timer value 500 ms is valid; no return needed for void function.Final Answer:
Duplicate GPIO wake-up enable causes conflict -> Option AQuick Check:
Duplicate GPIO enable = conflict [OK]
- Thinking timer value is invalid
- Expecting return in void function
- Ignoring duplicate hardware config issues
Solution
Step 1: Understand separate wake-up source configuration
GPIO and timer wake-up sources are usually enabled with separate calls specifying pin and time in milliseconds.Step 2: Evaluate options for correctness
WAKEUP_ENABLE_GPIO(4); WAKEUP_ENABLE_TIMER(2000); correctly enables GPIO pin 4 and timer with 2000 ms (2 seconds). WAKEUP_ENABLE_TIMER(2); WAKEUP_ENABLE_GPIO(4); uses 2 (seconds) instead of milliseconds. WAKEUP_ENABLE_GPIO(4, 2000); incorrectly combines parameters. WAKEUP_CONFIG(GPIO4 | TIMER_2S); uses an undefined macro.Final Answer:
WAKEUP_ENABLE_GPIO(4); WAKEUP_ENABLE_TIMER(2000); -> Option CQuick Check:
Separate calls with correct units = WAKEUP_ENABLE_GPIO(4); WAKEUP_ENABLE_TIMER(2000); [OK]
- Using seconds instead of milliseconds for timer
- Combining GPIO and timer in one call
- Using undefined macros
Ready to prove it?
Pass the test and this pattern moves unknown
