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 /* ------------------------------------------------------------------------ */
52 # define ulong unsigned long
55 # define uint unsigned int
58 /* defaults if not in config file: */
59 #ifndef HAVE_EEPROM_PAGED_ACCESS
60 # define HAVE_EEPROM_PAGED_ACCESS 0
62 #ifndef HAVE_EEPROM_BYTE_ACCESS
63 # define HAVE_EEPROM_BYTE_ACCESS 0
65 #ifndef BOOTLOADER_CAN_EXIT
66 # define BOOTLOADER_CAN_EXIT 0
69 /* allow compatibility with avrusbboot's bootloaderconfig.h: */
70 #ifdef BOOTLOADER_INIT
71 # define bootLoaderInit() BOOTLOADER_INIT
72 # define bootLoaderExit()
74 #ifdef BOOTLOADER_CONDITION
75 # define bootLoaderCondition() BOOTLOADER_CONDITION
78 /* device compatibility: */
79 #ifndef GICR /* ATMega*8 don't have GICR, use MCUCR instead */
83 /* ------------------------------------------------------------------------ */
85 #if (FLASHEND) > 0xffff /* we need long addressing */
86 # define CURRENT_ADDRESS currentAddress.l
89 # define CURRENT_ADDRESS currentAddress.w[0]
93 typedef union longConverter
{
95 uint w
[sizeof(addr_t
)/2];
96 uchar b
[sizeof(addr_t
)];
100 #if BOOTLOADER_CAN_EXIT
101 static uchar requestBootLoaderExit
;
103 static volatile unsigned char stayinloader
= 0xfe;
105 static longConverter_t currentAddress
; /* in bytes */
106 static uchar bytesRemaining
;
107 static uchar isLastPage
;
108 #if HAVE_EEPROM_PAGED_ACCESS
109 static uchar currentRequest
;
111 static const uchar currentRequest
= 0;
114 static const uchar signatureBytes
[4] = {
115 #ifdef SIGNATURE_BYTES
117 #elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8HVA__)
119 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48P__)
121 #elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__)
123 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
125 #elif defined (__AVR_ATmega328P__)
127 #elif defined (__AVR_ATmega1284P__)
130 # error "Device signature is not known, please edit main.c!"
134 /* ------------------------------------------------------------------------ */
136 static void (*nullVector
)(void) __attribute__((__noreturn__
));
138 static void leaveBootloader()
142 usbDeviceDisconnect();
145 USB_INTR_CFG
= 0; /* also reset config bits */
146 GICR
= (1 << IVCE
); /* enable change of interrupt vectors */
147 GICR
= (0 << IVSEL
); /* move interrupts to application flash section */
149 /* We must go through a global function pointer variable instead of writing
150 * ((void (*)(void))0)();
151 * because the compiler optimizes a constant 0 to "rcall 0" which is not
152 * handled correctly by the assembler.
157 /* ------------------------------------------------------------------------ */
159 uchar
usbFunctionSetup(uchar data
[8])
161 usbRequest_t
*rq
= (void *)data
;
163 static uchar replyBuffer
[4];
165 usbMsgPtr
= replyBuffer
;
166 if(rq
->bRequest
== USBASP_FUNC_TRANSMIT
){ /* emulate parts of ISP protocol */
169 address
.bytes
[1] = rq
->wValue
.bytes
[1];
170 address
.bytes
[0] = rq
->wIndex
.bytes
[0];
171 if(rq
->wValue
.bytes
[0] == 0x30){ /* read signature */
172 rval
= rq
->wIndex
.bytes
[0] & 3;
173 rval
= signatureBytes
[rval
];
174 #if HAVE_READ_LOCK_FUSE
175 #if defined (__AVR_ATmega8__)
176 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x00){ /* read lock bits */
177 rval
= boot_lock_fuse_bits_get(GET_LOCK_BITS
);
178 }else if(rq
->wValue
.bytes
[0] == 0x50 && rq
->wValue
.bytes
[1] == 0x00){ /* read lfuse bits */
179 rval
= boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS
);
180 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x08){ /* read hfuse bits */
181 rval
= boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS
);
184 #if HAVE_EEPROM_BYTE_ACCESS
185 }else if(rq
->wValue
.bytes
[0] == 0xa0){ /* read EEPROM byte */
186 rval
= eeprom_read_byte((void *)address
.word
);
187 }else if(rq
->wValue
.bytes
[0] == 0xc0){ /* write EEPROM byte */
188 eeprom_write_byte((void *)address
.word
, rq
->wIndex
.bytes
[1]);
191 }else if(rq
->wValue
.bytes
[0] == 0xac && rq
->wValue
.bytes
[1] == 0x80){ /* chip erase */
193 for(addr
= 0; addr
< FLASHEND
+ 1 - 2048; addr
+= SPM_PAGESIZE
) {
194 /* wait and erase page */
196 # ifndef NO_FLASH_WRITE
197 boot_spm_busy_wait();
199 boot_page_erase(addr
);
205 /* ignore all others, return default value == 0 */
207 replyBuffer
[3] = rval
;
209 }else if(rq
->bRequest
== USBASP_FUNC_ENABLEPROG
){
210 /* replyBuffer[0] = 0; is never touched and thus always 0 which means success */
212 }else if(rq
->bRequest
>= USBASP_FUNC_READFLASH
&& rq
->bRequest
<= USBASP_FUNC_SETLONGADDRESS
){
213 currentAddress
.w
[0] = rq
->wValue
.word
;
214 if(rq
->bRequest
== USBASP_FUNC_SETLONGADDRESS
){
215 #if (FLASHEND) > 0xffff
216 currentAddress
.w
[1] = rq
->wIndex
.word
;
219 bytesRemaining
= rq
->wLength
.bytes
[0];
220 /* if(rq->bRequest == USBASP_FUNC_WRITEFLASH) only evaluated during writeFlash anyway */
221 isLastPage
= rq
->wIndex
.bytes
[1] & 0x02;
222 #if HAVE_EEPROM_PAGED_ACCESS
223 currentRequest
= rq
->bRequest
;
225 len
= 0xff; /* hand over to usbFunctionRead() / usbFunctionWrite() */
228 }else if(rq
->bRequest
== USBASP_FUNC_DISCONNECT
){
229 stayinloader
&= (0xfe);
230 #if BOOTLOADER_CAN_EXIT
231 requestBootLoaderExit
= 1; /* allow proper shutdown/close of connection */
234 /* ignore: others, but could be USBASP_FUNC_CONNECT */
235 stayinloader
|= (0x01);
240 uchar
usbFunctionWrite(uchar
*data
, uchar len
)
244 DBG1(0x31, (void *)¤tAddress
.l
, 4);
245 if(len
> bytesRemaining
)
246 len
= bytesRemaining
;
247 bytesRemaining
-= len
;
248 isLast
= bytesRemaining
== 0;
249 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
251 for(i
= 0; i
< len
; i
++){
252 eeprom_write_byte((void *)(currentAddress
.w
[0]++), *data
++);
256 for(i
= 0; i
< len
;){
257 #if HAVE_BLB11_SOFTW_LOCKBIT
258 if (CURRENT_ADDRESS
>= (addr_t
)(BOOTLOADER_ADDRESS
)) {
265 boot_page_fill(CURRENT_ADDRESS
, *(short *)data
);
267 CURRENT_ADDRESS
+= 2;
269 /* write page when we cross page boundary or we have the last partial page */
270 if((currentAddress
.w
[0] & (SPM_PAGESIZE
- 1)) == 0 || (isLast
&& i
>= len
&& isLastPage
)){
273 # ifndef NO_FLASH_WRITE
275 boot_page_erase(CURRENT_ADDRESS
- 2); /* erase page */
277 boot_spm_busy_wait(); /* wait until page is erased */
281 #ifndef NO_FLASH_WRITE
283 boot_page_write(CURRENT_ADDRESS
- 2);
285 boot_spm_busy_wait();
292 DBG1(0x35, (void *)¤tAddress
.l
, 4);
297 uchar
usbFunctionRead(uchar
*data
, uchar len
)
301 if(len
> bytesRemaining
)
302 len
= bytesRemaining
;
303 bytesRemaining
-= len
;
304 for(i
= 0; i
< len
; i
++){
305 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
306 *data
= eeprom_read_byte((void *)currentAddress
.w
[0]);
308 *data
= pgm_read_byte((void *)CURRENT_ADDRESS
);
316 /* ------------------------------------------------------------------------ */
318 static void initForUsbConnectivity(void)
323 /* enforce USB re-enumerate: */
324 usbDeviceDisconnect(); /* do this while interrupts are disabled */
325 while(--i
){ /* fake USB disconnect for > 250 ms */
332 int __attribute__((noreturn
)) main(void)
338 #ifndef NO_FLASH_WRITE
339 GICR
= (1 << IVCE
); /* enable change of interrupt vectors */
340 GICR
= (1 << IVSEL
); /* move interrupts to boot flash section */
342 if(bootLoaderCondition()){
343 wdt_disable(); /* main app may have enabled watchdog */
344 #if BOOTLOADER_CAN_EXIT
347 initForUsbConnectivity();
350 #if BOOTLOADER_CAN_EXIT
351 if(requestBootLoaderExit
){
358 if (stayinloader
>= 0x10) {
359 if (!bootLoaderCondition()) {
363 if (bootLoaderCondition()) {
364 if (stayinloader
> 1) stayinloader
-=2;
368 }while (stayinloader
); /* main event loop */
373 /* ------------------------------------------------------------------------ */