-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogEEPROM.c
2143 lines (2121 loc) · 58.3 KB
/
progEEPROM.c
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
/**
* \file progEEPROM.c
* algorithms to program various EEPROM types
* Copyright (C) 2009-2016 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/>
*/
#include "common.h"
void ReadI2C(int dim,int addr)
// read I2C memories
// dim=size in bytes
// addr:
// [3:0] =0: 1 byte address =1: 2 byte address
// [7:4] A2:A0 value
// [11:8] 17th address bit location (added to control byte)
{
int k=0,z=0,i,j;
int AX=(addr>>4)&7;
int addr17=(addr>>8)&0xF;
addr&=1;
if(dim>0x30000||dim<0){
PrintMessage(strings[S_EELim]); //"EEPROM size out of limits\r\n"
return;
}
if(saveLog){
OpenLogFile(); //"Log.txt"
fprintf(logfile,"ReadI2C(%d,%d) (0x%X,0x%X)\n",dim,addr,dim,addr);
}
sizeEE=dim;
if(memEE) free(memEE);
memEE=(unsigned char*)malloc(dim); //EEPROM
unsigned int start=GetTickCount();
hvreg=0;
j=0;
bufferU[j++]=VREG_DIS;
bufferU[j++]=I2C_INIT;
bufferU[j++]=AX; //100k
bufferU[j++]=EN_VPP_VCC; //VDD
bufferU[j++]=0x1;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(5);
//****************** read ********************
PrintMessage(strings[S_ReadEE]); //read EEPROM ...
PrintStatusSetup();
int inc;
for(i=0,j=0;i<dim;i+=inc){
if(i<0x10000&&i>0x10000-(DIMBUF-4)) inc=0x10000-i; //do not cross 64KB boundary
else inc=i<dim-(DIMBUF-4)?DIMBUF-4:dim-i;
if(!addr){ //1 byte address
bufferU[j++]=I2C_READ;
bufferU[j++]=inc;
bufferU[j++]=0xA0+(i>>7&0x0E);
bufferU[j++]=i&0xFF;
}
else{ //2 byte address
bufferU[j++]=I2C_READ2;
bufferU[j++]=inc;
bufferU[j++]=0xA0+(i>0xFFFF?addr17:0); //17th bit if>64K
bufferU[j++]=(i>>8)&0xFF;
bufferU[j++]=i&0xFF;
}
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(8);
for(j=0;j<DIMBUF-1&&bufferI[j]!=I2C_READ&&bufferI[j]!=I2C_READ2;j++);
if(j<DIMBUF-1&&bufferI[j+1]<0xFA){
for(z=j+2;z<j+2+bufferI[j+1]&&z<DIMBUF;z++) memEE[k++]=bufferI[z];
}
PrintStatus(strings[S_CodeReading2],i*100/(dim),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_ReadEEErr],dim,k); //"Error reading EEPROM area, requested %d bytes, read %d\r\n"
sizeEE=k;
}
else PrintMessage(strings[S_Compl]);
//****************** exit ********************
bufferU[j++]=EN_VPP_VCC; //0
bufferU[j++]=0x0;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
unsigned int stop=GetTickCount();
PrintStatusClear();
DisplayEE(); //visualize
int sum=0;
for(i=0;i<sizeEE;i++) sum+=memEE[i];
PrintMessage1("Checksum: 0x%X\r\n",sum&0xFFFF);
sprintf(str,strings[S_End],(stop-start)/1000.0); //"\r\nEnd (%.2f s)\r\n"
PrintMessage(str);
if(saveLog){
fprintf(logfile,str);
CloseLogFile();
}
}
void WriteI2C(int dim,int addr,int page)
// write I2C memories
// dim=size in bytes
// addr:
// [3:0] =0: 1 byte address =1: 2 byte address
// [7:4] A2:A0 value
// [11:8] 17th address bit location (added to control byte)
// page=page size
{
int k=0,z=0,i,j;
int err=0;
int AX=(addr>>4)&7;
int addr17=(addr>>8)&0xF;
addr&=1;
hvreg=0;
if(dim>0x30000||dim<0){
PrintMessage(strings[S_EELim]); //"EEPROM size out of limits\r\n"
return;
}
if(saveLog){
OpenLogFile(); //"Log.txt"
fprintf(logfile,"WriteI2C(%d,%d,%d) (0x%X,0x%X)\n",dim,addr,page,dim,addr);
}
if(dim>sizeEE){
i=sizeEE;
memEE=(unsigned char*)realloc(memEE,dim);
for(;i<dim;i++) memEE[i]=0xFF;
sizeEE=dim;
}
if(dim<1){
PrintMessage(strings[S_NoCode]); //"Data area is empty\r\n"
return;
}
unsigned int start=GetTickCount();
j=0;
bufferU[j++]=VREG_DIS;
bufferU[j++]=I2C_INIT;
bufferU[j++]=AX; //100k
bufferU[j++]=EN_VPP_VCC; //VDD
bufferU[j++]=0x1;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(5);
//****************** write ********************
PrintMessage(strings[S_EEAreaW]); //"Write EEPROM ... "
PrintStatusSetup();
for(;page>=DIMBUF-6;page>>=1);
for(i=0,j=0;i<dim;i+=page){
bufferU[j++]=I2C_WRITE;
if(!addr){ //1 byte address
bufferU[j++]=page;
bufferU[j++]=0xA0+(i>>7&0x0E);
bufferU[j++]=i&0xFF;
}
else{ //2 byte address
bufferU[j++]=page+1;
bufferU[j++]=0xA0+(i>0xFFFF?addr17:0); //17th bit if>64K
bufferU[j++]=(i>>8)&0xFF;
bufferU[j++]=i&0xFF;
}
for(k=0;k<page;k++) bufferU[j++]=memEE[i+k];
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(3);
for(j=0;j<DIMBUF-1&&bufferI[j]!=I2C_WRITE;j++);
if(bufferI[j]!=I2C_WRITE||bufferI[j+1]>=0xFA) i=dim+10;
PrintStatus(strings[S_CodeWriting2],i*100/(dim),i); //"Write: %d%%, addr. %04X"
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"
}
bufferU[j++]=I2C_WRITE;
bufferU[j++]=0;
bufferU[j++]=0xA0; //ACK polling
bufferU[j++]=0;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
int ack=0xFD;
for(j=0;ack==0xFD&&j<20;j++){ //ACK polling until write complete
PacketIO(2);
for(j=0;j<DIMBUF-1&&bufferI[j]!=I2C_WRITE;j++);
if(bufferI[j]!=I2C_WRITE||bufferI[j+1]>=0xFA) ack=0xFD;
else ack=bufferI[j+1];
}
j=0;
}
PrintStatusEnd();
PrintMessage(strings[S_Compl]); //"completed\r\n"
//****************** verify EEPROM ********************
PrintMessage(strings[S_EEV]); //"Verify EEPROM ... "
PrintStatusSetup();
k=0;
int inc;
for(i=0,j=0;i<dim;i+=inc){
if(i<0x10000&&i>0x10000-(DIMBUF-4)) inc=0x10000-i; //do not cross 64KB boundary
else inc=i<dim-(DIMBUF-4)?DIMBUF-4:dim-i;
if(!addr){ //1 byte address
bufferU[j++]=I2C_READ;
bufferU[j++]=inc;
bufferU[j++]=0xA0+(i>>7&0x0E);
bufferU[j++]=i&0xFF;
}
else{ //2 byte address
bufferU[j++]=I2C_READ2;
bufferU[j++]=inc;
bufferU[j++]=0xA0+(i>0xFFFF?addr17:0); //17th bit if>64K
bufferU[j++]=(i>>8)&0xFF;
bufferU[j++]=i&0xFF;
}
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(8);
for(j=0;j<DIMBUF-1&&bufferI[j]!=I2C_READ&&bufferI[j]!=I2C_READ2;j++);
if((bufferI[j]==I2C_READ||bufferI[j]==I2C_READ2)&&bufferI[j+1]<0xFA){
for(z=j+2;z<(j+2+bufferI[j+1])&&z<DIMBUF;z++){
if(memEE[k++]!=bufferI[z]){
PrintMessage("\r\n");
PrintMessage4(strings[S_CodeVError],i+z-3,i+z-3,memEE[k-1],bufferI[z]); //"Error verifying address %04X (%d), written %02X, read %02X\r\n"
err++;
}
}
}
PrintStatus(strings[S_CodeV2],i*100/(dim),i); //"Verify: %d%%, addr. %04X"
if(RWstop) i=dim;
j=0;
if(saveLog){
fprintf(logfile,strings[S_Log8],i,i,k,k,err); //"i=%d, k=%d, err=%d\n"
}
if(err>=max_err) break;
}
PrintStatusEnd();
if(k!=dim){
PrintMessage("\r\n");
PrintMessage2(strings[S_ReadEEErr],dim,k); //"Error reading EEPROM area, requested %d bytes, read %d\r\n"
}
PrintMessage1(strings[S_ComplErr],err); //"completed: %d errors\r\n"
//****************** exit ********************
bufferU[j++]=EN_VPP_VCC;
bufferU[j++]=0x0;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
unsigned int stop=GetTickCount();
PrintStatusClear();
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();
}
}
#define PRE 0x08 //RB3
#define S 0x10 //RB4
#define W 0x20 //RB5
#define ORG 0x20 //RB5
void Read93x(int dim,int na,int options)
// read 93Sx6 uW memories
// dim=size in bytes
// na=address bits
// options=0: x16 organization =1: x8 organization
{
int k=0,z=0,i,j,x8;
hvreg=0;
if(dim>0x3000||dim<0){
PrintMessage(strings[S_EELim]); //"EEPROM size out of limits\r\n"
return;
}
if(na>13) na=13;
if(saveLog){
OpenLogFile(); //"Log.txt"
fprintf(logfile,"Read93x(%d,%d,%d) (0x%X,0x%X)\n",dim,na,options,dim,na);
}
x8=options&1;
sizeEE=dim;
if(memEE) free(memEE);
memEE=(unsigned char*)malloc(dim); //EEPROM
unsigned int start=GetTickCount();
j=0;
bufferU[j++]=VREG_DIS;
bufferU[j++]=uW_INIT;
bufferU[j++]=EN_VPP_VCC; //VDD
bufferU[j++]=0x1;
bufferU[j++]=EXT_PORT;
bufferU[j++]=x8?S:S+ORG;
bufferU[j++]=0;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(5);
//****************** read ********************
PrintMessage(strings[S_ReadEE]); //read EEPROM ...
PrintStatusSetup();
int dim2=x8?dim:dim/2;
for(i=0;i<dim2;){
for(j=0;j<DIMBUF-14&&i<dim2;){
bufferU[j++]=uWTX;
bufferU[j++]=na+3; //READ
bufferU[j++]=0xC0+((i>>(na-5))&0x1F); //110aaaaa aaax0000
bufferU[j++]=(i<<(13-na))&0xFF;
bufferU[j++]=uWRX;
bufferU[j++]=x8?8:16;
bufferU[j++]=EXT_PORT;
bufferU[j++]=x8?0:ORG;
bufferU[j++]=0;
bufferU[j++]=EXT_PORT;
bufferU[j++]=x8?S:S+ORG;
bufferU[j++]=0;
i++;
}
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
for(z=0;z<DIMBUF-3;z++){
for(;bufferI[z]!=uWRX&&z<DIMBUF-3;z++);
if(bufferI[z]==uWRX){
if(x8) memEE[k++]=bufferI[z+2];
else{
memEE[k+1]=bufferI[z+2];
memEE[k]=bufferI[z+3];
k+=2;
}
z+=3;
}
}
PrintStatus(strings[S_CodeReading2],i*100/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_ReadEEErr],dim,k); //"Error reading EEPROM area, requested %d bytes, read %d\r\n"
sizeEE=k;
}
else PrintMessage(strings[S_Compl]);
//****************** exit ********************
bufferU[j++]=EXT_PORT;
bufferU[j++]=0;
bufferU[j++]=0;
bufferU[j++]=EN_VPP_VCC; //0
bufferU[j++]=0x0;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
unsigned int stop=GetTickCount();
PrintStatusClear();
DisplayEE(); //visualize
int sum=0;
for(i=0;i<sizeEE;i++) sum+=memEE[i];
PrintMessage1("Checksum: 0x%X\r\n",sum&0xFFFF);
sprintf(str,strings[S_End],(stop-start)/1000.0); //"\r\nEnd (%.2f s)\r\n"
PrintMessage(str);
if(saveLog){
fprintf(logfile,str);
CloseLogFile();
}
}
void Write93Sx(int dim,int na,int page)
// write 93Sx6 uW memories
// dim=size in bytes
// na=address bits
// page=page size (bytes)
// automatic write delay
{
int k=0,z=0,i,j;
int err=0;
hvreg=0;
if(dim>0x1000||dim<0){
PrintMessage(strings[S_EELim]); //"EEPROM size out of limits\r\n"
return;
}
if(na>13) na=13;
if(page>48) page=48;
if(saveLog){
OpenLogFile(); //"Log.txt"
fprintf(logfile,"Write93Sx(%d,%d,%d) (0x%X,0x%X)\n",dim,na,page,dim,na);
}
if(dim>sizeEE){
i=sizeEE;
memEE=(unsigned char*)realloc(memEE,dim);
for(;i<dim;i++) memEE[i]=0xFF;
sizeEE=dim;
}
if(dim<1){
PrintMessage(strings[S_NoCode]); //"Data area is empty\r\n"
return;
}
unsigned int start=GetTickCount();
j=0;
bufferU[j++]=VREG_DIS;
bufferU[j++]=EXT_PORT;
bufferU[j++]=0;
bufferU[j++]=0;
bufferU[j++]=uW_INIT;
bufferU[j++]=EN_VPP_VCC; //VDD
bufferU[j++]=0x1;
bufferU[j++]=WAIT_T3;
bufferU[j++]=EXT_PORT;
bufferU[j++]=S+W;
bufferU[j++]=0;
bufferU[j++]=uWTX;
bufferU[j++]=na+3;
bufferU[j++]=0x98; //100 11xxx write enable
bufferU[j++]=0;
bufferU[j++]=EXT_PORT;
bufferU[j++]=W;
bufferU[j++]=0;
bufferU[j++]=EXT_PORT;
bufferU[j++]=S+W+PRE;
bufferU[j++]=0;
bufferU[j++]=uWTX;
bufferU[j++]=na+3;
bufferU[j++]=0x98; //100 11xxx Prot. reg. enable
bufferU[j++]=0;
bufferU[j++]=EXT_PORT;
bufferU[j++]=W+PRE;
bufferU[j++]=0;
bufferU[j++]=EXT_PORT;
bufferU[j++]=S+W+PRE;
bufferU[j++]=0;
bufferU[j++]=uWTX;
bufferU[j++]=na+3;
bufferU[j++]=0xFF; //111 11111111 Prot. reg. clear
bufferU[j++]=0xF0;
bufferU[j++]=EXT_PORT;
bufferU[j++]=W+PRE;
bufferU[j++]=0;
bufferU[j++]=EXT_PORT;
bufferU[j++]=S+W;
bufferU[j++]=0;
bufferU[j++]=uWTX;
bufferU[j++]=na+3;
bufferU[j++]=0x98; //100 11xxx write enable
bufferU[j++]=0;
bufferU[j++]=EXT_PORT;
bufferU[j++]=W;
bufferU[j++]=0;
bufferU[j++]=EXT_PORT;
bufferU[j++]=S+W+PRE;
bufferU[j++]=0;
bufferU[j++]=uWTX;
bufferU[j++]=na+3;
bufferU[j++]=0xC0; //110 xxxxx Prot. reg. read
bufferU[j++]=0;
bufferU[j++]=uWRX;
bufferU[j++]=10;
bufferU[j++]=EXT_PORT;
bufferU[j++]=W+PRE;
bufferU[j++]=0;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(5);
//****************** write ********************
PrintMessage(strings[S_EEAreaW]); //"Write EEPROM ... "
PrintStatusSetup();
int addr=0;
for(i=0,j=0;i<dim;i+=page,addr+=(0x10000>>na)*page/2){
bufferU[j++]=EXT_PORT;
bufferU[j++]=W; //make sure to start with S=0
bufferU[j++]=0;
bufferU[j++]=EXT_PORT;
bufferU[j++]=S+W;
bufferU[j++]=0;
bufferU[j++]=uWTX;
bufferU[j++]=3;
bufferU[j++]=0xE0; //111aaaaa aaa(a) D page write
bufferU[j++]=uWTX;
bufferU[j++]=na;
bufferU[j++]=addr>>8;
if(na>8) bufferU[j++]=addr&0xFF;
bufferU[j++]=uWTX;
bufferU[j++]=8*page;
for(k=0;k<page;k+=2){
bufferU[j++]=memEE[i+k+1];
bufferU[j++]=memEE[i+k];
}
bufferU[j++]=EXT_PORT;
bufferU[j++]=W;
bufferU[j++]=0;
bufferU[j++]=EXT_PORT;
bufferU[j++]=S+W; //S=1 to check status
bufferU[j++]=0;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
j=0;
for(j=0;j<DIMBUF-1&&bufferI[j]!=uWTX;j++);
if(bufferI[j]!=uWTX||bufferI[j+1]>=0xFA) i=dim+10;
bufferU[j++]=uWRX;
bufferU[j++]=1;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
for(z=0,k=0;z<30&&!k;z++){ //Wait until ready
PacketIO(2);
for(j=0;j<DIMBUF-1&&bufferI[j]!=uWRX;j++);
if(bufferI[j]==uWRX) k=bufferI[j+2];
}
j=0;
PrintStatus(strings[S_CodeWriting2],i*100/(dim),i); //"Write: %d%%, addr. %04X"
if(RWstop) i=dim;
if(saveLog){
fprintf(logfile,strings[S_Log7],i,i,k,k); //"i=%d(0x%X), k=%d(0x%X)\n"
}
}
PrintStatusEnd();
PrintMessage(strings[S_Compl]); //"completed\r\n"
//****************** verify EEPROM ********************
PrintMessage(strings[S_EEV]); //"Verify EEPROM ... "
PrintStatusSetup();
j=0;
bufferU[j++]=EXT_PORT;
bufferU[j++]=S;
bufferU[j++]=0;
bufferU[j++]=uWTX;
bufferU[j++]=na+3; //READ (16bit)
bufferU[j++]=0xC0; //110aaaaa aaax0000
bufferU[j++]=0;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
k=0;
int n=(DIMBUF-2);
if(n>30) n=30; //max 240 bit = 30 Byte
for(i=0,j=0;i<dim;i+=n){
bufferU[j++]=uWRX;
bufferU[j++]=i<(dim-n)?n*8:(dim-i)*8;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
for(j=0;bufferI[j]!=uWRX&&j<DIMBUF-1;j++);
if(bufferI[j]==uWRX){
for(z=j+2;z<j+2+bufferI[j+1]/8&&z<DIMBUF;z+=2,k+=2){
if(memEE[k+1]!=bufferI[z]){
PrintMessage("\r\n");
PrintMessage4(strings[S_CodeVError],i+z-3,i+z-3,memEE[k+1],bufferI[z]); //"Error verifying address %04X (%d), written %02X, read %02X\r\n"
err++;
}
if(memEE[k]!=bufferI[z+1]){
PrintMessage("\r\n");
PrintMessage4(strings[S_CodeVError],i+z-3,i+z-3,memEE[k],bufferI[z+1]); //"Error verifying address %04X (%d), written %02X, read %02X\r\n"
err++;
}
}
}
PrintStatus(strings[S_CodeV2],i*100/(dim),i); //"Verify: %d%%, addr. %04X"
if(RWstop) i=dim;
j=0;
if(saveLog){
fprintf(logfile,strings[S_Log8],i,i,k,k,err); //"i=%d, k=%d, err=%d\n"
}
if(err>=max_err) break;
}
PrintStatusEnd();
if(k!=dim){
PrintMessage("\r\n");
PrintMessage2(strings[S_ReadEEErr],dim,k); //"Error reading EEPROM area, requested %d bytes, read %d\r\n"
}
PrintMessage1(strings[S_ComplErr],err); //"completed: %d errors\r\n"
//****************** exit ********************
bufferU[j++]=EXT_PORT;
bufferU[j++]=0;
bufferU[j++]=0;
bufferU[j++]=EN_VPP_VCC;
bufferU[j++]=0x0;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
unsigned int stop=GetTickCount();
PrintStatusClear();
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();
}
}
void Write93Cx(int dim,int na, int options)
// write 93Cx6 uW memories
// dim=size in bytes
// na=address bits
// options=0: x16 organization =1: x8 organization
{
int k=0,z=0,i,j;
int err=0;
hvreg=0;
if(dim>0x1000||dim<0){
PrintMessage(strings[S_EELim]); //"EEPROM size out of limits\r\n"
return;
}
if(na>13) na=13;
if(saveLog){
OpenLogFile(); //"Log.txt"
fprintf(logfile,"Write93Cx(%d,%d,%d) (0x%X,0x%X)\n",dim,na,options,dim,na);
}
if(dim>sizeEE){
i=sizeEE;
memEE=(unsigned char*)realloc(memEE,dim);
for(;i<dim;i++) memEE[i]=0xFF;
sizeEE=dim;
}
if(dim<1){
PrintMessage(strings[S_NoCode]); //"Data area is empty\r\n"
return;
}
unsigned int start=GetTickCount();
j=0;
bufferU[j++]=VREG_DIS;
bufferU[j++]=EXT_PORT;
bufferU[j++]=0;
bufferU[j++]=0;
bufferU[j++]=uW_INIT;
bufferU[j++]=EN_VPP_VCC; //VDD
bufferU[j++]=0x1;
bufferU[j++]=WAIT_T3;
bufferU[j++]=EXT_PORT;
bufferU[j++]=options==0?S+ORG+PRE:S+PRE;
bufferU[j++]=0;
bufferU[j++]=uWTX;
bufferU[j++]=na+3;
bufferU[j++]=0x98; //100 11xxx EWEN
bufferU[j++]=0;
bufferU[j++]=EXT_PORT;
bufferU[j++]=options==0?ORG+PRE:PRE;
bufferU[j++]=0;
bufferU[j++]=EXT_PORT;
bufferU[j++]=options==0?S+ORG+PRE:S+PRE;
bufferU[j++]=0;
bufferU[j++]=uWTX;
bufferU[j++]=na+3;
bufferU[j++]=0x90; //100 10xxx ERAL
bufferU[j++]=0;
bufferU[j++]=EXT_PORT;
bufferU[j++]=options==0?ORG+PRE:PRE;
bufferU[j++]=0;
bufferU[j++]=EXT_PORT;
bufferU[j++]=options==0?S+ORG+PRE:S+PRE;
bufferU[j++]=0;
bufferU[j++]=uWRX;
bufferU[j++]=1;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(5);
j=0;
bufferU[j++]=uWRX;
bufferU[j++]=1;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
for(i=0,k=0;i<30&&!k;i++){ //Wait until ready
PacketIO(2);
for(j=0;j<DIMBUF-1&&bufferI[j]!=uWRX;j++);
if(bufferI[j]==uWRX) k=bufferI[j+2];
}
//****************** write ********************
PrintMessage(strings[S_EEAreaW]); //"Write EEPROM ... "
PrintStatusSetup();
int addr=0;
j=0;
for(i=0;i<dim;i+=options==0?2:1,addr+=0x10000>>na){
if(memEE[i]<0xFF||(options==0&&memEE[i+1]<0xFF)){
bufferU[j++]=EXT_PORT;
bufferU[j++]=options==0?ORG+PRE:PRE;
bufferU[j++]=0;
bufferU[j++]=EXT_PORT;
bufferU[j++]=options==0?S+ORG+PRE:S+PRE;
bufferU[j++]=0;
bufferU[j++]=uWTX;
bufferU[j++]=3;
bufferU[j++]=0xA0; //101aaaaa aaa(a) write
bufferU[j++]=uWTX;
bufferU[j++]=na;
bufferU[j++]=addr>>8;
if(na>8) bufferU[j++]=addr&0xFF;
bufferU[j++]=uWTX;
if(options==0){ //x16
bufferU[j++]=16;
bufferU[j++]=memEE[i+1];
bufferU[j++]=memEE[i];
}
else{ //x8
bufferU[j++]=8;
bufferU[j++]=memEE[i];
}
bufferU[j++]=EXT_PORT;
bufferU[j++]=options==0?ORG+PRE:PRE;
bufferU[j++]=0;
bufferU[j++]=EXT_PORT;
bufferU[j++]=options==0?S+ORG+PRE:S+PRE;
bufferU[j++]=0;
bufferU[j++]=uWRX;
bufferU[j++]=1;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
PrintStatus(strings[S_CodeWriting2],i*100/(dim),i); //"Write: %d%%, addr. %04X"
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"
}
bufferU[j++]=uWRX;
bufferU[j++]=1;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
for(z=0,k=0;z<30&&!k;z++){ //Wait until ready
PacketIO(2);
for(j=0;j<DIMBUF-1&&bufferI[j]!=uWRX;j++);
if(bufferI[j]==uWRX) k=bufferI[j+2];
}
j=0;
}
}
msDelay(2);
PrintStatusEnd();
if(i!=dim){
PrintMessage2(strings[S_CodeWError4],i,dim); //"Error writing code area, requested %d bytes, read %d\r\n"
}
else PrintMessage(strings[S_Compl]); //"completed\r\n"
//****************** verify EEPROM ********************
PrintMessage(strings[S_EEV]); //"Verify EEPROM ... "
PrintStatusSetup();
j=0;
bufferU[j++]=EXT_PORT;
bufferU[j++]=options==0?ORG+PRE:PRE;
bufferU[j++]=0;
bufferU[j++]=EXT_PORT;
bufferU[j++]=options==0?S+ORG+PRE:S+PRE;
bufferU[j++]=0;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
k=0;
int dim2=options==0?dim/2:dim;
for(i=0;i<dim2;){
for(j=0;j<DIMBUF-14&&i<dim2;){
bufferU[j++]=uWTX;
bufferU[j++]=na+3; //READ
bufferU[j++]=0xC0+((i>>(na-5))&0x1F); //110aaaaa aaax0000
bufferU[j++]=(i<<(13-na))&0xFF;
bufferU[j++]=uWRX;
bufferU[j++]=options==0?16:8;
bufferU[j++]=EXT_PORT;
bufferU[j++]=options==0?ORG:0;
bufferU[j++]=0;
bufferU[j++]=EXT_PORT;
bufferU[j++]=options==0?S+ORG:S;
bufferU[j++]=0;
i++;
}
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
for(z=0;z<DIMBUF-3;z++){
for(;bufferI[z]!=uWRX&&z<DIMBUF-3;z++);
if(bufferI[z]==uWRX){
if(options==1){ //x8
if(memEE[k]!=bufferI[z+2]){
PrintMessage("\r\n");
PrintMessage4(strings[S_CodeVError],k,k,memEE[k],bufferI[z+2]); //"Error verifying address %04X (%d), written %02X, read %02X\r\n"
err++;
}
k++;
}
else{ //x16
if(memEE[k]!=bufferI[z+3]){
PrintMessage("\r\n");
PrintMessage4(strings[S_CodeVError],k,k,memEE[k],bufferI[z+3]); //"Error verifying address %04X (%d), written %02X, read %02X\r\n"
err++;
}
if(memEE[k+1]!=bufferI[z+2]){
PrintMessage("\r\n");
PrintMessage4(strings[S_CodeVError],k+1,k+1,memEE[k+1],bufferI[z+2]); //"Error verifying address %04X (%d), written %02X, read %02X\r\n"
err++;
}
k+=2;
}
z+=3;
}
}
PrintStatus(strings[S_CodeV2],i*100/dim2,i); //"Verify: %d%%, addr. %04X"
if(RWstop) i=dim;
j=0;
if(saveLog){
fprintf(logfile,strings[S_Log8],i,i,k,k,err); //"i=%d, k=%d, err=%d\n"
}
if(err>=max_err) break;
}
PrintStatusEnd();
if(k!=dim){
PrintMessage("\r\n");
PrintMessage2(strings[S_ReadEEErr],dim,k); //"Error reading EEPROM area, requested %d bytes, read %d\r\n"
}
PrintMessage1(strings[S_ComplErr],err); //"completed: %d errors\r\n"
//****************** exit ********************
bufferU[j++]=EXT_PORT;
bufferU[j++]=0;
bufferU[j++]=0;
bufferU[j++]=EN_VPP_VCC;
bufferU[j++]=0x0;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
unsigned int stop=GetTickCount();
PrintStatusClear();
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();
}
}
#define CS 8
#define HLD 16 //Hold
#define WP 0x40 //Write protect
void Read25xx(int dim)
// read 25xx SPI memories
// dim=size in bytes
{
int k=0,z=0,i,j,ID;
hvreg=0;
if(dim>0x1000000||dim<0){
PrintMessage(strings[S_EELim]); //"EEPROM size out of limits\r\n"
return;
}
if(saveLog){
OpenLogFile(); //"Log.txt"
fprintf(logfile,"Read25xx(%d) (0x%X)\n",dim,dim);
}
sizeEE=dim;
if(memEE) free(memEE);
memEE=(unsigned char*)malloc(dim); //EEPROM
unsigned int start=GetTickCount();
j=0;
bufferU[j++]=VREG_DIS;
bufferU[j++]=SPI_INIT;
bufferU[j++]=3; //0=100k, 1=200k, 2=300k, 3=500k (in reality 200k)
bufferU[j++]=SET_T1T2;
bufferU[j++]=2; //force T=28us -> 286 kbps
bufferU[j++]=0;
bufferU[j++]=EN_VPP_VCC; //VDD
bufferU[j++]=0x1;
bufferU[j++]=WAIT_T3;
bufferU[j++]=EXT_PORT; //CS=1, HLD=1, WP=0
bufferU[j++]=CS+HLD;
bufferU[j++]=0;
bufferU[j++]=EXT_PORT; //CS=0, HLD=1, WP=1
bufferU[j++]=HLD;
bufferU[j++]=WP;
bufferU[j++]=SPI_WRITE; //READ ID
bufferU[j++]=1;
bufferU[j++]=0x9F;
bufferU[j++]=SPI_READ;
bufferU[j++]=3;
bufferU[j++]=EXT_PORT; //CS=1, HLD=1, WP=1
bufferU[j++]=CS+HLD;
bufferU[j++]=WP;
bufferU[j++]=EXT_PORT; //CS=0, HLD=1, WP=0
bufferU[j++]=HLD;
bufferU[j++]=0;
bufferU[j++]=SPI_WRITE; //Read
if(dim>0x10000){ //24 bit address
bufferU[j++]=4;
bufferU[j++]=3;
bufferU[j++]=0;
bufferU[j++]=0;
bufferU[j++]=0;
}
else if(dim>0x200){ //16 bit address
bufferU[j++]=3;
bufferU[j++]=3;
bufferU[j++]=0;
bufferU[j++]=0;
}
else{ //8 bit address
bufferU[j++]=2;
bufferU[j++]=3;
bufferU[j++]=0;
}
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(5);
for(z=0;z<DIMBUF-4&&bufferI[z]!=SPI_READ;z++);
ID=(bufferI[z+2]<<16)+(bufferI[z+3]<<8)+bufferI[z+4];
if(ID>0&&ID!=0xFFFFFF) PrintMessage1("DEVICE ID=0x%06X\r\n",ID);
//****************** read ********************
PrintMessage(strings[S_ReadEE]); //read EEPROM ...
PrintStatusSetup();
for(i=0,j=0;i<dim;i+=DIMBUF-4){
bufferU[j++]=SPI_READ;
bufferU[j++]=i<dim-(DIMBUF-4)?DIMBUF-4:dim-i;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(4);
for(j=0;j<DIMBUF-1&&bufferI[j]!=SPI_READ;j++);
if(bufferI[j]==SPI_READ&&bufferI[j+1]<0xFA){
for(z=j+2;z<j+2+bufferI[j+1]&&z<DIMBUF;z++) memEE[k++]=bufferI[z];
}
PrintStatus(strings[S_CodeReading2],i*100/(dim),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_ReadEEErr],dim,k); //"Error reading EEPROM area, requested %d bytes, read %d\r\n"
sizeEE=k;
}
else PrintMessage(strings[S_Compl]);
//****************** exit ********************
bufferU[j++]=EXT_PORT;
bufferU[j++]=0;
bufferU[j++]=0;
bufferU[j++]=EN_VPP_VCC; //0
bufferU[j++]=0x0;
bufferU[j++]=FLUSH;
for(;j<DIMBUF;j++) bufferU[j]=0x0;
PacketIO(2);
unsigned int stop=GetTickCount();
PrintStatusClear();
DisplayEE(); //visualize
int sum=0;
for(i=0;i<sizeEE;i++) sum+=memEE[i];
PrintMessage1("Checksum: 0x%X\r\n",sum&0xFFFF);
sprintf(str,strings[S_End],(stop-start)/1000.0); //"\r\nEnd (%.2f s)\r\n"
PrintMessage(str);
if(saveLog){
fprintf(logfile,str);
CloseLogFile();
}
}
void Write25xx(int dim,int options)
// write SPI memories
// dim=size in bytes
// options:
// [11:0]=page size
// [12]=erase before write
// [13]=use status register 2
// automatic write delay
{
int k=0,z=0,i,j,ID;
int err=0;
hvreg=0;
int page=options&0xFFF;
if(dim>0x1000000||dim<0){
PrintMessage(strings[S_EELim]); //"EEPROM size out of limits\r\n"
return;
}
if(saveLog){
OpenLogFile(); //"Log.txt"
fprintf(logfile,"Write25xx(%d,%d) (0x%X,0x%X)\n",dim,options,dim,options);
}
if(dim>sizeEE){
i=sizeEE;
memEE=(unsigned char*)realloc(memEE,dim);
for(;i<dim;i++) memEE[i]=0xFF;
sizeEE=dim;
}
if(dim<1){
PrintMessage(strings[S_NoCode]); //"Data area is empty\r\n"
return;
}
unsigned int start=GetTickCount();
j=0;
bufferU[j++]=VREG_DIS;
bufferU[j++]=SPI_INIT;
bufferU[j++]=3; //0=100k, 1=200k, 2=300k, 3=500k
bufferU[j++]=EN_VPP_VCC; //VDD
bufferU[j++]=0x1;
bufferU[j++]=WAIT_T3;
bufferU[j++]=EXT_PORT; //CS=1, HLD=1, WP=1