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 ((HAVE_UNPRECISEWAIT))
117 /* here we have to assume we need to optimize for every byte */
118 #define __REGISTER_stayinloader_initialValue 0xfe
119 volatile register uint8_t stayinloader
__asm__("r17");
121 static volatile uint8_t stayinloader
= 0xfe;
125 static longConverter_t currentAddress
; /* in bytes */
126 static uchar bytesRemaining
;
127 static uchar isLastPage
;
128 #if HAVE_EEPROM_PAGED_ACCESS
129 static uchar currentRequest
;
131 static const uchar currentRequest
= 0;
134 static const uchar signatureBytes
[4] = {
135 #ifdef SIGNATURE_BYTES
137 #elif defined (__AVR_ATmega8535__)
139 #elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
141 #elif defined (__AVR_ATmega16__)
143 #elif defined (__AVR_ATmega32__)
145 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48A__) || defined (__AVR_ATmega48P__)
146 #error ATmega48 does not support bootloaders!
148 #elif defined (__AVR_ATmega48PA__)
149 #error ATmega48 does not support bootloaders!
151 #elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88P__)
153 #elif defined (__AVR_ATmega88PA__)
155 #elif defined (__AVR_ATmega164A__)
157 #elif defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__)
159 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168P__)
161 #elif defined (__AVR_ATmega168PA__)
163 #elif defined (__AVR_ATmega324A__)
165 #elif defined (__AVR_ATmega324P__)
167 #elif defined (__AVR_ATmega324PA__)
169 #elif defined (__AVR_ATmega328__)
171 #elif defined (__AVR_ATmega328P__)
173 #elif defined (__AVR_ATmega640__)
175 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__)
177 #elif defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
179 #elif defined (__AVR_ATmega128__)
181 #elif defined (__AVR_ATmega1280__)
183 #elif defined (__AVR_ATmega1281__)
185 #elif defined (__AVR_ATmega1284__)
187 #elif defined (__AVR_ATmega1284P__)
189 #elif defined (__AVR_ATmega2560__)
191 #elif defined (__AVR_ATmega2561__)
194 # if (defined(SIGNATURE_0) && defined(SIGNATURE_1) && defined(SIGNATURE_2))
195 # warning "Device signature is not known - using AVR Libc suggestion..."
196 SIGNATURE_0
, SIGNATURE_1
, SIGNATURE_2
, 0
198 # error "Device signature is not known, please edit main.c!"
203 /* ------------------------------------------------------------------------ */
205 #if (__REGISTER_stayinloader_initialValue)
206 /* need to put it after libc init - otherwise it fucks up the register */
207 void __attribute__ ((section(".init8"),naked
,used
,no_instrument_function
)) __REGISTER_stayinloader_initialValue_INITIALIZATION(void);
208 void __REGISTER_stayinloader_initialValue_INITIALIZATION(void) {
210 "ldi %[silreg] , %[silval]\n\t"
211 : [silreg
] "=a" (stayinloader
)
212 : [silval
] "M" (__REGISTER_stayinloader_initialValue
)
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 stayinloader
&= (0xfe);
488 /* ignore: others, but could be USBASP_FUNC_CONNECT */
489 #if BOOTLOADER_CAN_EXIT
490 stayinloader
|= (0x01);
496 #if (USE_EXCESSIVE_ASSEMBLER) && ((!HAVE_CHIP_ERASE) || (HAVE_ONDEMAND_PAGEERASE)) && (SPM_PAGESIZE <= 256) && (((BOOTLOADER_PAGEADDR>>0)&0xff) == 0)
497 uchar
usbFunctionWrite(uchar
*data
, uchar len
)
501 DBG1(0x31, (void *)¤tAddress
.l
, 4);
502 if(len
> bytesRemaining
)
503 len
= bytesRemaining
;
504 bytesRemaining
-= len
;
505 isLast
= bytesRemaining
== 0;
506 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
508 for(i
= 0; i
< len
; i
++){
509 eeprom_write_byte((void *)(currentAddress
.w
[0]++), *data
++);
515 "usbFunctionWrite_flashloop:\n\t"
517 "brlo usbFunctionWrite_finished\n\t"
519 #if HAVE_BLB11_SOFTW_LOCKBIT
520 "cpi r31, %[blsaddrhi]\n\t" /* accelerated BLB11_SOFTW_LOCKBIT check */
521 "brsh usbFunctionWrite_finished\n\t"
522 // "brlo usbFunctionWrite_addrunlock_ok\n\t"
523 // "brne usbFunctionWrite_finished\n\t"
524 // "cpi r30, %[blsaddrlo]\n\t"
525 // "brlo usbFunctionWrite_addrunlock_ok\n\t"
526 // "rjmp usbFunctionWrite_finished\n\t"
527 // "usbFunctionWrite_addrunlock_ok:\n\t"
529 "rcall usbFunctionWrite_waitA\n\t"
530 "cli\n\t" /* r0 or r1 may be __zero_reg__ and may become dangerous nonzero within interrupts */
534 "ldi r18, %[pagfillval]\n\t"
535 "rcall usbFunctionWrite_saveflash\n\t" /* page fill */
538 "subi r18, 0xfe\n\t" /* add with 2 */
539 "andi r18, %[pagemask]\n\t"
540 "breq usbFunctionWrite_pageisfull\n\t"
542 "breq usbFunctionWrite_skippageisfull\n\t"
543 "tst %[isLastPage]\n\t"
544 "breq usbFunctionWrite_skippageisfull\n\t"
546 "brne usbFunctionWrite_skippageisfull\n\t"
548 "usbFunctionWrite_pageisfull:\n\t" /* start writing the page */
549 "ldi r18, %[pageraseval]\n\t"
550 "rcall usbFunctionWrite_saveflash\n\t" /* page erase */
551 "rcall usbFunctionWrite_waitA\n\t"
553 "ldi r18, %[pagwriteval]\n\t"
554 "rcall usbFunctionWrite_saveflash\n\t" /* page write */
555 "rcall usbFunctionWrite_waitA\n\t"
557 "in __tmp_reg__, %[spmcr]\n\t"
558 "sbrs __tmp_reg__, %[rwwsbbit]\n\t"
559 "rjmp usbFunctionWrite_skippageisfull\n\t"
560 "ldi r18, %[rwwenrval]\n\t"
561 "rcall usbFunctionWrite_saveflash\n\t" /* reenable rww*/
562 // "rcall usbFunctionWrite_waitA\n\t"
565 "usbFunctionWrite_skippageisfull:\n\t"
567 "rjmp usbFunctionWrite_flashloop\n\t"
569 "usbFunctionWrite_saveflash:\n\t"
571 "out %[spmcr], r18\n\t"
573 "clr __zero_reg__\n\t" /* if r0 or r1 is __zero_reg__ it may have become inconsisten while page-fill */
577 "usbFunctionWrite_waitA:\n\t"
578 "in __tmp_reg__, %[spmcr]\n\t"
579 "sbrc __tmp_reg__, %[spmenbit]\n\t"
580 "rjmp usbFunctionWrite_waitA\n\t"
583 "usbFunctionWrite_finished:\n\t"
584 : [addr
] "+z" (currentAddress
.l
)
586 : [spmenbit
] "I" (SPMEN
),
587 [rwwsbbit
] "I" (RWWSB
),
588 [spmcr
] "I" (_SFR_IO_ADDR(__SPM_REG
)),
589 [pagfillval
] "M" ((1<<SPMEN
)),
590 [pageraseval
] "M" ((1<<PGERS
) | (1<<SPMEN
)),
591 [pagwriteval
] "M" ((1<<PGWRT
) | (1<<SPMEN
)),
592 [rwwenrval
] "M" ((1<<RWWSRE
) | (1<<SPMEN
)),
593 [pagemask
] "M" (SPM_PAGESIZE
-1),
594 #if HAVE_BLB11_SOFTW_LOCKBIT
595 [blsaddrhi
] "M" ((uint8_t)((BOOTLOADER_PAGEADDR
>>8)&0xff)),
596 // [blsaddrlo] "M" ((uint8_t)((BOOTLOADER_PAGEADDR>>0)&0xff)),
598 [islast
] "r" (isLast
),
599 [isLastPage
] "r" (isLastPage
),
609 uchar
usbFunctionWrite(uchar
*data
, uchar len
)
613 DBG1(0x31, (void *)¤tAddress
.l
, 4);
614 if(len
> bytesRemaining
)
615 len
= bytesRemaining
;
616 bytesRemaining
-= len
;
617 isLast
= bytesRemaining
== 0;
618 for(i
= 0; i
< len
;) {
619 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
620 eeprom_write_byte((void *)(currentAddress
.w
[0]++), *data
++);
623 #if HAVE_BLB11_SOFTW_LOCKBIT
624 if (CURRENT_ADDRESS
>= (addr_t
)(BOOTLOADER_PAGEADDR
)) {
631 boot_page_fill(CURRENT_ADDRESS
, *(short *)data
);
633 CURRENT_ADDRESS
+= 2;
635 /* write page when we cross page boundary or we have the last partial page */
636 if((currentAddress
.w
[0] & (SPM_PAGESIZE
- 1)) == 0 || (isLast
&& i
>= len
&& isLastPage
)){
637 #if (!HAVE_CHIP_ERASE) || (HAVE_ONDEMAND_PAGEERASE)
639 # ifndef NO_FLASH_WRITE
641 boot_page_erase(CURRENT_ADDRESS
- 2); /* erase page */
643 boot_spm_busy_wait(); /* wait until page is erased */
647 #ifndef NO_FLASH_WRITE
649 boot_page_write(CURRENT_ADDRESS
- 2);
651 boot_spm_busy_wait();
658 DBG1(0x35, (void *)¤tAddress
.l
, 4);
664 uchar
usbFunctionRead(uchar
*data
, uchar len
)
668 if(len
> bytesRemaining
)
669 len
= bytesRemaining
;
670 bytesRemaining
-= len
;
671 for(i
= 0; i
< len
; i
++){
672 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
673 *data
= eeprom_read_byte((void *)currentAddress
.w
[0]);
675 #if ((FLASHEND) > 65535)
676 *data
= pgm_read_byte_far(CURRENT_ADDRESS
);
678 *data
= pgm_read_byte(CURRENT_ADDRESS
);
687 /* ------------------------------------------------------------------------ */
689 static void initForUsbConnectivity(void)
691 #if HAVE_UNPRECISEWAIT
692 /* (0.25s*F_CPU)/(4 cycles per loop) ~ (65536*waitloopcnt)
693 * F_CPU/(16*65536) ~ waitloopcnt
694 * F_CPU / 1048576 ~ waitloopcnt
696 uint8_t waitloopcnt
= 1 + (F_CPU
/1048576);
699 /* enforce USB re-enumerate: */
700 usbDeviceDisconnect(); /* do this while interrupts are disabled */
701 #if HAVE_UNPRECISEWAIT
703 /*we really don't care what value Z has...
704 * ...if we loop 65536/F_CPU more or less...
705 * ...unimportant - just save some opcodes
707 "initForUsbConnectivity_sleeploop: \n\t"
710 "brne initForUsbConnectivity_sleeploop \n\t"
716 _delay_ms(260); /* fake USB disconnect for > 250 ms */
722 int __attribute__((__noreturn__
)) main(void)
728 #ifndef NO_FLASH_WRITE
729 GICR
= (1 << IVCE
); /* enable change of interrupt vectors */
730 GICR
= (1 << IVSEL
); /* move interrupts to boot flash section */
732 if(bootLoaderCondition()){
734 # if (defined(MCUSR) && defined(WDRF))
736 * Fix issue 6: (special thanks to coldtobi)
738 * The WDRF bit in the MCUSR needs to be cleared first,
739 * otherwise it is not possible to disable the watchdog
741 MCUSR
&= ~(_BV(WDRF
));
743 wdt_disable(); /* main app may have enabled watchdog */
745 MCUCSR
= 0; /* clear all reset flags for next time */
746 initForUsbConnectivity();
749 #if BOOTLOADER_CAN_EXIT
750 #if USE_EXCESSIVE_ASSEMBLER
752 "cpi %[sil], 0x10\n\t"
753 "brlo main_stayinloader_smaller\n\t"
754 "sbic %[pin], %[bit]\n\t"
755 "subi %[sil], 0x10\n\t"
756 "rjmp main_stayinloader_finished\n\t"
758 "main_stayinloader_smaller:\n\t"
759 "cpi %[sil], 0x2\n\t"
760 "brlo main_stayinloader_finished\n\t"
761 "sbis %[pin], %[bit]\n\t"
762 "subi %[sil], 0x2\n\t"
764 "main_stayinloader_finished:\n\t"
765 : [sil
] "+d" (stayinloader
)
766 : [pin
] "I" (_SFR_IO_ADDR(PIN_PIN(JUMPER_PORT
))),
767 [bit
] "I" (PIN(JUMPER_PORT
, JUMPER_BIT
))
770 if (stayinloader
>= 0x10) {
771 if (!bootLoaderConditionSimple()) {
775 if (bootLoaderConditionSimple()) {
776 if (stayinloader
> 1) stayinloader
-=2;
782 #if BOOTLOADER_CAN_EXIT
783 }while (stayinloader
); /* main event loop, if BOOTLOADER_CAN_EXIT*/
785 }while (1); /* main event loop */
791 /* ------------------------------------------------------------------------ */