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>
21 #include <avr/cpufunc.h>
27 static void leaveBootloader() __attribute__((__noreturn__
));
29 #include "bootloaderconfig.h"
30 #include "usbdrv/usbdrv.c"
32 #ifndef BOOTLOADER_ADDRESS
33 #error need to know the bootloaders flash address!
36 /* ------------------------------------------------------------------------ */
38 /* Request constants used by USBasp */
39 #define USBASP_FUNC_CONNECT 1
40 #define USBASP_FUNC_DISCONNECT 2
41 #define USBASP_FUNC_TRANSMIT 3
42 #define USBASP_FUNC_READFLASH 4
43 #define USBASP_FUNC_ENABLEPROG 5
44 #define USBASP_FUNC_WRITEFLASH 6
45 #define USBASP_FUNC_READEEPROM 7
46 #define USBASP_FUNC_WRITEEEPROM 8
47 #define USBASP_FUNC_SETLONGADDRESS 9
49 // additional USBasp Commands
50 #define USBASP_FUNC_SETISPSCK 10
51 #define USBASP_FUNC_TPI_CONNECT 11
52 #define USBASP_FUNC_TPI_DISCONNECT 12
53 #define USBASP_FUNC_TPI_RAWREAD 13
54 #define USBASP_FUNC_TPI_RAWWRITE 14
55 #define USBASP_FUNC_TPI_READBLOCK 15
56 #define USBASP_FUNC_TPI_WRITEBLOCK 16
57 #define USBASP_FUNC_GETCAPABILITIES 127
58 /* ------------------------------------------------------------------------ */
61 # define ulong unsigned long
64 # define uint unsigned int
67 /* defaults if not in config file: */
68 #ifndef HAVE_EEPROM_PAGED_ACCESS
69 # define HAVE_EEPROM_PAGED_ACCESS 0
71 #ifndef HAVE_EEPROM_BYTE_ACCESS
72 # define HAVE_EEPROM_BYTE_ACCESS 0
74 #ifndef BOOTLOADER_CAN_EXIT
75 # define BOOTLOADER_CAN_EXIT 0
78 /* allow compatibility with avrusbboot's bootloaderconfig.h: */
79 #ifdef BOOTLOADER_INIT
80 # define bootLoaderInit() BOOTLOADER_INIT
81 # define bootLoaderExit()
83 #ifdef BOOTLOADER_CONDITION
84 # define bootLoaderCondition() BOOTLOADER_CONDITION
87 /* device compatibility: */
88 #ifndef GICR /* ATMega*8 don't have GICR, use MCUCR instead */
92 /* ------------------------------------------------------------------------ */
94 #if (FLASHEND) > 0xffff /* we need long addressing */
95 # define CURRENT_ADDRESS currentAddress.l
98 # define CURRENT_ADDRESS currentAddress.w[0]
102 typedef union longConverter
{
104 uint w
[sizeof(addr_t
)/2];
105 uchar b
[sizeof(addr_t
)];
109 #if BOOTLOADER_CAN_EXIT
110 static uchar requestBootLoaderExit
;
112 static volatile unsigned char stayinloader
= 0xfe;
114 static longConverter_t currentAddress
; /* in bytes */
115 static uchar bytesRemaining
;
116 static uchar isLastPage
;
117 #if HAVE_EEPROM_PAGED_ACCESS
118 static uchar currentRequest
;
120 static const uchar currentRequest
= 0;
123 static const uchar signatureBytes
[4] = {
124 #ifdef SIGNATURE_BYTES
126 #elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8HVA__)
128 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48P__)
130 #elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__)
132 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
134 #elif defined (__AVR_ATmega328P__)
136 #elif defined (__AVR_ATmega1284P__)
139 # error "Device signature is not known, please edit main.c!"
143 /* ------------------------------------------------------------------------ */
145 static void (*nullVector
)(void) __attribute__((__noreturn__
));
147 static void leaveBootloader()
151 usbDeviceDisconnect();
154 USB_INTR_CFG
= 0; /* also reset config bits */
155 GICR
= (1 << IVCE
); /* enable change of interrupt vectors */
156 GICR
= (0 << IVSEL
); /* move interrupts to application flash section */
158 /* We must go through a global function pointer variable instead of writing
159 * ((void (*)(void))0)();
160 * because the compiler optimizes a constant 0 to "rcall 0" which is not
161 * handled correctly by the assembler.
166 /* ------------------------------------------------------------------------ */
168 uchar
usbFunctionSetup(uchar data
[8])
170 usbRequest_t
*rq
= (void *)data
;
172 static uchar replyBuffer
[4];
174 usbMsgPtr
= replyBuffer
;
175 if(rq
->bRequest
== USBASP_FUNC_TRANSMIT
){ /* emulate parts of ISP protocol */
178 address
.bytes
[1] = rq
->wValue
.bytes
[1];
179 address
.bytes
[0] = rq
->wIndex
.bytes
[0];
180 if(rq
->wValue
.bytes
[0] == 0x30){ /* read signature */
181 rval
= rq
->wIndex
.bytes
[0] & 3;
182 rval
= signatureBytes
[rval
];
183 #if HAVE_READ_LOCK_FUSE
184 #if defined (__AVR_ATmega8__)
185 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x00){ /* read lock bits */
186 rval
= boot_lock_fuse_bits_get(GET_LOCK_BITS
);
187 }else if(rq
->wValue
.bytes
[0] == 0x50 && rq
->wValue
.bytes
[1] == 0x00){ /* read lfuse bits */
188 rval
= boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS
);
189 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x08){ /* read hfuse bits */
190 rval
= boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS
);
193 #if HAVE_EEPROM_BYTE_ACCESS
194 }else if(rq
->wValue
.bytes
[0] == 0xa0){ /* read EEPROM byte */
195 rval
= eeprom_read_byte((void *)address
.word
);
196 }else if(rq
->wValue
.bytes
[0] == 0xc0){ /* write EEPROM byte */
197 eeprom_write_byte((void *)address
.word
, rq
->wIndex
.bytes
[1]);
200 }else if(rq
->wValue
.bytes
[0] == 0xac && rq
->wValue
.bytes
[1] == 0x80){ /* chip erase */
202 for(addr
= 0; addr
< FLASHEND
+ 1 - 2048; addr
+= SPM_PAGESIZE
) {
203 /* wait and erase page */
205 # ifndef NO_FLASH_WRITE
206 boot_spm_busy_wait();
208 boot_page_erase(addr
);
214 /* ignore all others, return default value == 0 */
216 replyBuffer
[3] = rval
;
218 }else if(rq
->bRequest
== USBASP_FUNC_ENABLEPROG
){
219 /* replyBuffer[0] = 0; is never touched and thus always 0 which means success */
221 }else if(rq
->bRequest
>= USBASP_FUNC_READFLASH
&& rq
->bRequest
<= USBASP_FUNC_SETLONGADDRESS
){
222 currentAddress
.w
[0] = rq
->wValue
.word
;
223 if(rq
->bRequest
== USBASP_FUNC_SETLONGADDRESS
){
224 #if (FLASHEND) > 0xffff
225 currentAddress
.w
[1] = rq
->wIndex
.word
;
228 bytesRemaining
= rq
->wLength
.bytes
[0];
229 /* if(rq->bRequest == USBASP_FUNC_WRITEFLASH) only evaluated during writeFlash anyway */
230 isLastPage
= rq
->wIndex
.bytes
[1] & 0x02;
231 #if HAVE_EEPROM_PAGED_ACCESS
232 currentRequest
= rq
->bRequest
;
234 len
= 0xff; /* hand over to usbFunctionRead() / usbFunctionWrite() */
237 }else if(rq
->bRequest
== USBASP_FUNC_DISCONNECT
){
238 stayinloader
&= (0xfe);
239 #if BOOTLOADER_CAN_EXIT
240 requestBootLoaderExit
= 1; /* allow proper shutdown/close of connection */
243 /* ignore: others, but could be USBASP_FUNC_CONNECT */
244 stayinloader
|= (0x01);
249 uchar
usbFunctionWrite(uchar
*data
, uchar len
)
253 DBG1(0x31, (void *)¤tAddress
.l
, 4);
254 if(len
> bytesRemaining
)
255 len
= bytesRemaining
;
256 bytesRemaining
-= len
;
257 isLast
= bytesRemaining
== 0;
258 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
260 for(i
= 0; i
< len
; i
++){
261 eeprom_write_byte((void *)(currentAddress
.w
[0]++), *data
++);
265 for(i
= 0; i
< len
;){
266 #if HAVE_BLB11_SOFTW_LOCKBIT
267 if (CURRENT_ADDRESS
>= (addr_t
)(BOOTLOADER_ADDRESS
)) {
274 boot_page_fill(CURRENT_ADDRESS
, *(short *)data
);
276 CURRENT_ADDRESS
+= 2;
278 /* write page when we cross page boundary or we have the last partial page */
279 if((currentAddress
.w
[0] & (SPM_PAGESIZE
- 1)) == 0 || (isLast
&& i
>= len
&& isLastPage
)){
282 # ifndef NO_FLASH_WRITE
284 boot_page_erase(CURRENT_ADDRESS
- 2); /* erase page */
286 boot_spm_busy_wait(); /* wait until page is erased */
290 #ifndef NO_FLASH_WRITE
292 boot_page_write(CURRENT_ADDRESS
- 2);
294 boot_spm_busy_wait();
301 DBG1(0x35, (void *)¤tAddress
.l
, 4);
306 uchar
usbFunctionRead(uchar
*data
, uchar len
)
310 if(len
> bytesRemaining
)
311 len
= bytesRemaining
;
312 bytesRemaining
-= len
;
313 for(i
= 0; i
< len
; i
++){
314 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
315 *data
= eeprom_read_byte((void *)currentAddress
.w
[0]);
317 *data
= pgm_read_byte((void *)CURRENT_ADDRESS
);
325 /* ------------------------------------------------------------------------ */
327 static void initForUsbConnectivity(void)
332 /* enforce USB re-enumerate: */
333 usbDeviceDisconnect(); /* do this while interrupts are disabled */
334 while(--i
){ /* fake USB disconnect for > 250 ms */
341 int __attribute__((noreturn
)) main(void)
347 #ifndef NO_FLASH_WRITE
348 GICR
= (1 << IVCE
); /* enable change of interrupt vectors */
349 GICR
= (1 << IVSEL
); /* move interrupts to boot flash section */
351 if(bootLoaderCondition()){
352 wdt_disable(); /* main app may have enabled watchdog */
353 #if BOOTLOADER_CAN_EXIT
356 initForUsbConnectivity();
359 #if BOOTLOADER_CAN_EXIT
360 if(requestBootLoaderExit
){
367 if (stayinloader
>= 0x10) {
368 if (!bootLoaderCondition()) {
372 if (bootLoaderCondition()) {
373 if (stayinloader
> 1) stayinloader
-=2;
377 }while (stayinloader
); /* main event loop */
382 /* ------------------------------------------------------------------------ */