2 * Project: USBaspLoader
3 * Author: Christian Starkjohann
4 * Creation Date: 2007-12-08
6 * Copyright: (c) 2007 by OBJECTIVE DEVELOPMENT Software GmbH
7 * License: GNU GPL v2 (see License.txt)
8 * This Revision: $Id: main.c 786 2010-05-30 20:41:40Z cs $
11 #include "spminterface.h" /* must be included as first! */
14 #include <avr/interrupt.h>
15 #include <avr/pgmspace.h>
18 #include <avr/eeprom.h>
19 #include <util/delay.h>
24 * 29.09.2012 / 30.09.2012
26 * Since cpufunc.h is not needed in this context and
27 * since it is not available in all toolchains, this include
28 * becomes deactivated by github issue-report.
29 * (In case of trouble it remains in sourcecode for reactivation.)
31 * The autor would like to thank Lena-M for reporting this
32 * issue (https://github.com/baerwolf/USBaspLoader/issues/1).
34 #include <avr/cpufunc.h>
43 static void leaveBootloader() __attribute__((__noreturn__
));
45 #include "bootloaderconfig.h"
46 #include "usbdrv/usbdrv.c"
48 #ifndef BOOTLOADER_ADDRESS
49 #error need to know the bootloaders flash address!
52 /* ------------------------------------------------------------------------ */
54 /* Request constants used by USBasp */
55 #define USBASP_FUNC_CONNECT 1
56 #define USBASP_FUNC_DISCONNECT 2
57 #define USBASP_FUNC_TRANSMIT 3
58 #define USBASP_FUNC_READFLASH 4
59 #define USBASP_FUNC_ENABLEPROG 5
60 #define USBASP_FUNC_WRITEFLASH 6
61 #define USBASP_FUNC_READEEPROM 7
62 #define USBASP_FUNC_WRITEEEPROM 8
63 #define USBASP_FUNC_SETLONGADDRESS 9
65 // additional USBasp Commands
66 #define USBASP_FUNC_SETISPSCK 10
67 #define USBASP_FUNC_TPI_CONNECT 11
68 #define USBASP_FUNC_TPI_DISCONNECT 12
69 #define USBASP_FUNC_TPI_RAWREAD 13
70 #define USBASP_FUNC_TPI_RAWWRITE 14
71 #define USBASP_FUNC_TPI_READBLOCK 15
72 #define USBASP_FUNC_TPI_WRITEBLOCK 16
73 #define USBASP_FUNC_GETCAPABILITIES 127
74 /* ------------------------------------------------------------------------ */
77 # define ulong unsigned long
80 # define uint unsigned int
83 /* defaults if not in config file: */
84 #ifndef HAVE_EEPROM_PAGED_ACCESS
85 # define HAVE_EEPROM_PAGED_ACCESS 0
87 #ifndef HAVE_EEPROM_BYTE_ACCESS
88 # define HAVE_EEPROM_BYTE_ACCESS 0
90 #ifndef BOOTLOADER_CAN_EXIT
91 # define BOOTLOADER_CAN_EXIT 0
94 /* allow compatibility with avrusbboot's bootloaderconfig.h: */
95 #ifdef BOOTLOADER_INIT
96 # define bootLoaderInit() BOOTLOADER_INIT
97 # define bootLoaderExit()
99 #ifdef BOOTLOADER_CONDITION
100 # define bootLoaderCondition() BOOTLOADER_CONDITION
103 /* device compatibility: */
104 #ifndef GICR /* ATMega*8 don't have GICR, use MCUCR instead */
108 /* ------------------------------------------------------------------------ */
110 #if (FLASHEND) > 0xffff /* we need long addressing */
111 # define CURRENT_ADDRESS currentAddress.l
112 # define addr_t ulong
114 # define CURRENT_ADDRESS currentAddress.w[0]
118 typedef union longConverter
{
120 uint w
[sizeof(addr_t
)/2];
121 uchar b
[sizeof(addr_t
)];
125 #if BOOTLOADER_CAN_EXIT
126 static uchar requestBootLoaderExit
;
128 static volatile unsigned char stayinloader
= 0xfe;
130 static longConverter_t currentAddress
; /* in bytes */
131 static uchar bytesRemaining
;
132 static uchar isLastPage
;
133 #if HAVE_EEPROM_PAGED_ACCESS
134 static uchar currentRequest
;
136 static const uchar currentRequest
= 0;
139 static const uchar signatureBytes
[4] = {
140 #ifdef SIGNATURE_BYTES
142 #elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
144 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48A__) || defined (__AVR_ATmega48P__)
146 #elif defined (__AVR_ATmega48PA__)
148 #elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88P__)
150 #elif defined (__AVR_ATmega88PA__)
152 #elif defined (__AVR_ATmega164A__)
154 #elif defined (__AVR_ATmega164P__)
156 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168P__)
158 #elif defined (__AVR_ATmega168PA__)
160 #elif defined (__AVR_ATmega32__)
162 #elif defined (__AVR_ATmega324A__)
164 #elif defined (__AVR_ATmega324P__)
166 #elif defined (__AVR_ATmega328__)
168 #elif defined (__AVR_ATmega328P__)
170 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__)
172 #elif defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
174 #elif defined (__AVR_ATmega128__)
176 #elif defined (__AVR_ATmega1284__)
178 #elif defined (__AVR_ATmega1284P__)
181 # error "Device signature is not known, please edit main.c!"
185 /* ------------------------------------------------------------------------ */
187 static void (*nullVector
)(void) __attribute__((__noreturn__
));
189 static void leaveBootloader()
193 usbDeviceDisconnect();
196 USB_INTR_CFG
= 0; /* also reset config bits */
197 GICR
= (1 << IVCE
); /* enable change of interrupt vectors */
198 GICR
= (0 << IVSEL
); /* move interrupts to application flash section */
200 /* We must go through a global function pointer variable instead of writing
201 * ((void (*)(void))0)();
202 * because the compiler optimizes a constant 0 to "rcall 0" which is not
203 * handled correctly by the assembler.
208 /* ------------------------------------------------------------------------ */
210 uchar
usbFunctionSetup(uchar data
[8])
212 usbRequest_t
*rq
= (void *)data
;
214 static uchar replyBuffer
[4];
216 usbMsgPtr
= replyBuffer
;
217 if(rq
->bRequest
== USBASP_FUNC_TRANSMIT
){ /* emulate parts of ISP protocol */
220 address
.bytes
[1] = rq
->wValue
.bytes
[1];
221 address
.bytes
[0] = rq
->wIndex
.bytes
[0];
222 if(rq
->wValue
.bytes
[0] == 0x30){ /* read signature */
223 rval
= rq
->wIndex
.bytes
[0] & 3;
224 rval
= signatureBytes
[rval
];
225 #if HAVE_READ_LOCK_FUSE
226 #if defined (__AVR_ATmega8__)
227 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x00){ /* read lock bits */
228 rval
= boot_lock_fuse_bits_get(GET_LOCK_BITS
);
229 }else if(rq
->wValue
.bytes
[0] == 0x50 && rq
->wValue
.bytes
[1] == 0x00){ /* read lfuse bits */
230 rval
= boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS
);
231 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x08){ /* read hfuse bits */
232 rval
= boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS
);
235 #if HAVE_EEPROM_BYTE_ACCESS
236 }else if(rq
->wValue
.bytes
[0] == 0xa0){ /* read EEPROM byte */
237 rval
= eeprom_read_byte((void *)address
.word
);
238 }else if(rq
->wValue
.bytes
[0] == 0xc0){ /* write EEPROM byte */
239 eeprom_write_byte((void *)address
.word
, rq
->wIndex
.bytes
[1]);
242 }else if(rq
->wValue
.bytes
[0] == 0xac && rq
->wValue
.bytes
[1] == 0x80){ /* chip erase */
244 for(addr
= 0; addr
< FLASHEND
+ 1 - 2048; addr
+= SPM_PAGESIZE
) {
245 /* wait and erase page */
247 # ifndef NO_FLASH_WRITE
248 boot_spm_busy_wait();
250 boot_page_erase(addr
);
256 /* ignore all others, return default value == 0 */
258 replyBuffer
[3] = rval
;
260 }else if((rq
->bRequest
== USBASP_FUNC_ENABLEPROG
) || (rq
->bRequest
== USBASP_FUNC_SETISPSCK
)){
261 /* replyBuffer[0] = 0; is never touched and thus always 0 which means success */
263 }else if(rq
->bRequest
>= USBASP_FUNC_READFLASH
&& rq
->bRequest
<= USBASP_FUNC_SETLONGADDRESS
){
264 currentAddress
.w
[0] = rq
->wValue
.word
;
265 if(rq
->bRequest
== USBASP_FUNC_SETLONGADDRESS
){
266 #if (FLASHEND) > 0xffff
267 currentAddress
.w
[1] = rq
->wIndex
.word
;
270 bytesRemaining
= rq
->wLength
.bytes
[0];
271 /* if(rq->bRequest == USBASP_FUNC_WRITEFLASH) only evaluated during writeFlash anyway */
272 isLastPage
= rq
->wIndex
.bytes
[1] & 0x02;
273 #if HAVE_EEPROM_PAGED_ACCESS
274 currentRequest
= rq
->bRequest
;
276 len
= 0xff; /* hand over to usbFunctionRead() / usbFunctionWrite() */
279 }else if(rq
->bRequest
== USBASP_FUNC_DISCONNECT
){
280 stayinloader
&= (0xfe);
281 #if BOOTLOADER_CAN_EXIT
282 requestBootLoaderExit
= 1; /* allow proper shutdown/close of connection */
285 /* ignore: others, but could be USBASP_FUNC_CONNECT */
286 stayinloader
|= (0x01);
291 uchar
usbFunctionWrite(uchar
*data
, uchar len
)
295 DBG1(0x31, (void *)¤tAddress
.l
, 4);
296 if(len
> bytesRemaining
)
297 len
= bytesRemaining
;
298 bytesRemaining
-= len
;
299 isLast
= bytesRemaining
== 0;
300 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
302 for(i
= 0; i
< len
; i
++){
303 eeprom_write_byte((void *)(currentAddress
.w
[0]++), *data
++);
307 for(i
= 0; i
< len
;){
308 #if HAVE_BLB11_SOFTW_LOCKBIT
309 if (CURRENT_ADDRESS
>= (addr_t
)(BOOTLOADER_ADDRESS
)) {
316 boot_page_fill(CURRENT_ADDRESS
, *(short *)data
);
318 CURRENT_ADDRESS
+= 2;
320 /* write page when we cross page boundary or we have the last partial page */
321 if((currentAddress
.w
[0] & (SPM_PAGESIZE
- 1)) == 0 || (isLast
&& i
>= len
&& isLastPage
)){
324 # ifndef NO_FLASH_WRITE
326 boot_page_erase(CURRENT_ADDRESS
- 2); /* erase page */
328 boot_spm_busy_wait(); /* wait until page is erased */
332 #ifndef NO_FLASH_WRITE
334 boot_page_write(CURRENT_ADDRESS
- 2);
336 boot_spm_busy_wait();
343 DBG1(0x35, (void *)¤tAddress
.l
, 4);
348 uchar
usbFunctionRead(uchar
*data
, uchar len
)
352 if(len
> bytesRemaining
)
353 len
= bytesRemaining
;
354 bytesRemaining
-= len
;
355 for(i
= 0; i
< len
; i
++){
356 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
357 *data
= eeprom_read_byte((void *)currentAddress
.w
[0]);
359 *data
= pgm_read_byte((void *)CURRENT_ADDRESS
);
367 /* ------------------------------------------------------------------------ */
369 static void initForUsbConnectivity(void)
374 /* enforce USB re-enumerate: */
375 usbDeviceDisconnect(); /* do this while interrupts are disabled */
376 while(--i
){ /* fake USB disconnect for > 250 ms */
383 int __attribute__((noreturn
)) main(void)
389 #ifndef NO_FLASH_WRITE
390 GICR
= (1 << IVCE
); /* enable change of interrupt vectors */
391 GICR
= (1 << IVSEL
); /* move interrupts to boot flash section */
393 if(bootLoaderCondition()){
394 wdt_disable(); /* main app may have enabled watchdog */
395 #if BOOTLOADER_CAN_EXIT
398 initForUsbConnectivity();
401 #if BOOTLOADER_CAN_EXIT
402 if(requestBootLoaderExit
){
409 if (stayinloader
>= 0x10) {
410 if (!bootLoaderCondition()) {
414 if (bootLoaderCondition()) {
415 if (stayinloader
> 1) stayinloader
-=2;
419 }while (stayinloader
); /* main event loop */
424 /* ------------------------------------------------------------------------ */