4 #include <avr/eeprom.h>
5 #include <avr/interrupt.h>
7 #include <util/delay.h>
8 #include "bootloader.h"
12 #include <LUFA/Drivers/USB/USB.h>
16 /** \brief Bootloader Size in *bytes*
18 * AVR Boot section size are defined by setting BOOTSZ fuse in fact. Consult with your MCU datasheet.
19 * 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
30 * AVR Boot section is located at the end of Flash memory like the followings.
32 * byte Atmel/LUFA(ATMega32u4) byte Atmel(AT90SUB128)
33 * 0x0000 +---------------+ 0x00000 +---------------+
36 * | Application | | Application |
39 * | | 32KB-4KB | | 128KB-8KB
40 * 0x7000 +---------------+ 0x1E000 +---------------+
41 * | Bootloader | 4KB | Bootloader | 8KB
42 * 0x7FFF +---------------+ 0x1FFFF +---------------+
45 * byte Teensy(ATMega32u4) byte Teensy++(AT90SUB128)
46 * 0x0000 +---------------+ 0x00000 +---------------+
49 * | Application | | Application |
52 * | | 32KB-512B | | 128KB-1KB
53 * 0x7E00 +---------------+ 0x1FC00 +---------------+
54 * | Bootloader | 512B | Bootloader | 1KB
55 * 0x7FFF +---------------+ 0x1FFFF +---------------+
57 #define FLASH_SIZE (FLASHEND + 1L)
59 #if !defined(BOOTLOADER_SIZE)
60 uint16_t bootloader_start;
63 #define BOOT_SIZE_256 0b110
64 #define BOOT_SIZE_512 0b100
65 #define BOOT_SIZE_1024 0b010
66 #define BOOT_SIZE_2048 0b000
68 /** \brief 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 /** \brief initialize MCU status by watchdog reset
79 void bootloader_jump(void) {
81 #if !defined(BOOTLOADER_SIZE)
82 uint8_t high_fuse = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
84 if (high_fuse & BOOT_SIZE_256) {
85 bootloader_start = (FLASH_SIZE - 512) >> 1;
86 } else if (high_fuse & BOOT_SIZE_512) {
87 bootloader_start = (FLASH_SIZE - 1024) >> 1;
88 } else if (high_fuse & BOOT_SIZE_1024) {
89 bootloader_start = (FLASH_SIZE - 2048) >> 1;
91 bootloader_start = (FLASH_SIZE - 4096) >> 1;
95 // Something like this might work, but it compiled larger than the block above
96 // bootloader_start = FLASH_SIZE - (256 << (~high_fuse & 0b110 >> 1));
99 #if defined(BOOTLOADER_HALFKAY)
100 // http://www.pjrc.com/teensy/jump_to_bootloader.html
102 // disable watchdog, if enabled (it's not)
103 // disable all peripherals
104 // a shutdown call might make sense here
106 USBCON = (1<<FRZCLK); // disable USB
109 #if defined(__AVR_AT90USB162__) // Teensy 1.0
110 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
111 TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
112 DDRB = 0; DDRC = 0; DDRD = 0;
113 PORTB = 0; PORTC = 0; PORTD = 0;
114 asm volatile("jmp 0x3E00");
115 #elif defined(__AVR_ATmega32U4__) // Teensy 2.0
116 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
117 TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
118 DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
119 PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
120 asm volatile("jmp 0x7E00");
121 #elif defined(__AVR_AT90USB646__) // Teensy++ 1.0
122 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
123 TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
124 DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
125 PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
126 asm volatile("jmp 0xFC00");
127 #elif defined(__AVR_AT90USB1286__) // Teensy++ 2.0
128 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
129 TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
130 DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
131 PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
132 asm volatile("jmp 0x1FC00");
135 #elif defined(BOOTLOADER_CATERINA)
136 // this block may be optional
137 // TODO: figure it out
139 uint16_t *const bootKeyPtr = (uint16_t *)0x0800;
141 // Value used by Caterina bootloader use to determine whether to run the
142 // sketch or the bootloader programmer.
143 uint16_t bootKey = 0x7777;
145 *bootKeyPtr = bootKey;
147 // setup watchdog timeout
148 wdt_enable(WDTO_60MS);
150 while(1) {} // wait for watchdog timer to trigger
152 #else // Assume remaining boards are DFU, even if the flag isn't set
154 #ifndef __AVR_ATmega32A__ // no USB - maybe BOOTLOADER_BOOTLOADHID instead though?
156 USBCON = (1<<FRZCLK); // disable USB
158 _delay_ms(5); // 5 seems to work fine
161 #ifdef BOOTLOADER_BOOTLOADHID
162 // force bootloadHID to stay in bootloader mode, so that it waits
163 // for a new firmware to be flashed
164 eeprom_write_byte((uint8_t *)1, 0x00);
168 reset_key = BOOTLOADER_RESET_KEY;
169 wdt_enable(WDTO_250MS);
175 #ifdef __AVR_ATmega32A__
176 // MCUSR is actually called MCUCSR in ATmega32A
180 /* this runs before main() */
181 void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3")));
182 void bootloader_jump_after_watchdog_reset(void)
184 #ifndef BOOTLOADER_HALFKAY
185 if ((MCUSR & (1<<WDRF)) && reset_key == BOOTLOADER_RESET_KEY) {
188 // My custom USBasploader requires this to come up.
191 // Seems like Teensy halfkay loader requires clearing WDRF and disabling watchdog.
196 // This is compled into 'icall', address should be in word unit, not byte.
197 #ifdef BOOTLOADER_SIZE
198 ((void (*)(void))( (FLASH_SIZE - BOOTLOADER_SIZE) >> 1))();
200 asm("ijmp" :: "z" (bootloader_start));
209 * USBaspLoader - I'm not sure if this is used at all in any projects
210 * would love to support it if it is -Jack
212 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
213 // This makes custom USBasploader come up.
217 PORTB = 0; PORTC= 0; PORTD = 0;
218 DDRB = 0; DDRC= 0; DDRD = 0;
220 // disable interrupts
221 EIMSK = 0; EECR = 0; SPCR = 0;
222 ACSR = 0; SPMCSR = 0; WDTCSR = 0; PCICR = 0;
223 TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0;
224 ADCSRA = 0; TWCR = 0; UCSR0B = 0;
227 // This is compled into 'icall', address should be in word unit, not byte.
228 ((void (*)(void))(BOOTLOADER_START/2))();