Arduino Leonardo and Watchdog timer?

Hey there.

Tell me, can I use the watchdog timer on Leonardo board? Or for that I need to change the default boot loader?

VVSh: Hey there.

Tell me, can I use the watchdog timer on Leonardo board? Or for that I need to change the default boot loader?

http://forum.arduino.cc/index.php?topic=161565.0

Thank you very much for your reply. I really do not know much English. If I understand correctly, that topic has not answered my question. Or does it mean that the board of Leonardo can not use the watchdog?

The usual problem with using Watchdog on an Arduino is that the bootloader does not disable the watchdog, which causes the system to loop within the bootloader (RESET, bootloader starts, bootloader waits for traffic, watchdog expires while waiting, RESET (and loop)) I looked at the sources for the 32u4 bootloader used on Leonardo ("Caterina"), and it SEEMS that it DOES include the necessary wdt_disable(), so it SHOULD be possible to use the watchdog in Leonardo Sketches.

But I didn't test it. Try it out and see if it works!

Thank you very much! Probably have to try. But I have one board Leonardo. If I get bootloop, then restore the bootloader will not be possible without programming device.

P.S. And is there and this forum manufacturers Arduino? Most likely they would be able to clearly answer the problem of the bootloop on Leonardo board.

When watchdog occupied by bootloader

I am using a Arduino Pro Micro - ATmega32U4 board and found that changes in USB status will disable the watchdog. The “caterina” bootloader/USB driver is monitoring all USB port status changes and particularly one where connect is made using baudrate 1200. In that case it will configure the watchdog for a 125ms timeout and enable watchdog reset mode. The idea is this will trigger shortly and a the reset will start the bootloader code. For all other USB status changes it will just disable the watchdog.

If you are using the watchdog it will be disabled by USB status changes and bootloader and if you prolong the watchdog timer in your code the USB is unable to reset and start upload.

With some cooperation you can use the watchdog timer anyway

One simple way to use the watchdog anyway is for your code to enable the watchdog whenever it is disabled and skip prolonging if watchdog setting indicates USB status has initiated a reset.

I have noticed the bootloader only sets the watchdog reset enable bit so if you only use the interrupt enable bit you only have to check the watchdog reset bit is not configured before prolonging the watchdog timer.

If you want to use the reset mode as well you may select a watchdog timer value different than the bootloader and skip prolonging if timer value setting changes and watchdog reset is enabled.

Some sample code that allows for USB reset and restores a disabled watchdog:

//Test if watchdog interrupt enabled
if (WDTCSR & (1<<WDIE))
{   
    //Prolong wtachdog timer
    wdt_reset();

}
//No interrupt enabled - Test if watchdog reset enabled
else if (WDTCSR & (1<<WDE))
{
    //Bootloader about to reset - do not prolong watchdog
}
//It has ben disabled - Enable and prolong
else
{
    //Watchdog disabled - Enable again
    wdt_enable();
}

If there is no USB status changes or if no USB connected - the watchdog runs completely undisturbed and your settings are not affected by the bootloader.

VVSh: Thank you very much! Probably have to try. But I have one board Leonardo. If I get bootloop, then restore the bootloader will not be possible without programming device.

I think you should be able to hold down the reset button, and release at the appropriate point while trying to upload - no matter how hard your sketch hoses things, if you do a hardware reset, the bootloader will run that first time after reset.