diff options
author | Scott Allen <saydisp-git@yahoo.ca> | 2017-03-10 14:47:18 -0500 |
---|---|---|
committer | Martino Facchin <m.facchin@arduino.cc> | 2017-11-13 17:34:41 +0100 |
commit | 289faaa75900b35a785584d25532a55e4daaddd9 (patch) | |
tree | 300abbbc5b077d5fc06eb9480672e3b9f17faf1b /cores/arduino | |
parent | b084848f2eaf9ccb3ac9a64ac5492d91df4706bf (diff) |
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.
Diffstat (limited to 'cores/arduino')
-rw-r--r-- | cores/arduino/CDC.cpp | 10 |
1 files changed, 6 insertions, 4 deletions
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(); |