-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftdi_i2c.c
1650 lines (1480 loc) · 52.8 KB
/
ftdi_i2c.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_i2c.c
*
* \author FTDI
* \date 20110321
*
* Copyright (C) 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: I2C
*
* Rivision History:
* 0.1 - initial version
* 0.2 - 20110708 - Added 3-phase-clocking functionality in I2C_InitChannel
* 0.3 - 20111103 - Added I2C_TRANSFER_OPTIONS_NACK_LAST_BYTE
* Added I2C_TRANSFER_OPTIONS_FAST_TRANSFER(bit/byte/noAddress modes)
* Returns FT_DEVICE_NOT_FOUND if addressed slave doesn't respond
* Adjustment to clock rate if 3-phase-clocking is enabled
* 0.5 - 20140918 - Modified R/W functions to fix glitch issue
* I2C_Read8bitsAndGiveAck, I2C_Write8bitsAndGetAck,
* I2C_FastRead and I2C_FastWrite
*/
/******************************************************************************/
/* Include files */
/******************************************************************************/
#include "ftdi_infra.h" /*Common portable infrastructure(datatypes, libraries, etc)*/
#include "ftdi_common.h" /*Common across I2C, SPI, JTAG modules*/
#include "ftdi_i2c.h" /*I2C specific*/
#include "ftdi_mid.h" /*Middle layer*/
/******************************************************************************/
/* Macro and type defines */
/******************************************************************************/
/* Enabling the macro will lead to checking of all parameters that are passed to the library from
the user application*/
#define ENABLE_PARAMETER_CHECKING 1
/* This macro will enable all code that is needed to support I2C_GETDEVICEID feature */
/* #define I2C_CMD_GETDEVICEID_SUPPORTED 1 */
/* This macro will enable the code to read acknowledgements from slaves in I2C_RawWrite */
#define FASTWRITE_READ_ACK
#define START_DURATION_1 10
#define START_DURATION_2 20
#define STOP_DURATION_1 10
#define STOP_DURATION_2 10
#define STOP_DURATION_3 10
#define SEND_ACK 0x00
#define SEND_NACK 0x80
#define I2C_ADDRESS_READ_MASK 0x01 /*LSB 1 = Read*/
#define I2C_ADDRESS_WRITE_MASK 0xFE /*LSB 0 = Write*/
#ifdef I2C_CMD_GETDEVICEID_SUPPORTED
/* This enum lists the supported I2C modes*/
typedef enum I2C_Modes_t{
I2C_STANDARD_MODE = 0,
I2C_FAST_MODE,
I2C_FAST_MODE_PLUS,
I2C_HIGH_SPEED_MODE,
I2C_MAXIMUM_SUPPORTED_MODES
}I2C_Modes;
/* This enum lists the various I2C bus condition*/
typedef enum I2C_Bus_Condition_t{
I2C_CONDITION_PRESTART,
I2C_CONDITION_START,
I2C_CONDITION_POSTSTART,
I2C_CONDITION_PRESTOP,
I2C_CONDITION_STOP,
I2C_CONDITION_POSTSTOP,
I2C_MAXIMUM_SUPPORTED_CONDITIONS
}I2C_Bus_Condition;
#endif
/******************************************************************************/
/* Local function declarations */
/******************************************************************************/
FT_STATUS I2C_Restart(FT_HANDLE handle);
FT_STATUS I2C_Write8bitsAndGetAck(FT_HANDLE handle, uint8 data, bool *ack);
FT_STATUS I2C_Read8bitsAndGiveAck(FT_HANDLE handle, uint8 *data, bool ack);
FT_STATUS I2C_WriteDeviceAddress(FT_HANDLE handle, uint32 deviceAddress,
bool direction, bool AddLen10Bit, bool *ack);
FT_STATUS I2C_SaveChannelConfig(FT_HANDLE handle, ChannelConfig *config);
FT_STATUS I2C_GetChannelConfig(FT_HANDLE handle, ChannelConfig *config);
FT_STATUS I2C_Start(FT_HANDLE handle);
FT_STATUS I2C_Stop(FT_HANDLE handle);
FT_STATUS I2C_FastWrite(FT_HANDLE handle, uint32 deviceAddress,
uint32 bitsToTransfer, uint8 *buffer, uint8 *ack, uint32 *bytesTransferred,
uint32 options);
FT_STATUS I2C_FastRead(FT_HANDLE handle,uint32 deviceAddress,
uint32 bitsToTransfer, uint8 *buffer, uint8 *ack, uint32 *bytesTransferred,
uint32 options);
/******************************************************************************/
/* Global variables */
/******************************************************************************/
#ifdef I2C_CMD_GETDEVICEID_SUPPORTED
/*!
* \brief I2C bus condition timings table
*
* This table contains the minimim time that the I2C bus needs to be held in, in order to register
* a condition such as start, stop restart, etc. The table has one row each for the different bus
* speeds.
*
* \sa
* \note
* \warning
*/
const uint64 I2C_Timings[I2C_MAXIMUM_SUPPORTED_MODES]
[I2C_MAXIMUM_SUPPORTED_CONDITIONS] = {
/*Durations for conditions are as follows:
pre-start, start, post-start, pre-stop, stop, post-stop */
{0,0,0,0,0,0,}, /* I2C_CLOCK_STANDARD_MODE */
{0,0,0,0,0,0,}, /*I2C_CLOCK_FAST_MODE */
{0,0,0,0,0,0,}, /* I2C_CLOCK_FAST_MODE_PLUS */
{0,0,0,0,0,0} /* I2C_CLOCK_HIGH_SPEED_MODE */
};
#endif
/******************************************************************************/
/* Public function definitions */
/******************************************************************************/
/*!
* \brief Gets the number of I2C channels connected to the host
*
* This function gets the number of I2C channels that are connected to the host system
* The number of ports available in each of these chips are different.
*
* \param[out] *numChannels Pointer to variable in which the no of channels will be returned
* \return Returns status code of type FT_STATUS(see D2XX Programmer's Guide)
* \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 call to this function will return 2 if a FT4232 is connected to it.
* \warning
*/
FTDI_API FT_STATUS I2C_GetNumChannels(uint32 *numChannels)
{
FT_STATUS status;
FN_ENTER;
#ifdef ENABLE_PARAMETER_CHECKING
CHECK_NULL_RET(numChannels);
#endif
status = FT_GetNumChannels(I2C, numChannels);
CHECK_STATUS(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] index Index of the channel
* \param[out] chanInfo Pointer to FT_DEVICE_LIST_INFO_NODE structure(see D2XX \
* Programmer's Guide)
* \return Returns status code of type FT_STATUS(see D2XX Programmer's Guide)
* \note The channel ID can be determined by the user from the last digit of the location ID
* \sa
* \warning
*/
FTDI_API FT_STATUS I2C_GetChannelInfo(uint32 index,
FT_DEVICE_LIST_INFO_NODE *chanInfo)
{
FT_STATUS status;
FN_ENTER;
#ifdef ENABLE_PARAMETER_CHECKING
CHECK_NULL_RET(chanInfo);
#endif
status = FT_GetChannelInfo(I2C,index+1,chanInfo);
CHECK_STATUS(status);
FN_EXIT;
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] index Index of the channel
* \param[out] handle Pointer to the handle of the opened channel
* \return Returns status code of type FT_STATUS(see D2XX Programmer's Guide)
* \sa
* \note Trying to open an already open channel will return an error code
* \warning
*/
FTDI_API FT_STATUS I2C_OpenChannel(uint32 index, FT_HANDLE *handle)
{
FT_STATUS status;
/* Opens a channel and returns the pointer to its handle */
FN_ENTER;
#ifdef ENABLE_PARAMETER_CHECKING
CHECK_NULL_RET(handle);
#endif
status = FT_OpenChannel(I2C,index+1,handle);
DBG(MSG_DEBUG,"index=%u handle=%u\n",(unsigned)index,(unsigned)*handle);
CHECK_STATUS(status);
FN_EXIT;
return status;
}
/*!
* \brief Initializes a channel
*
* This function initializes the channel and the communication parameters associated with it
*
* \param[in] handle Handle of the channel
* \param[out] config Pointer to ChannelConfig structure(memory to be allocated by caller)
* \return Returns status code of type FT_STATUS(see D2XX Programmer's Guide)
* \sa
* \note
* \warning
*/
FTDI_API FT_STATUS I2C_InitChannel(FT_HANDLE handle, ChannelConfig *config)
{
FT_STATUS status;
uint8 buffer[3];//3
uint32 noOfBytesToTransfer;
uint32 noOfBytesTransferred;
FN_ENTER;
#ifdef ENABLE_PARAMETER_CHECKING
CHECK_NULL_RET(config);
CHECK_NULL_RET(handle);
#endif
if(!(config->Options & I2C_DISABLE_3PHASE_CLOCKING))
{/* Adjust clock rate if 3phase clocking should be enabled */
config->ClockRate = (config->ClockRate * 3)/2;
}
DBG(MSG_DEBUG,"handle=0x%x ClockRate=%u LatencyTimer=%u Options=0x%x\n",\
(unsigned)handle,(unsigned)config->ClockRate, \
(unsigned)config->LatencyTimer,(unsigned)config->Options);
status = FT_InitChannel(I2C,handle,(uint32)config->ClockRate, \
(uint32)config->LatencyTimer,(uint32)config->Options);
CHECK_STATUS(status);
if(!(config->Options & I2C_DISABLE_3PHASE_CLOCKING))
{
DBG(MSG_DEBUG,"Enabling 3 phase clocking\n");
noOfBytesToTransfer = 1;
noOfBytesTransferred = 0;
buffer[0] = MPSSE_CMD_ENABLE_3PHASE_CLOCKING;/* MPSSE command */
status = FT_Channel_Write(I2C,handle,noOfBytesToTransfer,
buffer,&noOfBytesTransferred);
CHECK_STATUS(status);
}
/*Save the channel's config data for later use*/
status = I2C_SaveChannelConfig(handle,config);
CHECK_STATUS(status);
FN_EXIT;
return status;
}
/*!
* \brief Closes a channel
*
* Closes a channel and frees all resources that were used by it
*
* \param[in] handle Handle of the channel
* \param[out] none
* \return Returns status code of type FT_STATUS(see D2XX Programmer's Guide)
* \sa
* \note
* \warning
*/
FTDI_API FT_STATUS I2C_CloseChannel(FT_HANDLE handle)
{
FT_STATUS status;
FN_ENTER;
#ifdef ENABLE_PARAMETER_CHECKING
CHECK_NULL_RET(handle);
#endif
status = FT_CloseChannel(I2C,handle);
CHECK_STATUS(status);
FN_EXIT;
return status;
}
/*!
* \brief Reads data from I2C slave
*
* This function reads the specified number of bytes from an addressed I2C slave
*
* \param[in] handle Handle of the channel
* \param[in] deviceAddress Address of the I2C slave
* \param[in] sizeToTransfer Number of bytes to be read
* \param[out] buffer Pointer to the buffer where data is to be read
* \param[out] sizeTransferred Pointer to variable containing the number of bytes read
* \param[in] options This parameter specifies data transfer options. Namely, if a start/stop bits
* are required, if the transfer should continue or stop if device nAcks, etc
* \return Returns status code of type FT_STATUS(see D2XX Programmer's Guide)
* \sa Definitions of macros I2C_TRANSFER_OPTIONS_START_BIT,
* I2C_TRANSFER_OPTIONS_STOP_BIT, I2C_TRANSFER_OPTIONS_BREAK_ON_NACK,
* I2C_TRANSFER_OPTIONS_FAST_TRANSFER_BYTES,
* I2C_TRANSFER_OPTIONS_FAST_TRANSFER_BITS &
* I2C_TRANSFER_OPTIONS_NO_ADDRESS
* \note
* \warning
*/
FTDI_API FT_STATUS I2C_DeviceRead(FT_HANDLE handle, uint32 deviceAddress,
uint32 sizeToTransfer, uint8 *buffer, uint32 *sizeTransferred, uint32 options)
{
FT_STATUS status=FT_OK;
bool ack=FALSE;
uint32 i;
FN_ENTER;
#ifdef ENABLE_PARAMETER_CHECKING
CHECK_NULL_RET(handle);
CHECK_NULL_RET(buffer);
CHECK_NULL_RET(sizeTransferred);
if(deviceAddress>127)
{
DBG(MSG_WARN,"deviceAddress(0x%x) is greater than 127\n", \
(unsigned)deviceAddress);
return FT_INVALID_PARAMETER;
}
#endif
DBG(MSG_DEBUG,"handle=0x%x deviceAddress=0x%x sizeToTransfer=%u options= \
0x%x\n",(unsigned)handle, (unsigned)deviceAddress, (unsigned)sizeToTransfer,
(unsigned)options);
LOCK_CHANNEL(handle);
Mid_PurgeDevice(handle);
if(options & I2C_TRANSFER_OPTIONS_FAST_TRANSFER)
{
status = I2C_FastRead(handle, deviceAddress, sizeToTransfer, buffer, NULL, sizeTransferred, options);
}
else
{
/* Write START bit */
if(options & I2C_TRANSFER_OPTIONS_START_BIT)
{
status = I2C_Start(handle);
CHECK_STATUS(status);
}
/* Write device address (with LSB=1 => READ) & Get ACK */
status = I2C_WriteDeviceAddress(handle,deviceAddress,TRUE,FALSE,&ack);
CHECK_STATUS(status);
if(!ack) /*ack bit set actually means device nAcked*/
{
/* LOOP until sizeToTransfer */
for(i=0; ((i<sizeToTransfer) && (status == FT_OK)); i++)
{
/* Read byte to buffer & Give ACK
(or nACK if it is last byte and I2C_TRANSFER_OPTIONS_NACK_LAST_BYTE is set)*/
status = I2C_Read8bitsAndGiveAck(handle,&(buffer[i]), \
(i<(sizeToTransfer-1))?TRUE: \
((options & I2C_TRANSFER_OPTIONS_NACK_LAST_BYTE)?FALSE:TRUE));
}
*sizeTransferred = i;
if(*sizeTransferred != sizeToTransfer)
{
DBG(MSG_ERR," sizeToTransfer=%u sizeTransferred=%u\n",\
(unsigned)sizeToTransfer, (unsigned)*sizeTransferred);
status = FT_IO_ERROR;
}
else
{
/* Write STOP bit */
if(options & I2C_TRANSFER_OPTIONS_STOP_BIT)
{
status = I2C_Stop(handle);
CHECK_STATUS(status);
}
}
}
else
{
DBG(MSG_ERR,"I2C device with address 0x%x didn't ack when addressed\n",(unsigned)deviceAddress);
/* Write STOP bit */
if(options & I2C_TRANSFER_OPTIONS_STOP_BIT)
{
status = I2C_Stop(handle);
CHECK_STATUS(status);
}
/*20111102 : FT_IO_ERROR was returned when a device doesn't respond to the
master when it is addressed, as well as when a data transfer fails. To distinguish
between these to errors, FT_DEVICE_NOT_FOUND is now returned after a device
doesn't respond when its addressed*/
/* old code: status = FT_IO_ERROR; */
status = FT_DEVICE_NOT_FOUND;
}
}
UNLOCK_CHANNEL(handle);
FN_EXIT;
return status;
}
/*!
* \brief Writes data from I2C slave
*
* This function writes the specified number of bytes from an addressed I2C slave
*
* \param[in] handle Handle of the channel
* \param[in] deviceAddress Address of the I2C slave
* \param[in] sizeToTransfer Number of bytes to be written
* \param[out] buffer Pointer to the buffer from where data is to be written
* \param[out] sizeTransferred Pointer to variable containing the number of bytes written
* \param[in] options This parameter specifies data transfer options. Namely if a start/stop bits
* are required, if the transfer should continue or stop if device nAcks, etc
* \return Returns status code of type FT_STATUS(see D2XX Programmer's Guide)
* \sa Definitions of macros I2C_TRANSFER_OPTIONS_START_BIT,
* I2C_TRANSFER_OPTIONS_STOP_BIT, I2C_TRANSFER_OPTIONS_BREAK_ON_NACK,
* I2C_TRANSFER_OPTIONS_FAST_TRANSFER_BYTES,
* I2C_TRANSFER_OPTIONS_FAST_TRANSFER_BITS &
* I2C_TRANSFER_OPTIONS_NO_ADDRESS
* \note
* \warning
*/
FTDI_API FT_STATUS I2C_DeviceWrite(FT_HANDLE handle, uint32 deviceAddress,
uint32 sizeToTransfer, uint8 *buffer, uint32 *sizeTransferred, uint32 options)
{
FT_STATUS status=FT_OK;
bool ack=FALSE;
uint32 i;
FN_ENTER;
#ifdef ENABLE_PARAMETER_CHECKING
CHECK_NULL_RET(handle);
CHECK_NULL_RET(buffer);
CHECK_NULL_RET(sizeTransferred);
if(deviceAddress>127)
{
DBG(MSG_WARN,"deviceAddress(0x%x) is greater than 127\n", \
(unsigned)deviceAddress);
return FT_INVALID_PARAMETER;
}
#endif
DBG(MSG_DEBUG,"handle=0x%x deviceAddress=0x%x sizeToTransfer=%u options= \
0x%x\n",(unsigned)handle, (unsigned)deviceAddress, (unsigned)sizeToTransfer,
(unsigned)options);
LOCK_CHANNEL(handle);
Mid_PurgeDevice(handle);
if(options & I2C_TRANSFER_OPTIONS_FAST_TRANSFER)
{
status = I2C_FastWrite(handle, deviceAddress, sizeToTransfer, buffer, NULL, sizeTransferred, options);
}
else
{
/* Write START bit */
if(options & I2C_TRANSFER_OPTIONS_START_BIT)
{
status = I2C_Start(handle);
CHECK_STATUS(status);
}
/* Write device address (with LSB=0 => WRITE) & Get ACK*/
status = I2C_WriteDeviceAddress(handle,deviceAddress,FALSE,FALSE,&ack);
CHECK_STATUS(status);
if(!ack) /*ack bit set actually means device nAcked*/
{
/* LOOP until sizeToTransfer */
for(i=0; ((i<sizeToTransfer) && (status == FT_OK)); i++)
{
/* Write byte to buffer & Get ACK */
ack=0;
status = I2C_Write8bitsAndGetAck(handle,buffer[i],&ack);
DBG(MSG_DEBUG,"handle=0x%x buffer[%u]=0x%x ack=0x%x \n", \
(unsigned)handle, (unsigned)i, (unsigned)buffer[i],
(unsigned)(1&ack));
if(ack)
{
DBG(MSG_WARN,"I2C device(address 0x%x) nAcked while writing\
byte no %d(i.e. 0x%x\n",(unsigned)deviceAddress,(int)i,(unsigned)buffer[i]);
/* add bit in options to return with error if device nAcked
sizeTransferred = number of correctly transfered bytes */
if(options & I2C_TRANSFER_OPTIONS_BREAK_ON_NACK)
{
/*status = FT_FAILED_TO_WRITE_DEVICE;
break;*/
DBG(MSG_WARN,"returning FT_FAILED_TO_WRITE_DEVICE \
options=0x%x ack=0x%x\n",options,ack);
/* Write STOP bit */
if(options & I2C_TRANSFER_OPTIONS_STOP_BIT)
{
status = I2C_Stop(handle);
CHECK_STATUS(status);
}
return FT_FAILED_TO_WRITE_DEVICE;
}
}
}
*sizeTransferred = i;
if(*sizeTransferred != sizeToTransfer)
{
DBG(MSG_ERR," sizeToTransfer=%u sizeTransferred=%u\n",\
(unsigned)sizeToTransfer, (unsigned)*sizeTransferred);
status = FT_IO_ERROR;
}
else
{
/* Write STOP bit */
if(options & I2C_TRANSFER_OPTIONS_STOP_BIT)
{
status = I2C_Stop(handle);
CHECK_STATUS(status);
}
}
}
else
{
DBG(MSG_ERR,"I2C device with address 0x%x didn't ack when addressed\n",(unsigned)deviceAddress);
/* Write STOP bit */
if(options & I2C_TRANSFER_OPTIONS_STOP_BIT)
{
status = I2C_Stop(handle);
CHECK_STATUS(status);
}
/*20111102 : FT_IO_ERROR was returned when a device doesn't respond to the
master when it is addressed, as well as when a data transfer fails. To distinguish
between these to errors, FT_DEVICE_NOT_FOUND is now returned after a device
doesn't respond when its addressed*/
/* old code: status = FT_IO_ERROR; */
status = FT_DEVICE_NOT_FOUND;
}
}
UNLOCK_CHANNEL(handle);
FN_EXIT;
return status;
}
#ifdef I2C_CMD_GETDEVICEID_SUPPORTED
/*!
* \brief Get the I2C device ID
*
* This function retrieves the I2C device ID
*
* \param[in] handle Handle of the channel
* \param[in] deviceAddress Address of the I2C slave
* \param[out] deviceID Address of memory where the 3byte I2C device ID will be stored
* \return Returns status code of type FT_STATUS(see D2XX Programmer's Guide)
* \sa
* \note
* \warning
*/
FTDI_API FT_STATUS I2C_GetDeviceID(FT_HANDLE handle, uint8 deviceAddress,
uint8* deviceID)
{
FT_STATUS status=FT_OTHER_ERROR;
bool ack;
FN_ENTER;
#ifdef ENABLE_PARAMETER_CHECKING
CHECK_NULL_RET(handle);
CHECK_NULL_RET(deviceID);
if(deviceAddress>127)
{
DBG(MSG_WARN,"deviceAddress(0x%x) is greater than 127\n", \
(unsigned)deviceAddress);
status = FT_INVALID_PARAMETER;
return status;
}
#endif
status = I2C_Start(handle);
CHECK_STATUS(status);
status = I2C_Write8bitsAndGetAck(handle,(uint8)I2C_CMD_GETDEVICEID_RD,&ack);
CHECK_STATUS(status);
status = I2C_Write8bitsAndGetAck(handle,deviceAddress, &ack);
CHECK_STATUS(status);
status = I2C_Restart(handle);
CHECK_STATUS(status);
status = I2C_Write8bitsAndGetAck(handle,(uint8)I2C_CMD_GETDEVICEID_WR,&ack);
CHECK_STATUS(status);
status = I2C_Read8bitsAndGiveAck(handle,&(deviceID[0]),I2C_GIVE_ACK);
CHECK_STATUS(status);
status = I2C_Read8bitsAndGiveAck(handle,&(deviceID[1]),I2C_GIVE_ACK);
CHECK_STATUS(status);
/*NACK 3rd byte*/
status = I2C_Read8bitsAndGiveAck(handle,&(deviceID[2]),I2C_GIVE_NACK);
CHECK_STATUS(status);
FN_EXIT;
return status;
}
#endif
/******************************************************************************/
/* Local function definations */
/******************************************************************************/
#ifdef I2C_CMD_GETDEVICEID_SUPPORTED
/*!
* \brief Generate I2C bus restart condition
*
* This function generates the restart condition in the I2C bus
*
* \param[in] handle Handle of the channel
* \return Returns status code of type FT_STATUS(see D2XX Programmer's Guide)
* \sa
* \note
* \warning
*/
FT_STATUS I2C_Restart(FT_HANDLE handle)
{
FT_STATUS status;
uint8 buffer[3];
uint32 noOfBytesToTransfer;
uint32 noOfBytesTransferred;
I2C_Modes mode;
FN_ENTER;
#if 0
status = I2C_GetChannelConfig(handle,config);
if(FT_OK != status)
Infra_DbgPrintStatus(status);
CHECK_NULL_RET(config);
switch(config->ClockRate)
{
case I2C_CLOCK_STANDARD_MODE:
mode = I2C_STANDARD_MODE;
break;
case I2C_CLOCK_FAST_MODE:
mode = I2C_FAST_MODE;
break;
case I2C_CLOCK_FAST_MODE_PLUS:
mode = I2C_FAST_MODE_PLUS;
break;
case I2C_CLOCK_HIGH_SPEED_MODE:
mode = I2C_HIGH_SPEED_MODE;
break;
default:
mode = I2C_MAXIMUM_SUPPORTED_MODES;
}
#else
mode = I2C_STANDARD_MODE;
#endif
/*Restart condition SDA, SCL: 0,1->1,1->1,0->1,1->0,1*/
/*I2C_CONDITION_RESTART_1*/
noOfBytesToTransfer = 3;
noOfBytesTransferred = 0;
buffer[0] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;/* MPSSE command */
buffer[1] = VALUE_SCLHIGH_SDALOW; /* Value */
buffer[2] = DIRECTION_SCLOUT_SDAOUT; /* Direction */
status = FT_Channel_Write(I2C,handle,noOfBytesToTransfer,
buffer,&noOfBytesTransferred);
if( (FT_OK != status) && (noOfBytesToTransfer != noOfBytesTransferred) )
{
Infra_DbgPrintStatus(status);
DBG(MSG_ERR,"noOfBytesToTransfer=%d noOfBytesTransferred=%d\n",
(int)noOfBytesToTransfer,(int)noOfBytesTransferred);
}
Infra_Delay(I2C_Timings[mode][I2C_CONDITION_PRESTART]);
/*I2C_CONDITION_RESTART_2*/
noOfBytesToTransfer = 3;
noOfBytesTransferred = 0;
buffer[0] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;/* MPSSE command */
buffer[1] = VALUE_SCLHIGH_SDALOW;// _SDAHIGH; /* Value */ // should NOT be driving SDA high
buffer[2] = DIRECTION_SCLOUT_SDAIN;// _SDAOUT; /* Direction */ // Make this input instead to let line be pulled up
status = FT_Channel_Write(I2C,handle,noOfBytesToTransfer,
buffer,&noOfBytesTransferred);
if( (FT_OK != status) && (noOfBytesToTransfer != noOfBytesTransferred) )
{
Infra_DbgPrintStatus(status);
DBG(MSG_ERR,"noOfBytesToTransfer=%d noOfBytesTransferred=%d\n",
(int)noOfBytesToTransfer,(int)noOfBytesTransferred);
}
Infra_Delay(I2C_Timings[mode][I2C_CONDITION_PRESTART]);
/*I2C_CONDITION_RESTART_3*/
noOfBytesToTransfer = 3;
noOfBytesTransferred = 0;
buffer[0] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;/* MPSSE command */
buffer[1] = VALUE_SCLLOW_SDALOW;// _SDAHIGH; /* Value */ // should NOT be driving SDA high
buffer[2] = DIRECTION_SCLOUT_SDAIN;// _SDAOUT; /* Direction */ // Make this input instead to let line be pulled up
status = FT_Channel_Write(I2C,handle,noOfBytesToTransfer,
buffer,&noOfBytesTransferred);
if( (FT_OK != status) && (noOfBytesToTransfer != noOfBytesTransferred) )
{
Infra_DbgPrintStatus(status);
DBG(MSG_ERR,"noOfBytesToTransfer=%d noOfBytesTransferred=%d\n",
(int)noOfBytesToTransfer,(int)noOfBytesTransferred);
}
Infra_Delay(I2C_Timings[mode][I2C_CONDITION_PRESTART]);
/*I2C_CONDITION_RESTART_4*/
noOfBytesToTransfer = 3;
noOfBytesTransferred = 0;
buffer[0] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;/* MPSSE command */
buffer[1] = VALUE_SCLHIGH_SDALOW;// _SDAHIGH; /* Value */ // should NOT be driving SDA high
buffer[2] = DIRECTION_SCLOUT_SDAIN;// _SDAOUT; /* Direction */ // Make this input instead to let line be pulled up
status = FT_Channel_Write(I2C,handle,noOfBytesToTransfer,
buffer,&noOfBytesTransferred);
if( (FT_OK != status) && (noOfBytesToTransfer != noOfBytesTransferred) )
{
Infra_DbgPrintStatus(status);
DBG(MSG_ERR,"noOfBytesToTransfer=%d noOfBytesTransferred=%d\n",
(int)noOfBytesToTransfer,(int)noOfBytesTransferred);
}
Infra_Delay(I2C_Timings[mode][I2C_CONDITION_PRESTART]);
/*I2C_CONDITION_RESTART_5*/
noOfBytesToTransfer = 3;
noOfBytesTransferred = 0;
buffer[0] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;/* MPSSE command */
buffer[1] = VALUE_SCLLOW_SDALOW;// _SDAHIGH; /* Value */ // should NOT be driving SDA high
buffer[2] = DIRECTION_SCLOUT_SDAIN;// _SDAOUT; /* Direction */ // Make this input instead to let line be pulled up
status = FT_Channel_Write(I2C,handle,noOfBytesToTransfer,
buffer,&noOfBytesTransferred);
if( (FT_OK != status) && (noOfBytesToTransfer != noOfBytesTransferred) )
{
Infra_DbgPrintStatus(status);
DBG(MSG_ERR,"noOfBytesToTransfer=%d noOfBytesTransferred=%d\n",
(int)noOfBytesToTransfer,(int)noOfBytesTransferred);
}
Infra_Delay(I2C_Timings[mode][I2C_CONDITION_PRESTART]);
/*I2C_CONDITION_RESTART -tristate SCL & SDA */
noOfBytesToTransfer = 3;
noOfBytesTransferred = 0;
buffer[0] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;/* MPSSE command */
buffer[1] = VALUE_SCLLOW_SDALOW; /* Value(0x00=SCL low, SDA low) */
buffer[2] = DIRECTION_SCLIN_SDAIN; /* Direction */
status = FT_Channel_Write(I2C,handle,noOfBytesToTransfer,
buffer,&noOfBytesTransferred);
if( (FT_OK != status) && (noOfBytesToTransfer != noOfBytesTransferred) )
{
Infra_DbgPrintStatus(status);
DBG(MSG_ERR,"noOfBytesToTransfer=%d noOfBytesTransferred=%d\n",
(int)noOfBytesToTransfer,(int)noOfBytesTransferred);
}
Infra_Delay(I2C_Timings[mode][I2C_CONDITION_POSTSTOP]);
FN_EXIT;
return status;
}
#endif
/*!
* \brief Writes 8 bits and gets the ack bit
*
* This function writes 8 bits of data to the I2C bus and gets the ack bit from the device
*
* \param[in] handle Handle of the channel
* \param[in] data The 8bits of data that are to be written to the I2C bus
* \param[out] ack The acknowledgment bit returned by the I2C device
* \return Returns status code of type FT_STATUS(see D2XX Programmer's Guide)
* \sa
* \note
* \warning
*/
FT_STATUS I2C_Write8bitsAndGetAck(FT_HANDLE handle, uint8 data, bool *ack)
{
FT_STATUS status = FT_OTHER_ERROR;
uint8 buffer[20] = {0};
uint8 inBuffer[3] = {0};
uint32 noOfBytes = 0;
uint32 noOfBytesTransferred = 0;
FN_ENTER;
DBG(MSG_DEBUG,"----------Writing byte 0x%x \n",data);
/* Set direction */
buffer[noOfBytes++] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;
buffer[noOfBytes++] = VALUE_SCLLOW_SDALOW;
buffer[noOfBytes++] = DIRECTION_SCLOUT_SDAOUT;
/* Command to write 8 bits */
buffer[noOfBytes++]= MPSSE_CMD_DATA_OUT_BITS_NEG_EDGE;
buffer[noOfBytes++]= DATA_SIZE_8BITS;
buffer[noOfBytes++] = data;
/* Set SDA to input mode before reading ACK bit */
buffer[noOfBytes++] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;
buffer[noOfBytes++] = VALUE_SCLLOW_SDALOW;
buffer[noOfBytes++] = DIRECTION_SCLOUT_SDAIN;
/* Command to get ACK bit */
buffer[noOfBytes++] = MPSSE_CMD_DATA_IN_BITS_POS_EDGE;
buffer[noOfBytes++] = DATA_SIZE_1BIT;
/* Command MPSSE to send data to PC immediately */
buffer[noOfBytes++] = MPSSE_CMD_SEND_IMMEDIATE;
status = FT_Channel_Write(I2C, handle, noOfBytes, buffer, &noOfBytesTransferred);
if(FT_OK != status)
{
DBG(MSG_DEBUG, "FT_OK != status \n");
Infra_DbgPrintStatus(status);
}
else if(noOfBytes != noOfBytesTransferred)
{
DBG(MSG_ERR, "Requested to send %u bytes, no. of bytes sent is %u \
bytes",(unsigned)noOfBytes,(unsigned)noOfBytesTransferred);
status = FT_IO_ERROR;
}
else
{
noOfBytes=1;
noOfBytesTransferred=0;
#if 0
{
uint32 noOfBytesInQueue = 0;
do
{
status = Mid_GetQueueStatus(handle, &noOfBytesInQueue);
} while (noOfBytesInQueue < noOfBytes && status == FT_OK);
}
#else
INFRA_SLEEP(1);
#endif
status = FT_Channel_Read(I2C, handle, noOfBytes, inBuffer, &noOfBytesTransferred);
if(FT_OK != status)
{
Infra_DbgPrintStatus(status);
}
else if(noOfBytes != noOfBytesTransferred)
{
DBG(MSG_ERR, "Requested to send %u bytes, no. of bytes sent is %u \
bytes",(unsigned)noOfBytes,(unsigned)noOfBytesTransferred);
status = FT_IO_ERROR;
}
else
{
*ack = (bool)(inBuffer[0] & 0x01);
DBG(MSG_DEBUG," *ack = 0x%x\n", (unsigned)*ack);
}
}
FN_EXIT;
return status;
}
/*!
* \brief Reads 8 bits of data and sends ack bit
*
* This function reads 8 bits of data from the I2C bus and then writes an ack bit to the bus
*
* \param[in] handle Handle of the channel
* \param[in] *data Pointer to the buffer where the 8bits would be read to
* \param[in] ack Gives ack to device if set, otherwise gives nAck
* \return Returns status code of type FT_STATUS(see D2XX Programmer's Guide)
* \sa
* \note
* \warning
*/
FT_STATUS I2C_Read8bitsAndGiveAck(FT_HANDLE handle, uint8 *data, bool ack)
{
FT_STATUS status = FT_OTHER_ERROR;
uint8 buffer[20] = {0};
uint8 inBuffer[3] = {0};
uint32 noOfBytes = 0;
uint32 noOfBytesTransferred = 0;
FN_ENTER;
/* Set pin directions - SCL is output driven low, SDA is input (set high but does not matter) */
buffer[noOfBytes++] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;
buffer[noOfBytes++] = VALUE_SCLLOW_SDALOW;
buffer[noOfBytes++] = DIRECTION_SCLOUT_SDAIN;
/* Command to read 8 bits */
buffer[noOfBytes++] = MPSSE_CMD_DATA_IN_BITS_POS_EDGE;
buffer[noOfBytes++] = DATA_SIZE_8BITS;
/* Set directions to make SDA drive out. Pre-set state of SDA first though to avoid glitch */
if (ack)
{
/* We will drive the ACK bit to a '0' so pre-set pin to a '0' */
buffer[noOfBytes++] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;
buffer[noOfBytes++] = VALUE_SCLLOW_SDALOW;
buffer[noOfBytes++] = DIRECTION_SCLOUT_SDAOUT;
/* Clock out the ack bit as a '0' on negative edge */
buffer[noOfBytes++] = MPSSE_CMD_DATA_OUT_BITS_NEG_EDGE;
buffer[noOfBytes++] = DATA_SIZE_1BIT;
buffer[noOfBytes++] = SEND_ACK;
}
else
{
/* We will release the ACK bit to a '1' so pre-set pin to a '1' by making it an input */
buffer[noOfBytes++] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;
buffer[noOfBytes++] = VALUE_SCLLOW_SDALOW;
buffer[noOfBytes++] = DIRECTION_SCLOUT_SDAIN;
/* Clock out the ack bit as a '1' on negative edge - never actually seen on line since SDA is input but burns off one bit time */
buffer[noOfBytes++] = MPSSE_CMD_DATA_OUT_BITS_NEG_EDGE;
buffer[noOfBytes++] = DATA_SIZE_1BIT;
buffer[noOfBytes++] = SEND_NACK;
}
/* Back to Idle */
buffer[noOfBytes++] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;
buffer[noOfBytes++] = VALUE_SCLLOW_SDALOW;
buffer[noOfBytes++] = DIRECTION_SCLOUT_SDAIN;
/* Command MPSSE to send data to PC immediately */
buffer[noOfBytes++] = MPSSE_CMD_SEND_IMMEDIATE;
status = FT_Channel_Write(I2C, handle, noOfBytes, buffer, &noOfBytesTransferred);
if(FT_OK != status)
{
DBG(MSG_DEBUG, "FT_OK != status \n");
Infra_DbgPrintStatus(status);
}
else if(noOfBytes != noOfBytesTransferred)
{
DBG(MSG_ERR, "Requested to send %u bytes, no. of bytes sent is %u \
bytes",(unsigned)noOfBytes,(unsigned)noOfBytesTransferred);
status = FT_IO_ERROR;
}
else
{
noOfBytes=1;
noOfBytesTransferred=0;
#if 0
{
uint32 noOfBytesInQueue = 0;
do
{
status = Mid_GetQueueStatus(handle, &noOfBytesInQueue);
} while (noOfBytesInQueue < noOfBytes && status == FT_OK);
}
#else
INFRA_SLEEP(1);
#endif
status = FT_Channel_Read(I2C, handle, noOfBytes, inBuffer, &noOfBytesTransferred);
if(FT_OK != status)
{
Infra_DbgPrintStatus(status);
}
else if(noOfBytes != noOfBytesTransferred)
{
DBG(MSG_ERR, "Requested to read %u bytes, no. of bytes read is %u \
bytes",(unsigned)noOfBytes,(unsigned)noOfBytesTransferred);
status = FT_IO_ERROR;
}
else
{
*data = inBuffer[0];
DBG(MSG_DEBUG," *data = 0x%x\n", (unsigned)*data);
}
}
FN_EXIT;
return status;
}
/*!
* \brief This function generates the START, ADDRESS, DATA(write) & STOP phases in the I2C
* bus without having delays between these phases
*
* This function allocates memory locally, makes MPSSE command frames to write each data
* byte/bit, makes MPSSE command frames to read the acknowledgement bits, and then writes
* all these to the MPSSE in one shot. This function is useful where delays between START, DATA
* and STOP phases are not prefered.
*
* \param[in] handle Handle of the channel
* \param[in] deviceAddress Address of the I2C Slave. This parameter is ignored if flag
* I2C_TRANSFER_OPTIONS_NO_ADDRESS is set in the options parameter
* \param[in] sizeToTransfer Number of bytes or bits to be written, depending on if