6565#define BOOT_SIZE_1024 0b010
6666#define BOOT_SIZE_2048 0b000
6767
68+ //compatibility between ATMega8 and ATMega88
69+ #if !defined (MCUCSR )
70+ #if defined (MCUSR )
71+ #define MCUCSR MCUSR
72+ #endif
73+ #endif
74+
6875/** \brief Entering the Bootloader via Software
6976 *
7077 * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html
@@ -149,6 +156,39 @@ void bootloader_jump(void) {
149156
150157 while (1 ) {} // wait for watchdog timer to trigger
151158
159+ #elif defined(BOOTLOADER_USBASP )
160+ // Taken with permission of Stephan Baerwolf from https://github.com/tinyusbboard/API/blob/master/apipage.c
161+ wdt_enable (WDTO_15MS );
162+ wdt_reset ();
163+ asm volatile (
164+ "cli \n\t"
165+ "ldi r29 , %[ramendhi] \n\t"
166+ "ldi r28 , %[ramendlo] \n\t"
167+ #if (FLASHEND > 131071 )
168+ "ldi r18 , %[bootaddrhi] \n\t"
169+ "st Y+, r18 \n\t"
170+ #endif
171+ "ldi r18 , %[bootaddrme] \n\t"
172+ "st Y+, r18 \n\t"
173+ "ldi r18 , %[bootaddrlo] \n\t"
174+ "st Y+, r18 \n\t"
175+ "out %[mcucsrio], __zero_reg__ \n\t"
176+ "bootloader_startup_loop%=: \n\t"
177+ "rjmp bootloader_startup_loop%= \n\t"
178+ :
179+ : [mcucsrio ] "I" (_SFR_IO_ADDR (MCUCSR )),
180+ #if (FLASHEND > 131071 )
181+ [ramendhi ] "M" (((RAMEND - 2 ) >> 8 ) & 0xff ),
182+ [ramendlo ] "M" (((RAMEND - 2 ) >> 0 ) & 0xff ),
183+ [bootaddrhi ] "M" ((((FLASH_SIZE - BOOTLOADER_SIZE ) >> 1 ) >>16 ) & 0xff ),
184+ #else
185+ [ramendhi ] "M " (((RAMEND - 1) >> 8) & 0xff),
186+ [ramendlo ] "M" (((RAMEND - 1 ) >> 0 ) & 0xff ),
187+ #endif
188+ [bootaddrme ] "M " ((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 8) & 0xff),
189+ [bootaddrlo ] "M" ((((FLASH_SIZE - BOOTLOADER_SIZE ) >> 1 ) >> 0 ) & 0xff )
190+ );
191+
152192 #else // Assume remaining boards are DFU, even if the flag isn't set
153193
154194 #if !(defined(__AVR_ATmega32A__ ) || defined(__AVR_ATmega328P__ )) // no USB - maybe BOOTLOADER_BOOTLOADHID instead though?
@@ -172,24 +212,19 @@ void bootloader_jump(void) {
172212
173213}
174214
175- #ifdef __AVR_ATmega32A__
176- // MCUSR is actually called MCUCSR in ATmega32A
177- #define MCUSR MCUCSR
178- #endif
179-
180215/* this runs before main() */
181216void bootloader_jump_after_watchdog_reset (void ) __attribute__ ((used , naked , section (".init3" )));
182217void bootloader_jump_after_watchdog_reset (void )
183218{
184219 #ifndef BOOTLOADER_HALFKAY
185- if ((MCUSR & (1 <<WDRF )) && reset_key == BOOTLOADER_RESET_KEY ) {
220+ if ((MCUCSR & (1 <<WDRF )) && reset_key == BOOTLOADER_RESET_KEY ) {
186221 reset_key = 0 ;
187222
188223 // My custom USBasploader requires this to come up.
189- MCUSR = 0 ;
224+ MCUCSR = 0 ;
190225
191226 // Seems like Teensy halfkay loader requires clearing WDRF and disabling watchdog.
192- MCUSR &= ~(1 <<WDRF );
227+ MCUCSR &= ~(1 <<WDRF );
193228 wdt_disable ();
194229
195230
@@ -202,29 +237,3 @@ void bootloader_jump_after_watchdog_reset(void)
202237 }
203238 #endif
204239}
205-
206-
207- #if 0
208- /*
209- * USBaspLoader - I'm not sure if this is used at all in any projects
210- * would love to support it if it is -Jack
211- */
212- #if defined(__AVR_ATmega168__ ) || defined(__AVR_ATmega168P__ ) || defined(__AVR_ATmega328P__ )
213- // This makes custom USBasploader come up.
214- MCUSR = 0 ;
215-
216- // initialize ports
217- PORTB = 0 ; PORTC = 0 ; PORTD = 0 ;
218- DDRB = 0 ; DDRC = 0 ; DDRD = 0 ;
219-
220- // disable interrupts
221- EIMSK = 0 ; EECR = 0 ; SPCR = 0 ;
222- ACSR = 0 ; SPMCSR = 0 ; WDTCSR = 0 ; PCICR = 0 ;
223- TIMSK0 = 0 ; TIMSK1 = 0 ; TIMSK2 = 0 ;
224- ADCSRA = 0 ; TWCR = 0 ; UCSR0B = 0 ;
225- #endif
226-
227- // This is compled into 'icall', address should be in word unit, not byte.
228- ((void (* )(void ))(BOOTLOADER_START /2 ))();
229- }
230- #endif
0 commit comments