]> git.donarmstrong.com Git - tmk_firmware.git/blob - common/bootloader.c
Add BOOTLOADER_SIZE and remove BOOT_SIZE
[tmk_firmware.git] / common / bootloader.c
1 #include <stdint.h>
2 #include <stdbool.h>
3 #include <avr/io.h>
4 #include <avr/interrupt.h>
5 #include <avr/wdt.h>
6 #include <util/delay.h>
7 #include "bootloader.h"
8
9 #ifdef PROTOCOL_LUFA
10 #include <LUFA/Drivers/USB/USB.h>
11 #endif
12
13
14 /* Boot Section Size in *BYTEs*
15  *   Teensy   halfKay    512
16  *   Teensy++ halfKay    1024
17  *   Atmel DFU loader    4096
18  *   LUFA bootloader     4096
19  *   USBaspLoader        2048
20  */
21 #ifndef BOOTLOADER_SIZE
22 #warn To use bootloader_jump() you need to define BOOTLOADER_SIZE in config.h.
23 #endif
24
25 #define FLASH_SIZE          (FLASHEND + 1L)
26 #define BOOTLOADER_START    (FLASH_SIZE - BOOTLOADER_SIZE)
27
28
29 /* 
30  * Entering the Bootloader via Software 
31  * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html
32  */
33 #define BOOTLOADER_RESET_KEY 0xB007B007
34 uint32_t reset_key  __attribute__ ((section (".noinit")));
35
36 /* initialize MCU status by watchdog reset */
37 void bootloader_jump(void) {
38 #ifdef PROTOCOL_LUFA
39     USB_Disable();
40     cli();
41     _delay_ms(2000);
42 #endif
43
44 #ifdef PROTOCOL_PJRC
45     cli();
46     UDCON = 1;
47     USBCON = (1<<FRZCLK);
48     UCSR1B = 0;
49     _delay_ms(5);
50 #endif
51
52     // watchdog reset
53     reset_key = BOOTLOADER_RESET_KEY;
54     wdt_enable(WDTO_250MS);
55     for (;;);
56 }
57
58
59 /* this runs before main() */
60 void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3")));
61 void bootloader_jump_after_watchdog_reset(void)
62 {
63     if ((MCUSR & (1<<WDRF)) && reset_key == BOOTLOADER_RESET_KEY) {
64         reset_key = 0;
65
66         // My custom USBasploader requires this to come up.
67         MCUSR = 0;
68
69         // Seems like Teensy halfkay loader requires clearing WDRF and disabling watchdog.
70         MCUSR &= ~(1<<WDRF);
71         wdt_disable();
72
73         ((void (*)(void))BOOTLOADER_START)();
74     }
75 }
76
77
78 #if 0
79 /* Jumping To The Bootloader
80  * http://www.pjrc.com/teensy/jump_to_bootloader.html
81  * 
82  * This method doen't work when using LUFA. idk why.
83  * - needs to initialize more regisers or interrupt setting?
84  */
85 void bootloader_jump(void) {
86 #ifdef PROTOCOL_LUFA
87     USB_Disable();
88     cli();
89     _delay_ms(2000);
90 #endif
91
92 #ifdef PROTOCOL_PJRC
93     cli();
94     UDCON = 1;
95     USBCON = (1<<FRZCLK);
96     UCSR1B = 0;
97     _delay_ms(5);
98 #endif
99
100     /*
101      * Initialize
102      */
103 #if defined(__AVR_AT90USB162__)
104     EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
105     TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
106     DDRB = 0; DDRC = 0; DDRD = 0;
107     PORTB = 0; PORTC = 0; PORTD = 0;
108 #elif defined(__AVR_ATmega32U4__)
109     EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
110     TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
111     DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
112     PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
113 #elif defined(__AVR_AT90USB646__)
114     EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
115     TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
116     DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
117     PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
118 #elif defined(__AVR_AT90USB1286__)
119     EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
120     TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
121     DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
122     PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
123 #endif
124
125     /*
126      * USBaspLoader
127      */
128 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
129     // This makes custom USBasploader come up.
130     MCUSR = 0;
131
132     // initialize ports
133     PORTB = 0; PORTC= 0; PORTD = 0;
134     DDRB = 0; DDRC= 0; DDRD = 0;
135
136     // disable interrupts
137     EIMSK = 0; EECR = 0; SPCR = 0;
138     ACSR = 0; SPMCSR = 0; WDTCSR = 0; PCICR = 0;
139     TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0;
140     ADCSRA = 0; TWCR = 0; UCSR0B = 0;
141 #endif
142
143     // start Bootloader
144     ((void (*)(void))BOOTLOADER_START)();
145 }
146 #endif