0
0
Embedded Cprogramming~5 mins

Why watchdog timer is needed in Embedded C - Performance Analysis

Choose your learning style9 modes available
Time Complexity: Why watchdog timer is needed
O(n)
Understanding Time Complexity

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?

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

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.

Final Time Complexity

Time Complexity: O(n)

This means the system checks and resets the watchdog timer once every cycle, growing steadily as the program runs.

Common Mistake

[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.

Interview Connect

Understanding how often the watchdog timer runs helps you explain system reliability and timing in embedded systems clearly.

Self-Check

"What if the watchdog reset was called only every 10 cycles instead of every cycle? How would the time complexity change?"