]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/avr/bootloader.c
ee150817c36f3e15061d1d0642da8f56ab5a30e9
[qmk_firmware.git] / tmk_core / common / avr / bootloader.c
1 #include <stdint.h>
2 #include <stdbool.h>
3 #include <avr/io.h>
4 #include <avr/eeprom.h>
5 #include <avr/interrupt.h>
6 #include <avr/wdt.h>
7 #include <util/delay.h>
8 #include "bootloader.h"
9 #include <avr/boot.h>
10
11 #ifdef PROTOCOL_LUFA
12 #include <LUFA/Drivers/USB/USB.h>
13 #endif
14
15
16 /* Bootloader Size in *bytes*
17  *
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'.
20  *
21  *
22  * Size of Bootloaders in bytes:
23  *   Atmel DFU loader(ATmega32U4)   4096
24  *   Atmel DFU loader(AT90USB128)   8192
25  *   LUFA bootloader(ATmega32U4)    4096
26  *   Arduino Caterina(ATmega32U4)   4096
27  *   USBaspLoader(ATmega***)        2048
28  *   Teensy   halfKay(ATmega32U4)   512
29  *   Teensy++ halfKay(AT90USB128)   1024
30  *
31  *
32  * AVR Boot section is located at the end of Flash memory like the followings.
33  *
34  *
35  * byte     Atmel/LUFA(ATMega32u4)          byte     Atmel(AT90SUB128)
36  * 0x0000   +---------------+               0x00000  +---------------+
37  *          |               |                        |               |
38  *          |               |                        |               |
39  *          |  Application  |                        |  Application  |
40  *          |               |                        |               |
41  *          =               =                        =               =
42  *          |               | 32KB-4KB               |               | 128KB-8KB
43  * 0x7000   +---------------+               0x1E000  +---------------+
44  *          |  Bootloader   | 4KB                    |  Bootloader   | 8KB
45  * 0x7FFF   +---------------+               0x1FFFF  +---------------+
46  *
47  *
48  * byte     Teensy(ATMega32u4)              byte     Teensy++(AT90SUB128)
49  * 0x0000   +---------------+               0x00000  +---------------+
50  *          |               |                        |               |
51  *          |               |                        |               |
52  *          |  Application  |                        |  Application  |
53  *          |               |                        |               |
54  *          =               =                        =               =
55  *          |               | 32KB-512B              |               | 128KB-1KB
56  * 0x7E00   +---------------+               0x1FC00  +---------------+
57  *          |  Bootloader   | 512B                   |  Bootloader   | 1KB
58  * 0x7FFF   +---------------+               0x1FFFF  +---------------+
59  */
60
61 #define FLASH_SIZE (FLASHEND + 1L)
62
63 #if !defined(BOOTLOADER_SIZE)
64     uint16_t bootloader_start;
65 #endif
66
67 #define BOOT_SIZE_256  0b110
68 #define BOOT_SIZE_512  0b100
69 #define BOOT_SIZE_1024 0b010
70 #define BOOT_SIZE_2048 0b000
71
72 /*
73  * Entering the Bootloader via Software
74  * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html
75  */
76 #define BOOTLOADER_RESET_KEY 0xB007B007
77 uint32_t reset_key  __attribute__ ((section (".noinit")));
78
79 /* initialize MCU status by watchdog reset */
80 void bootloader_jump(void) {
81
82     #if !defined(BOOTLOADER_SIZE)
83         uint8_t high_fuse = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
84
85         if (high_fuse & BOOT_SIZE_256) { 
86             bootloader_start = (FLASH_SIZE - 512) >> 1;
87         } else if (high_fuse & BOOT_SIZE_512) {
88             bootloader_start = (FLASH_SIZE - 1024) >> 1;
89         } else if (high_fuse & BOOT_SIZE_1024) {
90             bootloader_start = (FLASH_SIZE - 2048) >> 1;
91         } else {
92             bootloader_start = (FLASH_SIZE - 4096) >> 1;
93         }
94     #endif
95
96     // Something like this might work, but it compiled larger than the block above
97     // bootloader_start = FLASH_SIZE - (256 << (~high_fuse & 0b110 >> 1));
98
99
100     #if defined(BOOTLOADER_HALFKAY)
101         //  http://www.pjrc.com/teensy/jump_to_bootloader.html
102         cli();
103         // disable watchdog, if enabled (it's not)
104         // disable all peripherals
105         // a shutdown call might make sense here
106         UDCON = 1;
107         USBCON = (1<<FRZCLK);  // disable USB
108         UCSR1B = 0;
109         _delay_ms(5);
110         #if defined(__AVR_AT90USB162__)                // Teensy 1.0
111             EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
112             TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
113             DDRB = 0; DDRC = 0; DDRD = 0;
114             PORTB = 0; PORTC = 0; PORTD = 0;
115             asm volatile("jmp 0x3E00");
116         #elif defined(__AVR_ATmega32U4__)              // Teensy 2.0
117             EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
118             TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
119             DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
120             PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
121             asm volatile("jmp 0x7E00");
122         #elif defined(__AVR_AT90USB646__)              // Teensy++ 1.0
123             EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
124             TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
125             DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
126             PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
127             asm volatile("jmp 0xFC00");
128         #elif defined(__AVR_AT90USB1286__)             // Teensy++ 2.0
129             EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
130             TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
131             DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
132             PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
133             asm volatile("jmp 0x1FC00");
134         #endif 
135
136     #elif defined(BOOTLOADER_CATERINA)
137         // this block may be optional
138         // TODO: figure it out
139
140         uint16_t *const bootKeyPtr = (uint16_t *)0x0800;
141
142         // Value used by Caterina bootloader use to determine whether to run the
143         // sketch or the bootloader programmer.
144         uint16_t bootKey = 0x7777;
145
146         *bootKeyPtr = bootKey;
147
148         // setup watchdog timeout
149         wdt_enable(WDTO_60MS);
150
151         while(1) {} // wait for watchdog timer to trigger
152
153     #else // Assume remaining boards are DFU, even if the flag isn't set
154
155         #ifndef __AVR_ATmega32A__ // no USB - maybe BOOTLOADER_BOOTLOADHID instead though?
156             UDCON = 1;
157             USBCON = (1<<FRZCLK);  // disable USB
158             UCSR1B = 0;
159             _delay_ms(5); // 5 seems to work fine
160         #endif
161
162         #ifdef BOOTLOADER_BOOTLOADHID
163             // force bootloadHID to stay in bootloader mode, so that it waits
164             // for a new firmware to be flashed
165             eeprom_write_byte((uint8_t *)1, 0x00);
166         #endif
167
168         // watchdog reset
169         reset_key = BOOTLOADER_RESET_KEY;
170         wdt_enable(WDTO_250MS);
171         for (;;);
172     #endif
173
174 }
175
176 #ifdef __AVR_ATmega32A__
177     // MCUSR is actually called MCUCSR in ATmega32A
178     #define MCUSR MCUCSR
179 #endif
180
181 /* this runs before main() */
182 void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3")));
183 void bootloader_jump_after_watchdog_reset(void)
184 {
185     #ifndef BOOTLOADER_HALFKAY
186         if ((MCUSR & (1<<WDRF)) && reset_key == BOOTLOADER_RESET_KEY) {
187             reset_key = 0;
188
189             // My custom USBasploader requires this to come up.
190             MCUSR = 0;
191
192             // Seems like Teensy halfkay loader requires clearing WDRF and disabling watchdog.
193             MCUSR &= ~(1<<WDRF);
194             wdt_disable();
195
196
197             // This is compled into 'icall', address should be in word unit, not byte.
198             #ifdef BOOTLOADER_SIZE
199                 ((void (*)(void))( (FLASH_SIZE - BOOTLOADER_SIZE) >> 1))();
200             #else
201                 asm("ijmp" :: "z" (bootloader_start));
202             #endif
203         }
204     #endif
205 }
206
207
208 #if 0
209     /*
210      * USBaspLoader - I'm not sure if this is used at all in any projects
211      *                would love to support it if it is -Jack
212      */
213 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
214     // This makes custom USBasploader come up.
215     MCUSR = 0;
216
217     // initialize ports
218     PORTB = 0; PORTC= 0; PORTD = 0;
219     DDRB = 0; DDRC= 0; DDRD = 0;
220
221     // disable interrupts
222     EIMSK = 0; EECR = 0; SPCR = 0;
223     ACSR = 0; SPMCSR = 0; WDTCSR = 0; PCICR = 0;
224     TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0;
225     ADCSRA = 0; TWCR = 0; UCSR0B = 0;
226 #endif
227
228     // This is compled into 'icall', address should be in word unit, not byte.
229     ((void (*)(void))(BOOTLOADER_START/2))();
230 }
231 #endif