Adafruit_SleepyDog icon indicating copy to clipboard operation
Adafruit_SleepyDog copied to clipboard

Patch to prevent Reboot loop when using watchdog

Open ermtl opened this issue 6 years ago • 0 comments

When resetting with the watchdog on AVR micros, the watchdog remains enabled after the reset, but with a very short 15ms timeout. With the default bootloader, that creates a situation where the arduino constantly resets itself until physically powered off.

Here is a description of the problem: https://github.com/arduino/Arduino/issues/4492 https://arduino.stackexchange.com/questions/2922/watchdog-timer-stuck-in-restart-loop-green-led-flashing/45281#45281

The patch adds the ability to use the ISR to perform a "soft" reset that will disable the watchdog before resetting. It solves the issue without having to reflash the bootloader with WATCHDOG_MODS The patch behaves as expected when using the sleep function (even when exiting sleep by another interrupt).

Changes are in utility/WatchdogAVR.cpp and utility/WatchdogAVR.h

Since this is a AVR only problem, nothing is changed for other architectures.

To enable the soft reset, use a negative number in the Watchdog.enable function. Watchdog.enable(4000); // This uses the hard reset but will hang in a boot loop on some boards (depending on the bootloader) Watchdog.enable(-4000); // This will never hang and uses the soft reset (adding an optional softReset parameter to Watchdog.enable could be a cleaner way to do it)

The BasicUsage.ino example can be modified as shown to prevent endless reset on arduinos with a bootloader that does not disable the watchdog.

ermtl avatar Nov 25 '18 09:11 ermtl