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_DOSPM_TUNNELCMD
101 ;input: spmcrval determines SPM action
102 ;disable interrupts if enabled, store status
103 ;temp1 will be register: r7
104 ;temp2 will be register: r8
105 ;spmcrval will be register: r9
107 in temp2, SREG ; --> has to be done before calling
108 cli ; --> has to be done before calling
109 ;check for previous SPM complete
117 ;restore SREG (to enable interrupts if originally enabled)
121 #if defined (__AVR_ATmega8__)
122 const uint16_t bootloader__do_spm
[15] PROGMEM
= {0xb68f , 0x94f8, 0xb677, 0xfc70, 0xcffd, 0xbe97, 0x95e8, 0xbe8f, 0x9508, 0x00, 0xFFFF, 0x95e8, 0x9508, 0x0000, 0xFFFF};
124 #error "bootloader__do_spm has to be adapted, since there is no guaranty for SREG==0x3f, SPMCR==0x37, SPMEN==0x00"
129 #if BOOTLOADER_CAN_EXIT
130 static uchar requestBootLoaderExit
;
132 static volatile unsigned char stayinloader
= 0xfe;
134 static longConverter_t currentAddress
; /* in bytes */
135 static uchar bytesRemaining
;
136 static uchar isLastPage
;
137 #if HAVE_EEPROM_PAGED_ACCESS
138 static uchar currentRequest
;
140 static const uchar currentRequest
= 0;
143 static const uchar signatureBytes
[4] = {
144 #ifdef SIGNATURE_BYTES
146 #elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8HVA__)
148 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48P__)
150 #elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__)
152 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
154 #elif defined (__AVR_ATmega328P__)
157 # error "Device signature is not known, please edit main.c!"
161 /* ------------------------------------------------------------------------ */
163 static void (*nullVector
)(void) __attribute__((__noreturn__
));
165 static void leaveBootloader()
169 usbDeviceDisconnect();
172 USB_INTR_CFG
= 0; /* also reset config bits */
173 GICR
= (1 << IVCE
); /* enable change of interrupt vectors */
174 GICR
= (0 << IVSEL
); /* move interrupts to application flash section */
175 /* We must go through a global function pointer variable instead of writing
176 * ((void (*)(void))0)();
177 * because the compiler optimizes a constant 0 to "rcall 0" which is not
178 * handled correctly by the assembler.
183 /* ------------------------------------------------------------------------ */
185 uchar
usbFunctionSetup(uchar data
[8])
187 usbRequest_t
*rq
= (void *)data
;
189 static uchar replyBuffer
[4];
191 usbMsgPtr
= replyBuffer
;
192 if(rq
->bRequest
== USBASP_FUNC_TRANSMIT
){ /* emulate parts of ISP protocol */
195 address
.bytes
[1] = rq
->wValue
.bytes
[1];
196 address
.bytes
[0] = rq
->wIndex
.bytes
[0];
197 if(rq
->wValue
.bytes
[0] == 0x30){ /* read signature */
198 rval
= rq
->wIndex
.bytes
[0] & 3;
199 rval
= signatureBytes
[rval
];
200 #if HAVE_READ_LOCK_FUSE
201 #if defined (__AVR_ATmega8__)
202 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x00){ /* read lock bits */
203 rval
= boot_lock_fuse_bits_get(GET_LOCK_BITS
);
204 }else if(rq
->wValue
.bytes
[0] == 0x50 && rq
->wValue
.bytes
[1] == 0x00){ /* read lfuse bits */
205 rval
= boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS
);
206 }else if(rq
->wValue
.bytes
[0] == 0x58 && rq
->wValue
.bytes
[1] == 0x08){ /* read hfuse bits */
207 rval
= boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS
);
210 #if HAVE_EEPROM_BYTE_ACCESS
211 }else if(rq
->wValue
.bytes
[0] == 0xa0){ /* read EEPROM byte */
212 rval
= eeprom_read_byte((void *)address
.word
);
213 }else if(rq
->wValue
.bytes
[0] == 0xc0){ /* write EEPROM byte */
214 eeprom_write_byte((void *)address
.word
, rq
->wIndex
.bytes
[1]);
217 }else if(rq
->wValue
.bytes
[0] == 0xac && rq
->wValue
.bytes
[1] == 0x80){ /* chip erase */
219 for(addr
= 0; addr
< FLASHEND
+ 1 - 2048; addr
+= SPM_PAGESIZE
) {
220 /* wait and erase page */
222 # ifndef NO_FLASH_WRITE
223 boot_spm_busy_wait();
225 boot_page_erase(addr
);
231 /* ignore all others, return default value == 0 */
233 replyBuffer
[3] = rval
;
235 }else if(rq
->bRequest
== USBASP_FUNC_ENABLEPROG
){
236 /* replyBuffer[0] = 0; is never touched and thus always 0 which means success */
238 }else if(rq
->bRequest
>= USBASP_FUNC_READFLASH
&& rq
->bRequest
<= USBASP_FUNC_SETLONGADDRESS
){
239 currentAddress
.w
[0] = rq
->wValue
.word
;
240 if(rq
->bRequest
== USBASP_FUNC_SETLONGADDRESS
){
241 #if (FLASHEND) > 0xffff
242 currentAddress
.w
[1] = rq
->wIndex
.word
;
245 bytesRemaining
= rq
->wLength
.bytes
[0];
246 /* if(rq->bRequest == USBASP_FUNC_WRITEFLASH) only evaluated during writeFlash anyway */
247 isLastPage
= rq
->wIndex
.bytes
[1] & 0x02;
248 #if HAVE_EEPROM_PAGED_ACCESS
249 currentRequest
= rq
->bRequest
;
251 len
= 0xff; /* hand over to usbFunctionRead() / usbFunctionWrite() */
254 }else if(rq
->bRequest
== USBASP_FUNC_DISCONNECT
){
255 stayinloader
&= (0xfe);
256 #if BOOTLOADER_CAN_EXIT
257 requestBootLoaderExit
= 1; /* allow proper shutdown/close of connection */
260 /* ignore: others, but could be USBASP_FUNC_CONNECT */
261 stayinloader
|= (0x01);
266 uchar
usbFunctionWrite(uchar
*data
, uchar len
)
270 DBG1(0x31, (void *)¤tAddress
.l
, 4);
271 if(len
> bytesRemaining
)
272 len
= bytesRemaining
;
273 bytesRemaining
-= len
;
274 isLast
= bytesRemaining
== 0;
275 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
277 for(i
= 0; i
< len
; i
++){
278 eeprom_write_byte((void *)(currentAddress
.w
[0]++), *data
++);
282 for(i
= 0; i
< len
;){
283 #if HAVE_BLB11_SOFTW_LOCKBIT
284 if (CURRENT_ADDRESS
>= (addr_t
)(BOOTLOADER_ADDRESS
)) {
285 #if HAVE_BLB11_SOFTW_BACKDOOR
286 if (!((stayinloader
>= 0x10) && (bootLoaderCondition()))) return 1;
295 boot_page_fill(CURRENT_ADDRESS
, *(short *)data
);
297 CURRENT_ADDRESS
+= 2;
299 /* write page when we cross page boundary or we have the last partial page */
300 if((currentAddress
.w
[0] & (SPM_PAGESIZE
- 1)) == 0 || (isLast
&& i
>= len
&& isLastPage
)){
303 # ifndef NO_FLASH_WRITE
305 boot_page_erase(CURRENT_ADDRESS
- 2); /* erase page */
307 boot_spm_busy_wait(); /* wait until page is erased */
311 #ifndef NO_FLASH_WRITE
313 boot_page_write(CURRENT_ADDRESS
- 2);
315 boot_spm_busy_wait();
322 DBG1(0x35, (void *)¤tAddress
.l
, 4);
327 uchar
usbFunctionRead(uchar
*data
, uchar len
)
331 if(len
> bytesRemaining
)
332 len
= bytesRemaining
;
333 bytesRemaining
-= len
;
334 for(i
= 0; i
< len
; i
++){
335 if(currentRequest
>= USBASP_FUNC_READEEPROM
){
336 *data
= eeprom_read_byte((void *)currentAddress
.w
[0]);
338 *data
= pgm_read_byte((void *)CURRENT_ADDRESS
);
346 /* ------------------------------------------------------------------------ */
348 static void initForUsbConnectivity(void)
353 /* enforce USB re-enumerate: */
354 usbDeviceDisconnect(); /* do this while interrupts are disabled */
355 while(--i
){ /* fake USB disconnect for > 250 ms */
362 int __attribute__((noreturn
)) main(void)
365 wdt_disable(); /* main app may have enabled watchdog */
369 #ifndef NO_FLASH_WRITE
370 GICR
= (1 << IVCE
); /* enable change of interrupt vectors */
371 GICR
= (1 << IVSEL
); /* move interrupts to boot flash section */
373 if(bootLoaderCondition()){
374 #if BOOTLOADER_CAN_EXIT
377 initForUsbConnectivity();
380 #if BOOTLOADER_CAN_EXIT
381 if(requestBootLoaderExit
){
388 if (stayinloader
>= 0x10) {
389 if (!bootLoaderCondition()) {
393 if (bootLoaderCondition()) {
394 if (stayinloader
> 1) stayinloader
-=2;
398 }while (stayinloader
); /* main event loop */
403 /* ------------------------------------------------------------------------ */