adafruit / Adafruit_SleepyDog

Arduino library to use the watchdog timer for system reset and low power sleep.
MIT License
233 stars 65 forks source link

<Nano> If using without bootloader, wdt_reset stucks #14

Open Alan-NTHU opened 6 years ago

Alan-NTHU commented 6 years ago

I use with arduino Nano and upload with usbasp (ICSP), which means it is not programmed with a bootloader I think using other programmer that doesn't use the bootloader will also reproduce the problem.

Problem: After a first wdt_timeout , the nano hangs with crazy resetting (I use with apa102 led, so I see them showing my setup indicator repeatingly) , and reset button doesn't help, I would need to re-program the nano to get it work.

Later I found that by adding a clear flag of {MCUSR = 0;} just under "setup()// which a line found in <avr/wdt.h> and followed by a { Watchdog.disable();} can stop wdt_hanging after a wdt_reset. and program can run good again.

but {MCUSR = 0;} or {Watchdog.disable();}alone will not work, they are both needed and {MCUSR = 0;} should done first. I guess clearing the wdt reset flag (not sure if it is or not?) is very essential in this case.

please add this in your library, maybe like a Watchdog.begin() ? because it is a simple fix for users using ICSP, other fixes are pretty complex and I can't handle.

ermtl commented 5 years ago

The problem also happens in Nanos with a bootloader that does not clear the watchdog settings. I made a patch that solves the issue by issuing a soft reset instead of a hard reset.

The way to use it is to specify a negative number in the Watchdog.enable parameter. (works with the BasicUsage.ino example) Watchdog.enable(-4000);

https://github.com/ermtl/Adafruit_SleepyDog/tree/ermtl-softReset