Watchdog Timers: Why Doing It Right Matter More Than You Think!
Why Calculating the Watchdog Period is Challenging
In the world of embedded systems, reliability is paramount. One of the critical tools engineers use to ensure system stability is the watchdog timer (WDT). While setting up a watchdog timer may seem straightforward, calculating the correct watchdog period is far more complex than it appears. This blog will cover why the watchdog is required, the difficulties in calculating the right period, how testing impacts the timer, key considerations, and what to do when the watchdog triggers—both in development and manufacturing stages.
1. Why Is a Watchdog Timer Required?
The watchdog timer serves as a safeguard against system failures or software malfunctions. In an embedded system, there are instances where software may crash, enter infinite loops, or encounter deadlocks. Without a mechanism to recover, such failures could make the system unresponsive, leading to costly consequences, especially in safety-critical applications like automotive, medical devices, and industrial automation.
The watchdog timer ensures that if the system stops responding, it will automatically reset itself to avoid further issues. It essentially functions as a reset button, constantly monitoring the health of the system and expecting a regular "kick" (a signal that the system is still functioning). If the kick doesn’t happen within a set period, the watchdog assumes the system has malfunctioned and triggers a reset.
2. Why Is It Difficult to Calculate the Watchdog Period?
Calculating the correct watchdog period is a balancing act that comes with challenges. If the period is too short, you risk premature resets (false positives) when the system may simply be running a longer task. On the other hand, setting it too long may delay detection of an actual system failure, reducing its effectiveness. The difficulty lies in accounting for real-world factors that affect system timing:
Task Execution Variability: In systems with multiple processes, some tasks may take longer to execute than others. You need to ensure that the watchdog period accommodates the longest task without triggering a false reset.
Interruptions: Interrupts or higher-priority tasks can delay execution of the watchdog-resetting function. Ensuring the timer is long enough to account for such delays can be tricky.
Dynamic System Behavior: In systems where the workload can vary significantly, predicting how much time is needed to reset the watchdog can be challenging.
3. Why Does Testing Impact the Calculated Value?
Testing plays a crucial role in refining the watchdog period, as it helps to identify the system's worst-case execution scenarios. During development, engineers may estimate how long tasks will take based on theoretical or average values. However, under real-world conditions, testing may reveal unexpected delays, bottlenecks, or unaccounted-for system behaviors that alter the expected task durations.
Stress Testing: When a system is under heavy load, tasks may take longer to execute due to resource contention. This can impact how frequently the watchdog can be kicked.
Real-Time Performance: Some tasks might execute slower in a deployed environment compared to a controlled development environment due to hardware limitations or environmental factors like power fluctuations. Testing across different conditions is essential to adjust the watchdog period accordingly.
Ensure WDG period is not immediately increased when it’s triggered in testing. Like it or not, this is a very common practices. This kills the purpose of implementing WDG.
WDG period shall not be changed without through analysis.
4. What Should Be Considered in Watchdog Calculation?
Several factors need to be considered when calculating the watchdog period to ensure it is set correctly:
Task Cycle Times: Understand the execution time of each critical task, particularly the longest task. The watchdog should not reset the system prematurely if a valid task is still running.
System Latency: Consider delays from interrupts and task scheduling, especially in real-time operating systems (RTOS). These can vary based on priority levels and timing jitter.
Safety Margin: Always include a buffer or safety margin on top of your worst-case execution time to account for unexpected delays.
Clock Frequency: Take into account the clock frequency of the watchdog timer itself and how it interacts with your system's clock, as this will affect your calculation for timeout periods.
Power Consumption: In low-power or battery-operated systems, the watchdog timer might operate slower, so these factors should be reflected in your calculation.
WDG period is calculated theoretically based on your task executions. Setting this value validates both your design and real-time performance.
If they do not work, evaluate where is system is deviating. Again, do not just change the WDG period
5. What to Do When the Watchdog Triggers in the Development Cycle?
When the watchdog timer triggers during the development phase, it is important to treat it as a signal that something in the system isn’t behaving as expected. Here's what you should do:
Identify the Cause: Use debugging tools to figure out why the watchdog wasn’t kicked in time. Did a task run longer than expected? Was there an unhandled exception or deadlock? Trace back the issue to see where the code or system behavior broke down.
Optimize Task Execution: If certain tasks are causing delays, consider optimizing the code or breaking up tasks into smaller, quicker cycles to ensure the watchdog is regularly kicked.
Implement Graceful Recovery: If the watchdog reset occurs, ensure the system can recover smoothly without data loss or critical failure. Logging the watchdog reset event can be useful for postmortem analysis.
Adjust the Watchdog Period: If the task cycles are indeed running longer than initially calculated, consider extending the watchdog period. Be careful, though—this should be done only after verifying that the delay is legitimate and not due to a deeper issue in the code or system design.
Recovery from a watchdog timer (WDG) reset is highly dependent on the product type. For consumer-grade products, frequent resets can erode customer trust in the device and damage the brand’s reputation.
It's crucial to carefully plan how to handle resets gracefully to minimize user impact and maintain confidence in the product.
6. What to Do in Manufacturing?
Once your product is ready for manufacturing, the behavior of the watchdog timer can be more challenging to control since the system will be deployed in real-world conditions. Here’s what to consider:
Field Testing: Before mass production, field-test your system under various conditions (temperature, power supply, interference, etc.) to validate the watchdog period.
Default Watchdog Settings: Set conservative default values for the watchdog timer in production. Ensure these values have been tested to work reliably under normal operating conditions.
Bootloader - Application code: If you intend to use bootloader, ensure when an application is switched, the watchdog is reinitialized or setting are verified. As when large amount of code flashing is done, clearing watchdog is not practical and usually watchdog is disabled or keep at max period.
In-Field Debugging: Implement logging mechanisms that can store information before a watchdog reset happens. This will allow you to retrieve useful data when troubleshooting in the field.
OTA Updates (Over-the-Air): If possible, implement OTA update mechanisms so that watchdog parameters can be adjusted remotely if issues arise post-deployment.
Graceful Handling of Resets: Ensure that when the watchdog triggers a reset in the field, the system can restart without major disruptions, data loss, or user impact. Smooth system recovery is essential for maintaining customer satisfaction.
Conclusion
The watchdog timer is a critical safety component in any embedded system, but getting the timing right is no easy task. A poorly calculated watchdog period can either render the system unresponsive to failures or cause unnecessary resets that disrupt normal operations. Understanding your system’s behavior, testing under real-world conditions, and considering factors like task cycle time, interrupts, and safety margins are key to calculating an optimal watchdog period. Both during the development and manufacturing phases, keeping a vigilant eye on how the watchdog performs will ensure the long-term reliability of your system.


