4c612d6baec68b7dfe8ef818440b4007c567bccb
[pub/USBaspLoader.git] / firmware / main.c
1 /* Name: main.c
2 * Project: USBaspLoader
3 * Author: Christian Starkjohann
4 * Author: Stephan Baerwolf
5 * Creation Date: 2007-12-08
6 * Modification Date: 2012-11-10
7 * Tabsize: 4
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 $
11 */
12
13 #include "spminterface.h" /* must be included as first! */
14
15 #include <avr/io.h>
16 #include <avr/interrupt.h>
17 #include <avr/pgmspace.h>
18 #include <avr/wdt.h>
19 #include <avr/boot.h>
20 #include <avr/eeprom.h>
21 #include <util/delay.h>
22
23
24 #if 0
25 /*
26 * 29.09.2012 / 30.09.2012
27 *
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.)
32 *
33 * The autor would like to thank Lena-M for reporting this
34 * issue (https://github.com/baerwolf/USBaspLoader/issues/1).
35 */
36 #include <avr/cpufunc.h>
37 #endif
38
39 #include <avr/boot.h>
40
41 #include <string.h>
42
43
44
45 #include "bootloaderconfig.h"
46 #include "usbdrv/usbdrv.c"
47
48 #ifndef BOOTLOADER_ADDRESS
49 #error need to know the bootloaders flash address!
50 #endif
51 #define BOOTLOADER_PAGEADDR (BOOTLOADER_ADDRESS - (BOOTLOADER_ADDRESS % SPM_PAGESIZE))
52
53 /* ------------------------------------------------------------------------ */
54
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
65
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 /* ------------------------------------------------------------------------ */
76
77 #ifndef ulong
78 # define ulong unsigned long
79 #endif
80 #ifndef uint
81 # define uint unsigned int
82 #endif
83
84
85 /* allow compatibility with avrusbboot's bootloaderconfig.h: */
86 #ifdef BOOTLOADER_INIT
87 # define bootLoaderInit() BOOTLOADER_INIT
88 # define bootLoaderExit()
89 #endif
90 #ifdef BOOTLOADER_CONDITION
91 # define bootLoaderCondition() BOOTLOADER_CONDITION
92 #endif
93
94 /* device compatibility: */
95 #ifndef GICR /* ATMega*8 don't have GICR, use MCUCR instead */
96 # define GICR MCUCR
97 #endif
98
99 /* ------------------------------------------------------------------------ */
100
101 #if (FLASHEND) > 0xffff /* we need long addressing */
102 # define CURRENT_ADDRESS currentAddress.l
103 # define addr_t ulong
104 #else
105 # define CURRENT_ADDRESS currentAddress.w[0]
106 # define addr_t uint
107 #endif
108
109 typedef union longConverter{
110 addr_t l;
111 uint w[sizeof(addr_t)/2];
112 uchar b[sizeof(addr_t)];
113 }longConverter_t;
114
115
116 #if BOOTLOADER_CAN_EXIT
117 static volatile unsigned char stayinloader = 0xfe;
118 #endif
119
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;
125 #else
126 static const uchar currentRequest = 0;
127 #endif
128
129 static const uchar signatureBytes[4] = {
130 #ifdef SIGNATURE_BYTES
131 SIGNATURE_BYTES
132 #elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
133 0x1e, 0x93, 0x07, 0
134 #elif defined (__AVR_ATmega16__)
135 0x1e, 0x94, 0x03, 0
136 #elif defined (__AVR_ATmega32__)
137 0x1e, 0x95, 0x02, 0
138 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48A__) || defined (__AVR_ATmega48P__)
139 #error ATmega48 does not support bootloaders!
140 0x1e, 0x92, 0x05, 0
141 #elif defined (__AVR_ATmega48PA__)
142 #error ATmega48 does not support bootloaders!
143 0x1e, 0x92, 0x0A, 0
144 #elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88P__)
145 0x1e, 0x93, 0x0a, 0
146 #elif defined (__AVR_ATmega88PA__)
147 0x1e, 0x93, 0x0F, 0
148 #elif defined (__AVR_ATmega164A__)
149 0x1e, 0x94, 0x0f, 0
150 #elif defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__)
151 0x1e, 0x94, 0x0a, 0
152 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168P__)
153 0x1e, 0x94, 0x06, 0
154 #elif defined (__AVR_ATmega168PA__)
155 0x1e, 0x94, 0x0B, 0
156 #elif defined (__AVR_ATmega324A__)
157 0x1e, 0x95, 0x15, 0
158 #elif defined (__AVR_ATmega324P__)
159 0x1e, 0x95, 0x08, 0
160 #elif defined (__AVR_ATmega324PA__)
161 0x1e, 0x95, 0x11, 0
162 #elif defined (__AVR_ATmega328__)
163 0x1e, 0x95, 0x14, 0
164 #elif defined (__AVR_ATmega328P__)
165 0x1e, 0x95, 0x0f, 0
166 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__)
167 0x1e, 0x96, 0x09, 0
168 #elif defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
169 0x1e, 0x96, 0x0a, 0
170 #elif defined (__AVR_ATmega128__)
171 0x1e, 0x97, 0x02, 0
172 #elif defined (__AVR_ATmega1284__)
173 0x1e, 0x97, 0x06, 0
174 #elif defined (__AVR_ATmega1284P__)
175 0x1e, 0x97, 0x05, 0
176 #else
177 # error "Device signature is not known, please edit main.c!"
178 #endif
179 };
180
181 /* ------------------------------------------------------------------------ */
182
183 #if (USE_BOOTUP_CLEARRAM)
184 /*
185 * Under normal circumstances, RESET will not clear contents of RAM.
186 * As always, if you want it done - do it yourself...
187 */
188 void __attribute__ ((naked)) __attribute__ ((section (".init3"))) __clearram(void);
189 void __clearram(void) {
190 extern size_t __bss_end;
191 asm volatile (
192 "__clearram:\n\t"
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"
200 :
201 : [ramendhi] "M" (((RAMEND+1)>>8) & 0xff),
202 [ramendlo] "M" (((RAMEND+1)>>0) & 0xff),
203 [bssend] "r" (&__bss_end)
204 : "memory"
205 );
206 }
207 #endif
208
209 #if (!USE_EXCESSIVE_ASSEMBLER) || (!(defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)))
210 static void (*nullVector)(void) __attribute__((__noreturn__));
211 #endif
212
213 static void __attribute__((__noreturn__)) leaveBootloader()
214 {
215 #if (USE_EXCESSIVE_ASSEMBLER) && (defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__))
216 asm volatile (
217 "cli\n\t"
218 "clr r30\n\t"
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"
226 "clr r31\n\t"
227 "icall\n\t"
228 :
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)),
236 [ivce] "I" (1<<IVCE)
237 );
238 /* TODO: compiler will put an unnecessary "ret" at this end ! */
239 #else
240 DBG1(0x01, 0, 0);
241 cli();
242 usbDeviceDisconnect();
243 bootLoaderExit();
244 USB_INTR_ENABLE = 0;
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 */
248
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.
253 */
254 nullVector();
255 #endif
256 }
257
258 /* ------------------------------------------------------------------------ */
259
260
261 uchar usbFunctionSetup_USBASP_FUNC_TRANSMIT(usbRequest_t *rq) {
262 uchar rval = 0;
263 usbWord_t address;
264 address.bytes[1] = rq->wValue.bytes[1];
265 address.bytes[0] = rq->wIndex.bytes[0];
266
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);
278
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 );
296 #else
297 #warning "HAVE_READ_LOCK_FUSE is activated but MCU unknown -> will not support this feature"
298 #endif
299 #endif
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);
304 #else
305 rval = pgm_read_byte((((addr_t)address.word)<<1)+0);
306 #endif
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);
310 #else
311 rval = pgm_read_byte((((addr_t)address.word)<<1)+1);
312 #endif
313 #endif
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]);
319 #endif
320 #if HAVE_CHIP_ERASE
321 }else if(rq->wValue.bytes[0] == 0xac && rq->wValue.bytes[1] == 0x80){ /* chip erase */
322 addr_t addr;
323 #if HAVE_BLB11_SOFTW_LOCKBIT
324 for(addr = 0; addr < (addr_t)(BOOTLOADER_PAGEADDR) ; addr += SPM_PAGESIZE) {
325 #else
326 for(addr = 0; addr <= (addr_t)(FLASHEND) ; addr += SPM_PAGESIZE) {
327 #endif
328 /* wait and erase page */
329 DBG1(0x33, 0, 0);
330 # ifndef NO_FLASH_WRITE
331 boot_spm_busy_wait();
332 cli();
333 boot_page_erase(addr);
334 sei();
335 # endif
336 }
337 #endif
338 }else{
339 /* ignore all others, return default value == 0 */
340 }
341
342 return rval;
343 }
344
345
346 uchar usbFunctionSetup(uchar data[8])
347 {
348 usbRequest_t *rq = (void *)data;
349 uchar len = 0;
350 static uchar replyBuffer[4];
351
352 usbMsgPtr = replyBuffer;
353 if(rq->bRequest == USBASP_FUNC_TRANSMIT){ /* emulate parts of ISP protocol */
354 replyBuffer[3] = usbFunctionSetup_USBASP_FUNC_TRANSMIT(rq);
355 len = 4;
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 */
358 len = 1;
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;
364 #endif
365 }else{
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;
371 #endif
372 len = 0xff; /* hand over to usbFunctionRead() / usbFunctionWrite() */
373 }
374
375 }else if(rq->bRequest == USBASP_FUNC_DISCONNECT){
376
377 #if BOOTLOADER_CAN_EXIT
378 stayinloader &= (0xfe);
379 #endif
380 }else{
381 /* ignore: others, but could be USBASP_FUNC_CONNECT */
382 #if BOOTLOADER_CAN_EXIT
383 stayinloader |= (0x01);
384 #endif
385 }
386 return len;
387 }
388
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)
391 {
392 uchar isLast;
393
394 DBG1(0x31, (void *)&currentAddress.l, 4);
395 if(len > bytesRemaining)
396 len = bytesRemaining;
397 bytesRemaining -= len;
398 isLast = bytesRemaining == 0;
399 if(currentRequest >= USBASP_FUNC_READEEPROM){
400 uchar i;
401 for(i = 0; i < len; i++){
402 eeprom_write_byte((void *)(currentAddress.w[0]++), *data++);
403 }
404 }else{
405 asm volatile (
406 "sbrc %[len], 0\n\t"
407 "inc %[len]\n\t"
408 "usbFunctionWrite_flashloop:\n\t"
409 "subi %[len], 2\n\t"
410 "brlo usbFunctionWrite_finished\n\t"
411
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"
421 #endif
422 "rcall usbFunctionWrite_waitA\n\t"
423 "cli\n\t" /* r0 or r1 may be __zero_reg__ and may become dangerous nonzero within interrupts */
424 "ld r0, X+\n\t"
425 "ld r1, X+\n\t"
426
427 "ldi r18, %[pagfillval]\n\t"
428 "rcall usbFunctionWrite_saveflash\n\t" /* page fill */
429
430 "mov r18, r30\n\t"
431 "subi r18, 0xfe\n\t" /* add with 2 */
432 "andi r18, %[pagemask]\n\t"
433 "breq usbFunctionWrite_pageisfull\n\t"
434 "tst %[islast]\n\t"
435 "breq usbFunctionWrite_skippageisfull\n\t"
436 "tst %[isLastPage]\n\t"
437 "breq usbFunctionWrite_skippageisfull\n\t"
438 "cpi %[len], 0\n\t"
439 "brne usbFunctionWrite_skippageisfull\n\t"
440
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"
445
446 "ldi r18, %[pagwriteval]\n\t"
447 "rcall usbFunctionWrite_saveflash\n\t" /* page write */
448 "rcall usbFunctionWrite_waitA\n\t"
449
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"
456
457
458 "usbFunctionWrite_skippageisfull:\n\t"
459 "adiw r30, 0x2\n\t"
460 "rjmp usbFunctionWrite_flashloop\n\t"
461
462 "usbFunctionWrite_saveflash:\n\t"
463 "cli\n\t"
464 "out %[spmcr], r18\n\t"
465 "spm\n\t"
466 "clr __zero_reg__\n\t" /* if r0 or r1 is __zero_reg__ it may have become inconsisten while page-fill */
467 "sei\n\t"
468 "ret\n\t"
469
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"
474 "ret\n\t"
475
476 "usbFunctionWrite_finished:\n\t"
477 : [addr] "+z" (currentAddress.l)
478
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)),
490 #endif
491 [islast] "r" (isLast),
492 [isLastPage] "r" (isLastPage),
493 [len] "d" (len),
494 [dataptr] "x" (data)
495
496 : "r0", "r1", "r18"
497 );
498 }
499 return isLast;
500 }
501 #else
502 uchar usbFunctionWrite(uchar *data, uchar len)
503 {
504 uchar i,isLast;
505
506 DBG1(0x31, (void *)&currentAddress.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++);
514 i++;
515 } else {
516 #if HAVE_BLB11_SOFTW_LOCKBIT
517 if (CURRENT_ADDRESS >= (addr_t)(BOOTLOADER_PAGEADDR)) {
518 return 1;
519 }
520 #endif
521 i += 2;
522 DBG1(0x32, 0, 0);
523 cli();
524 boot_page_fill(CURRENT_ADDRESS, *(short *)data);
525 sei();
526 CURRENT_ADDRESS += 2;
527 data += 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)
531 DBG1(0x33, 0, 0);
532 # ifndef NO_FLASH_WRITE
533 cli();
534 boot_page_erase(CURRENT_ADDRESS - 2); /* erase page */
535 sei();
536 boot_spm_busy_wait(); /* wait until page is erased */
537 # endif
538 #endif
539 DBG1(0x34, 0, 0);
540 #ifndef NO_FLASH_WRITE
541 cli();
542 boot_page_write(CURRENT_ADDRESS - 2);
543 sei();
544 boot_spm_busy_wait();
545 cli();
546 boot_rww_enable();
547 sei();
548 #endif
549 }
550 }
551 DBG1(0x35, (void *)&currentAddress.l, 4);
552 }
553 return isLast;
554 }
555 #endif
556
557 uchar usbFunctionRead(uchar *data, uchar len)
558 {
559 uchar i;
560
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]);
567 }else{
568 #if ((FLASHEND) > 65535)
569 *data = pgm_read_byte_far(CURRENT_ADDRESS);
570 #else
571 *data = pgm_read_byte(CURRENT_ADDRESS);
572 #endif
573 }
574 data++;
575 CURRENT_ADDRESS++;
576 }
577 return len;
578 }
579
580 /* ------------------------------------------------------------------------ */
581
582 static void initForUsbConnectivity(void)
583 {
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
588 */
589 uint8_t waitloopcnt = 1 + (F_CPU/1048576);
590 #endif
591 usbInit();
592 /* enforce USB re-enumerate: */
593 usbDeviceDisconnect(); /* do this while interrupts are disabled */
594 #if HAVE_UNPRECISEWAIT
595 asm volatile (
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
599 */
600 "initForUsbConnectivity_sleeploop: \n\t"
601 "sbiw r30, 1 \n\t"
602 "sbci %0, 0 \n\t"
603 "brne initForUsbConnectivity_sleeploop \n\t"
604 : "+d" (waitloopcnt)
605 :
606 : "r30","r31"
607 );
608 #else
609 _delay_ms(260); /* fake USB disconnect for > 250 ms */
610 #endif
611 usbDeviceConnect();
612 sei();
613 }
614
615 int __attribute__((__noreturn__)) main(void)
616 {
617 /* initialize */
618 bootLoaderInit();
619 odDebugInit();
620 DBG1(0x00, 0, 0);
621 #ifndef NO_FLASH_WRITE
622 GICR = (1 << IVCE); /* enable change of interrupt vectors */
623 GICR = (1 << IVSEL); /* move interrupts to boot flash section */
624 #endif
625 if(bootLoaderCondition()){
626 #if NEED_WATCHDOG
627 wdt_disable(); /* main app may have enabled watchdog */
628 #endif
629 initForUsbConnectivity();
630 do{
631 usbPoll();
632 #if BOOTLOADER_CAN_EXIT
633 #if USE_EXCESSIVE_ASSEMBLER
634 asm volatile (
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"
640
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"
646
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))
651 );
652 #else
653 if (stayinloader >= 0x10) {
654 if (!bootLoaderCondition()) {
655 stayinloader-=0x10;
656 }
657 } else {
658 if (bootLoaderCondition()) {
659 if (stayinloader > 1) stayinloader-=2;
660 }
661 }
662 #endif
663 #endif
664
665 #if BOOTLOADER_CAN_EXIT
666 }while (stayinloader); /* main event loop, if BOOTLOADER_CAN_EXIT*/
667 #else
668 }while (1); /* main event loop */
669 #endif
670 }
671 leaveBootloader();
672 }
673
674 /* ------------------------------------------------------------------------ */