From deb59fd6054ce105487bd107dec014fab116cc3a Mon Sep 17 00:00:00 2001 From: NicoHood Date: Fri, 7 Aug 2015 19:36:28 +0200 Subject: [PATCH] Made Magic Key Settings more flexible --- cores/arduino/CDC.cpp | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/cores/arduino/CDC.cpp b/cores/arduino/CDC.cpp index d694a2d..63983d9 100644 --- a/cores/arduino/CDC.cpp +++ b/cores/arduino/CDC.cpp @@ -92,11 +92,24 @@ bool CDC_Setup(USBSetup& setup) // with a relatively long period so it can finish housekeeping tasks // like servicing endpoints before the sketch ends +#ifndef MAGIC_KEY +#define MAGIC_KEY 0x7777 +#endif +#ifndef MAGIC_KEY_POS +#define MAGIC_KEY_POS 0x0800 +#endif + // We check DTR state to determine if host port is open (bit 0 of lineState). if (1200 == _usbLineInfo.dwDTERate && (_usbLineInfo.lineState & 0x01) == 0) { - *(uint16_t *)(RAMEND-1) = *(uint16_t *)0x0800; - *(uint16_t *)0x0800 = 0x7777; +#if MAGIC_KEY_POS != (RAMEND-1) + *(uint16_t *)(RAMEND-1) = *(uint16_t *)MAGIC_KEY_POS; + *(uint16_t *)MAGIC_KEY_POS = MAGIC_KEY; +#else + // for future boards save the key in the inproblematic RAMEND + // which is reserved for the main() return value (which will never return) + *(uint16_t *)MAGIC_KEY_POS = MAGIC_KEY; +#endif wdt_enable(WDTO_120MS); } else @@ -108,7 +121,11 @@ bool CDC_Setup(USBSetup& setup) wdt_disable(); wdt_reset(); - *(uint16_t *)0x0800 = *(uint16_t *)(RAMEND-1); +#if MAGIC_KEY_POS != (RAMEND-1) + *(uint16_t *)MAGIC_KEY_POS = *(uint16_t *)(RAMEND-1); +#else + *(uint16_t *)MAGIC_KEY_POS = 0x0000; +#endif } } return true;