diff options
Diffstat (limited to 'common/bootloader.c')
| -rw-r--r-- | common/bootloader.c | 100 |
1 files changed, 77 insertions, 23 deletions
diff --git a/common/bootloader.c b/common/bootloader.c index 6e04efbbd..77fa1b30a 100644 --- a/common/bootloader.c +++ b/common/bootloader.c | |||
| @@ -1,15 +1,16 @@ | |||
| 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 | |
| 10 | */ | ||
| 11 | 13 | ||
| 12 | // TODO: support usbasp | ||
| 13 | /* Boot Section Size in bytes | 14 | /* Boot Section Size in bytes |
| 14 | * Teensy halfKay 512 | 15 | * Teensy halfKay 512 |
| 15 | * Atmel DFU loader 4096 | 16 | * Atmel DFU loader 4096 |
| @@ -18,28 +19,82 @@ | |||
| 18 | #ifndef BOOT_SIZE | 19 | #ifndef BOOT_SIZE |
| 19 | #define BOOT_SIZE 512 | 20 | #define BOOT_SIZE 512 |
| 20 | #endif | 21 | #endif |
| 21 | |||
| 22 | #define FLASH_SIZE (FLASHEND + 1) | 22 | #define FLASH_SIZE (FLASHEND + 1) |
| 23 | #define BOOTLOADER_START (FLASHEND - BOOT_SIZE) | 23 | #define BOOTLOADER_START (FLASH_SIZE - BOOT_SIZE) |
| 24 | 24 | ||
| 25 | |||
| 26 | /* | ||
| 27 | * Entering the Bootloader via Software | ||
| 28 | * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html | ||
| 29 | */ | ||
| 30 | #define BOOTLOADER_RESET_KEY 0xB007B007 | ||
| 31 | uint32_t reset_key __attribute__ ((section (".noinit"))); | ||
| 32 | |||
| 33 | /* initialize MCU status by watchdog reset */ | ||
| 25 | void bootloader_jump(void) { | 34 | void bootloader_jump(void) { |
| 35 | #ifdef PROTOCOL_LUFA | ||
| 36 | USB_Disable(); | ||
| 26 | cli(); | 37 | cli(); |
| 38 | _delay_ms(2000); | ||
| 39 | #endif | ||
| 27 | 40 | ||
| 28 | // | 41 | #ifdef PROTOCOL_PJRC |
| 29 | //Teensy | 42 | 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; | 43 | UDCON = 1; |
| 35 | USBCON = (1<<FRZCLK); // disable USB | 44 | USBCON = (1<<FRZCLK); |
| 36 | UCSR1B = 0; | 45 | UCSR1B = 0; |
| 37 | _delay_ms(5); | 46 | _delay_ms(5); |
| 38 | #else | ||
| 39 | // This makes custom USBasploader come up. | ||
| 40 | MCUSR = 0; | ||
| 41 | #endif | 47 | #endif |
| 42 | 48 | ||
| 49 | // watchdog reset | ||
| 50 | reset_key = BOOTLOADER_RESET_KEY; | ||
| 51 | wdt_enable(WDTO_250MS); | ||
| 52 | for (;;); | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | /* this runs before main() */ | ||
| 57 | void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3"))); | ||
| 58 | void bootloader_jump_after_watchdog_reset(void) | ||
| 59 | { | ||
| 60 | if ((MCUSR & (1<<WDRF)) && reset_key == BOOTLOADER_RESET_KEY) { | ||
| 61 | |||
| 62 | #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__) | ||
| 63 | // This makes custom USBasploader come up. | ||
| 64 | MCUSR = 0; | ||
| 65 | #endif | ||
| 66 | |||
| 67 | reset_key = 0; | ||
| 68 | ((void (*)(void))BOOTLOADER_START)(); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | #if 0 | ||
| 74 | /* Jumping To The Bootloader | ||
| 75 | * http://www.pjrc.com/teensy/jump_to_bootloader.html | ||
| 76 | * | ||
| 77 | * This method doen't work when using LUFA. idk why. | ||
| 78 | * - needs to initialize more regisers or interrupt setting? | ||
| 79 | */ | ||
| 80 | void bootloader_jump(void) { | ||
| 81 | #ifdef PROTOCOL_LUFA | ||
| 82 | USB_Disable(); | ||
| 83 | cli(); | ||
| 84 | _delay_ms(2000); | ||
| 85 | #endif | ||
| 86 | |||
| 87 | #ifdef PROTOCOL_PJRC | ||
| 88 | cli(); | ||
| 89 | UDCON = 1; | ||
| 90 | USBCON = (1<<FRZCLK); | ||
| 91 | UCSR1B = 0; | ||
| 92 | _delay_ms(5); | ||
| 93 | #endif | ||
| 94 | |||
| 95 | /* | ||
| 96 | * Initialize | ||
| 97 | */ | ||
| 43 | #if defined(__AVR_AT90USB162__) | 98 | #if defined(__AVR_AT90USB162__) |
| 44 | EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; | 99 | EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; |
| 45 | TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0; | 100 | TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0; |
| @@ -62,10 +117,9 @@ void bootloader_jump(void) { | |||
| 62 | PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0; | 117 | PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0; |
| 63 | #endif | 118 | #endif |
| 64 | 119 | ||
| 65 | 120 | /* | |
| 66 | // | 121 | * USBaspLoader |
| 67 | //USBasp | 122 | */ |
| 68 | // | ||
| 69 | #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__) | 123 | #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__) |
| 70 | // This makes custom USBasploader come up. | 124 | // This makes custom USBasploader come up. |
| 71 | MCUSR = 0; | 125 | MCUSR = 0; |
| @@ -81,7 +135,7 @@ void bootloader_jump(void) { | |||
| 81 | ADCSRA = 0; TWCR = 0; UCSR0B = 0; | 135 | ADCSRA = 0; TWCR = 0; UCSR0B = 0; |
| 82 | #endif | 136 | #endif |
| 83 | 137 | ||
| 84 | |||
| 85 | // start Bootloader | 138 | // start Bootloader |
| 86 | ((void (*)(void))BOOTLOADER_START)(); | 139 | ((void (*)(void))BOOTLOADER_START)(); |
| 87 | } | 140 | } |
| 141 | #endif | ||
