System unexpectedly rebooting, critical data lost? It might be your watchdog ‘protesting’!As an embedded developer, have you ever encountered such an awkward scenario: the microcontroller is writing critical data to internal Flash, and suddenly the system reboots, resulting in data loss and making it difficult to diagnose the fault? This is likely due to the Window Watchdog (WWDG) timing out during Flash operations, leading to a reset. Today, we will delve into this issue and its solutions.
1. Root Cause: Why Do Flash Operations Trigger the Watchdog?
Core Conflict: Internal Flash erase/write operations take a considerable amount of time (from a few milliseconds to tens of milliseconds), during which the CPU cannot perform other tasks. The feeding window for the window watchdog is usually very short (also in milliseconds), making it easy to time out.
2. Solution Comparison
Solution 1: Temporarily Disable the Watchdog
Method: Disable the watchdog before performing Flash operations and re-enable it afterward.Advantages:1. Simple implementation with minimal code changes.2. Completely resolves the timeout issue.Disadvantages:1. Safety risk: If the system hangs after the Flash operation, the watchdog cannot reset the system.2. Does not comply with high reliability design principles.
Solution 2: Chunk Operations + Intermittent Dog FeedingMethod: Divide large data into chunks and feed the watchdog after each chunk is written.
Advantages:
-
1. Maintains the watchdog protection function.
-
2. Suitable for large data operations.
Disadvantages:
-
1. Complex implementation.
-
2. Extends total operation time.
-
3. Requires careful design of chunk sizes.

Solution 3: Use Independent Watchdog (IWDG)
Method: Replace WWDG with IWDG, as the IWDG has a more relaxed window limitation.

IWDG_Init(IWDG_PRESCALER_64,0XFFF);
Feed the watchdog in the interrupt function:

Advantages:
-
1. IWDG has a smaller window limitation, making it easier to feed the watchdog in time.
-
2. Still provides hardware fault protection.
Disadvantages:
-
1. Protection capability is not as strict as WWDG.
-
2. Requires hardware support.
3. Best Practice Recommendations
-
Always Have a Plan B: Regardless of the solution adopted, ensure there is a data recovery mechanism.
-
Monitoring and Logging: Record the number of Flash operations and their status for easier debugging.
-
Consider External Flash: For large data storage, consider using external Flash or EEPROM.
4. Conclusion
The conflict between Flash operations and the watchdog is a classic issue in embedded development. When choosing a solution, it is necessary to weigh security, complexity, and reliability. For most applications, chunk operations + intermittent dog feeding is the most balanced choice.
Remember: There is no one-size-fits-all solution, only the solution that best fits your project needs!