-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest_Utility.c
849 lines (763 loc) · 31.2 KB
/
Test_Utility.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
/*
* Test utility
* Copyright (C) 2013 Cypress Semiconductor
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <signal.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
#include <ctype.h>
#include "../../common/header/CyUSBSerial.h"
#define CY_MAX_DEVICES 30
#define CY_MAX_INTERFACES 4
typedef struct _CY_DEVICE_STRUCT {
int deviceNumber;
int interfaceFunctionality[CY_MAX_INTERFACES];
bool isI2c;
bool isSpi;
bool isUart;
int numInterface;
}CY_DEVICE_STRUCT;
CY_DEVICE_STRUCT *glDevice;
int i2cDeviceIndex[CY_MAX_DEVICES][CY_MAX_INTERFACES];
unsigned char *deviceNumber = NULL;
int cyDevices, i2cDevices = 0, numDevices = 0;
int selectedDeviceNum = -1, selectedInterfaceNum = -1;
bool exitApp = false;
unsigned short pageAddress = -1;
short readWriteLength = -1;
bool deviceAddedRemoved = false;
unsigned char read_buffer[512];
int getUserInput()
{
char userInput[6], x;
int output,i = 0;
bool isDigit = true;
x = getchar();
while (x != '\n'){
if (i < 5){
userInput[i] = x;
i++;
}
if (!isdigit(x))
isDigit = false;
x = getchar();
}
userInput[i] = '\0';
if (isDigit == false)
return -1;
output = atoi(userInput);
return output;
}
unsigned int GetCheckSum(unsigned int *buff, unsigned int length)
{
unsigned int i,val;
unsigned int checkSum =0;
for (i = 0; i < length; i++) // start at 12th byte
{
checkSum += buff[i];
}
return checkSum;
}
void deviceHotPlug () {
CY_RETURN_STATUS rStatus;
deviceAddedRemoved = true;
selectedDeviceNum = -1;
selectedInterfaceNum = -1;
printf ("Device of interest Removed/Added \n");
rStatus = CyGetListofDevices (&numDevices);
if (rStatus != CY_SUCCESS) {
printf ("CY:Error in Getting List of Devices: Error NO:<%d> \n", rStatus);
return rStatus;
}
printListOfDevices (false);
}
int main (int argc, char **argv)
{
int index = 0, i, j, userInput;
int tempSelectedDeviceNum, tempSelectedInterfaceNum, tempPageAddress, tempLength;
CY_RETURN_STATUS rStatus;
signal (SIGUSR1, deviceHotPlug);
glDevice = (CY_DEVICE_STRUCT *)malloc (CY_MAX_DEVICES *sizeof (CY_DEVICE_STRUCT));
if (glDevice == NULL){
printf ("Memory allocation failed ...!! \n");
return -1;
}
rStatus = CyLibraryInit ();
if (rStatus != CY_SUCCESS) {
printf ("CY:Error in Doing library init Error NO:<%d> \n", rStatus);
return rStatus;
}
rStatus = CyGetListofDevices (&numDevices);
if (rStatus != CY_SUCCESS) {
printf ("CY:Error in Getting List of Devices: Error NO:<%d> \n", rStatus);
return rStatus;
}
printListOfDevices(true);
do {
printf ("-------------------------------------------------------------------\n");
printf ("1: Print list of devices \n");
if (selectedDeviceNum != -1 && selectedInterfaceNum != -1){
printf ("2: Change device selection--selected device: [Device number %d] : [Interface No %d]",\
selectedDeviceNum, selectedInterfaceNum);
if (glDevice[selectedDeviceNum].interfaceFunctionality[selectedInterfaceNum] == CY_TYPE_I2C)
printf (" : I2C\n");
else if (glDevice[selectedDeviceNum].interfaceFunctionality[selectedInterfaceNum] == CY_TYPE_SPI)
printf (" : SPI\n");
else if (glDevice[selectedDeviceNum].interfaceFunctionality[selectedInterfaceNum] == CY_TYPE_UART)
printf (" : UART\n");
else
printf (" : NA\n");
}
else
printf ("2: Select device...No device selected !!\n");
printf ("3: Change CYBUSBS236 configuration\n");
if (readWriteLength != -1 && pageAddress != -1){
printf ("4: Change Flash page address and length ...Entered is page address %d and length %d\n", pageAddress, readWriteLength);
printf ("5: Verify data\n6: Exit\n");
}
else
printf ("4: Enter I2C/SPI Flash page address and length to write/read.\n5: Verify data\n6: Exit\n");
printf ("-------------------------------------------------------------------\n");
userInput = getUserInput();
if (userInput < 1 || userInput > 6){
printf ("Wrong selection code ... Enter again \n");
continue;
}
switch (userInput){
case 1:
printListOfDevices(true);
break;
case 2:
if (cyDevices == 0) {
printf ("No device of interest connected ...!!\n");
continue;
}
printf ("Enter Device number to be selected.. \n");
tempSelectedDeviceNum = getUserInput();
//printf ("Selected device number is %d \n",tempSelectedDeviceNum);
if (tempSelectedDeviceNum >= cyDevices || tempSelectedDeviceNum == -1){
printf ("Wrong device selection \n");
continue;
}
printf ("Enter interface number..\n");
tempSelectedInterfaceNum = getUserInput();
//printf ("Selected device number is %d %d\n",tempSelectedInterfaceNum, glDevice[tempSelectedDeviceNum].numInterface);
if (tempSelectedInterfaceNum >= glDevice[tempSelectedDeviceNum].numInterface ||
tempSelectedInterfaceNum == -1) {
printf ("Wrong interface Selection selection \n");
continue;
}
if ((glDevice[tempSelectedDeviceNum].interfaceFunctionality[tempSelectedInterfaceNum] == -1)){
printf ("Selected device does not have I2C or SPI or UART !!! \n");
continue;
}
if (deviceAddedRemoved == true) {
printf ("Device of interest was added/removed .... Print and select from new list\n");
continue;
}
selectedDeviceNum = tempSelectedDeviceNum;
selectedInterfaceNum = tempSelectedInterfaceNum;
//pageAddress = -1;
//readWriteLength = -1;
break;
case 3:
{
int output;
int count=0,h=0,cnt;
int size_buffer,size_checksum;
FILE *fp=NULL;
CY_HANDLE handle;
unsigned char buff[516];
int silicon_id;
char src_file[100];
char id[4];
memset(buff,0,sizeof(buff));
if (cyDevices == 0) {
printf ("No device of interest connected ...!!\n");
continue;
}
printf ("Enter Device number to be selected.. \n");
tempSelectedDeviceNum = getUserInput();
//printf ("Selected device number is %d \n",tempSelectedDeviceNum);
if (tempSelectedDeviceNum >= cyDevices || tempSelectedDeviceNum == -1){
printf ("Wrong device selection \n");
continue;
}
printf ("Enter interface number..\n");
tempSelectedInterfaceNum = getUserInput();
//printf ("Selected device number is %d %d\n",tempSelectedInterfaceNum, glDevice[tempSelectedDeviceNum].numInterface);
if (tempSelectedInterfaceNum >= glDevice[tempSelectedDeviceNum].numInterface ||
tempSelectedInterfaceNum == -1) {
printf ("Wrong interface Selection selection \n");
continue;
}
if ((glDevice[tempSelectedDeviceNum].interfaceFunctionality[tempSelectedInterfaceNum] == -1)){
printf ("Illegal device selected !!! \n");
continue;
}
if (deviceAddedRemoved == true) {
printf ("Device of interest was added/removed .... Print and select from new list\n");
continue;
}
selectedDeviceNum = tempSelectedDeviceNum;
selectedInterfaceNum = tempSelectedInterfaceNum;
rStatus = CyOpen (glDevice[selectedDeviceNum].deviceNumber,selectedInterfaceNum , &handle);
//printf("return status = %d", rStatus);
if (rStatus == CY_SUCCESS){
//printf("device opened \n");
}
else
{
printf("device fail to open \n");
}
// printf("Please enter file to be opened");
fp = fopen(argv[1],"rb+");
// printf("%s", src_file);
rStatus = CyFlashConfigEnable(handle,1);
if (rStatus == CY_SUCCESS){
// printf("Flash is configured");
}
else
{
printf("Manufacturing mode of FLASH is not configured");
}
// printf("status %d",rStatus);
if(fp == NULL)
{
printf("\n fopen() Error!!!\n");
return 1;
}
// printf("\n File opened successfully\n");
if(sizeof(buff) != fread(buff,1,516,fp))
{
printf("\n fread() failed\n");
return 1;
}
// printf("\n Bytes successfully read \n");
// printf("reached here");
// silicon_id = *(unsigned int *)buff;
rStatus=CyGetSiliconID(handle,&silicon_id);
if (rStatus == CY_SUCCESS){
// printf(" Correct silicon id");
}
else
{
printf("Not correct ID");
}
// printf("silicon id %04x,%d",silicon_id,sizeof(silicon_id));
id[0]= (silicon_id);
id[1]= ((silicon_id >> 8) & 0xFF);
id[2]= ((silicon_id >> 16) & 0xFF);
id[3]= ((silicon_id >> 24) & 0xFF);
rStatus=CyReadDeviceConfig(handle,&read_buffer);
if (rStatus == CY_SUCCESS){
// printf(" Got the data");
}
else
{
printf("Not done");
}
size_buffer = sizeof(read_buffer);
//printf("The size is %d, buff %d", size_buffer,sizeof(buff));
memcpy (&buff[4], read_buffer, 28);
size_checksum= GetCheckSum((unsigned int *)(&buff[16]), 125);
// printf("The checksum size is %d",size_checksum);
buff[12]= (size_checksum & 0xFF);
buff[13]= ((size_checksum >> 8) & 0xFF);
buff[14]= ((size_checksum >> 16) & 0xFF);
buff[15]= ((size_checksum >> 24) & 0xFF);
// printf("checksum 0x%08x\n", size_checksum);
// if(((id[0] == ffffffA1) || (id[0] == ffffffA2) || (id[0] == ffffffA3)) && (id[1] == 08))
if((silicon_id == 0x08A1) || (silicon_id == 0x08A2) || (silicon_id == 0x08A3))
{
rStatus= CyWriteDeviceConfig(handle,&buff[4]);
if (rStatus == CY_SUCCESS){
printf("\n\n Programming Flash is done \n\n");
printf(" Press reset switch SW3 to configure \n\n");
printf(" Remember to set configuration jumpers for SCB0 & SCB1 \n\n");
}
else
{
printf("Not done");
}
}
else
{
printf("wrong silicon id");
}
fclose(fp);
// printf("File stream closed \n");
rStatus= CyClose(handle);
if (rStatus == CY_SUCCESS){
//printf("Device closed");
}
else
{
printf("Device not closed");
}
exitApp = true;
CyLibraryExit ();
break;
}
case 4:
if (selectedDeviceNum == -1) {
printf ("Select proper device!!! \n");
continue;
}
if (selectedDeviceNum >= cyDevices){
printf ("Select proper device!!! \n");
continue;
}
if (glDevice[selectedDeviceNum].interfaceFunctionality[selectedInterfaceNum] == CY_TYPE_I2C){
printf ("Enter Page address... (less than 256)\n");
tempPageAddress = getUserInput();
printf ("Enter length to read/write...(less than 64)\n");
tempLength = getUserInput();
if (deviceAddedRemoved == true) {
printf ("Device of interest was added/removed .... Print and select from new list\n");
continue;
}
if (tempPageAddress > 256){
printf ("Invalid page address ..Enter page address less than 256 (1 bytes len)\n");
continue;
}
if (tempLength < 0 || tempLength > 64){
printf ("Invalid Length ..Enter length less than 64 bytes\n");
continue;
}
pageAddress = tempPageAddress;
readWriteLength = tempLength;
break;
}
if (glDevice[selectedDeviceNum].interfaceFunctionality[selectedInterfaceNum] == CY_TYPE_SPI){
printf ("Enter Page address... (less than 65536)\n");
tempPageAddress = getUserInput();
printf ("Enter length to read/write...(less than 256)\n");
tempLength = getUserInput();
if (deviceAddedRemoved == true) {
printf ("Device of interest was added/removed .... Print and select from new list\n");
continue;
}
if (tempPageAddress > 65536){
printf ("Invalid page address ..Enter page address less than 65536 (1 bytes len)\n");
continue;
}
if (tempLength < 0 || tempLength > 256){
printf ("Invalid Length ..Enter length less than 256 bytes\n");
continue;
}
pageAddress = tempPageAddress;
readWriteLength = tempLength;
break;
}
case 5:
if (selectedDeviceNum == -1) {
printf ("Select proper device!!! \n");
continue;
}
if (glDevice[selectedDeviceNum].interfaceFunctionality[selectedInterfaceNum] == -1){
printf ("Selected device does not have I2C or SPI or UART !!! \n");
continue;
}
if (glDevice[selectedDeviceNum].interfaceFunctionality[selectedInterfaceNum] == CY_TYPE_UART){
printf ("Calling UART \n");
uartVerifyData(glDevice[selectedDeviceNum].deviceNumber, selectedInterfaceNum);
continue;
}
if ((pageAddress == -1) || (readWriteLength == -1)){
printf ("Select proper page address and length !!! \n");
continue;
}
if (glDevice[selectedDeviceNum].interfaceFunctionality[selectedInterfaceNum] == CY_TYPE_I2C){
if (pageAddress > 256){
printf ("Invalid Page address for I2C .. need to be less than 256\n");
continue;
}
if (readWriteLength > 64){
printf ("Invalid read write length for I2C .. need to be less than 64\n");
continue;
}
printf ("Calling I2C \n");
i2cVerifyData(glDevice[selectedDeviceNum].deviceNumber, selectedInterfaceNum);
}
if (glDevice[selectedDeviceNum].interfaceFunctionality[selectedInterfaceNum] == CY_TYPE_SPI){
if (pageAddress > 65536){
printf ("Invalid Page address for SPI .. need to be less than 65536\n");
continue;
}
if (readWriteLength > 256){
printf ("Invalid read write length for SPI .. need to be less than 256\n");
continue;
}
printf ("Calling SPI \n");
spiVerifyData(glDevice[selectedDeviceNum].deviceNumber, selectedInterfaceNum);
}
break;
case 6:
exitApp = true;
CyLibraryExit ();
break;
}
}while (exitApp == false);
free (glDevice);
}
int spiWriteEnable (CY_HANDLE handle)
{
unsigned char wr_data,rd_data;
CY_RETURN_STATUS status = CY_SUCCESS;
CY_DATA_BUFFER writeBuf;
CY_DATA_BUFFER readBuf;
writeBuf.buffer = &wr_data;
writeBuf.length = 1;
readBuf.buffer = &rd_data;
readBuf.length = 1;
wr_data = 0x06; /* Write enable */
status = CySpiReadWrite (handle, &readBuf, &writeBuf, 5000);
if (status != CY_SUCCESS)
{
return status;
}
return status;
}
//Helper functions for doing data transfer to/from SPI flash
int spiWaitForIdle (CY_HANDLE handle)
{
char rd_data[2], wr_data[2];
CY_DATA_BUFFER writeBuf, readBuf;
writeBuf.length = 2;
writeBuf.buffer = (unsigned char *)wr_data;
int timeout = 0xFFFF;
CY_RETURN_STATUS status;
readBuf.length = 2;
readBuf.buffer = (unsigned char *)rd_data;
do
{
wr_data[0] = 0x05; /* Status */
status = CySpiReadWrite (handle, &readBuf, &writeBuf, 5000);
if (status != CY_SUCCESS)
{
break;
}
timeout--;
if (timeout == 0)
{
status = CY_ERROR_IO_TIMEOUT;
return status;
}
} while (rd_data[1] & 0x01);
return status;
}
int spiVerifyData (int deviceNumber, int interfaceNum)
{
CY_DATA_BUFFER dataBufferWrite,dataBufferRead;
CY_HANDLE handle;
bool isVerify = true;
unsigned char wbuffer[256 + 4], rbuffer[256 + 4];
int rStatus, length;
memset (rbuffer, 0x00, 256);
memset (wbuffer, 0x00, 256);
rStatus = CyOpen (deviceNumber, interfaceNum, &handle);
if (rStatus != CY_SUCCESS){
printf ("CY_SPI: Open failed \n");
return rStatus;
}
dataBufferWrite.buffer = wbuffer;
dataBufferRead.buffer = rbuffer;
rStatus = spiWaitForIdle (handle);
if (rStatus){
printf("Error in Waiting for EEPOM active state %d \n", rStatus);
CyClose (handle);
return CY_ERROR_REQUEST_FAILED;
}
printf ("Calling spi write enable \n");
rStatus = spiWriteEnable (handle);
if (rStatus){
printf("Error in setting Write Enable %d \n", rStatus);
CyClose (handle);
return CY_ERROR_REQUEST_FAILED;
}
//Write SPI write command
wbuffer[0] = 0x02;
//SPI flash address
wbuffer[1] = (pageAddress >> 8);
wbuffer[2] = (pageAddress) & 0x00FF;
wbuffer[3] = 0;
printf ("The Data written is ...\n");
printf ("\n--------------------------------------------------------------------\n");
for (rStatus = 4; rStatus < (readWriteLength + 4); rStatus++){
wbuffer[rStatus] = rand() % 256;
printf ("%x ",wbuffer[rStatus]);
}
printf ("\n--------------------------------------------------------------------\n");
dataBufferRead.length = (4 + readWriteLength);
dataBufferWrite.length = (4 + readWriteLength);
rStatus = CySpiReadWrite (handle , &dataBufferRead, &dataBufferWrite, 5000);
if (rStatus != CY_SUCCESS){
CyClose (handle);
printf ("Error in doing SPI data write data Write is %d data read is %d\n" , dataBufferWrite.transferCount,dataBufferRead.transferCount);
return CY_ERROR_REQUEST_FAILED;
}
spiWaitForIdle (handle);
//Write SPI read command
wbuffer[0] = 0x03;
dataBufferRead.length = (4 + readWriteLength);
dataBufferWrite.length = (4 + readWriteLength);
rStatus = CySpiReadWrite (handle, &dataBufferRead, &dataBufferWrite, 5000);
if (rStatus != CY_SUCCESS){
CyClose (handle);
printf ("The Error is %d \n", rStatus);
printf ("Error in doing SPI data write data Write is %d data read is %d\n" , dataBufferWrite.transferCount,dataBufferRead.transferCount);
return CY_ERROR_REQUEST_FAILED;
}
printf ("Data Read back is \n");
printf ("\n---------------------------------------------------------------------\n");
for (rStatus = 4; rStatus < (readWriteLength + 4); rStatus++){
printf ("%x ",rbuffer[rStatus]);
if (rbuffer[rStatus] != wbuffer[rStatus]){
isVerify = false;
}
}
printf ("\n---------------------------------------------------------------------\n");
if (isVerify)
printf ("Data verified successfully \n");
else
printf ("Data corruption occured!!\n");
CyClose (handle);
return CY_SUCCESS;
}
int i2cVerifyData (int deviceNumber, int interfaceNum)
{
CY_DATA_BUFFER dataBufferWrite, dataBufferRead;
CY_HANDLE handle;
int length = 0;
bool isVerify = true;
int loopCount = 100, i;
CY_RETURN_STATUS rStatus;
unsigned char bytesPending = 0, address[2], wbuffer[66], rbuffer[66];
CY_I2C_DATA_CONFIG i2cDataConfig;
memset (wbuffer, 0, 66);
memset (rbuffer, 0, 66);
i2cDataConfig.isStopBit = true;
i2cDataConfig.slaveAddress = 0x51;
rStatus = CyOpen (deviceNumber, interfaceNum, &handle);
if (rStatus != CY_SUCCESS){
printf("CY_I2C: Open failed \n");
return rStatus;
}
loopCount = 100;
length = readWriteLength;
wbuffer[0]= pageAddress;
wbuffer[1] = 0;
dataBufferWrite.buffer = wbuffer;
i2cDataConfig.isStopBit = true;
dataBufferWrite.length = (length + 2);
printf ("\n Data that is written on to i2c ...\n");
printf ("\n-----------------------------------------------------------------\n");
for (i = 2; i < (length +2); i++){
wbuffer[i] = rand() % 256;
printf ("%x ", wbuffer[i]);
}
printf ("\n-----------------------------------------------------------------\n");
rStatus = CyI2cWrite (handle, &i2cDataConfig, &dataBufferWrite, 5000);
if (rStatus != CY_SUCCESS){
printf ("Error in doing i2c write \n");
CyClose (handle);
return -1;
}
//We encountered a error in I2C read repeat the procedure again
//Loop here untill Read vendor command passes
i2cDataConfig.isStopBit = false;
dataBufferWrite.length = 2;
do {
rStatus = CyI2cWrite (handle, &i2cDataConfig, &dataBufferWrite, 5000);
loopCount--;
}while (rStatus != CY_SUCCESS && loopCount != 0);
if (loopCount == 0 && rStatus != CY_SUCCESS){
printf ("Error in sending read command \n");
CyClose (handle);
return -1;
}
dataBufferRead.buffer = rbuffer;
rbuffer[0]= address[0];
rbuffer[1] = 0;
i2cDataConfig.isStopBit = true;
i2cDataConfig.isNakBit = true;
dataBufferRead.length = length;
dataBufferRead.buffer = rbuffer;
memset (rbuffer, 0, 64);
rStatus = CyI2cRead (handle, &i2cDataConfig, &dataBufferRead, 5000);
if (rStatus != CY_SUCCESS){
printf ("Error in doing i2c read ... Error is %d \n", rStatus);
CyClose (handle);
return -1;
}
printf ("\n Data that is read from i2c ...\n");
printf ("\n-----------------------------------------------------------------\n");
for (rStatus = 0; rStatus < length; rStatus++){
printf ("%x ", rbuffer[rStatus]);
if (rbuffer[rStatus] != wbuffer[rStatus + 2]){
isVerify = false;
}
}
printf ("\n-----------------------------------------------------------------\n");
if (!isVerify)
printf ("Data corruption occured ..!!!\n");
else
printf ("Data verified successfully \n");
CyClose (handle);
}
int uartVerifyData (int deviceNumber, int interfaceNum)
{
CY_DATA_BUFFER dataBufferWrite;
CY_HANDLE handle;
bool isVerify = true;
unsigned char wbuffer[256 + 4];
int rStatus, length;
memset (wbuffer, 0x00, 256);
rStatus = CyOpen (deviceNumber, interfaceNum, &handle);
if (rStatus != CY_SUCCESS){
printf ("CY_UART: Open failed \n");
return rStatus;
}
dataBufferWrite.buffer = wbuffer;
printf ("The Data written is ...\n");
printf ("\n--------------------------------------------------------------------\n");
for (rStatus = 4; rStatus < (2 + 4); rStatus++){
wbuffer[rStatus] = rand() % 256;
printf ("%x ",wbuffer[rStatus]);
}
printf ("\n--------------------------------------------------------------------\n");
dataBufferWrite.length = (4 + 2);
rStatus = CyUartWrite (handle , &dataBufferWrite, 5000);
if (rStatus != CY_SUCCESS){
CyClose (handle);
printf ("Error in doing UART data write data Write is %d\n" , dataBufferWrite.transferCount);
return CY_ERROR_REQUEST_FAILED;
}
CyClose (handle);
return CY_SUCCESS;
}
bool isCypressDevice (int deviceNum) {
CY_HANDLE handle;
unsigned char interfaceNum = 0;
unsigned char sig[6];
CY_RETURN_STATUS rStatus;
rStatus = CyOpen (deviceNum, interfaceNum, &handle);
if (rStatus == CY_SUCCESS){
rStatus = CyGetSignature (handle, sig);
if (rStatus == CY_SUCCESS){
CyClose (handle);
return true;
}
else {
CyClose (handle);
return false;
}
}
else
return false;
}
void printListOfDevices (bool isPrint)
{
int index_i = 0, index_j = 0, i, j, countOfDevice = 0, devNum;
int length, index = 0, numInterfaces, interfaceNum;
bool set1 = false;
unsigned char deviceID[CY_MAX_DEVICES];
unsigned char functionality[64];
CY_DEVICE_INFO deviceInfo;
CY_DEVICE_CLASS deviceClass[CY_MAX_INTERFACES];
CY_DEVICE_TYPE deviceType[CY_MAX_INTERFACES];
CY_RETURN_STATUS rStatus;
deviceAddedRemoved = false;
CyGetListofDevices (&numDevices);
//printf ("The number of devices is %d \n", numDevices);
for (i = 0; i < numDevices; i++){
for (j = 0; j< CY_MAX_INTERFACES; j++)
glDevice[i].interfaceFunctionality[j] = -1;
}
if (isPrint){
printf ("\n\n---------------------------------------------------------------------------------\n");
printf ("Device Number | VID | PID | INTERFACE NUMBER | FUNCTIONALITY \n");
printf ("---------------------------------------------------------------------------------\n");
}
cyDevices = 0;
for (devNum = 0; devNum < numDevices; devNum++){
rStatus = CyGetDeviceInfo (devNum, &deviceInfo);
interfaceNum = 0;
if (!rStatus)
{
if (!isCypressDevice (devNum)){
continue;
}
strcpy (functionality, "NA");
numInterfaces = deviceInfo.numInterfaces;
glDevice[index].numInterface = numInterfaces;
cyDevices++;
while (numInterfaces){
if (deviceInfo.deviceClass[interfaceNum] == CY_CLASS_VENDOR)
{
glDevice[index].deviceNumber = devNum;
switch (deviceInfo.deviceType[interfaceNum]){
case CY_TYPE_I2C:
glDevice[index].interfaceFunctionality[interfaceNum] = CY_TYPE_I2C;
strcpy (functionality, "VENDOR_I2C");
glDevice[index].isI2c = true;
break;
case CY_TYPE_SPI:
glDevice[index].interfaceFunctionality[interfaceNum] = CY_TYPE_SPI;
strcpy (functionality, "VENDOR_SPI");
glDevice[index].isSpi = true;
break;
case CY_TYPE_UART:
glDevice[index].interfaceFunctionality[interfaceNum] = CY_TYPE_UART;
strcpy (functionality, "VENDOR_UART");
glDevice[index].isUart = true;
break;
default:
strcpy (functionality, "NA");
break;
}
}
else if (deviceInfo.deviceClass[interfaceNum] == CY_CLASS_CDC){
strcpy (functionality, "NA");
}
if (isPrint) {
printf ("%d |%x |%x | %d | %s\n", \
index, \
deviceInfo.vidPid.vid, \
deviceInfo.vidPid.pid, \
interfaceNum, \
functionality \
);
}
interfaceNum++;
numInterfaces--;
}
index++;
}
}
if (isPrint){
printf ("---------------------------------------------------------------------------------\n\n");
}
}