-
Notifications
You must be signed in to change notification settings - Fork 2
/
eeprom-programmer.ino
1025 lines (891 loc) · 26.6 KB
/
eeprom-programmer.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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* eeprom-programmer.ino
*
* Arduino Nano code for the at28c64b-15pu programmer. Note that this code is
* specific to the Nano (really, the ATmega328P). If you use this on another
* device, either make sure the port mappings are the same or you will need to
* rewrite most of the code to use the correct mappings.
*
* I haven't looked at other EEPROMs, so I don't know how easy/difficult it
* would be to adapt this code to one. I'm not even sure if the pinouts for the
* address lines or page sizes for the block write operations are the same
* for different sizes of this same chip.
*
* The clock speed of the Nano (16MHz) is also assumed for getting the number
* of nops to use for delays: 1 nop = 1 clock cycle = 1 / speed in MHz. So,
* in the case of the Nano, 1 nop = 1 / 16000000 = 62.5ns.
*
* Serial port settings:
* 57600, 8n1, no flow control
*
* If you want to use other than 57600, change the SERIAL_BAUD constant below.
*
* Terminal settings:
* turn off local echo
* Newline receive: LF
* Newline transmit: LF
*/
/* These are used in the x-modem protocol */
#define CHAR_SOH 0x01
#define CHAR_EOT 0x04
#define CHAR_ACK 0x06
#define CHAR_NAK 0x15
#define CHAR_ETB 0x17
#define CHAR_CAN 0x18
#define CHAR_ESC 0x1b
#define CHAR_C 0x43
#define XMODEM_RETRIES 10
#define XMODEM_DELAY 2000
#define XMODEM_PACKET_MAX 128
/* Serial commands accepted by the programmer */
#define CMD_ERASE 'e'
#define CMD_FILL 'f'
#define CMD_HELP 'h'
#define CMD_LOCK 'l'
#define CMD_READ 'r'
#define CMD_SIZE 's'
#define CMD_UNLOCK 'u'
#define CMD_VERSION 'v'
#define CMD_XMODEM 'x'
/* Software version
*/
#define VERSION_MAJ 1
#define VERSION_MIN 2
#define VERSION_BLD 0
/* Change this to match whatever baud rate you want to use for the serial
* connection.
*/
#define SERIAL_BAUD 57600
/* Set this to the size, in bytes of the EEPROM. In the case of the at28c64b,
* it's 8k.
*/
#define DEFAULT_EEPROM_SIZE 32768
#define PAGE_SIZE 64
/* Milliseconds to wait for a write to complete. */
#define WRITE_DELAY 25
/* Pin assignments
*
* These pin assigments correspond to these port mappings on the Atmega328P
* (and Atmega168):
*
* x4HC595 shift registers
* PORTC, bit 4 = serial data pin (SER)
* PORTC, bit 3 = serial clock pin (SRCLK)
* PORTC, bit 2 = serial output enable (/OE)
* PORTC, bit 1 = serial read clock (RCLK)
* PORTC, bit 0 = reset all (/SRCLR)
*
* at28c64b eeprom
* PORTB, bit 4 = eeprom output enable (/OE)
* PORTB, bit 3 = eeprom chip enable (/CE)
* PORTB, bit 2 = eeprom write enable (/WE)
* PORTB, bit 1 = I/O7
* PORTB, bit 0 = I/O6
* PORTD, bit 7 = I/O5
* PORTD, bit 6 = I/O4
* PORTD, bit 5 = I/O3
* PORTD, bit 4 = I/O2
* PORTD, bit 3 = I/O1
* PORTD, bit 2 = I/O0
*/
const int srSerialData = A4;
const int srSerialClock = A3;
const int srNotOutputEnable = A2;
const int srReadClock = A1;
const int srNotResetAll = A0;
const int eeNotWriteEnable = 10;
const int eeNotChipEnable = 11;
const int eeNotOutputEnable = 12;
const int eeD0 = 2;
const int eeD1 = 3;
const int eeD2 = 4;
const int eeD3 = 5;
const int eeD4 = 6;
const int eeD5 = 7;
const int eeD6 = 8;
const int eeD7 = 9;
unsigned int eeprom_size = DEFAULT_EEPROM_SIZE;
/* Clear the outputs of all shift registers to 0 by toggling the /srclr line.
* /SRCLR needs to be low for 120ns, worst case. That's 3 nops, counting a
* nop as 60ns.
*/
void sr_clear(void)
{
PORTC = B00000100;
__asm__ __volatile__ ("nop\n\t");
__asm__ __volatile__ ("nop\n\t");
__asm__ __volatile__ ("nop\n\t");
PORTC = B00000101;
}
/* Setup the shift registers and shift in an address bit. Assumes that the bit
* in question has already been shifted into bit 4: so, in the same position as
* the "1" here: 00010000.
*/
void __attribute__((always_inline)) sr_shift_bit(byte bit)
{
bit &= B00010000;
// clear clock and data lines
PORTC = B00000101 | bit;
__asm__ __volatile__ ("nop\n\t");
PORTC = B00001101 | bit;
__asm__ __volatile__ ("nop\n\t");
}
/* Shifts out a given address on the 74HC595s.
*
* This is all about speed: no loops, no digitalRead()/Write(), always inline.
* It's expected that the proper directions for the PORT bits used here are
* already setup properly. Namely, PORTC bits 3 and 4 should be setup for
* output.
*
* The observant programmer may notice we shift 16 bits onto the address lines.
* This isn't a problem, even though we only have 13 address lines (on the
* targetted 8k x 8 chip, chips with more memory, of course, will have more
* address lines). The upper three bits simply aren't connected to anything and
* the order in which the bits go out the shift registers guarantees that the
* lower bits are always in the right place, no matter how many bits are
* shifted out. If this ends up being used with a larger EEPROM, then this
* function at least won't have to be modified.
*/
void __attribute__((always_inline)) sr_address(word address)
{
// disable output
PORTC |= B00000100;
// shift the address into the shift registers
sr_shift_bit(address >> 11);
sr_shift_bit(address >> 10);
sr_shift_bit(address >> 9);
sr_shift_bit(address >> 8);
sr_shift_bit(address >> 7);
sr_shift_bit(address >> 6);
sr_shift_bit(address >> 5);
sr_shift_bit(address >> 4);
sr_shift_bit(address >> 3);
sr_shift_bit(address >> 2);
sr_shift_bit(address >> 1);
sr_shift_bit(address);
sr_shift_bit(address << 1);
sr_shift_bit(address << 2);
sr_shift_bit(address << 3);
sr_shift_bit(address << 4);
// make sure the clock and data lines are clear
PORTC = B00000101;
// put the address on the output pins by setting output enable low
// and setting the read clock high
PORTC = B00000011;
}
/* Puts a data value onto the data bus. The lower six bits of the data bus go
* to the upper 6 bits of PORTD. PORTB's lower two bits hold the the upper two
* bits of data. Again, this is about speed: The function is inline and uses
* direct PORT writes. It's expected that all data pins are setup as OUTPUT
* prior to this function being invoked.
*/
void __attribute__((always_inline)) write_data(byte data)
{
PORTD = ((PORTD & B00000011) | (data << 2));
PORTB = ((PORTB & B11111100) | (data >> 6));
}
/* Reads the data value in the EEPROM at the address currently on the address
* pins. Assumes that the I/O lines are already setup as INPUTs and that the
* /OE line is set properly.
*/
byte __attribute__((always_inline)) read_data(void)
{
return ((PIND >> 2) | (PINB << 6));
}
/* Set all I/O (data) pins to output
*/
void io_set_output(void)
{
// all data pins are outputs
DDRD |= B11111100;
DDRB |= B00000011;
}
/* Set all I/O (data) pins to input
*/
void io_set_input(void)
{
// all data pins are inputs
DDRD &= B00000011;
DDRB &= B11111100;
}
/* Executes the appropriate write sequence to enable the software locking
* mechanism of the chip. After this function executes, the chip will not
* recognize writes unless unlock_chip() is called.
*/
int lock_chip(void)
{
// Setup a known state. Enable chip.
digitalWrite(eeNotChipEnable, LOW);
digitalWrite(eeNotOutputEnable, HIGH);
digitalWrite(eeNotWriteEnable, HIGH);
// Set data lines to outputs
io_set_output();
// aah to address 1555h
sr_address(0x1555);
write_data(0xaa);
digitalWrite(eeNotWriteEnable, LOW);
digitalWrite(eeNotWriteEnable, HIGH);
// 55h to address 0aaah
sr_address(0x0aaa);
write_data(0x55);
digitalWrite(eeNotWriteEnable, LOW);
digitalWrite(eeNotWriteEnable, HIGH);
// a0h to address 1555h
sr_address(0x1555);
write_data(0xa0);
digitalWrite(eeNotWriteEnable, LOW);
digitalWrite(eeNotWriteEnable, HIGH);
delay(WRITE_DELAY);
return 1;
}
/* Disables the software write protect mechanism.
*/
int unlock_chip(void)
{
// Setup a known state. Enable chip.
digitalWrite(eeNotChipEnable, LOW);
digitalWrite(eeNotOutputEnable, HIGH);
digitalWrite(eeNotWriteEnable, HIGH);
// Set data lines to outputs
io_set_output();
// aah to address 1555h
sr_address(0x1555);
write_data(0xaa);
digitalWrite(eeNotWriteEnable, LOW);
digitalWrite(eeNotWriteEnable, HIGH);
// 55h to address 0aaah
sr_address(0x0aaa);
write_data(0x55);
digitalWrite(eeNotWriteEnable, LOW);
digitalWrite(eeNotWriteEnable, HIGH);
// 80h to address 1555h
sr_address(0x1555);
write_data(0x80);
digitalWrite(eeNotWriteEnable, LOW);
digitalWrite(eeNotWriteEnable, HIGH);
// aah to address 1555h
sr_address(0x1555);
write_data(0xaa);
digitalWrite(eeNotWriteEnable, LOW);
digitalWrite(eeNotWriteEnable, HIGH);
// 55h to address 0aaah
sr_address(0x0aaa);
write_data(0x55);
digitalWrite(eeNotWriteEnable, LOW);
digitalWrite(eeNotWriteEnable, HIGH);
// 20h to address 1555h
sr_address(0x1555);
write_data(0x20);
digitalWrite(eeNotWriteEnable, LOW);
digitalWrite(eeNotWriteEnable, HIGH);
delay(WRITE_DELAY);
return 1;
}
/* Writes a single input byte at a specified address.
*/
int write_byte(word address, byte data)
{
// set data lines as outputs
io_set_output();
// setup initial chip state
digitalWrite(eeNotChipEnable, LOW);
digitalWrite(eeNotOutputEnable, HIGH);
digitalWrite(eeNotWriteEnable, HIGH);
// set the address pins from the shift registers
sr_address(address);
// put the data on the EEPROM's i/o pins
write_data(data);
// setup the we-controlled write state
digitalWrite(eeNotWriteEnable, LOW);
// end the write state
digitalWrite(eeNotWriteEnable, HIGH);
delay(WRITE_DELAY);
// byte was successfully written
return 1;
}
/* Write a page of a given length (64 bytes max) at a given address.
*/
int write_page(word address, byte *data, byte len)
{
// all data pins are outputs
io_set_output();
// setup initial state for control lines
digitalWrite(eeNotChipEnable, LOW);
digitalWrite(eeNotOutputEnable, HIGH);
digitalWrite(eeNotWriteEnable, HIGH);
while (len--) {
// put address into shift registers
sr_address(address++);
// put data byte on EEPROM pins
write_data(*data++);
// bring write enable low
digitalWrite(eeNotWriteEnable, LOW);
// bring write enable high
digitalWrite(eeNotWriteEnable, HIGH);
}
// turn off write state
digitalWrite(eeNotWriteEnable, HIGH);
digitalWrite(eeNotOutputEnable, LOW);
// wait for updates to be committed to chip
delay(WRITE_DELAY);
return 1;
}
/* Write an xmodem packet 1 EEPROM page at a time.
*/
void write_packet(word address, byte *data, word len)
{
word i;
for (i = 0; i < len; i += PAGE_SIZE, data += PAGE_SIZE) {
write_page(address + i, data, PAGE_SIZE);
}
}
/* Very that the received xmodem packet matches what was
* written to the EEPROM.
*/
int verify_packet(word address, byte *data, word len)
{
word i;
for (i = 0; i < len; i++, address++, data++) {
if (read_byte(address) != *data) {
return 0;
}
}
return 1;
}
/* Erase the EEPROM by writing all 1s to it. We do it this way, since the
* hardware isn't there to do a hardware erase. That would require putting
* a 12 volt line in the circuit, and... why bother...
*/
int erase_eeprom(void)
{
byte fill[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
word i;
for (i = 0; i < eeprom_size; i += PAGE_SIZE) {
if (i % 1024 == 0) {
Serial.print(".");
}
if (!write_page(i, fill, PAGE_SIZE)) {
return 0;
}
}
return 1;
}
/* Returns the byte at the given address in the EEPROM.
*/
byte read_byte(word address)
{
byte data;
digitalWrite(eeNotOutputEnable, HIGH);
digitalWrite(eeNotWriteEnable, HIGH);
digitalWrite(eeNotChipEnable, LOW);
// all data pins are inputs
io_set_input();
sr_address(address);
digitalWrite(eeNotOutputEnable, LOW);
data = read_data();
digitalWrite(eeNotOutputEnable, HIGH);
return data;
}
/* Writes a pattern to the EEPROM. The pattern is address = address & 0xff. So,
* essentially, every address is set to the value of its lower 8 bits. I guess
* that's the long way to say, it repeats from 0 to 255 until the end of memory.
*/
int write_pattern(void)
{
byte fill[PAGE_SIZE];
word i, j;
for (i = 0; i < eeprom_size; i += PAGE_SIZE) {
for (j = 0; j < PAGE_SIZE; j++) {
fill[j] = i + j;
}
if (i % 1024 == 0) {
Serial.print(".");
}
if (!write_page(i, fill, PAGE_SIZE)) {
return 0;
}
}
return 1;
}
/* Reads the entire EEPROM. Prints the data out in the format:
* aaaa xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx ................
* Where aaaa is the address in hex, xx is 16 bytes of data in hex and
* ............... is the ascii representation for each data byte, if it is
* printable.
*/
void read_eeprom(void)
{
char s[6];
word i, j;
byte d;
for (i = 0; i < eeprom_size; i += 16) {
sprintf(s, "%04x ", i);
Serial.print(s);
for (j = 0; j < 16; j++) {
sprintf(s, " %02x", read_byte(i + j));
Serial.print(s);
}
Serial.print(" ");
for (j = 0; j < 16; j++) {
d = read_byte(i + j);
sprintf(s, "%c", isprint(d) ? d : '.');
Serial.print(s);
}
Serial.println("");
}
}
/* Display terminal prompt
*/
void show_prompt(void)
{
Serial.print("> ");
}
/* Display serial command help.
*/
void cmd_help(void)
{
Serial.println("commands:");
Serial.print(CMD_ERASE); Serial.println(" - erase the eeprom");
Serial.print(CMD_FILL); Serial.println(" - fill eeprom with incrementing bytes");
Serial.print(CMD_HELP); Serial.println(" - help (this) text");
Serial.print(CMD_LOCK); Serial.println(" - lock the eeprom against writes");
Serial.print(CMD_READ); Serial.println(" - read and dump eeprom contents");
Serial.print(CMD_SIZE); Serial.print(" - set the size of the eeprom (currently "); Serial.print(eeprom_size / 1024); Serial.print("k x 8, ");
if (eeprom_size == 8192) {
Serial.print("AT28C64");
} else if (eeprom_size == 32768) {
Serial.print("AT28C256");
} else {
Serial.print("Unknown chip");
}
Serial.println(")");
Serial.print(CMD_VERSION); Serial.println(" - print software version info");
Serial.print(CMD_UNLOCK); Serial.println(" - unlock eeprom writes");
Serial.print(CMD_XMODEM); Serial.println(" - xmodem transfer binary file to eeprom");
}
/* Initial board setup.
*/
void setup()
{
pinMode(srSerialData, OUTPUT);
pinMode(srSerialClock, OUTPUT);
pinMode(srNotOutputEnable, OUTPUT);
pinMode(srReadClock, OUTPUT);
pinMode(srNotResetAll, OUTPUT);
pinMode(eeNotWriteEnable, OUTPUT);
pinMode(eeNotChipEnable, OUTPUT);
pinMode(eeNotOutputEnable, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(eeD0, OUTPUT);
pinMode(eeD1, OUTPUT);
pinMode(eeD2, OUTPUT);
pinMode(eeD3, OUTPUT);
pinMode(eeD4, OUTPUT);
pinMode(eeD5, OUTPUT);
pinMode(eeD6, OUTPUT);
pinMode(eeD7, OUTPUT);
sr_clear();
digitalWrite(srSerialData, LOW);
digitalWrite(srSerialClock, LOW);
digitalWrite(srNotOutputEnable, HIGH);
digitalWrite(srReadClock, LOW);
digitalWrite(eeNotWriteEnable, HIGH);
digitalWrite(eeNotChipEnable, LOW);
digitalWrite(eeNotOutputEnable, HIGH);
Serial.begin(SERIAL_BAUD);
cmd_help();
show_prompt();
}
/* Flush the serial input buffer
*/
void flush_serial_input(void)
{
while (Serial.available()) {
Serial.read();
}
}
/* Sends the xmodem abort sequence out the serial port.
*/
void abort_xmodem(char *s)
{
int i;
for (i = 0; i < 8; i++) {
Serial.write(CHAR_CAN);
Serial.flush();
delay(1000);
}
Serial.print("transfer aborted: ");
Serial.println(s);
flush_serial_input();
}
/* Read the next available byte from the serial port.
*/
int read_serial(void)
{
while (!Serial.available());
return Serial.read();
}
/* Erase the EEPROM.
*/
void cmd_erase(void)
{
Serial.print("erasing eeprom ");
if (!erase_eeprom()) {
Serial.println(" error");
return;
} else {
Serial.println(" ok");
}
}
/* Fill the EEPROM with a sequence.
*/
void cmd_fill(void)
{
Serial.print("filling eeprom ");
if (!write_pattern()) {
Serial.println(" error");
return;
} else {
Serial.println(" ok");
}
}
/* Lock the chip against writes.
*/
void cmd_lock(void)
{
Serial.print("locking chip ... ");
if (lock_chip()) {
Serial.println("ok");
} else {
Serial.println("error");
}
}
/* Read the entire EEPROM.
*/
void cmd_read(void)
{
Serial.println("reading eeprom:");
read_eeprom();
Serial.println("all addresses read");
}
/* Display size command help
*/
void cmd_size_help(void)
{
Serial.println("select new eeprom size:");
Serial.println("1 - 8k x 8 (AT28C64)");
Serial.println("2 - 32k x 8 (AT28C256)");
Serial.println("any other key to cancel");
}
/* Set the EEPROM size.
*/
void cmd_size(void)
{
int ch;
cmd_size_help();
show_prompt();
ch = read_serial();
if (ch == '1') {
eeprom_size = 8192;
} else if (ch == '2') {
eeprom_size = 32768;
} else {
Serial.println("");
return;
}
Serial.print("eeprom size set to "); Serial.print(eeprom_size / 1024); Serial.print("k x 8 (");
if (eeprom_size == 8192) {
Serial.print("AT28C64");
} else if (eeprom_size == 32768) {
Serial.print("AT28C256");
}
Serial.println(")");
Serial.println("");
cmd_help();
}
/* Print software info.
*/
void cmd_version(void)
{
char version[12];
Serial.println("software version info:");
sprintf(version, "%02i\.%02i.%02i", VERSION_MAJ, VERSION_MIN, VERSION_BLD);
Serial.print("version: ");
Serial.println(version);
}
/* Disable software write protection.
*/
void cmd_unlock(void)
{
Serial.print("unlocking chip ... ");
if (unlock_chip()) {
Serial.println("ok");
} else {
Serial.println("error");
}
}
/* Read the next available byte from the serial port.
*/
int xmodem_read_serial(void)
{
unsigned long start = millis();
while (!Serial.available()) {
if (millis() - start >= XMODEM_DELAY) {
return -1;
}
}
return Serial.read();
}
/* Print packet data
*/
void print_packet_data(byte *packet) {
char s[3];
for (int i = 1; i <= XMODEM_PACKET_MAX; i++) {
sprintf(s, "%02x ", packet[i - 1]);
Serial.print(s);
if (i % 16 == 0) {
Serial.println("");
}
}
}
/* Begin xmodem transfer to the EEPROM.
*/
void cmd_xmodem(void)
{
unsigned long int start;
word addr = 0;
int retry;
int ch;
byte pkt_len;
byte prev_pkt_num = 0;
byte pkt_num = 1;
byte remote_pkt_num;
byte remote_not_pkt_num;
byte remote_chk;
byte chk;
byte response_char = CHAR_NAK;
byte error_count = 0;
static byte packet[XMODEM_PACKET_MAX];
char s[12];
char *last_error;
Serial.println("Starting xmodem");
Serial.println("Start transfer now or press 'ESC' key to abort");
while (1) {
// packet start
retry = XMODEM_RETRIES;
while (retry--) {
if (response_char) {
Serial.write(response_char);
Serial.flush();
ch = xmodem_read_serial();
switch (ch) {
case CHAR_SOH:
goto begin;
case CHAR_ESC:
Serial.println("Aborted by user");
return;
case CHAR_CAN:
ch = xmodem_read_serial();
if (ch == CHAR_CAN) {
flush_serial_input();
}
Serial.write(CHAR_ACK);
Serial.flush();
Serial.println("Remote cancelled transfer");
return;
case CHAR_EOT:
Serial.write(CHAR_NAK);
Serial.flush();
delay(1000);
ch = xmodem_read_serial();
if (ch == CHAR_EOT) {
Serial.write(CHAR_ACK);
Serial.flush();
delay(1000);
flush_serial_input();
Serial.println("transfer complete");
return;
}
ch = 0;
break;
}
}
}
begin:
if (ch != CHAR_SOH) {
response_char = CHAR_NAK;
flush_serial_input();
continue;
}
response_char = 0;
// packet number
ch = xmodem_read_serial();
if (ch == -1) {
last_error = "timeout waiting for remote_pkt_num";
error_count++;
goto skip;
}
remote_pkt_num = ch;
// complement packet number
ch = xmodem_read_serial();
if (ch == -1) {
last_error = "timeout waiting for remote_not_pkt_num";
error_count++;
goto skip;
}
remote_not_pkt_num = ch;
// packet
pkt_len = 0;
chk = 0;
while (pkt_len < XMODEM_PACKET_MAX) {
start = millis();
while (!Serial.available()) {
if (millis() - start >= XMODEM_DELAY) {
last_error = "timeout waiting for packet data";
error_count++;
goto skip;
}
}
ch = Serial.read();
if (ch == -1) {
last_error = "timeout reading packet data";
error_count++;
goto skip;
}
packet[pkt_len++] = ch;
chk += (byte)ch;
}
ch = xmodem_read_serial();
if (ch == -1) {
last_error = "timeout waiting for checksum";
error_count++;
goto skip;
}
remote_chk = ch;
// make sure data < EEPROM size
if (addr == eeprom_size) {
abort_xmodem("image is too large for eeprom");
return;
}
// check complement error
if (remote_not_pkt_num + pkt_num != 255) {
Serial.write(CHAR_NAK);
Serial.flush();
last_error = "check complement error";
error_count++;
goto skip;
}
// check duplicate packet
if (remote_pkt_num == prev_pkt_num) {
Serial.write(CHAR_ACK);
Serial.flush();
last_error = "check duplicate packet";
error_count = 0;
goto skip;
// check out of sequence
} else if (remote_pkt_num != pkt_num) {
last_error = "out of sequence";
goto skip;
}
// checksum test
if (remote_chk != chk) {
Serial.write(CHAR_NAK);
Serial.flush();
last_error = "checksum error";
error_count++;
goto skip;
}
// no errors, send an ACK and write the pages to the EEPROM
Serial.write(CHAR_ACK);
Serial.flush();
write_packet(addr, packet, XMODEM_PACKET_MAX);
if (!verify_packet(addr, packet, XMODEM_PACKET_MAX)) {
abort_xmodem("verify failed writing packet, aborting");
print_packet_data(packet);
return;
}
addr += XMODEM_PACKET_MAX;
prev_pkt_num = pkt_num;
pkt_num = remote_pkt_num + 1;
error_count = 0;
skip:
if (error_count == XMODEM_RETRIES) {
abort_xmodem(last_error);
Serial.print("prev_pkt_num = "); Serial.println(prev_pkt_num);
Serial.print("pkt_num = "); Serial.println(pkt_num);
sprintf(s, " (%02x)", remote_pkt_num);
Serial.print("remote_pkt_num = "); Serial.print(remote_pkt_num); Serial.println(s);
sprintf(s, " (%02x)", remote_not_pkt_num);
Serial.print("remote_not_pkt_num = "); Serial.print(remote_not_pkt_num); Serial.println(s);
sprintf(s, " (%02x)", remote_chk);
Serial.print("remote_chk = "); Serial.print(remote_chk); Serial.println(s);
Serial.print("chk = "); Serial.println(chk);
print_packet_data(packet);
return;
}
}
}
/* Unknown command.
*/
void cmd_unknown(char cmd)
{
Serial.print("unknown command: '");
Serial.print(cmd);
Serial.println("'");
Serial.println("use 'h' for help");
}
/* Process the specified command.
*/
void process_cmd(char cmd)
{
cmd = tolower(cmd);
Serial.println(cmd);
switch (cmd) {
case CMD_ERASE:
cmd_erase();
break;
case CMD_FILL:
cmd_fill();
break;
case CMD_HELP:
cmd_help();
break;
case CMD_LOCK:
cmd_lock();
break;
case CMD_READ:
cmd_read();
break;
case CMD_SIZE: