forked from bradwood/Standalone-Arduino-AVR-ISP-programmer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.cpp
434 lines (371 loc) · 11.5 KB
/
code.cpp
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
424
425
426
427
428
429
430
431
432
433
#include "optiLoader.h"
/*
* Bootload images.
* These are the intel Hex files produced by the optiboot makefile,
* with a small amount of automatic editing to turn them into C strings,
* and a header attched to identify them
*/
extern image_t *images[];
extern uint8_t NUMIMAGES;
/*
* readSignature
* read the bottom two signature bytes (if possible) and return them
* Note that the highest signature byte is the same over all AVRs so we skip it
*/
uint16_t readSignature (void)
{
SPI.setClockDivider(CLOCKSPEED_FUSES);
uint16_t target_type = 0;
Serial.print("\nReading signature:");
target_type = spi_transaction(0x30, 0x00, 0x01, 0x00);
target_type <<= 8;
target_type |= spi_transaction(0x30, 0x00, 0x02, 0x00);
Serial.println(target_type, HEX);
if (target_type == 0 || target_type == 0xFFFF) {
if (target_type == 0) {
Serial.println(" (no target attached?)");
}
}
return target_type;
}
/*
* findImage
*
* given 'signature' loaded with the relevant part of the device signature,
* search the hex images that we have programmed in flash, looking for one
* that matches.
*/
image_t *findImage (uint16_t signature)
{
image_t *ip;
Serial.println("Searching for image...");
for (byte i=0; i < NUMIMAGES; i++) {
ip = images[i];
if (ip && (pgm_read_word(&ip->image_chipsig) == signature)) {
Serial.print(" Found \"");
flashprint(&ip->image_name[0]);
Serial.print("\" for ");
flashprint(&ip->image_chipname[0]);
Serial.println();
return ip;
}
}
Serial.println(" Not Found");
return 0;
}
/*
* programmingFuses
* Program the fuse/lock bits
*/
boolean programFuses (const byte *fuses)
{
SPI.setClockDivider(CLOCKSPEED_FUSES);
byte f;
Serial.print("\nSetting fuses");
f = pgm_read_byte(&fuses[FUSE_PROT]);
if (f) {
Serial.print("\n Set Lock Fuse to: ");
Serial.print(f, HEX);
Serial.print(" -> ");
Serial.print(spi_transaction(0xAC, 0xE0, 0x00, f), HEX);
}
f = pgm_read_byte(&fuses[FUSE_LOW]);
if (f) {
Serial.print(" Set Low Fuse to: ");
Serial.print(f, HEX);
Serial.print(" -> ");
Serial.print(spi_transaction(0xAC, 0xA0, 0x00, f), HEX);
}
f = pgm_read_byte(&fuses[FUSE_HIGH]);
if (f) {
Serial.print(" Set High Fuse to: ");
Serial.print(f, HEX);
Serial.print(" -> ");
Serial.print(spi_transaction(0xAC, 0xA8, 0x00, f), HEX);
}
f = pgm_read_byte(&fuses[FUSE_EXT]);
if (f) {
Serial.print(" Set Ext Fuse to: ");
Serial.print(f, HEX);
Serial.print(" -> ");
Serial.print(spi_transaction(0xAC, 0xA4, 0x00, f), HEX);
}
Serial.println();
return true; /* */
}
/*
* verifyFuses
* Verifies a fuse set
*/
boolean verifyFuses (const byte *fuses, const byte *fusemask)
{
SPI.setClockDivider(CLOCKSPEED_FUSES);
byte f;
Serial.println("Verifying fuses...");
f = pgm_read_byte(&fuses[FUSE_PROT]);
if (f) {
uint8_t readfuse = spi_transaction(0x58, 0x00, 0x00, 0x00); // lock fuse
readfuse &= pgm_read_byte(&fusemask[FUSE_PROT]);
Serial.print("\tLock Fuse: "); Serial.print(f, HEX); Serial.print(" is "); Serial.print(readfuse, HEX);
if (readfuse != f)
return false;
}
f = pgm_read_byte(&fuses[FUSE_LOW]);
if (f) {
uint8_t readfuse = spi_transaction(0x50, 0x00, 0x00, 0x00); // low fuse
Serial.print("\tLow Fuse: 0x"); Serial.print(f, HEX); Serial.print(" is 0x"); Serial.print(readfuse, HEX);
readfuse &= pgm_read_byte(&fusemask[FUSE_LOW]);
if (readfuse != f)
return false;
}
f = pgm_read_byte(&fuses[FUSE_HIGH]);
if (f) {
uint8_t readfuse = spi_transaction(0x58, 0x08, 0x00, 0x00); // high fuse
readfuse &= pgm_read_byte(&fusemask[FUSE_HIGH]);
Serial.print("\tHigh Fuse: 0x"); Serial.print(f, HEX); Serial.print(" is 0x"); Serial.print(readfuse, HEX);
if (readfuse != f)
return false;
}
f = pgm_read_byte(&fuses[FUSE_EXT]);
if (f) {
uint8_t readfuse = spi_transaction(0x50, 0x08, 0x00, 0x00); // ext fuse
readfuse &= pgm_read_byte(&fusemask[FUSE_EXT]);
Serial.print("\tExt Fuse: 0x"); Serial.print(f, HEX); Serial.print(" is 0x"); Serial.print(readfuse, HEX);
if (readfuse != f)
return false;
}
Serial.println();
return true; /* */
}
/*
* readImagePage
*
* Read a page of intel hex image from a string in pgm memory.
*/
// Returns number of bytes decoded
byte * readImagePage (byte *hextext, uint16_t pageaddr, uint8_t pagesize, byte *page)
{
boolean firstline = true;
uint16_t len;
uint8_t page_idx = 0;
byte *beginning = hextext;
byte b, cksum = 0;
//Serial.print("page size = "); Serial.println(pagesize, DEC);
// 'empty' the page by filling it with 0xFF's
for (uint8_t i=0; i<pagesize; i++)
page[i] = 0xFF;
while (1) {
uint16_t lineaddr;
// read one line!
if (pgm_read_byte(hextext++) != ':') {
error("No colon?");
break;
}
// Read the byte count into 'len'
len = hexton(pgm_read_byte(hextext++));
len = (len<<4) + hexton(pgm_read_byte(hextext++));
cksum = len;
// read high address byte
b = hexton(pgm_read_byte(hextext++));
b = (b<<4) + hexton(pgm_read_byte(hextext++));
cksum += b;
lineaddr = b;
// read low address byte
b = hexton(pgm_read_byte(hextext++));
b = (b<<4) + hexton(pgm_read_byte(hextext++));
cksum += b;
lineaddr = (lineaddr << 8) + b;
if (lineaddr >= (pageaddr + pagesize)) {
return beginning;
}
b = hexton(pgm_read_byte(hextext++)); // record type
b = (b<<4) + hexton(pgm_read_byte(hextext++));
cksum += b;
//Serial.print("Record type "); Serial.println(b, HEX);
if (b == 0x1) {
// end record!
break;
}
#if VERBOSE
Serial.print("\nLine address = 0x"); Serial.println(lineaddr, HEX);
Serial.print("Page address = 0x"); Serial.println(pageaddr, HEX);
#endif
for (byte i=0; i < len; i++) {
// read 'n' bytes
b = hexton(pgm_read_byte(hextext++));
b = (b<<4) + hexton(pgm_read_byte(hextext++));
cksum += b;
#if VERBOSE
Serial.print(b, HEX);
Serial.write(' ');
#endif
page[page_idx] = b;
page_idx++;
if (page_idx > pagesize) {
error("Too much code");
break;
}
}
b = hexton(pgm_read_byte(hextext++)); // chxsum
b = (b<<4) + hexton(pgm_read_byte(hextext++));
cksum += b;
if (cksum != 0) {
error("Bad checksum: ");
Serial.print(cksum, HEX);
}
if (pgm_read_byte(hextext++) != '\n') {
error("No end of line");
break;
}
#if VERBOSE
Serial.println();
Serial.println(page_idx, DEC);
#endif
if (page_idx == pagesize)
break;
}
#if VERBOSE
Serial.print("\n Total bytes read: ");
Serial.println(page_idx, DEC);
#endif
return hextext;
}
// Send one byte to the page buffer on the chip
void flashWord (uint8_t hilo, uint16_t addr, uint8_t data) {
#if VERBOSE
Serial.print(data, HEX); Serial.print(':');
Serial.print(spi_transaction(0x40+8*hilo, addr>>8 & 0xFF, addr & 0xFF, data), HEX);
Serial.print(" ");
#else
spi_transaction(0x40+8*hilo, addr>>8 & 0xFF, addr & 0xFF, data);
#endif
}
// Basically, write the pagebuff (with pagesize bytes in it) into page $pageaddr
boolean flashPage (byte *pagebuff, uint16_t pageaddr, uint8_t pagesize) {
SPI.setClockDivider(CLOCKSPEED_FLASH);
Serial.print("Flashing page "); Serial.println(pageaddr, HEX);
for (uint16_t i=0; i < pagesize/2; i++) {
#if VERBOSE
Serial.print(pagebuff[2*i], HEX); Serial.print(' ');
Serial.print(pagebuff[2*i+1], HEX); Serial.print(' ');
if ( i % 16 == 15) Serial.println();
#endif
flashWord(LOW, i, pagebuff[2*i]);
flashWord(HIGH, i, pagebuff[2*i+1]);
}
// page addr is in bytes, byt we need to convert to words (/2)
pageaddr = (pageaddr/2) & 0xFFC0;
uint16_t commitreply = spi_transaction(0x4C, (pageaddr >> 8) & 0xFF, pageaddr & 0xFF, 0);
Serial.print(" Commit Page: 0x"); Serial.print(pageaddr, HEX);
Serial.print(" -> 0x"); Serial.println(commitreply, HEX);
if (commitreply != pageaddr)
return false;
busyWait();
return true;
}
// verifyImage does a byte-by-byte verify of the flash hex against the chip
// Thankfully this does not have to be done by pages!
// returns true if the image is the same as the hextext, returns false on any error
boolean verifyImage (byte *hextext) {
uint16_t address = 0;
SPI.setClockDivider(CLOCKSPEED_FLASH);
uint16_t len;
byte b, cksum = 0;
while (1) {
uint16_t lineaddr;
// read one line!
if (pgm_read_byte(hextext++) != ':') {
error("No colon");
return false;
}
len = hexton(pgm_read_byte(hextext++));
len = (len<<4) + hexton(pgm_read_byte(hextext++));
cksum = len;
b = hexton(pgm_read_byte(hextext++)); // record type
b = (b<<4) + hexton(pgm_read_byte(hextext++));
cksum += b;
lineaddr = b;
b = hexton(pgm_read_byte(hextext++)); // record type
b = (b<<4) + hexton(pgm_read_byte(hextext++));
cksum += b;
lineaddr = (lineaddr << 8) + b;
b = hexton(pgm_read_byte(hextext++)); // record type
b = (b<<4) + hexton(pgm_read_byte(hextext++));
cksum += b;
//Serial.print("Record type "); Serial.println(b, HEX);
if (b == 0x1) {
// end record!
break;
}
for (byte i=0; i < len; i++) {
// read 'n' bytes
b = hexton(pgm_read_byte(hextext++));
b = (b<<4) + hexton(pgm_read_byte(hextext++));
cksum += b;
#if VERBOSE
Serial.print("$");
Serial.print(lineaddr, HEX);
Serial.print(":0x");
Serial.print(b, HEX);
Serial.write(" ? ");
#endif
// verify this byte!
if (lineaddr % 2) {
// for 'high' bytes:
if (b != (spi_transaction(0x28, lineaddr >> 9, lineaddr / 2, 0) & 0xFF)) {
Serial.print("verification error at address 0x"); Serial.print(lineaddr, HEX);
Serial.print(" Should be 0x"); Serial.print(b, HEX); Serial.print(" not 0x");
Serial.println((spi_transaction(0x28, lineaddr >> 9, lineaddr / 2, 0) & 0xFF), HEX);
return false;
}
} else {
// for 'low bytes'
if (b != (spi_transaction(0x20, lineaddr >> 9, lineaddr / 2, 0) & 0xFF)) {
Serial.print("verification error at address 0x"); Serial.print(lineaddr, HEX);
Serial.print(" Should be 0x"); Serial.print(b, HEX); Serial.print(" not 0x");
Serial.println((spi_transaction(0x20, lineaddr >> 9, lineaddr / 2, 0) & 0xFF), HEX);
return false;
}
}
lineaddr++;
}
b = hexton(pgm_read_byte(hextext++)); // chxsum
b = (b<<4) + hexton(pgm_read_byte(hextext++));
cksum += b;
if (cksum != 0) {
error("Bad checksum: ");
Serial.print(cksum, HEX);
return false;
}
if (pgm_read_byte(hextext++) != '\n') {
error("No end of line");
return false;
}
}
return true;
}
// Send the erase command, then busy wait until the chip is erased
void eraseChip(void) {
SPI.setClockDivider(CLOCKSPEED_FUSES);
spi_transaction(0xAC, 0x80, 0, 0); // chip erase
busyWait();
}
// Simply polls the chip until it is not busy any more - for erasing and programming
void busyWait(void) {
byte busybit;
do {
busybit = spi_transaction(0xF0, 0x0, 0x0, 0x0);
//Serial.print(busybit, HEX);
} while (busybit & 0x01);
}
/*
* Functions specific to ISP programming of an AVR
*/
uint16_t spi_transaction (uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
uint8_t n, m;
SPI.transfer(a);
n = SPI.transfer(b);
//if (n != a) error = -1;
m = SPI.transfer(c);
return 0xFFFFFF & ((n<<16)+(m<<8) + SPI.transfer(d));
}