-
Notifications
You must be signed in to change notification settings - Fork 46
/
afterburner.ino
3094 lines (2770 loc) · 80.9 KB
/
afterburner.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
/*
(banner font: aciiart.eu)
_____________________________________________________________
| _ __ _ _ \
| / \ / _| |_ ___ _ _| |__ _ _ _ __ ___ ___ _ _ |\
| / _ \| |_| '_/ _ \| '_/ '_ \| | | | '_/ _ \/ _ \| '_/ ||
| / ___ \ _| |_| __/| | | |_) | |_| | | | | | | __/| | ||
| /_/ \_\| \__\___||_| |____/\___,_|_| |_| |_|___||_| ||
\_____________________________________________________________||
'------------------------------------------------------------'
Afterburner: GAL IC Programmer for Arduino by -= olin =-
Based on ATFblast 3.1 by Bruce Abbott
http://www.bhabbott.net.nz/atfblast.html
Based on GALBLAST by Manfred Winterhoff
http://www.armory.com/%7Erstevew/Public/Pgmrs/GAL/_ClikMe1st.htm
Based on GALmate by Yorck Thiele
https://www.ythiee.com/2021/06/06/galmate-hardware/
Supports:
* National GAL16V8
* Lattice GAL16V8A, GAL16V8B, GAL16V8D
* Lattice GAL22V10B
* Lattice GAL20V8
* Atmel ATF16V8B, ATF16V8C, ATF22V10B, ATF22V10CQZ
Requires:
* afterburner PC program to upload JED fuse map, erase, read etc.
* simple programming circuit. See: https://github.com/ole00/afterburner
* 2024-02-02 Fixed: Command 'B9' (Calibration Offset = 0,25V) doesn't work
Note: Also requires elimination of a in the PC program afterburner.c
Added: 10.0V measurement in measureVppValues(()
*/
#define VERSION "0.6.0"
//#define DEBUG_PES
//#define DEBUG_VERIFY
//ARDUINO UNO pin mapping
// GAL PIN NAME | ARDUINO UNO PIN NUMBER
//programing voltage control pin
#define PIN_VPP 11
#define PIN_SDOUT 12
#define PIN_STROBE 13
#define PIN_PV 9
#define PIN_SDIN 8
#define PIN_RA0 10
#define PIN_RA1 2
#define PIN_RA2 3
#define PIN_RA3 4
#define PIN_RA4 5
#define PIN_RA5 6
#define PIN_SCLK 7
// pin multiplex: ZIF_PIN <----> ARDUINO PIN or Shift register pin (0b1xxx)
#define PIN_ZIF3 2
#define PIN_ZIF4 0b1
#define PIN_ZIF5 0b1000
#define PIN_ZIF6 0b100
#define PIN_ZIF7 0b10
#define PIN_ZIF8 5
#define PIN_ZIF9 6
#define PIN_ZIF10 7
#define PIN_ZIF11 8
#define PIN_ZIF13 12
#define PIN_ZIF14 11
#define PIN_ZIF15 10
#define PIN_ZIF16 9
#define PIN_ZIF20 0b100000
#define PIN_ZIF21 0b10000
#define PIN_ZIF22 4
#define PIN_ZIF23 3
#define PIN_ZIF_GND_CTRL 13
#if CONFIG_IDF_TARGET_ESP32S2 == 1
//A0: VPP sense
//A3: DIGI_POT CS
#define A0 14
#define A1 15
#define A2 16
#define A3 17
//clk and dat is shared SPI bus
#define A4 18
#define A5 21
#endif
// AVR, or UNO R4
//A0: VPP sense
//A3: DIGI_POT CS
#define PIN_SHR_EN A1
#define PIN_SHR_CS A2
//clk and dat is shared SPI bus
#define PIN_SHR_CLK A4
#define PIN_SHR_DAT A5
#define COMMAND_NONE 0
#define COMMAND_UNKNOWN 1
#define COMMAND_IDENTIFY_PROGRAMMER '*'
#define COMMAND_HELP 'h'
#define COMMAND_UPLOAD 'u'
#define COMMAND_DEBUG 'd'
#define COMMAND_READ_PES 'p'
#define COMMAND_WRITE_PES 'P'
#define COMMAND_READ_FUSES 'r'
#define COMMAND_WRITE_FUSES 'w'
#define COMMAND_VERIFY_FUSES 'v'
#define COMMAND_ERASE_GAL 'c'
#define COMMAND_ERASE_GAL_ALL '~'
#define COMMAND_UTX '#'
#define COMMAND_ECHO 'e'
#define COMMAND_TEST_VOLTAGE 't'
#define COMMAND_SET_GAL_TYPE 'g'
#define COMMAND_ENABLE_CHECK_TYPE 'f'
#define COMMAND_DISABLE_CHECK_TYPE 'F'
#define COMMAND_ENABLE_SECURITY 's'
#define COMMAND_ENABLE_APD 'z'
#define COMMAND_DISABLE_APD 'Z'
#define COMMAND_MEASURE_VPP 'm'
#define COMMAND_CALIBRATE_VPP 'b'
#define COMMAND_CALIBRATION_OFFSET 'B'
#define COMMAND_JTAG_PLAYER 'j'
#define READGAL 0
#define VERIFYGAL 1
#define READPES 2
#define SCLKTEST 3
#define WRITEGAL 4
#define ERASEGAL 5
#define ERASEALL 6
#define BURNSECURITY 7
#define WRITEPES 8
#define VPPTEST 9
#define INIT 100
//check GAL type before starting an operation
#define FLAG_BIT_TYPE_CHECK (1 << 0)
// ATF16V8C flavour
#define FLAG_BIT_ATF16V8C (1 << 1)
// Keep the power-down feature enabled for ATF C GALs
#define FLAG_BIT_APD (1 << 2)
// contents of pes[3]
// Atmel PES is text string eg. 1B8V61F1 or 3Z01V22F1
// ^ ^
#define LATTICE 0xA1
#define NATIONAL 0x8F
#define SGSTHOMSON 0x20
#define ATMEL16 'V'
#define ATMEL22 '1'
#define ATMEL750 'C'
typedef enum {
UNKNOWN,
GAL16V8,
GAL18V10,
GAL20V8,
GAL20RA10,
GAL20XV10,
GAL22V10,
GAL26CV12,
GAL26V12,
GAL6001,
GAL6002,
ATF16V8B,
ATF20V8B,
ATF22V10B,
ATF22V10C,
ATF750C,
LAST_GAL_TYPE //dummy
} GALTYPE;
typedef enum {
PINOUT_UNKNOWN,
PINOUT_16V8,
PINOUT_18V10,
PINOUT_20V8,
PINOUT_22V10,
PINOUT_600,
} PINOUT;
#define BIT_NONE 0
#define BIT_ZERO 1
#define BIT_ONE 2
// config bit numbers
#define CFG_BASE_16 2048
#define CFG_BASE_18 3456
#define CFG_BASE_20 2560
#define CFG_BASE_20RA 3200
#define CFG_BASE_20XV 1600
#define CFG_BASE_22 5808
#define CFG_BASE_26CV 6344
#define CFG_BASE_26V 7800
#define CFG_BASE_600 8154
#define CFG_BASE_750 14364
#define CFG_STROBE_ROW 0
#define CFG_SET_ROW 1
#define CFG_STROBE_ROW2 3
// Atmel power-down row
#define CFG_ROW_APD 59
// Naive detection of the board's RAM size - for support of big Fuse map:
// PIN_A11 - present on MEGA (8kB) or Leonardo (2.5kB SRAM)
// _RENESAS_RA_ - Uno R4 (32kB)
#if defined(PIN_A11) || defined(_RENESAS_RA_)
#define RAM_BIG
#endif
//ESP32-S2
#if CONFIG_IDF_TARGET_ESP32S2 == 1
#define RAM_BIG
#endif
// common CFG fuse address map for cfg16V8 and cfg20V8
// the only difference is the starting address: 2048 for cfg16V8 and 2560 for cfg20V8
// total size: 82
static const unsigned char cfgV8[] PROGMEM =
{
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,
0,1,2,3,
145,
72,73,74,75,76,77,78,79,
144,
4,5,6,7,
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,
};
// common CFG fuse address map for cfg16V8AB and cfg20V8AB
// the only difference is the starting address: 2048 for cfg16V8AB and 2560 for cfg20V8AB
// total size: 82
static const unsigned char cfgV8AB[] PROGMEM =
{
0,1,2,3,
145,
72,73,74,75,
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,
76,77,78,79,
144,
4,5,6,7,
};
// common CFG fuse address map for cfg18V10
// starting address: 3456
// total size 20
static const unsigned char cfg18V10[] PROGMEM =
{
1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14, 17, 16, 19, 18
};
// common CFG fuse address map for cfg20RA10
// starting address: 3200
// total size 10
static const unsigned char cfgRA10[] PROGMEM =
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
};
// common CFG fuse address map for cfg20XV10
// starting address: 1600
// total size 31
static const unsigned char cfgXV10[] PROGMEM =
{
30,
28, 29,
20, 21, 22,
10, 11, 12, 13, 14,
0, 1, 2, 3, 4,
27, 26,
23, 24, 25,
19, 18, 17, 16, 15,
9, 8, 7, 6, 5
};
// common CFG fuse address map for cfg22V10
// starting address: 5808
// total size 20
static const unsigned char cfgV10[] PROGMEM =
{
1,0,3,2,5,4,7,6,9,8,11,10,13,12,15,14,17,16,19,18,
};
// common CFG fuse address map for cfg26CV12
// starting address: 6344
// total size 24
static const unsigned char cfg26CV12[] PROGMEM =
{
1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14, 17, 16, 19, 18, 21, 20, 23, 22
};
// common CFG fuse address map for cfg26V12
// starting address: 7800
// total size 48
static const unsigned char cfg26V12[] PROGMEM =
{
36, 24, 12, 0, 37, 25, 13, 1, 38, 26, 14, 2, 39, 27, 15, 3,
40, 28, 16, 4, 41, 29, 17, 5, 42, 30, 18, 6, 43, 31, 19, 7,
44, 32, 20, 8, 45, 33, 21, 9, 46, 34, 22, 10, 47, 35, 23, 11
};
// common CFG fuse address map for cfg6001
// starting address: 8154
// total size 68
static const unsigned char cfg6001[] PROGMEM =
{
67, 66,
25, 29, 33, 37, 41, 45, 49, 53, 57, 61,
60, 56, 52, 48, 44, 40, 36, 32, 28, 24,
62, 63, 58, 59, 54, 55, 50, 51, 46, 47,
42, 43, 38, 39, 34, 35, 30, 31, 26, 27,
2, 5, 8, 11, 14, 17, 20, 23,
0, 3, 6, 9, 12, 15, 18, 21,
22, 19, 16, 13, 10, 7, 4, 1,
64, 65
};
// common CFG fuse address map for cfg6002
// starting address: 8154
// total size 104
static const unsigned char cfg6002[] PROGMEM =
{
103, 102,
25, 29, 33, 37, 41, 45, 49, 53, 57, 61,
60, 56, 52, 48, 44, 40, 36, 32, 28, 24,
62, 63, 58, 59, 54, 55, 50, 51, 46, 47,
42, 43, 38, 39, 34, 35, 30, 31, 26, 27,
101, 100, 99, 98, 97, 96, 95, 94, 93,
92, 91, 90, 89, 88, 87, 86, 85, 84,
66, 67, 68, 69, 70, 71, 72, 73, 74,
75, 76, 77, 78, 79, 80, 81, 82, 83,
2, 5, 8, 11, 14, 17, 20, 23,
0, 3, 6, 9, 12, 15, 18, 21,
22, 19, 16, 13, 10, 7, 4, 1,
64, 65
};
// TODO: handle those:
/*
30, // 75: Security?
135, // 70: Powerdown
136, // 71: PinKeeper
137, // 72: reserved1
138, // 73: reserved2
139, // 74: reserved3
*/
static const uint8_t cfgV750[] PROGMEM = {
0, 3, 6, 9, 12, 15, 18, 21, 24, 27, // S0
1, 4, 7, 10, 13, 16, 19, 22, 25, 28, // S1
2, 5, 8, 11, 14, 17, 20, 23, 26, 29, // S2
31, 35, 39, 43, 47, 51, 55, 59, 63, 67, // S3
32, 36, 40, 44, 48, 52, 56, 60, 64, 68, // S4
33, 37, 41, 45, 49, 53, 57, 61, 65, 69, // S5
34, 38, 42, 46, 50, 54, 58, 62, 66, 70 // S6
};
// UES user electronic signature
// PES programmer electronic signature (ATF = text string, others = Vendor/Vpp/timing)
// cfg configuration bits for OLMCs
// GAL info
typedef struct
{
GALTYPE type;
unsigned char id0,id1; /* variant 1, variant 2 (eg. 16V8=0x00, 16V8A+=0x1A)*/
short fuses; /* total number of fuses */
char pins; /* number of pins on chip */
char rows; /* number of fuse rows */
unsigned char bits; /* number of fuses per row */
char uesrow; /* UES row number */
short uesfuse; /* first UES fuse number */
char uesbytes; /* number of UES bytes */
char eraserow; /* row adddeess for erase */
char eraseallrow; /* row address for erase all (also PES) */
char pesrow; /* row address for PES read/write */
char pesbytes; /* number of PES bytes */
char cfgrow; /* row address of config bits (ACW) */
unsigned short cfgbase; /* base address of the config bit numbers */
const unsigned char *cfg; /* pointer to config bit numbers */
unsigned char cfgbits; /* number of config bits */
unsigned char cfgmethod; /* strobe or set row for reading config */
PINOUT pinout;
} galinfo_t;
const static galinfo_t galInfoList[] PROGMEM =
{
// +fuses +bits +uesbytes +pesrow +cfgbase
// | +pins | +uesrow | +eraserow| +pesbytes | +cfg
// +-- type + id0 + id1 | | +rows | | +uesfuse | +eraseallrow +cfgrow | | + cfgbits +cfgmethod +pinout
// | | | | | | | | | | | | | | | | | | | |
{UNKNOWN, 0x00, 0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, NULL , 0 , 0 , PINOUT_UNKNOWN},
{GAL16V8, 0x00, 0x1A, 2194, 20, 32, 64, 32, 2056, 8, 63, 62, 58, 8, 60, CFG_BASE_16 , cfgV8AB , sizeof(cfgV8AB) , CFG_STROBE_ROW, PINOUT_16V8 },
{GAL18V10, 0x50, 0x51, 3540, 20, 36, 96, 44, 3476, 8, 61, 60, 58, 10, 16, CFG_BASE_18 , cfg18V10 , sizeof(cfg18V10) , CFG_SET_ROW , PINOUT_18V10 },
{GAL20V8, 0x20, 0x3A, 2706, 24, 40, 64, 40, 2568, 8, 63, 62, 58, 8, 60, CFG_BASE_20 , cfgV8AB , sizeof(cfgV8AB) , CFG_STROBE_ROW, PINOUT_20V8 },
{GAL20RA10, 0x60, 0x61, 3274, 24, 40, 80, 40, 3210, 8, 61, 60, 58, 10, 16, CFG_BASE_20RA, cfgRA10 , sizeof(cfgRA10) , CFG_SET_ROW , PINOUT_22V10 },
{GAL20XV10, 0x65, 0x66, 1671, 24, 40, 40, 44, 1631, 5, 61, 60, 58, 5, 16, CFG_BASE_20XV, cfgXV10 , sizeof(cfgXV10) , CFG_SET_ROW , PINOUT_22V10 },
{GAL22V10, 0x48, 0x49, 5892, 24, 44, 132, 44, 5828, 8, 61, 62, 58, 10, 16, CFG_BASE_22 , cfgV10 , sizeof(cfgV10) , CFG_SET_ROW , PINOUT_22V10 },
{GAL26CV12, 0x58, 0x59, 6432, 28, 52, 122, 52, 6368, 8, 61, 60, 58, 12, 16, CFG_BASE_26CV, cfg26CV12, sizeof(cfg26CV12), CFG_SET_ROW , PINOUT_22V10 },
{GAL26V12, 0x5D, 0x5D, 7912, 28, 52, 150, 52, 7848, 8, 61, 60, 58, 12, 16, CFG_BASE_26V , cfg26V12 , sizeof(cfg26V12) , CFG_SET_ROW , PINOUT_22V10 },
{GAL6001, 0x40, 0x41, 8294, 24, 78, 75, 97, 8222, 9, 63, 62, 96, 8, 8, CFG_BASE_600 , cfg6001 , sizeof(cfg6001) , CFG_SET_ROW , PINOUT_600 },
{GAL6002, 0x44, 0x44, 8330, 24, 78, 75, 97, 8258, 9, 63, 62, 96, 8, 8, CFG_BASE_600 , cfg6002 , sizeof(cfg6002) , CFG_SET_ROW , PINOUT_600 },
{ATF16V8B, 0x00, 0x00, 2194, 20, 32, 64, 32, 2056, 8, 63, 62, 58, 8, 60, CFG_BASE_16 , cfgV8AB , sizeof(cfgV8AB) , CFG_STROBE_ROW, PINOUT_16V8 },
{ATF20V8B, 0x00, 0x00, 2706, 24, 40, 64, 40, 2568, 8, 63, 62, 58, 8, 60, CFG_BASE_20 , cfgV8AB , sizeof(cfgV8AB) , CFG_STROBE_ROW, PINOUT_20V8 },
{ATF22V10B, 0x00, 0x00, 5892, 24, 44, 132, 44, 5828, 8, 61, 62, 58, 10, 16, CFG_BASE_22 , cfgV10 , sizeof(cfgV10) , CFG_SET_ROW , PINOUT_22V10 },
{ATF22V10C, 0x00, 0x00, 5892, 24, 44, 132, 44, 5828, 8, 61, 62, 58, 10, 16, CFG_BASE_22 , cfgV10 , sizeof(cfgV10) , CFG_SET_ROW , PINOUT_22V10 },
{ATF750C, 0x00, 0x00, 14499, 24, 84, 171, 84,14435, 8, 61, 60,127, 10, 16, CFG_BASE_750 , cfgV750 , sizeof(cfgV750) , CFG_STROBE_ROW2, PINOUT_22V10 }, // TODO: not all numbers are clear
};
galinfo_t galinfo __attribute__ ((section (".noinit"))); //preserve data between resets
#ifdef RAM_BIG
// for ATF750C
// MAXFUSES = (((171 * 84 bits) + uesbits + (10*3 + 1 + 10*4 + 5)) + 7) / 8
// (14504 + 7) / 8 = 1813
#define MAXFUSES 1813
#else
// Boards with small RAM (< 2.5kB) do not support ATF750C
// MAXFUSES calculated as the biggest required space to hold the fuse bitmap
// MAXFUSES = GAL6002 8330 bits = 8330/8 = 1041.25 bytes rounded up to 1042 bytes
//#define MAXFUSES 1042
//extra space added for sparse fusemap (just enough to fit erased ATF750C)
#define MAXFUSES 1332
#define USE_SPARSE_FUSEMAP
#endif
GALTYPE gal __attribute__ ((section (".noinit"))); //the gal device index pointing to galInfoList, value is preserved between resets
static short erasetime = 100, progtime = 100;
static uint8_t vpp = 0;
char echoEnabled;
unsigned char pes[12];
char line[32];
short lineIndex;
char endOfLine;
char mapUploaded;
char isUploading;
char uploadError;
unsigned char fusemap[MAXFUSES];
unsigned char flagBits;
char varVppExists;
uint8_t lastShiftRegVal = 0;
static void setFuseBit(unsigned short bitPos);
static unsigned short checkSum(unsigned short n);
static char checkGalTypeViaPes(void);
static void turnOff(void);
static void printFormatedNumberHex2(unsigned char num) ;
#include "aftb_vpp.h"
#include "aftb_sparse.h"
#include "aftb_seram.h"
// share fusemap buffer with jtag
#define XSVF_HEAP fusemap
#include "jtag_xsvf_player.h"
// print some help on the serial console
void printHelp(char full) {
Serial.println(F("AFTerburner v." VERSION));
// indication for PC software that the new board desgin is used
if (varVppExists) {
Serial.println(F(" varVpp "));
}
#ifdef RAM_BIG
Serial.println(F(" RAM-BIG "));
#endif
if (!full) {
Serial.println(F("type 'h' for help"));
return;
}
Serial.println(F("commands:"));
Serial.println(F(" h - print help"));
Serial.println(F(" e - toggle echo"));
Serial.println(F(" p - read & print PES"));
Serial.println(F(" r - read & print fuses"));
Serial.println(F(" u - upload fuses"));
Serial.println(F(" w - write uploaded fuses"));
Serial.println(F(" v - verify fuses"));
Serial.println(F(" c - erase chip"));
Serial.println(F(" t - test & set VPP"));
Serial.println(F(" b - calibrate VPP"));
Serial.println(F(" m - measure VPP"));
}
static void setFlagBit(uint8_t flag, uint8_t value) {
if (value) {
flagBits |= flag;
} else {
flagBits &= ~flag;
}
}
static void setPinMuxUnused(uint8_t pin, uint8_t pm) {
// set to OUTPUT during active GAL operation and to INPUT when GAL is inactive
pinMode(pin, pm);
digitalWrite(pin, LOW);
}
static void setPinMux(uint8_t pm) {
// ensure pull-up is enabled during reading and disabled when inactive on DOUT pin
uint8_t doutMode = pm == OUTPUT ? INPUT_PULLUP: INPUT;
switch (gal) {
case GAL16V8:
case ATF16V8B:
pinMode(PIN_ZIF10, INPUT); //GND via MOSFET
pinMode(PIN_ZIF11, INPUT);
pinMode(PIN_ZIF13, INPUT);
pinMode(PIN_ZIF14, INPUT);
pinMode(PIN_ZIF16, doutMode); //DOUT
// ensure ZIF10 is Grounded via transistor
digitalWrite(PIN_ZIF_GND_CTRL, pm == OUTPUT ? HIGH: LOW);
break;
case GAL18V10:
pinMode(PIN_ZIF10, INPUT); //GND via MOSFET
pinMode(PIN_ZIF11, INPUT);
pinMode(PIN_ZIF13, INPUT);
pinMode(PIN_ZIF14, INPUT);
pinMode(PIN_ZIF9, doutMode); //DOUT
// ensure ZIF10 is Grounded via transistor
digitalWrite(PIN_ZIF_GND_CTRL, pm == OUTPUT ? HIGH: LOW);
//pull down unused pins when active
setPinMuxUnused(PIN_ZIF15, pm);
setPinMuxUnused(PIN_ZIF16, pm);
break;
case GAL20V8:
case ATF20V8B:
pinMode(PIN_ZIF10, pm);
pinMode(PIN_ZIF11, pm);
pinMode(PIN_ZIF13, pm);
pinMode(PIN_ZIF14, pm);
pinMode(PIN_ZIF15, doutMode); //DOUT
pinMode(PIN_ZIF16, pm);
// ensure ZIF10 GND pull is disabled
digitalWrite(PIN_ZIF_GND_CTRL, LOW);
//pull down unused pins when active
setPinMuxUnused(PIN_ZIF14, pm);
setPinMuxUnused(PIN_ZIF16, pm);
setPinMuxUnused(PIN_ZIF23, pm);
break;
case GAL20RA10:
case GAL20XV10:
case GAL22V10:
case GAL26CV12:
case GAL26V12:
case ATF22V10B:
case ATF22V10C:
case ATF750C:
pinMode(PIN_ZIF10, pm);
pinMode(PIN_ZIF11, pm);
pinMode(PIN_ZIF13, pm);
pinMode(PIN_ZIF14, doutMode); //DOUT
pinMode(PIN_ZIF15, pm);
pinMode(PIN_ZIF16, pm);
// ensure ZIF10 GND pull is disabled
digitalWrite(PIN_ZIF_GND_CTRL, LOW);
//pull down unused pins when active
setPinMuxUnused(PIN_ZIF15, pm);
setPinMuxUnused(PIN_ZIF16, pm);
setPinMuxUnused(PIN_ZIF22, pm);
setPinMuxUnused(PIN_ZIF23, pm);
break;
case GAL6001:
case GAL6002:
pinMode(PIN_ZIF10, pm);
pinMode(PIN_ZIF11, pm);
pinMode(PIN_ZIF13, pm);
pinMode(PIN_ZIF14, doutMode); //DOUT
pinMode(PIN_ZIF15, pm);
pinMode(PIN_ZIF16, pm);
// ensure ZIF10 GND pull is disabled
digitalWrite(PIN_ZIF_GND_CTRL, LOW);
//pull down unused pins when active
setPinMuxUnused(PIN_ZIF3, pm);
setPinMuxUnused(PIN_ZIF15, pm);
setPinMuxUnused(PIN_ZIF16, pm);
setPinMuxUnused(PIN_ZIF22, pm);
break;
}
}
static void setupGpios(uint8_t pm) {
// Serial input of the GAL chip, output from Arduino
pinMode(PIN_SDIN, pm);
pinMode(PIN_STROBE, pm);
pinMode(PIN_PV, pm);
pinMode(PIN_RA0, pm);
pinMode(PIN_RA1, pm);
pinMode(PIN_RA2, pm);
pinMode(PIN_RA3, pm);
pinMode(PIN_RA4, pm);
pinMode(PIN_RA5, pm);
pinMode(PIN_SCLK, pm);
pinMode(PIN_VPP, pm);
if (varVppExists) {
pinMode(PIN_ZIF_GND_CTRL, OUTPUT);
//disconnect shift register pins (High Z) when pm == Input
digitalWrite(PIN_SHR_EN, pm == INPUT ? HIGH : LOW);
setPinMux(pm);
}
}
#define SHR_SET_BIT(X) digitalWrite(PIN_SHR_CLK, 0); \
digitalWrite(PIN_SHR_DAT, (X) ? HIGH : LOW); \
digitalWrite(PIN_SHR_CLK, 1)
static void setShiftReg(uint8_t val) {
lastShiftRegVal = val;
//assume CS is high
//ensure CLK is high (might be set low by other SPI devices)
digitalWrite(PIN_SHR_CLK, 1);
// set CS low
digitalWrite(PIN_SHR_CS, 0);
SHR_SET_BIT(val & 0b10000000);
SHR_SET_BIT(val & 0b1000000);
SHR_SET_BIT(val & 0b100000);
SHR_SET_BIT(val & 0b10000);
SHR_SET_BIT(val & 0b1000);
SHR_SET_BIT(val & 0b100);
SHR_SET_BIT(val & 0b10);
SHR_SET_BIT(val & 0b1);
digitalWrite(PIN_SHR_CS, 1);
}
// setup the Arduino board
void setup() {
// initialize serial:
Serial.begin(57600);
isUploading = 0;
endOfLine = 0;
echoEnabled = 0;
mapUploaded = 0;
lineIndex = 0;
setFlagBit(FLAG_BIT_TYPE_CHECK, 1); //do type check
//check & initialise variable voltage (old / new board design)
varVppExists = varVppInit();
// shift register
pinMode(PIN_SHR_EN, OUTPUT);
// Serial output from the GAL chip, input for Arduino
pinMode(PIN_SDOUT, INPUT);
// Set all GPIO pins to Input to prevent accidents when
// inserting the GAL IC into socket.
setupGpios(INPUT);
if (varVppExists) {
// reads the calibration values
if (varVppCheckCalibration()) {
Serial.println(F("I: VPP calib. OK"));
}
// set shift reg Chip select
pinMode(PIN_SHR_CS, OUTPUT);
digitalWrite(PIN_SHR_CS, 1); //unselect the POT's SPI bus
//setup serial RAM
if (seRamInit()) {
Serial.println(F("I: SeRAM OK"));
}
}
printHelp(0);
Serial.println(">");
}
static void sparseSetup(char clearArray){
// Note: Sparse fuse map is ignored on MCUs with big SRAM
if (gal == ATF750C) {
sparseInit(clearArray);
} else {
sparseDisable();
}
}
//copy galinfo item from the flash array into RAM backed struct
static void copyGalInfo(void) {
memcpy_P(&galinfo, &galInfoList[gal], sizeof(galinfo_t));
sparseSetup(0);
}
// read from serial line and discard the data
void readGarbage() {
while (Serial.available() > 0) {
Serial.read();
}
}
// Reads input from the serial terminal and returns the command
// which is the first character of the entered text.
char handleTerminalCommands() {
char c;
while (Serial.available() > 0) {
c = Serial.read();
line[lineIndex] = c;
if (c == '\n' || c == '\r') {
endOfLine = 1;
}
//echo input to output
else {
if (!isUploading && echoEnabled) {
Serial.print(c);
}
}
if (lineIndex >= sizeof(line)- 2) {
lineIndex = 0;
readGarbage();
Serial.println();
Serial.println(F("Error: line too long."));
} else {
lineIndex++;
}
}
if (endOfLine) {
c = COMMAND_NONE;
//single letter command entered
if (lineIndex == 2) {
c = line[0];
} else if (lineIndex > 2) {
c = line[0];
if (!isUploading || c != '#') {
// prevent 2 character commands from being flagged as invalid
if (!(c == COMMAND_SET_GAL_TYPE || c == COMMAND_CALIBRATION_OFFSET || c == COMMAND_JTAG_PLAYER)) {
c = COMMAND_UNKNOWN;
}
}
}
if (!isUploading) {
Serial.println();
line[lineIndex] = 0;
lineIndex = 0;
}
endOfLine = 0;
return c;
}
return COMMAND_NONE;
}
// Parses decimal integer number typed as 4 or 5 digits.
// Returns the number value.
unsigned short parse45dec(char i, char five) {
unsigned short v = 0;
if (five) { //containts 5 digits
v = (line[i++] - '0') * 10000;
}
v += (line[i++] - '0') * 1000;
v += (line[i++] - '0') * 100;
v += (line[i++] - '0') * 10;
v += line[i] - '0';
return v;
}
// Converts textual hex value 0-9, A-F to a number.
unsigned char toHex(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
return 0;
}
// Parses hexdecimal integer number typed as 2 digit.
// Returns the number value.
unsigned short parse2hex(char i) {
if (line[i] == '\r' || line[i] == 0 || line[i] == ' ') {
return -1;
}
unsigned short v = toHex(line[i++]) << 4;
return v + toHex(line[i]);
}
// Parses hexdecimal integer number typed as 4 digit.
// Returns the number value.
unsigned short parse4hex(char i) {
unsigned short v;
if (line[i] == '\r' || line[i] == 0 || line[i] == ' ') {
return -1;
}
v = toHex(line[i++]);
v <<= 4;
v |= toHex(line[i++]);
v <<= 4;
v |= toHex(line[i++]);
v <<= 4;
v |= toHex(line[i]);
return v ;
}
// Parses a line fed by the serial connection.
// This hnadles a primitive upload protocol that
// expects a programatic data feed - not suitable
// for human interaction.
// Data: each command on its own line
// line starts with '#' character followed by a command
// and a space. Then a command specific data follow.
// Commands:
// t <gal index>: gal type index to the GALTYPEE enum
// f <fuse index> <row>: row of fuse-map data starting on fuse bit index
// c <checksum> : checksum of the whole fuse map
// e : end ofthe upload transfer - returns to terminal
void parseUploadLine() {
switch (line[1]) {
case 'e': {
if (uploadError) {
Serial.print(F("ER upload failed"));
} else {
Serial.print(F("OK upload finished"));
}
isUploading = 0;
} break;
// gal type
case 't': {
short v = line[3] - '0';
if (v > 0 && v < LAST_GAL_TYPE) {
gal = (GALTYPE) v;
copyGalInfo();
Serial.print(F("OK gal set: "));
Serial.println((short) gal, DEC);
} else {
Serial.println(F("ER unknown gal index"));
uploadError = 1;
}
} break;
//fusemap data
case 'f': {
char i = 8;
char j;
unsigned short addr;
short v;
char fiveDigitAddr = (line[7] != ' ') ? 1 : 0;
addr = parse45dec(3, fiveDigitAddr);
i += fiveDigitAddr;
do {
v = parse2hex(i);
if (v >= 0) {
for (j = 0; j < 8; j++) {
// if fuse bit is set -> then change the fusemap
if (v & (1 << j)) {
setFuseBit(addr);
}
addr++;
}
i += 2;
}
} while (v >= 0);
//any fuse being set is considered as uploaded fuse map
mapUploaded = 1;
Serial.print(F("OK "));
Serial.println((short) addr, DEC);
} break;
//checksum
case 'c': {
unsigned short val = parse4hex(3);
unsigned char apdFuse = (flagBits & FLAG_BIT_APD) ? 1 : 0;
unsigned short cs = checkSum(galinfo.fuses + apdFuse);
if (cs == val) {
Serial.println(F("OK checksum matches"));
// Conditioning jed files might not have any fuse set, so as long as
// they supply empty checksum (C0000) the upload is OK.
mapUploaded = 1;
} else {
uploadError = 1;
Serial.print(F("ER checksum:"));
Serial.print(cs, HEX);
Serial.print(F(" expected:"));
Serial.println(val, HEX);
}
} break;
// PES
case 'p': {
uint8_t i = 0;
uint8_t j = 3;
while (i < 8) {
pes[i] = parse2hex(j);
i++;
j+=3; //AB:00:... - 3 characters per one PES byte
}
} break;
default:
uploadError = 1;
Serial.println(F("ER unknown upload cmd"));
}
lineIndex = 0;
}
// *********************************************************
// set/reset individual pins of GAL
static void setVCC(char on) {
//no control for turning the voltage on of
//it is assumed the voltage is always on
}
static void setVPP(char on) {
// new board desgin
if (varVppExists) {
uint8_t v = VPP_11V0;
// when PES is read the VPP is not determined via PES
if (on == READPES) {
if (gal == ATF16V8B || gal == ATF20V8B || gal == ATF22V10B || gal == ATF22V10C || gal == ATF750C) {
v = VPP_10V5;
} else {
v = VPP_11V5;
}
#if 0
Serial.print(F("VPP index="));
Serial.println(v);
#endif
} else {
//safety check
if (vpp < 36) {
vpp = 36; //9V
} else
if (vpp > 66) {
vpp = 48; //12V
}
v = (vpp >> 1) - 18; // 18: 2 * 9V, resolution 0.5V (not 0.25V) hence 'vpp >> 1'
#if 0
Serial.print(F("setVPP "));
Serial.print(vpp);
Serial.print(F(" index="));
Serial.println(v);
#endif
}
varVppSet(on ? v : VPP_5V0);
delay(50); //settle the voltage
}
// old board design
else {
//programming voltage is controlled by VPP_PIN,
//but the programming voltage must be set manually by user turning a Pot
digitalWrite(PIN_VPP, on ? 1 : 0);
//Serial.print(F("VPP set to:"));
//Serial.println( on ? "12V": "5V");
delay(10);
}
}
static void setSTB(char on) {
if (varVppExists) {
const PINOUT p = galinfo.pinout;
uint8_t pin = PIN_ZIF13;
if (p == PINOUT_16V8) {
pin = PIN_ZIF15;
} else
if (p == PINOUT_18V10) {
pin = PIN_ZIF8;
}
digitalWrite(pin, on ? 1:0);