Skip to content

Commit 42f7318

Browse files
committed
Made Magic Key Settings more flexible
1 parent bd93add commit 42f7318

File tree

1 file changed

+21
-3
lines changed
  • hardware/arduino/avr/cores/arduino

1 file changed

+21
-3
lines changed

hardware/arduino/avr/cores/arduino/CDC.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,25 @@ bool CDC_Setup(USBSetup& setup)
9292
// with a relatively long period so it can finish housekeeping tasks
9393
// like servicing endpoints before the sketch ends
9494

95+
#ifndef MAGIC_KEY
96+
#define MAGIC_KEY 0x7777
97+
#endif
98+
#ifndef MAGIC_KEY_POS
99+
#define MAGIC_KEY_POS (uint16_t *)0x0800
100+
#endif
101+
// NO_MAGIC_KEY_BACKUP can be used if you want to store the key in the safer RAMEND directly, so no backup is needed
102+
95103
// We check DTR state to determine if host port is open (bit 0 of lineState).
96104
if (1200 == _usbLineInfo.dwDTERate && (_usbLineInfo.lineState & 0x01) == 0)
97105
{
98-
*(uint16_t *)(RAMEND-1) = *(uint16_t *)0x0800;
99-
*(uint16_t *)0x0800 = 0x7777;
106+
#if !defined(NO_MAGIC_KEY_BACKUP)
107+
*(uint16_t *)(RAMEND-1) = *MAGIC_KEY_POS;
108+
*MAGIC_KEY_POS = MAGIC_KEY;
109+
#else
110+
// for future boards save the key in the inproblematic RAMEND
111+
// which is reserved for the main() return value (which will never return)
112+
*MAGIC_KEY_POS = MAGIC_KEY;
113+
#endif
100114
wdt_enable(WDTO_120MS);
101115
}
102116
else
@@ -108,7 +122,11 @@ bool CDC_Setup(USBSetup& setup)
108122

109123
wdt_disable();
110124
wdt_reset();
111-
*(uint16_t *)0x0800 = *(uint16_t *)(RAMEND-1);
125+
#if !defined(NO_MAGIC_KEY_BACKUP)
126+
*MAGIC_KEY_POS = *(uint16_t *)(RAMEND-1);
127+
#else
128+
*MAGIC_KEY_POS = 0x0000;
129+
#endif
112130
}
113131
}
114132
return true;

0 commit comments

Comments
 (0)