-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.c
423 lines (354 loc) · 9.59 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Compact USBASP-compatible bootloader for AVR
// License: GNU GPL v2 (see License.txt)
// Copyright (c) 2007 Christian Starkjohann
// Copyright (c) 2007 OBJECTIVE DEVELOPMENT Software GmbH
// Copyright (c) 2012 Stephan Baerwolf
// Copyright (c) 2013 Shay Green
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/boot.h>
#include <avr/eeprom.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#ifndef MCUCSR
#define MCUCSR MCUSR
#endif
static void leaveBootloader( void ) __attribute__((noreturn));
#include "usbconfig.h" // includes "bootloaderconfig.h" indirectly
#include "postconfig.h"
#include "usbdrv/usbdrv.h"
#include "usbdrv/oddebug.h"
#define USBASP_FUNC_CONNECT 1
#define USBASP_FUNC_DISCONNECT 2
#define USBASP_FUNC_TRANSMIT 3
#define USBASP_FUNC_READFLASH 4
#define USBASP_FUNC_ENABLEPROG 5
#define USBASP_FUNC_WRITEFLASH 6
#define USBASP_FUNC_READEEPROM 7
#define USBASP_FUNC_WRITEEEPROM 8
#define USBASP_FUNC_SETLONGADDRESS 9
#define USBASP_FUNC_SETISPSCK 10
#define CLI_SEI( expr ) do { cli(); (expr); sei(); } while ( 0 )
#if FLASHEND > 0xFFFF // >64KB flash
typedef uint32_t addr_t;
#define PGM_READ_BYTE pgm_read_byte_far
#else
typedef uint16_t addr_t;
#define PGM_READ_BYTE pgm_read_byte
#endif
union currentAddress_t {
addr_t a; // full address
uint16_t w [sizeof (addr_t) / 2]; // lower/upper words
};
#if USE_GLOBAL_REGS
#define GLOBAL_REG( r, type, name, init ) \
enum { name##_init = init };\
register type name asm(#r)
#else
#define GLOBAL_REG( r, type, name, init ) static type name = init
#endif
// Effective number of clocks per iteration of main loop. Empirically timed.
// Accounts for USB interrupts. Oddly same regardless of GLOBAL_REGS.
enum { main_loop_clk = 28 };
static union currentAddress_t currentAddress; // in bytes
GLOBAL_REG( r3, uchar, bytesRemaining, 0 );
GLOBAL_REG( r4, uchar, isLastPage, 0 ); // needs to be masked with 0x02
#if AUTO_EXIT_NO_USB_MS
GLOBAL_REG( r5, uchar, currentRequest, USBASP_FUNC_DISCONNECT );
GLOBAL_REG( r6, uchar, timeoutHigh,
F_CPU/main_loop_clk / 1000 * (AUTO_EXIT_NO_USB_MS) / 0x10000 );
#else
GLOBAL_REG( r5, uchar, currentRequest, 0 );
#endif
static uchar notErased = 1;
// **** Commands
static uchar usbFunctionSetup_USBASP_FUNC_TRANSMIT( const usbRequest_t* rq )
{
usbWord_t u;
u.bytes [1] = rq->wValue.bytes [1]; // big-endian
u.bytes [0] = rq->wIndex.bytes [0];
#define RQ_BYTE (rq->wValue.bytes [0])
#define RQ_BYTE2 (rq->wValue.bytes [1])
if ( RQ_BYTE == 0x30 )
{
static const uchar signatureBytes [4] = { SIGNATURE_BYTES };
uchar i = rq->wIndex.bytes [0] & 3; // optimization: separate calc
return signatureBytes [i];
}
#if !defined (HAVE_READ_LOCK_FUSE) || HAVE_READ_LOCK_FUSE
else if ( RQ_BYTE == 0x50 || RQ_BYTE == 0x58 )
{
uchar n = 0;
if ( RQ_BYTE & 0x08 )
n |= 1;
if ( RQ_BYTE2 & 0x08 )
n |= 2;
return boot_lock_fuse_bits_get( n ); // shows up as LPM in disassembly
}
#endif
#if !defined (HAVE_FLASH_BYTE_READACCESS) || HAVE_FLASH_BYTE_READACCESS
else if ( RQ_BYTE == 0x20 || RQ_BYTE == 0x28 )
{
addr_t a = u.word;
a <<= 1;
if ( RQ_BYTE & 0x08 )
a |= 1;
return PGM_READ_BYTE( a );
}
#endif
#if !defined (HAVE_EEPROM_BYTE_ACCESS) || HAVE_EEPROM_BYTE_ACCESS
else if ( RQ_BYTE == 0xA0 )
{
return eeprom_read_byte( (void*) u.word );
}
else if ( RQ_BYTE == 0xC0 )
{
eeprom_write_byte( (void*) u.word, rq->wIndex.bytes [1] );
}
#endif
else if ( RQ_BYTE == 0xAC && RQ_BYTE2 == 0x80 )
{
#if !HAVE_CHIP_ERASE
notErased = 0;
#else
addr_t addr;
for ( addr = 0; addr < (addr_t) BOOTLOADER_ADDRESS; addr += SPM_PAGESIZE )
{
CLI_SEI( boot_page_erase( addr ) );
boot_spm_busy_wait();
}
#endif
}
else // ignore other commands
{
}
return 0;
}
uchar usbFunctionSetup( uchar data [8] )
{
const usbRequest_t* rq = (const usbRequest_t*) data;
#if AUTO_EXIT_NO_USB_MS
timeoutHigh = 2; // 1 could expire immediately
#endif
static uchar replyBuffer [4];
usbMsgPtr = (usbMsgPtr_t) replyBuffer;
currentRequest = rq->bRequest;
if ( rq->bRequest == USBASP_FUNC_TRANSMIT )
{
replyBuffer [3] = usbFunctionSetup_USBASP_FUNC_TRANSMIT( rq );
return 4;
}
else if ( rq->bRequest == USBASP_FUNC_ENABLEPROG ||
rq->bRequest == USBASP_FUNC_SETISPSCK )
{
// avrdude gives error if USBASP_FUNC_ENABLEPROG not handled, and
// warning if USBASP_FUNC_SETISPSCK not handled
//replyBuffer [0] = 0; // optimization: always zero; unnecessary to clear
return 1;
}
else if ( rq->bRequest >= USBASP_FUNC_READFLASH &&
rq->bRequest <= USBASP_FUNC_SETLONGADDRESS )
{
currentAddress.w [0] = rq->wValue.word;
if ( rq->bRequest == USBASP_FUNC_SETLONGADDRESS )
{
#if FLASHEND > 0xFFFF
currentAddress.w[1] = rq->wIndex.word;
#endif
}
else // USBASP_FUNC_(READ/WRITE)FLASH, USBASP_FUNC_(READ/WRITE)EEPROM
{
bytesRemaining = rq->wLength.bytes [0];
isLastPage = rq->wIndex.bytes [1];
return USB_NO_MSG; // causes callbacks to read/write functions below
}
}
else // ignored: USBASP_FUNC_CONNECT, USBASP_FUNC_DISCONNECT
{
}
return 0;
}
// **** Data read/write
uchar usbFunctionWrite( uchar* data, uchar len )
{
if ( len > bytesRemaining )
len = bytesRemaining;
bytesRemaining -= len;
uchar isLast = (bytesRemaining == 0);
for ( len++; len > 1; )
{
#if HAVE_EEPROM_PAGED_ACCESS
if ( currentRequest >= USBASP_FUNC_READEEPROM )
{
eeprom_write_byte( (void*) (currentAddress.w [0]++), *data++ );
len--;
}
else
#endif
if ( currentAddress.a >= (addr_t) BOOTLOADER_ADDRESS )
{
return 1;
}
else
{
CLI_SEI( boot_page_fill( currentAddress.a, *(uint16_t*) data ) );
data += 2;
len -= 2;
currentAddress.a += 2;
// write page after last word has been written for that page, either
// because it was last word of page, or last one host will be writing
if ( (currentAddress.w [0] & (SPM_PAGESIZE - 1)) == 0 ||
(isLast && len <= 1 && isLastPage & 0x02) )
{
#if !HAVE_CHIP_ERASE
if ( !notErased )
{
CLI_SEI( boot_page_erase( currentAddress.a - 2 ) );
boot_spm_busy_wait();
}
#endif
CLI_SEI( boot_page_write( currentAddress.a - 2 ) );
boot_spm_busy_wait();
CLI_SEI( boot_rww_enable() );
}
}
}
return isLast;
}
uchar usbFunctionRead( uchar* data, uchar len )
{
#if HAVE_FLASH_PAGED_READ || HAVE_EEPROM_PAGED_ACCESS
if ( len > bytesRemaining )
len = bytesRemaining;
bytesRemaining -= len;
addr_t a = currentAddress.a; // optimization
uchar n;
for ( n = len; n; n-- )
{
// optimization: read unconditionally, since extra pgm read is harmless
uchar b = PGM_READ_BYTE( a );
#if HAVE_EEPROM_PAGED_ACCESS
#if HAVE_FLASH_PAGED_READ
if ( currentRequest >= USBASP_FUNC_READEEPROM )
#endif
b = eeprom_read_byte( (void*) (uint16_t) a );
#endif
*data++ = b;
a++;
}
currentAddress.a = a;
return len;
#else
return 0;
#endif
}
// **** Self-update
#if !defined (HAVE_SELF_UPDATE) || HAVE_SELF_UPDATE
#include "do_spm.h"
#ifdef SPM_RDY_vect
ISR(SPM_RDY_vect,ISR_NAKED)
{
do_spm();
}
#else
#warning "Self-update disabled; device lacks SPM_RDY_vect"
#endif
#endif
// **** Main program
static void leaveBootloader( void )
{
LED_EXIT();
cli();
usbDeviceDisconnect();
USB_INTR_ENABLE = 0;
USB_INTR_CFG = 0; // also reset config bits
SET_IVSEL( 0 );
bootLoaderExit();
// GCC doesn't set EIND when generating EICALL on devices with large flash.
#ifdef EIND
EIND = 0;
#endif
// GCC bug: optimizes this to RCALL 0, which is mishandled by assembler.
// TODO: or not? seems to work now
//void (*nullVector)(void) __attribute__((noreturn)) = 0;
static void (* const nullVector)(void) __attribute__((noreturn)) = 0;
nullVector();
}
#ifndef WDTCSR
#define WDTCSR WDTCR
#endif
#ifndef WDTOE
#define WDTOE WDCE
#endif
static void initHardware( void )
{
// Clear cause-of-reset flags and try to disable WDT
MCUCSR = 0; // WDRF must be clear or WDT can't be disabled on some MCUs
WDTCSR = 1<<WDTOE | 1<<WDE;
WDTCSR = 1<<WDP2 | 1<<WDP1 | 1<<WDP0; // maximum timeout in case WDT is fused on
SET_IVSEL( 1 );
usbInit();
// Force USB re-enumerate so host sees us
usbDeviceDisconnect();
_delay_ms( 260 );
usbDeviceConnect();
sei();
LED_INIT();
}
int main( void ) __attribute__((noreturn,OS_main)); // optimization
int main( void )
{
#if USE_GLOBAL_REGS
currentRequest = currentRequest_init;
#if AUTO_EXIT_NO_USB_MS
timeoutHigh = timeoutHigh_init;
#endif
#endif
odDebugInit();
// Allow user to see registers before any disruption
bootLoaderInit();
initHardware(); // gives time for jumper pull-ups to stabilize
uchar i = 0; // tried unsigned int counter but added 60 bytes
uchar j = 0;
while ( bootLoaderCondition() )
{
wdt_reset(); // in case wdt is fused on
usbPoll();
if ( --i == 0 )
{
if ( --j == 0 )
{
LED_BLINK();
#if BOOTLOADER_CAN_EXIT
if ( currentRequest == USBASP_FUNC_DISCONNECT )
{
#if AUTO_EXIT_NO_USB_MS
if ( --timeoutHigh == 0 )
#endif
break;
}
#endif
}
}
}
leaveBootloader();
}
// Called when reset is received from host, possibly multiple times
#if AUTO_EXIT_NO_USB_MS
// TODO: less hacky approach (USB_RESET_HOOK took 18 bytes more code)
#undef DBG1
#if AUTO_EXIT_MS
#define DBG1( a, b, c ) {\
if ( (a) == 0xff )\
timeoutHigh = F_CPU/main_loop_clk / 1000 * (AUTO_EXIT_MS) / 0x10000;\
}
#else
#define DBG1( a, b, c ) {\
if ( (a) == 0xff )\
currentRequest = 0;\
}
#endif
#endif
// at end so we don't mistakenly use some of its internal variables
#include "usbdrv/usbdrv.c" // optimization: helps to have source in same file