Why watchdog timer is needed in Embedded C - Performance Analysis
We want to understand how often a watchdog timer checks the system to keep it running well.
How does the system's checking cost grow as the program runs longer or gets more complex?
Analyze the time complexity of the following embedded C code snippet.
// Simple watchdog timer reset example
void watchdog_reset() {
// Reset the watchdog timer counter
WDT->RESET = 1;
}
int main() {
while(1) {
// Main program tasks
perform_tasks();
watchdog_reset();
}
}
This code resets the watchdog timer repeatedly inside an infinite loop to prevent system reset.
Look at what repeats in the code.
- Primary operation: The infinite loop runs forever.
- How many times: The watchdog reset happens every loop cycle, repeating endlessly.
The loop runs continuously, so the number of operations grows directly with time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 (cycles) | 10 watchdog resets |
| 100 (cycles) | 100 watchdog resets |
| 1000 (cycles) | 1000 watchdog resets |
Pattern observation: The number of resets grows linearly with the number of loop cycles.
Time Complexity: O(n)
This means the system checks and resets the watchdog timer once every cycle, growing steadily as the program runs.
[X] Wrong: "The watchdog timer only needs to reset once at the start."
[OK] Correct: The watchdog must be reset regularly to show the system is alive; otherwise, it will reset the system thinking it is stuck.
Understanding how often the watchdog timer runs helps you explain system reliability and timing in embedded systems clearly.
"What if the watchdog reset was called only every 10 cycles instead of every cycle? How would the time complexity change?"