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 $
12 #include <avr/interrupt.h>
13 #include <avr/pgmspace.h>
16 #include <avr/eeprom.h>
17 #include <util/delay.h>
19 #include <avr/cpufunc.h>
25 static void leaveBootloader() __attribute__((__noreturn__
));
27 #include "bootloaderconfig.h"
28 #include "usbdrv/usbdrv.c"
30 #ifndef BOOTLOADER_ADDRESS
31 #error need to know the bootloaders flash address!
34 /* ------------------------------------------------------------------------ */
36 /* Request constants used by USBasp */
37 #define USBASP_FUNC_CONNECT 1
38 #define USBASP_FUNC_DISCONNECT 2
39 #define USBASP_FUNC_TRANSMIT 3
40 #define USBASP_FUNC_READFLASH 4
41 #define USBASP_FUNC_ENABLEPROG 5
42 #define USBASP_FUNC_WRITEFLASH 6
43 #define USBASP_FUNC_READEEPROM 7
44 #define USBASP_FUNC_WRITEEEPROM 8
45 #define USBASP_FUNC_SETLONGADDRESS 9
47 /* ------------------------------------------------------------------------ */
50 # define ulong unsigned long
53 # define uint unsigned int
56 /* defaults if not in config file: */
57 #ifndef HAVE_EEPROM_PAGED_ACCESS
58 # define HAVE_EEPROM_PAGED_ACCESS 0
60 #ifndef HAVE_EEPROM_BYTE_ACCESS
61 # define HAVE_EEPROM_BYTE_ACCESS 0
63 #ifndef BOOTLOADER_CAN_EXIT
64 # define BOOTLOADER_CAN_EXIT 0
67 /* allow compatibility with avrusbboot's bootloaderconfig.h: */
68 #ifdef BOOTLOADER_INIT
69 # define bootLoaderInit() BOOTLOADER_INIT
70 # define bootLoaderExit()
72 #ifdef BOOTLOADER_CONDITION
73 # define bootLoaderCondition() BOOTLOADER_CONDITION
76 /* device compatibility: */
77 #ifndef GICR /* ATMega*8 don't have GICR, use MCUCR instead */
81 /* ------------------------------------------------------------------------ */
83 #if (FLASHEND) > 0xffff /* we need long addressing */
84 # define CURRENT_ADDRESS currentAddress.l
87 # define CURRENT_ADDRESS currentAddress.w[0]
91 typedef union longConverter
{
93 uint w
[sizeof(addr_t
)/2];
94 uchar b
[sizeof(addr_t
)];
99 #if BOOTLOADER_CAN_EXIT
100 static uchar requestBootLoaderExit
;
102 static volatile unsigned char stayinloader
= 0xfe;
104 static longConverter_t currentAddress
; /* in bytes */
105 static uchar bytesRemaining
;
106 static uchar isLastPage
;
107 #if HAVE_EEPROM_PAGED_ACCESS
108 static uchar currentRequest
;
110 static const uchar currentRequest
= 0;
113 static const uchar signatureBytes
[4] = {
114 #ifdef SIGNATURE_BYTES
116 #elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8HVA__)
118 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48P__)
120 #elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__)
122 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
124 #elif defined (__AVR_ATmega328P__)
127 # error "Device signature is not known, please edit main.c!"
131 /* ------------------------------------------------------------------------ */
133 static void (*nullVector
)(void) __attribute__((__noreturn__
));
135 static void leaveBootloader()
139 usbDeviceDisconnect();
142 USB_INTR_CFG
= 0; /* also reset config bits */
143 GICR
= (1 << IVCE
); /* enable change of interrupt vectors */
144 GICR
= (0 << IVSEL
); /* move interrupts to application flash section */
145 /* We must go through a global function pointer variable instead of writing
146 * ((void (*)(void))0)();
147 * because the compiler optimizes a constant 0 to "rcall 0" which is not
148 * handled correctly by the assembler.
153 /* ------------------------------------------------------------------------ */
155 uchar
usbFunctionSetup(uchar data
[8])
157 usbRequest_t
*rq
= (void *)data
;
159 static uchar replyBuffer
[4];
161 usbMsgPtr
= replyBuffer
;
162 if(rq
->bRequest
== USBASP_FUNC_TRANSMIT
){ /* emulate parts of ISP protocol */
165 address
.bytes
[1] = rq
->wValue
.bytes
[1];
166 address
.bytes
[0] = rq
->wIndex
.bytes
[0];
167 if(rq
->wValue
.bytes
[0] == 0x30){ /* read signature */
168 rval
= rq
->wIndex
.bytes
[0] & 3;
169 rval
= signatureBytes
[rval
];
170 #if HAVE_READ_LOCK_FUSE
171 #if defined (__AVR_ATmega8__)
172 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x00){ /* read lock bits */
173 rval
= boot_lock_fuse_bits_get(GET_LOCK_BITS
);
174 }else if(rq
->wValue
.bytes
[0] == 0x50 && rq
->wValue
.bytes
[1] == 0x00){ /* read lfuse bits */
175 rval
= boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS
);
176 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x08){ /* read hfuse bits */
177 rval
= boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS
);
180 #if HAVE_EEPROM_BYTE_ACCESS
181 }else if(rq
->wValue
.bytes
[0] == 0xa0){ /* read EEPROM byte */
182 rval
= eeprom_read_byte((void *)address
.word
);
183 }else if(rq
->wValue
.bytes
[0] == 0xc0){ /* write EEPROM byte */
184 eeprom_write_byte((void *)address
.word
, rq
->wIndex
.bytes
[1]);
187 }else if(rq
->wValue
.bytes
[0] == 0xac && rq
->wValue
.bytes
[1] == 0x80){ /* chip erase */
189 for(addr
= 0; addr
< FLASHEND
+ 1 - 2048; addr
+= SPM_PAGESIZE
) {
190 /* wait and erase page */
192 # ifndef NO_FLASH_WRITE
193 boot_spm_busy_wait();
195 boot_page_erase(addr
);
201 /* ignore all others, return default value == 0 */
203 replyBuffer
[3] = rval
;
205 }else if(rq
->bRequest
== USBASP_FUNC_ENABLEPROG
){
206 /* replyBuffer[0] = 0; is never touched and thus always 0 which means success */
208 }else if(rq
->bRequest
>= USBASP_FUNC_READFLASH
&& rq
->bRequest
<= USBASP_FUNC_SETLONGADDRESS
){
209 currentAddress
.w
[0] = rq
->wValue
.word
;
210 if(rq
->bRequest
== USBASP_FUNC_SETLONGADDRESS
){
211 #if (FLASHEND) > 0xffff
212 currentAddress
.w
[1] = rq
->wIndex
.word
;
215 bytesRemaining
= rq
->wLength
.bytes
[0];
216 /* if(rq->bRequest == USBASP_FUNC_WRITEFLASH) only evaluated during writeFlash anyway */
217 isLastPage
= rq
->wIndex
.bytes
[1] & 0x02;
218 #if HAVE_EEPROM_PAGED_ACCESS
219 currentRequest
= rq
->bRequest
;
221 len
= 0xff; /* hand over to usbFunctionRead() / usbFunctionWrite() */
224 }else if(rq
->bRequest
== USBASP_FUNC_DISCONNECT
){
225 stayinloader
&= (0xfe);
226 #if BOOTLOADER_CAN_EXIT
227 requestBootLoaderExit
= 1; /* allow proper shutdown/close of connection */
229 }else if(rq
->bRequest
== USBASP_FUNC_CONNECT
){
230 stayinloader
|= (0x01);
237 uchar
usbFunctionWrite(uchar
*data
, uchar len
)
241 DBG1(0x31, (void *)¤tAddress
.l
, 4);
242 if(len
> bytesRemaining
)
243 len
= bytesRemaining
;
244 bytesRemaining
-= len
;
245 isLast
= bytesRemaining
== 0;
246 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
248 for(i
= 0; i
< len
; i
++){
249 eeprom_write_byte((void *)(currentAddress
.w
[0]++), *data
++);
253 #if HAVE_BLB11_SOFTW_LOCKBIT
256 for(i
= 0; i
< len
;){
257 #if HAVE_BLB11_SOFTW_LOCKBIT
258 allowWrite
= (CURRENT_ADDRESS
< (addr_t
)(BOOTLOADER_ADDRESS
));
261 if((currentAddress
.w
[0] & (SPM_PAGESIZE
- 1)) == 0){ /* if page start: erase */
263 # ifndef NO_FLASH_WRITE
264 # if HAVE_BLB11_SOFTW_LOCKBIT
268 boot_page_erase(CURRENT_ADDRESS
); /* erase page */
270 boot_spm_busy_wait(); /* wait until page is erased */
271 # if HAVE_BLB11_SOFTW_LOCKBIT
279 # if HAVE_BLB11_SOFTW_LOCKBIT
283 boot_page_fill(CURRENT_ADDRESS
, *(short *)data
);
285 # if HAVE_BLB11_SOFTW_LOCKBIT
288 CURRENT_ADDRESS
+= 2;
290 /* write page when we cross page boundary or we have the last partial page */
291 if((currentAddress
.w
[0] & (SPM_PAGESIZE
- 1)) == 0 || (isLast
&& i
>= len
&& isLastPage
)){
293 #ifndef NO_FLASH_WRITE
294 # if HAVE_BLB11_SOFTW_LOCKBIT
298 boot_page_write(CURRENT_ADDRESS
- 2);
300 boot_spm_busy_wait();
304 # if HAVE_BLB11_SOFTW_LOCKBIT
310 DBG1(0x35, (void *)¤tAddress
.l
, 4);
315 uchar
usbFunctionRead(uchar
*data
, uchar len
)
319 if(len
> bytesRemaining
)
320 len
= bytesRemaining
;
321 bytesRemaining
-= len
;
322 for(i
= 0; i
< len
; i
++){
323 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
324 *data
= eeprom_read_byte((void *)currentAddress
.w
[0]);
326 *data
= pgm_read_byte((void *)CURRENT_ADDRESS
);
334 /* ------------------------------------------------------------------------ */
336 static void initForUsbConnectivity(void)
341 /* enforce USB re-enumerate: */
342 usbDeviceDisconnect(); /* do this while interrupts are disabled */
343 while(--i
){ /* fake USB disconnect for > 250 ms */
351 int __attribute__((noreturn
)) main(void)
354 wdt_disable(); /* main app may have enabled watchdog */
358 #ifndef NO_FLASH_WRITE
359 GICR
= (1 << IVCE
); /* enable change of interrupt vectors */
360 GICR
= (1 << IVSEL
); /* move interrupts to boot flash section */
362 if(bootLoaderCondition()){
363 #if BOOTLOADER_CAN_EXIT
366 initForUsbConnectivity();
369 #if BOOTLOADER_CAN_EXIT
370 if(requestBootLoaderExit
){
377 if (stayinloader
> 0x04) {
378 if (!bootLoaderCondition()) {
382 if (bootLoaderCondition()) {
383 stayinloader
&= 0x01;
387 }while (stayinloader
); /* main event loop */
392 /* ------------------------------------------------------------------------ */