07300b9f8609a9e83f63cf4681305ee40b2c41d9
[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_ATmega640__)
167 0x1e, 0x96, 0x08, 0
168 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__)
169 0x1e, 0x96, 0x09, 0
170 #elif defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
171 0x1e, 0x96, 0x0a, 0
172 #elif defined (__AVR_ATmega128__)
173 0x1e, 0x97, 0x02, 0
174 #elif defined (__AVR_ATmega1280__)
175 0x1e, 0x97, 0x03, 0
176 #elif defined (__AVR_ATmega1281__)
177 0x1e, 0x97, 0x04, 0
178 #elif defined (__AVR_ATmega1284__)
179 0x1e, 0x97, 0x06, 0
180 #elif defined (__AVR_ATmega1284P__)
181 0x1e, 0x97, 0x05, 0
182 #elif defined (__AVR_ATmega2560__)
183 0x1e, 0x98, 0x01, 0
184 #elif defined (__AVR_ATmega2561__)
185 0x1e, 0x98, 0x02, 0
186 #else
187 # if (defined(SIGNATURE_0) && defined(SIGNATURE_1) && defined(SIGNATURE_2))
188 # warning "Device signature is not known - using AVR Libc suggestion..."
189 SIGNATURE_0, SIGNATURE_1, SIGNATURE_2, 0
190 # else
191 # error "Device signature is not known, please edit main.c!"
192 # endif
193 #endif
194 };
195
196 /* ------------------------------------------------------------------------ */
197
198 #if (USE_BOOTUP_CLEARRAM)
199 /*
200 * Under normal circumstances, RESET will not clear contents of RAM.
201 * As always, if you want it done - do it yourself...
202 */
203 void __attribute__ ((naked)) __attribute__ ((section (".init3"))) __clearram(void);
204 void __clearram(void) {
205 extern size_t __bss_end;
206 asm volatile (
207 "__clearram:\n\t"
208 "ldi r29, %[ramendhi]\n\t"
209 "ldi r28, %[ramendlo]\n\t"
210 "__clearramloop%=:\n\t"
211 "st -Y , __zero_reg__\n\t"
212 "cp r28, %A[bssend]\n\t"
213 "cpc r29, %B[bssend]\n\t"
214 "brne __clearramloop%=\n\t"
215 :
216 : [ramendhi] "M" (((RAMEND+1)>>8) & 0xff),
217 [ramendlo] "M" (((RAMEND+1)>>0) & 0xff),
218 [bssend] "r" (&__bss_end)
219 : "memory"
220 );
221 }
222 #endif
223
224 #if (!USE_EXCESSIVE_ASSEMBLER) || (!(defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)))
225 static void (*nullVector)(void) __attribute__((__noreturn__));
226 #endif
227
228 static void __attribute__((__noreturn__)) leaveBootloader()
229 {
230 #if (USE_EXCESSIVE_ASSEMBLER) && (defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__))
231 asm volatile (
232 "cli\n\t"
233 "clr r30\n\t"
234 "sbi %[usbddr], %[usbminus]\n\t"
235 "cbi %[port], %[bit]\n\t"
236 "out %[usbintrenab], r30\n\t"
237 "out %[usbintrcfg], r30\n\t"
238 "ldi r31, %[ivce]\n\t"
239 "out %[mygicr], r31\n\t"
240 "out %[mygicr], r30\n\t"
241 "clr r31\n\t"
242 "icall\n\t"
243 :
244 : [port] "I" (_SFR_IO_ADDR(PIN_PORT(JUMPER_PORT))),
245 [bit] "I" (PIN(JUMPER_PORT, JUMPER_BIT)),
246 [usbintrenab] "I" (_SFR_IO_ADDR(USB_INTR_ENABLE)),
247 [usbintrcfg] "I" (_SFR_IO_ADDR(USB_INTR_CFG)),
248 [usbddr] "I" (_SFR_IO_ADDR(USBDDR)),
249 [usbminus] "I" (USBMINUS),
250 [mygicr] "I" (_SFR_IO_ADDR(GICR)),
251 [ivce] "I" (1<<IVCE)
252 );
253 /* TODO: compiler will put an unnecessary "ret" at this end ! */
254 #else
255 DBG1(0x01, 0, 0);
256 cli();
257 usbDeviceDisconnect();
258 bootLoaderExit();
259 USB_INTR_ENABLE = 0;
260 USB_INTR_CFG = 0; /* also reset config bits */
261 GICR = (1 << IVCE); /* enable change of interrupt vectors */
262 GICR = (0 << IVSEL); /* move interrupts to application flash section */
263
264 /*
265 * There seems to be another funny compiler Bug.
266 * When gcc is using "eicall" opcode it forgets to modify EIND.
267 * On devices with large flash memory there are some target address bits
268 * missing. In this case some zero bits...
269 */
270 #if (defined(EIND) && ((FLASHEND)>131071))
271 EIND=0;
272 #endif
273 /* We must go through a global function pointer variable instead of writing
274 * ((void (*)(void))0)();
275 * because the compiler optimizes a constant 0 to "rcall 0" which is not
276 * handled correctly by the assembler.
277 */
278 nullVector();
279 #endif
280 }
281
282 /* ------------------------------------------------------------------------ */
283
284
285 uchar usbFunctionSetup_USBASP_FUNC_TRANSMIT(usbRequest_t *rq) {
286 uchar rval = 0;
287 usbWord_t address;
288 address.bytes[1] = rq->wValue.bytes[1];
289 address.bytes[0] = rq->wIndex.bytes[0];
290
291 if(rq->wValue.bytes[0] == 0x30){ /* read signature */
292 rval = rq->wIndex.bytes[0] & 3;
293 rval = signatureBytes[rval];
294 #if HAVE_READ_LOCK_FUSE
295 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__)
296 }else if(rq->wValue.bytes[0] == 0x58 && rq->wValue.bytes[1] == 0x00){ /* read lock bits */
297 rval = boot_lock_fuse_bits_get(GET_LOCK_BITS);
298 }else if(rq->wValue.bytes[0] == 0x50 && rq->wValue.bytes[1] == 0x00){ /* read lfuse bits */
299 rval = boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS);
300 }else if(rq->wValue.bytes[0] == 0x58 && rq->wValue.bytes[1] == 0x08){ /* read hfuse bits */
301 rval = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
302
303 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48A__) || defined (__AVR_ATmega48P__) || defined (__AVR_ATmega48PA__) || \
304 defined (__AVR_ATmega88__) || defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88P__) || defined (__AVR_ATmega88PA__) || \
305 defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__) || \
306 defined (__AVR_ATmega168__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168P__) || defined (__AVR_ATmega168PA__) || \
307 defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__) || \
308 defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__) || \
309 defined (__AVR_ATmega640__) || \
310 defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__) || \
311 defined (__AVR_ATmega128__) || \
312 defined (__AVR_ATmega1280__) || \
313 defined (__AVR_ATmega1281__) || \
314 defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__) || \
315 defined (__AVR_ATmega2560__) || \
316 defined (__AVR_ATmega2561__)
317 }else if(rq->wValue.bytes[0] == 0x58 && rq->wValue.bytes[1] == 0x00){ /* read lock bits */
318 rval = boot_lock_fuse_bits_get(GET_LOCK_BITS);
319 }else if(rq->wValue.bytes[0] == 0x50 && rq->wValue.bytes[1] == 0x00){ /* read lfuse bits */
320 rval = boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS);
321 }else if(rq->wValue.bytes[0] == 0x58 && rq->wValue.bytes[1] == 0x08){ /* read hfuse bits */
322 rval = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
323 }else if(rq->wValue.bytes[0] == 0x50 && rq->wValue.bytes[1] == 0x08){ /* read efuse bits */
324 rval = boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS );
325 #else
326 #warning "HAVE_READ_LOCK_FUSE is activated but MCU unknown -> will not support this feature"
327 #endif
328 #endif
329 #if HAVE_FLASH_BYTE_READACCESS
330 }else if(rq->wValue.bytes[0] == 0x20){ /* read FLASH low byte */
331 #if ((FLASHEND) > 65535)
332 rval = pgm_read_byte_far((((addr_t)address.word)<<1)+0);
333 #else
334 rval = pgm_read_byte((((addr_t)address.word)<<1)+0);
335 #endif
336 }else if(rq->wValue.bytes[0] == 0x28){ /* read FLASH high byte */
337 #if ((FLASHEND) > 65535)
338 rval = pgm_read_byte_far((((addr_t)address.word)<<1)+1);
339 #else
340 rval = pgm_read_byte((((addr_t)address.word)<<1)+1);
341 #endif
342 #endif
343 #if HAVE_EEPROM_BYTE_ACCESS
344 }else if(rq->wValue.bytes[0] == 0xa0){ /* read EEPROM byte */
345 rval = eeprom_read_byte((void *)address.word);
346 }else if(rq->wValue.bytes[0] == 0xc0){ /* write EEPROM byte */
347 eeprom_write_byte((void *)address.word, rq->wIndex.bytes[1]);
348 #endif
349 #if HAVE_CHIP_ERASE
350 }else if(rq->wValue.bytes[0] == 0xac && rq->wValue.bytes[1] == 0x80){ /* chip erase */
351 addr_t addr;
352 #if HAVE_BLB11_SOFTW_LOCKBIT
353 for(addr = 0; addr < (addr_t)(BOOTLOADER_PAGEADDR) ; addr += SPM_PAGESIZE) {
354 #else
355 for(addr = 0; addr <= (addr_t)(FLASHEND) ; addr += SPM_PAGESIZE) {
356 #endif
357 /* wait and erase page */
358 DBG1(0x33, 0, 0);
359 # ifndef NO_FLASH_WRITE
360 boot_spm_busy_wait();
361 cli();
362 boot_page_erase(addr);
363 sei();
364 # endif
365 }
366 #endif
367 }else{
368 /* ignore all others, return default value == 0 */
369 }
370
371 return rval;
372 }
373
374
375 uchar usbFunctionSetup(uchar data[8])
376 {
377 usbRequest_t *rq = (void *)data;
378 uchar len = 0;
379 static uchar replyBuffer[4];
380
381 usbMsgPtr = (usbMsgPtr_t)replyBuffer;
382 if(rq->bRequest == USBASP_FUNC_TRANSMIT){ /* emulate parts of ISP protocol */
383 replyBuffer[3] = usbFunctionSetup_USBASP_FUNC_TRANSMIT(rq);
384 len = 4;
385 }else if((rq->bRequest == USBASP_FUNC_ENABLEPROG) || (rq->bRequest == USBASP_FUNC_SETISPSCK)){
386 /* replyBuffer[0] = 0; is never touched and thus always 0 which means success */
387 len = 1;
388 }else if(rq->bRequest >= USBASP_FUNC_READFLASH && rq->bRequest <= USBASP_FUNC_SETLONGADDRESS){
389 currentAddress.w[0] = rq->wValue.word;
390 if(rq->bRequest == USBASP_FUNC_SETLONGADDRESS){
391 #if (FLASHEND) > 0xffff
392 currentAddress.w[1] = rq->wIndex.word;
393 #endif
394 }else{
395 bytesRemaining = rq->wLength.bytes[0];
396 /* if(rq->bRequest == USBASP_FUNC_WRITEFLASH) only evaluated during writeFlash anyway */
397 isLastPage = rq->wIndex.bytes[1] & 0x02;
398 #if HAVE_EEPROM_PAGED_ACCESS
399 currentRequest = rq->bRequest;
400 #endif
401 len = 0xff; /* hand over to usbFunctionRead() / usbFunctionWrite() */
402 }
403
404 }else if(rq->bRequest == USBASP_FUNC_DISCONNECT){
405
406 #if BOOTLOADER_CAN_EXIT
407 stayinloader &= (0xfe);
408 #endif
409 }else{
410 /* ignore: others, but could be USBASP_FUNC_CONNECT */
411 #if BOOTLOADER_CAN_EXIT
412 stayinloader |= (0x01);
413 #endif
414 }
415 return len;
416 }
417
418 #if (USE_EXCESSIVE_ASSEMBLER) && ((!HAVE_CHIP_ERASE) || (HAVE_ONDEMAND_PAGEERASE)) && (SPM_PAGESIZE <= 256) && (((BOOTLOADER_PAGEADDR>>0)&0xff) == 0)
419 uchar usbFunctionWrite(uchar *data, uchar len)
420 {
421 uchar isLast;
422
423 DBG1(0x31, (void *)&currentAddress.l, 4);
424 if(len > bytesRemaining)
425 len = bytesRemaining;
426 bytesRemaining -= len;
427 isLast = bytesRemaining == 0;
428 if(currentRequest >= USBASP_FUNC_READEEPROM){
429 uchar i;
430 for(i = 0; i < len; i++){
431 eeprom_write_byte((void *)(currentAddress.w[0]++), *data++);
432 }
433 }else{
434 asm volatile (
435 "sbrc %[len], 0\n\t"
436 "inc %[len]\n\t"
437 "usbFunctionWrite_flashloop:\n\t"
438 "subi %[len], 2\n\t"
439 "brlo usbFunctionWrite_finished\n\t"
440
441 #if HAVE_BLB11_SOFTW_LOCKBIT
442 "cpi r31, %[blsaddrhi]\n\t" /* accelerated BLB11_SOFTW_LOCKBIT check */
443 "brsh usbFunctionWrite_finished\n\t"
444 // "brlo usbFunctionWrite_addrunlock_ok\n\t"
445 // "brne usbFunctionWrite_finished\n\t"
446 // "cpi r30, %[blsaddrlo]\n\t"
447 // "brlo usbFunctionWrite_addrunlock_ok\n\t"
448 // "rjmp usbFunctionWrite_finished\n\t"
449 // "usbFunctionWrite_addrunlock_ok:\n\t"
450 #endif
451 "rcall usbFunctionWrite_waitA\n\t"
452 "cli\n\t" /* r0 or r1 may be __zero_reg__ and may become dangerous nonzero within interrupts */
453 "ld r0, X+\n\t"
454 "ld r1, X+\n\t"
455
456 "ldi r18, %[pagfillval]\n\t"
457 "rcall usbFunctionWrite_saveflash\n\t" /* page fill */
458
459 "mov r18, r30\n\t"
460 "subi r18, 0xfe\n\t" /* add with 2 */
461 "andi r18, %[pagemask]\n\t"
462 "breq usbFunctionWrite_pageisfull\n\t"
463 "tst %[islast]\n\t"
464 "breq usbFunctionWrite_skippageisfull\n\t"
465 "tst %[isLastPage]\n\t"
466 "breq usbFunctionWrite_skippageisfull\n\t"
467 "cpi %[len], 0\n\t"
468 "brne usbFunctionWrite_skippageisfull\n\t"
469
470 "usbFunctionWrite_pageisfull:\n\t" /* start writing the page */
471 "ldi r18, %[pageraseval]\n\t"
472 "rcall usbFunctionWrite_saveflash\n\t" /* page erase */
473 "rcall usbFunctionWrite_waitA\n\t"
474
475 "ldi r18, %[pagwriteval]\n\t"
476 "rcall usbFunctionWrite_saveflash\n\t" /* page write */
477 "rcall usbFunctionWrite_waitA\n\t"
478
479 "in __tmp_reg__, %[spmcr]\n\t"
480 "sbrs __tmp_reg__, %[rwwsbbit]\n\t"
481 "rjmp usbFunctionWrite_skippageisfull\n\t"
482 "ldi r18, %[rwwenrval]\n\t"
483 "rcall usbFunctionWrite_saveflash\n\t" /* reenable rww*/
484 // "rcall usbFunctionWrite_waitA\n\t"
485
486
487 "usbFunctionWrite_skippageisfull:\n\t"
488 "adiw r30, 0x2\n\t"
489 "rjmp usbFunctionWrite_flashloop\n\t"
490
491 "usbFunctionWrite_saveflash:\n\t"
492 "cli\n\t"
493 "out %[spmcr], r18\n\t"
494 "spm\n\t"
495 "clr __zero_reg__\n\t" /* if r0 or r1 is __zero_reg__ it may have become inconsisten while page-fill */
496 "sei\n\t"
497 "ret\n\t"
498
499 "usbFunctionWrite_waitA:\n\t"
500 "in __tmp_reg__, %[spmcr]\n\t"
501 "sbrc __tmp_reg__, %[spmenbit]\n\t"
502 "rjmp usbFunctionWrite_waitA\n\t"
503 "ret\n\t"
504
505 "usbFunctionWrite_finished:\n\t"
506 : [addr] "+z" (currentAddress.l)
507
508 : [spmenbit] "I" (SPMEN),
509 [rwwsbbit] "I" (RWWSB),
510 [spmcr] "I" (_SFR_IO_ADDR(__SPM_REG)),
511 [pagfillval] "M" ((1<<SPMEN)),
512 [pageraseval] "M" ((1<<PGERS) | (1<<SPMEN)),
513 [pagwriteval] "M" ((1<<PGWRT) | (1<<SPMEN)),
514 [rwwenrval] "M" ((1<<RWWSRE) | (1<<SPMEN)),
515 [pagemask] "M" (SPM_PAGESIZE-1),
516 #if HAVE_BLB11_SOFTW_LOCKBIT
517 [blsaddrhi] "M" ((uint8_t)((BOOTLOADER_PAGEADDR>>8)&0xff)),
518 // [blsaddrlo] "M" ((uint8_t)((BOOTLOADER_PAGEADDR>>0)&0xff)),
519 #endif
520 [islast] "r" (isLast),
521 [isLastPage] "r" (isLastPage),
522 [len] "d" (len),
523 [dataptr] "x" (data)
524
525 : "r0", "r1", "r18"
526 );
527 }
528 return isLast;
529 }
530 #else
531 uchar usbFunctionWrite(uchar *data, uchar len)
532 {
533 uchar i,isLast;
534
535 DBG1(0x31, (void *)&currentAddress.l, 4);
536 if(len > bytesRemaining)
537 len = bytesRemaining;
538 bytesRemaining -= len;
539 isLast = bytesRemaining == 0;
540 for(i = 0; i < len;) {
541 if(currentRequest >= USBASP_FUNC_READEEPROM){
542 eeprom_write_byte((void *)(currentAddress.w[0]++), *data++);
543 i++;
544 } else {
545 #if HAVE_BLB11_SOFTW_LOCKBIT
546 if (CURRENT_ADDRESS >= (addr_t)(BOOTLOADER_PAGEADDR)) {
547 return 1;
548 }
549 #endif
550 i += 2;
551 DBG1(0x32, 0, 0);
552 cli();
553 boot_page_fill(CURRENT_ADDRESS, *(short *)data);
554 sei();
555 CURRENT_ADDRESS += 2;
556 data += 2;
557 /* write page when we cross page boundary or we have the last partial page */
558 if((currentAddress.w[0] & (SPM_PAGESIZE - 1)) == 0 || (isLast && i >= len && isLastPage)){
559 #if (!HAVE_CHIP_ERASE) || (HAVE_ONDEMAND_PAGEERASE)
560 DBG1(0x33, 0, 0);
561 # ifndef NO_FLASH_WRITE
562 cli();
563 boot_page_erase(CURRENT_ADDRESS - 2); /* erase page */
564 sei();
565 boot_spm_busy_wait(); /* wait until page is erased */
566 # endif
567 #endif
568 DBG1(0x34, 0, 0);
569 #ifndef NO_FLASH_WRITE
570 cli();
571 boot_page_write(CURRENT_ADDRESS - 2);
572 sei();
573 boot_spm_busy_wait();
574 cli();
575 boot_rww_enable();
576 sei();
577 #endif
578 }
579 }
580 DBG1(0x35, (void *)&currentAddress.l, 4);
581 }
582 return isLast;
583 }
584 #endif
585
586 uchar usbFunctionRead(uchar *data, uchar len)
587 {
588 uchar i;
589
590 if(len > bytesRemaining)
591 len = bytesRemaining;
592 bytesRemaining -= len;
593 for(i = 0; i < len; i++){
594 if(currentRequest >= USBASP_FUNC_READEEPROM){
595 *data = eeprom_read_byte((void *)currentAddress.w[0]);
596 }else{
597 #if ((FLASHEND) > 65535)
598 *data = pgm_read_byte_far(CURRENT_ADDRESS);
599 #else
600 *data = pgm_read_byte(CURRENT_ADDRESS);
601 #endif
602 }
603 data++;
604 CURRENT_ADDRESS++;
605 }
606 return len;
607 }
608
609 /* ------------------------------------------------------------------------ */
610
611 static void initForUsbConnectivity(void)
612 {
613 #if HAVE_UNPRECISEWAIT
614 /* (0.25s*F_CPU)/(4 cycles per loop) ~ (65536*waitloopcnt)
615 * F_CPU/(16*65536) ~ waitloopcnt
616 * F_CPU / 1048576 ~ waitloopcnt
617 */
618 uint8_t waitloopcnt = 1 + (F_CPU/1048576);
619 #endif
620 usbInit();
621 /* enforce USB re-enumerate: */
622 usbDeviceDisconnect(); /* do this while interrupts are disabled */
623 #if HAVE_UNPRECISEWAIT
624 asm volatile (
625 /*we really don't care what value Z has...
626 * ...if we loop 65536/F_CPU more or less...
627 * ...unimportant - just save some opcodes
628 */
629 "initForUsbConnectivity_sleeploop: \n\t"
630 "sbiw r30, 1 \n\t"
631 "sbci %0, 0 \n\t"
632 "brne initForUsbConnectivity_sleeploop \n\t"
633 : "+d" (waitloopcnt)
634 :
635 : "r30","r31"
636 );
637 #else
638 _delay_ms(260); /* fake USB disconnect for > 250 ms */
639 #endif
640 usbDeviceConnect();
641 sei();
642 }
643
644 int __attribute__((__noreturn__)) main(void)
645 {
646 /* initialize */
647 bootLoaderInit();
648 odDebugInit();
649 DBG1(0x00, 0, 0);
650 #ifndef NO_FLASH_WRITE
651 GICR = (1 << IVCE); /* enable change of interrupt vectors */
652 GICR = (1 << IVSEL); /* move interrupts to boot flash section */
653 #endif
654 if(bootLoaderCondition()){
655 #if NEED_WATCHDOG
656 # if (defined(MCUSR) && defined(WDRF))
657 /*
658 * Fix issue 6: (special thanks to coldtobi)
659 *
660 * The WDRF bit in the MCUSR needs to be cleared first,
661 * otherwise it is not possible to disable the watchdog
662 */
663 MCUSR &= ~(_BV(WDRF));
664 # endif
665 wdt_disable(); /* main app may have enabled watchdog */
666 #endif
667 initForUsbConnectivity();
668 do{
669 usbPoll();
670 #if BOOTLOADER_CAN_EXIT
671 #if USE_EXCESSIVE_ASSEMBLER
672 asm volatile (
673 "cpi %[sil], 0x10\n\t"
674 "brlo main_stayinloader_smaller\n\t"
675 "sbic %[pin], %[bit]\n\t"
676 "subi %[sil], 0x10\n\t"
677 "rjmp main_stayinloader_finished\n\t"
678
679 "main_stayinloader_smaller:\n\t"
680 "cpi %[sil], 0x2\n\t"
681 "brlo main_stayinloader_finished\n\t"
682 "sbis %[pin], %[bit]\n\t"
683 "subi %[sil], 0x2\n\t"
684
685 "main_stayinloader_finished:\n\t"
686 : [sil] "+d" (stayinloader)
687 : [pin] "I" (_SFR_IO_ADDR(PIN_PIN(JUMPER_PORT))),
688 [bit] "I" (PIN(JUMPER_PORT, JUMPER_BIT))
689 );
690 #else
691 if (stayinloader >= 0x10) {
692 if (!bootLoaderCondition()) {
693 stayinloader-=0x10;
694 }
695 } else {
696 if (bootLoaderCondition()) {
697 if (stayinloader > 1) stayinloader-=2;
698 }
699 }
700 #endif
701 #endif
702
703 #if BOOTLOADER_CAN_EXIT
704 }while (stayinloader); /* main event loop, if BOOTLOADER_CAN_EXIT*/
705 #else
706 }while (1); /* main event loop */
707 #endif
708 }
709 leaveBootloader();
710 }
711
712 /* ------------------------------------------------------------------------ */