Skip to content

Patch to prevent Reboot loop when using watchdog#16

Open
ermtl wants to merge 2 commits into
adafruit:masterfrom
ermtl:ermtl-softReset
Open

Patch to prevent Reboot loop when using watchdog#16
ermtl wants to merge 2 commits into
adafruit:masterfrom
ermtl:ermtl-softReset

Conversation

@ermtl

@ermtl ermtl commented Nov 25, 2018

Copy link
Copy Markdown

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.

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.
The patch adds the ability to use the ISR to perform a "soft" reset that will disable the watchdog before resetting.
The patch behaves as expected when using the sleep function.
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 might hang on some boards (depending on the bootloader)
Watchdog.enable(-4000); // This will never hang and uses the soft reset
Corrects a bug if sleep was exited by another interrupt (the sleep timer interrupt would later reset the AVR when it times out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant