-
Notifications
You must be signed in to change notification settings - Fork 20
/
LGTISP.ino
557 lines (510 loc) · 12.2 KB
/
LGTISP.ino
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// author : brother_yan
//
// LarduinoISP for LGT8FX8P series
// Project fork from
// - ArduinoISP version 04m3
// Copyright (c) 2008-2011 Randall Bohn
// If you require a license, see
// http://www.opensource.org/licenses/bsd-license.php
//
// This sketch turns the Arduino into a AVRISP
// using the following arduino pins:
//
// pin name: Arduino: LGT8FX8P:
// slave reset: 10: PC6/RESET
// SWD: 12: PE2/SWD
// SWC: 13: PE0/SCK
//
// Put an LED (with resistor) on the following pins:
// 9: Heartbeat - shows the programmer is running
// 8: Error - Lights up if something goes wrong (use red if that makes sense)
// 7: Programming - In communication with the slave
//
// 23 July 2011 Randall Bohn
// -Address Arduino issue 509 :: Portability of ArduinoISP
// http://code.google.com/p/arduino/issues/detail?id=509
//
// October 2010 by Randall Bohn
// - Write to EEPROM > 256 bytes
// - Better use of LEDs:
// -- Flash LED_PMODE on each flash commit
// -- Flash LED_PMODE while writing EEPROM (both give visual feedback of writing progress)
// - Light LED_ERR whenever we hit a STK_NOSYNC. Turn it off when back in sync.
// - Use pins_arduino.h (should also work on Arduino Mega)
//
// October 2009 by David A. Mellis
// - Added support for the read signature command
//
// February 2009 by Randall Bohn
// - Added support for writing to EEPROM (what took so long?)
// Windows users should consider WinAVR's avrdude instead of the
// avrdude included with Arduino software.
//
// January 2008 by Randall Bohn
// - Thanks to Amplificar for helping me with the STK500 protocol
// - The AVRISP/STK500 (mk I) protocol is used in the arduino bootloader
// - The SPI functions herein were developed for the AVR910_ARD programmer
// - More information at http://code.google.com/p/mega-isp
// LarduinoISP for LGTF8FX8P Series
#include "swd_lgt8fx8p.h"
#if SERIAL_RX_BUFFER_SIZE < 250 // 64 bytes的RX缓冲不够大
#error : Please change the macro SERIAL_RX_BUFFER_SIZE to 250
#endif
#define LED_HB 9
#define LED_ERR 8
#define LED_PMODE 7
#define PROG_FLICKER true
#define HWVER 3
#define SWMAJ 6
#define SWMIN 4
// STK Definitions
#define STK_OK 0x10
#define STK_FAILED 0x11
#define STK_UNKNOWN 0x12
#define STK_INSYNC 0x14
#define STK_NOSYNC 0x15
#define CRC_EOP 0x20 //ok it is a space...
void pulse(int pin, int times);
void setup()
{
Serial.begin(115200);
pinMode(LED_PMODE, OUTPUT);
//pulse(LED_PMODE, 2);
pinMode(LED_ERR, OUTPUT);
//pulse(LED_ERR, 2);
//pinMode(LED_HB, OUTPUT);
//pulse(LED_HB, 2);
}
uint8_t error=0;
// address for reading and writing, set by 'U' command
int address;
uint8_t buff[256]; // global block storage
#define beget16(addr) (*addr * 256 + *(addr+1) )
typedef struct param {
uint8_t devicecode;
uint8_t revision;
uint8_t progtype;
uint8_t parmode;
uint8_t polling;
uint8_t selftimed;
uint8_t lockbytes;
uint8_t fusebytes;
uint8_t flashpoll;
uint16_t eeprompoll;
uint16_t pagesize;
uint16_t eepromsize;
uint32_t flashsize;
} parameter_t;
parameter_t param;
// this provides a heartbeat on pin 9, so you can tell the software is running.
uint8_t hbval=128;
uint8_t hbdelta=8;
void heartbeat()
{
if (hbval > 192) hbdelta = -hbdelta;
if (hbval < 32) hbdelta = -hbdelta;
hbval += hbdelta;
analogWrite(LED_HB, hbval);
delay(40);
}
void loop(void)
{
// is pmode active?
if (LGTISP.isPmode()) digitalWrite(LED_PMODE, HIGH);
else digitalWrite(LED_PMODE, LOW);
// is taddress an error?
if (error) digitalWrite(LED_ERR, HIGH);
else digitalWrite(LED_ERR, LOW);
// light the heartbeat LED
//heartbeat();
if (Serial.available())
avrisp();
}
uint8_t getch() {
while(!Serial.available());
return Serial.read();
}
void fill(int n)
{
for (int x = 0; x < n; x++) {
buff[x] = getch();
}
}
#define PTIME 30
void pulse(int pin, int times)
{
do {
digitalWrite(pin, HIGH);
delay(PTIME);
digitalWrite(pin, LOW);
delay(PTIME);
}
while (times--);
}
void prog_lamp(int state)
{
if (PROG_FLICKER)
digitalWrite(LED_PMODE, state);
}
void empty_reply()
{
if (CRC_EOP == getch()) {
Serial.print((char)STK_INSYNC);
Serial.print((char)STK_OK);
}
else {
error++;
Serial.print((char)STK_NOSYNC);
}
}
void breply(uint8_t b)
{
if (CRC_EOP == getch()) {
Serial.print((char)STK_INSYNC);
Serial.print((char)b);
Serial.print((char)STK_OK);
}
else {
error++;
Serial.print((char)STK_NOSYNC);
}
}
void get_version(uint8_t c)
{
switch(c) {
case 0x80:
breply(HWVER);
break;
case 0x81:
breply(SWMAJ);
break;
case 0x82:
breply(SWMIN);
break;
case 0x93:
breply('S'); // serial programmer
break;
default:
breply(0);
}
}
void set_parameters()
{
// call this after reading paramter packet into buff[]
param.devicecode = buff[0];
param.revision = buff[1];
param.progtype = buff[2];
param.parmode = buff[3];
param.polling = buff[4];
param.selftimed = buff[5];
param.lockbytes = buff[6];
param.fusebytes = buff[7];
param.flashpoll = buff[8];
// ignore buff[9] (= buff[8])
// following are 16 bits (big endian)
param.eeprompoll = beget16(&buff[10]);
param.pagesize = beget16(&buff[12]);
param.eepromsize = beget16(&buff[14]);
// 32 bits flashsize (big endian)
param.flashsize = buff[16] * 0x01000000
+ buff[17] * 0x00010000
+ buff[18] * 0x00000100
+ buff[19];
}
void universal()
{
fill(4);
if(buff[0] == 0x30 && buff[1] == 0x00) {
switch(buff[2]) {
case 0x00:
breply(0x1e);
break;
case 0x01:
breply(0x95);
break;
case 0x02:
breply(0x0f);
break;
default:
breply(0xff);
break;
}
} else if(buff[0] == 0xf0) {
breply(0x00);
} else {
breply(0xff);
}
}
void write_flash(int length)
{
int addr = address * 2; // 字节地址
/*
lgt8fx8p的flash是按4字节编址的,而avr是按2字节编址的,avrdude传过来的是按2字节编址的address
avrisp()函数中也有证实:
case 'U': // set address (word)
*/
fill(length);
if (CRC_EOP == getch()) {
Serial.print((char) STK_INSYNC);
LGTISP.write(addr, buff, length);
Serial.print((char) STK_OK);
}
else {
error++;
Serial.print((char) STK_NOSYNC);
}
}
#define EECHUNK (32)
uint8_t write_eeprom(int length)
{
// address is a word address, get the byte address
int start = address * 2;
int remaining = length;
if (length > param.eepromsize) {
error++;
return STK_FAILED;
}
while (remaining > EECHUNK) {
write_eeprom_chunk(start, EECHUNK);
start += EECHUNK;
remaining -= EECHUNK;
}
write_eeprom_chunk(start, remaining);
return STK_OK;
}
// write (length) bytes, (start) is a byte address
uint8_t write_eeprom_chunk(int start, int length)
{
// this writes byte-by-byte,
// page writing may be faster (4 bytes at a time)
fill(length);
prog_lamp(LOW);
for (int x = 0; x < length; x++) {
int addr = start+x;
// do e2prom program here
// donothing for lgt8fx8d series
delay(45);
}
prog_lamp(HIGH);
return STK_OK;
}
void program_page()
{
char result = (char) STK_FAILED;
// get length
uint16_t length = getch() << 8;
length += getch();
char memtype = getch();
// flash memory @address, (length) bytes
if (memtype == 'F') {
write_flash(length);
return;
}
if (memtype == 'E') {
result = (char)write_eeprom(length);
if (CRC_EOP == getch()) {
Serial.print((char) STK_INSYNC);
Serial.print(result);
}
else {
error++;
Serial.print((char) STK_NOSYNC);
}
return;
}
Serial.print((char)STK_FAILED);
return;
}
char eeprom_read_page(uint16_t length)
{
// address again we have a word address
uint16_t start = address * 2;
for (int x = 0; x < length; x++) {
uint16_t addr = start + x;
// do ep2rom read here
// but donothing for lgt8fx8d series (by now...)
Serial.print((char) 0xff);
}
return STK_OK;
}
void read_page()
{
char result = (char)STK_FAILED;
int addr = address * 2; // 字节地址
/*
lgt8fx8p的flash是按4字节编址的,而avr是按2字节编址的,avrdude传过来的是按2字节编址的address
avrisp()函数中也有证实:
case 'U': // set address (word)
*/
uint16_t length = getch() << 8;
length += getch();
char memtype = getch();
if (CRC_EOP != getch()) {
error++;
Serial.print((char) STK_NOSYNC);
return;
}
Serial.print((char) STK_INSYNC);
if (memtype == 'F')
{
LGTISP.read(addr, buff, length);
for (int i = 0; i < length; ++i)
Serial.print((char)buff[i]);
result = STK_OK;
}
if (memtype == 'E') result = eeprom_read_page(length);
Serial.print(result);
return;
}
void read_signature()
{
if (CRC_EOP != getch()) {
error++;
Serial.print((char) STK_NOSYNC);
return;
}
Serial.print((char) STK_INSYNC);
Serial.print((char) 0x1e);
Serial.print((char) 0x95);
Serial.print((char) 0x0a);
Serial.print((char) STK_OK);
}
//////////////////////////////////////////
//////////////////////////////////////////
////////////////////////////////////
////////////////////////////////////
int avrisp()
{
const char copyright[] = "{\"author\" : \"brother_yan\"}";
uint16_t sum = 0;
for (uint16_t i = 0; copyright[i] != 0; ++i)
sum ^= (uint8_t)(copyright[i]) * i;
if (sum != 0x0bdc)
return 0;
uint8_t data, low, high;
uint8_t ch = getch();
switch (ch) {
case '0': // signon
error = 0;
empty_reply();
break;
case '1':
if (getch() == CRC_EOP) {
Serial.print((char) STK_INSYNC);
Serial.print("AVR ISP");
Serial.print((char) STK_OK);
} else {
error++;
Serial.print((char) STK_NOSYNC);
}
break;
case 'y': // 0x79, not used in stk500 protocol. I use this command to read copyright information
if (getch() == CRC_EOP)
{
Serial.print((char) STK_INSYNC);
Serial.println(copyright);
Serial.print((char) STK_OK);
}
else
{
error++;
Serial.print((char) STK_NOSYNC);
}
break;
case 'z': // 0x7a, not used in stk500 protocol. I use this command to read GUID
if (getch() == CRC_EOP)
{
char guid[4];
*((uint32_t *)guid) = LGTISP.getGUID();
Serial.print((char) STK_INSYNC);
Serial.print(guid[0]);
Serial.print(guid[1]);
Serial.print(guid[2]);
Serial.print(guid[3]);
Serial.print((char) STK_OK);
}
else
{
error++;
Serial.print((char) STK_NOSYNC);
}
break;
case 'A':
get_version(getch());
break;
case 'B': // optional for lgt8fx8d series
fill(20);
set_parameters();
empty_reply();
break;
case 'E': // extended parameters - ignore for now
fill(5);
empty_reply();
break;
case 'P':
if (LGTISP.isPmode()) {
pulse(LED_ERR, 3);
} else {
LGTISP.begin();
}
if (LGTISP.isPmode())
empty_reply();
else
{
if (CRC_EOP == getch()) {
Serial.print((char)STK_INSYNC);
Serial.print((char)STK_FAILED);
}
else {
error++;
Serial.print((char)STK_NOSYNC);
}
}
break;
case 'U': // set address (word)
address = getch();
address += (getch() << 8);
empty_reply();
break;
case 0x60: //STK_PROG_FLASH
low = getch();
high = getch();
empty_reply();
break;
case 0x61: //STK_PROG_DATA
data = getch();
empty_reply();
break;
case 0x64: //STK_PROG_PAGE
program_page();
break;
case 0x74: //STK_READ_PAGE 't'
read_page();
break;
case 'V': //0x56
universal();
break;
case 'Q': //0x51
error=0;
LGTISP.end();
empty_reply();
break;
case 0x75: //STK_READ_SIGN 'u'
read_signature();
break;
// expecting a command, not CRC_EOP
// this is how we can get back in sync
case CRC_EOP:
error++;
Serial.print((char) STK_NOSYNC);
break;
// anything else we will return STK_UNKNOWN
default:
error++;
if (CRC_EOP == getch())
Serial.print((char)STK_UNKNOWN);
else
Serial.print((char)STK_NOSYNC);
}
return 0;
}