-
Notifications
You must be signed in to change notification settings - Fork 1
/
ftdi_mid.c
executable file
·1353 lines (1229 loc) · 37 KB
/
ftdi_mid.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 ftdi_mid.c
*
* \author FTDI
* \date 20110321
*
* Copyright © 2000-2014 Future Technology Devices International Limited
*
*
* THIS SOFTWARE IS PROVIDED BY FUTURE TECHNOLOGY DEVICES INTERNATIONAL LIMITED ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL FUTURE TECHNOLOGY DEVICES INTERNATIONAL LIMITED
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*
* Project: libMPSSE
* Module: Middle Layer
*
* Rivision History:
* 0.1 - initial version
* 0.2 - 20110524 - updated for SPI and cleaned up
* 0.21 - 20110708 - Added functions FT_ReadGPIO & FT_WriteGPIO
* 0.3 - 20111103 - Added MPSSE_CMD_ENABLE_DRIVE_ONLY_ZERO
* 0.41 - 20140903 - fixed compile warnings
*/
/******************************************************************************/
/* Include files */
/******************************************************************************/
/*moved this two header file inclusion to ftdi_mid.h*/
#include "ftdi_infra.h" /*Common portable infrastructure(datatypes, libraries, etc)*/
#include "ftdi_common.h" /*Common across I2C, SPI, JTAG modules*/
#include "ftdi_mid.h" /*Midlayer specific specific*/
#include "string.h"
#ifdef __APPLE__
#include <stdarg.h>
#endif
/******************************************************************************/
/* Macro defines */
/******************************************************************************/
/******************************************************************************/
/* Local function declarations */
/******************************************************************************/
/******************************************************************************/
/* Global variables */
/******************************************************************************/
/******************************************************************************/
/* Public function definitions */
/******************************************************************************/
/*!
* \brief Returns the number of MPSSE channels a available
*
* This function returns the number of MPSSE channels that are connected to the host system
* Although all the MPSSE channels that are available in the current chips support SPI/I2C/JTAG,
* future versions of MPSSE may have support for other legacy protocols too. The caller of this
* function (i.e. I2C_GetNumChannel, SPI_GetNumChannel, etc) should check if all the channels
* reported by this function support the legacy protocol that the caller needs to support.
*
* \param[in] Protocol Specifies the protocol type(I2C/SPI/JTAG)
* \param[out] *numChans
* \return status
* \sa
* \note This function doesn't return the number of FTDI chips connected to the host system
* \note FT2232D has 1 MPSSE port
* \note FT2232H has 2 MPSSE ports
* \note FT4232H has 4 ports but only 2 of them have MPSSEs
* so a call to this function will return 2 if a FT4232 is connected to it.
* \warning
*/
FT_STATUS FT_GetNumChannels(FT_LegacyProtocol Protocol, uint32 *numChans)
{
DWORD tempNumChannels;
FT_DEVICE_LIST_INFO_NODE *pDeviceList;
FT_DEVICE_LIST_INFO_NODE deviceList;
FT_STATUS status;
uint32 devLoop = MID_NO_CHANNEL_FOUND;
//bool ret;
FN_ENTER;
/*initalize *numChansto 0 */
*numChans = MID_NO_CHANNEL_FOUND;
/*Get the number of devices connected to the system(FT_CreateDeviceInfoList)*/
status = varFunctionPtrLst.p_FT_GetNumChannel(&tempNumChannels);
// printf("\n status=0x%x tempNumChannels=%d\n",status,tempNumChannels);
/*Check if the status is Ok */
if(status == FT_OK)
{
/*Check if No of channel is greater than 0*/
if(tempNumChannels > MID_NO_CHANNEL_FOUND)
{
/*Allocate space for getting the information about the device (based on
number of devices)*/
pDeviceList = INFRA_MALLOC(sizeof(FT_DEVICE_LIST_INFO_NODE)\
*tempNumChannels);
if(NULL == pDeviceList)
{
return FT_INSUFFICIENT_RESOURCES;
}
/*get the devices information(FT_GetDeviceInfoList)*/
status = varFunctionPtrLst.p_FT_GetDeviceInfoList(pDeviceList,\
&tempNumChannels);
while(devLoop < tempNumChannels)
{
deviceList = pDeviceList[devLoop];
/*check if device is I2C*/
//ret = midCheckI2C(deviceList);
if(Mid_CheckMPSSEAvailable(deviceList))
{
/*increment *numChans*/
*numChans = *numChans + 1;
}
devLoop++;
}
INFRA_FREE(pDeviceList);
}
else
{
*numChans = tempNumChannels;
//resource Not found MID_T0DO
}
}
else
{
*numChans = tempNumChannels;
}
/*return status*/
FN_EXIT;
return status;
}
/*!
* \brief Provides information about channel
*
* This function takes a channel index (valid values are from 1 to the value returned by
* I2C_GetNumChannels) and provides information about the channel in the form of a populated
* ChannelInfo structure.
*
* \param[in] Protocol Specifies the protocol type(I2C/SPI/JTAG)
* \param[in] index Index of the channel
* \param[out] chanInfo Pointer to device information structure
* \return status
* \sa
* \note memory should be allocated and freed by caller
* \warning
*/
FT_STATUS FT_GetChannelInfo(FT_LegacyProtocol Protocol, uint32 index,
FT_DEVICE_LIST_INFO_NODE *chanInfo)
{
DWORD tempNumChannels;
uint32 channelCount;
FT_DEVICE_LIST_INFO_NODE *pDeviceList;
FT_DEVICE_LIST_INFO_NODE deviceList;
FT_STATUS status;
uint32 devLoop = MID_NO_CHANNEL_FOUND;
FN_ENTER;
/*initalize *numChansto 0 */
channelCount = MID_NO_CHANNEL_FOUND;
/*Get the number of devices connected to the system(FT_CreateDeviceInfoList)*/
status = varFunctionPtrLst.p_FT_GetNumChannel(&tempNumChannels);
CHECK_STATUS(status);
/*Check if No of channel is greater than 0*/
if(tempNumChannels >= index)
{
/*Allocate space for getting the information about the device (based on
number of devices)*/
pDeviceList = INFRA_MALLOC(sizeof(FT_DEVICE_LIST_INFO_NODE)\
*tempNumChannels);
if(NULL == pDeviceList)
{
return FT_INSUFFICIENT_RESOURCES;
}
/*get the devices information(FT_GetDeviceInfoList)*/
status = varFunctionPtrLst.p_FT_GetDeviceInfoList(pDeviceList,\
&tempNumChannels);
CHECK_STATUS(status);
while(devLoop < tempNumChannels)
{
deviceList = pDeviceList[devLoop];
if(Mid_CheckMPSSEAvailable(deviceList))
{
/*increment *numChans*/
channelCount++;
}
/*check if the index matches the I2C channel count*/
if(channelCount == index)
{
INFRA_MEMCPY(chanInfo,&deviceList,sizeof(FT_DEVICE_LIST_INFO_NODE));
break;
}
devLoop++;
}
INFRA_FREE(pDeviceList);
}
else
{
/* The index of the device is greater than the max number of devices available */
return FT_INVALID_HANDLE;
}
FN_EXIT;
/*return status*/
return status;
}
/*!
* \brief Opens a channel and returns a handle to it
*
* This function opens the indexed channel and returns a handle to it
*
* \param[in] Protocol Specifies the protocol type(I2C/SPI/JTAG)
* \param[in] index Index of the channel
* \param[out] handle Pointer to the handle
* \return status
* \sa
* \note Trying to open an already open channel will return an error code
* \warning
*/
FT_STATUS FT_OpenChannel(FT_LegacyProtocol Protocol, uint32 index,
FT_HANDLE *handle)
{
/* Opens a channel and returns the pointer to its handle */
DWORD tempNumChannels;
uint32 channelCount;
FT_DEVICE_LIST_INFO_NODE *pDeviceList;
FT_DEVICE_LIST_INFO_NODE deviceList;
FT_STATUS status;
uint32 devLoop = MID_NO_CHANNEL_FOUND;
FN_ENTER;
/*initalize *numChansto 0 */
channelCount = MID_NO_CHANNEL_FOUND;
/*Get the number of devices connected to the system(FT_CreateDeviceInfoList)*/
status = varFunctionPtrLst.p_FT_GetNumChannel(&tempNumChannels);
CHECK_STATUS(status);
/*Check if No of channel is greater than 0*/
if(tempNumChannels >= index)
{
/*Allocate space for getting the information about the device (based on
number of devices)*/
pDeviceList = INFRA_MALLOC(sizeof(FT_DEVICE_LIST_INFO_NODE)\
*tempNumChannels);
if(NULL == pDeviceList)
{
return FT_INSUFFICIENT_RESOURCES;
}
/*get the devices information(FT_GetDeviceInfoList)*/
status = varFunctionPtrLst.p_FT_GetDeviceInfoList(pDeviceList,\
&tempNumChannels);
CHECK_STATUS(status);
/*loop until No of devices */
while(devLoop < tempNumChannels)
{
deviceList = pDeviceList[devLoop];
/*check if device is I2C*/
if(Mid_CheckMPSSEAvailable(deviceList))
{
/*increment *numChans*/
channelCount++;
}
/*check if the index matches the I2C channel count*/
if(channelCount == index)
{
/*call FT_Open*/
status = varFunctionPtrLst.p_FT_Open(devLoop,handle);
break;
}
devLoop++;
}
INFRA_FREE(pDeviceList);
}
else
{
/* The index of the device is greater than the max number of devices available */
return FT_INVALID_HANDLE;
}
FN_EXIT;
/*return status*/
return status;
}
/*!
* \brief Initializes a channel
*
* This function initializes the channel and the communication parameters associated with it. The
* function takes variable number of parameters. For example, if channelType is I2C then the
* number of variable parameters will be equal to the number of members in the structure
* ChannelConfig. The variable parameters will be passed to this function in the same order as
* they appear in the structure defination
* The function performs the USB function specific initialization followed by MPSSE initialization.
* Once that is done it will configure MPSSE with the legacy protocol specific initializations(SPI/
* I2C/JTAG) with the help of that parameters that are passed by the caller
*
* \param[in] Protocol Specifies the protocol type(I2C/SPI/JTAG)
* \param[in] handle Handle of the channel
* \param[in] varArg1 Clock rate
* \param[in] varArg2 Latency timer
* \param[in] varArg3 Configuration options
* \return status
* \sa
* \note
* \warning
*/
FT_STATUS FT_InitChannel(FT_LegacyProtocol Protocol, FT_HANDLE handle,...)
{
va_list argumentList;
uint32 clockRate,latencyTimer,configOptions;
FT_STATUS status;
FT_DEVICE ftDevice;
FN_ENTER;
/*initialise the argument list*/
va_start(argumentList,handle);
/*Get the value for Clockrate*/
clockRate = va_arg(argumentList,uint32);
/*latencyTimer*/
latencyTimer = va_arg(argumentList,uint32);
/* The options parameter passed in I2C_Init, SPI_Init */
configOptions = va_arg(argumentList,uint32);
/*Check parameters*/
if((clockRate<MIN_CLOCK_RATE)||(clockRate>MAX_CLOCK_RATE)||(latencyTimer<
MIN_LATENCY_TIMER)||(latencyTimer>MAX_LATENCY_TIMER))
return FT_INVALID_PARAMETER;
/*Get the device type*/
status = Mid_GetFtDeviceType(handle, &ftDevice);
CHECK_STATUS(status);
/*reset the device*/
status = Mid_ResetDevice(handle);
CHECK_STATUS(status);
/*Purge*/
status = Mid_PurgeDevice(handle);
CHECK_STATUS(status);
/*set USB buffer size*/
status = Mid_SetUSBParameters(handle,\
USB_INPUT_BUFFER_SIZE,USB_OUTPUT_BUFFER_SIZE);
CHECK_STATUS(status);
/*sets the special characters for the device,
disable event and error characters*/
status = Mid_SetDeviceSpecialChar(handle,FALSE,DISABLE_EVENT,FALSE,\
DISABLE_CHAR);
CHECK_STATUS(status);
/*SetTimeOut*/
#ifdef FT800_HACK
status = Mid_SetDeviceTimeOut(handle, 0 \
/*DEVICE_READ_TIMEOUT_INFINITE*/,DEVICE_WRITE_TIMEOUT);
CHECK_STATUS(status);
#else
status = Mid_SetDeviceTimeOut(handle, 5000 \
/*DEVICE_READ_TIMEOUT_INFINITE*/,DEVICE_WRITE_TIMEOUT);
CHECK_STATUS(status);
#endif
/*SetLatencyTimer*/
status = Mid_SetLatencyTimer(handle,(UCHAR)latencyTimer);
CHECK_STATUS(status);
/*ResetMPSSE*/
status = Mid_ResetMPSSE(handle);
CHECK_STATUS(status);
/*EnableMPSSEInterface*/
status = Mid_EnableMPSSEIn(handle);
CHECK_STATUS(status);
/*20110608 - enabling loopback before sync*/
status = Mid_SetDeviceLoopbackState(handle,MID_LOOPBACK_TRUE);
CHECK_STATUS(status);
/*Sync MPSSE */
status = Mid_SyncMPSSE(handle);
CHECK_STATUS(status);
/*wait for USB*/
INFRA_SLEEP(50);
/*set Clock frequency*/
status = Mid_SetClock(handle, ftDevice, clockRate);
CHECK_STATUS(status);
DBG(MSG_INFO, "Mid_SetClock Status Ok return 0x%x\n",(unsigned)status);
INFRA_SLEEP(20);
/*Stop Loop back*/
status = Mid_SetDeviceLoopbackState(handle,MID_LOOPBACK_FALSE);
CHECK_STATUS(status);
DBG(MSG_INFO, "Mid_SetDeviceLoopbackState Status Ok return 0x%x\n",\
(unsigned)status);
status = Mid_EmptyDeviceInputBuff(handle);
CHECK_STATUS(status);
DBG(MSG_INFO, "Mid_EmptyDeviceInputBuff Status Ok return 0x%x\n",(unsigned)\
status);
switch(Protocol)
{
case I2C:
{
/*Set i/o pin states*/
status = Mid_SetGPIOLow(handle,MID_SET_LOW_BYTE_DATA_BITS_DATA,\
MID_SET_LOW_BYTE_DATA_BITS_DATA);
CHECK_STATUS(status);
/* The I2C master should actually drive the SDA line only when the output is LOW.
It should tristate the SDA line when the output should be high. This tristating
the SDA line during output HIGH is supported only in FT232H chip*/
#if 1
if((ftDevice == FT_DEVICE_232H) && (configOptions & I2C_ENABLE_DRIVE_ONLY_ZERO))
{
uint8 buffer[3];//3
uint32 noOfBytesToTransfer;
uint32 noOfBytesTransferred;
DBG(MSG_DEBUG,"Enabling DRIVE_ONLY_ZERO\n");
noOfBytesToTransfer = 3;
noOfBytesTransferred = 0;
buffer[0] = MPSSE_CMD_ENABLE_DRIVE_ONLY_ZERO;/* MPSSE command */
buffer[1] = 0x03; /* LowByte */
buffer[2] = 0x00; /* HighByte */
status = FT_Channel_Write(I2C,handle,noOfBytesToTransfer,
buffer,&noOfBytesTransferred);
CHECK_STATUS(status);
}
#endif
}
break;
case SPI:
{
}
break;
case JTAG:
break;
default:
DBG(MSG_WARN, "undefined protocol value(%u)\n",(unsigned)Protocol);
}
FN_EXIT;
return FT_OK;
}
/*!
* \brief Closes a channel
*
* Closes a channel and frees all resources that were used by it
*
* \param[in] Protocol Specifies the protocol type(I2C/SPI/JTAG)
* \param[in] handle Handle of the channel
* \param[out] none
* \return status
* \sa
* \note
* \warning
*/
FT_STATUS FT_CloseChannel(FT_LegacyProtocol Protocol, FT_HANDLE handle)
{
FT_STATUS status;
FN_ENTER;
status = varFunctionPtrLst.p_FT_Close(handle);
FN_EXIT;
return status;
}
/*!
* \brief Reads data from channel
*
* This function reads the specified number of bytes from channel.
*
* \param[in] Protocol Specifies the protocol type(I2C/SPI/JTAG)
* \param[in] handle Handle of the channel
* \param[in] noOfBytes Number of bytes to be read
* \param[out] buffer Pointer to the buffer where data is to be read
* \param[out] noOfBytesTransferred The actual number of bytes transfered
* \return status
* \sa
* \note
* \warning
*/
FT_STATUS FT_Channel_Read(FT_LegacyProtocol Protocol, FT_HANDLE handle,
uint32 noOfBytes, uint8* buffer, uint32 *noOfBytesTransferred)
{
FT_STATUS status;
FN_ENTER;
status = varFunctionPtrLst.p_FT_Read(handle, buffer, noOfBytes, \
(DWORD*)noOfBytesTransferred);
#if 0
/* Disabled after code review */
DWORD numOfBytesToRead = 0;
DWORD numOfBytesRead = 0;
DWORD totalNumOfBytesRead = 0;
/*check if no of bytes to send is greater than Max size*/
if (noOfBytes > MID_MAX_USB_READ_BYTES)
{
do
{
if ((totalNumOfBytesRead + MID_MAX_USB_READ_BYTES) <= noOfBytes)
{
numOfBytesToRead = MID_MAX_USB_READ_BYTES;
}
else
{
numOfBytesToRead = (noOfBytes - totalNumOfBytesRead);
}
status = varFunctionPtrLst.p_FT_Read(handle,\
&buffer[totalNumOfBytesRead],numOfBytesToRead,&numOfBytesRead);
totalNumOfBytesRead = totalNumOfBytesRead + numOfBytesRead;
}while ((totalNumOfBytesRead < noOfBytes) && (status == FT_OK));
/*copy the total number of bytes read */
*noOfBytesTransferred = totalNumOfBytesRead;
}
else
{
status = varFunctionPtrLst.p_FT_Read(handle, buffer, noOfBytes, \
noOfBytesTransferred);
}
#endif
#ifdef INFRA_DEBUG_ENABLE
{
uint32 i;
printf("FT_Channel_Read called with noOfBytes=%u\n",(unsigned)noOfBytes);
for(i=0;i<noOfBytes;i++)
{
printf(" 0x%x",buffer[i]);
}
printf("\n");
}
#endif
FN_EXIT;
return status;
}
/*!
* \brief Writes data to the channel
*
* This function writes the specified number of bytes to the channel
*
* \param[in] Protocol Specifies the protocol type(I2C/SPI/JTAG)
* \param[in] handle Handle of the channel
* \param[in] noOfBytes Number of bytes to be written
* \param[in] buffer Pointer to the buffer from where data is to be written
* \param[out] noOfBytesTransferred The actual number of bytes transfered
* \return status
* \sa
* \note
* \warning
*/
FT_STATUS FT_Channel_Write(FT_LegacyProtocol Protocol, FT_HANDLE handle,
uint32 noOfBytes, uint8* buffer, uint32 *noOfBytesTransferred)
{
FT_STATUS status;
FN_ENTER;
#ifdef INFRA_DEBUG_ENABLE
{
uint32 i;
printf("FT_Channel_Write called with noOfBytes=%u\n",(unsigned)noOfBytes);
for(i=0;i<noOfBytes;i++)
{
printf(" 0x%x",buffer[i]);
}
printf("\n");
}
#endif
status = varFunctionPtrLst.p_FT_Write(handle, buffer, noOfBytes, \
(DWORD*)noOfBytesTransferred);
#if 0
/* Disabled after code review */
DWORD numOfBytesToSend = 0;
DWORD numOfBytesSent = 0;
DWORD totalNumOfBytesSent = 0;
/*check if no of bytes to send is greater than Max size*/
if (noOfBytes > MID_MAX_USB_WRITE_BYTES)
{
do
{
if ((totalNumOfBytesSent + MID_MAX_USB_WRITE_BYTES) <= noOfBytes)
{
numOfBytesToSend = MID_MAX_USB_WRITE_BYTES;
}
else
{
numOfBytesToSend = (noOfBytes - totalNumOfBytesSent);
}
status = varFunctionPtrLst.p_FT_Write(handle,\
&buffer[totalNumOfBytesSent],numOfBytesToSend,&numOfBytesSent);
totalNumOfBytesSent = totalNumOfBytesSent + numOfBytesSent;
}while ((totalNumOfBytesSent < noOfBytes) && (status == FT_OK));
/*copy the total number of bytes transfered */
*noOfBytesTransferred = totalNumOfBytesSent;
}
else
{
status = varFunctionPtrLst.p_FT_Write(handle, buffer, noOfBytes, \
noOfBytesTransferred);
}
#endif
FN_EXIT;
return status;
}
/*
*\brief Check if the device has MPSSE
* This function looks for the device type if it matches then look for the Location Id to determine
* the availability of MPSSE if matches then returns 1 otherwise returns 0
*\Param[in] FT_DEVICE_LIST_INFO_NODE device info node wich contains the information about the device
*\return bool return a boolean value
*/
bool Mid_CheckMPSSEAvailable(FT_DEVICE_LIST_INFO_NODE devList)
{
//FT_STATUS status=FT_OK;
bool isMPSSEAvailable = MID_NO_MPSSE;
FN_ENTER;
/*check TYPE field*/
// printf("\n\tdevList.Type=0x%x\n",devList.Type);
// printf("\tdevList.LocId=0x%x\n",devList.LocId);
size_t los = strlen(devList.Description);
switch(devList.Type)
{
case FT_DEVICE_2232C:
if (devList.Description[los-1] == 0x41) //Last character = 0x41 = ASCII "A"
{
isMPSSEAvailable = MID_MPSSE_AVAILABLE;
}
break;
case FT_DEVICE_2232H:
if ((devList.Description[los - 1] == 0x41) || (devList.Description[los - 1] == 0x42)) //Last character = 0x41 = ASCII "A", 0x42 = ASCII "B"
{
isMPSSEAvailable = MID_MPSSE_AVAILABLE;
}
break;
case FT_DEVICE_4232H:
if ((devList.Description[los - 1] == 0x41) || (devList.Description[los - 1] == 0x42)) //Last character = 0x41 = ASCII "A", 0x42 = ASCII "B"
{
isMPSSEAvailable = MID_MPSSE_AVAILABLE;
}
break;
case FT_DEVICE_232H:
isMPSSEAvailable = MID_MPSSE_AVAILABLE;
break;
default:
break;
};
FN_EXIT;
return isMPSSEAvailable;
}
/*!
* \brief Reset the device
*
* This function calles the FT_Reset to reset the device
* \param[in] handle of the device
* \return status
* \sa
* \note
* \warning
*/
FT_STATUS Mid_ResetDevice(FT_HANDLE handle)
{
FT_STATUS status;
FN_ENTER;
status = varFunctionPtrLst.p_FT_ResetDevice(handle);
FN_EXIT;
return status;
}
/*!
* \brief Purge the device
*
* This function clears the Input and Output buffer of the device
* \param[in] handle Handle of the channel
* \return status
* \sa
* \note
* \warning
*/
FT_STATUS Mid_PurgeDevice (FT_HANDLE handle)
{
FT_STATUS status;
FN_ENTER;
status = varFunctionPtrLst.p_FT_Purge(handle, FT_PURGE_RX | FT_PURGE_TX);
FN_EXIT;
return status;
}
/*!
* \brief Sets the Input and Output buffer size
*
* This function sets the Input and Output buffer size as requested in the parameter.
*
* \param[in] handle Handle of the channel
* \param[in] inputBufSize Size of the Input buffer
* \param[in] outputBufSize Size of the Output buffer
* \return status
* \sa
* \note
* \warning
*/
FT_STATUS Mid_SetUSBParameters(FT_HANDLE handle,DWORD inputBufSize,DWORD \
outputBufSize)
{
FT_STATUS status;
FN_ENTER;
status = varFunctionPtrLst.p_FT_SetUSBParameters(handle,inputBufSize,\
outputBufSize);
FN_EXIT;
return status;
}
/*!
* \brief Sets the events and error characters
*
* This function sets the events and special characters as requested in the parameter.
* \param[in] handle Handle of the channel
* \param[in] eventCh event char to set
* \param[in] eventStatus 0 if event character disabled, non-zero otherwise
* \param[in] errorCh error char to set
* \param[in] errorStatus 0 if error character disabled, non-zero otherwise
* \return status
* \sa
* \note
* \warning
*/
FT_STATUS Mid_SetDeviceSpecialChar(FT_HANDLE handle,UCHAR eventCh,
UCHAR eventStatus, UCHAR errorCh, UCHAR errorStatus)
{
FT_STATUS status;
FN_ENTER;
status = varFunctionPtrLst.p_FT_SetChars(handle,eventCh,eventStatus,\
errorCh,errorStatus);
FN_EXIT;
return status;
}
/*!
* \brief sets read and write timeout
*
* This function set the read and write timeout as requested in the parameter
*
* \param[in] handle Handle of the channel
* \param[in] rdTimeOut timeout valu for read
* \param[in] wrTimeOut timeout valu for write
* \return status
* \sa
* \note
* \warning
*/
FT_STATUS Mid_SetDeviceTimeOut(FT_HANDLE handle,DWORD rdTimeOut,\
DWORD wrTimeOut)
{
FT_STATUS status;
FN_ENTER;
status = varFunctionPtrLst.p_FT_SetTimeouts(handle,rdTimeOut,wrTimeOut);
FN_EXIT;
return status;
}
/*!
* \brief sets the latency timer
*
* This function set the latency timer for the device as requested in the parameter
* \param[in] handle Handle of the channel
* \param[in] milliSecond time in millisecond to be set for latencytimer
* \return status
* \sa
* \note
* \warning
*/
FT_STATUS Mid_SetLatencyTimer (FT_HANDLE handle, UCHAR milliSecond)
{
FT_STATUS status;
FN_ENTER;
status = varFunctionPtrLst.p_FT_SetLatencyTimer(handle,milliSecond);
FN_EXIT;
return status;
}
/*!
* \brief Set the bit mode to zero
*
* This function sets the bit mode to 0
* \param[in] handle Handle of the channel
* \return status
* \sa
* \note
* \warning
*/
FT_STATUS Mid_ResetMPSSE(FT_HANDLE handle)
{
FT_STATUS status;
FN_ENTER;
status = varFunctionPtrLst.p_FT_SetBitmode(handle, INTERFACE_MASK_IN,\
RESET_INTERFACE);
FN_EXIT;
return status;
}
/*!
* \brief enable MPSSE
*
* This function set the bit 2 to enable the MPSSE
* \param[in] handle Handle of the channel
* \return status
* \sa
* \note
* \warning
*/
FT_STATUS Mid_EnableMPSSEIn(FT_HANDLE handle)
{
FT_STATUS status;
FN_ENTER;
status = varFunctionPtrLst.p_FT_SetBitmode(handle,INTERFACE_MASK_IN,\
ENABLE_MPSSE);
FN_EXIT;
return status;
}
/*!
* \brief syncronize MPSSE Channel
*
* This function syncronice the MPSSE by continiously sending 0XAA until it is it is readback
*and then sends 0XAB once and read until we get it back
* \param[in] handle Handle of the channel
* \return status
* \sa
* \note
* \warning
*/
FT_STATUS Mid_SyncMPSSE(FT_HANDLE handle)
{
FT_STATUS status;
UCHAR cmdEchoed;
FN_ENTER;
status = Mid_EmptyDeviceInputBuff(handle);
CHECK_STATUS(status);
/*send and receive command*/
status = Mid_SendReceiveCmdFromMPSSE(handle,MID_ECHO_COMMAND_CONTINUOUSLY,\
MID_ECHO_CMD_1,&cmdEchoed);
CHECK_STATUS(status);
/**/
if(cmdEchoed == MID_CMD_ECHOED)
{
status = Mid_SendReceiveCmdFromMPSSE(handle,MID_ECHO_COMMAND_ONCE,\
MID_ECHO_CMD_2,&cmdEchoed);
CHECK_STATUS(status);
if (cmdEchoed != MID_CMD_ECHOED)
{
status = FT_OTHER_ERROR;
}
}
else
{
status = FT_OTHER_ERROR;
}
FN_EXIT;
return status;
}
/*!
* \brief sends the data and read it back from the device
*
* This function sends the ecoCmd based on the ecoCmdFlag(continiously/Once) and reads it back
* \param[in] handle Handle of the channel
* \param[in] echoCmdFlag specifies whether to sen char continiously or once
* \param[in] ecoCmd char to be sent
* \param[out] cmdEchoed 1 if char echoed from device otherwise 0
* \return status
* \sa
* \note
* \warning
*/
FT_STATUS Mid_SendReceiveCmdFromMPSSE(FT_HANDLE handle, UCHAR echoCmdFlag,UCHAR ecoCmd,UCHAR *cmdEchoed)
{
FT_STATUS status;
DWORD bytesInInputBuf = 0;
DWORD numOfBytesRead = 0;
DWORD bytesWritten;
DWORD byteCounter;
UCHAR cmdResponse = MID_CMD_NOT_ECHOED;
int loopCounter = 0;
UCHAR *readBuffer=NULL;
FN_ENTER;
readBuffer = (UCHAR*)INFRA_MALLOC(MID_MAX_IN_BUF_SIZE);
if(NULL == readBuffer)
{
return FT_INSUFFICIENT_RESOURCES;
}
/*initialize cmdEchoed to MID_CMD_NOT_ECHOED*/
*cmdEchoed = MID_CMD_NOT_ECHOED;
/* check whether command has to be sent only once*/
if (echoCmdFlag == MID_ECHO_COMMAND_ONCE)
{
status = varFunctionPtrLst.p_FT_Write(handle,&ecoCmd,1,&bytesWritten);
CHECK_STATUS(status);
}
do
{
DBG(MSG_DEBUG,"In Loop Mid_SendReceiveCmdFromMPSSE loopCounter = %d \n"\
,loopCounter);
/*check whether command has to be sent every time in the loop*/
if(echoCmdFlag == MID_ECHO_COMMAND_CONTINUOUSLY)
{
status = varFunctionPtrLst.p_FT_Write(handle,&ecoCmd,1,&bytesWritten);
CHECK_STATUS(status);
}
/*read the no of bytes available in Receive buffer*/
status = varFunctionPtrLst.p_FT_GetQueueStatus(handle,&bytesInInputBuf);
CHECK_STATUS(status);
INFRA_SLEEP(1);
DBG(MSG_DEBUG,"bytesInInputBuf size = %d\n",bytesInInputBuf);
if(bytesInInputBuf >0)
{
MID_CHK_IN_BUF_OK(bytesInInputBuf);
status = varFunctionPtrLst.p_FT_Read(handle,readBuffer,bytesInInputBuf,&numOfBytesRead);
CHECK_STATUS(status);
if(numOfBytesRead >0)
{
byteCounter = 0;
do
{
if(byteCounter <= (numOfBytesRead-1))
{
if(readBuffer[byteCounter]==MID_BAD_COMMAND_RESPONSE)
{
cmdResponse = MID_BAD_COMMAND_RESPONSE;
}
else
{
if(cmdResponse == MID_BAD_COMMAND_RESPONSE)
{
if(readBuffer[byteCounter]==ecoCmd)
{
*cmdEchoed = MID_CMD_ECHOED;
}
}
cmdResponse = MID_CMD_NOT_ECHOED;
}
}
byteCounter++;
}while((byteCounter < bytesInInputBuf)&&(*cmdEchoed == MID_CMD_NOT_ECHOED));
}
}