4 #include <avr/interrupt.h>
6 #include <util/delay.h>
7 #include "bootloader.h"
10 #include <LUFA/Drivers/USB/USB.h>
14 /* Bootloader Size in *bytes*
16 * AVR Boot section size are defined by setting BOOTSZ fuse in fact. Consult with your MCU datasheet.
17 * 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(ATmega32U4) 4096
22 * Atmel DFU loader(AT90USB128) 8192
23 * LUFA bootloader(ATmega32U4) 4096
24 * Arduino Caterina(ATmega32U4) 4096
25 * USBaspLoader(ATmega***) 2048
26 * Teensy halfKay(ATmega32U4) 512
27 * Teensy++ halfKay(AT90USB128) 1024
30 * AVR Boot section is located at the end of Flash memory like the followings.
33 * byte Atmel/LUFA(ATMega32u4) byte Atmel(AT90SUB128)
34 * 0x0000 +---------------+ 0x00000 +---------------+
37 * | Application | | Application |
40 * | | 32KB-4KB | | 128KB-8KB
41 * 0x6000 +---------------+ 0x1FC00 +---------------+
42 * | Bootloader | 4KB | Bootloader | 8KB
43 * 0x7FFF +---------------+ 0x1FFFF +---------------+
46 * byte Teensy(ATMega32u4) byte Teensy++(AT90SUB128)
47 * 0x0000 +---------------+ 0x00000 +---------------+
50 * | Application | | Application |
53 * | | 32KB-512B | | 128KB-1KB
54 * 0x7E00 +---------------+ 0x1FC00 +---------------+
55 * | Bootloader | 512B | Bootloader | 1KB
56 * 0x7FFF +---------------+ 0x1FFFF +---------------+
58 #ifndef BOOTLOADER_SIZE
59 #warning To use bootloader_jump() you need to define BOOTLOADER_SIZE in config.h.
60 #define BOOTLOADER_SIZE 4096
63 #define FLASH_SIZE (FLASHEND + 1L)
64 #define BOOTLOADER_START (FLASH_SIZE - BOOTLOADER_SIZE)
68 * Entering the Bootloader via Software
69 * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html
71 #define BOOTLOADER_RESET_KEY 0xB007B007
72 uint32_t reset_key __attribute__ ((section (".noinit")));
74 /* initialize MCU status by watchdog reset */
75 void bootloader_jump(void) {
90 #ifndef NO_BOOTLOADER_CATERINA_BOOTKEY
91 // Set bootkey for Arduino Leonardo and Pro Micro bootloader
92 // Watchdog reset with bootkey causes the bootloader to enter program mode instead of starting application.
93 // https://github.com/arduino/ArduinoCore-avr/blob/master/bootloaders/caterina/Caterina.c#L68-L69
94 // https://github.com/sparkfun/SF32u4_boards/blob/master/sparkfun/avr/bootloaders/caterina/Caterina.c#L88-L89
95 *(volatile uint16_t *)0x0800 = 0x7777;
99 reset_key = BOOTLOADER_RESET_KEY;
100 wdt_enable(WDTO_250MS);
105 /* this runs before main() */
106 void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3")));
107 void bootloader_jump_after_watchdog_reset(void)
109 if ((MCUSR & (1<<WDRF)) && reset_key == BOOTLOADER_RESET_KEY) {
112 // My custom USBasploader requires this to come up.
115 // Seems like Teensy halfkay loader requires clearing WDRF and disabling watchdog.
119 #ifndef NO_BOOTLOADER_CATERINA_BOOTKEY
120 // Clear bootkey of Caterina bootloader for other bootloaders
121 // Leonardo and Pro Micro with Arduino default fuse setting don't reach here
122 // because bootloader section are executed before application everytime.
123 *(volatile uint16_t *)0x0800 = 0;
126 // This is compled into 'icall', address should be in word unit, not byte.
127 ((void (*)(void))(BOOTLOADER_START/2))();
133 /* Jumping To The Bootloader
134 * http://www.pjrc.com/teensy/jump_to_bootloader.html
136 * This method doen't work when using LUFA. idk why.
137 * - needs to initialize more regisers or interrupt setting?
139 void bootloader_jump(void) {
149 USBCON = (1<<FRZCLK);
157 #if defined(__AVR_AT90USB162__)
158 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
159 TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
160 DDRB = 0; DDRC = 0; DDRD = 0;
161 PORTB = 0; PORTC = 0; PORTD = 0;
162 #elif defined(__AVR_ATmega32U4__)
163 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
164 TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
165 DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
166 PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
167 #elif defined(__AVR_AT90USB646__)
168 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
169 TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
170 DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
171 PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
172 #elif defined(__AVR_AT90USB1286__)
173 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
174 TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
175 DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
176 PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
182 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
183 // This makes custom USBasploader come up.
187 PORTB = 0; PORTC= 0; PORTD = 0;
188 DDRB = 0; DDRC= 0; DDRD = 0;
190 // disable interrupts
191 EIMSK = 0; EECR = 0; SPCR = 0;
192 ACSR = 0; SPMCSR = 0; WDTCSR = 0; PCICR = 0;
193 TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0;
194 ADCSRA = 0; TWCR = 0; UCSR0B = 0;
197 // This is compled into 'icall', address should be in word unit, not byte.
198 ((void (*)(void))(BOOTLOADER_START/2))();