2 * Project: USBaspLoader
3 * Author: Christian Starkjohann
4 * Author: Stephan Baerwolf
5 * Creation Date: 2007-12-08
6 * Modification Date: 2013-03-31
8 * Copyright: (c) 2007 by OBJECTIVE DEVELOPMENT Software GmbH
9 * License: GNU GPL v2 (see License.txt)
12 #include "spminterface.h" /* must be included as first! */
15 #include <avr/interrupt.h>
16 #include <avr/pgmspace.h>
19 #include <avr/eeprom.h>
20 #include <util/delay.h>
25 * 29.09.2012 / 30.09.2012
27 * Since cpufunc.h is not needed in this context and
28 * since it is not available in all toolchains, this include
29 * becomes deactivated by github issue-report.
30 * (In case of trouble it remains in sourcecode for reactivation.)
32 * The autor would like to thank Lena-M for reporting this
33 * issue (https://github.com/baerwolf/USBaspLoader/issues/1).
35 #include <avr/cpufunc.h>
44 #include "bootloaderconfig.h"
45 #include "usbdrv/usbdrv.c"
47 #ifndef BOOTLOADER_ADDRESS
48 #error need to know the bootloaders flash address!
50 #define BOOTLOADER_PAGEADDR (BOOTLOADER_ADDRESS - (BOOTLOADER_ADDRESS % SPM_PAGESIZE))
52 /* ------------------------------------------------------------------------ */
54 /* Request constants used by USBasp */
55 #define USBASP_FUNC_CONNECT 1
56 #define USBASP_FUNC_DISCONNECT 2
57 #define USBASP_FUNC_TRANSMIT 3
58 #define USBASP_FUNC_READFLASH 4
59 #define USBASP_FUNC_ENABLEPROG 5
60 #define USBASP_FUNC_WRITEFLASH 6
61 #define USBASP_FUNC_READEEPROM 7
62 #define USBASP_FUNC_WRITEEEPROM 8
63 #define USBASP_FUNC_SETLONGADDRESS 9
65 // additional USBasp Commands
66 #define USBASP_FUNC_SETISPSCK 10
67 #define USBASP_FUNC_TPI_CONNECT 11
68 #define USBASP_FUNC_TPI_DISCONNECT 12
69 #define USBASP_FUNC_TPI_RAWREAD 13
70 #define USBASP_FUNC_TPI_RAWWRITE 14
71 #define USBASP_FUNC_TPI_READBLOCK 15
72 #define USBASP_FUNC_TPI_WRITEBLOCK 16
73 #define USBASP_FUNC_GETCAPABILITIES 127
74 /* ------------------------------------------------------------------------ */
77 # define ulong unsigned long
80 # define uint unsigned int
84 /* allow compatibility with avrusbboot's bootloaderconfig.h: */
85 #ifdef BOOTLOADER_INIT
86 # define bootLoaderInit() BOOTLOADER_INIT
87 # define bootLoaderExit()
89 #ifdef BOOTLOADER_CONDITION
90 # define bootLoaderCondition() BOOTLOADER_CONDITION
93 /* device compatibility: */
94 #ifndef GICR /* ATMega*8 don't have GICR, use MCUCR instead */
98 /* ------------------------------------------------------------------------ */
100 #if (FLASHEND) > 0xffff /* we need long addressing */
101 # define CURRENT_ADDRESS currentAddress.l
102 # define addr_t ulong
104 # define CURRENT_ADDRESS currentAddress.w[0]
108 typedef union longConverter
{
110 uint w
[sizeof(addr_t
)/2];
111 uchar b
[sizeof(addr_t
)];
115 #if (BOOTLOADER_CAN_EXIT)
116 # if (BOOTLOADER_LOOPCYCLES_TIMEOUT)
117 # if (BOOTLOADER_LOOPCYCLES_TIMEOUT < 256)
118 # if ((HAVE_UNPRECISEWAIT))
119 volatile register uint8_t timeout_remaining
__asm__("r2");
121 static volatile uint8_t timeout_remaining
;
124 static volatile uint16_t timeout_remaining
;
128 # define stayinloader_initialValue 0xfe
129 # if ((HAVE_UNPRECISEWAIT))
130 /* here we have to assume we need to optimize for every byte */
131 volatile register uint8_t stayinloader
__asm__("r17");
133 static volatile uint8_t stayinloader
;
137 static longConverter_t currentAddress
; /* in bytes */
138 static uchar bytesRemaining
;
139 static uchar isLastPage
;
140 #if HAVE_EEPROM_PAGED_ACCESS
141 static uchar currentRequest
;
143 static const uchar currentRequest
= 0;
146 static const uchar signatureBytes
[4] = {
147 #ifdef SIGNATURE_BYTES
149 #elif defined (__AVR_ATmega8535__)
151 #elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
153 #elif defined (__AVR_ATmega16__)
155 #elif defined (__AVR_ATmega32__)
157 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48A__) || defined (__AVR_ATmega48P__)
158 #error ATmega48 does not support bootloaders!
160 #elif defined (__AVR_ATmega48PA__)
161 #error ATmega48 does not support bootloaders!
163 #elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88P__)
165 #elif defined (__AVR_ATmega88PA__)
167 #elif defined (__AVR_ATmega164A__)
169 #elif defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__)
171 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168P__)
173 #elif defined (__AVR_ATmega168PA__)
175 #elif defined (__AVR_ATmega324A__)
177 #elif defined (__AVR_ATmega324P__)
179 #elif defined (__AVR_ATmega324PA__)
181 #elif defined (__AVR_ATmega328__)
183 #elif defined (__AVR_ATmega328P__)
185 #elif defined (__AVR_ATmega640__)
187 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__)
189 #elif defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
191 #elif defined (__AVR_ATmega128__)
193 #elif defined (__AVR_ATmega1280__)
195 #elif defined (__AVR_ATmega1281__)
197 #elif defined (__AVR_ATmega1284__)
199 #elif defined (__AVR_ATmega1284P__)
201 #elif defined (__AVR_ATmega2560__)
203 #elif defined (__AVR_ATmega2561__)
206 # if (defined(SIGNATURE_0) && defined(SIGNATURE_1) && defined(SIGNATURE_2))
207 # warning "Device signature is not known - using AVR Libc suggestion..."
208 SIGNATURE_0
, SIGNATURE_1
, SIGNATURE_2
, 0
210 # error "Device signature is not known, please edit main.c!"
215 /* ------------------------------------------------------------------------ */
217 #if (HAVE_BOOTLOADERENTRY_FROMSOFTWARE)
218 void __attribute__ ((section(".init3"),naked
,used
,no_instrument_function
)) __BOOTLOADERENTRY_FROMSOFTWARE__bootup_investigate_RAMEND(void);
219 void __BOOTLOADERENTRY_FROMSOFTWARE__bootup_investigate_RAMEND(void) {
221 "in %[mcucsrval] , %[mcucsrio]\n\t"
222 "ldi r29 , %[ramendhi]\n\t"
223 "ldi r28 , %[ramendlo]\n\t"
224 #if (FLASHEND>131071)
225 "ld %[result] , Y+\n\t"
226 "cpi %[result] , %[bootaddrhi]\n\t"
227 "brne __BOOTLOADERENTRY_FROMSOFTWARE__bootup_investigate_RAMEND_mismatch%=\n\t"
229 "ld %[result] , Y+\n\t"
230 "cpi %[result] , %[bootaddrme]\n\t"
231 "ld %[result] , Y+\n\t"
232 "breq __BOOTLOADERENTRY_FROMSOFTWARE__bootup_investigate_RAMEND_done%=\n\t"
234 "__BOOTLOADERENTRY_FROMSOFTWARE__bootup_investigate_RAMEND_mismatch%=:\n\t"
235 "ldi %[result] , 0xff\n\t"
237 "__BOOTLOADERENTRY_FROMSOFTWARE__bootup_investigate_RAMEND_done%=:\n\t"
238 : [result
] "=a" (__BOOTLOADERENTRY_FROMSOFTWARE__bootup_RAMEND_doesmatch
),
239 [mcucsrval
] "=a" (__BOOTLOADERENTRY_FROMSOFTWARE__bootup_MCUCSR
)
240 : [mcucsrio
] "I" (_SFR_IO_ADDR(MCUCSR
)),
241 #if (FLASHEND>131071)
242 [ramendhi
] "M" (((RAMEND
- 2) >> 8) & 0xff),
243 [ramendlo
] "M" (((RAMEND
- 2) >> 0) & 0xff),
244 [bootaddrhi
] "M" (((__BOOTLOADERENTRY_FROMSOFTWARE__EXPECTEDADDRESS
) >>16) & 0xff),
246 [ramendhi
] "M" (((RAMEND
- 1) >> 8) & 0xff),
247 [ramendlo
] "M" (((RAMEND
- 1) >> 0) & 0xff),
249 [bootaddrme
] "M" (((__BOOTLOADERENTRY_FROMSOFTWARE__EXPECTEDADDRESS
) >> 8) & 0xff)
255 #if (USE_BOOTUP_CLEARRAM)
257 * Under normal circumstances, RESET will not clear contents of RAM.
258 * As always, if you want it done - do it yourself...
260 void __attribute__ ((section(".init3"),naked
,used
,no_instrument_function
)) __func_clearram(void);
261 void __func_clearram(void) {
262 extern size_t __bss_end
;
265 #if (!(HAVE_BOOTLOADERENTRY_FROMSOFTWARE))
266 "ldi r29, %[ramendhi]\n\t"
267 "ldi r28, %[ramendlo]\n\t"
269 "__clearramloop%=:\n\t"
270 "st -Y , __zero_reg__\n\t"
271 "cp r28, %A[bssend]\n\t"
272 "cpc r29, %B[bssend]\n\t"
273 "brne __clearramloop%=\n\t"
275 : [ramendhi
] "M" (((RAMEND
+1)>>8) & 0xff),
276 [ramendlo
] "M" (((RAMEND
+1)>>0) & 0xff),
277 [bssend
] "r" (&__bss_end
)
283 #if (!USE_EXCESSIVE_ASSEMBLER) || (!(defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)))
284 static void (*nullVector
)(void) __attribute__((__noreturn__
));
287 #if (USE_EXCESSIVE_ASSEMBLER) && (defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__))
288 static void __attribute__((naked
,__noreturn__
)) leaveBootloader(void);
289 static void leaveBootloader(void) {
292 "sbi %[usbddr], %[usbminus]\n\t"
293 "cbi %[port], %[bit]\n\t"
294 "out %[usbintrenab], __zero_reg__\n\t"
295 "out %[usbintrcfg], __zero_reg__\n\t"
296 "ldi r31, %[ivce]\n\t"
297 "out %[mygicr], r31\n\t"
298 "out %[mygicr], __zero_reg__\n\t"
299 "rjmp nullVector\n\t"
301 : [port
] "I" (_SFR_IO_ADDR(PIN_PORT(JUMPER_PORT
))),
302 [bit
] "I" (PIN(JUMPER_PORT
, JUMPER_BIT
)),
303 [usbintrenab
] "I" (_SFR_IO_ADDR(USB_INTR_ENABLE
)),
304 [usbintrcfg
] "I" (_SFR_IO_ADDR(USB_INTR_CFG
)),
305 [usbddr
] "I" (_SFR_IO_ADDR(USBDDR
)),
306 [usbminus
] "I" (USBMINUS
),
307 [mygicr
] "I" (_SFR_IO_ADDR(GICR
)),
312 static void __attribute__((__noreturn__
)) leaveBootloader(void);
313 static void leaveBootloader(void) {
316 usbDeviceDisconnect();
319 USB_INTR_CFG
= 0; /* also reset config bits */
320 GICR
= (1 << IVCE
); /* enable change of interrupt vectors */
321 GICR
= (0 << IVSEL
); /* move interrupts to application flash section */
324 * There seems to be another funny compiler Bug.
325 * When gcc is using "eicall" opcode it forgets to modify EIND.
326 * On devices with large flash memory there are some target address bits
327 * missing. In this case some zero bits...
329 #if (defined(EIND) && ((FLASHEND)>131071))
332 /* We must go through a global function pointer variable instead of writing
333 * ((void (*)(void))0)();
334 * because the compiler optimizes a constant 0 to "rcall 0" which is not
335 * handled correctly by the assembler.
341 /* ------------------------------------------------------------------------ */
344 uchar
usbFunctionSetup_USBASP_FUNC_TRANSMIT(usbRequest_t
*rq
) {
347 address
.bytes
[1] = rq
->wValue
.bytes
[1];
348 address
.bytes
[0] = rq
->wIndex
.bytes
[0];
350 if(rq
->wValue
.bytes
[0] == 0x30){ /* read signature */
351 rval
= rq
->wIndex
.bytes
[0] & 3;
352 rval
= signatureBytes
[rval
];
353 #if HAVE_READ_LOCK_FUSE
354 #if defined (__AVR_ATmega8535__) || \
355 defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || \
356 defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__)
357 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x00){ /* read lock bits */
358 rval
= boot_lock_fuse_bits_get(GET_LOCK_BITS
);
359 }else if(rq
->wValue
.bytes
[0] == 0x50 && rq
->wValue
.bytes
[1] == 0x00){ /* read lfuse bits */
360 rval
= boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS
);
361 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x08){ /* read hfuse bits */
362 rval
= boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS
);
364 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48A__) || defined (__AVR_ATmega48P__) || defined (__AVR_ATmega48PA__) || \
365 defined (__AVR_ATmega88__) || defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88P__) || defined (__AVR_ATmega88PA__) || \
366 defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__) || \
367 defined (__AVR_ATmega168__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168P__) || defined (__AVR_ATmega168PA__) || \
368 defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__) || \
369 defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__) || \
370 defined (__AVR_ATmega640__) || \
371 defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__) || \
372 defined (__AVR_ATmega128__) || \
373 defined (__AVR_ATmega1280__) || \
374 defined (__AVR_ATmega1281__) || \
375 defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__) || \
376 defined (__AVR_ATmega2560__) || \
377 defined (__AVR_ATmega2561__)
378 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x00){ /* read lock bits */
379 rval
= boot_lock_fuse_bits_get(GET_LOCK_BITS
);
380 }else if(rq
->wValue
.bytes
[0] == 0x50 && rq
->wValue
.bytes
[1] == 0x00){ /* read lfuse bits */
381 rval
= boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS
);
382 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x08){ /* read hfuse bits */
383 rval
= boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS
);
384 }else if(rq
->wValue
.bytes
[0] == 0x50 && rq
->wValue
.bytes
[1] == 0x08){ /* read efuse bits */
385 rval
= boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS
);
387 #warning "HAVE_READ_LOCK_FUSE is activated but MCU unknown -> will not support this feature"
390 #if HAVE_FLASH_BYTE_READACCESS
391 }else if(rq
->wValue
.bytes
[0] == 0x20){ /* read FLASH low byte */
392 #if ((FLASHEND) > 65535)
393 rval
= pgm_read_byte_far((((addr_t
)address
.word
)<<1)+0);
395 rval
= pgm_read_byte((((addr_t
)address
.word
)<<1)+0);
397 }else if(rq
->wValue
.bytes
[0] == 0x28){ /* read FLASH high byte */
398 #if ((FLASHEND) > 65535)
399 rval
= pgm_read_byte_far((((addr_t
)address
.word
)<<1)+1);
401 rval
= pgm_read_byte((((addr_t
)address
.word
)<<1)+1);
404 #if HAVE_EEPROM_BYTE_ACCESS
405 }else if(rq
->wValue
.bytes
[0] == 0xa0){ /* read EEPROM byte */
406 rval
= eeprom_read_byte((void *)address
.word
);
407 }else if(rq
->wValue
.bytes
[0] == 0xc0){ /* write EEPROM byte */
408 eeprom_write_byte((void *)address
.word
, rq
->wIndex
.bytes
[1]);
411 }else if(rq
->wValue
.bytes
[0] == 0xac && rq
->wValue
.bytes
[1] == 0x80){ /* chip erase */
413 #if HAVE_BLB11_SOFTW_LOCKBIT
414 for(addr
= 0; addr
< (addr_t
)(BOOTLOADER_PAGEADDR
) ; addr
+= SPM_PAGESIZE
) {
416 for(addr
= 0; addr
<= (addr_t
)(FLASHEND
) ; addr
+= SPM_PAGESIZE
) {
418 /* wait and erase page */
420 # ifndef NO_FLASH_WRITE
421 boot_spm_busy_wait();
423 boot_page_erase(addr
);
428 #if ((HAVE_BOOTLOADER_HIDDENEXITCOMMAND) && (BOOTLOADER_CAN_EXIT))
429 # if ((HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0xac) && \
430 (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x20) && (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x28) && \
431 (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x40) && (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x48) && \
432 (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x4c) && \
433 (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0xa0) && \
434 (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0xc0) && \
435 (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x58) && \
436 (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x5c) && \
437 (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x30) && \
438 (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0xac) && \
439 (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x50) && (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x58) && \
440 (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x38))
441 }else if(rq
->wValue
.bytes
[0] == (HAVE_BOOTLOADER_HIDDENEXITCOMMAND
)){ /* cause a bootLoaderExit at disconnect */
442 stayinloader
= 0xf1; /* we need to be connected - so assume it */
446 /* ignore all others, return default value == 0 */
453 usbMsgLen_t
usbFunctionSetup(uchar data
[8])
455 usbRequest_t
*rq
= (void *)data
;
457 static uchar replyBuffer
[4];
459 usbMsgPtr
= (usbMsgPtr_t
)replyBuffer
;
460 if(rq
->bRequest
== USBASP_FUNC_TRANSMIT
){ /* emulate parts of ISP protocol */
461 replyBuffer
[3] = usbFunctionSetup_USBASP_FUNC_TRANSMIT(rq
);
462 len
= (usbMsgLen_t
)4;
463 }else if((rq
->bRequest
== USBASP_FUNC_ENABLEPROG
) || (rq
->bRequest
== USBASP_FUNC_SETISPSCK
)){
464 /* replyBuffer[0] = 0; is never touched and thus always 0 which means success */
465 len
= (usbMsgLen_t
)1;
466 }else if(rq
->bRequest
>= USBASP_FUNC_READFLASH
&& rq
->bRequest
<= USBASP_FUNC_SETLONGADDRESS
){
467 currentAddress
.w
[0] = rq
->wValue
.word
;
468 if(rq
->bRequest
== USBASP_FUNC_SETLONGADDRESS
){
469 #if (FLASHEND) > 0xffff
470 currentAddress
.w
[1] = rq
->wIndex
.word
;
473 bytesRemaining
= rq
->wLength
.bytes
[0];
474 /* if(rq->bRequest == USBASP_FUNC_WRITEFLASH) only evaluated during writeFlash anyway */
475 isLastPage
= rq
->wIndex
.bytes
[1] & 0x02;
476 #if HAVE_EEPROM_PAGED_ACCESS
477 currentRequest
= rq
->bRequest
;
479 len
= USB_NO_MSG
; /* hand over to usbFunctionRead() / usbFunctionWrite() */
482 }else if(rq
->bRequest
== USBASP_FUNC_DISCONNECT
){
484 #if BOOTLOADER_CAN_EXIT
485 # ifdef CONFIG_HAVE__BOOTLOADER_ABORTTIMEOUTONACT
486 /* let the main loop know for ever that here was activity */
487 stayinloader
&= (0xfc);
489 stayinloader
&= (0xfe);
493 /* ignore: others, but could be USBASP_FUNC_CONNECT */
494 #if BOOTLOADER_CAN_EXIT
495 stayinloader
|= (0x01);
501 #if (USE_EXCESSIVE_ASSEMBLER) && ((!HAVE_CHIP_ERASE) || (HAVE_ONDEMAND_PAGEERASE)) && (SPM_PAGESIZE <= 256) && (((BOOTLOADER_PAGEADDR>>0)&0xff) == 0)
502 uchar
usbFunctionWrite(uchar
*data
, uchar len
)
506 DBG1(0x31, (void *)¤tAddress
.l
, 4);
507 if(len
> bytesRemaining
)
508 len
= bytesRemaining
;
509 bytesRemaining
-= len
;
510 isLast
= bytesRemaining
== 0;
511 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
513 for(i
= 0; i
< len
; i
++){
514 eeprom_write_byte((void *)(currentAddress
.w
[0]++), *data
++);
520 "usbFunctionWrite_flashloop:\n\t"
522 "brlo usbFunctionWrite_finished\n\t"
524 #if HAVE_BLB11_SOFTW_LOCKBIT
525 "cpi r31, %[blsaddrhi]\n\t" /* accelerated BLB11_SOFTW_LOCKBIT check */
526 "brsh usbFunctionWrite_finished\n\t"
527 // "brlo usbFunctionWrite_addrunlock_ok\n\t"
528 // "brne usbFunctionWrite_finished\n\t"
529 // "cpi r30, %[blsaddrlo]\n\t"
530 // "brlo usbFunctionWrite_addrunlock_ok\n\t"
531 // "rjmp usbFunctionWrite_finished\n\t"
532 // "usbFunctionWrite_addrunlock_ok:\n\t"
534 "rcall usbFunctionWrite_waitA\n\t"
535 "cli\n\t" /* r0 or r1 may be __zero_reg__ and may become dangerous nonzero within interrupts */
539 "ldi r18, %[pagfillval]\n\t"
540 "rcall usbFunctionWrite_saveflash\n\t" /* page fill */
543 "subi r18, 0xfe\n\t" /* add with 2 */
544 "andi r18, %[pagemask]\n\t"
545 "breq usbFunctionWrite_pageisfull\n\t"
547 "breq usbFunctionWrite_skippageisfull\n\t"
548 "tst %[isLastPage]\n\t"
549 "breq usbFunctionWrite_skippageisfull\n\t"
551 "brne usbFunctionWrite_skippageisfull\n\t"
553 "usbFunctionWrite_pageisfull:\n\t" /* start writing the page */
554 "ldi r18, %[pageraseval]\n\t"
555 "rcall usbFunctionWrite_saveflash\n\t" /* page erase */
556 "rcall usbFunctionWrite_waitA\n\t"
558 "ldi r18, %[pagwriteval]\n\t"
559 "rcall usbFunctionWrite_saveflash\n\t" /* page write */
560 "rcall usbFunctionWrite_waitA\n\t"
562 "in __tmp_reg__, %[spmcr]\n\t"
563 "sbrs __tmp_reg__, %[rwwsbbit]\n\t"
564 "rjmp usbFunctionWrite_skippageisfull\n\t"
565 "ldi r18, %[rwwenrval]\n\t"
566 "rcall usbFunctionWrite_saveflash\n\t" /* reenable rww*/
567 // "rcall usbFunctionWrite_waitA\n\t"
570 "usbFunctionWrite_skippageisfull:\n\t"
572 "rjmp usbFunctionWrite_flashloop\n\t"
574 "usbFunctionWrite_saveflash:\n\t"
576 "out %[spmcr], r18\n\t"
578 "clr __zero_reg__\n\t" /* if r0 or r1 is __zero_reg__ it may have become inconsisten while page-fill */
582 "usbFunctionWrite_waitA:\n\t"
583 "in __tmp_reg__, %[spmcr]\n\t"
584 "sbrc __tmp_reg__, %[spmenbit]\n\t"
585 "rjmp usbFunctionWrite_waitA\n\t"
588 "usbFunctionWrite_finished:\n\t"
589 : [addr
] "+z" (currentAddress
.l
)
591 : [spmenbit
] "I" (SPMEN
),
592 [rwwsbbit
] "I" (RWWSB
),
593 [spmcr
] "I" (_SFR_IO_ADDR(__SPM_REG
)),
594 [pagfillval
] "M" ((1<<SPMEN
)),
595 [pageraseval
] "M" ((1<<PGERS
) | (1<<SPMEN
)),
596 [pagwriteval
] "M" ((1<<PGWRT
) | (1<<SPMEN
)),
597 [rwwenrval
] "M" ((1<<RWWSRE
) | (1<<SPMEN
)),
598 [pagemask
] "M" (SPM_PAGESIZE
-1),
599 #if HAVE_BLB11_SOFTW_LOCKBIT
600 [blsaddrhi
] "M" ((uint8_t)((BOOTLOADER_PAGEADDR
>>8)&0xff)),
601 // [blsaddrlo] "M" ((uint8_t)((BOOTLOADER_PAGEADDR>>0)&0xff)),
603 [islast
] "r" (isLast
),
604 [isLastPage
] "r" (isLastPage
),
614 uchar
usbFunctionWrite(uchar
*data
, uchar len
)
618 DBG1(0x31, (void *)¤tAddress
.l
, 4);
619 if(len
> bytesRemaining
)
620 len
= bytesRemaining
;
621 bytesRemaining
-= len
;
622 isLast
= bytesRemaining
== 0;
623 for(i
= 0; i
< len
;) {
624 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
625 eeprom_write_byte((void *)(currentAddress
.w
[0]++), *data
++);
628 #if HAVE_BLB11_SOFTW_LOCKBIT
629 if (CURRENT_ADDRESS
>= (addr_t
)(BOOTLOADER_PAGEADDR
)) {
636 boot_page_fill(CURRENT_ADDRESS
, *(short *)data
);
638 CURRENT_ADDRESS
+= 2;
640 /* write page when we cross page boundary or we have the last partial page */
641 if((currentAddress
.w
[0] & (SPM_PAGESIZE
- 1)) == 0 || (isLast
&& i
>= len
&& isLastPage
)){
642 #if (!HAVE_CHIP_ERASE) || (HAVE_ONDEMAND_PAGEERASE)
644 # ifndef NO_FLASH_WRITE
646 boot_page_erase(CURRENT_ADDRESS
- 2); /* erase page */
648 boot_spm_busy_wait(); /* wait until page is erased */
652 #ifndef NO_FLASH_WRITE
654 boot_page_write(CURRENT_ADDRESS
- 2);
656 boot_spm_busy_wait();
663 DBG1(0x35, (void *)¤tAddress
.l
, 4);
669 uchar
usbFunctionRead(uchar
*data
, uchar len
)
673 if(len
> bytesRemaining
)
674 len
= bytesRemaining
;
675 bytesRemaining
-= len
;
676 for(i
= 0; i
< len
; i
++){
677 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
678 *data
= eeprom_read_byte((void *)currentAddress
.w
[0]);
680 #if ((FLASHEND) > 65535)
681 *data
= pgm_read_byte_far(CURRENT_ADDRESS
);
683 *data
= pgm_read_byte(CURRENT_ADDRESS
);
692 /* ------------------------------------------------------------------------ */
694 static void initForUsbConnectivity(void)
696 #if HAVE_UNPRECISEWAIT
697 /* (0.25s*F_CPU)/(4 cycles per loop) ~ (65536*waitloopcnt)
698 * F_CPU/(16*65536) ~ waitloopcnt
699 * F_CPU / 1048576 ~ waitloopcnt
701 uint8_t waitloopcnt
= 1 + (F_CPU
/1048576);
704 /* enforce USB re-enumerate: */
705 usbDeviceDisconnect(); /* do this while interrupts are disabled */
706 #if HAVE_UNPRECISEWAIT
708 /*we really don't care what value Z has...
709 * ...if we loop 65536/F_CPU more or less...
710 * ...unimportant - just save some opcodes
712 "initForUsbConnectivity_sleeploop: \n\t"
715 "brne initForUsbConnectivity_sleeploop \n\t"
721 _delay_ms(260); /* fake USB disconnect for > 250 ms */
727 int __attribute__((__noreturn__
)) main(void)
729 #if ((BOOTLOADER_LOOPCYCLES_TIMEOUT) && (BOOTLOADER_CAN_EXIT))
730 uint16_t __loopscycles
;
731 timeout_remaining
= BOOTLOADER_LOOPCYCLES_TIMEOUT
;
737 #ifndef NO_FLASH_WRITE
738 GICR
= (1 << IVCE
); /* enable change of interrupt vectors */
739 GICR
= (1 << IVSEL
); /* move interrupts to boot flash section */
741 if(bootLoaderCondition()){
742 #if (BOOTLOADER_CAN_EXIT)
743 # if (USE_EXCESSIVE_ASSEMBLER)
745 "ldi %[sil], %[normval]\n\t"
746 # if ((defined(CONFIG_HAVE__BOOTLOADER_ABORTTIMEOUTONACT)) && (!(BOOTLOADER_IGNOREPROGBUTTON)) && (BOOTLOADER_LOOPCYCLES_TIMEOUT))
747 "sbis %[pin], %[bit]\n\t"
748 "subi %[sil], 0x02\n\t"
750 : [sil
] "=d" (stayinloader
)
751 : [normval
] "M" (stayinloader_initialValue
)
752 # if (!(BOOTLOADER_IGNOREPROGBUTTON))
754 [pin
] "I" (_SFR_IO_ADDR(PIN_PIN(JUMPER_PORT
))),
755 [bit
] "I" (PIN(JUMPER_PORT
, JUMPER_BIT
))
759 # if ((defined(CONFIG_HAVE__BOOTLOADER_ABORTTIMEOUTONACT)) && (!(BOOTLOADER_IGNOREPROGBUTTON)) && (BOOTLOADER_LOOPCYCLES_TIMEOUT))
760 if (bootLoaderConditionSimple()) {
761 stayinloader
= stayinloader_initialValue
- 0x02;
764 stayinloader
= stayinloader_initialValue
;
768 # if (defined(MCUSR) && defined(WDRF))
770 * Fix issue 6: (special thanks to coldtobi)
772 * The WDRF bit in the MCUSR needs to be cleared first,
773 * otherwise it is not possible to disable the watchdog
775 MCUSR
&= ~(_BV(WDRF
));
777 wdt_disable(); /* main app may have enabled watchdog */
779 MCUCSR
= 0; /* clear all reset flags for next time */
780 initForUsbConnectivity();
782 #if ((BOOTLOADER_LOOPCYCLES_TIMEOUT) && (BOOTLOADER_CAN_EXIT))
783 # ifdef CONFIG_HAVE__BOOTLOADER_ABORTTIMEOUTONACT
784 if (stayinloader
!= 0x0e) {
785 /* can be reached, since high-nibble is decreased every cycle... */
787 if (stayinloader
& 0x01) {
789 timeout_remaining
= BOOTLOADER_LOOPCYCLES_TIMEOUT
;
792 if (!(__loopscycles
)) {
793 if(timeout_remaining
) timeout_remaining
--;
794 else stayinloader
&=0xf1;
799 #if BOOTLOADER_CAN_EXIT
800 #if BOOTLOADER_IGNOREPROGBUTTON
802 * remove the high nibble as it would be subtracted due to:
803 * "(!bootLoaderConditionSimple())"
805 #if USE_EXCESSIVE_ASSEMBLER
807 "andi %[sil], 0x0f\n\t"
808 : [sil
] "+d" (stayinloader
)
812 stayinloader
&= 0x0f;
815 #if USE_EXCESSIVE_ASSEMBLER
817 "cpi %[sil], 0x10\n\t"
818 "brlo main_stayinloader_smaller\n\t"
819 "sbic %[pin], %[bit]\n\t"
820 "subi %[sil], 0x10\n\t"
821 "rjmp main_stayinloader_finished\n\t"
823 "main_stayinloader_smaller:\n\t"
824 "cpi %[sil], 0x2\n\t"
825 "brlo main_stayinloader_finished\n\t"
826 "sbis %[pin], %[bit]\n\t"
827 "subi %[sil], 0x2\n\t"
829 "main_stayinloader_finished:\n\t"
830 : [sil
] "+d" (stayinloader
)
831 : [pin
] "I" (_SFR_IO_ADDR(PIN_PIN(JUMPER_PORT
))),
832 [bit
] "I" (PIN(JUMPER_PORT
, JUMPER_BIT
))
835 if (stayinloader
>= 0x10) {
836 if (!bootLoaderConditionSimple()) {
840 if (bootLoaderConditionSimple()) {
841 if (stayinloader
> 1) stayinloader
-=2;
848 #if BOOTLOADER_CAN_EXIT
849 }while (stayinloader
); /* main event loop, if BOOTLOADER_CAN_EXIT*/
851 }while (1); /* main event loop */
857 /* ------------------------------------------------------------------------ */