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
)];
98 #if HAVE_STMOPCODE_TUNNELCMD
99 #if HAVE_BLB11_SOFTW_BACKDOOR
100 const uint16_t bootloader__do_spm
[12] PROGMEM
= {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
104 ;input: spmcrval determines SPM action
105 ;disable interrupts if enabled, store status
106 ;temp1 will be register: r7
107 ;temp2 will be register: r8
108 ;spmcrval will be register: r9
110 in temp2, SREG ; --> has to be done before calling
111 cli ; --> has to be done before calling
112 ;check for previous SPM complete
120 ;restore SREG (to enable interrupts if originally enabled)
124 #if defined (__AVR_ATmega8__)
125 const uint16_t bootloader__do_spm
[12] PROGMEM
= {0xb68f , 0x94f8, 0xb677, 0xfc70, 0xcffd, 0xbe97, 0x95e8, 0xbe8f, 0x9508, 0x00, 0xFFFF, 0xFFFF};#else
126 #error "bootloader__do_spm has to be adapted, since there is no guaranty for SREG==0x3f, SPMCR==0x37, SPMEN==0x00"
132 #if BOOTLOADER_CAN_EXIT
133 static uchar requestBootLoaderExit
;
135 static volatile unsigned char stayinloader
= 0xfe;
137 static longConverter_t currentAddress
; /* in bytes */
138 static uchar bytesRemaining
;
139 static uchar isLastPage
;
140 #if HAVE_EEPROM_PAGED_ACCESS
141 static uchar currentRequest
;
143 static const uchar currentRequest
= 0;
146 static const uchar signatureBytes
[4] = {
147 #ifdef SIGNATURE_BYTES
149 #elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8HVA__)
151 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48P__)
153 #elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__)
155 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
157 #elif defined (__AVR_ATmega328P__)
160 # error "Device signature is not known, please edit main.c!"
164 /* ------------------------------------------------------------------------ */
166 static void (*nullVector
)(void) __attribute__((__noreturn__
));
168 static void leaveBootloader()
172 usbDeviceDisconnect();
175 USB_INTR_CFG
= 0; /* also reset config bits */
176 GICR
= (1 << IVCE
); /* enable change of interrupt vectors */
177 GICR
= (0 << IVSEL
); /* move interrupts to application flash section */
178 /* We must go through a global function pointer variable instead of writing
179 * ((void (*)(void))0)();
180 * because the compiler optimizes a constant 0 to "rcall 0" which is not
181 * handled correctly by the assembler.
186 /* ------------------------------------------------------------------------ */
188 uchar
usbFunctionSetup(uchar data
[8])
190 usbRequest_t
*rq
= (void *)data
;
192 static uchar replyBuffer
[4];
194 usbMsgPtr
= replyBuffer
;
195 if(rq
->bRequest
== USBASP_FUNC_TRANSMIT
){ /* emulate parts of ISP protocol */
198 address
.bytes
[1] = rq
->wValue
.bytes
[1];
199 address
.bytes
[0] = rq
->wIndex
.bytes
[0];
200 if(rq
->wValue
.bytes
[0] == 0x30){ /* read signature */
201 rval
= rq
->wIndex
.bytes
[0] & 3;
202 rval
= signatureBytes
[rval
];
203 #if HAVE_READ_LOCK_FUSE
204 #if defined (__AVR_ATmega8__)
205 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x00){ /* read lock bits */
206 rval
= boot_lock_fuse_bits_get(GET_LOCK_BITS
);
207 }else if(rq
->wValue
.bytes
[0] == 0x50 && rq
->wValue
.bytes
[1] == 0x00){ /* read lfuse bits */
208 rval
= boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS
);
209 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x08){ /* read hfuse bits */
210 rval
= boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS
);
213 #if HAVE_EEPROM_BYTE_ACCESS
214 }else if(rq
->wValue
.bytes
[0] == 0xa0){ /* read EEPROM byte */
215 rval
= eeprom_read_byte((void *)address
.word
);
216 }else if(rq
->wValue
.bytes
[0] == 0xc0){ /* write EEPROM byte */
217 eeprom_write_byte((void *)address
.word
, rq
->wIndex
.bytes
[1]);
220 }else if(rq
->wValue
.bytes
[0] == 0xac && rq
->wValue
.bytes
[1] == 0x80){ /* chip erase */
222 for(addr
= 0; addr
< FLASHEND
+ 1 - 2048; addr
+= SPM_PAGESIZE
) {
223 /* wait and erase page */
225 # ifndef NO_FLASH_WRITE
226 boot_spm_busy_wait();
228 boot_page_erase(addr
);
234 /* ignore all others, return default value == 0 */
236 replyBuffer
[3] = rval
;
238 }else if(rq
->bRequest
== USBASP_FUNC_ENABLEPROG
){
239 /* replyBuffer[0] = 0; is never touched and thus always 0 which means success */
241 }else if(rq
->bRequest
>= USBASP_FUNC_READFLASH
&& rq
->bRequest
<= USBASP_FUNC_SETLONGADDRESS
){
242 currentAddress
.w
[0] = rq
->wValue
.word
;
243 if(rq
->bRequest
== USBASP_FUNC_SETLONGADDRESS
){
244 #if (FLASHEND) > 0xffff
245 currentAddress
.w
[1] = rq
->wIndex
.word
;
248 bytesRemaining
= rq
->wLength
.bytes
[0];
249 /* if(rq->bRequest == USBASP_FUNC_WRITEFLASH) only evaluated during writeFlash anyway */
250 isLastPage
= rq
->wIndex
.bytes
[1] & 0x02;
251 #if HAVE_EEPROM_PAGED_ACCESS
252 currentRequest
= rq
->bRequest
;
254 len
= 0xff; /* hand over to usbFunctionRead() / usbFunctionWrite() */
257 }else if(rq
->bRequest
== USBASP_FUNC_DISCONNECT
){
258 stayinloader
&= (0xfe);
259 #if BOOTLOADER_CAN_EXIT
260 requestBootLoaderExit
= 1; /* allow proper shutdown/close of connection */
263 /* ignore: others, but could be USBASP_FUNC_CONNECT */
264 stayinloader
|= (0x01);
269 uchar
usbFunctionWrite(uchar
*data
, uchar len
)
273 DBG1(0x31, (void *)¤tAddress
.l
, 4);
274 if(len
> bytesRemaining
)
275 len
= bytesRemaining
;
276 bytesRemaining
-= len
;
277 isLast
= bytesRemaining
== 0;
278 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
280 for(i
= 0; i
< len
; i
++){
281 eeprom_write_byte((void *)(currentAddress
.w
[0]++), *data
++);
285 for(i
= 0; i
< len
;){
286 #if HAVE_BLB11_SOFTW_LOCKBIT
287 if (CURRENT_ADDRESS
>= (addr_t
)(BOOTLOADER_ADDRESS
)) {
288 #if HAVE_BLB11_SOFTW_BACKDOOR
289 if (!((stayinloader
>= 0x10) && (bootLoaderCondition()))) return 1;
296 if((currentAddress
.w
[0] & (SPM_PAGESIZE
- 1)) == 0){ /* if page start: erase */
298 # ifndef NO_FLASH_WRITE
300 boot_page_erase(CURRENT_ADDRESS
); /* erase page */
302 boot_spm_busy_wait(); /* wait until page is erased */
309 boot_page_fill(CURRENT_ADDRESS
, *(short *)data
);
311 CURRENT_ADDRESS
+= 2;
313 /* write page when we cross page boundary or we have the last partial page */
314 if((currentAddress
.w
[0] & (SPM_PAGESIZE
- 1)) == 0 || (isLast
&& i
>= len
&& isLastPage
)){
316 #ifndef NO_FLASH_WRITE
318 boot_page_write(CURRENT_ADDRESS
- 2);
320 boot_spm_busy_wait();
327 DBG1(0x35, (void *)¤tAddress
.l
, 4);
332 uchar
usbFunctionRead(uchar
*data
, uchar len
)
336 if(len
> bytesRemaining
)
337 len
= bytesRemaining
;
338 bytesRemaining
-= len
;
339 for(i
= 0; i
< len
; i
++){
340 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
341 *data
= eeprom_read_byte((void *)currentAddress
.w
[0]);
343 *data
= pgm_read_byte((void *)CURRENT_ADDRESS
);
351 /* ------------------------------------------------------------------------ */
353 static void initForUsbConnectivity(void)
358 /* enforce USB re-enumerate: */
359 usbDeviceDisconnect(); /* do this while interrupts are disabled */
360 while(--i
){ /* fake USB disconnect for > 250 ms */
367 int __attribute__((noreturn
)) main(void)
370 wdt_disable(); /* main app may have enabled watchdog */
374 #ifndef NO_FLASH_WRITE
375 GICR
= (1 << IVCE
); /* enable change of interrupt vectors */
376 GICR
= (1 << IVSEL
); /* move interrupts to boot flash section */
378 if(bootLoaderCondition()){
379 #if BOOTLOADER_CAN_EXIT
382 initForUsbConnectivity();
385 #if BOOTLOADER_CAN_EXIT
386 if(requestBootLoaderExit
){
393 if (stayinloader
>= 0x10) {
394 if (!bootLoaderCondition()) {
398 if (bootLoaderCondition()) {
399 if (stayinloader
> 1) stayinloader
-=2;
403 }while (stayinloader
); /* main event loop */
408 /* ------------------------------------------------------------------------ */