I2C.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2018, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*!****************************************************************************
33  * @file I2C.h
34  * @brief Inter-Integrated Circuit driver interface.
35  *
36  * The I2C header file should be included in an application as follows:
37  * @code
38  * #include <ti/drivers/I2C.h>
39  * @endcode
40  *
41  * This module serves as the main interface for applications using an
42  * underlying I2C peripheral. Its purpose is to redirect the I2C APIs to
43  * device specific driver implementations which are specified using a pointer
44  * to a #I2C_FxnTable.
45  *
46  * # Overview #
47  *
48  * This section assumes that you have prior knowledge about the I2C protocol.
49  * For the full I2C-bus specification and user manual, view the \b UM10204
50  * document available online.
51  *
52  * This I2C driver is designed to operate as an I2C master and will not
53  * function as an I2C slave. Multi-master arbitration is not supported;
54  * therefore, this driver assumes it is the only I2C master on the bus.
55  * This I2C driver's API set provides the ability to transmit and receive
56  * data over an I2C bus between the I2C master and I2C slave(s). The
57  * application is responsible for manipulating and interpreting the data.
58  *
59  * # Thread Safety #
60  *
61  * This driver has been designed to operate with a Real-Time Operating System
62  * (RTOS). All I2C APIs are globally thread safe.
63  *
64  * # I2C Driver Configuration #
65  *
66  * In order to use the I2C APIs, the application is required to provide
67  * device-specific I2C configuration in the Board.c file. The I2C driver
68  * interface defines a configuration data structure, #I2C_Config.
69  *
70  * The application must declare an array of #I2C_Config elements, named
71  * \p I2C_config[]. Each element of \p I2C_config[] is populated with
72  * pointers to a device specific I2C driver implementation's function
73  * table, driver object, and hardware attributes. The hardware attributes
74  * define properties such as the I2C peripheral's base address and
75  * pins. Each element in \p I2C_config[] corresponds to an I2C instance,
76  * and none of the elements should have \p NULL pointers.
77  *
78  * The I2C configuration is device dependent. You will need to check the
79  * device specific I2C driver documentation. There you will find a
80  * description of the I2C hardware attributes.
81  *
82  * # Usage #
83  *
84  * For general usage, refer to the function documentation.
85  *
86  * ## Initializing the I2C Driver ##
87  *
88  * I2C_init() must be called before any other I2C API. This function
89  * calls the device specific implementation's I2C initialization function
90  * for each element of \p I2C_config[].
91  *
92  * ## Opening the I2C Driver ##
93  *
94  * After calling I2C_init(), the application can open an I2C instance by
95  * calling I2C_open(). This function takes an index into the \p I2C_config[]
96  * array and an #I2C_Params structure. The #I2C_Handle returned from the
97  * I2C_open() is then associated with that index into the \p I2C_config[]
98  * array. The following code example opens an I2C instance with default
99  * parameters by passing \p NULL for the #I2C_Params argument.
100  *
101  * @code
102  * I2C_Handle i2cHandle;
103  *
104  * i2cHandle = I2C_open(Board_I2C0, NULL);
105  *
106  * if (i2cHandle == NULL) {
107  * // Error opening I2C
108  * while (1) {}
109  * }
110  * @endcode
111  *
112  * \note Each I2C index can only be opened exclusively. Calling I2C_open()
113  * multiple times with the same index will result in an error. The index can
114  * be re-used if I2C_close() is called first.
115  *
116  * This example shows opening an I2C driver instance in #I2C_MODE_CALLBACK
117  * with a bit rate of #I2C_400kHz.
118  *
119  * @code
120  * void myCallbackFxn(I2C_Handle handle, I2C_Transaction *msg, bool status)
121  * {
122  * if (status == false) {
123  * //transfer failed
124  * }
125  * }
126  * @endcode
127  *
128  * @code
129  * I2C_Handle i2cHandle;
130  * I2C_Params i2cParams;
131  *
132  * I2C_Params_init(&i2cParams);
133  *
134  * i2cParams.transferMode = I2C_MODE_CALLBACK;
135  * i2cParams.transferCallbackFxn = myCallbackFxn;
136  * i2cParams.bitRate = I2C_400kHz;
137  *
138  * i2cHandle = I2C_open(Board_I2C0, &i2cParams);
139  *
140  * if (i2cHandle == NULL) {
141  * // Error opening I2C
142  * while (1);
143  * }
144  * @endcode
145  *
146  * ## Transferring data ##
147  *
148  * An I2C data transfer is performed using the I2C_transfer() function. Three
149  * types of transactions are supported: write, read, and write + read. The
150  * details of each transaction are specified with an #I2C_Transaction
151  * structure. Each transfer is completed before another transfer is initiated.
152  *
153  * For write + read transactions, the specified data is first written to the
154  * peripheral, then a repeated start is sent by the driver, which initiates
155  * the read operation. This type of transfer is useful if an I2C peripheral
156  * has a pointer register that needs to be adjusted prior to reading from
157  * the referenced data register.
158  *
159  * The following examples assume an I2C instance has been opened in
160  * #I2C_MODE_BLOCKING mode.
161  *
162  * ---------------------------------------------------------------------------
163  *
164  * Sending three bytes of data.
165  *
166  * @code
167  * I2C_Transaction i2cTransaction;
168  * uint8_t writeBuffer[3];
169  *
170  * writeBuffer[0] = 0xAB;
171  * writeBuffer[1] = 0xCD;
172  * writeBuffer[2] = 0xEF;
173  *
174  * i2cTransaction.slaveAddress = 0x50;
175  * i2cTransaction.writeBuf = writeBuffer;
176  * i2cTransaction.writeCount = 3;
177  * i2cTransaction.readBuf = NULL;
178  * i2cTransaction.readCount = 0;
179  *
180  * status = I2C_transfer(i2cHandle, &i2cTransaction);
181  *
182  * if (status == false) {
183  * // Unsuccessful I2C transfer
184  * }
185  * @endcode
186  *
187  * Reading five bytes of data.
188  *
189  * @code
190  * I2C_Transaction i2cTransaction;
191  * uint8_t readBuffer[5];
192  *
193  * i2cTransaction.slaveAddress = 0x50;
194  * i2cTransaction.writeBuf = NULL;
195  * i2cTransaction.writeCount = 0;
196  * i2cTransaction.readBuf = readBuffer;
197  * i2cTransaction.readCount = 5;
198  *
199  * status = I2C_transfer(i2cHandle, &i2cTransaction);
200  *
201  * if (status == false) {
202  * // Unsuccessful I2C transfer
203  * }
204  * @endcode
205  *
206  * Writing two bytes and reading four bytes in a single transaction.
207  *
208  * @code
209  * I2C_Transaction i2cTransaction;
210  * uint8_t readBuffer[4];
211  * uint8_t writeBuffer[2];
212  *
213  * writeBuffer[0] = 0xAB;
214  * writeBuffer[1] = 0xCD;
215  *
216  * i2cTransaction.slaveAddress = 0x50;
217  * i2cTransaction.writeBuf = writeBuffer;
218  * i2cTransaction.writeCount = 2;
219  * i2cTransaction.readBuf = readBuffer;
220  * i2cTransaction.readCount = 4;
221  *
222  * status = I2C_transfer(i2cHandle, &i2cTransaction);
223  *
224  * if (status == false) {
225  * // Unsuccessful I2C transfer
226  * }
227  * @endcode
228  *
229  * ---------------------------------------------------------------------------
230  *
231  * This final example shows usage of #I2C_MODE_CALLBACK, with queuing
232  * of multiple transactions. Because multiple transactions are simultaneously
233  * queued, separate #I2C_Transaction structures must be used. Each
234  * #I2C_Transaction will contain a custom application argument of a
235  * semaphore handle. The #I2C_Transaction.arg will point to the semaphore
236  * handle. When the callback function is called, the #I2C_Transaction.arg is
237  * checked for \p NULL. If this value is not \p NULL, then it can be assumed
238  * the \p arg is pointing to a valid semaphore handle. The semaphore handle
239  * is then used to call \p sem_post(). Hypothetically, this can be used to
240  * signal transaction completion to the task(s) that queued the
241  * transaction(s).
242  *
243  * @code
244  * void callbackFxn(I2C_Handle handle, I2C_Transaction *msg, bool status)
245  * {
246  *
247  * if (status == false) {
248  * //transaction failed
249  * }
250  *
251  * // Check for a semaphore handle
252  * if (msg->arg != NULL) {
253  *
254  * // Perform a semaphore post
255  * sem_post((sem_t *) (msg->arg));
256  * }
257  * }
258  * @endcode
259  *
260  * Snippets of the thread code that initiates the transactions are shown below.
261  * Note the use of multiple #I2C_Transaction structures. The handle of the
262  * semaphore to be posted is specified via \p i2cTransaction2.arg.
263  * I2C_transfer() is called three times to initiate each transaction.
264  * Since callback mode is used, these functions return immediately. After
265  * the transactions have been queued, other work can be done. Eventually,
266  * \p sem_wait() is called causing the thread to block until the transaction
267  * completes. When the transaction completes, the application's callback
268  * function, \p callbackFxn will be called. Once \p callbackFxn posts the
269  * semaphore, the thread will be unblocked and can resume execution.
270  *
271  * @code
272  * void thread(arg0, arg1)
273  * {
274  *
275  * I2C_Transaction i2cTransaction0;
276  * I2C_Transaction i2cTransaction1;
277  * I2C_Transaction i2cTransaction2;
278  *
279  * // ...
280  *
281  * i2cTransaction0.arg = NULL;
282  * i2cTransaction1.arg = NULL;
283  * i2cTransaction2.arg = semaphoreHandle;
284  *
285  * // ...
286  *
287  * I2C_transfer(i2c, &i2cTransaction0);
288  * I2C_transfer(i2c, &i2cTransaction1);
289  * I2C_transfer(i2c, &i2cTransaction2);
290  *
291  * // ...
292  *
293  * sem_wait(semaphoreHandle);
294  * }
295  * @endcode
296  *
297  * # Implementation #
298  *
299  * This top-level I2C module serves as the main interface for RTOS
300  * applications. Its purpose is to redirect the module's APIs to specific
301  * peripheral implementations which are specified using a pointer to an
302  * #I2C_FxnTable.
303  *
304  * The I2C driver interface module is joined (at link time) to an
305  * array of #I2C_Config data structures named \p I2C_config.
306  * \p I2C_config is typically defined in the Board.c file used for the
307  * application. If there are multiple instances of I2C peripherals on the
308  * device, there will typically be multiple #I2C_Config structures defined in
309  * the board file in the form of an array. Each entry in \p I2C_config
310  * contains a:
311  * - #I2C_FxnTable pointer to a set of functions that implement an I2C
312  * peripheral.
313  * - (\p void *) data object that is associated with the #I2C_FxnTable
314  * - (\p void *) hardware attributes that are associated to the #I2C_FxnTable
315  *
316  *
317  ******************************************************************************
318  */
319 
320 #ifndef ti_drivers_I2C__include
321 #define ti_drivers_I2C__include
322 
323 #ifdef __cplusplus
324 extern "C" {
325 #endif
326 
327 #include <stdbool.h>
328 #include <stddef.h>
329 #include <stdint.h>
330 
348 #define I2C_CMD_RESERVED (32)
349 
362 #define I2C_STATUS_RESERVED (-32)
363 
377 #define I2C_STATUS_SUCCESS (0)
378 
385 #define I2C_STATUS_ERROR (-1)
386 
394 #define I2C_STATUS_UNDEFINEDCMD (-2)
395 
405 /* Add I2C_CMD_<commands> here */
406 
414 typedef struct I2C_Config_ *I2C_Handle;
415 
431 typedef struct I2C_Transaction_ {
434  void *writeBuf;
437  size_t writeCount;
440  void *readBuf;
443  size_t readCount;
445  uint_least8_t slaveAddress;
449  void *arg;
452  void *nextPtr;
454 
460 typedef enum I2C_TransferMode_ {
482 
502 typedef void (*I2C_CallbackFxn)(I2C_Handle handle, I2C_Transaction *transaction,
503  bool transferStatus);
504 
512 typedef enum I2C_BitRate_ {
513 
519 } I2C_BitRate;
520 
534 typedef struct I2C_Params_ {
536  I2C_TransferMode transferMode;
543  void *custom;
544 } I2C_Params;
545 
550 typedef void (*I2C_CancelFxn) (I2C_Handle handle);
551 
556 typedef void (*I2C_CloseFxn) (I2C_Handle handle);
557 
562 typedef int_fast16_t (*I2C_ControlFxn) (I2C_Handle handle, uint_fast16_t cmd,
563  void *controlArg);
564 
569 typedef void (*I2C_InitFxn) (I2C_Handle handle);
570 
575 typedef I2C_Handle (*I2C_OpenFxn) (I2C_Handle handle, I2C_Params *params);
576 
581 typedef bool (*I2C_TransferFxn) (I2C_Handle handle,
582  I2C_Transaction *transaction);
583 
589 typedef struct I2C_FxnTable_ {
596 } I2C_FxnTable;
597 
609 typedef struct I2C_Config_ {
612 
614  void *object;
615 
617  void const *hwAttrs;
618 } I2C_Config;
619 
640 extern void I2C_cancel(I2C_Handle handle);
641 
651 extern void I2C_close(I2C_Handle handle);
652 
690 extern int_fast16_t I2C_control(I2C_Handle handle, uint_fast16_t cmd,
691  void *controlArg);
692 
701 extern void I2C_init(void);
702 
720 extern I2C_Handle I2C_open(uint_least8_t index, I2C_Params *params);
721 
734 extern void I2C_Params_init(I2C_Params *params);
735 
781 extern bool I2C_transfer(I2C_Handle handle, I2C_Transaction *transaction);
782 
783 #ifdef __cplusplus
784 }
785 #endif
786 
787 #endif /* ti_drivers_I2C__include */
I2C Parameters.
Definition: I2C.h:534
size_t readCount
Definition: I2C.h:443
Structure used to perform I2C bus transfers.
Definition: I2C.h:431
I2C_BitRate_
Specifies the standard I2C bus bit rate.
Definition: I2C.h:512
void * object
Definition: I2C.h:614
I2C_OpenFxn openFxn
Definition: I2C.h:594
void I2C_Params_init(I2C_Params *params)
Initialize an I2C_Params structure to its default values.
void(* I2C_CloseFxn)(I2C_Handle handle)
A function pointer to a driver-specific implementation of I2C_close().
Definition: I2C.h:556
void * nextPtr
Definition: I2C.h:452
I2C_TransferMode transferMode
Definition: I2C.h:536
void(* I2C_InitFxn)(I2C_Handle handle)
A function pointer to a driver-specific implementation of I2C_init().
Definition: I2C.h:569
struct I2C_Transaction_ I2C_Transaction
Structure used to perform I2C bus transfers.
struct I2C_Config_ * I2C_Handle
A handle that is returned from an I2C_open() call.
Definition: I2C.h:414
size_t writeCount
Definition: I2C.h:437
void const * hwAttrs
Definition: I2C.h:617
int_fast16_t I2C_control(I2C_Handle handle, uint_fast16_t cmd, void *controlArg)
Perform implementation-specific features on a given I2C_Handle.
Definition: I2C.h:514
I2C_CloseFxn closeFxn
Definition: I2C.h:591
void(* I2C_CancelFxn)(I2C_Handle handle)
A function pointer to a driver-specific implementation of I2C_cancel().
Definition: I2C.h:550
struct I2C_Params_ I2C_Params
I2C Parameters.
void * readBuf
Definition: I2C.h:440
I2C_InitFxn initFxn
Definition: I2C.h:593
I2C_CancelFxn cancelFxn
Definition: I2C.h:590
I2C_TransferMode_
Specifies the behavior of I2C_Transfer().
Definition: I2C.h:460
Definition: I2C.h:518
enum I2C_TransferMode_ I2C_TransferMode
Specifies the behavior of I2C_Transfer().
I2C_CallbackFxn transferCallbackFxn
Definition: I2C.h:539
bool(* I2C_TransferFxn)(I2C_Handle handle, I2C_Transaction *transaction)
A function pointer to a driver-specific implementation of I2C_transfer().
Definition: I2C.h:581
I2C_Handle I2C_open(uint_least8_t index, I2C_Params *params)
Open an I2C instance.
Definition: I2C.h:517
Definition: I2C.h:516
The definition of an I2C function table that contains the required set of functions to control a spec...
Definition: I2C.h:589
struct I2C_FxnTable_ I2C_FxnTable
The definition of an I2C function table that contains the required set of functions to control a spec...
enum I2C_BitRate_ I2C_BitRate
Specifies the standard I2C bus bit rate.
void I2C_close(I2C_Handle handle)
Close an I2C driver instance specified by an I2C_Handle.
I2C_BitRate bitRate
Definition: I2C.h:541
Definition: I2C.h:480
void * writeBuf
Definition: I2C.h:434
struct I2C_Config_ I2C_Config
I2C global configuration.
Definition: I2C.h:515
void I2C_cancel(I2C_Handle handle)
Cancels all I2C transfers.
void * arg
Definition: I2C.h:449
uint_least8_t slaveAddress
Definition: I2C.h:445
I2C global configuration.
Definition: I2C.h:609
void(* I2C_CallbackFxn)(I2C_Handle handle, I2C_Transaction *transaction, bool transferStatus)
I2C callback function prototype.
Definition: I2C.h:502
void I2C_init(void)
Initializes the I2C module.
I2C_FxnTable const * fxnTablePtr
Definition: I2C.h:611
void * custom
Definition: I2C.h:543
bool I2C_transfer(I2C_Handle handle, I2C_Transaction *transaction)
Perform an I2C transaction with an I2C slave peripheral.
int_fast16_t(* I2C_ControlFxn)(I2C_Handle handle, uint_fast16_t cmd, void *controlArg)
A function pointer to a driver-specific implementation of I2C_control().
Definition: I2C.h:562
I2C_ControlFxn controlFxn
Definition: I2C.h:592
I2C_Handle(* I2C_OpenFxn)(I2C_Handle handle, I2C_Params *params)
A function pointer to a driver-specific implementation of I2C_open().
Definition: I2C.h:575
I2C_TransferFxn transferFxn
Definition: I2C.h:595
Definition: I2C.h:467
© Copyright 1995-2018, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale