aboutsummaryrefslogtreecommitdiff
path: root/common/bootloader.c
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2013-03-11 15:35:55 +0900
committertmk <nobody@nowhere>2013-03-11 15:35:55 +0900
commit48433a5e9988647a737234c11dd9db4080fd4a4e (patch)
tree4af03a20658cb7e6cd43f9c65dfa002f1b544332 /common/bootloader.c
parent5d6b848a157a2e94859949961297d40da6a77527 (diff)
parentef8439bddb2d7fe5fd95faf2b6bebd8235acf160 (diff)
downloadqmk_firmware-48433a5e9988647a737234c11dd9db4080fd4a4e.tar.gz
qmk_firmware-48433a5e9988647a737234c11dd9db4080fd4a4e.zip
Merge branch 'eeprom_config'
Diffstat (limited to 'common/bootloader.c')
-rw-r--r--common/bootloader.c116
1 files changed, 88 insertions, 28 deletions
diff --git a/common/bootloader.c b/common/bootloader.c
index 6e04efbbd..43a7e47ce 100644
--- a/common/bootloader.c
+++ b/common/bootloader.c
@@ -1,45 +1,106 @@
1#include <stdint.h>
2#include <stdbool.h>
1#include <avr/io.h> 3#include <avr/io.h>
2#include <avr/interrupt.h> 4#include <avr/interrupt.h>
5#include <avr/wdt.h>
3#include <util/delay.h> 6#include <util/delay.h>
4#include "bootloader.h" 7#include "bootloader.h"
5 8
6/* Start Bootloader from Application 9#ifdef PROTOCOL_LUFA
7 * See 10#include <LUFA/Drivers/USB/USB.h>
8 * http://www.pjrc.com/teensy/jump_to_bootloader.html 11#endif
9 * http://www.fourwalledcubicle.com/files/LUFA/Doc/120219/html/_page__software_bootloader_start.html 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
10 */ 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
11 25
12// TODO: support usbasp 26#define FLASH_SIZE (FLASHEND + 1L)
13/* Boot Section Size in bytes 27#define BOOTLOADER_START (FLASH_SIZE - BOOTLOADER_SIZE)
14 * Teensy halfKay 512 28
15 * Atmel DFU loader 4096 29
16 * LUFA bootloader 4096 30/*
31 * Entering the Bootloader via Software
32 * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html
17 */ 33 */
18#ifndef BOOT_SIZE 34#define BOOTLOADER_RESET_KEY 0xB007B007
19#define BOOT_SIZE 512 35uint32_t reset_key __attribute__ ((section (".noinit")));
36
37/* initialize MCU status by watchdog reset */
38void 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);
20#endif 51#endif
21 52
22#define FLASH_SIZE (FLASHEND + 1) 53 // watchdog reset
23#define BOOTLOADER_START (FLASHEND - BOOT_SIZE) 54 reset_key = BOOTLOADER_RESET_KEY;
55 wdt_enable(WDTO_250MS);
56 for (;;);
57}
24 58
59
60/* this runs before main() */
61void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3")));
62void 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 */
25void bootloader_jump(void) { 86void bootloader_jump(void) {
87#ifdef PROTOCOL_LUFA
88 USB_Disable();
26 cli(); 89 cli();
90 _delay_ms(2000);
91#endif
27 92
28 // 93#ifdef PROTOCOL_PJRC
29 //Teensy 94 cli();
30 //
31#if defined(__AVR_AT90USB162__) || defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
32 // disable watchdog, if enabled
33 // disable all peripherals
34 UDCON = 1; 95 UDCON = 1;
35 USBCON = (1<<FRZCLK); // disable USB 96 USBCON = (1<<FRZCLK);
36 UCSR1B = 0; 97 UCSR1B = 0;
37 _delay_ms(5); 98 _delay_ms(5);
38#else
39 // This makes custom USBasploader come up.
40 MCUSR = 0;
41#endif 99#endif
42 100
101 /*
102 * Initialize
103 */
43#if defined(__AVR_AT90USB162__) 104#if defined(__AVR_AT90USB162__)
44 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; 105 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
45 TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0; 106 TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
@@ -62,10 +123,9 @@ void bootloader_jump(void) {
62 PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0; 123 PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
63#endif 124#endif
64 125
65 126 /*
66 // 127 * USBaspLoader
67 //USBasp 128 */
68 //
69#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__) 129#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
70 // This makes custom USBasploader come up. 130 // This makes custom USBasploader come up.
71 MCUSR = 0; 131 MCUSR = 0;
@@ -81,7 +141,7 @@ void bootloader_jump(void) {
81 ADCSRA = 0; TWCR = 0; UCSR0B = 0; 141 ADCSRA = 0; TWCR = 0; UCSR0B = 0;
82#endif 142#endif
83 143
84
85 // start Bootloader 144 // start Bootloader
86 ((void (*)(void))BOOTLOADER_START)(); 145 ((void (*)(void))BOOTLOADER_START)();
87} 146}
147#endif