I2C.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2019, 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 (I2C) Driver
35  *
36  * @anchor ti_drivers_I2C_Overview
37  * # Overview
38  *
39  * The I2C driver is designed to operate as an I2C master and will not
40  * function as an I2C slave. Multi-master arbitration is not supported;
41  * therefore, this driver assumes it is the only I2C master on the bus.
42  * This I2C driver's API set provides the ability to transmit and receive
43  * data over an I2C bus between the I2C master and I2C slave(s). The
44  * application is responsible for manipulating and interpreting the data.
45  *
46  *
47  * <hr>
48  * @anchor ti_drivers_I2C_Usage
49  * # Usage
50  *
51  * This section provides a basic @ref ti_drivers_I2C_Synopsis
52  * "usage summary" and a set of @ref ti_drivers_I2C_Examples "examples"
53  * in the form of commented code fragments. Detailed descriptions of the
54  * I2C APIs and their effect are provided in subsequent sections.
55  *
56  * @anchor ti_drivers_I2C_Synopsis
57  * ## Synopsis #
58  * @anchor ti_drivers_I2C_Synopsis_Code
59  * @code
60  * // Import I2C Driver definitions
61  * #include <ti/drivers/I2C.h>
62  *
63  * // Define name for an index of an I2C bus
64  * #define SENSORS 0
65  *
66  * // Define the slave address of device on the SENSORS bus
67  * #define OPT_ADDR 0x47
68  *
69  * // One-time init of I2C driver
70  * I2C_init();
71  *
72  * // initialize optional I2C bus parameters
73  * I2C_Params params;
74  * I2C_Params_init(&params);
75  * params.bitRate = I2C_400kHz;
76  *
77  * // Open I2C bus for usage
78  * I2C_Handle i2cHandle = I2C_open(SENSORS, &params);
79  *
80  * // Initialize slave address of transaction
81  * I2C_Transaction transaction = {0};
82  * transaction.slaveAddress = OPT_ADDR;
83  *
84  * // Read from I2C slave device
85  * transaction.readBuf = data;
86  * transaction.readCount = sizeof(data);
87  * transaction.writeCount = 0;
88  * I2C_transfer(i2cHandle, &transaction);
89  *
90  * // Write to I2C slave device
91  * transaction.writeBuf = command;
92  * transaction.writeCount = sizeof(command);
93  * transaction.readCount = 0;
94  * I2C_transfer(i2cHandle, &transaction);
95  *
96  * // Close I2C
97  * I2C_close(i2cHandle);
98  * @endcode
99  *
100  * @anchor ti_drivers_I2C_Examples
101  * ## Examples
102  *
103  * @li @ref ti_drivers_I2C_Example_open "Getting an I2C bus handle"
104  * @li @ref ti_drivers_I2C_Example_write3bytes "Sending 3 bytes"
105  * @li @ref ti_drivers_I2C_Example_read5bytes "Reading 5 bytes"
106  * @li @ref ti_drivers_I2C_Example_writeread "Writing then reading in a single transaction"
107  * @li @ref ti_drivers_I2C_Example_callback "Using Callback mode"
108  *
109  * @anchor ti_drivers_I2C_Example_open
110  * ## Opening the I2C Driver
111  *
112  * After calling I2C_init(), the application can open an I2C instance by
113  * calling I2C_open().The following code example opens an I2C instance with
114  * default parameters by passing @p NULL for the #I2C_Params argument.
115  *
116  * @code
117  * I2C_Handle i2cHandle;
118  *
119  * i2cHandle = I2C_open(0, NULL);
120  *
121  * if (i2cHandle == NULL) {
122  * // Error opening I2C
123  * while (1) {}
124  * }
125  * @endcode
126  *
127  * @anchor ti_drivers_I2C_Example_write3bytes
128  * ## Sending three bytes of data.
129  *
130  * @code
131  * I2C_Transaction i2cTransaction = {0};
132  * uint8_t writeBuffer[3];
133  *
134  * writeBuffer[0] = 0xAB;
135  * writeBuffer[1] = 0xCD;
136  * writeBuffer[2] = 0xEF;
137  *
138  * i2cTransaction.slaveAddress = 0x50;
139  * i2cTransaction.writeBuf = writeBuffer;
140  * i2cTransaction.writeCount = 3;
141  * i2cTransaction.readBuf = NULL;
142  * i2cTransaction.readCount = 0;
143  *
144  * status = I2C_transfer(i2cHandle, &i2cTransaction);
145  *
146  * if (status == false) {
147  * // Unsuccessful I2C transfer
148  * }
149  * @endcode
150  *
151  * @anchor ti_drivers_I2C_Example_read5bytes
152  * ## Reading five bytes of data.
153  *
154  * @code
155  * I2C_Transaction i2cTransaction = {0};
156  * uint8_t readBuffer[5];
157  *
158  * i2cTransaction.slaveAddress = 0x50;
159  * i2cTransaction.writeBuf = NULL;
160  * i2cTransaction.writeCount = 0;
161  * i2cTransaction.readBuf = readBuffer;
162  * i2cTransaction.readCount = 5;
163  *
164  * status = I2C_transfer(i2cHandle, &i2cTransaction);
165  *
166  * if (status == false) {
167  * // Unsuccessful I2C transfer
168  * }
169  * @endcode
170  *
171  * @anchor ti_drivers_I2C_Example_writeread
172  * ## Writing two bytes and reading four bytes in a single transaction.
173  *
174  * @code
175  * I2C_Transaction i2cTransaction = {0};
176  * uint8_t readBuffer[4];
177  * uint8_t writeBuffer[2];
178  *
179  * writeBuffer[0] = 0xAB;
180  * writeBuffer[1] = 0xCD;
181  *
182  * i2cTransaction.slaveAddress = 0x50;
183  * i2cTransaction.writeBuf = writeBuffer;
184  * i2cTransaction.writeCount = 2;
185  * i2cTransaction.readBuf = readBuffer;
186  * i2cTransaction.readCount = 4;
187  *
188  * status = I2C_transfer(i2cHandle, &i2cTransaction);
189  *
190  * if (status == false) {
191  * // Unsuccessful I2C transfer
192  * }
193  * @endcode
194  *
195  * @anchor ti_drivers_I2C_Example_callback
196  * ## Using callback mode
197  * This final example shows usage of #I2C_MODE_CALLBACK, with queuing
198  * of multiple transactions. Because multiple transactions are simultaneously
199  * queued, separate #I2C_Transaction structures must be used. Each
200  * #I2C_Transaction will contain a custom application argument of a
201  * semaphore handle. The #I2C_Transaction.arg will point to the semaphore
202  * handle. When the callback function is called, the #I2C_Transaction.arg is
203  * checked for @p NULL. If this value is not @p NULL, then it can be assumed
204  * the @p arg is pointing to a valid semaphore handle. The semaphore handle
205  * is then used to call @p sem_post(). Hypothetically, this can be used to
206  * signal transaction completion to the task(s) that queued the
207  * transaction(s).
208  *
209  * @code
210  * void callbackFxn(I2C_Handle handle, I2C_Transaction *msg, bool status)
211  * {
212  *
213  * if (status == false) {
214  * //transaction failed
215  * }
216  *
217  * // Check for a semaphore handle
218  * if (msg->arg != NULL) {
219  *
220  * // Perform a semaphore post
221  * sem_post((sem_t *) (msg->arg));
222  * }
223  * }
224  * @endcode
225  *
226  * Snippets of the thread code that initiates the transactions are shown below.
227  * Note the use of multiple #I2C_Transaction structures. The handle of the
228  * semaphore to be posted is specified via @p i2cTransaction2.arg.
229  * I2C_transfer() is called three times to initiate each transaction.
230  * Since callback mode is used, these functions return immediately. After
231  * the transactions have been queued, other work can be done. Eventually,
232  * @p sem_wait() is called causing the thread to block until the transaction
233  * completes. When the transaction completes, the application's callback
234  * function, @p callbackFxn will be called. Once #I2C_CallbackFxn posts the
235  * semaphore, the thread will be unblocked and can resume execution.
236  *
237  * @code
238  * void thread(arg0, arg1)
239  * {
240  *
241  * I2C_Transaction i2cTransaction0 = {0};
242  * I2C_Transaction i2cTransaction1 = {0};
243  * I2C_Transaction i2cTransaction2 = {0};
244  *
245  * // ...
246  *
247  * i2cTransaction0.arg = NULL;
248  * i2cTransaction1.arg = NULL;
249  * i2cTransaction2.arg = semaphoreHandle;
250  *
251  * // ...
252  *
253  * I2C_transfer(i2c, &i2cTransaction0);
254  * I2C_transfer(i2c, &i2cTransaction1);
255  * I2C_transfer(i2c, &i2cTransaction2);
256  *
257  * // ...
258  *
259  * sem_wait(semaphoreHandle);
260  * }
261  * @endcode
262  *
263  * <hr>
264  * @anchor ti_drivers_I2C_Configuration
265  * # Configuration
266  *
267  * Refer to the @ref driver_configuration "Driver's Configuration" section
268  * for driver configuration information.
269  * <hr>
270  ******************************************************************************
271  */
272 
273 #ifndef ti_drivers_I2C__include
274 #define ti_drivers_I2C__include
275 
277 #include <stdbool.h>
278 #include <stddef.h>
279 #include <stdint.h>
282 #ifdef __cplusplus
283 extern "C" {
284 #endif
285 
305 #define I2C_CMD_RESERVED (32)
306 
320 #define I2C_STATUS_RESERVED (-32)
321 
333 #define I2C_STATUS_SUCCESS (0)
334 
338 #define I2C_STATUS_ERROR (-1)
339 
343  #define I2C_STATUS_TIMEOUT (-2)
344 
349 #define I2C_STATUS_UNDEFINEDCMD (-2)
350 
360 /* Add I2C_CMD_<commands> here */
361 
369 #define I2C_WAIT_FOREVER (~(0U))
370 
374 typedef struct I2C_Config_ *I2C_Handle;
375 
381 typedef struct {
386  void *writeBuf;
387 
402  size_t writeCount;
403 
408  void *readBuf;
409 
422  size_t readCount;
423 
430  uint_least8_t slaveAddress;
431 
442  void *arg;
443 
448  void *nextPtr;
450 
458 typedef enum {
471 
486 
508 typedef void (*I2C_CallbackFxn)(I2C_Handle handle, I2C_Transaction *transaction,
509  bool transferStatus);
510 
519 typedef enum {
525 } I2C_BitRate;
526 
535 typedef struct {
537  I2C_TransferMode transferMode;
538 
544 
550 
552  void *custom;
553 } I2C_Params;
554 
560 typedef void (*I2C_CancelFxn) (I2C_Handle handle);
561 
567 typedef void (*I2C_CloseFxn) (I2C_Handle handle);
568 
574 typedef int_fast16_t (*I2C_ControlFxn) (I2C_Handle handle, uint_fast16_t cmd,
575  void *controlArg);
576 
582 typedef void (*I2C_InitFxn) (I2C_Handle handle);
583 
589 typedef I2C_Handle (*I2C_OpenFxn) (I2C_Handle handle, I2C_Params *params);
590 
596 typedef int_fast16_t (*I2C_TransferFxn) (I2C_Handle handle,
597  I2C_Transaction *transaction, uint32_t timeout);
598 
604 typedef struct {
605  I2C_CancelFxn cancelFxn;
606  I2C_CloseFxn closeFxn;
607  I2C_ControlFxn controlFxn;
608  I2C_InitFxn initFxn;
609  I2C_OpenFxn openFxn;
610  I2C_TransferFxn transferFxn;
611 } I2C_FxnTable;
612 
620 typedef struct I2C_Config_ {
624 
626  void *object;
627 
630  void const *hwAttrs;
631 } I2C_Config;
632 
654 extern void I2C_cancel(I2C_Handle handle);
655 
663 extern void I2C_close(I2C_Handle handle);
664 
686 extern int_fast16_t I2C_control(I2C_Handle handle, uint_fast16_t cmd,
687  void *controlArg);
688 
694 extern void I2C_init(void);
695 
711 extern I2C_Handle I2C_open(uint_least8_t index, I2C_Params *params);
712 
725 extern void I2C_Params_init(I2C_Params *params);
726 
758 extern bool I2C_transfer(I2C_Handle handle, I2C_Transaction *transaction);
759 
800 extern int_fast16_t I2C_transferTimeout(I2C_Handle handle, I2C_Transaction *transaction, uint32_t timeout);
801 
802 #ifdef __cplusplus
803 }
804 #endif
805 
806 #endif /* ti_drivers_I2C__include */
void * writeBuf
Definition: I2C.h:386
void * arg
Definition: I2C.h:442
I2C_TransferMode transferMode
Definition: I2C.h:537
void * object
Definition: I2C.h:626
void * readBuf
Definition: I2C.h:408
void I2C_Params_init(I2C_Params *params)
Initialize an I2C_Params structure to its default values.
I2C_BitRate
Bit rate for an I2C driver instance specified in the I2C_Params.
Definition: I2C.h:519
struct I2C_Config_ * I2C_Handle
A handle that is returned from an I2C_open() call.
Definition: I2C.h:374
void const * hwAttrs
Definition: I2C.h:630
int_fast16_t I2C_control(I2C_Handle handle, uint_fast16_t cmd, void *controlArg)
Function performs implementation specific features on a driver instance.
Definition: I2C.h:470
I2C_TransferMode
Return behavior of I2C_Transfer() specified in the I2C_Params.
Definition: I2C.h:458
I2C_CallbackFxn transferCallbackFxn
Definition: I2C.h:543
I2C_Handle I2C_open(uint_least8_t index, I2C_Params *params)
Open an I2C driver instance.
The definition of an I2C function table that contains the required set of functions to control a spec...
Definition: I2C.h:604
Definition: I2C.h:523
Definition: I2C.h:521
I2C_BitRate bitRate
Definition: I2C.h:549
void I2C_close(I2C_Handle handle)
Function to close an I2C driver instance.
I2C parameters used with I2C_open().
Definition: I2C.h:535
struct I2C_Config_ I2C_Config
I2C driver&#39;s custom configuration structure.
Definition: I2C.h:524
uint_least8_t slaveAddress
Definition: I2C.h:430
Definition: I2C.h:522
void I2C_cancel(I2C_Handle handle)
Cancels all I2C transfers.
void * custom
Definition: I2C.h:552
I2C driver&#39;s custom configuration structure.
Definition: I2C.h:620
void(* I2C_CallbackFxn)(I2C_Handle handle, I2C_Transaction *transaction, bool transferStatus)
The definition of a callback function.
Definition: I2C.h:508
Definition: I2C.h:484
void I2C_init(void)
Function to initialize the I2C driver.
I2C_FxnTable const * fxnTablePtr
Definition: I2C.h:623
Defines a transaction to be used with I2C_transfer()
Definition: I2C.h:381
bool I2C_transfer(I2C_Handle handle, I2C_Transaction *transaction)
Perform an I2C transaction with an I2C slave peripheral.
size_t writeCount
Definition: I2C.h:402
Definition: I2C.h:520
size_t readCount
Definition: I2C.h:422
int_fast16_t I2C_transferTimeout(I2C_Handle handle, I2C_Transaction *transaction, uint32_t timeout)
Perform an I2C transaction with an I2C slave peripheral.
© Copyright 1995-2019, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale