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