4 #include <avr/interrupt.h>
7 #include <util/delay.h>
8 #include "bootloader.h"
11 #include <LUFA/Drivers/USB/USB.h>
15 /* Bootloader Size in *bytes*
17 * AVR Boot section size are defined by setting BOOTSZ fuse in fact. Consult with your MCU datasheet.
18 * Note that 'Word'(2 bytes) size and address are used in datasheet while TMK uses 'Byte'.
20 * Size of Bootloaders in bytes:
21 * Atmel DFU loader(ATmega32U2) 4096
22 * Atmel DFU loader(ATmega32U4) 4096
23 * Atmel DFU loader(AT90USB128) 8192
24 * LUFA bootloader(ATmega32U4) 4096
25 * Arduino Caterina(ATmega32U4) 4096
26 * USBaspLoader(ATmega***) 2048
27 * Teensy halfKay(ATmega32U4) 512
28 * Teensy++ halfKay(AT90USB128) 1024
31 * AVR Boot section is located at the end of Flash memory.
33 * byte ATMega32u4 with 4096-byte Boot section
34 * 0x0000 +---------------+
37 * | Application | 28KB
41 * 0x7000 +---------------+ <---- BOOTLOADER_START
42 * | Bootloader | 4KB BOOTLOADER_SIZE
43 * 0x7FFF +---------------+ <---- FLASHEND
46 /* bootloader start address in byte */
47 #define BOOTLOADER_START (FLASHEND - bootloader_size() + 1)
49 /* boot section size in byte */
50 static inline uint16_t bootloader_size(void)
52 #if defined(BOOTLOADER_SIZE)
53 return BOOTLOADER_SIZE;
55 #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega32U2__) || \
56 defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega16U2__) || \
57 defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__)
58 uint8_t hfuse = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
59 switch ((hfuse >> 1) & 3) {
66 #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || \
67 defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__)
68 uint8_t hfuse = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
69 switch ((hfuse >> 1) & 3) {
77 #error Set Boot section size to BOOTLOADER_SIZE in config.h
84 * Entering the Bootloader via Software
85 * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html
87 #define BOOTLOADER_RESET_KEY 0xB007B007
88 uint32_t reset_key __attribute__ ((section (".noinit")));
90 /* initialize MCU status by watchdog reset */
91 void bootloader_jump(void) {
101 USBCON = (1<<FRZCLK);
106 #ifndef NO_BOOTLOADER_CATERINA_BOOTKEY
107 // Set bootkey for Arduino Leonardo and Pro Micro bootloader
108 // Watchdog reset with bootkey causes the bootloader to enter program mode instead of starting application.
109 // https://github.com/arduino/ArduinoCore-avr/blob/master/bootloaders/caterina/Caterina.c#L68-L69
110 // https://github.com/sparkfun/SF32u4_boards/blob/master/sparkfun/avr/bootloaders/caterina/Caterina.c#L88-L89
111 *(volatile uint16_t *)0x0800 = 0x7777;
115 reset_key = BOOTLOADER_RESET_KEY;
116 wdt_enable(WDTO_250MS);
121 /* this runs before main() */
122 void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3")));
123 void bootloader_jump_after_watchdog_reset(void)
125 if ((MCUSR & (1<<WDRF)) && reset_key == BOOTLOADER_RESET_KEY) {
128 // My custom USBasploader requires this to come up.
131 // Seems like Teensy halfkay loader requires clearing WDRF and disabling watchdog.
135 #ifndef NO_BOOTLOADER_CATERINA_BOOTKEY
136 // Clear bootkey of Caterina bootloader for other bootloaders
137 // Leonardo and Pro Micro with Arduino default fuse setting don't reach here
138 // because bootloader section are executed before application everytime.
139 *(volatile uint16_t *)0x0800 = 0;
142 // This is compled into 'icall', address should be in word unit, not byte.
143 ((void (*)(void))(BOOTLOADER_START / 2))();