4 #include <avr/interrupt.h>
6 #include <util/delay.h>
7 #include "bootloader.h"
10 #include <LUFA/Drivers/USB/USB.h>
14 /* Boot Section Size in bytes
16 * Atmel DFU loader 4096
17 * LUFA bootloader 4096
22 #define FLASH_SIZE (FLASHEND + 1)
23 #define BOOTLOADER_START (FLASH_SIZE - BOOT_SIZE)
27 * Entering the Bootloader via Software
28 * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html
30 #define BOOTLOADER_RESET_KEY 0xB007B007
31 uint32_t reset_key __attribute__ ((section (".noinit")));
33 /* initialize MCU status by watchdog reset */
34 void bootloader_jump(void) {
50 reset_key = BOOTLOADER_RESET_KEY;
51 wdt_enable(WDTO_250MS);
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)
60 if ((MCUSR & (1<<WDRF)) && reset_key == BOOTLOADER_RESET_KEY) {
62 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
63 // This makes custom USBasploader come up.
68 ((void (*)(void))BOOTLOADER_START)();
74 /* Jumping To The Bootloader
75 * http://www.pjrc.com/teensy/jump_to_bootloader.html
77 * This method doen't work when using LUFA. idk why.
78 * - needs to initialize more regisers or interrupt setting?
80 void bootloader_jump(void) {
98 #if defined(__AVR_AT90USB162__)
99 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
100 TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
101 DDRB = 0; DDRC = 0; DDRD = 0;
102 PORTB = 0; PORTC = 0; PORTD = 0;
103 #elif defined(__AVR_ATmega32U4__)
104 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
105 TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
106 DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
107 PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
108 #elif defined(__AVR_AT90USB646__)
109 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
110 TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
111 DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
112 PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
113 #elif defined(__AVR_AT90USB1286__)
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;
123 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
124 // This makes custom USBasploader come up.
128 PORTB = 0; PORTC= 0; PORTD = 0;
129 DDRB = 0; DDRC= 0; DDRD = 0;
131 // disable interrupts
132 EIMSK = 0; EECR = 0; SPCR = 0;
133 ACSR = 0; SPMCSR = 0; WDTCSR = 0; PCICR = 0;
134 TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0;
135 ADCSRA = 0; TWCR = 0; UCSR0B = 0;
139 ((void (*)(void))BOOTLOADER_START)();