Skip to content

Commit 10e0811

Browse files
MLXXXpfacchinm
authored andcommitted
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.
1 parent 289faaa commit 10e0811

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Diff for: cores/arduino/CDC.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ typedef struct
3434
static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 };
3535
static volatile int32_t breakValue = -1;
3636

37+
static u8 wdtcsr_save;
38+
3739
bool _updatedLUFAbootloader = false;
3840

3941
#define WEAK __attribute__ ((weak))
@@ -127,6 +129,8 @@ bool CDC_Setup(USBSetup& setup)
127129
#endif
128130
// Store boot key
129131
*(uint16_t *)magic_key_pos = MAGIC_KEY;
132+
// Save the watchdog state in case the reset is aborted.
133+
wdtcsr_save = WDTCSR;
130134
wdt_enable(WDTO_120MS);
131135
}
132136
else if (*(uint16_t *)magic_key_pos == MAGIC_KEY)
@@ -138,8 +142,10 @@ bool CDC_Setup(USBSetup& setup)
138142
// Cancellation is only done if an auto-reset was started, which is
139143
// indicated by the magic key having been set.
140144

141-
wdt_disable();
142145
wdt_reset();
146+
// Restore the watchdog state in case the sketch was using it.
147+
WDTCSR |= (1<<WDCE) | (1<<WDE);
148+
WDTCSR = wdtcsr_save;
143149
#if MAGIC_KEY_POS != (RAMEND-1)
144150
// Restore backed up (old bootloader) magic key data
145151
if (magic_key_pos != (RAMEND-1)) {

0 commit comments

Comments
 (0)