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 static void leaveBootloader() __attribute__((__noreturn__
));
47 #include "bootloaderconfig.h"
48 #include "usbdrv/usbdrv.c"
50 #ifndef BOOTLOADER_ADDRESS
51 #error need to know the bootloaders flash address!
54 /* ------------------------------------------------------------------------ */
56 /* Request constants used by USBasp */
57 #define USBASP_FUNC_CONNECT 1
58 #define USBASP_FUNC_DISCONNECT 2
59 #define USBASP_FUNC_TRANSMIT 3
60 #define USBASP_FUNC_READFLASH 4
61 #define USBASP_FUNC_ENABLEPROG 5
62 #define USBASP_FUNC_WRITEFLASH 6
63 #define USBASP_FUNC_READEEPROM 7
64 #define USBASP_FUNC_WRITEEEPROM 8
65 #define USBASP_FUNC_SETLONGADDRESS 9
67 // additional USBasp Commands
68 #define USBASP_FUNC_SETISPSCK 10
69 #define USBASP_FUNC_TPI_CONNECT 11
70 #define USBASP_FUNC_TPI_DISCONNECT 12
71 #define USBASP_FUNC_TPI_RAWREAD 13
72 #define USBASP_FUNC_TPI_RAWWRITE 14
73 #define USBASP_FUNC_TPI_READBLOCK 15
74 #define USBASP_FUNC_TPI_WRITEBLOCK 16
75 #define USBASP_FUNC_GETCAPABILITIES 127
76 /* ------------------------------------------------------------------------ */
79 # define ulong unsigned long
82 # define uint unsigned int
86 /* allow compatibility with avrusbboot's bootloaderconfig.h: */
87 #ifdef BOOTLOADER_INIT
88 # define bootLoaderInit() BOOTLOADER_INIT
89 # define bootLoaderExit()
91 #ifdef BOOTLOADER_CONDITION
92 # define bootLoaderCondition() BOOTLOADER_CONDITION
95 /* device compatibility: */
96 #ifndef GICR /* ATMega*8 don't have GICR, use MCUCR instead */
100 /* ------------------------------------------------------------------------ */
102 #if (FLASHEND) > 0xffff /* we need long addressing */
103 # define CURRENT_ADDRESS currentAddress.l
104 # define addr_t ulong
106 # define CURRENT_ADDRESS currentAddress.w[0]
110 typedef union longConverter
{
112 uint w
[sizeof(addr_t
)/2];
113 uchar b
[sizeof(addr_t
)];
117 #if BOOTLOADER_CAN_EXIT
118 static volatile unsigned char stayinloader
= 0xfe;
121 static longConverter_t currentAddress
; /* in bytes */
122 static uchar bytesRemaining
;
123 static uchar isLastPage
;
124 #if HAVE_EEPROM_PAGED_ACCESS
125 static uchar currentRequest
;
127 static const uchar currentRequest
= 0;
130 static const uchar signatureBytes
[4] = {
131 #ifdef SIGNATURE_BYTES
133 #elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
135 #elif defined (__AVR_ATmega32__)
137 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48A__) || defined (__AVR_ATmega48P__)
138 #error ATmega48 does not support bootloaders!
140 #elif defined (__AVR_ATmega48PA__)
141 #error ATmega48 does not support bootloaders!
143 #elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88P__)
145 #elif defined (__AVR_ATmega88PA__)
147 #elif defined (__AVR_ATmega164A__)
149 #elif defined (__AVR_ATmega164P__)
151 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168P__)
153 #elif defined (__AVR_ATmega168PA__)
155 #elif defined (__AVR_ATmega324A__)
157 #elif defined (__AVR_ATmega324P__)
159 #elif defined (__AVR_ATmega324PA__)
161 #elif defined (__AVR_ATmega328__)
163 #elif defined (__AVR_ATmega328P__)
165 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__)
167 #elif defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
169 #elif defined (__AVR_ATmega128__)
171 #elif defined (__AVR_ATmega1284__)
173 #elif defined (__AVR_ATmega1284P__)
176 # error "Device signature is not known, please edit main.c!"
180 /* ------------------------------------------------------------------------ */
182 static void (*nullVector
)(void) __attribute__((__noreturn__
));
184 static void leaveBootloader()
188 usbDeviceDisconnect();
191 USB_INTR_CFG
= 0; /* also reset config bits */
192 GICR
= (1 << IVCE
); /* enable change of interrupt vectors */
193 GICR
= (0 << IVSEL
); /* move interrupts to application flash section */
195 /* We must go through a global function pointer variable instead of writing
196 * ((void (*)(void))0)();
197 * because the compiler optimizes a constant 0 to "rcall 0" which is not
198 * handled correctly by the assembler.
203 /* ------------------------------------------------------------------------ */
205 uchar
usbFunctionSetup(uchar data
[8])
207 usbRequest_t
*rq
= (void *)data
;
209 static uchar replyBuffer
[4];
211 usbMsgPtr
= replyBuffer
;
212 if(rq
->bRequest
== USBASP_FUNC_TRANSMIT
){ /* emulate parts of ISP protocol */
215 address
.bytes
[1] = rq
->wValue
.bytes
[1];
216 address
.bytes
[0] = rq
->wIndex
.bytes
[0];
217 if(rq
->wValue
.bytes
[0] == 0x30){ /* read signature */
218 rval
= rq
->wIndex
.bytes
[0] & 3;
219 rval
= signatureBytes
[rval
];
220 #if HAVE_READ_LOCK_FUSE
221 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega32__)
222 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x00){ /* read lock bits */
223 rval
= boot_lock_fuse_bits_get(GET_LOCK_BITS
);
224 }else if(rq
->wValue
.bytes
[0] == 0x50 && rq
->wValue
.bytes
[1] == 0x00){ /* read lfuse bits */
225 rval
= boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS
);
226 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x08){ /* read hfuse bits */
227 rval
= boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS
);
229 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48A__) || defined (__AVR_ATmega48P__) || defined (__AVR_ATmega48PA__) || \
230 defined (__AVR_ATmega88__) || defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88P__) || defined (__AVR_ATmega88PA__) || \
231 defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__) || \
232 defined (__AVR_ATmega168__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168P__) || defined (__AVR_ATmega168PA__) || \
233 defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__) || \
234 defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__) || \
235 defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__) || \
236 defined (__AVR_ATmega128__) || \
237 defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
238 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x00){ /* read lock bits */
239 rval
= boot_lock_fuse_bits_get(GET_LOCK_BITS
);
240 }else if(rq
->wValue
.bytes
[0] == 0x50 && rq
->wValue
.bytes
[1] == 0x00){ /* read lfuse bits */
241 rval
= boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS
);
242 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x08){ /* read hfuse bits */
243 rval
= boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS
);
244 }else if(rq
->wValue
.bytes
[0] == 0x50 && rq
->wValue
.bytes
[1] == 0x08){ /* read efuse bits */
245 rval
= boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS
);
247 #warning "HAVE_READ_LOCK_FUSE is activated but MCU unknown -> will not support this feature"
250 #if HAVE_EEPROM_BYTE_ACCESS
251 }else if(rq
->wValue
.bytes
[0] == 0xa0){ /* read EEPROM byte */
252 rval
= eeprom_read_byte((void *)address
.word
);
253 }else if(rq
->wValue
.bytes
[0] == 0xc0){ /* write EEPROM byte */
254 eeprom_write_byte((void *)address
.word
, rq
->wIndex
.bytes
[1]);
257 }else if(rq
->wValue
.bytes
[0] == 0xac && rq
->wValue
.bytes
[1] == 0x80){ /* chip erase */
259 for(addr
= 0; addr
< FLASHEND
+ 1 - 2048; addr
+= SPM_PAGESIZE
) {
260 /* wait and erase page */
262 # ifndef NO_FLASH_WRITE
263 boot_spm_busy_wait();
265 boot_page_erase(addr
);
271 /* ignore all others, return default value == 0 */
273 replyBuffer
[3] = rval
;
275 }else if((rq
->bRequest
== USBASP_FUNC_ENABLEPROG
) || (rq
->bRequest
== USBASP_FUNC_SETISPSCK
)){
276 /* replyBuffer[0] = 0; is never touched and thus always 0 which means success */
278 }else if(rq
->bRequest
>= USBASP_FUNC_READFLASH
&& rq
->bRequest
<= USBASP_FUNC_SETLONGADDRESS
){
279 currentAddress
.w
[0] = rq
->wValue
.word
;
280 if(rq
->bRequest
== USBASP_FUNC_SETLONGADDRESS
){
281 #if (FLASHEND) > 0xffff
282 currentAddress
.w
[1] = rq
->wIndex
.word
;
285 bytesRemaining
= rq
->wLength
.bytes
[0];
286 /* if(rq->bRequest == USBASP_FUNC_WRITEFLASH) only evaluated during writeFlash anyway */
287 isLastPage
= rq
->wIndex
.bytes
[1] & 0x02;
288 #if HAVE_EEPROM_PAGED_ACCESS
289 currentRequest
= rq
->bRequest
;
291 len
= 0xff; /* hand over to usbFunctionRead() / usbFunctionWrite() */
294 }else if(rq
->bRequest
== USBASP_FUNC_DISCONNECT
){
296 #if BOOTLOADER_CAN_EXIT
297 stayinloader
&= (0xfe);
300 /* ignore: others, but could be USBASP_FUNC_CONNECT */
301 #if BOOTLOADER_CAN_EXIT
302 stayinloader
|= (0x01);
308 uchar
usbFunctionWrite(uchar
*data
, uchar len
)
312 DBG1(0x31, (void *)¤tAddress
.l
, 4);
313 if(len
> bytesRemaining
)
314 len
= bytesRemaining
;
315 bytesRemaining
-= len
;
316 isLast
= bytesRemaining
== 0;
317 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
319 for(i
= 0; i
< len
; i
++){
320 eeprom_write_byte((void *)(currentAddress
.w
[0]++), *data
++);
324 for(i
= 0; i
< len
;){
325 #if HAVE_BLB11_SOFTW_LOCKBIT
326 if (CURRENT_ADDRESS
>= (addr_t
)(BOOTLOADER_ADDRESS
)) {
333 boot_page_fill(CURRENT_ADDRESS
, *(short *)data
);
335 CURRENT_ADDRESS
+= 2;
337 /* write page when we cross page boundary or we have the last partial page */
338 if((currentAddress
.w
[0] & (SPM_PAGESIZE
- 1)) == 0 || (isLast
&& i
>= len
&& isLastPage
)){
341 # ifndef NO_FLASH_WRITE
343 boot_page_erase(CURRENT_ADDRESS
- 2); /* erase page */
345 boot_spm_busy_wait(); /* wait until page is erased */
349 #ifndef NO_FLASH_WRITE
351 boot_page_write(CURRENT_ADDRESS
- 2);
353 boot_spm_busy_wait();
360 DBG1(0x35, (void *)¤tAddress
.l
, 4);
365 uchar
usbFunctionRead(uchar
*data
, uchar len
)
369 if(len
> bytesRemaining
)
370 len
= bytesRemaining
;
371 bytesRemaining
-= len
;
372 for(i
= 0; i
< len
; i
++){
373 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
374 *data
= eeprom_read_byte((void *)currentAddress
.w
[0]);
376 *data
= pgm_read_byte((void *)CURRENT_ADDRESS
);
384 /* ------------------------------------------------------------------------ */
386 static void initForUsbConnectivity(void)
388 #if HAVE_UNPRECISEWAIT
389 /* (0.25s*F_CPU)/(4 cycles per loop) ~ (65536*waitloopcnt)
390 * F_CPU/(16*65536) ~ waitloopcnt
391 * F_CPU / 1048576 ~ waitloopcnt
393 uint8_t waitloopcnt
= 1 + (F_CPU
/1048576);
396 /* enforce USB re-enumerate: */
397 usbDeviceDisconnect(); /* do this while interrupts are disabled */
398 #if HAVE_UNPRECISEWAIT
400 /*we really don't care what value Z has...
401 * ...if we loop 65536/F_CPU more or less...
402 * ...unimportant - just save some opcodes
404 "initForUsbConnectivity_sleeploop: \n\t"
407 "brne initForUsbConnectivity_sleeploop \n\t"
413 _delay_ms(260); /* fake USB disconnect for > 250 ms */
419 int __attribute__((noreturn
)) main(void)
425 #ifndef NO_FLASH_WRITE
426 GICR
= (1 << IVCE
); /* enable change of interrupt vectors */
427 GICR
= (1 << IVSEL
); /* move interrupts to boot flash section */
429 if(bootLoaderCondition()){
431 wdt_disable(); /* main app may have enabled watchdog */
433 initForUsbConnectivity();
436 #if BOOTLOADER_CAN_EXIT
437 if (stayinloader
>= 0x10) {
438 if (!bootLoaderCondition()) {
442 if (bootLoaderCondition()) {
443 if (stayinloader
> 1) stayinloader
-=2;
448 #if BOOTLOADER_CAN_EXIT
449 }while (stayinloader
); /* main event loop, if BOOTLOADER_CAN_EXIT*/
451 }while (1); /* main event loop */
457 /* ------------------------------------------------------------------------ */