diff options
author | Scott Allen <saydisp-git@yahoo.ca> | 2017-03-10 15:35:59 -0500 |
---|---|---|
committer | Martino Facchin <m.facchin@arduino.cc> | 2017-11-13 17:34:41 +0100 |
commit | 10e0811c520666bb7a4882651f13e8349aba717d (patch) | |
tree | 9c265d76a89400c9b02484c4fa8a044f87a28fd1 /cores | |
parent | 289faaa75900b35a785584d25532a55e4daaddd9 (diff) |
Save/restore the watchdog during USB auto-reset
The state of the watchdog timer is saved during a USB auto-reset and then
restored if the reset is aborted, in case the sketch is using the watchdog.
Diffstat (limited to 'cores')
-rw-r--r-- | cores/arduino/CDC.cpp | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/cores/arduino/CDC.cpp b/cores/arduino/CDC.cpp index c06b64a..125dc85 100644 --- a/cores/arduino/CDC.cpp +++ b/cores/arduino/CDC.cpp @@ -34,6 +34,8 @@ typedef struct static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 }; static volatile int32_t breakValue = -1; +static u8 wdtcsr_save; + bool _updatedLUFAbootloader = false; #define WEAK __attribute__ ((weak)) @@ -127,6 +129,8 @@ bool CDC_Setup(USBSetup& setup) #endif // Store boot key *(uint16_t *)magic_key_pos = MAGIC_KEY; + // Save the watchdog state in case the reset is aborted. + wdtcsr_save = WDTCSR; wdt_enable(WDTO_120MS); } else if (*(uint16_t *)magic_key_pos == MAGIC_KEY) @@ -138,8 +142,10 @@ bool CDC_Setup(USBSetup& setup) // Cancellation is only done if an auto-reset was started, which is // indicated by the magic key having been set. - wdt_disable(); wdt_reset(); + // Restore the watchdog state in case the sketch was using it. + WDTCSR |= (1<<WDCE) | (1<<WDE); + WDTCSR = wdtcsr_save; #if MAGIC_KEY_POS != (RAMEND-1) // Restore backed up (old bootloader) magic key data if (magic_key_pos != (RAMEND-1)) { |