From 289faaa75900b35a785584d25532a55e4daaddd9 Mon Sep 17 00:00:00 2001 From: Scott Allen Date: Fri, 10 Mar 2017 14:47:18 -0500 Subject: 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. --- cores/arduino/CDC.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'cores') diff --git a/cores/arduino/CDC.cpp b/cores/arduino/CDC.cpp index 0a743e1..c06b64a 100644 --- a/cores/arduino/CDC.cpp +++ b/cores/arduino/CDC.cpp @@ -119,9 +119,9 @@ bool CDC_Setup(USBSetup& setup) if (1200 == _usbLineInfo.dwDTERate && (_usbLineInfo.lineState & 0x01) == 0) { #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 - 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; } #endif @@ -129,12 +129,14 @@ bool CDC_Setup(USBSetup& setup) *(uint16_t *)magic_key_pos = MAGIC_KEY; 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 // 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. + // Cancellation is only done if an auto-reset was started, which is + // indicated by the magic key having been set. wdt_disable(); wdt_reset(); -- cgit v1.2.3-18-g5258