]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/avr/bootloader.c
Usbasploader bootloader option addition (#6304)
[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 /** \brief 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  * 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
29  *
30  * AVR Boot section is located at the end of Flash memory like the followings.
31  *
32  * byte     Atmel/LUFA(ATMega32u4)          byte     Atmel(AT90SUB128)
33  * 0x0000   +---------------+               0x00000  +---------------+
34  *          |               |                        |               |
35  *          |               |                        |               |
36  *          |  Application  |                        |  Application  |
37  *          |               |                        |               |
38  *          =               =                        =               =
39  *          |               | 32KB-4KB               |               | 128KB-8KB
40  * 0x7000   +---------------+               0x1E000  +---------------+
41  *          |  Bootloader   | 4KB                    |  Bootloader   | 8KB
42  * 0x7FFF   +---------------+               0x1FFFF  +---------------+
43  *
44  *
45  * byte     Teensy(ATMega32u4)              byte     Teensy++(AT90SUB128)
46  * 0x0000   +---------------+               0x00000  +---------------+
47  *          |               |                        |               |
48  *          |               |                        |               |
49  *          |  Application  |                        |  Application  |
50  *          |               |                        |               |
51  *          =               =                        =               =
52  *          |               | 32KB-512B              |               | 128KB-1KB
53  * 0x7E00   +---------------+               0x1FC00  +---------------+
54  *          |  Bootloader   | 512B                   |  Bootloader   | 1KB
55  * 0x7FFF   +---------------+               0x1FFFF  +---------------+
56  */
57 #define FLASH_SIZE (FLASHEND + 1L)
58
59 #if !defined(BOOTLOADER_SIZE)
60     uint16_t bootloader_start;
61 #endif
62
63 #define BOOT_SIZE_256  0b110
64 #define BOOT_SIZE_512  0b100
65 #define BOOT_SIZE_1024 0b010
66 #define BOOT_SIZE_2048 0b000
67
68 //compatibility between ATMega8 and ATMega88
69 #if !defined (MCUCSR)
70     #if defined (MCUSR)
71         #define MCUCSR MCUSR
72     #endif
73 #endif
74
75 /** \brief Entering the Bootloader via Software
76  *
77  * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html
78  */
79 #define BOOTLOADER_RESET_KEY 0xB007B007
80 uint32_t reset_key  __attribute__ ((section (".noinit,\"aw\",@nobits;")));
81
82 /** \brief initialize MCU status by watchdog reset
83  *
84  * FIXME: needs doc
85  */
86 void bootloader_jump(void) {
87
88     #if !defined(BOOTLOADER_SIZE)
89         uint8_t high_fuse = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
90
91         if (high_fuse & BOOT_SIZE_256) {
92             bootloader_start = (FLASH_SIZE - 512) >> 1;
93         } else if (high_fuse & BOOT_SIZE_512) {
94             bootloader_start = (FLASH_SIZE - 1024) >> 1;
95         } else if (high_fuse & BOOT_SIZE_1024) {
96             bootloader_start = (FLASH_SIZE - 2048) >> 1;
97         } else {
98             bootloader_start = (FLASH_SIZE - 4096) >> 1;
99         }
100     #endif
101
102     // Something like this might work, but it compiled larger than the block above
103     // bootloader_start = FLASH_SIZE - (256 << (~high_fuse & 0b110 >> 1));
104
105
106     #if defined(BOOTLOADER_HALFKAY)
107         //  http://www.pjrc.com/teensy/jump_to_bootloader.html
108         cli();
109         // disable watchdog, if enabled (it's not)
110         // disable all peripherals
111         // a shutdown call might make sense here
112         UDCON = 1;
113         USBCON = (1<<FRZCLK);  // disable USB
114         UCSR1B = 0;
115         _delay_ms(5);
116         #if defined(__AVR_AT90USB162__)                // Teensy 1.0
117             EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
118             TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
119             DDRB = 0; DDRC = 0; DDRD = 0;
120             PORTB = 0; PORTC = 0; PORTD = 0;
121             asm volatile("jmp 0x3E00");
122         #elif defined(__AVR_ATmega32U4__)              // Teensy 2.0
123             EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
124             TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
125             DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
126             PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
127             asm volatile("jmp 0x7E00");
128         #elif defined(__AVR_AT90USB646__)              // Teensy++ 1.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 0xFC00");
134         #elif defined(__AVR_AT90USB1286__)             // Teensy++ 2.0
135             EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
136             TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
137             DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
138             PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
139             asm volatile("jmp 0x1FC00");
140         #endif
141
142     #elif defined(BOOTLOADER_CATERINA)
143         // this block may be optional
144         // TODO: figure it out
145
146         uint16_t *const bootKeyPtr = (uint16_t *)0x0800;
147
148         // Value used by Caterina bootloader use to determine whether to run the
149         // sketch or the bootloader programmer.
150         uint16_t bootKey = 0x7777;
151
152         *bootKeyPtr = bootKey;
153
154         // setup watchdog timeout
155         wdt_enable(WDTO_60MS);
156
157         while(1) {} // wait for watchdog timer to trigger
158
159     #elif defined(BOOTLOADER_USBASP)
160         // Taken with permission of Stephan Baerwolf from https://github.com/tinyusbboard/API/blob/master/apipage.c
161         wdt_enable(WDTO_15MS);
162         wdt_reset();
163         asm volatile (
164             "cli                    \n\t"
165             "ldi    r29 ,       %[ramendhi] \n\t"
166             "ldi    r28 ,       %[ramendlo] \n\t"
167             #if (FLASHEND>131071)
168                 "ldi    r18 ,       %[bootaddrhi]   \n\t"
169                 "st     Y+,         r18     \n\t"
170             #endif
171             "ldi    r18 ,       %[bootaddrme]   \n\t"
172             "st     Y+,     r18     \n\t"
173             "ldi    r18 ,       %[bootaddrlo]   \n\t"
174             "st     Y+,     r18     \n\t"
175             "out    %[mcucsrio],    __zero_reg__    \n\t"
176             "bootloader_startup_loop%=:         \n\t"
177             "rjmp bootloader_startup_loop%=     \n\t"
178             : 
179             : [mcucsrio]    "I" (_SFR_IO_ADDR(MCUCSR)),
180             #if (FLASHEND>131071)
181                 [ramendhi]    "M" (((RAMEND - 2) >> 8) & 0xff),
182                 [ramendlo]    "M" (((RAMEND - 2) >> 0) & 0xff),
183                 [bootaddrhi]  "M" ((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >>16) & 0xff),
184             #else
185                 [ramendhi]    "M" (((RAMEND - 1) >> 8) & 0xff),
186                 [ramendlo]    "M" (((RAMEND - 1) >> 0) & 0xff),
187             #endif
188             [bootaddrme]  "M" ((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 8) & 0xff),
189             [bootaddrlo]  "M" ((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 0) & 0xff)
190         );
191
192     #else // Assume remaining boards are DFU, even if the flag isn't set
193
194         #if !(defined(__AVR_ATmega32A__) || defined(__AVR_ATmega328P__)) // no USB - maybe BOOTLOADER_BOOTLOADHID instead though?
195             UDCON = 1;
196             USBCON = (1<<FRZCLK);  // disable USB
197             UCSR1B = 0;
198             _delay_ms(5); // 5 seems to work fine
199         #endif
200
201         #ifdef BOOTLOADER_BOOTLOADHID
202             // force bootloadHID to stay in bootloader mode, so that it waits
203             // for a new firmware to be flashed
204             eeprom_write_byte((uint8_t *)1, 0x00);
205         #endif
206
207         // watchdog reset
208         reset_key = BOOTLOADER_RESET_KEY;
209         wdt_enable(WDTO_250MS);
210         for (;;);
211     #endif
212
213 }
214
215 /* this runs before main() */
216 void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3")));
217 void bootloader_jump_after_watchdog_reset(void)
218 {
219     #ifndef BOOTLOADER_HALFKAY
220         if ((MCUCSR & (1<<WDRF)) && reset_key == BOOTLOADER_RESET_KEY) {
221             reset_key = 0;
222
223             // My custom USBasploader requires this to come up.
224             MCUCSR = 0;
225
226             // Seems like Teensy halfkay loader requires clearing WDRF and disabling watchdog.
227             MCUCSR &= ~(1<<WDRF);
228             wdt_disable();
229
230
231             // This is compled into 'icall', address should be in word unit, not byte.
232             #ifdef BOOTLOADER_SIZE
233                 ((void (*)(void))( (FLASH_SIZE - BOOTLOADER_SIZE) >> 1))();
234             #else
235                 asm("ijmp" :: "z" (bootloader_start));
236             #endif
237         }
238     #endif
239 }