Commit 289faaa7 authored by Scott Allen's avatar Scott Allen Committed by Martino Facchin

Fix save/restore of magic key location during reset

In the USB CDC code to invoke an auto-reset, the magic key location could be
restored before it had actually been saved. The sketch would then have a
corrupted value at this location. This fix prevents the value from being
restored if it hasn't previously been saved.
parent b084848f
...@@ -119,9 +119,9 @@ bool CDC_Setup(USBSetup& setup) ...@@ -119,9 +119,9 @@ bool CDC_Setup(USBSetup& setup)
if (1200 == _usbLineInfo.dwDTERate && (_usbLineInfo.lineState & 0x01) == 0) if (1200 == _usbLineInfo.dwDTERate && (_usbLineInfo.lineState & 0x01) == 0)
{ {
#if MAGIC_KEY_POS != (RAMEND-1) #if MAGIC_KEY_POS != (RAMEND-1)
// Backup ram value if its not a newer bootloader. // Backup ram value if its not a newer bootloader and it hasn't already been saved.
// This should avoid memory corruption at least a bit, not fully // This should avoid memory corruption at least a bit, not fully
if (magic_key_pos != (RAMEND-1)) { if (magic_key_pos != (RAMEND-1) && *(uint16_t *)magic_key_pos != MAGIC_KEY) {
*(uint16_t *)(RAMEND-1) = *(uint16_t *)magic_key_pos; *(uint16_t *)(RAMEND-1) = *(uint16_t *)magic_key_pos;
} }
#endif #endif
...@@ -129,12 +129,14 @@ bool CDC_Setup(USBSetup& setup) ...@@ -129,12 +129,14 @@ bool CDC_Setup(USBSetup& setup)
*(uint16_t *)magic_key_pos = MAGIC_KEY; *(uint16_t *)magic_key_pos = MAGIC_KEY;
wdt_enable(WDTO_120MS); wdt_enable(WDTO_120MS);
} }
else else if (*(uint16_t *)magic_key_pos == MAGIC_KEY)
{ {
// Most OSs do some intermediate steps when configuring ports and DTR can // Most OSs do some intermediate steps when configuring ports and DTR can
// twiggle more than once before stabilizing. // twiggle more than once before stabilizing.
// To avoid spurious resets we set the watchdog to 250ms and eventually // To avoid spurious resets we set the watchdog to 120ms and eventually
// cancel if DTR goes back high. // cancel if DTR goes back high.
// Cancellation is only done if an auto-reset was started, which is
// indicated by the magic key having been set.
wdt_disable(); wdt_disable();
wdt_reset(); wdt_reset();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment