]> git.friedersdorff.com Git - max/tmk_keyboard.git/commitdiff
core: Add bootkey of Caterina bootloader
authortmk <hasu@tmk-kbd.com>
Mon, 16 Sep 2019 08:49:38 +0000 (17:49 +0900)
committertmk <hasu@tmk-kbd.com>
Mon, 16 Sep 2019 08:49:38 +0000 (17:49 +0900)
The bootkey set in bootloader_jump() works with Pro Micro and Leonardo.
This fix doesn't seem to prevent other bootloaders, however, it can be
disabled by defining NO_BOOTLOADER_CATERINA_BOOTKEY.

tmk_core/common/avr/bootloader.c

index 7c744e8c79ec8aaed46a25dc37282fcb242a481e..8d6de1a0b372ecc3586cdb7e3d22c5fa8c5a3b12 100644 (file)
@@ -87,6 +87,14 @@ void bootloader_jump(void) {
     _delay_ms(5);
 #endif
 
+#ifndef NO_BOOTLOADER_CATERINA_BOOTKEY
+    // Set bootkey for Arduino Leonardo and Pro Micro bootloader
+    // Watchdog reset with bootkey causes the bootloader to enter program mode instead of starting application.
+    // https://github.com/arduino/ArduinoCore-avr/blob/master/bootloaders/caterina/Caterina.c#L68-L69
+    // https://github.com/sparkfun/SF32u4_boards/blob/master/sparkfun/avr/bootloaders/caterina/Caterina.c#L88-L89
+    *(volatile uint16_t *)0x0800 = 0x7777;
+#endif
+
     // watchdog reset
     reset_key = BOOTLOADER_RESET_KEY;
     wdt_enable(WDTO_250MS);
@@ -108,6 +116,13 @@ void bootloader_jump_after_watchdog_reset(void)
         MCUSR &= ~(1<<WDRF);
         wdt_disable();
 
+#ifndef NO_BOOTLOADER_CATERINA_BOOTKEY
+        // Clear bootkey of Caterina bootloader for other bootloaders
+        // Leonardo and Pro Micro with Arduino default fuse setting don't reach here
+        // because bootloader section are executed before application everytime.
+        *(volatile uint16_t *)0x0800 = 0;
+#endif
+
         // This is compled into 'icall', address should be in word unit, not byte.
         ((void (*)(void))(BOOTLOADER_START/2))();
     }