Watchdog timer operation in Embedded C - Time & Space Complexity
We want to understand how the time taken by a watchdog timer operation changes as the program runs.
Specifically, how often the watchdog reset happens affects the program's timing.
Analyze the time complexity of the following code snippet.
void watchdog_reset() {
WDT->CLEAR = 0xA5; // Reset watchdog timer
}
void main_loop(int n) {
for (int i = 0; i < n; i++) {
// Some processing
watchdog_reset();
}
}
This code resets the watchdog timer inside a loop that runs n times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop running n times calling
watchdog_reset(). - How many times: Exactly n times, once per loop iteration.
As n grows, the number of times the watchdog reset happens grows linearly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 watchdog resets |
| 100 | 100 watchdog resets |
| 1000 | 1000 watchdog resets |
Pattern observation: The operations increase directly with n, doubling n doubles the work.
Time Complexity: O(n)
This means the time taken grows in direct proportion to the number of loop iterations.
[X] Wrong: "The watchdog reset happens only once, so time is constant."
[OK] Correct: The reset is inside the loop, so it happens every time the loop runs, making time grow with n.
Understanding how loops affect timing is key in embedded systems, especially for safety features like watchdog timers.
"What if the watchdog reset was moved outside the loop? How would the time complexity change?"
Practice
watchdog timer in embedded systems?Solution
Step 1: Understand watchdog timer function
A watchdog timer monitors the program to detect if it stops responding or freezes.Step 2: Identify watchdog timer action
If the program fails to reset the watchdog timer in time, the watchdog resets the system to recover from the freeze.Final Answer:
To reset the system if the program stops responding -> Option CQuick Check:
Watchdog timer resets system on freeze = D [OK]
- Thinking watchdog speeds up CPU
- Confusing watchdog with memory storage
- Assuming watchdog provides power
Solution
Step 1: Recall standard watchdog reset function
In many embedded C libraries, the function to reset the watchdog timer iswdt_reset().Step 2: Check other options
Other options likewatchdog_reset(),reset_watchdog(), andkick_watchdog()are not standard function names and will cause errors.Final Answer:
wdt_reset(); -> Option DQuick Check:
Standard watchdog reset function = wdt_reset() [OK]
- Using non-standard function names
- Forgetting parentheses in function call
- Confusing reset with initialization
void main() {
setup_watchdog(5); // 5 seconds timeout
while(1) {
// No watchdog reset here
}
printf("Running");
}Solution
Step 1: Analyze watchdog setup and loop
The watchdog is set with a 5-second timeout, but inside the infinite loop, the watchdog reset function is never called.Step 2: Understand watchdog behavior on timeout
Since the watchdog is not reset, after 5 seconds it triggers a system reset. Theprintfstatement is never reached.Final Answer:
The program resets after 5 seconds and never prints 'Running' -> Option BQuick Check:
No watchdog reset causes reset before printf = A [OK]
- Assuming printf runs before reset
- Thinking program runs normally without reset
- Confusing compile error with runtime reset
void main() {
setup_watchdog(10);
while(1) {
if (some_condition) {
wdt_reset();
}
}
}Solution
Step 1: Review watchdog reset condition
The watchdog resetwdt_reset()is called only ifsome_conditionis true, so it may not be called regularly.Step 2: Understand watchdog requirement
Watchdog must be reset regularly regardless of conditions to prevent system reset. Conditional reset risks unexpected resets.Final Answer:
Watchdog is reset only conditionally, may cause unexpected reset -> Option AQuick Check:
Conditional watchdog reset risks reset = C [OK]
- Ignoring conditional reset risk
- Thinking timeout length is error
- Assuming missing initialization
Solution
Step 1: Set watchdog timeout to desired reset delay
To reset after 3 seconds of no reset, watchdog timeout must be set to 3 seconds.Step 2: Reset watchdog regularly within timeout
Callwdt_reset()regularly before 3 seconds elapse to prevent reset during normal operation.Final Answer:
Initialize watchdog with 3 seconds timeout and call wdt_reset() regularly within 3 seconds -> Option AQuick Check:
Timeout = 3s and regular reset = B [OK]
- Setting timeout longer than reset interval
- Resetting watchdog less frequently than timeout
- Not initializing watchdog at all
Ready to prove it?
Pass the test and this pattern moves unknown
