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>
44 #include "bootloaderconfig.h"
45 #include "usbdrv/usbdrv.c"
47 #if BOOTLOADER_CAN_EXIT
48 static void leaveBootloader() __attribute__((__noreturn__
));
51 #ifndef BOOTLOADER_ADDRESS
52 #error need to know the bootloaders flash address!
55 /* ------------------------------------------------------------------------ */
57 /* Request constants used by USBasp */
58 #define USBASP_FUNC_CONNECT 1
59 #define USBASP_FUNC_DISCONNECT 2
60 #define USBASP_FUNC_TRANSMIT 3
61 #define USBASP_FUNC_READFLASH 4
62 #define USBASP_FUNC_ENABLEPROG 5
63 #define USBASP_FUNC_WRITEFLASH 6
64 #define USBASP_FUNC_READEEPROM 7
65 #define USBASP_FUNC_WRITEEEPROM 8
66 #define USBASP_FUNC_SETLONGADDRESS 9
68 // additional USBasp Commands
69 #define USBASP_FUNC_SETISPSCK 10
70 #define USBASP_FUNC_TPI_CONNECT 11
71 #define USBASP_FUNC_TPI_DISCONNECT 12
72 #define USBASP_FUNC_TPI_RAWREAD 13
73 #define USBASP_FUNC_TPI_RAWWRITE 14
74 #define USBASP_FUNC_TPI_READBLOCK 15
75 #define USBASP_FUNC_TPI_WRITEBLOCK 16
76 #define USBASP_FUNC_GETCAPABILITIES 127
77 /* ------------------------------------------------------------------------ */
80 # define ulong unsigned long
83 # define uint unsigned int
87 /* allow compatibility with avrusbboot's bootloaderconfig.h: */
88 #ifdef BOOTLOADER_INIT
89 # define bootLoaderInit() BOOTLOADER_INIT
90 # define bootLoaderExit()
92 #ifdef BOOTLOADER_CONDITION
93 # define bootLoaderCondition() BOOTLOADER_CONDITION
96 /* device compatibility: */
97 #ifndef GICR /* ATMega*8 don't have GICR, use MCUCR instead */
101 /* ------------------------------------------------------------------------ */
103 #if (FLASHEND) > 0xffff /* we need long addressing */
104 # define CURRENT_ADDRESS currentAddress.l
105 # define addr_t ulong
107 # define CURRENT_ADDRESS currentAddress.w[0]
111 typedef union longConverter
{
113 uint w
[sizeof(addr_t
)/2];
114 uchar b
[sizeof(addr_t
)];
118 #if BOOTLOADER_CAN_EXIT
119 static volatile unsigned char stayinloader
= 0xfe;
122 static longConverter_t currentAddress
; /* in bytes */
123 static uchar bytesRemaining
;
124 static uchar isLastPage
;
125 #if HAVE_EEPROM_PAGED_ACCESS
126 static uchar currentRequest
;
128 static const uchar currentRequest
= 0;
131 static const uchar signatureBytes
[4] = {
132 #ifdef SIGNATURE_BYTES
134 #elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
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__)
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 BOOTLOADER_CAN_EXIT
184 static void (*nullVector
)(void) __attribute__((__noreturn__
));
186 static void leaveBootloader()
190 usbDeviceDisconnect();
193 USB_INTR_CFG
= 0; /* also reset config bits */
194 GICR
= (1 << IVCE
); /* enable change of interrupt vectors */
195 GICR
= (0 << IVSEL
); /* move interrupts to application flash section */
197 /* We must go through a global function pointer variable instead of writing
198 * ((void (*)(void))0)();
199 * because the compiler optimizes a constant 0 to "rcall 0" which is not
200 * handled correctly by the assembler.
206 /* ------------------------------------------------------------------------ */
208 uchar
usbFunctionSetup(uchar data
[8])
210 usbRequest_t
*rq
= (void *)data
;
212 static uchar replyBuffer
[4];
214 usbMsgPtr
= replyBuffer
;
215 if(rq
->bRequest
== USBASP_FUNC_TRANSMIT
){ /* emulate parts of ISP protocol */
218 address
.bytes
[1] = rq
->wValue
.bytes
[1];
219 address
.bytes
[0] = rq
->wIndex
.bytes
[0];
220 if(rq
->wValue
.bytes
[0] == 0x30){ /* read signature */
221 rval
= rq
->wIndex
.bytes
[0] & 3;
222 rval
= signatureBytes
[rval
];
223 #if HAVE_READ_LOCK_FUSE
224 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega32__)
225 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x00){ /* read lock bits */
226 rval
= boot_lock_fuse_bits_get(GET_LOCK_BITS
);
227 }else if(rq
->wValue
.bytes
[0] == 0x50 && rq
->wValue
.bytes
[1] == 0x00){ /* read lfuse bits */
228 rval
= boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS
);
229 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x08){ /* read hfuse bits */
230 rval
= boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS
);
232 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48A__) || defined (__AVR_ATmega48P__) || defined (__AVR_ATmega48PA__) || \
233 defined (__AVR_ATmega88__) || defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88P__) || defined (__AVR_ATmega88PA__) || \
234 defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__) || \
235 defined (__AVR_ATmega168__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168P__) || defined (__AVR_ATmega168PA__) || \
236 defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__) || \
237 defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__) || \
238 defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__) || \
239 defined (__AVR_ATmega128__) || \
240 defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
241 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x00){ /* read lock bits */
242 rval
= boot_lock_fuse_bits_get(GET_LOCK_BITS
);
243 }else if(rq
->wValue
.bytes
[0] == 0x50 && rq
->wValue
.bytes
[1] == 0x00){ /* read lfuse bits */
244 rval
= boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS
);
245 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x08){ /* read hfuse bits */
246 rval
= boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS
);
247 }else if(rq
->wValue
.bytes
[0] == 0x50 && rq
->wValue
.bytes
[1] == 0x08){ /* read efuse bits */
248 rval
= boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS
);
250 #warning "HAVE_READ_LOCK_FUSE is activated but MCU unknown -> will not support this feature"
253 #if HAVE_EEPROM_BYTE_ACCESS
254 }else if(rq
->wValue
.bytes
[0] == 0xa0){ /* read EEPROM byte */
255 rval
= eeprom_read_byte((void *)address
.word
);
256 }else if(rq
->wValue
.bytes
[0] == 0xc0){ /* write EEPROM byte */
257 eeprom_write_byte((void *)address
.word
, rq
->wIndex
.bytes
[1]);
260 }else if(rq
->wValue
.bytes
[0] == 0xac && rq
->wValue
.bytes
[1] == 0x80){ /* chip erase */
262 for(addr
= 0; addr
< FLASHEND
+ 1 - 2048; addr
+= SPM_PAGESIZE
) {
263 /* wait and erase page */
265 # ifndef NO_FLASH_WRITE
266 boot_spm_busy_wait();
268 boot_page_erase(addr
);
274 /* ignore all others, return default value == 0 */
276 replyBuffer
[3] = rval
;
278 }else if((rq
->bRequest
== USBASP_FUNC_ENABLEPROG
) || (rq
->bRequest
== USBASP_FUNC_SETISPSCK
)){
279 /* replyBuffer[0] = 0; is never touched and thus always 0 which means success */
281 }else if(rq
->bRequest
>= USBASP_FUNC_READFLASH
&& rq
->bRequest
<= USBASP_FUNC_SETLONGADDRESS
){
282 currentAddress
.w
[0] = rq
->wValue
.word
;
283 if(rq
->bRequest
== USBASP_FUNC_SETLONGADDRESS
){
284 #if (FLASHEND) > 0xffff
285 currentAddress
.w
[1] = rq
->wIndex
.word
;
288 bytesRemaining
= rq
->wLength
.bytes
[0];
289 /* if(rq->bRequest == USBASP_FUNC_WRITEFLASH) only evaluated during writeFlash anyway */
290 isLastPage
= rq
->wIndex
.bytes
[1] & 0x02;
291 #if HAVE_EEPROM_PAGED_ACCESS
292 currentRequest
= rq
->bRequest
;
294 len
= 0xff; /* hand over to usbFunctionRead() / usbFunctionWrite() */
297 }else if(rq
->bRequest
== USBASP_FUNC_DISCONNECT
){
299 #if BOOTLOADER_CAN_EXIT
300 stayinloader
&= (0xfe);
303 /* ignore: others, but could be USBASP_FUNC_CONNECT */
304 #if BOOTLOADER_CAN_EXIT
305 stayinloader
|= (0x01);
311 uchar
usbFunctionWrite(uchar
*data
, uchar len
)
315 DBG1(0x31, (void *)¤tAddress
.l
, 4);
316 if(len
> bytesRemaining
)
317 len
= bytesRemaining
;
318 bytesRemaining
-= len
;
319 isLast
= bytesRemaining
== 0;
320 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
322 for(i
= 0; i
< len
; i
++){
323 eeprom_write_byte((void *)(currentAddress
.w
[0]++), *data
++);
327 for(i
= 0; i
< len
;){
328 #if HAVE_BLB11_SOFTW_LOCKBIT
329 if (CURRENT_ADDRESS
>= (addr_t
)(BOOTLOADER_ADDRESS
)) {
336 boot_page_fill(CURRENT_ADDRESS
, *(short *)data
);
338 CURRENT_ADDRESS
+= 2;
340 /* write page when we cross page boundary or we have the last partial page */
341 if((currentAddress
.w
[0] & (SPM_PAGESIZE
- 1)) == 0 || (isLast
&& i
>= len
&& isLastPage
)){
344 # ifndef NO_FLASH_WRITE
346 boot_page_erase(CURRENT_ADDRESS
- 2); /* erase page */
348 boot_spm_busy_wait(); /* wait until page is erased */
352 #ifndef NO_FLASH_WRITE
354 boot_page_write(CURRENT_ADDRESS
- 2);
356 boot_spm_busy_wait();
363 DBG1(0x35, (void *)¤tAddress
.l
, 4);
368 uchar
usbFunctionRead(uchar
*data
, uchar len
)
372 if(len
> bytesRemaining
)
373 len
= bytesRemaining
;
374 bytesRemaining
-= len
;
375 for(i
= 0; i
< len
; i
++){
376 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
377 *data
= eeprom_read_byte((void *)currentAddress
.w
[0]);
379 *data
= pgm_read_byte((void *)CURRENT_ADDRESS
);
387 /* ------------------------------------------------------------------------ */
389 static void initForUsbConnectivity(void)
391 #if HAVE_UNPRECISEWAIT
392 /* (0.25s*F_CPU)/(4 cycles per loop) ~ (65536*waitloopcnt)
393 * F_CPU/(16*65536) ~ waitloopcnt
394 * F_CPU / 1048576 ~ waitloopcnt
396 uint8_t waitloopcnt
= 1 + (F_CPU
/1048576);
399 /* enforce USB re-enumerate: */
400 usbDeviceDisconnect(); /* do this while interrupts are disabled */
401 #if HAVE_UNPRECISEWAIT
403 /*we really don't care what value Z has...
404 * ...if we loop 65536/F_CPU more or less...
405 * ...unimportant - just save some opcodes
407 "initForUsbConnectivity_sleeploop: \n\t"
410 "brne initForUsbConnectivity_sleeploop \n\t"
416 _delay_ms(260); /* fake USB disconnect for > 250 ms */
422 int __attribute__((noreturn
)) main(void)
428 #ifndef NO_FLASH_WRITE
429 GICR
= (1 << IVCE
); /* enable change of interrupt vectors */
430 GICR
= (1 << IVSEL
); /* move interrupts to boot flash section */
432 if(bootLoaderCondition()){
434 wdt_disable(); /* main app may have enabled watchdog */
436 initForUsbConnectivity();
439 #if BOOTLOADER_CAN_EXIT
440 if (stayinloader
>= 0x10) {
441 if (!bootLoaderCondition()) {
445 if (bootLoaderCondition()) {
446 if (stayinloader
> 1) stayinloader
-=2;
451 #if BOOTLOADER_CAN_EXIT
452 }while (stayinloader
); /* main event loop, if BOOTLOADER_CAN_EXIT*/
454 }while (1); /* main event loop */
457 #if BOOTLOADER_CAN_EXIT
462 /* ------------------------------------------------------------------------ */