forked from DediProgSW/SF100Linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.c
executable file
·993 lines (822 loc) · 20.7 KB
/
board.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
#include "Macro.h"
#include "usbdriver.h"
#include "board.h"
#define SerialFlash_FALSE -1
#define SerialFlash_TRUE 1
#define min(a,b) (a>b? b:a)
volatile bool g_bIsSF600=false;
extern char g_board_type[8];
extern int g_firmversion;
extern unsigned int g_IO1Select;
extern unsigned int g_IO4Select;
extern unsigned int g_Vcc;
extern void Sleep(unsigned int ms);
extern bool Is_NewUSBCommand(int Index);
//extern int is_SF100nBoardVersionGreaterThan_5_2_0(int Index);
//extern int is_SF600nBoardVersionGreaterThan_6_9_0(int Index);
void QueryBoard(int Index)
{
// printf("QueryBoard\r\n");
return;
if(!Is_usbworking(Index))
{
printf("Do not find SFxx programmer!!\n");
return ;
}
CNTRPIPE_RQ rq ;
unsigned char vBuffer[16];
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_IN ;
rq.Request = PROGINFO_REQUEST ;
rq.Value = RFU ;
rq.Index = 0x00 ;
rq.Length = (unsigned long)16;
memset(vBuffer, '\0', 16);
if(InCtrlRequest(&rq, vBuffer,16,Index) == SerialFlash_FALSE)
{
printf("send fail\r\n");
return ;
}
memcpy(g_board_type,&vBuffer[0],8);
// memcpy(g_firmversion,&vBuffer[10],8);
// printf("g_board_type=%s\r\n",g_board_type);
// if(strstr(g_board_type,"SF600") != NULL)
// g_bIsSF600=true;
// else
// g_bIsSF600=false;
#if 0
m_info.fpga_version=GetFPGAVersion(Index);
vector<unsigned char> vBuffer1(3) ;
rq.Function = URB_FUNCTION_VENDOR_OTHER ;
rq.Direction = VENDOR_DIRECTION_IN ;
rq.Request = 0x7;
rq.Value = 0 ;
rq.Index = 0xEF00 ;
rq.Length = 3;
if(! m_usb.InCtrlRequest(rq, vBuffer1,Index))
{
return ;
}
if(m_info.board_type==L"SF600")
{
ReadOnBoardFlash(false,Index);
m_info.dwUID=(DWORD)m_vUID[0]<<16 | (DWORD)m_vUID[1]<<8 | m_vUID[2];
}
else
m_info.dwUID=(DWORD)vBuffer1[0]<<16 | (DWORD)vBuffer1[1]<<8 | vBuffer1[2];
#endif
}
unsigned char GetFPGAVersion(int Index)
{
if(strstr(g_board_type,"SF600")==NULL) return -1;
CNTRPIPE_RQ rq ;
unsigned char vDataPack ;
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_IN;
rq.Request = 0x1C ;
rq.Value = RFU ;
rq.Index = 0 ;
rq.Length = 1 ;
if(InCtrlRequest(&rq,&vDataPack,1,Index)==SerialFlash_FALSE){
return -1 ;
}
return vDataPack;
}
bool SetIO(unsigned char ioState, int Index)
{
CNTRPIPE_RQ rq ;
unsigned char vDataPack ; // 1 bytes, in fact no needs
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_OUT ;
rq.Request = SET_IO ;
if(Is_NewUSBCommand(Index))
{
rq.Value = (ioState&0x0F)|0x70 ;
rq.Index = 0;
}
else
{
rq.Value = ioState ;
rq.Index = 0x07 ;
}
rq.Length = 0 ;
if(OutCtrlRequest(&rq,&vDataPack,0,Index)==SerialFlash_FALSE){
return false ;
}
return true ;
}
bool SetTargetFlash(unsigned char StartupMode,int Index)
{
CNTRPIPE_RQ rq ;
unsigned char vInstruction ;
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_OUT ;
rq.Request = SET_TARGET_FLASH;
rq.Value = StartupMode;
rq.Index = 0 ;
rq.Length = 0 ;
if(OutCtrlRequest(&rq,&vInstruction,0,Index)==SerialFlash_FALSE )
{
return false ;
}
return true;
}
bool SetLEDProgBoard(size_t Color,int Index)
{
if(! Is_usbworking(Index))
{
return false;
}
CNTRPIPE_RQ rq ;
unsigned char vBuffer[16];
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_OUT ;
rq.Request = SET_IO ;
if(Is_NewUSBCommand(Index))
{
rq.Value = (Color&0xFFF7) | (g_IO1Select<<1)|(g_IO4Select<<3);
rq.Index=0;
}
else
{
rq.Value = 0x09 ;
rq.Index = Color>>8 ;//LED 0:ON 1:OFF Bit0:Green Bit1:Orange Bit2:Red
}
rq.Length = 0 ;
if(OutCtrlRequest(&rq,vBuffer,0,Index)==SerialFlash_FALSE)
{
return false;
}
return true;
}
bool SetGreenLEDOn(bool boOn,int Index)
{
return SetLEDProgBoard(boOn?0x0609:0x0709,Index);
}
bool SetOrangeLEDOn(bool boOn, int Index)
{
return SetLEDProgBoard(boOn?0x0509:0x0709,Index);
}
bool SetRedLEDOn(bool boOn, int Index)
{
return SetLEDProgBoard(boOn?0x0309:0x0709,Index);
}
bool SetLEDOnOff(size_t Color,int Index)
{
bool result=true;
switch(Color)
{
case SITE_ERROR:
result&=SetRedLEDOn(true,Index);
break;
case SITE_BUSY:
result&=SetOrangeLEDOn(true,Index);
break;
case SITE_OK:
result&=SetGreenLEDOn(true,Index);
break;
case SITE_NORMAL:
result&=SetLEDProgBoard(0x0709,Index);// Turn off LED
break;
}
return result;
}
bool SetCS(size_t value,int Index)
{
if(! Is_usbworking(Index))
{
return false;
}
CNTRPIPE_RQ rq ;
unsigned char vBuffer ;
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_OUT ;
rq.Request = SET_CS;
rq.Value = value; // 12MHz
rq.Index = RFU ;
rq.Length = 0 ;
if(OutCtrlRequest(&rq, &vBuffer,0,Index)==SerialFlash_FALSE)
{
return false;
}
return true;
}
bool SetIOModeToSF600(size_t value,int Index)
{
if(! Is_usbworking(Index))
{
return false;
}
CNTRPIPE_RQ rq ;
unsigned char vBuffer ;
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_OUT ;
rq.Request = SET_IOMODE;
rq.Value = value;
rq.Index = RFU ;
rq.Length = 0 ;
if(OutCtrlRequest(&rq, &vBuffer,0,Index)==SerialFlash_FALSE)
{
return false;
}
return true;
}
bool BlinkProgBoard(bool boIsV5,int Index)
{
if(! Is_usbworking(Index) )
{
return false;
}
SetGreenLEDOn(true,Index);
Sleep(500);
SetGreenLEDOn(false,Index);
return true;
}
bool ReadOnBoardFlash(unsigned char* Data,bool ReadUID,int Index)
{
CNTRPIPE_RQ rq ;
unsigned char vBuffer[16] ;
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_IN ;
rq.Request = 0x05 ;
if(Is_NewUSBCommand(Index))
{
rq.Value = ReadUID;
rq.Index = 0;
}
else
{
rq.Value = 0x00 ;
rq.Index = ReadUID;
}
rq.Length = 16 ;
if(InCtrlRequest(&rq, vBuffer,16,Index)==SerialFlash_FALSE)
{
return false;
}
memcpy(Data,vBuffer,16);
}
bool LeaveSF600Standalone(bool Enable,int Index)
{
if(! Is_usbworking(Index))
{
return false;
}
CNTRPIPE_RQ rq ;
unsigned char vBuffer ;
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_OUT ;
rq.Request = SET_SA;
rq.Value = Enable;
rq.Index = RFU ;
rq.Length = 0 ;
if(OutCtrlRequest(&rq,&vBuffer,0,Index)==SerialFlash_FALSE)
{
return false;
}
return true;
}
bool SetSPIClockValue(unsigned short v,int Index)
{
if(!Is_usbworking(Index) )
return false;
// send request
CNTRPIPE_RQ rq ;
unsigned char vBuffer;
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_OUT ;
rq.Request = SET_SPICLK;
rq.Value = v ;
rq.Index = RFU ;
rq.Length = 0 ;
if(OutCtrlRequest(&rq, &vBuffer,0,Index)==SerialFlash_FALSE)
return false;
return true;
}
unsigned int ReadUID(int Index)
{
if(! Is_usbworking(Index) )
{
return false;
}
unsigned int dwUID=0;
if(g_bIsSF600==true)
{
unsigned char vUID[16];
ReadOnBoardFlash(vUID,false,Index);
dwUID=(unsigned int)vUID[0]<<16 | (unsigned int)vUID[1]<<8 | vUID[2];
return dwUID;
}
CNTRPIPE_RQ rq ;
unsigned char vBuffer[3] ;
// read
rq.Function = URB_FUNCTION_VENDOR_OTHER ;
rq.Direction = VENDOR_DIRECTION_IN ;
rq.Request = 0x7;
rq.Value = 0 ;
rq.Index = 0xEF00 ;
rq.Length = 3;
if( InCtrlRequest(&rq, vBuffer,3,Index)==SerialFlash_FALSE)
{
return false;
}
dwUID=(unsigned int)vBuffer[0]<<16 | (unsigned int)vBuffer[1]<<8 | vBuffer[2];
return dwUID;
}
bool SetSPIClockDefault(int Index)
{
if(!Is_usbworking(Index) )
return false;
// send request
CNTRPIPE_RQ rq ;
unsigned char vBuffer ;
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_OUT ;
rq.Request = SET_SPICLK;
rq.Value = 0x02; // 12MHz
rq.Index = 0 ;
rq.Length = 0 ;
if(OutCtrlRequest(&rq, &vBuffer,0,Index)==SerialFlash_FALSE)
{
printf("Set SPI clock error\r\n");
return false;
}
return true;
}
bool SetVpp4IAP(bool bOn,int Index)
{
if(!Is_usbworking(Index) )
return false;
// send request
CNTRPIPE_RQ rq ;
unsigned char vBuffer ;
rq.Function = URB_FUNCTION_VENDOR_OTHER ;
rq.Direction = VENDOR_DIRECTION_IN ;
rq.Request = bOn ? 0x0 : 0x01 ;
rq.Value = 0 ;
rq.Index = 0 ;
rq.Length = 0x01 ;
if(OutCtrlRequest(&rq, &vBuffer,1,Index)==SerialFlash_FALSE)
{
return false;
}
Sleep(200);
return true;
}
bool UnlockRASS(int Index)
{
if(!Is_usbworking(Index) )
return false;
// send request
CNTRPIPE_RQ rq ;
unsigned char vBuffer ;
rq.Function = URB_FUNCTION_VENDOR_OTHER ;
rq.Direction = VENDOR_DIRECTION_IN ;
rq.Request = 0x03 ;
rq.Value = 0 ;
rq.Index = 0 ;
rq.Length = 0x01 ;
if(OutCtrlRequest(&rq, &vBuffer,1,Index)==SerialFlash_FALSE)
{
return false;
}
return true;
}
unsigned char ReadManufacturerID(int Index)
{
if(!Is_usbworking(Index) )
return false;
if(g_bIsSF600==true)
{
unsigned char vUID[16];
ReadOnBoardFlash(vUID,false,Index);
return vUID[3];
}
CNTRPIPE_RQ rq ;
unsigned char vBuffer;
// read
rq.Function = URB_FUNCTION_VENDOR_OTHER ;
rq.Direction = VENDOR_DIRECTION_IN ;
rq.Request = 0x7;
rq.Value = 0 ;
rq.Index = 0xEF03;
rq.Length = 1;
if(InCtrlRequest(&rq, &vBuffer,1,Index)==SerialFlash_FALSE)
{
return false;
}
return vBuffer;
}
bool EraseST7Sectors(bool bSect1,int Index)
{
if(!Is_usbworking(Index) )
return false;
// send request
CNTRPIPE_RQ rq ;
unsigned char vBuffer ;
rq.Function = URB_FUNCTION_VENDOR_OTHER ;
rq.Direction = VENDOR_DIRECTION_IN ;
rq.Request = bSect1 ? 0x04 : 0x05 ;
rq.Value = 0 ;
rq.Index = 0 ;
rq.Length = 0x01 ;
if(OutCtrlRequest(&rq, &vBuffer,1,Index)==SerialFlash_FALSE)
{
return false;
}
return true;
}
bool ProgramSectors(const char* sFilePath, bool bSect1,int Index)
{
const unsigned int iSect1StartAddr = 0xE000;
const unsigned int iSect2StartAddr = 0x8000;
const unsigned int iSect1Size = 0x1000;
const unsigned int iSect2Size = 0x6000;
unsigned char* pBuffer=NULL;
unsigned char* pTmp=NULL;
FW_INFO fw_info;
FILE * pFile;
size_t lSize;
unsigned char Data;
unsigned int iStartAddr = bSect1 ? iSect1StartAddr : iSect2StartAddr;
unsigned int Size,tmp;
unsigned int uiIndex=0;
// prog sectors
CNTRPIPE_RQ rq ;
pFile = fopen( sFilePath , "rb" );
if (pFile==NULL)
{
printf("Can not open %s \n",sFilePath);
return false;
}
fseek (pFile , 0 , SEEK_SET);
lSize=fread ((unsigned char*)&fw_info,1,sizeof(FW_INFO),pFile);
if(lSize != sizeof(FW_INFO))
printf("Possible read length error %s\n", sFilePath);
if(bSect1==true)
{
Size=iSect1Size;
uiIndex=fw_info.FirstIndex;
}
else
{
Size=iSect2Size;
uiIndex=fw_info.SecondIndex;
}
pBuffer = (unsigned char*)malloc(Size);
memset(pBuffer,0xFF,Size);
fseek (pFile , uiIndex, SEEK_SET);
lSize = fread(pBuffer,1,Size,pFile);
if(lSize != Size)
printf("Possible read length error %s\n", sFilePath);
fclose (pFile);
pTmp=pBuffer;
while(Size)
{
tmp=min(Size,0x100);
/// receive page
rq.Function = URB_FUNCTION_VENDOR_OTHER;
rq.Direction = VENDOR_DIRECTION_OUT;
rq.Request = 0x1;
rq.Value = 0;
rq.Index = 0;
rq.Length = 0x100; /// plage size for ST7 Iap prog
if(OutCtrlRequest(&rq,pTmp,tmp,Index)==SerialFlash_FALSE)
{
free(pBuffer);
return false;
}
/// program page
rq.Function = URB_FUNCTION_VENDOR_OTHER;
rq.Direction = VENDOR_DIRECTION_IN;
rq.Request = 0x8;
rq.Value = 0;
rq.Index = iStartAddr & 0xFFFF; ///< ConvLongToInt(lngST7address Mod &H10000)
rq.Length = 0x1;
iStartAddr += 0x100;
if(OutCtrlRequest(&rq,&Data,1,Index )==SerialFlash_FALSE)
{
free(pBuffer);
return false;
}
Size-=tmp;
pTmp+=tmp;
}
free(pBuffer);
return true;
}
bool UpdateChkSum(int Index)
{
if(!Is_usbworking(Index) )
return false;
CNTRPIPE_RQ rq ;
unsigned char vBuffer[2] ;
// send to calculate checksum
rq.Function = URB_FUNCTION_VENDOR_OTHER ;
rq.Direction = VENDOR_DIRECTION_IN ;
rq.Request = 0x9;
rq.Value = 0 ;
rq.Index = 0 ;
rq.Length = 2 ;
if(OutCtrlRequest(&rq, vBuffer,2,Index)==SerialFlash_FALSE)
{
return false;
}
return true;
}
bool WriteUID(unsigned int dwUID,int Index)
{
if(!Is_usbworking(Index) )
return false;
if(g_bIsSF600)
return true;
CNTRPIPE_RQ rq;
unsigned char Data;
// read
rq.Function = URB_FUNCTION_VENDOR_OTHER ;
rq.Direction = VENDOR_DIRECTION_IN ;
rq.Request = 0x6;
rq.Value = (unsigned char)(dwUID>>16) ;
rq.Index = 0xEF00 ;
rq.Length = 1;
if(InCtrlRequest(&rq, &Data,1,Index)==SerialFlash_FALSE)
{
return false;
}
rq.Index = 0xEF01 ;
rq.Value = (unsigned char)(dwUID>>8) ;
if(InCtrlRequest(&rq, &Data,1,Index)==SerialFlash_FALSE)
{
return false;
}
rq.Index = 0xEF02 ;
rq.Value = (unsigned char)(dwUID) ;
if(!InCtrlRequest(&rq, &Data,1,Index))
{
return false;
}
return true;
}
bool WriteManufacturerID(unsigned char ManuID,int Index)
{
if(!Is_usbworking(Index) )
return false;
if(g_bIsSF600)
return true;
CNTRPIPE_RQ rq ;
unsigned char Data;
// read
rq.Function = URB_FUNCTION_VENDOR_OTHER ;
rq.Direction = VENDOR_DIRECTION_IN ;
rq.Request = 0x6;
rq.Value = ManuID ;
rq.Index = 0xEF03 ;
rq.Length = 1;
if(InCtrlRequest(&rq, &Data,1,Index)==SerialFlash_FALSE)
{
return false;
}
return true;
}
bool ReadMemOnST7(unsigned int iAddr,int Index)
{
if(!Is_usbworking(Index) )
return false;
CNTRPIPE_RQ rq ;
unsigned char vBuffer[2] ;
// read
rq.Function = URB_FUNCTION_VENDOR_OTHER ;
rq.Direction = VENDOR_DIRECTION_IN ;
rq.Request = 0x7;
rq.Value = 0 ;
rq.Index = iAddr & 0xFFFF ;
rq.Length = 2;
if(OutCtrlRequest(&rq, vBuffer,2,Index)==SerialFlash_FALSE)
{
return false;
}
return true;
}
bool UpdateSF600Firmware(const char* sFolder,int Index)
{
bool boResult=true;
unsigned char vUID[16];
unsigned int dwUID;
ReadOnBoardFlash(vUID,false,Index);
dwUID=(unsigned int)vUID[0]<<16 | (unsigned int)vUID[1]<<8 | vUID[2];
boResult &= UpdateSF600Flash(sFolder,Index);
Sleep(200);
boResult &= UpdateSF600Flash_FPGA(sFolder,Index);
Sleep(1000);
WriteSF600UID(dwUID,vUID[3],Index);
return boResult;
}
bool WriteSF600UID(unsigned int dwUID, unsigned char ManuID,int Index)
{
CNTRPIPE_RQ rq ;
unsigned char vBuffer[16] ;
// first control packet
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_OUT ;
rq.Request = WRITE_EEPROM ;
rq.Value = 0;
rq.Index = 0 ;
rq.Length = 16;
vBuffer[0]=(unsigned char)(dwUID>>16) ;
vBuffer[1]=(unsigned char)(dwUID>>8) ;
vBuffer[2]=(unsigned char)(dwUID) ;
vBuffer[3]=ManuID ;
if(!OutCtrlRequest(&rq, vBuffer,16,Index))
return false ;
return true ;
}
void EncrypFirmware(unsigned char* vBuffer,unsigned int Size,int Index)
{
unsigned char vUID[16];
unsigned int i=0;
ReadOnBoardFlash(vUID,true,Index);
for(i=0; i<16; i++)
vBuffer[i]=vBuffer[i]^vUID[i];
for(; i<Size; i++)
vBuffer[i]=vBuffer[i]^vBuffer[i-16];
}
bool UpdateSF600Flash(const char* sFilePath,int Index)
{
CNTRPIPE_RQ rq ;
unsigned char* pBuffer;
int pagenum=0;
unsigned int dwsize=0;
FW_INFO fw_info;
FILE * pFile;
size_t lSize;
int i=0;
pFile = fopen( sFilePath , "rb" );
if (pFile==NULL)
{
printf("Can not open %s \n",sFilePath);
return false;
}
fseek (pFile , 0 , SEEK_SET);
lSize = fread ((unsigned char*)&fw_info,1,sizeof(FW_INFO),pFile);
if(lSize != sizeof(FW_INFO))
printf("Possible read length error %s\n", sFilePath);
pBuffer = (unsigned char*) malloc(fw_info.FirstSize);
fseek (pFile , fw_info.FirstIndex, SEEK_SET);
lSize = fread (pBuffer,1,fw_info.FirstSize,pFile);
if(lSize != fw_info.FirstSize)
printf("Possible read length error %s\n", sFilePath);
fclose(pFile);
EncrypFirmware(pBuffer,fw_info.FirstSize,Index);
if(Is_NewUSBCommand(Index))
{//for win8.1
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_OUT ;
rq.Request = 0x1A;
rq.Value = 0;
rq.Index = 0;
rq.Length = 6 ;
//evy 6.0.4.25 for win 8.1 with new firmware(7.0.2 up)
unsigned char Package[6];
Package[0]=pBuffer[0];
Package[1]=pBuffer[1];
Package[2]=(fw_info.FirstSize & 0xff);
Package[3]=((fw_info.FirstSize >>8) & 0xff);
Package[4]=((fw_info.FirstSize >> 16) & 0xff);
Package[5]=((fw_info.FirstSize >> 24) & 0xff);
if(!OutCtrlRequest(&rq, Package,6,Index))
{
free(pBuffer);
return false ;
}
}
else
{
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_OUT ;
rq.Request = 0x1A ;
rq.Value = (unsigned short)(fw_info.FirstSize & 0xffff) ;
rq.Index = (unsigned short)((fw_info.FirstSize >> 16) & 0xffff) ;
rq.Length = 0 ;
if(!OutCtrlRequest(&rq, pBuffer,0,Index) )
{
free(pBuffer);
return false ;
}
}
pagenum=(fw_info.FirstSize>>9)+((fw_info.FirstSize%(1<<9))? 1:0);
dwsize=fw_info.FirstSize;
for( i=0; i<pagenum ; i++)
{
BulkPipeWrite((pBuffer+(i<<9)),min(512,dwsize),USB_TIMEOUT,Index);
dwsize-=512;
}
free(pBuffer);
return true;
}
bool UpdateSF600Flash_FPGA(const char* sFilePath,int Index)
{
CNTRPIPE_RQ rq ;
unsigned char* pBuffer;
int pagenum=0;
unsigned int dwsize=0;
FW_INFO fw_info;
FILE * pFile;
size_t lSize;
int i=0;
pFile = fopen( sFilePath , "rb" );
if (pFile==NULL)
{
printf("Can not open %s \n",sFilePath);
return false;
}
fseek (pFile , 0 , SEEK_SET);
lSize = fread ((unsigned char*)&fw_info,1,sizeof(FW_INFO),pFile);
if(lSize != sizeof(FW_INFO))
printf("Possible read length error %s\n", sFilePath);
pBuffer = (unsigned char*) malloc(fw_info.SecondSize);
fseek (pFile , fw_info.SecondIndex, SEEK_SET);
lSize = fread (pBuffer,1,fw_info.SecondSize,pFile);
if(lSize != fw_info.SecondSize)
printf("Possible read length error %s\n", sFilePath);
fclose(pFile);
EncrypFirmware(pBuffer,fw_info.SecondSize,Index);
if(Is_NewUSBCommand(Index))
{//for win8.1
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_OUT ;
rq.Request = 0x1B;
rq.Value = 0;//static_cast<unsigned short>(vBuffer.size() & 0xffff) ;
rq.Index = 0;//static_cast<unsigned short>((vBuffer.size() >> 16) & 0xffff) ;
rq.Length = 4 ;
//evy 6.0.4.25 for win 8.1 with new firmware(7.0.2 up)
unsigned char Package[4];
Package[0]=(fw_info.SecondSize & 0xff);
Package[1]=((fw_info.SecondSize>>8) & 0xff);
Package[2]=((fw_info.SecondSize >> 16) & 0xff);
Package[3]=((fw_info.SecondSize >> 24) & 0xff);
if(!OutCtrlRequest(&rq, Package,4,Index) )
{
printf("Error: 1\r\n");
free(pBuffer);
return false ;
}
}
else
{
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_OUT ;
rq.Request = 0x1B;
rq.Value = (unsigned short)(fw_info.SecondSize & 0xffff) ;
rq.Index = (unsigned short)((fw_info.SecondSize >> 16) & 0xffff) ;
rq.Length = 0 ;
if(!OutCtrlRequest(&rq, pBuffer,0,Index) )
{
printf("Error: 2\r\n");
free(pBuffer);
return false ;
}
}
pagenum=(fw_info.SecondSize>>9)+((fw_info.SecondSize%(1<<9))? 1:0);
dwsize=fw_info.SecondSize;
for( i=0; i<pagenum ; i++)
{
BulkPipeWrite(pBuffer+(i<<9),min(512,dwsize),USB_TIMEOUT,Index);
dwsize-=512;
}
return true;
}
bool UpdateFirmware(const char* sFolder,int Index)
{
bool bResult = true;
unsigned int UID=0;
unsigned char ManID=0;
// read status
if(g_bIsSF600==true)
return UpdateSF600Firmware(sFolder,Index);
dediprog_set_spi_voltage(g_Vcc,Index);
if(g_firmversion > 0x040107) // 4.1.7
bResult &= SetSPIClockDefault(Index);
bResult &= SetVpp4IAP(true,Index);
bResult &= UnlockRASS(Index);
UID = ReadUID(Index);
ManID = ReadManufacturerID(Index);
// erase & program sector 1
bResult &= EraseST7Sectors(true,Index);
bResult &= ProgramSectors(sFolder, true,Index);
// erase & program sector 2
bResult &= EraseST7Sectors(false,Index);
bResult &= ProgramSectors(sFolder, false,Index);
// calculate and read back checksum
bResult &= UpdateChkSum(Index);
bResult &= WriteUID(UID,Index);
bResult &= WriteManufacturerID(ManID,Index);
bResult &= SetVpp4IAP(false,Index);
// read back checksum
bResult &= ReadMemOnST7(0xEFFE,Index);
return bResult;
}
void SendFFSequence(int Index)
{
unsigned char v[4]={0xff,0xff,0xff,0xff};
FlashCommand_SendCommand_OutOnlyInstruction(v,4,Index);
}