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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
|
/**
* \file progP18F.c - algorithms to program the PIC18 family of microcontrollers
* Copyright (C) 2009-2022 Alberto Maccioni
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
* or see <http://www.gnu.org/licenses/>
*/
//This cannot be executed conditionally on MSVC
//#include "stdafx.h"
#include "common.h"
struct ID18{
int id;
char *device;
int revmask;
} PIC18LIST[]={
{0x0400,"18F252/2539 rev%d\r\n",0x1F},
{0x0420,"18F452/4539 rev%d\r\n",0x1F},
{0x0480,"18F242/2439 rev%d\r\n",0x1F},
{0x04A0,"18F442/4439 rev%d\r\n",0x1F},
{0x0500,"18F2320 rev%d\r\n",0x1F},
{0x0520,"18F4320 rev%d\r\n",0x1F},
{0x0580,"18F2220 rev%d\r\n",0x1F},
{0x05A0,"18F4220 rev%d\r\n",0x1F},
{0x0600,"18F8720 rev%d\r\n",0x1F},
{0x0620,"18F6720 rev%d\r\n",0x1F},
{0x0640,"18F8620 rev%d\r\n",0x1F},
{0x0660,"18F6620 rev%d\r\n",0x1F},
{0x07C0,"18F1320 rev%d\r\n",0x1F},
{0x07E0,"18F1220 rev%d\r\n",0x1F},
{0x0800,"18F248 rev%d\r\n",0x1F},
{0x0820,"18F448 rev%d\r\n",0x1F},
{0x0840,"18F258 rev%d\r\n",0x1F},
{0x0860,"18F458 rev%d\r\n",0x1F},
{0x0880,"18F4431 rev%d\r\n",0x1F},
{0x08A0,"18F4331 rev%d\r\n",0x1F},
{0x08C0,"18F2431 rev%d\r\n",0x1F},
{0x08E0,"18F2331 rev%d\r\n",0x1F},
{0x0B20,"18F6520 rev%d\r\n",0x1F},
{0x0B00,"18F8520 rev%d\r\n",0x1F},
{0x0C00,"18F4620 rev%d\r\n",0x1F},
{0x0C20,"18F4610 rev%d\r\n",0x1F},
{0x0C40,"18F4525 rev%d\r\n",0x1F},
{0x0C60,"18F4515 rev%d\r\n",0x1F},
{0x0C80,"18F2620 rev%d\r\n",0x1F},
{0x0CA0,"18F2610 rev%d\r\n",0x1F},
{0x0CC0,"18F2525 rev%d\r\n",0x1F},
{0x0CE0,"18F2515 rev%d\r\n",0x1F},
{0x0E80,"18F4680 rev%d\r\n",0x1F},
{0x0EA0,"18F4585 rev%d\r\n",0x1F},
{0x0EC0,"18F2680 rev%d\r\n",0x1F},
{0x0EE0,"18F2585 rev%d\r\n",0x1F},
{0x1081,"18F4523 rev%d\r\n",0x0F},
{0x1080,"18F4520 rev%d\r\n",0x0F},
{0x10A0,"18F4510 rev%d\r\n",0x1F},
{0x10C1,"18F4423 rev%d\r\n",0x0F},
{0x10C0,"18F4420 rev%d\r\n",0x0F},
{0x10E0,"18F4410 rev%d\r\n",0x1F},
{0x1100,"18F2523 rev%d\r\n",0x0F},
{0x1100,"18F2520 rev%d\r\n",0x0F},
{0x1120,"18F2510 rev%d\r\n",0x1F},
{0x1140,"18F2423 rev%d\r\n",0x0F},
{0x1140,"18F2420 rev%d\r\n",0x0F},
{0x1160,"18F2410 rev%d\r\n",0x1F},
{0x1200,"18F4550 rev%d\r\n",0x1F},
{0x1220,"18F4455 rev%d\r\n",0x1F},
{0x1240,"18F2550 rev%d\r\n",0x1F},
{0x1260,"18F2455 rev%d\r\n",0x1F},
{0x1340,"18F6527 rev%d\r\n",0x1F},
{0x1360,"18F8527 rev%d\r\n",0x1F},
{0x1380,"18F6622 rev%d\r\n",0x1F},
{0x13A0,"18F8622 rev%d\r\n",0x1F},
{0x13C0,"18F6627 rev%d\r\n",0x1F},
{0x13E0,"18F8627 rev%d\r\n",0x1F},
{0x1400,"18F6722 rev%d\r\n",0x1F},
{0x1420,"18F8722 rev%d\r\n",0x1F},
{0x1520,"18F65J10 rev%d\r\n",0x1F},
{0x1540,"18F65J15 rev%d\r\n",0x1F},
{0x1560,"18F66J10 rev%d\r\n",0x1F},
{0x1580,"18F66J15 rev%d\r\n",0x1F},
{0x15A0,"18F67J10 rev%d\r\n",0x1F},
{0x15E0,"18F85J10 rev%d\r\n",0x1F},
{0x1700,"18F85J15 rev%d\r\n",0x1F},
{0x1720,"18F86J10 rev%d\r\n",0x1F},
{0x1740,"18F86J15 rev%d\r\n",0x1F},
{0x1760,"18F87J10 rev%d\r\n",0x1F},
{0x1800,"18F66J60 rev%d\r\n",0x1F},
{0x1820,"18F86J60 rev%d\r\n",0x1F},
{0x1840,"18F96J60 rev%d\r\n",0x1F},
{0x1A80,"18F4580 rev%d\r\n",0x1F},
{0x1AA0,"18F4480 rev%d\r\n",0x1F},
{0x1AC0,"18F2580 rev%d\r\n",0x1F},
{0x1AE0,"18F2480 rev%d\r\n",0x1F},
{0x1C00,"18F25J10 rev%d\r\n",0x1F},
{0x1C20,"18F45J10 rev%d\r\n",0x1F},
{0x1C40,"18LF25J10 rev%d\r\n",0x1F},
{0x1C60,"18LF45J10 rev%d\r\n",0x1F},
{0x1D00,"18F24J10 rev%d\r\n",0x1F},
{0x1D20,"18F44J10 rev%d\r\n",0x1F},
{0x1D40,"18LF24J10 rev%d\r\n",0x1F},
{0x1D60,"18LF44J10 rev%d\r\n",0x1F},
{0x1E00,"18F1230 rev%d\r\n",0x1F},
{0x1E20,"18F1330 rev%d\r\n",0x1F},
{0x1F00,"18F66J65 rev%d\r\n",0x1F},
{0x1F20,"18F67J60 rev%d\r\n",0x1F},
{0x1F40,"18F86J65 rev%d\r\n",0x1F},
{0x1F60,"18F87J60 rev%d\r\n",0x1F},
{0x1F80,"18F96J65 rev%d\r\n",0x1F},
{0x1FA0,"18F97J60 rev%d\r\n",0x1F},
{0x1FE0,"18F1330-ICD rev%d\r\n",0x1F},
{0x2000,"18F46K20 rev%d\r\n",0x1F},
{0x2020,"18F26K20 rev%d\r\n",0x1F},
{0x2040,"18F45K20 rev%d\r\n",0x1F},
{0x2060,"18F25K20 rev%d\r\n",0x1F},
{0x2080,"18F44K20 rev%d\r\n",0x1F},
{0x20A0,"18F24K20 rev%d\r\n",0x1F},
{0x20C0,"18F43K20 rev%d\r\n",0x1F},
{0x20E0,"18F23K20 rev%d\r\n",0x1F},
{0x2100,"18F4321 rev%d\r\n",0x1F},
{0x2120,"18F2321 rev%d\r\n",0x1F},
{0x2140,"18F4221 rev%d\r\n",0x1F},
{0x2160,"18F2221 rev%d\r\n",0x1F},
{0x2400,"18F4450 rev%d\r\n",0x1F},
{0x2420,"18F2450 rev%d\r\n",0x1F},
{0x2700,"18F2682 rev%d\r\n",0x1F},
{0x2720,"18F2685 rev%d\r\n",0x1F},
{0x2740,"18F4682 rev%d\r\n",0x1F},
{0x2760,"18F4685 rev%d\r\n",0x1F},
{0x2A00,"18F4553 rev%d\r\n",0x1F},
{0x2A20,"18F4458 rev%d\r\n",0x1F},
{0x2A40,"18F2553 rev%d\r\n",0x1F},
{0x2A60,"18F2458 rev%d\r\n",0x1F},
{0x3800,"18F63J90 rev%d\r\n",0x1F},
{0x3820,"18F64J90 rev%d\r\n",0x1F},
{0x3860,"18F65J90 rev%d\r\n",0x1F},
{0x3880,"18F83J90 rev%d\r\n",0x1F},
{0x38A0,"18F84J90 rev%d\r\n",0x1F},
{0x38E0,"18F85J90 rev%d\r\n",0x1F},
{0x3900,"18F63J11 rev%d\r\n",0x1F},
{0x3920,"18F64J11 rev%d\r\n",0x1F},
{0x3960,"18F65J11 rev%d\r\n",0x1F},
{0x3980,"18F83J11 rev%d\r\n",0x1F},
{0x39A0,"18F84J11 rev%d\r\n",0x1F},
{0x39E0,"18F85J11 rev%d\r\n",0x1F},
{0x4100,"18F65J50 rev%d\r\n",0x1F},
{0x4140,"18F66J50 rev%d\r\n",0x1F},
{0x4160,"18F66J55 rev%d\r\n",0x1F},
{0x4180,"18F67J50 rev%d\r\n",0x1F},
{0x41A0,"18F85J50 rev%d\r\n",0x1F},
{0x41E0,"18F86J50 rev%d\r\n",0x1F},
{0x4200,"18F86J55 rev%d\r\n",0x1F},
{0x4220,"18F87J50 rev%d\r\n",0x1F},
{0x4440,"18F66J11 rev%d\r\n",0x1F},
{0x4460,"18F66J16 rev%d\r\n",0x1F},
{0x4480,"18F67J11 rev%d\r\n",0x1F},
{0x44E0,"18F86J11 rev%d\r\n",0x1F},
{0x4500,"18F86J16 rev%d\r\n",0x1F},
{0x4520,"18F87J11 rev%d\r\n",0x1F},
{0x4700,"18LF13K50 rev%d\r\n",0x1F},
{0x4720,"18LF14K50 rev%d\r\n",0x1F},
{0x4740,"18F13K50 rev%d\r\n",0x1F},
{0x4760,"18F14K50 rev%d\r\n",0x1F},
{0x49C0,"18F6628 rev%d\r\n",0x1F},
{0x49E0,"18F8628 rev%d\r\n",0x1F},
{0x4A00,"18F6723 rev%d\r\n",0x1F},
{0x4A20,"18F8723 rev%d\r\n",0x1F},
{0x4C00,"18F24J50 rev%d\r\n",0x1F},
{0x4C20,"18F25J50 rev%d\r\n",0x1F},
{0x4C40,"18F26J50 rev%d\r\n",0x1F},
{0x4C60,"18F44J50 rev%d\r\n",0x1F},
{0x4C80,"18F45J50 rev%d\r\n",0x1F},
{0x4D80,"18F24J11 rev%d\r\n",0x1F},
{0x4DA0,"18F25J11 rev%d\r\n",0x1F},
{0x4DC0,"18F26J11 rev%d\r\n",0x1F},
{0x4DE0,"18F44J11 rev%d\r\n",0x1F},
{0x4E00,"18F45J11 rev%d\r\n",0x1F},
{0x4E20,"18F46J11 rev%d\r\n",0x1F},
{0x4E60,"18LF25J11 rev%d\r\n",0x1F},
{0x4E80,"18LF26J11 rev%d\r\n",0x1F},
{0x4EA0,"18LF44J11 rev%d\r\n",0x1F},
{0x4EC0,"18LF45J11 rev%d\r\n",0x1F},
{0x4EE0,"18LF46J11 rev%d\r\n",0x1F},
{0x4F20,"18F14K22 rev%d\r\n",0x1F},
{0x4F40,"18F13K22 rev%d\r\n",0x1F},
{0x4F60,"18LF14K22 rev%d\r\n",0x1F},
{0x4F80,"18LF13K22 rev%d\r\n",0x1F},
{0x5000,"18F66J90 rev%d\r\n",0x1F},
{0x5020,"18F67J90 rev%d\r\n",0x1F},
{0x5040,"18F66J93/86J72 rev%d\r\n",0x1F},
{0x5060,"18F67J93/87J72 rev%d\r\n",0x1F},
{0x5080,"18F86J90 rev%d\r\n",0x1F},
{0x50A0,"18F87J90 rev%d\r\n",0x1F},
{0x50C0,"18F86J93 rev%d\r\n",0x1F},
{0x50E0,"18F87J93 rev%d\r\n",0x1F},
{0x5400,"18F46K22 rev%d\r\n",0x1F},
{0x5420,"18LF46K22 rev%d\r\n",0x1F},
{0x5440,"18F26K22 rev%d\r\n",0x1F},
{0x5460,"18LF26K22 rev%d\r\n",0x1F},
{0x5500,"18F45K22 rev%d\r\n",0x1F},
{0x5520,"18LF45K22 rev%d\r\n",0x1F},
{0x5540,"18F25K22 rev%d\r\n",0x1F},
{0x5560,"18LF25K22 rev%d\r\n",0x1F},
{0x5600,"18F44K22 rev%d\r\n",0x1F},
{0x5620,"18LF44K22 rev%d\r\n",0x1F},
{0x5640,"18F24K22 rev%d\r\n",0x1F},
{0x5660,"18LF24K22 rev%d\r\n",0x1F},
{0x5700,"18F43K22 rev%d\r\n",0x1F},
{0x5720,"18LF43K22 rev%d\r\n",0x1F},
{0x5740,"18F23K22 rev%d\r\n",0x1F},
{0x5760,"18LF23K22 rev%d\r\n",0x1F},
{0x5820,"18F26J53 rev%d\r\n",0x1F},
{0x5860,"18F27J53 rev%d\r\n",0x1F},
{0x58A0,"18F46J53 rev%d\r\n",0x1F},
{0x58E0,"18F47J53 rev%d\r\n",0x1F},
{0x5920,"18F26J13 rev%d\r\n",0x1F},
{0x5960,"18F27J13 rev%d\r\n",0x1F},
{0x59A0,"18F46J13 rev%d\r\n",0x1F},
{0x59E0,"18F47J13 rev%d\r\n",0x1F},
{0x5A20,"18LF26J53 rev%d\r\n",0x1F},
{0x5A60,"18LF27J53 rev%d\r\n",0x1F},
{0x5AA0,"18LF46J53 rev%d\r\n",0x1F},
{0x5AE0,"18LF47J53 rev%d\r\n",0x1F},
{0x5B20,"18LF26J13 rev%d\r\n",0x1F},
{0x5B60,"18LF27J13 rev%d\r\n",0x1F},
{0x5BA0,"18LF46J13 rev%d\r\n",0x1F},
{0x5BE0,"18LF47J13 rev%d\r\n",0x1F},
{0x5C00,"18F45K50 rev%d\r\n",0x1F},
{0x5C20,"18F25K50 rev%d\r\n",0x1F},
{0x5C60,"18F24K50 rev%d\r\n",0x1F},
{0x5C80,"18LF45K50 rev%d\r\n",0x1F},
{0x5CA0,"18LF25K50 rev%d\r\n",0x1F},
{0x5CE0,"18LF24K50 rev%d\r\n",0x1F},
{0x5D00,"18F46K50 rev%d\r\n",0x1F},
{0x5D20,"18F26K50 rev%d\r\n",0x1F},
{0x5D40,"18LF46K50 rev%d\r\n",0x1F},
{0x5D60,"18LF26K50 rev%d\r\n",0x1F},
{0x60E0,"18F66K80 rev%d\r\n",0x1F},
{0x6100,"18F46K80 rev%d\r\n",0x1F},
{0x6120,"18F26K80 rev%d\r\n",0x1F},
{0x6140,"18F65K80 rev%d\r\n",0x1F},
{0x6160,"18F45K80 rev%d\r\n",0x1F},
{0x6180,"18F25K80 rev%d\r\n",0x1F},
{0x61C0,"18LF66K80 rev%d\r\n",0x1F},
{0x61E0,"18LF46K80 rev%d\r\n",0x1F},
{0x6200,"18LF26K80 rev%d\r\n",0x1F},
{0x6220,"18LF65K80 rev%d\r\n",0x1F},
{0x6240,"18LF45K80 rev%d\r\n",0x1F},
{0x6260,"18LF25K80 rev%d\r\n",0x1F},
{0x6B80,"18F57K42 rev%c%d\r\n",0xFFFF},
{0x6BA0,"18F56K42 rev%c%d\r\n",0xFFFF},
{0x6BC0,"18F55K42 rev%c%d\r\n",0xFFFF},
{0x6BE0,"18F47K42 rev%c%d\r\n",0xFFFF},
{0x6C00,"18F46K42 rev%c%d\r\n",0xFFFF},
{0x6C20,"18F45K42 rev%c%d\r\n",0xFFFF},
{0x6C40,"18F27K42 rev%c%d\r\n",0xFFFF},
{0x6C60,"18F26K42 rev%c%d\r\n",0xFFFF},
{0x6CC0,"18LF57K42 rev%c%d\r\n",0xFFFF},
{0x6CE0,"18LF56K42 rev%c%d\r\n",0xFFFF},
{0x6D00,"18LF55K42 rev%c%d\r\n",0xFFFF},
{0x6D20,"18LF47K42 rev%c%d\r\n",0xFFFF},
{0x6D40,"18LF46K42 rev%c%d\r\n",0xFFFF},
{0x6D60,"18LF45K42 rev%c%d\r\n",0xFFFF},
{0x6D80,"18LF27K42 rev%c%d\r\n",0xFFFF},
{0x6DA0,"18LF26K42 rev%c%d\r\n",0xFFFF},
{0x6EC0,"18F26K83 rev%c%d\r\n",0xFFFF},
{0x6EE0,"18F25K83 rev%c%d\r\n",0xFFFF},
{0x6F00,"18LF26K83 rev%c%d\r\n",0xFFFF},
{0x6F20,"18LF25K83 rev%c%d\r\n",0xFFFF},
{0x73C0,"18F25Q43 rev%c%d\r\n",0xFFFF},
{0x73E0,"18F45Q43 rev%c%d\r\n",0xFFFF},
{0x7400,"18F55Q43 rev%c%d\r\n",0xFFFF},
{0x7420,"18F26Q43 rev%c%d\r\n",0xFFFF},
{0x7440,"18F46Q43 rev%c%d\r\n",0xFFFF},
{0x7460,"18F56Q43 rev%c%d\r\n",0xFFFF},
{0x7480,"18F27Q43 rev%c%d\r\n",0xFFFF},
{0x74A0,"18F47Q43 rev%c%d\r\n",0xFFFF},
{0x74C0,"18F57Q43 rev%c%d\r\n",0xFFFF},
{0x74E0,"18F15Q41 rev%c%d\r\n",0xFFFF},
{0x7500,"18F05Q41 rev%c%d\r\n",0xFFFF},
{0x7520,"18F14Q41 rev%c%d\r\n",0xFFFF},
{0x7540,"18F04Q41 rev%c%d\r\n",0xFFFF},
{0x7560,"18F16Q41 rev%c%d\r\n",0xFFFF},
{0x7580,"18F06Q41 rev%c%d\r\n",0xFFFF},
{0x75A0,"18F16Q40 rev%c%d\r\n",0xFFFF},
{0x75C0,"18F06Q40 rev%c%d\r\n",0xFFFF},
{0x75E0,"18F15Q40 rev%c%d\r\n",0xFFFF},
{0x7600,"18F05Q40 rev%c%d\r\n",0xFFFF},
{0x7620,"18F14Q40 rev%c%d\r\n",0xFFFF},
{0x7640,"18F04Q40 rev%c%d\r\n",0xFFFF},
{0x7660,"18F24Q71 rev%c%d\r\n",0xFFFF},
{0x76A0,"18F25Q71 rev%c%d\r\n",0xFFFF},
{0x76E0,"18F26Q71 rev%c%d\r\n",0xFFFF},
{0x7720,"18F46Q71 rev%c%d\r\n",0xFFFF},
{0x7760,"18F56Q71 rev%c%d\r\n",0xFFFF},
{0x77A0,"18F44Q71 rev%c%d\r\n",0xFFFF},
{0x77E0,"18F45Q71 rev%c%d\r\n",0xFFFF},
{0x7820,"18F54Q71 rev%c%d\r\n",0xFFFF},
{0x7860,"18F55Q71 rev%c%d\r\n",0xFFFF},
{0x9900,"18F26Q84 rev%c%d\r\n",0xFFFF},
{0x9901,"18F46Q84 rev%c%d\r\n",0xFFFF},
{0x9902,"18F56Q84 rev%c%d\r\n",0xFFFF},
{0x9903,"18F27Q84 rev%c%d\r\n",0xFFFF},
{0x9904,"18F47Q84 rev%c%d\r\n",0xFFFF},
{0x9905,"18F57Q84 rev%c%d\r\n",0xFFFF},
{0x9906,"18F26Q83 rev%c%d\r\n",0xFFFF},
{0x9907,"18F46Q83 rev%c%d\r\n",0xFFFF},
{0x9908,"18F56Q83 rev%c%d\r\n",0xFFFF},
{0x9909,"18F27Q83 rev%c%d\r\n",0xFFFF},
{0x990A,"18F47Q83 rev%c%d\r\n",0xFFFF},
{0x990B,"18F57Q83 rev%c%d\r\n",0xFFFF},
};
void PIC18_ID(int id)
{
char s[128];
int i;
for(i=0;i<sizeof(PIC18LIST)/sizeof(PIC18LIST[0]);i++){
if(PIC18LIST[i].revmask!=0xFFFF&&PIC18LIST[i].id==(id&~PIC18LIST[i].revmask)){
sprintf(s,PIC18LIST[i].device,id&PIC18LIST[i].revmask);
PrintMessage(s);
return;
}
else if(PIC18LIST[i].revmask==0xFFFF&&PIC18LIST[i].id==(id&0xFFFF)){
int MJrev=(id>>22)&0x3F;
int MNrev=(id>>16)&0x3F;
sprintf(s,PIC18LIST[i].device,MJrev+'A',MNrev);
PrintMessage(s);
return;
}
}
sprintf(s,"%s",strings[S_nodev]); //"Unknown device\r\n");
PrintMessage(s);
}
void DisplayCODE18F(int dim){
// display PIC18F CODE memory
char s[256]="",t[256]="";
char* aux=(char*)malloc((dim/COL+1)*(16+COL*6));
aux[0]=0;
int valid=0,empty=1,i,j,lines=0;
for(i=0;i<dim&&i<size;i+=COL*2){
valid=0;
for(j=i;j<i+COL*2&&j<dim;j++){
sprintf(t,"%02X ",memCODE[j]);
strcat(s,t);
if(memCODE[j]<0xff) valid=1;
}
if(valid){
sprintf(t,"%04X: %s\r\n",i,s);
strcat(aux,t);
empty=0;
lines++;
if(lines>500){ //limit number of lines printed
strcat(aux,"(...)\r\n");
i=(dim<size?dim:size)-COL*4;
lines=490;
}
}
s[0]=0;
}
if(empty) PrintMessage(strings[S_Empty]); //empty
else PrintMessage(aux);
free(aux);
}
void Read18Fx(int dim,int dim2,int options)
// read 16 bit PIC 18Fxxxx
// dim=program size (bytes) dim2=eeprom size (bytes)
// options:
// bit [3:0]
// 0 = vdd before vpp (12V)
// 1 = vdd before vpp (9V)
// 2 = low voltage entry with 32 bit key
// 3 = vdd before vpp (9V) with 32 bit key
// bit [7:4]
// 0 = normal control registers address
// 1 = new control registers address (18FxxK80)
{
int k=0,k2=0,z=0,i,j;
int entry=options&0xF;
int newAddr=(options>>4)&0xF;
if(dim>0x1FFFFF||dim<0){
PrintMessage(strings[S_CodeLim]); //"Code size out of limits\r\n"
return;
}
if(dim2>0x800||dim2<0){
PrintMessage(strings[S_EELim]); //"EEPROM size out of limits\r\n"
return;
}
if(entry>0&&!CheckV33Regulator()){
PrintMessage(strings[S_noV33reg]); //Can't find 3.3V expansion board
return;
}
double vpp=-1;
if(entry==0) vpp=12;
else if(entry==1||entry==3) vpp=8.5;
if(!StartHVReg(vpp)){
PrintMessage(strings[S_HVregErr]); //"HV regulator error\r\n"
return;
}
if(saveLog){
OpenLogFile(); //"log.txt"
fprintf(logfile,"Read18F(%d,%d,%d) (0x%X,0x%X,0x%X)\n",dim,dim2,options,dim,dim2,options);
}
size=dim;
sizeEE=dim2;
if(memCODE) free(memCODE);
memCODE=(unsigned char*)malloc(size); //CODE
if(memEE) free(memEE);
memEE=(unsigned char*)malloc(sizeEE); //EEPROM
for(j=0;j<8;j++) memID[j]=0xFF;
for(j=0;j<14;j++) memCONFIG[j]=0xFF;
unsigned int start=GetTickCount();
j=0;
bufferU[j++]=SET_PARAMETER;
bufferU[j++]=SET_T1T2;
bufferU[j++]=1; //T1=1u
bufferU[j++]=100; //T2=100u
bufferU[j++]=SET_PARAMETER;
bufferU[j++]=SET_T3;
bufferU[j++]=2000>>8;
bufferU[j++]=2000&0xff;
bufferU[j++]=EN_VPP_VCC; //enter program mode
bufferU[j++]=0x0;
bufferU[j++]=SET_CK_D;
bufferU[j++]=0x0;
bufferU[j++]=EN_VPP_VCC; //VDD
bufferU[j++]=0x1;
bufferU[j++]=NOP;
bufferU[j++]=EN_VPP_VCC; //VDD+VPP
bufferU[j++]=0x5;
if(entry==2){ //LV entry with key
bufferU[j++]=EN_VPP_VCC; //VDD
bufferU[j++]=0x1;
}
if(entry==2||entry==3){ //entry with key
bufferU[j++]=WAIT_T3;
bufferU[j++]=WAIT_T3;
bufferU[j++]=TX16;
bufferU[j++]=2;
bufferU[j++]=0x4D;
bufferU[j++]=0x43;
bufferU[j++]=0x48;
bufferU[j++]=0x50;
bufferU[j++]=EN_VPP_VCC; //VDD+VPP
bufferU[j++]=0x5;
bufferU[j++]=WAIT_T3;
}
bufferU[j++]=WAIT_T3;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //3F
bufferU[j++]=0x3F;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //-> TBLPTRU
bufferU[j++]=0xF8;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //FF
bufferU[j++]=0xFF;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //-> TBLPTRH
bufferU[j++]=0xF7;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //FE
bufferU[j++]=0xFE;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //-> TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=TBLR_INC_N; //DevID1-2 0x3FFFFE-F
bufferU[j++]=2;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //CLRF TBLPTRU
bufferU[j++]=0xF8;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //CLRF TBLPTRH
bufferU[j++]=0xF7;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //CLRF TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(entry==2?11:4);
for(z=0;bufferI[z]!=TBLR_INC_N&&z<DIMBUF;z++);
if(z<DIMBUF-3){
PrintMessage2(strings[S_DevID2],bufferI[z+3],bufferI[z+2]); //"DevID: 0x%02X%02X\r\n"
PIC18_ID(bufferI[z+2]+(bufferI[z+3]<<8));
}
//****************** read code ********************
PrintMessage(strings[S_CodeReading1]); //code read ...
PrintStatusSetup();
if(saveLog) fprintf(logfile,"Read code\n");
for(i=0,j=0;i<dim;i+=DIMBUF-4){
bufferU[j++]=TBLR_INC_N;
bufferU[j++]=i<dim-(DIMBUF-4)?DIMBUF-4:dim-i;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
for(z=0;bufferI[z]!=TBLR_INC_N&&z<DIMBUF;z++);
for(j=z+2;(j<z+2+bufferI[z+1])&&j<DIMBUF;j++) memCODE[k++]=bufferI[j];
PrintStatus(strings[S_CodeReading2],i*100/(dim+dim2),i); //"Read: %d%%, addr. %05X"
if(RWstop) i=dim;
j=0;
if(saveLog){
fprintf(logfile,strings[S_Log7],i,i,k,k); //"i=%d(0x%X), k=%d(0x%X)\n"
}
}
PrintStatusEnd();
if(k!=dim){
PrintMessage("\r\n");
PrintMessage2(strings[S_ReadCodeErr2],dim,k); //"Error reading code area, requested %d bytes, read %d\r\n"
}
else PrintMessage(strings[S_Compl]);
//****************** read config area ********************
if(saveLog) fprintf(logfile,"Read config\n");
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //TBLPTRU ID 0x200000
bufferU[j++]=0x20; //TBLPTRU ID 0x200000
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //TBLPTRU
bufferU[j++]=0xF8; //TBLPTRU
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //TBLPTRH
bufferU[j++]=0xF7; //TBLPTRH
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //TBLPTRL
bufferU[j++]=0xF6; //TBLPTRL
bufferU[j++]=TBLR_INC_N;
bufferU[j++]=8;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //TBLPTRU CONFIG 0x300000
bufferU[j++]=0x30; //TBLPTRU CONFIG 0x300000
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //TBLPTRU
bufferU[j++]=0xF8; //TBLPTRU
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //TBLPTRH
bufferU[j++]=0xF7; //TBLPTRH
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //TBLPTRL
bufferU[j++]=0xF6; //TBLPTRL
bufferU[j++]=TBLR_INC_N;
bufferU[j++]=14;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(8);
for(z=0;bufferI[z]!=TBLR_INC_N&&z<DIMBUF-28;z++);
if(z<DIMBUF-28){
for(i=0;i<8;i++) memID[k2++]=bufferI[z+i+2];
for(;i<14+8;i++) memCONFIG[-8+k2++]=bufferI[z+i+8];
}
j=0;
if(saveLog){
fprintf(logfile,strings[S_Log7],i,i,k2,k2); //"i=%d(0x%X), k=%d(0x%X)\n"
}
if(k2!=22){
PrintMessage2(strings[S_ReadConfigErr],22,k2); //"Error reading config area, requested %d bytes, read %d\r\n"
}
//****************** read eeprom ********************
if(dim2){
PrintMessage(strings[S_ReadEE]); //read eeprom ...
PrintStatusSetup();
if(saveLog) fprintf(logfile,"Read EEPROM\n");
bufferU[j++]=CORE_INS;
bufferU[j++]=0x9E; //EEPGD=0
bufferU[j++]=newAddr==0?0xA6:0x7F;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x9C; //CFGS=0
bufferU[j++]=newAddr==0?0xA6:0x7F;
for(k2=0,i=0;i<dim2;i++){
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW
bufferU[j++]=i&0xFF;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF
bufferU[j++]=newAddr==0?0xA9:0x74; //ADDR
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW
bufferU[j++]=(i>>8)&0xFF;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF
bufferU[j++]=newAddr==0?0xAA:0x75; //ADDRH
bufferU[j++]=CORE_INS;
bufferU[j++]=0x80; //RD=1
bufferU[j++]=newAddr==0?0xA6:0x7F;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x50; //MOVF EEDATA,w
bufferU[j++]=newAddr==0?0xA8:0x73;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TABLAT
bufferU[j++]=0xF5;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x00; //NOP
bufferU[j++]=0x00;
bufferU[j++]=SHIFT_TABLAT;
if(j>DIMBUF-26||i==dim2-1){
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(10);
for(z=0;z<DIMBUF-1;z++){
if(bufferI[z]==SHIFT_TABLAT){
memEE[k2++]=bufferI[z+1];
z+=8;
}
}
PrintStatus(strings[S_CodeReading],(i+dim)*100/(dim+dim2),i); //"Read: %d%%, addr. %03X"
if(RWstop) i=dim2;
j=0;
if(saveLog){
fprintf(logfile,strings[S_Log7],i,i,k2,k2); //"i=%d(0x%X), k=%d(0x%X)\n"
}
}
}
PrintStatusEnd();
if(k2!=dim2){
PrintMessage("\r\n");
PrintMessage2(strings[S_ReadEEErr],dim2,k2); //"Error reading EEPROM area, requested %d bytes, read %d\r\n"
}
else PrintMessage(strings[S_Compl]);
}
PrintMessage("\r\n");
//****************** exit ********************
bufferU[j++]=EN_VPP_VCC; //VDD
bufferU[j++]=1;
bufferU[j++]=EN_VPP_VCC; //0
bufferU[j++]=0x0;
bufferU[j++]=SET_CK_D;
bufferU[j++]=0x0;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(1);
unsigned int stop=GetTickCount();
PrintStatusClear();
//****************** visualize ********************
for(i=0;i<8;i+=2){
PrintMessage4(strings[S_ChipID2],i,memID[i],i+1,memID[i+1]); //"ID%d: 0x%02X ID%d: 0x%02X\r\n"
}
for(i=0;i<7;i++){
PrintMessage2(strings[S_ConfigWordH],i+1,memCONFIG[i*2+1]); //"CONFIG%dH: 0x%02X\t"
PrintMessage2(strings[S_ConfigWordL],i+1,memCONFIG[i*2]); //"CONFIG%dL: 0x%02X\r\n"
}
PrintMessage(strings[S_CodeMem]); //"\r\nCode memory:\r\n"
DisplayCODE18F(dim);
if(dim2){
DisplayEE(); //visualize
}
sprintf(str,strings[S_End],(stop-start)/1000.0); //"\r\nEnd (%.2f s)\r\n"
PrintMessage(str);
if(saveLog){
fprintf(logfile,str);
CloseLogFile();
}
}
void Write18Fx(int dim,int dim2,int wbuf,int eraseW1,int eraseW2,int options)
// write 16 bit PIC 18Fxxxx
// dim=program size (bytes) dim2=eeprom size (bytes) wbuf=write buffer size {<=64}
// eraseW1=erase word @3C0005 (not used if >= 0x10000)
// eraseW2=erase word @3C0004 (not used if >= 0x10000)
// options:
// bit [3:0]
// 0 = vdd before vpp (12V)
// 1 = vdd before vpp (9V)
// 2 = low voltage entry with 32 bit key
// 3 = vdd before vpp (9V) with 32 bit key
// bit [7:4]
// 0 = normal eeprom write algoritm
// 1 = with unlock sequence 55 AA
// bit [11:8]
// 0 = 15ms erase delay, 1ms code write time, 5ms EE write, 5ms config write
// 1 = 550ms erase delay, 1.2ms code write time, no config or EEPROM
// 2 = 550ms erase delay, 3.4ms code write time, no config or EEPROM
// 3 = separate block erase with 5ms delay, 1ms code write, 5ms EE write, 5ms config write
// + new control registers address (18FxxK80)
{
int k=0,k2,z=0,i,j;
int err=0;
int EEalgo=(options>>4)&0xF,entry=options&0xF,optWrite=(options>>8)&0xF;
if(dim>0x1FFFFF||dim<0){
PrintMessage(strings[S_CodeLim]); //"Code size out of limits\r\n"
return;
}
if(dim2>0x800||dim2<0){
PrintMessage(strings[S_EELim]); //"EEPROM size out of limits\r\n"
return;
}
if(wbuf>64){
PrintMessage(strings[S_WbufLim]); //"Write buffer size out of limits\r\n"
return;
}
if(entry>0&&!CheckV33Regulator()){
PrintMessage(strings[S_noV33reg]); //Can't find 3.3V expansion board
return;
}
double vpp=-1;
if(entry==0) vpp=12;
else if(entry==1||entry==3) vpp=8.5;
if(!StartHVReg(vpp)){
PrintMessage(strings[S_HVregErr]); //"HV regulator error\r\n"
return;
}
if(saveLog){
OpenLogFile(); //"log.txt"
fprintf(logfile,"Write18F(%d,%d,%d,%d) (0x%X,0x%X,0x%X,0x%X)\n",dim,dim2,wbuf,options,dim,dim2,wbuf,options);
}
if(dim>size) dim=size;
if(dim%wbuf){ //grow to an integer number of rows
dim+=wbuf-dim%wbuf;
j=size;
if(j<dim){
size=dim;
memCODE=(unsigned char*)realloc(memCODE,size);
for(;j<dim;j++) memCODE[j]=0xFF;
}
}
if(dim2>sizeEE) dim2=sizeEE;
if(dim<1){
PrintMessage(strings[S_NoCode]); //"Empty data area\r\n"
return;
}
unsigned int start=GetTickCount();
j=0;
bufferU[j++]=SET_PARAMETER;
bufferU[j++]=SET_T1T2;
bufferU[j++]=1; //T1=1u
bufferU[j++]=100; //T2=100u
bufferU[j++]=EN_VPP_VCC; //enter program mode
bufferU[j++]=0x0;
bufferU[j++]=SET_CK_D;
bufferU[j++]=0x0;
bufferU[j++]=EN_VPP_VCC; //VDD
bufferU[j++]=0x1;
bufferU[j++]=EN_VPP_VCC; //VDD+VPP
bufferU[j++]=0x5;
if(entry==2){ //LV entry with key
bufferU[j++]=EN_VPP_VCC; //VDD
bufferU[j++]=0x1;
}
if(entry==2||entry==3){ //entry with key
bufferU[j++]=WAIT_T3;
bufferU[j++]=WAIT_T3;
bufferU[j++]=TX16;
bufferU[j++]=2;
bufferU[j++]=0x4D;
bufferU[j++]=0x43;
bufferU[j++]=0x48;
bufferU[j++]=0x50;
bufferU[j++]=EN_VPP_VCC; //VDD+VPP
bufferU[j++]=0x5;
bufferU[j++]=WAIT_T3;
}
bufferU[j++]=WAIT_T3;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //3F
bufferU[j++]=0x3F;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //-> TBLPTRU
bufferU[j++]=0xF8;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //FF
bufferU[j++]=0xFF;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //-> TBLPTRH
bufferU[j++]=0xF7;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //FE
bufferU[j++]=0xFE;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //-> TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=TBLR_INC_N; //DevID1-2 0x3FFFFE-F
bufferU[j++]=2;
bufferU[j++]=SET_PARAMETER;
bufferU[j++]=SET_T3;
bufferU[j++]=5100>>8;
bufferU[j++]=5100&0xff;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(entry==2?10:3);
for(z=0;bufferI[z]!=TBLR_INC_N&&z<DIMBUF;z++);
if(z<DIMBUF-3){
PrintMessage2(strings[S_DevID2],bufferI[z+3],bufferI[z+2]); //"DevID: 0x%02X%02X\r\n"
PIC18_ID(bufferI[z+2]+(bufferI[z+3]<<8));
}
j=0;
//****************** erase memory ********************
PrintMessage(strings[S_StartErase]); //"Erase ... "
if(optWrite!=3){ //chip erase
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //3C
bufferU[j++]=0x3C;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //-> TBLPTRU
bufferU[j++]=0xF8;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //TBLPTRH=0
bufferU[j++]=0xF7;
if(eraseW1<0x10000){
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //05
bufferU[j++]=0x05;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //-> TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=TABLE_WRITE; // eraseW1@3C0005
bufferU[j++]=(eraseW1>>8)&0xFF; //0x3F;
bufferU[j++]=eraseW1&0xFF; //0x3F;
}
if(eraseW2<0x10000){
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //04
bufferU[j++]=0x04;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //-> TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=TABLE_WRITE; // eraseW2@3C0004
bufferU[j++]=(eraseW2>>8)&0xFF; //0x8F;
bufferU[j++]=eraseW2&0xFF; //0x8F;
}
bufferU[j++]=CORE_INS; //NOP
bufferU[j++]=0x00;
bufferU[j++]=0x00;
bufferU[j++]=CORE_INS; //NOP
bufferU[j++]=0x00;
bufferU[j++]=0x00;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(16);
if(optWrite!=0) msDelay(550);//bulk erase delay
j=0;
}
else{ //separate block erase
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW 3C
bufferU[j++]=0x3C;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRU
bufferU[j++]=0xF8;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //CLRF TBLPTRH
bufferU[j++]=0xF7;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW 4
bufferU[j++]=0x04;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=TABLE_WRITE; //0x04 @ 0x3C0004
bufferU[j++]=0x04;
bufferU[j++]=0x04;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW 5
bufferU[j++]=0x05;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=TABLE_WRITE; //0x01 @ 0x3C0005
bufferU[j++]=0x01;
bufferU[j++]=0x01;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW 6
bufferU[j++]=0x06;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=TABLE_WRITE; //0x80 @ 0x3C0006
bufferU[j++]=0x80;
bufferU[j++]=0x80;
bufferU[j++]=CORE_INS; //NOP
bufferU[j++]=0x00;
bufferU[j++]=0x00;
bufferU[j++]=CORE_INS; //NOP
bufferU[j++]=0x00;
bufferU[j++]=0x00;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(7); //block erase delay
j=0;
if(saveLog){
fprintf(logfile,"ERASE BLOCK0\n");
}
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW 4
bufferU[j++]=0x04;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=TABLE_WRITE; //0x04 @ 0x3C0004
bufferU[j++]=0x04;
bufferU[j++]=0x04;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW 5
bufferU[j++]=0x05;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=TABLE_WRITE; //0x02 @ 0x3C0005
bufferU[j++]=0x02;
bufferU[j++]=0x02;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW 6
bufferU[j++]=0x06;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=TABLE_WRITE; //0x80 @ 0x3C0006
bufferU[j++]=0x80;
bufferU[j++]=0x80;
bufferU[j++]=CORE_INS; //NOP
bufferU[j++]=0x00;
bufferU[j++]=0x00;
bufferU[j++]=CORE_INS; //NOP
bufferU[j++]=0x00;
bufferU[j++]=0x00;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(7); //block erase delay
j=0;
if(saveLog){
fprintf(logfile,"ERASE BLOCK1\n");
}
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW 4
bufferU[j++]=0x04;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=TABLE_WRITE; //0x04 @ 0x3C0004
bufferU[j++]=0x04;
bufferU[j++]=0x04;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW 5
bufferU[j++]=0x05;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=TABLE_WRITE; //0x04 @ 0x3C0005
bufferU[j++]=0x04;
bufferU[j++]=0x04;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW 6
bufferU[j++]=0x06;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=TABLE_WRITE; //0x80 @ 0x3C0006
bufferU[j++]=0x80;
bufferU[j++]=0x80;
bufferU[j++]=CORE_INS; //NOP
bufferU[j++]=0x00;
bufferU[j++]=0x00;
bufferU[j++]=CORE_INS; //NOP
bufferU[j++]=0x00;
bufferU[j++]=0x00;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(7); //block erase delay
j=0;
if(saveLog){
fprintf(logfile,"ERASE BLOCK2\n");
}
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW 4
bufferU[j++]=0x04;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=TABLE_WRITE; //0x04 @ 0x3C0004
bufferU[j++]=0x04;
bufferU[j++]=0x04;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW 5
bufferU[j++]=0x05;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=TABLE_WRITE; //0x08 @ 0x3C0005
bufferU[j++]=0x08;
bufferU[j++]=0x08;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW 6
bufferU[j++]=0x06;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=TABLE_WRITE; //0x80 @ 0x3C0006
bufferU[j++]=0x80;
bufferU[j++]=0x80;
bufferU[j++]=CORE_INS; //NOP
bufferU[j++]=0x00;
bufferU[j++]=0x00;
bufferU[j++]=CORE_INS; //NOP
bufferU[j++]=0x00;
bufferU[j++]=0x00;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(7); //block erase delay
j=0;
if(saveLog){
fprintf(logfile,"ERASE BLOCK3\n");
}
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW 4
bufferU[j++]=0x04;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=TABLE_WRITE; //0x05 @ 0x3C0004
bufferU[j++]=0x05;
bufferU[j++]=0x05;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW 5
bufferU[j++]=0x05;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=TABLE_WRITE; //0x00 @ 0x3C0005
bufferU[j++]=0x00;
bufferU[j++]=0x00;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW 6
bufferU[j++]=0x06;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=TABLE_WRITE; //0x80 @ 0x3C0006
bufferU[j++]=0x80;
bufferU[j++]=0x80;
bufferU[j++]=CORE_INS; //NOP
bufferU[j++]=0x00;
bufferU[j++]=0x00;
bufferU[j++]=CORE_INS; //NOP
bufferU[j++]=0x00;
bufferU[j++]=0x00;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(7); //block erase delay
j=0;
if(saveLog){
fprintf(logfile,"ERASE BOOT BLOCK\n");
}
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW 4
bufferU[j++]=0x04;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=TABLE_WRITE; //0x02 @ 0x3C0004
bufferU[j++]=0x02;
bufferU[j++]=0x02;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW 5
bufferU[j++]=0x05;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=TABLE_WRITE; //0x00 @ 0x3C0005
bufferU[j++]=0x00;
bufferU[j++]=0x00;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW 6
bufferU[j++]=0x06;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=TABLE_WRITE; //0x80 @ 0x3C0006
bufferU[j++]=0x80;
bufferU[j++]=0x80;
bufferU[j++]=CORE_INS; //NOP
bufferU[j++]=0x00;
bufferU[j++]=0x00;
bufferU[j++]=CORE_INS; //NOP
bufferU[j++]=0x00;
bufferU[j++]=0x00;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(7); //block erase delay
j=0;
if(saveLog){
fprintf(logfile,"ERASE CONFIG\n");
}
if(programID){
bufferU[j++]=CORE_INS;
bufferU[j++]=0x8E; //EEPGD=1
bufferU[j++]=0x7F;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x9C; //CFGS=0
bufferU[j++]=0x7F;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x84; //WREN=1
bufferU[j++]=0x7F;
bufferU[j++]=CORE_INS; //ID 0x200000
bufferU[j++]=0x0E; //MOVLW
bufferU[j++]=0x20; //
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRU
bufferU[j++]=0xF8; //
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //CLRF TBLPTRH
bufferU[j++]=0xF7; //CLRF TBLPTRH
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //CLRF TBLPTRL
bufferU[j++]=0xF6; //CLRF TBLPTRL
bufferU[j++]=CORE_INS;
bufferU[j++]=0x88; //FREE=1
bufferU[j++]=0x7F;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x82; //WR=1
bufferU[j++]=0x7F;
bufferU[j++]=CORE_INS; //NOP
bufferU[j++]=0x00;
bufferU[j++]=0x00;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(7); //row erase delay
j=0;
if(saveLog){
fprintf(logfile,"ERASE ID\n");
}
}
}
PrintMessage(strings[S_Compl]); //"completed\r\n"
//****************** prepare write ********************
bufferU[j++]=CORE_INS;
bufferU[j++]=0x8E; //EEPGD=1
bufferU[j++]=optWrite!=3?0xA6:0x7F;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x9C; //CFCGS=0
bufferU[j++]=optWrite!=3?0xA6:0x7F;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x84; //WREN=1
bufferU[j++]=optWrite!=3?0xA6:0x7F;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //TBLPTRU
bufferU[j++]=0xF8; //TBLPTRU
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //TBLPTRH
bufferU[j++]=0xF7; //TBLPTRH
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //TBLPTRL
bufferU[j++]=0xF6; //TBLPTRL
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(1);
j=0;
//****************** write code ********************
PrintMessage(strings[S_StartCodeProg]); //"code write ... "
PrintStatusSetup();
int ww;
double wdly=1.0;
if(optWrite==1) wdly=1.2;
if(optWrite==2) wdly=3.4;
if(saveLog){
fprintf(logfile,"WRITE CODE\ndim=%d(0x%X)\n",dim,dim); //
}
int valid,i0;
j=0;
for(i=k=0;i<dim;){ //write xx instruction words
if(k==0){ //skip row if empty
i0=i;
for(valid=0;!valid&&i<dim;i+=valid?0:wbuf){
for(k2=0;k2<wbuf&&!valid;k2++) if(memCODE[i+k2]<0xFF) valid=1;
}
if(i>=dim) break;
if(i>i0){ //some rows were skipped; update current address
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E;
bufferU[j++]=(i>>16)&0xFF;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRU
bufferU[j++]=0xF8;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E;
bufferU[j++]=(i>>8)&0xFF;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRH
bufferU[j++]=0xF7;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E;
bufferU[j++]=i&0xFF;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
j=0;
}
}
if(DIMBUF-4-j<wbuf-2-k) ww=(DIMBUF-4-j)/2; //split data for a total of wbuf-2
else ww=(wbuf-2-k)/2;
bufferU[j++]=TBLW_INC_N;
bufferU[j++]=ww;
for(z=0;z<ww;z++){
bufferU[j++]=memCODE[i+1];
bufferU[j++]=memCODE[i];
i+=2;
}
k+=ww*2;
if(k==wbuf-2){ //Write row
if(j>DIMBUF-8){
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(3);
j=0;
}
bufferU[j++]=TBLW_PROG_INC;
bufferU[j++]=memCODE[i+1];
bufferU[j++]=memCODE[i];
if(optWrite==1){ //1.2ms
bufferU[j++]=1200>>8;
bufferU[j++]=1200&0xFF;
}
else if(optWrite==2){ //3.4ms
bufferU[j++]=3400>>8;
bufferU[j++]=3400&0xFF;
}
else{ //1ms
bufferU[j++]=1000>>8;
bufferU[j++]=1000&0xFF;
}
i+=2;
k=0;
}
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(k==0?wdly:2);
j=0;
PrintStatus(strings[S_CodeWriting2],i*100/(dim+dim2),i/2); //"Write: %d%%,addr. %04X"
if(RWstop) i=dim;
if(saveLog){
fprintf(logfile,strings[S_Log7],i,i,k,k); //"i=%d, k=%d 0=%d\n"
}
}
PrintStatusEnd();
PrintMessage(strings[S_Compl]); //"completed\r\n"
//****************** write ID ********************
if(programID&&(optWrite==0||optWrite==3)){
PrintMessage(strings[S_IDW]); //"Write ID ... "
if(saveLog) fprintf(logfile,"Write ID\n");
bufferU[j++]=CORE_INS;
bufferU[j++]=0x8E; //EEPGD=1
bufferU[j++]=optWrite!=3?0xA6:0x7F;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x9C; //CFCGS=0
bufferU[j++]=optWrite!=3?0xA6:0x7F;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //TBLPTRU ID 0x200000
bufferU[j++]=0x20; //TBLPTRU ID 0x200000
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //TBLPTRU
bufferU[j++]=0xF8; //TBLPTRU
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //CLRF TBLPTRH
bufferU[j++]=0xF7; //CLRF TBLPTRH
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //CLRF TBLPTRL
bufferU[j++]=0xF6; //CLRF TBLPTRL
bufferU[j++]=TBLW_INC_N;
bufferU[j++]=3;
for(i=0;i<3;i++){
bufferU[j++]=memID[i*2+1];
bufferU[j++]=memID[i*2];
}
bufferU[j++]=TBLW_PROG;
bufferU[j++]=memID[i*2+1];
bufferU[j++]=memID[i*2];
bufferU[j++]=1000>>8; //only one write so no need to specify different delay
bufferU[j++]=1000&0xFF;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(5);
j=0;
if(saveLog){
fprintf(logfile,strings[S_Log7],i,i,0,0); //"i=%d, k=%d 0=%d\n"
}
PrintMessage(strings[S_Compl]); //"completed\r\n"
}
//****************** write and verify EEPROM ********************
if(dim2&&(optWrite==0||optWrite==3)){
PrintMessage(strings[S_EEAreaW]); //"Write EEPROM ... "
PrintStatusSetup();
if(saveLog) fprintf(logfile,"Write EEPROM\n");
int errEE=0;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x9E; //EEPGD=0
bufferU[j++]=optWrite!=3?0xA6:0x7F;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x9C; //CFGS=0
bufferU[j++]=optWrite!=3?0xA6:0x7F;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x84; //WREN=1
bufferU[j++]=optWrite!=3?0xA6:0x7F;
for(i=0;i<dim2&&err<=max_err;i++){
if(memEE[i]!=0xFF){
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW
bufferU[j++]=i&0xFF;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF
bufferU[j++]=optWrite!=3?0xA9:0x74; //ADDR
if(EEalgo==0){
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW
bufferU[j++]=(i>>8)&0xFF;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF
bufferU[j++]=optWrite!=3?0xAA:0x75; //ADDRH
}
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW
bufferU[j++]=memEE[i];
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF
bufferU[j++]=optWrite!=3?0xA8:0x73; //EEDATA
if(EEalgo==1){ //memory unlock
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW
bufferU[j++]=0x55;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF
bufferU[j++]=0xA7; //EECON2
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //MOVLW
bufferU[j++]=0xAA;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF
bufferU[j++]=0xA7; //EECON2
}
bufferU[j++]=CORE_INS;
bufferU[j++]=0x82;
bufferU[j++]=optWrite!=3?0xA6:0x7F; //WR=1
bufferU[j++]=CORE_INS;
bufferU[j++]=0x00;
bufferU[j++]=0x00; //NOP
bufferU[j++]=CORE_INS;
bufferU[j++]=0x00;
bufferU[j++]=0x00; //NOP
bufferU[j++]=WAIT_T3; //write delay
bufferU[j++]=CORE_INS;
bufferU[j++]=0x80; //RD=1
bufferU[j++]=optWrite!=3?0xA6:0x7F;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x50; //MOVF EEDATA,w
bufferU[j++]=optWrite!=3?0xA8:0x73;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TABLAT
bufferU[j++]=0xF5;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x00; //NOP
bufferU[j++]=0x00;
bufferU[j++]=SHIFT_TABLAT;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(8);
PrintStatus(strings[S_CodeWriting],(i+dim)*100/(dim+dim2),i); //"Writing: %d%%, addr. %03X"
if(RWstop) i=dim2;
j=0;
for(z=DIMBUF-1;z&&!(bufferI[z]==SHIFT_TABLAT&&bufferI[z-1]==CORE_INS);z--);
if(z&&memEE[i]!=bufferI[z+1]) errEE++;
if(saveLog){
fprintf(logfile,strings[S_Log8],i,i,k,k,errEE); //"i=%d, k=%d, errors=%d\n"
}
}
}
PrintStatusEnd();
PrintMessage1(strings[S_ComplErr],errEE); //"completed: %d errors \r\n"
err+=errEE;
}
//****************** verify code ********************
PrintMessage(strings[S_CodeV]); //"Verify code ... "
PrintStatusSetup();
if(saveLog)fprintf(logfile,"VERIFY CODE\n");
bufferU[j++]=CORE_INS;
bufferU[j++]=0x8E; //EEPGD=1
bufferU[j++]=optWrite!=3?0xA6:0x7F;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x9C; //CFCGS=0
bufferU[j++]=optWrite!=3?0xA6:0x7F;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //CLRF TBLPTRU
bufferU[j++]=0xF8; //CLRF TBLPTRU
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //CLRF TBLPTRH
bufferU[j++]=0xF7; //CLRF TBLPTRH
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //CLRF TBLPTRL
bufferU[j++]=0xF6; //CLRF TBLPTRL
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
if(saveLog){
fprintf(logfile,"\n\n");
}
for(i=0,j=0,k=0;i<dim;i+=DIMBUF-4){
i0=i;
for(valid=0;!valid&&i<dim;i+=valid?0:DIMBUF-4){ //skip verification if 0xFF
for(k2=0;k2<DIMBUF-4&&!valid&&i+k2<dim;k2++) if(memCODE[i+k2]<0xFF) valid=1;
}
if(i>=dim) break;
if(i>i0){ //some data was skipped; update current address
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E;
bufferU[j++]=(i>>16)&0xFF;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRU
bufferU[j++]=0xF8;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E;
bufferU[j++]=(i>>8)&0xFF;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRH
bufferU[j++]=0xF7;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E;
bufferU[j++]=i&0xFF;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRL
bufferU[j++]=0xF6;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
j=0;
}
bufferU[j++]=TBLR_INC_N;
bufferU[j++]=i<dim-(DIMBUF-4)?DIMBUF-4:dim-i;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
if(bufferI[1]==TBLR_INC_N){
for(z=0;z<bufferI[2]&&z<DIMBUF;z++){
if(memCODE[i+z]!=bufferI[z+3]){
PrintMessage4(strings[S_CodeVError],i+z,i+z,memCODE[i+z],bufferI[z+3]); //"Error writing address %4X: written %02X, read %02X\r\n"
err++;
}
//k++;
}
}
PrintStatus(strings[S_CodeV2],i*100/(dim+dim2),i); //"Verifica: %d%%, ind. %04X"
if(RWstop) i=dim;
j=0;
if(saveLog){
fprintf(logfile,strings[S_Log8],i,i,k,k,err); //"i=%d, k=%d, errors=%d\n"
}
if(err>=max_err) break;
}
PrintStatusEnd();
if(i<dim){
PrintMessage2(strings[S_CodeVError2],dim,i); //"Error verifying code area, requested %d bytes, read %d\r\n"
}
PrintMessage1(strings[S_ComplErr],err); //"completed: %d errors\r\n"
if(err>=max_err){
PrintMessage1(strings[S_MaxErr],err); //"Exceeded maximum number of errors (%d), write interrupted\r\n"
}
//****************** verify ID ********************
if(programID&&err<max_err&&optWrite==0){
PrintMessage(strings[S_IDV]); //"Verify ID ... "
int errID=0;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //TBLPTRU ID 0x200000
bufferU[j++]=0x20; //TBLPTRU ID 0x200000
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //TBLPTRU
bufferU[j++]=0xF8; //TBLPTRU
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //CLRF TBLPTRH
bufferU[j++]=0xF7; //CLRF TBLPTRH
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //CLRF TBLPTRL
bufferU[j++]=0xF6; //CLRF TBLPTRL
bufferU[j++]=TBLR_INC_N;
bufferU[j++]=8;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
for(z=0;bufferI[z]!=TBLR_INC_N&&z<DIMBUF;z++);
for(i=0;i<8;i++) if(memID[i]!=0xFF&&memID[i]!=bufferI[z+i+2]) errID++;
PrintMessage1(strings[S_ComplErr],errID); //"completed: %d errors\r\n"
err+=errID;
if(err>=max_err){
PrintMessage1(strings[S_MaxErr],err); //"Exceeded maximum number of errors (%d), write interrupted\r\n"
}
j=0;
if(saveLog){
fprintf(logfile,strings[S_Log8],i,i,0,0,err); //"i=%d, k=%d, errors=%d\n"
}
}
//****************** write CONFIG ********************
if(err<max_err&&(optWrite==0||optWrite==3)){
PrintMessage(strings[S_ConfigW]); //"Write CONFIG ..."
if(saveLog) fprintf(logfile,"Write CONFIG\n");
bufferU[j++]=CORE_INS;
bufferU[j++]=0x8E; //EEPGD=1
bufferU[j++]=optWrite!=3?0xA6:0x7F;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x8C; //CFCGS=1
bufferU[j++]=optWrite!=3?0xA6:0x7F;
bufferU[j++]=CORE_INS; //CONFIG 0x300000
bufferU[j++]=0x0E; //MOVLW
bufferU[j++]=0x30;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRU
bufferU[j++]=0xF8; //MOVWF TBLPTRU
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //CLRF TBLPTRH
bufferU[j++]=0xF7; //CLRF TBLPTRH
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //CLRF TBLPTRL
bufferU[j++]=0xF6; //CLRF TBLPTRL
for(i=0;i<14;i++){
if(memCONFIG[i]<0xFF){
bufferU[j++]=TBLW_PROG;
bufferU[j++]=0;
bufferU[j++]=memCONFIG[i];
bufferU[j++]=5000>>8;
bufferU[j++]=5000&0xFF;
}
bufferU[j++]=CORE_INS;
bufferU[j++]=0x2A; //INCF
bufferU[j++]=0xF6; //TBLPTRL
i++;
if(memCONFIG[i]<0xFF){
bufferU[j++]=TBLW_PROG;
bufferU[j++]=memCONFIG[i];
bufferU[j++]=0;
bufferU[j++]=5000>>8;
bufferU[j++]=5000&0xFF;
}
bufferU[j++]=CORE_INS;
bufferU[j++]=0x2A; //INCF
bufferU[j++]=0xF6; //TBLPTRL
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(12);
j=0;
if(saveLog){
fprintf(logfile,strings[S_Log7],i,i,0,0); //"i=%d, k=%d\n"
}
}
PrintMessage(strings[S_Compl]); //"completed\r\n"
//****************** verify CONFIG ********************
PrintMessage(strings[S_ConfigV]); //"Verify CONFIG ... "
int errC=0;
bufferU[j++]=CORE_INS;
bufferU[j++]=0x0E; //TBLPTRU CONFIG 0x300000
bufferU[j++]=0x30; //
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6E; //MOVWF TBLPTRU
bufferU[j++]=0xF8; //MOVWF TBLPTRU
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //CLRF TBLPTRH
bufferU[j++]=0xF7; //CLRF TBLPTRH
bufferU[j++]=CORE_INS;
bufferU[j++]=0x6A; //CLRF TBLPTRL
bufferU[j++]=0xF6; //CLRF TBLPTRL
bufferU[j++]=TBLR_INC_N;
bufferU[j++]=14;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
for(z=0;bufferI[z]!=TBLR_INC_N&&z<DIMBUF-16;z++);
if(z<DIMBUF-16){
for(i=0;i<14;i++) if(~memCONFIG[i]&bufferI[z+i+2]) errC++; //error if written 0 and read 1 (~W&R)
}
PrintMessage1(strings[S_ComplErr],errC); //"completed: %d errors\r\n"
err+=errC;
if(err>=max_err){
PrintMessage1(strings[S_MaxErr],err); //"Exceeded maximum number of errors (%d), write interrupted\r\n"
}
j=0;
if(saveLog){
fprintf(logfile,strings[S_Log8],i,i,0,0,err); //"i=%d, k=%d, errors=%d\n"
}
}
bufferU[j++]=SET_PARAMETER;
bufferU[j++]=SET_T3;
bufferU[j++]=2000>>8;
bufferU[j++]=2000&0xff;
bufferU[j++]=EN_VPP_VCC; //VDD
bufferU[j++]=1;
bufferU[j++]=EN_VPP_VCC; //0
bufferU[j++]=0x0;
bufferU[j++]=SET_CK_D;
bufferU[j++]=0x0;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(1);
unsigned int stop=GetTickCount();
sprintf(str,strings[S_EndErr],(stop-start)/1000.0,err,err!=1?strings[S_ErrPlur]:strings[S_ErrSing]); //"\r\nEnd (%.2f s) %d %s\r\n\r\n"
PrintMessage(str);
if(saveLog){
fprintf(logfile,str);
CloseLogFile();
}
PrintStatusClear(); //clear status report
}
#define LOAD_PC_ADDR 0x80
#define BULK_ERASE 0x18
#define ROW_ERASE_PROGRAM_MEM 0xF0
#define LOAD_NVM 0x00
#define LOAD_NVM_INC 0x02
#define READ_NVM 0xFC
#define READ_NVM_INC 0xFE
#define INC_ADDR8 0xF8
#define BEGIN_INT_PROG 0xE0
#define BEGIN_EXT_PROG 0xC0
#define PROGRAM_DATA 0xC0
#define PROGRAM_DATA_INC 0xE0
#define END_EXT_PROG 0x82
void Read18FKx(int dim,int dim2,int options)
// read 16 bit PIC 18FxxKx3 with new 8b commands
// dim=program size (bytes)
// dim2=if>0 use eeprom, size is automatic when reading DCI
// options:
// bit [3:0]
// 0 = vpp before vdd (8.5V)
// bit [7:4] memory layout
// code config EE DCI DIA model
// 0 5 0x310000 0x3FFF00 0x3F0000 K42 K83
// 1 10 0x380000 0x3C0000 0x2C0000 Q40-43
// 2 35 0x380000 0x3C0000 0x2C0000 Q83-84
{
int k=0,k2=0,z=0,i,j;
int devID=0,devREV=0;
int MemAddr;
int EEaddr=0,DCIaddr=0,DIAaddr=0,DIAlen=0,UIDlen=0,CONFIGlen=0;
int type=(options>>4)&0xF;
if(type==0){ //K42 K83
EEaddr=0x310000;
DCIaddr=0x3FFF00;
DIAaddr=0x3F0000;
DIAlen=0x40;
UIDlen=0x10;
CONFIGlen=5;
}
else if(type==1){ //Q43
EEaddr=0x380000;
DCIaddr=0x3C0000;
DIAaddr=0x2C0000;
DIAlen=0x100;
UIDlen=0x40;
CONFIGlen=10;
}
else if(type==2){ //Q83-84
EEaddr=0x380000;
DCIaddr=0x3C0000;
DIAaddr=0x2C0000;
DIAlen=0x100;
UIDlen=0x40;
CONFIGlen=35;
}
else PrintMessage("unexpected parameter");
if(dim>0x1FFFFF||dim<0){
PrintMessage(strings[S_CodeLim]); //"Code size out of limits\r\n"
return;
}
if(dim2>0x800||dim2<0){
PrintMessage(strings[S_EELim]); //"EEPROM size out of limits\r\n"
return;
}
if(!CheckV33Regulator()){
PrintMessage(strings[S_noV33reg]); //Can't find 3.3V expansion board
return;
}
double vpp=8.5;
if(!StartHVReg(vpp)){
PrintMessage(strings[S_HVregErr]); //"HV regulator error\r\n"
return;
}
if(saveLog){
OpenLogFile(); //"log.txt"
fprintf(logfile,"Read18FKx(%d,%d,%d) (0x%X,0x%X,0x%X)\n",dim,dim2,options,dim,dim2,options);
}
size=dim;
if(memCODE) free(memCODE);
memCODE=(unsigned char*)malloc(size); //CODE
unsigned char* memDIA=(unsigned char*)malloc(DIAlen); //DIA
for(j=0;j<64;j++) memID[j]=0xFF;
for(j=0;j<14;j++) memCONFIG[j]=0xFF;
for(j=0;j<DIAlen;j++) memDIA[j]=0xFF;
unsigned int start=GetTickCount();
j=0;
bufferU[j++]=SET_PARAMETER;
bufferU[j++]=SET_T1T2;
bufferU[j++]=2; //T1=2u
bufferU[j++]=100; //T2=100u
bufferU[j++]=SET_PARAMETER;
bufferU[j++]=SET_T3;
bufferU[j++]=2000>>8;
bufferU[j++]=2000&0xff;
bufferU[j++]=EN_VPP_VCC; //enter program mode
bufferU[j++]=0x0;
bufferU[j++]=SET_CK_D;
bufferU[j++]=0x0;
bufferU[j++]=EN_VPP_VCC; //VPP
bufferU[j++]=0x4;
bufferU[j++]=EN_VPP_VCC; //VDD+VPP
bufferU[j++]=0x5;
bufferU[j++]=WAIT_T3;
MemAddr=0x3FFFFc<<1; //devREV
bufferU[j++]=TX16;
bufferU[j++]=2;
bufferU[j++]=0x80;
bufferU[j++]=MemAddr>>16;
bufferU[j++]=(MemAddr>>8)&0xFF;
bufferU[j++]=MemAddr&0xFF;
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
MemAddr=DCIaddr<<1; //DCI
bufferU[j++]=TX16;
bufferU[j++]=2;
bufferU[j++]=0x80;
bufferU[j++]=MemAddr>>16;
bufferU[j++]=(MemAddr>>8)&0xFF;
bufferU[j++]=MemAddr&0xFF;
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
bufferU[j++]=TX16;
bufferU[j++]=2;
bufferU[j++]=0x80;
bufferU[j++]=0x0;
bufferU[j++]=0x0;
bufferU[j++]=0x0;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(4);
for(z=0;bufferI[z]!=ICSP8_READ&&z<DIMBUF;z++);
if(z<DIMBUF-2){
devREV=(bufferI[z+1]<<8)+bufferI[z+2];
}
for(z+=3;bufferI[z]!=ICSP8_READ&&z<DIMBUF;z++);
if(z<DIMBUF-2){
devID=(bufferI[z+1]<<8)+bufferI[z+2];
}
PrintMessage1("DevID: 0x%04X\r\n",devID);
PrintMessage1("RevID: 0x%04X\r\n",devREV);
PIC18_ID(devID+(devREV<<16));
PrintMessage("Device Configuration Information:\r\n");
for(z+=3;bufferI[z]!=ICSP8_READ&&z<DIMBUF;z++);
if(z<DIMBUF-2){
PrintMessage1("Erase row size: %d\r\n",(bufferI[z+1]<<8)+bufferI[z+2]);
}
for(z+=3;bufferI[z]!=ICSP8_READ&&z<DIMBUF;z++);
if(z<DIMBUF-2){
PrintMessage1("Write latches per row: %d\r\n",(bufferI[z+1]<<8)+bufferI[z+2]);
}
for(z+=3;bufferI[z]!=ICSP8_READ&&z<DIMBUF;z++);
if(z<DIMBUF-2){
PrintMessage1("User rows: %d\r\n",(bufferI[z+1]<<8)+bufferI[z+2]);
}
for(z+=3;bufferI[z]!=ICSP8_READ&&z<DIMBUF;z++);
if(z<DIMBUF-2){
PrintMessage1("EEPROM size: %d\r\n",(bufferI[z+1]<<8)+bufferI[z+2]);
sizeEE=(bufferI[z+1]<<8)+bufferI[z+2];
}
for(z+=3;bufferI[z]!=ICSP8_READ&&z<DIMBUF;z++);
if(z<DIMBUF-3){
PrintMessage1("Pin count: %d\r\n",(bufferI[z+1]<<8)+bufferI[z+2]);
}
//****************** read code ********************
PrintMessage(strings[S_CodeReading1]); //read code ...
if(saveLog) fprintf(logfile,"%s\n",strings[S_CodeReading1]); //read code ...
PrintStatusSetup();
for(i=0,j=0;i<dim;i+=2){ //2 bytes per read
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
if((j+1)/2*3+3>DIMBUF||i==dim-2){ //2B cmd -> 3B data
bufferU[j++]=FLUSH; //remember: FLUSH generates a response (even empty)!
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(5);
for(z=0;z<DIMBUF-2;z++){
if(bufferI[z]==ICSP8_READ){
memCODE[k++]=bufferI[z+2]; //Low byte
memCODE[k++]=bufferI[z+1]; //High byte
z+=2;
}
}
PrintStatus(strings[S_CodeReading],(i+sizeEE+10+UIDlen+DIAlen)*100/(dim+sizeEE+10+UIDlen+DIAlen),i); //"Read: %d%%, addr. %03X"
j=0;
if(saveLog){
fprintf(logfile,strings[S_Log7],i,i,k,k); //"i=%d(0x%X), k=%d(0x%X)\n"
}
}
}
PrintStatusEnd();
if(k!=dim){
PrintMessage("\r\n");
PrintMessage2(strings[S_ReadCodeErr2],dim,k); //"Error reading code area, requested %d bytes, read %d\r\n"
}
else PrintMessage(strings[S_Compl]);
//****************** read config area ********************
PrintMessage(strings[S_Read_CONFIG_A]); //read config ...
if(saveLog) fprintf(logfile,"%s\n",strings[S_Read_CONFIG_A]); //read config ...
MemAddr=0x300000<<1; //config area
bufferU[j++]=TX16;
bufferU[j++]=2;
bufferU[j++]=0x80;
bufferU[j++]=MemAddr>>16;
bufferU[j++]=(MemAddr>>8)&0xFF;
bufferU[j++]=MemAddr&0xFF;
k2=0;
for(i=0;i<CONFIGlen;i++){ //Config
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
if((j+1)/2*3+3>DIMBUF||i==CONFIGlen-1){ //2B cmd -> 3B data
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(5);
for(z=0;z<DIMBUF-2;z++){
if(bufferI[z]==ICSP8_READ){
memCONFIG[k2++]=bufferI[z+2]; //LSB
if(type==0) memCONFIG[k2++]=bufferI[z+1]; //MSB
z+=2;
}
}
PrintStatus(strings[S_CodeReading],(dim+sizeEE+i+UIDlen+DIAlen)*100/(dim+sizeEE+CONFIGlen+UIDlen+DIAlen),i); //"Read: %d%%, addr %03X"
j=0;
if(saveLog){
fprintf(logfile,strings[S_Log7],i,i,k2,k2); //"i=%d(0x%X), k=%d(0x%X)\n"
}
}
}
if(k2!=10){
PrintMessage("\r\n");
PrintMessage2(strings[S_ReadConfigErr],10,k2); //"Error reading config area, requested %d bytes, read %d\r\n"
}
else PrintMessage(strings[S_Compl]);
//****************** read user ID ********************
if(saveLog) fprintf(logfile,"Read user ID\n");
MemAddr=0x200000<<1; //user ID
bufferU[j++]=TX16;
bufferU[j++]=2;
bufferU[j++]=0x80;
bufferU[j++]=MemAddr>>16;
bufferU[j++]=(MemAddr>>8)&0xFF;
bufferU[j++]=MemAddr&0xFF;
k2=0;
for(i=0;i<UIDlen;i+=2){
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
if((j+1)/2*3+3>DIMBUF||i==UIDlen-2){ //2B cmd -> 3B data
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(5);
for(z=0;z<DIMBUF-2;z++){
if(bufferI[z]==ICSP8_READ){
memID[k2++]=bufferI[z+2]; //Low byte
memID[k2++]=bufferI[z+1]; //High byte
z+=2;
}
}
PrintStatus(strings[S_CodeReading],(dim+sizeEE+10+i+DIAlen)*100/(dim+sizeEE+10+UIDlen+DIAlen),i); //"Read: %d%%, addr. %03X"
j=0;
if(saveLog){
fprintf(logfile,strings[S_Log7],i,i,k2,k2); //"i=%d(0x%X), k=%d(0x%X)\n"
}
}
}
if(k2!=UIDlen){
PrintMessage("\r\n");
PrintMessage2(strings[S_ReadConfigErr],UIDlen,k2); //"Error reading config area, requested %d bytes, read %d\r\n"
}
//****************** read device info area (DIA) ********************
if(saveLog) fprintf(logfile,"Read device info area\n");
MemAddr=DIAaddr<<1; //DIA
bufferU[j++]=TX16;
bufferU[j++]=2;
bufferU[j++]=0x80;
bufferU[j++]=MemAddr>>16;
bufferU[j++]=(MemAddr>>8)&0xFF;
bufferU[j++]=MemAddr&0xFF;
k2=0;
for(i=0;i<DIAlen;i+=2){ //DIA
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
if((j+1)/2*3+3>DIMBUF||i==DIAlen-2){ //2B cmd -> 3B data
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(5);
for(z=0;z<DIMBUF-2;z++){
if(bufferI[z]==ICSP8_READ){
memDIA[k2++]=bufferI[z+2];
memDIA[k2++]=bufferI[z+1];
z+=2;
}
}
PrintStatus(strings[S_CodeReading],(dim+sizeEE+10+UIDlen+i)*100/(dim+sizeEE+10+UIDlen+DIAlen),i); //"Read: %d%%, addr. %03X"
j=0;
if(saveLog){
fprintf(logfile,strings[S_Log7],i,i,k2,k2); //"i=%d(0x%X), k=%d(0x%X)\n"
}
}
}
if(k2!=DIAlen){
PrintMessage("\r\n");
PrintMessage2(strings[S_ReadConfigErr],DIAlen,k2); //"Error reading config area, requested %d bytes, read %d\r\n"
}
//****************** read eeprom ********************
if(dim2){ //EEPROM
if(sizeEE>0x1000||sizeEE<0){ //Max 4K
PrintMessage(strings[S_EELim]); //"EEPROM size exceeds limits\r\n"
return;
}
if(memEE) free(memEE);
memEE=(unsigned char*)malloc(sizeEE); //EEPROM
for(i=0;i<sizeEE;i++) memEE[i]=0xFF;
PrintMessage(strings[S_ReadEE]); //Read EEPROM ...
if(saveLog) fprintf(logfile,"%s\n",strings[S_ReadEE]); //Read EEPROM ...
MemAddr=EEaddr<<1; //EE base address
bufferU[j++]=TX16;
bufferU[j++]=2;
bufferU[j++]=0x80;
bufferU[j++]=MemAddr>>16;
bufferU[j++]=(MemAddr>>8)&0xFF;
bufferU[j++]=MemAddr&0xFF;
for(i=k=0;i<sizeEE;i++){
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
if((j+1)/2*3+3>DIMBUF||i==sizeEE-1){ //2B cmd -> 3B answer
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(5);
for(z=0;z<DIMBUF-2;z++){
if(bufferI[z]==ICSP8_READ){
memEE[k++]=bufferI[z+2]; //LSB
z+=2;
}
}
PrintStatus(strings[S_CodeReading],(dim+i+10+UIDlen+DIAlen)*100/(dim+sizeEE+10+UIDlen+DIAlen),i); //"Read: %d%%, addr %03X"
j=0;
if(saveLog){
fprintf(logfile,strings[S_Log7],i,i,k,k); //"i=%d(0x%X), k=%d(0x%X)\n"
}
}
}
PrintStatusEnd();
if(k!=sizeEE){
PrintMessage("\r\n");
PrintMessage2(strings[S_ReadEEErr],sizeEE,k); //"Error reading EE area, ..."
}
else PrintMessage(strings[S_Compl]);
}
//****************** exit ********************
bufferU[j++]=EN_VPP_VCC; //VDD
bufferU[j++]=1;
bufferU[j++]=EN_VPP_VCC; //0
bufferU[j++]=0x0;
bufferU[j++]=SET_CK_D;
bufferU[j++]=0x0;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(1);
unsigned int stop=GetTickCount();
PrintStatusClear();
//****************** visualize ********************
PrintMessage(strings[S_ConfigMem]); //"CONFIG"
for(i=0;i<UIDlen;i+=2){
if(memID[i]<0xFF||memID[i+1]<0xFF) PrintMessage4(strings[S_ChipID2],i,memID[i],i+1,memID[i+1]); //"ID%d: 0x%02X ID%d: 0x%02X\r\n"
}
if(type==0){ //K42 K83 etc.
for(i=0;i<5;i++){
PrintMessage2(strings[S_ConfigWordH],i+1,memCONFIG[i*2+1]); //"CONFIG%dH: 0x%02X\t"
PrintMessage2(strings[S_ConfigWordL],i+1,memCONFIG[i*2]); //"CONFIG%dL: 0x%02X\r\n"
}
}
else if(type==1){ //Q43 etc.
for(i=0;i<10;i++) PrintMessage2("CONFIG%d: 0x%02X\r\n",i+1,memCONFIG[i]);
}
PrintMessage(strings[S_CodeMem]); //"\r\nCode memory:\r\n"
DisplayCODE18F(dim);
if(dim2){
DisplayEE(); //visualize
}
sprintf(str,strings[S_End],(stop-start)/1000.0); //"\r\nEnd (%.2f s)\r\n"
PrintMessage(str);
if(saveLog){
fprintf(logfile,str);
CloseLogFile();
}
}
void Write18FKx(int dim,int dim2,int options,int nu1,int nu2, int nu3)
// write 16 bit PIC 18FxxKx3 with new 8b commands
// dim=program size (bytes) dim2=eeprom size (bytes)
// options:
// bit [3:0]
// 0 = vpp before vdd (8.5V)
// bit [7:4] memory layout
// code config EE DCI DIA model program cycle delays ms (E,W,EE)
// 0 5 0x310000 0x3FFF00 0x3F0000 K42 K83 multiple-word 25.5 2.8 5.6
// 1 10 0x380000 0x3C0000 0x2C0000 Q40-43 one-word 11 0.075 11
// 2 35 0x380000 0x3C0000 0x2C0000 Q83-84 one-word 11 0.075 11
{
int k=0,k2,z=0,i,j,x=0,w=0;
int err=0;
int devID=0,devREV=0;
int MemAddr,rowN=0;
int EEaddr=0,DCIaddr=0,DIAaddr=0,DIAlen=0,UIDlen=0,CONFIGlen=0;
int type=(options>>4)&0xF;
if(type==0){ //K42 K83
EEaddr=0x310000;
DCIaddr=0x3FFF00;
DIAaddr=0x3F0000;
DIAlen=0x40;
UIDlen=0x10;
CONFIGlen=5;
}
else if(type==1){ //Q43
EEaddr=0x380000;
DCIaddr=0x3C0000;
DIAaddr=0x2C0000;
DIAlen=0x100;
UIDlen=0x40;
CONFIGlen=10;
}
else if(type==2){ //Q83-84
EEaddr=0x380000;
DCIaddr=0x3C0000;
DIAaddr=0x2C0000;
DIAlen=0x100;
UIDlen=0x40;
CONFIGlen=35;
}
else PrintMessage("unexpected parameter");
if(dim>0x1FFFFF||dim<0){
PrintMessage(strings[S_CodeLim]); //"Code size out of limits\r\n"
return;
}
if(dim2>0x800||dim2<0){
PrintMessage(strings[S_EELim]); //"EEPROM size out of limits\r\n"
return;
}
if(!CheckV33Regulator()){
PrintMessage(strings[S_noV33reg]); //Can't find 3.3V expansion board
return;
}
double vpp=8.5;
if(!StartHVReg(vpp)){
PrintMessage(strings[S_HVregErr]); //"HV regulator error\r\n"
return;
}
if(saveLog){
OpenLogFile(); //"log.txt"
fprintf(logfile,"Write18FKx(%d,%d,%d) (0x%X,0x%X,0x%X)\n",dim,dim2,options,dim,dim2,options);
}
if(dim>size) dim=size;
if(dim2>sizeEE) dim2=sizeEE;
if(dim<1){
PrintMessage(strings[S_NoCode]); //"Empty data area\r\n"
return;
}
unsigned int start=GetTickCount();
j=0;
bufferU[j++]=SET_PARAMETER;
bufferU[j++]=SET_T1T2;
bufferU[j++]=2; //T1=2u
bufferU[j++]=35; //T2 adjusted to obtain ~80us when needed
bufferU[j++]=SET_PARAMETER;
bufferU[j++]=SET_T3;
bufferU[j++]=11000>>8;
bufferU[j++]=11000&0xff;
bufferU[j++]=EN_VPP_VCC; //enter program mode
bufferU[j++]=0x0;
bufferU[j++]=SET_CK_D;
bufferU[j++]=0x0;
bufferU[j++]=EN_VPP_VCC; //VPP
bufferU[j++]=0x4;
bufferU[j++]=EN_VPP_VCC; //VDD+VPP
bufferU[j++]=0x5;
bufferU[j++]=WAIT_T3;
MemAddr=0x3FFFFc<<1; //devREV
bufferU[j++]=TX16;
bufferU[j++]=2;
bufferU[j++]=0x80;
bufferU[j++]=MemAddr>>16;
bufferU[j++]=(MemAddr>>8)&0xFF;
bufferU[j++]=MemAddr&0xFF;
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
MemAddr=DCIaddr<<1; //DCI
bufferU[j++]=TX16;
bufferU[j++]=2;
bufferU[j++]=0x80;
bufferU[j++]=MemAddr>>16;
bufferU[j++]=(MemAddr>>8)&0xFF;
bufferU[j++]=MemAddr&0xFF;
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
bufferU[j++]=TX16;
bufferU[j++]=2;
bufferU[j++]=0x80;
bufferU[j++]=0x0;
bufferU[j++]=0x0;
bufferU[j++]=0x0;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(14);
for(z=0;bufferI[z]!=ICSP8_READ&&z<DIMBUF;z++);
if(z<DIMBUF-2){
devREV=(bufferI[z+1]<<8)+bufferI[z+2];
}
for(z+=3;bufferI[z]!=ICSP8_READ&&z<DIMBUF;z++);
if(z<DIMBUF-2){
devID=(bufferI[z+1]<<8)+bufferI[z+2];
}
PrintMessage1("DevID: 0x%04X\r\n",devID);
PrintMessage1("RevID: 0x%04X\r\n",devREV);
PIC18_ID(devID+(devREV<<16));
PrintMessage("Device Configuration Information:\r\n");
for(z+=3;bufferI[z]!=ICSP8_READ&&z<DIMBUF;z++);
if(z<DIMBUF-2){
PrintMessage1("Erase row size: %d\r\n",(bufferI[z+1]<<8)+bufferI[z+2]);
}
for(z+=3;bufferI[z]!=ICSP8_READ&&z<DIMBUF;z++);
if(z<DIMBUF-2){
rowN=(bufferI[z+1]<<8)+bufferI[z+2];
PrintMessage1("Write latches per row: %d\r\n",rowN);
}
for(z+=3;bufferI[z]!=ICSP8_READ&&z<DIMBUF;z++);
if(z<DIMBUF-2){
PrintMessage1("User rows: %d\r\n",(bufferI[z+1]<<8)+bufferI[z+2]);
}
for(z+=3;bufferI[z]!=ICSP8_READ&&z<DIMBUF;z++);
if(z<DIMBUF-2){
PrintMessage1("EEPROM size: %d\r\n",(bufferI[z+1]<<8)+bufferI[z+2]);
sizeEE=(bufferI[z+1]<<8)+bufferI[z+2];
}
for(z+=3;bufferI[z]!=ICSP8_READ&&z<DIMBUF;z++);
if(z<DIMBUF-3){
PrintMessage1("Pin count: %d\r\n",(bufferI[z+1]<<8)+bufferI[z+2]);
}
j=0;
//****************** erase memory ********************
PrintMessage(strings[S_StartErase]); //"Erase ... "
if(type==0){ //K42 K83
bufferU[j++]=SET_PARAMETER;
bufferU[j++]=SET_T3;
bufferU[j++]=25500>>8;
bufferU[j++]=25500&0xff;
MemAddr=0x300000<<1; //address to erase Flash+User+Config
bufferU[j++]=TX16;
bufferU[j++]=2;
bufferU[j++]=0x80;
bufferU[j++]=MemAddr>>16;
bufferU[j++]=(MemAddr>>8)&0xFF;
bufferU[j++]=MemAddr&0xFF;
bufferU[j++]=ICSP8_SHORT;
bufferU[j++]=BULK_ERASE;
bufferU[j++]=WAIT_T3; //TERAB
MemAddr=0x310000<<1; //address to erase EEPROM
bufferU[j++]=TX16;
bufferU[j++]=2;
bufferU[j++]=0x80;
bufferU[j++]=MemAddr>>16;
bufferU[j++]=(MemAddr>>8)&0xFF;
bufferU[j++]=MemAddr&0xFF;
bufferU[j++]=ICSP8_SHORT;
bufferU[j++]=BULK_ERASE;
bufferU[j++]=WAIT_T3; //TERAB
bufferU[j++]=SET_PARAMETER;
bufferU[j++]=SET_T3;
bufferU[j++]=2800>>8;
bufferU[j++]=2800&0xff;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(52);
}
else if(type==1){ //Q43
bufferU[j++]=ICSP8_LOAD;
bufferU[j++]=BULK_ERASE;
bufferU[j++]=0;
bufferU[j++]=0xF;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(1);
msDelay(12); //bulk erase delay
}
PrintMessage(strings[S_Compl]); //"completed\r\n"
//****************** write code ********************
PrintMessage(strings[S_StartCodeProg]); //"code write ... "
PrintStatusSetup();
if(saveLog){
fprintf(logfile,"WRITE CODE\ndim=%d(0x%X)\n",dim,dim); //
}
if(type==1){ //Q43
j=0;
bufferU[j++]=TX16;
bufferU[j++]=2;
bufferU[j++]=0x80;
bufferU[j++]=0x0;
bufferU[j++]=0x0;
bufferU[j++]=0x0;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
j=0;
for(i=k=0;i<dim;i+=2){
if(memCODE[i]<0xFF||memCODE[i+1]<0xFF){
bufferU[j++]=ICSP8_LOAD;
bufferU[j++]=PROGRAM_DATA;
bufferU[j++]=memCODE[i+1]; //High byte
bufferU[j++]=memCODE[i]; //Low byte
bufferU[j++]=WAIT_T2; //Tprogram 75us
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
}
else{
for(x=0;i+x<dim&&memCODE[i+x]==0xFF&&memCODE[i+x+1]==0xFF;x+=2);
if(x>9){ //set new address
i+=x;
MemAddr=i<<1;
bufferU[j++]=TX16;
bufferU[j++]=2;
bufferU[j++]=0x80;
bufferU[j++]=MemAddr>>16;
bufferU[j++]=(MemAddr>>8)&0xFF;
bufferU[j++]=MemAddr&0xFF;
i-=2;
}
else{
bufferU[j++]=ICSP8_SHORT;
bufferU[j++]=INC_ADDR8;
}
}
if(j>DIMBUF-8||i==dim-2){
PrintStatus(strings[S_CodeWriting],i*100/(dim+dim2),i); //"Writing: %d%%, add. %03X"
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
for(z=0;z<DIMBUF-4;z++){
if(bufferI[z]==ICSP8_SHORT&&memCODE[k]==0xff&&memCODE[k+1]==0xff) k+=2;
else if(bufferI[z]==TX16){ //need to find the next valid address
for(;k<dim&&memCODE[k]==0xFF&&memCODE[k+1]==0xFF;k+=2);
}
else if(bufferI[z]==ICSP8_LOAD&&bufferI[z+2]==ICSP8_READ){
if(memCODE[k]!=bufferI[z+4]||memCODE[k+1]!=bufferI[z+3]){
PrintMessage("\r\n");
PrintMessage3(strings[S_CodeWError2],k,(memCODE[k+1]<<8)+memCODE[k],(bufferI[z+3]<<8)+bufferI[z+4]); //"Error writing address %3X: written %04X, read %04X\r\n"
err++;
if(max_err&&err>max_err){
PrintMessage1(strings[S_MaxErr],err); //"Exceeded maximum number of errors (%d), write interrupted\r\n"
i=dim;
z=DIMBUF;
}
}
k+=2;
z+=4;
}
}
j=0;
if(saveLog){
fprintf(logfile,strings[S_Log8],i,i,k,k,err); //"i=%d, k=%d, errors=%d,\n"
}
}
}
err+=i-k;
}
else if(type==0){ //K42 K83
int valid;
for(;dim>0&&memCODE[dim-1]==0xff&&memCODE[dim-2]==0xff;dim-=2); //skip empty space at end
if(dim%rowN) dim+=rowN-dim%rowN; //grow to N byte multiple
if(dim>size){
memCODE=(unsigned char*)realloc(memCODE,dim);
for(;size<dim;size++) memCODE[size]=0xFF;
}
for(i=k=0,j=0;i<dim;i+=rowN){
for(valid=0;i<dim&&!valid;i+=valid?0:rowN){ //skip empty locations (N words)
valid=0;
for(k=0;k<rowN;k++){ //check row
if(memCODE[i+k]<0xff){
valid=1;
k=rowN;
}
}
}
MemAddr=i<<1;
bufferU[j++]=TX16; //load new address
bufferU[j++]=2;
bufferU[j++]=0x80;
bufferU[j++]=MemAddr>>16;
bufferU[j++]=(MemAddr>>8)&0xFF;
bufferU[j++]=MemAddr&0xFF;
for(k=0;k<rowN;k+=2){ //load all latches
bufferU[j++]=ICSP8_LOAD;
bufferU[j++]=k<(rowN-2)?LOAD_NVM_INC:LOAD_NVM;
bufferU[j++]=memCODE[i+k+1]; //High byte
bufferU[j++]=memCODE[i+k]; //Low byte
if(j>DIMBUF-5){
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
j=0;
}
}
if(i<dim){
bufferU[j++]=ICSP8_SHORT;
bufferU[j++]=BEGIN_INT_PROG; //internally timed, T=2.8ms
bufferU[j++]=WAIT_T3;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(6);
j=0;
if(saveLog){
fprintf(logfile,strings[S_Log7],i,i,0,0); //"i=%d, k=%d 0=%d\n"
}
PrintStatus(strings[S_CodeWriting],i*100/(dim*2+dim2),i); //"Writing: %d%%, addr. %03X"
}
}
PrintStatusEnd();
PrintMessage(strings[S_Compl]); //"completed\r\n"
//****************** verify code ********************
PrintMessage(strings[S_CodeV]); //"Verifying code ... "
PrintStatusSetup();
if(saveLog) fprintf(logfile,"%s\n",strings[S_CodeV]);
j=0;
bufferU[j++]=TX16;
bufferU[j++]=2;
bufferU[j++]=0x80;
bufferU[j++]=0x0;
bufferU[j++]=0x0;
bufferU[j++]=0x0;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
j=0;
for(i=k=0;i<dim;i+=2){
if(j==0){ //skip empty locations (only at the beginning of a packet)
for(x=0;i+x<dim&&memCODE[i+x]==0xFF&&memCODE[i+x+1]==0xFF;x+=2);
if(x>9){ //set new address
i+=x;
k=i;
MemAddr=i<<1;
bufferU[j++]=TX16;
bufferU[j++]=2;
bufferU[j++]=0x80;
bufferU[j++]=MemAddr>>16;
bufferU[j++]=(MemAddr>>8)&0xFF;
bufferU[j++]=MemAddr&0xFF;
}
}
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
if((j+1)/2*3+3>DIMBUF||i==dim-2){ //2B cmd -> 3B data
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
for(z=0;z<DIMBUF-2;z++){
if(bufferI[z]==ICSP8_READ){
if(memCODE[k]!=bufferI[z+2]||memCODE[k+1]!=bufferI[z+1]){
PrintMessage3(strings[S_CodeWError2],k,memCODE[k]+(memCODE[k+1]<<8),(bufferI[z+1]<<8)+bufferI[z+2]); //"Error writing address %3X: written %04X, read %04X\r\n"
err++;
}
z+=2;
k+=2;
}
}
PrintStatus(strings[S_CodeV2],(i+dim)/(dim*2+dim2)*100,i); //"Verify: %d%%, addr. %04X"
j=0;
if(saveLog){
fprintf(logfile,strings[S_Log8],i,i,k,k,err); //"i=%d, k=%d, errors=%d\n"
}
if(err>=max_err){
PrintMessage1(strings[S_MaxErr],err); //"Exceeded maximum number of errors (%d), write interrupted\r\n"
i=dim;
}
}
}
bufferU[j++]=SET_PARAMETER;
bufferU[j++]=SET_T3;
bufferU[j++]=5600>>8;
bufferU[j++]=5600&0xff;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(1);
j=0;
}
PrintStatusEnd();
PrintMessage1(strings[S_ComplErr],err); //"completed, %d errors\r\n"
//****************** write and verify EEPROM ********************
if(dim2){
PrintMessage(strings[S_EEAreaW]); //"Write EEPROM ... "
PrintStatusSetup();
if(saveLog) fprintf(logfile,"WRITE EEPROM\ndim2=%d(0x%X)\n",dim2,dim2);
int errEE=0;
j=0;
MemAddr=EEaddr<<1;
bufferU[j++]=TX16;
bufferU[j++]=2;
bufferU[j++]=0x80;
bufferU[j++]=MemAddr>>16;
bufferU[j++]=(MemAddr>>8)&0xFF;
bufferU[j++]=MemAddr&0xFF;
for(i=k=0;i<dim2;i++){
if(memEE[i]<0xFF){
if(type==1){ //Q43
bufferU[j++]=ICSP8_LOAD;
bufferU[j++]=PROGRAM_DATA;
bufferU[j++]=0;
bufferU[j++]=memEE[i];
}
else if(type==0){ //K42 K83
bufferU[j++]=ICSP8_LOAD;
bufferU[j++]=LOAD_NVM;
bufferU[j++]=0; //High byte
bufferU[j++]=memEE[i]; //Low byte
bufferU[j++]=ICSP8_SHORT;
bufferU[j++]=BEGIN_INT_PROG; //internally timed
}
bufferU[j++]=WAIT_T3; //Tprogram 11ms or 5.6ms
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
w++;
}
else{
bufferU[j++]=ICSP8_SHORT;
bufferU[j++]=INC_ADDR8;
}
if(j>DIMBUF-8||i==dim2-1){
PrintStatus(strings[S_CodeWriting],(i+dim)*100/(dim+dim2),i+EEaddr); //"Writing: %d%%, addr. %03X"
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
w*=type==1?11:5.6; //total write time
PacketIO(w+10);
for(z=0;z<DIMBUF-4;z++){
if(bufferI[z]==ICSP8_SHORT&&memEE[k]==0xff) k++;
else if(bufferI[z]==ICSP8_LOAD){
w=type==1?bufferI[z+4]:bufferI[z+5];
if(memEE[k]!=w){
PrintMessage("\r\n");
PrintMessage3(strings[S_CodeWError2],k,memEE[k],w); //"Error writing address %3X: written %04X, read %04X\r\n"
errEE++;
if(max_err&&err+errEE>max_err){
PrintMessage1(strings[S_MaxErr],err+errEE); //"Exceeded maximum number of errors (%d), write interrupted\r\n"
PrintMessage(strings[S_IntW]); //"Write interrupted"
i=dim2;
z=DIMBUF;
}
}
k++;
z+=type==1?4:5;
}
}
j=0;
w=0;
if(saveLog){
fprintf(logfile,strings[S_Log8],i,i,k,k,errEE); //"i=%d, k=%d, errors=%d,\n"
}
}
}
errEE+=i-k;
err+=errEE;
PrintStatusEnd();
PrintMessage1(strings[S_ComplErr],errEE); //"completed, %d errors\r\n"
}
//****************** write and verify ID ********************
#define IDaddr 0x200000
if(programID){
PrintMessage(strings[S_IDW]); //"Write ID ... "
if(saveLog) fprintf(logfile,"Write ID\n");
int errID=0;
j=0;
MemAddr=IDaddr<<1;
bufferU[j++]=TX16;
bufferU[j++]=2;
bufferU[j++]=0x80;
bufferU[j++]=MemAddr>>16;
bufferU[j++]=(MemAddr>>8)&0xFF;
bufferU[j++]=MemAddr&0xFF;
for(i=k=0;i<UIDlen;i+=2){
if(memID[i]<0xFF||memID[i+1]<0xFF){
if(type==1){ //Q43
bufferU[j++]=ICSP8_LOAD;
bufferU[j++]=PROGRAM_DATA;
bufferU[j++]=memID[i+1];
bufferU[j++]=memID[i];
}
else if(type==0){ //K42 K83
bufferU[j++]=ICSP8_LOAD;
bufferU[j++]=LOAD_NVM;
bufferU[j++]=memID[i+1];
bufferU[j++]=memID[i];
bufferU[j++]=ICSP8_SHORT;
bufferU[j++]=BEGIN_INT_PROG; //internally timed
}
bufferU[j++]=WAIT_T3; //Tprogram 11ms or 5.6ms
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
}
else{
bufferU[j++]=ICSP8_SHORT;
bufferU[j++]=INC_ADDR8;
}
if(j>DIMBUF-8||i==UIDlen-2){
PrintStatus(strings[S_CodeWriting],99,IDaddr+i); //"Writing: %d%%, add. %03X"
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(80);
for(z=0;z<DIMBUF-4;z++){
if(bufferI[z]==ICSP8_SHORT&&memID[k]==0xff&&memID[k+1]==0xff) k+=2;
else if(bufferI[z]==ICSP8_LOAD){
if(type==1) w=(bufferI[z+3]<<8)+bufferI[z+4];
else if(type==0) w=(bufferI[z+4]<<8)+bufferI[z+5];
x=(memID[k+1]<<8)+memID[k];
if(x!=w){
PrintMessage("\r\n");
PrintMessage3(strings[S_CodeWError2],k,x,w); //"Error writing address %3X: written %04X, read %04X\r\n"
errID++;
if(max_err&&err+errID>max_err){
PrintMessage1(strings[S_MaxErr],err+errID); //"Exceeded maximum number of errors (%d), write interrupted\r\n"
PrintMessage(strings[S_IntW]); //"Write interrupted"
i=UIDlen;
z=DIMBUF;
}
}
k+=2;
z+=type==1?4:5;
}
}
j=0;
if(saveLog){
fprintf(logfile,strings[S_Log8],i,i,k,k,errID); //"i=%d, k=%d, errors=%d,\n"
}
}
}
errID+=i-k;
err+=errID;
PrintStatusEnd();
PrintMessage1(strings[S_ComplErr],errID); //"completed, %d errors\r\n"
}
//****************** write and verify CONFIG ********************
#define CONFIGaddr 0x300000
if(err<max_err){
PrintMessage(strings[S_ConfigW]); //"Write CONFIG ..."
if(saveLog) fprintf(logfile,"Write CONFIG\n");
int errC=0;
j=0;
MemAddr=CONFIGaddr<<1;
bufferU[j++]=TX16;
bufferU[j++]=2;
bufferU[j++]=0x80;
bufferU[j++]=MemAddr>>16;
bufferU[j++]=(MemAddr>>8)&0xFF;
bufferU[j++]=MemAddr&0xFF;
for(i=k=0;i<CONFIGlen;i++){
if(type==1){ //Q43
bufferU[j++]=ICSP8_LOAD;
bufferU[j++]=PROGRAM_DATA;
bufferU[j++]=0;
bufferU[j++]=memCONFIG[i];
}
else if(type==0){ //K42 K83
bufferU[j++]=ICSP8_LOAD;
bufferU[j++]=LOAD_NVM;
bufferU[j++]=memCONFIG[i*2+1]; //High byte
bufferU[j++]=memCONFIG[i*2]; //Low byte
bufferU[j++]=ICSP8_SHORT;
bufferU[j++]=BEGIN_INT_PROG; //internally timed
}
bufferU[j++]=WAIT_T3; //Tprogram 11ms or 5.6ms
bufferU[j++]=ICSP8_READ;
bufferU[j++]=READ_NVM_INC;
if(j>DIMBUF-8||i==CONFIGlen-1){
PrintStatus(strings[S_CodeWriting],99,i+CONFIGaddr); //"Writing: %d%%, addr. %03X"
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(80);
for(z=0;z<DIMBUF-4;z++){
if(bufferI[z]==ICSP8_LOAD){
if(type==1){ //Q43 single byte
w=bufferI[z+4];
x=memCONFIG[k];
}
else if(type==0){ //K42 K83 2 bytes
w=(bufferI[z+4]<<8)+bufferI[z+5];
x=(memCONFIG[k+1]<<8)+memCONFIG[k];
}
if(~x&w){ //error if written 0 and read 1 (~Written&Read)
PrintMessage("\r\n");
PrintMessage3(strings[S_CodeWError2],k+CONFIGaddr,x,w); //"Error writing address %3X: written %04X, read %04X\r\n"
errC++;
if(max_err&&err+errC>max_err){
PrintMessage1(strings[S_MaxErr],err+errC); //"Exceeded maximum number of errors (%d), write interrupted\r\n"
PrintMessage(strings[S_IntW]); //"Write interrupted"
i=dim2;
z=DIMBUF;
}
}
if(type==1){ //Q43 single byte
k++;
z+=4;
}
else if(type==0){ //K42 K83 2 bytes
k+=2;
z+=5;
}
}
}
j=0;
if(saveLog){
fprintf(logfile,strings[S_Log8],i,i,k,k,errC); //"i=%d, k=%d, errors=%d,\n"
}
}
}
errC+=10-k;
err+=errC;
PrintStatusEnd();
PrintMessage1(strings[S_ComplErr],errC); //"completed, %d errors\r\n"
}
bufferU[j++]=EN_VPP_VCC; //VDD
bufferU[j++]=1;
bufferU[j++]=EN_VPP_VCC; //0
bufferU[j++]=0x0;
bufferU[j++]=SET_CK_D;
bufferU[j++]=0x0;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(1);
unsigned int stop=GetTickCount();
sprintf(str,strings[S_EndErr],(stop-start)/1000.0,err,err!=1?strings[S_ErrPlur]:strings[S_ErrSing]); //"\r\nEnd (%.2f s) %d %s\r\n\r\n"
PrintMessage(str);
if(saveLog){
fprintf(logfile,str);
CloseLogFile();
}
PrintStatusClear(); //clear status report
}
|