4 #include <avr/eeprom.h>
5 #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'.
21 * Size of Bootloaders in bytes:
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 like the followings.
34 * byte Atmel/LUFA(ATMega32u4) byte Atmel(AT90SUB128)
35 * 0x0000 +---------------+ 0x00000 +---------------+
38 * | Application | | Application |
41 * | | 32KB-4KB | | 128KB-8KB
42 * 0x7000 +---------------+ 0x1E000 +---------------+
43 * | Bootloader | 4KB | Bootloader | 8KB
44 * 0x7FFF +---------------+ 0x1FFFF +---------------+
47 * byte Teensy(ATMega32u4) byte Teensy++(AT90SUB128)
48 * 0x0000 +---------------+ 0x00000 +---------------+
51 * | Application | | Application |
54 * | | 32KB-512B | | 128KB-1KB
55 * 0x7E00 +---------------+ 0x1FC00 +---------------+
56 * | Bootloader | 512B | Bootloader | 1KB
57 * 0x7FFF +---------------+ 0x1FFFF +---------------+
59 #ifndef BOOTLOADER_SIZE
60 #warning To use bootloader_jump() you need to define BOOTLOADER_SIZE in config.h.
61 #define BOOTLOADER_SIZE 4096
64 #define FLASH_SIZE (FLASHEND + 1L)
65 #define BOOTLOADER_START (FLASH_SIZE - BOOTLOADER_SIZE)
69 * Entering the Bootloader via Software
70 * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html
72 #define BOOTLOADER_RESET_KEY 0xB007B007
73 uint32_t reset_key __attribute__ ((section (".noinit")));
75 /* initialize MCU status by watchdog reset */
76 void bootloader_jump(void) {
77 #ifndef CATERINA_BOOTLOADER
93 #ifdef EEPROM_BOOTLOADER_START
94 eeprom_write_byte((uint8_t *)EEPROM_BOOTLOADER_START, 0x00);
98 reset_key = BOOTLOADER_RESET_KEY;
99 wdt_enable(WDTO_250MS);
103 // this block may be optional
104 // TODO: figure it out
106 uint16_t *const bootKeyPtr = (uint16_t *)0x0800;
108 // Value used by Caterina bootloader use to determine whether to run the
109 // sketch or the bootloader programmer.
110 uint16_t bootKey = 0x7777;
112 *bootKeyPtr = bootKey;
114 // setup watchdog timeout
115 wdt_enable(WDTO_60MS);
117 while(1) {} // wait for watchdog timer to trigger
122 #ifdef __AVR_ATmega32A__
123 // MCUSR is actually called MCUCSR in ATmega32A
127 /* this runs before main() */
128 void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3")));
129 void bootloader_jump_after_watchdog_reset(void)
131 if ((MCUSR & (1<<WDRF)) && reset_key == BOOTLOADER_RESET_KEY) {
134 // My custom USBasploader requires this to come up.
137 // Seems like Teensy halfkay loader requires clearing WDRF and disabling watchdog.
141 // This is compled into 'icall', address should be in word unit, not byte.
142 ((void (*)(void))(BOOTLOADER_START/2))();
148 /* Jumping To The Bootloader
149 * http://www.pjrc.com/teensy/jump_to_bootloader.html
151 * This method doen't work when using LUFA. idk why.
152 * - needs to initialize more regisers or interrupt setting?
154 void bootloader_jump(void) {
164 USBCON = (1<<FRZCLK);
172 #if defined(__AVR_AT90USB162__)
173 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
174 TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
175 DDRB = 0; DDRC = 0; DDRD = 0;
176 PORTB = 0; PORTC = 0; PORTD = 0;
177 #elif defined(__AVR_ATmega32U4__)
178 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
179 TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
180 DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
181 PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
182 #elif defined(__AVR_AT90USB646__)
183 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
184 TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
185 DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
186 PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
187 #elif defined(__AVR_AT90USB1286__)
188 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
189 TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
190 DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
191 PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
197 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
198 // This makes custom USBasploader come up.
202 PORTB = 0; PORTC= 0; PORTD = 0;
203 DDRB = 0; DDRC= 0; DDRD = 0;
205 // disable interrupts
206 EIMSK = 0; EECR = 0; SPCR = 0;
207 ACSR = 0; SPMCSR = 0; WDTCSR = 0; PCICR = 0;
208 TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0;
209 ADCSRA = 0; TWCR = 0; UCSR0B = 0;
212 // This is compled into 'icall', address should be in word unit, not byte.
213 ((void (*)(void))(BOOTLOADER_START/2))();