2 * Project: USBaspLoader
3 * Author: Christian Starkjohann
4 * Author: Stephan Baerwolf
5 * Creation Date: 2007-12-08
6 * Modification Date: 2012-11-10
8 * Copyright: (c) 2007 by OBJECTIVE DEVELOPMENT Software GmbH
9 * License: GNU GPL v2 (see License.txt)
10 * This Revision: $Id: main.c 786 2010-05-30 20:41:40Z cs $
13 #include "spminterface.h" /* must be included as first! */
16 #include <avr/interrupt.h>
17 #include <avr/pgmspace.h>
20 #include <avr/eeprom.h>
21 #include <util/delay.h>
26 * 29.09.2012 / 30.09.2012
28 * Since cpufunc.h is not needed in this context and
29 * since it is not available in all toolchains, this include
30 * becomes deactivated by github issue-report.
31 * (In case of trouble it remains in sourcecode for reactivation.)
33 * The autor would like to thank Lena-M for reporting this
34 * issue (https://github.com/baerwolf/USBaspLoader/issues/1).
36 #include <avr/cpufunc.h>
45 #include "bootloaderconfig.h"
46 #include "usbdrv/usbdrv.c"
48 #ifndef BOOTLOADER_ADDRESS
49 #error need to know the bootloaders flash address!
51 #define BOOTLOADER_PAGEADDR (BOOTLOADER_ADDRESS - (BOOTLOADER_ADDRESS % SPM_PAGESIZE))
53 /* ------------------------------------------------------------------------ */
55 /* Request constants used by USBasp */
56 #define USBASP_FUNC_CONNECT 1
57 #define USBASP_FUNC_DISCONNECT 2
58 #define USBASP_FUNC_TRANSMIT 3
59 #define USBASP_FUNC_READFLASH 4
60 #define USBASP_FUNC_ENABLEPROG 5
61 #define USBASP_FUNC_WRITEFLASH 6
62 #define USBASP_FUNC_READEEPROM 7
63 #define USBASP_FUNC_WRITEEEPROM 8
64 #define USBASP_FUNC_SETLONGADDRESS 9
66 // additional USBasp Commands
67 #define USBASP_FUNC_SETISPSCK 10
68 #define USBASP_FUNC_TPI_CONNECT 11
69 #define USBASP_FUNC_TPI_DISCONNECT 12
70 #define USBASP_FUNC_TPI_RAWREAD 13
71 #define USBASP_FUNC_TPI_RAWWRITE 14
72 #define USBASP_FUNC_TPI_READBLOCK 15
73 #define USBASP_FUNC_TPI_WRITEBLOCK 16
74 #define USBASP_FUNC_GETCAPABILITIES 127
75 /* ------------------------------------------------------------------------ */
78 # define ulong unsigned long
81 # define uint unsigned int
85 /* allow compatibility with avrusbboot's bootloaderconfig.h: */
86 #ifdef BOOTLOADER_INIT
87 # define bootLoaderInit() BOOTLOADER_INIT
88 # define bootLoaderExit()
90 #ifdef BOOTLOADER_CONDITION
91 # define bootLoaderCondition() BOOTLOADER_CONDITION
94 /* device compatibility: */
95 #ifndef GICR /* ATMega*8 don't have GICR, use MCUCR instead */
99 /* ------------------------------------------------------------------------ */
101 #if (FLASHEND) > 0xffff /* we need long addressing */
102 # define CURRENT_ADDRESS currentAddress.l
103 # define addr_t ulong
105 # define CURRENT_ADDRESS currentAddress.w[0]
109 typedef union longConverter
{
111 uint w
[sizeof(addr_t
)/2];
112 uchar b
[sizeof(addr_t
)];
116 #if BOOTLOADER_CAN_EXIT
117 static volatile unsigned char stayinloader
= 0xfe;
120 static longConverter_t currentAddress
; /* in bytes */
121 static uchar bytesRemaining
;
122 static uchar isLastPage
;
123 #if HAVE_EEPROM_PAGED_ACCESS
124 static uchar currentRequest
;
126 static const uchar currentRequest
= 0;
129 static const uchar signatureBytes
[4] = {
130 #ifdef SIGNATURE_BYTES
132 #elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
134 #elif defined (__AVR_ATmega16__)
136 #elif defined (__AVR_ATmega32__)
138 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48A__) || defined (__AVR_ATmega48P__)
139 #error ATmega48 does not support bootloaders!
141 #elif defined (__AVR_ATmega48PA__)
142 #error ATmega48 does not support bootloaders!
144 #elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88P__)
146 #elif defined (__AVR_ATmega88PA__)
148 #elif defined (__AVR_ATmega164A__)
150 #elif defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__)
152 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168P__)
154 #elif defined (__AVR_ATmega168PA__)
156 #elif defined (__AVR_ATmega324A__)
158 #elif defined (__AVR_ATmega324P__)
160 #elif defined (__AVR_ATmega324PA__)
162 #elif defined (__AVR_ATmega328__)
164 #elif defined (__AVR_ATmega328P__)
166 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__)
168 #elif defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
170 #elif defined (__AVR_ATmega128__)
172 #elif defined (__AVR_ATmega1284__)
174 #elif defined (__AVR_ATmega1284P__)
177 # error "Device signature is not known, please edit main.c!"
181 /* ------------------------------------------------------------------------ */
183 #if (USE_BOOTUP_CLEARRAM)
185 * Under normal circumstances, RESET will not clear contents of RAM.
186 * As always, if you want it done - do it yourself...
188 void __attribute__ ((naked
)) __attribute__ ((section (".init3"))) __clearram(void);
189 void __clearram(void) {
190 extern size_t __bss_end
;
193 "ldi r29, %[ramendhi]\n\t"
194 "ldi r28, %[ramendlo]\n\t"
195 "__clearramloop%=:\n\t"
196 "st -Y , __zero_reg__\n\t"
197 "cp r28, %A[bssend]\n\t"
198 "cpc r29, %B[bssend]\n\t"
199 "brne __clearramloop%=\n\t"
201 : [ramendhi
] "M" (((RAMEND
+1)>>8) & 0xff),
202 [ramendlo
] "M" (((RAMEND
+1)>>0) & 0xff),
203 [bssend
] "r" (&__bss_end
)
209 #if (!USE_EXCESSIVE_ASSEMBLER) || (!(defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)))
210 static void (*nullVector
)(void) __attribute__((__noreturn__
));
213 static void __attribute__((__noreturn__
)) leaveBootloader()
215 #if (USE_EXCESSIVE_ASSEMBLER) && (defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__))
219 "sbi %[usbddr], %[usbminus]\n\t"
220 "cbi %[port], %[bit]\n\t"
221 "out %[usbintrenab], r30\n\t"
222 "out %[usbintrcfg], r30\n\t"
223 "ldi r31, %[ivce]\n\t"
224 "out %[mygicr], r31\n\t"
225 "out %[mygicr], r30\n\t"
229 : [port
] "I" (_SFR_IO_ADDR(PIN_PORT(JUMPER_PORT
))),
230 [bit
] "I" (PIN(JUMPER_PORT
, JUMPER_BIT
)),
231 [usbintrenab
] "I" (_SFR_IO_ADDR(USB_INTR_ENABLE
)),
232 [usbintrcfg
] "I" (_SFR_IO_ADDR(USB_INTR_CFG
)),
233 [usbddr
] "I" (_SFR_IO_ADDR(USBDDR
)),
234 [usbminus
] "I" (USBMINUS
),
235 [mygicr
] "I" (_SFR_IO_ADDR(GICR
)),
238 /* TODO: compiler will put an unnecessary "ret" at this end ! */
242 usbDeviceDisconnect();
245 USB_INTR_CFG
= 0; /* also reset config bits */
246 GICR
= (1 << IVCE
); /* enable change of interrupt vectors */
247 GICR
= (0 << IVSEL
); /* move interrupts to application flash section */
249 /* We must go through a global function pointer variable instead of writing
250 * ((void (*)(void))0)();
251 * because the compiler optimizes a constant 0 to "rcall 0" which is not
252 * handled correctly by the assembler.
258 /* ------------------------------------------------------------------------ */
261 uchar
usbFunctionSetup_USBASP_FUNC_TRANSMIT(usbRequest_t
*rq
) {
264 address
.bytes
[1] = rq
->wValue
.bytes
[1];
265 address
.bytes
[0] = rq
->wIndex
.bytes
[0];
267 if(rq
->wValue
.bytes
[0] == 0x30){ /* read signature */
268 rval
= rq
->wIndex
.bytes
[0] & 3;
269 rval
= signatureBytes
[rval
];
270 #if HAVE_READ_LOCK_FUSE
271 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__)
272 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x00){ /* read lock bits */
273 rval
= boot_lock_fuse_bits_get(GET_LOCK_BITS
);
274 }else if(rq
->wValue
.bytes
[0] == 0x50 && rq
->wValue
.bytes
[1] == 0x00){ /* read lfuse bits */
275 rval
= boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS
);
276 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x08){ /* read hfuse bits */
277 rval
= boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS
);
279 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48A__) || defined (__AVR_ATmega48P__) || defined (__AVR_ATmega48PA__) || \
280 defined (__AVR_ATmega88__) || defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88P__) || defined (__AVR_ATmega88PA__) || \
281 defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__) || \
282 defined (__AVR_ATmega168__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168P__) || defined (__AVR_ATmega168PA__) || \
283 defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__) || \
284 defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__) || \
285 defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__) || \
286 defined (__AVR_ATmega128__) || \
287 defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
288 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x00){ /* read lock bits */
289 rval
= boot_lock_fuse_bits_get(GET_LOCK_BITS
);
290 }else if(rq
->wValue
.bytes
[0] == 0x50 && rq
->wValue
.bytes
[1] == 0x00){ /* read lfuse bits */
291 rval
= boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS
);
292 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x08){ /* read hfuse bits */
293 rval
= boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS
);
294 }else if(rq
->wValue
.bytes
[0] == 0x50 && rq
->wValue
.bytes
[1] == 0x08){ /* read efuse bits */
295 rval
= boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS
);
297 #warning "HAVE_READ_LOCK_FUSE is activated but MCU unknown -> will not support this feature"
300 #if HAVE_FLASH_BYTE_READACCESS
301 }else if(rq
->wValue
.bytes
[0] == 0x20){ /* read FLASH low byte */
302 #if ((FLASHEND) > 65535)
303 rval
= pgm_read_byte_far((((addr_t
)address
.word
)<<1)+0);
305 rval
= pgm_read_byte((((addr_t
)address
.word
)<<1)+0);
307 }else if(rq
->wValue
.bytes
[0] == 0x28){ /* read FLASH high byte */
308 #if ((FLASHEND) > 65535)
309 rval
= pgm_read_byte_far((((addr_t
)address
.word
)<<1)+1);
311 rval
= pgm_read_byte((((addr_t
)address
.word
)<<1)+1);
314 #if HAVE_EEPROM_BYTE_ACCESS
315 }else if(rq
->wValue
.bytes
[0] == 0xa0){ /* read EEPROM byte */
316 rval
= eeprom_read_byte((void *)address
.word
);
317 }else if(rq
->wValue
.bytes
[0] == 0xc0){ /* write EEPROM byte */
318 eeprom_write_byte((void *)address
.word
, rq
->wIndex
.bytes
[1]);
321 }else if(rq
->wValue
.bytes
[0] == 0xac && rq
->wValue
.bytes
[1] == 0x80){ /* chip erase */
323 #if HAVE_BLB11_SOFTW_LOCKBIT
324 for(addr
= 0; addr
< (addr_t
)(BOOTLOADER_PAGEADDR
) ; addr
+= SPM_PAGESIZE
) {
326 for(addr
= 0; addr
<= (addr_t
)(FLASHEND
) ; addr
+= SPM_PAGESIZE
) {
328 /* wait and erase page */
330 # ifndef NO_FLASH_WRITE
331 boot_spm_busy_wait();
333 boot_page_erase(addr
);
339 /* ignore all others, return default value == 0 */
346 uchar
usbFunctionSetup(uchar data
[8])
348 usbRequest_t
*rq
= (void *)data
;
350 static uchar replyBuffer
[4];
352 usbMsgPtr
= replyBuffer
;
353 if(rq
->bRequest
== USBASP_FUNC_TRANSMIT
){ /* emulate parts of ISP protocol */
354 replyBuffer
[3] = usbFunctionSetup_USBASP_FUNC_TRANSMIT(rq
);
356 }else if((rq
->bRequest
== USBASP_FUNC_ENABLEPROG
) || (rq
->bRequest
== USBASP_FUNC_SETISPSCK
)){
357 /* replyBuffer[0] = 0; is never touched and thus always 0 which means success */
359 }else if(rq
->bRequest
>= USBASP_FUNC_READFLASH
&& rq
->bRequest
<= USBASP_FUNC_SETLONGADDRESS
){
360 currentAddress
.w
[0] = rq
->wValue
.word
;
361 if(rq
->bRequest
== USBASP_FUNC_SETLONGADDRESS
){
362 #if (FLASHEND) > 0xffff
363 currentAddress
.w
[1] = rq
->wIndex
.word
;
366 bytesRemaining
= rq
->wLength
.bytes
[0];
367 /* if(rq->bRequest == USBASP_FUNC_WRITEFLASH) only evaluated during writeFlash anyway */
368 isLastPage
= rq
->wIndex
.bytes
[1] & 0x02;
369 #if HAVE_EEPROM_PAGED_ACCESS
370 currentRequest
= rq
->bRequest
;
372 len
= 0xff; /* hand over to usbFunctionRead() / usbFunctionWrite() */
375 }else if(rq
->bRequest
== USBASP_FUNC_DISCONNECT
){
377 #if BOOTLOADER_CAN_EXIT
378 stayinloader
&= (0xfe);
381 /* ignore: others, but could be USBASP_FUNC_CONNECT */
382 #if BOOTLOADER_CAN_EXIT
383 stayinloader
|= (0x01);
389 #if (USE_EXCESSIVE_ASSEMBLER) && ((!HAVE_CHIP_ERASE) || (HAVE_ONDEMAND_PAGEERASE)) && (SPM_PAGESIZE <= 256) && (((BOOTLOADER_PAGEADDR>>0)&0xff) == 0)
390 uchar
usbFunctionWrite(uchar
*data
, uchar len
)
394 DBG1(0x31, (void *)¤tAddress
.l
, 4);
395 if(len
> bytesRemaining
)
396 len
= bytesRemaining
;
397 bytesRemaining
-= len
;
398 isLast
= bytesRemaining
== 0;
399 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
401 for(i
= 0; i
< len
; i
++){
402 eeprom_write_byte((void *)(currentAddress
.w
[0]++), *data
++);
408 "usbFunctionWrite_flashloop:\n\t"
410 "brlo usbFunctionWrite_finished\n\t"
412 #if HAVE_BLB11_SOFTW_LOCKBIT
413 "cpi r31, %[blsaddrhi]\n\t" /* accelerated BLB11_SOFTW_LOCKBIT check */
414 "brsh usbFunctionWrite_finished\n\t"
415 // "brlo usbFunctionWrite_addrunlock_ok\n\t"
416 // "brne usbFunctionWrite_finished\n\t"
417 // "cpi r30, %[blsaddrlo]\n\t"
418 // "brlo usbFunctionWrite_addrunlock_ok\n\t"
419 // "rjmp usbFunctionWrite_finished\n\t"
420 // "usbFunctionWrite_addrunlock_ok:\n\t"
422 "rcall usbFunctionWrite_waitA\n\t"
423 "cli\n\t" /* r0 or r1 may be __zero_reg__ and may become dangerous nonzero within interrupts */
427 "ldi r18, %[pagfillval]\n\t"
428 "rcall usbFunctionWrite_saveflash\n\t" /* page fill */
431 "subi r18, 0xfe\n\t" /* add with 2 */
432 "andi r18, %[pagemask]\n\t"
433 "breq usbFunctionWrite_pageisfull\n\t"
435 "breq usbFunctionWrite_skippageisfull\n\t"
436 "tst %[isLastPage]\n\t"
437 "breq usbFunctionWrite_skippageisfull\n\t"
439 "brne usbFunctionWrite_skippageisfull\n\t"
441 "usbFunctionWrite_pageisfull:\n\t" /* start writing the page */
442 "ldi r18, %[pageraseval]\n\t"
443 "rcall usbFunctionWrite_saveflash\n\t" /* page erase */
444 "rcall usbFunctionWrite_waitA\n\t"
446 "ldi r18, %[pagwriteval]\n\t"
447 "rcall usbFunctionWrite_saveflash\n\t" /* page write */
448 "rcall usbFunctionWrite_waitA\n\t"
450 "in __tmp_reg__, %[spmcr]\n\t"
451 "sbrs __tmp_reg__, %[rwwsbbit]\n\t"
452 "rjmp usbFunctionWrite_skippageisfull\n\t"
453 "ldi r18, %[rwwenrval]\n\t"
454 "rcall usbFunctionWrite_saveflash\n\t" /* reenable rww*/
455 // "rcall usbFunctionWrite_waitA\n\t"
458 "usbFunctionWrite_skippageisfull:\n\t"
460 "rjmp usbFunctionWrite_flashloop\n\t"
462 "usbFunctionWrite_saveflash:\n\t"
464 "out %[spmcr], r18\n\t"
466 "clr __zero_reg__\n\t" /* if r0 or r1 is __zero_reg__ it may have become inconsisten while page-fill */
470 "usbFunctionWrite_waitA:\n\t"
471 "in __tmp_reg__, %[spmcr]\n\t"
472 "sbrc __tmp_reg__, %[spmenbit]\n\t"
473 "rjmp usbFunctionWrite_waitA\n\t"
476 "usbFunctionWrite_finished:\n\t"
477 : [addr
] "+z" (currentAddress
.l
)
479 : [spmenbit
] "I" (SPMEN
),
480 [rwwsbbit
] "I" (RWWSB
),
481 [spmcr
] "I" (_SFR_IO_ADDR(__SPM_REG
)),
482 [pagfillval
] "M" ((1<<SPMEN
)),
483 [pageraseval
] "M" ((1<<PGERS
) | (1<<SPMEN
)),
484 [pagwriteval
] "M" ((1<<PGWRT
) | (1<<SPMEN
)),
485 [rwwenrval
] "M" ((1<<RWWSRE
) | (1<<SPMEN
)),
486 [pagemask
] "M" (SPM_PAGESIZE
-1),
487 #if HAVE_BLB11_SOFTW_LOCKBIT
488 [blsaddrhi
] "M" ((uint8_t)((BOOTLOADER_PAGEADDR
>>8)&0xff)),
489 // [blsaddrlo] "M" ((uint8_t)((BOOTLOADER_PAGEADDR>>0)&0xff)),
491 [islast
] "r" (isLast
),
492 [isLastPage
] "r" (isLastPage
),
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 for(i
= 0; i
< len
;) {
512 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
513 eeprom_write_byte((void *)(currentAddress
.w
[0]++), *data
++);
516 #if HAVE_BLB11_SOFTW_LOCKBIT
517 if (CURRENT_ADDRESS
>= (addr_t
)(BOOTLOADER_PAGEADDR
)) {
524 boot_page_fill(CURRENT_ADDRESS
, *(short *)data
);
526 CURRENT_ADDRESS
+= 2;
528 /* write page when we cross page boundary or we have the last partial page */
529 if((currentAddress
.w
[0] & (SPM_PAGESIZE
- 1)) == 0 || (isLast
&& i
>= len
&& isLastPage
)){
530 #if (!HAVE_CHIP_ERASE) || (HAVE_ONDEMAND_PAGEERASE)
532 # ifndef NO_FLASH_WRITE
534 boot_page_erase(CURRENT_ADDRESS
- 2); /* erase page */
536 boot_spm_busy_wait(); /* wait until page is erased */
540 #ifndef NO_FLASH_WRITE
542 boot_page_write(CURRENT_ADDRESS
- 2);
544 boot_spm_busy_wait();
551 DBG1(0x35, (void *)¤tAddress
.l
, 4);
557 uchar
usbFunctionRead(uchar
*data
, uchar len
)
561 if(len
> bytesRemaining
)
562 len
= bytesRemaining
;
563 bytesRemaining
-= len
;
564 for(i
= 0; i
< len
; i
++){
565 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
566 *data
= eeprom_read_byte((void *)currentAddress
.w
[0]);
568 #if ((FLASHEND) > 65535)
569 *data
= pgm_read_byte_far(CURRENT_ADDRESS
);
571 *data
= pgm_read_byte(CURRENT_ADDRESS
);
580 /* ------------------------------------------------------------------------ */
582 static void initForUsbConnectivity(void)
584 #if HAVE_UNPRECISEWAIT
585 /* (0.25s*F_CPU)/(4 cycles per loop) ~ (65536*waitloopcnt)
586 * F_CPU/(16*65536) ~ waitloopcnt
587 * F_CPU / 1048576 ~ waitloopcnt
589 uint8_t waitloopcnt
= 1 + (F_CPU
/1048576);
592 /* enforce USB re-enumerate: */
593 usbDeviceDisconnect(); /* do this while interrupts are disabled */
594 #if HAVE_UNPRECISEWAIT
596 /*we really don't care what value Z has...
597 * ...if we loop 65536/F_CPU more or less...
598 * ...unimportant - just save some opcodes
600 "initForUsbConnectivity_sleeploop: \n\t"
603 "brne initForUsbConnectivity_sleeploop \n\t"
609 _delay_ms(260); /* fake USB disconnect for > 250 ms */
615 int __attribute__((__noreturn__
)) main(void)
621 #ifndef NO_FLASH_WRITE
622 GICR
= (1 << IVCE
); /* enable change of interrupt vectors */
623 GICR
= (1 << IVSEL
); /* move interrupts to boot flash section */
625 if(bootLoaderCondition()){
627 wdt_disable(); /* main app may have enabled watchdog */
629 initForUsbConnectivity();
632 #if BOOTLOADER_CAN_EXIT
633 #if USE_EXCESSIVE_ASSEMBLER
635 "cpi %[sil], 0x10\n\t"
636 "brlo main_stayinloader_smaller\n\t"
637 "sbic %[pin], %[bit]\n\t"
638 "subi %[sil], 0x10\n\t"
639 "rjmp main_stayinloader_finished\n\t"
641 "main_stayinloader_smaller:\n\t"
642 "cpi %[sil], 0x2\n\t"
643 "brlo main_stayinloader_finished\n\t"
644 "sbis %[pin], %[bit]\n\t"
645 "subi %[sil], 0x2\n\t"
647 "main_stayinloader_finished:\n\t"
648 : [sil
] "+d" (stayinloader
)
649 : [pin
] "I" (_SFR_IO_ADDR(PIN_PIN(JUMPER_PORT
))),
650 [bit
] "I" (PIN(JUMPER_PORT
, JUMPER_BIT
))
653 if (stayinloader
>= 0x10) {
654 if (!bootLoaderCondition()) {
658 if (bootLoaderCondition()) {
659 if (stayinloader
> 1) stayinloader
-=2;
665 #if BOOTLOADER_CAN_EXIT
666 }while (stayinloader
); /* main event loop, if BOOTLOADER_CAN_EXIT*/
668 }while (1); /* main event loop */
674 /* ------------------------------------------------------------------------ */