MSPM0G1X0X_G3X0X TI-Driver Library  1.20.01.06
I2C.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2023, 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  *
35  * @brief I2C Driver
36  *
37  * @defgroup I2C Inter-Integrated Circuit (I2C)
38  *
39  * The I2C header file should be included in an application as follows:
40  * @code
41  * #include <ti/drivers/I2C.h>
42  * @endcode
43  *
44  * @anchor ti_drivers_I2C_Overview
45  * # Overview
46  *
47  * The I2C driver is designed to operate as an I2C controller and will not
48  * function as an I2C target. Multi-controller arbitration is not supported;
49  * therefore, this driver assumes it is the only I2C controller on the bus.
50  * This I2C driver's API set provides the ability to transmit and receive
51  * data over an I2C bus between the I2C controller and I2C target(s). The
52  * application is responsible for manipulating and interpreting the data.
53  *
54  *
55  * <hr>
56  * @anchor ti_drivers_I2C_Usage
57  * # Usage
58  *
59  * This section provides a basic @ref ti_drivers_I2C_Synopsis
60  * "usage summary" and a set of @ref ti_drivers_I2C_Examples "examples"
61  * in the form of commented code fragments. Detailed descriptions of the
62  * I2C APIs and their effect are provided in subsequent sections.
63  *
64  * @anchor ti_drivers_I2C_Synopsis
65  * ## Synopsis #
66  * @anchor ti_drivers_I2C_Synopsis_Code
67  * @code
68  * // Import I2C Driver definitions.
69  * #include <ti/drivers/I2C.h>
70  *
71  * // Define name for an index of an I2C bus.
72  * #define SENSORS 0
73  *
74  * // Define the target address of device on the SENSORS bus.
75  * #define OPT_ADDR 0x48
76  *
77  * // One-time init of I2C driver.
78  * I2C_init();
79  *
80  * // initialize optional I2C bus parameters.
81  * I2C_Params params;
82  * I2C_Params_init(&params);
83  * params.bitRate = I2C_100kHz;
84  *
85  * // Open I2C bus for usage.
86  * I2C_Handle i2cHandle = I2C_open(SENSORS, &params);
87  *
88  * // Initialize target address of transaction.
89  * I2C_Transaction transaction = {0};
90  * transaction.targetAddress = OPT_ADDR;
91  *
92  * // Read from I2C target device.
93  * transaction.readBuf = data;
94  * transaction.readCount = sizeof(data);
95  * transaction.writeCount = 0;
96  * I2C_transfer(i2cHandle, &transaction);
97  *
98  * // Write to I2C target device.
99  * transaction.writeBuf = command;
100  * transaction.writeCount = sizeof(command);
101  * transaction.readCount = 0;
102  * I2C_transferTimeout(i2cHandle, &transaction, 5000);
103  *
104  * // Close I2C.
105  * I2C_close(i2cHandle);
106  * @endcode
107  *
108  * @anchor ti_drivers_I2C_Examples
109  * ## Examples
110  *
111  * @li @ref ti_drivers_I2C_Example_open "Getting an I2C bus handle"
112  * @li @ref ti_drivers_I2C_Example_write3bytes "Sending 3 bytes"
113  * @li @ref ti_drivers_I2C_Example_read5bytes "Reading 5 bytes"
114  * @li @ref ti_drivers_I2C_Example_writeread "Writing then reading in a single transaction"
115  * @li @ref ti_drivers_I2C_Example_callback "Using Callback mode"
116  *
117  * @anchor ti_drivers_I2C_Example_open
118  * ## Opening the I2C Driver
119  *
120  * After calling I2C_init(), the application can open an I2C instance by
121  * calling I2C_open().The following code example opens an I2C instance with
122  * default parameters by passing @p NULL for the #I2C_Params argument.
123  *
124  * @code
125  * I2C_Handle i2cHandle;
126  *
127  * i2cHandle = I2C_open(0, NULL);
128  *
129  * if (i2cHandle == NULL) {
130  * // Error opening I2C
131  * while (1) {}
132  * }
133  * @endcode
134  *
135  * @anchor ti_drivers_I2C_Example_write3bytes
136  * ## Sending three bytes of data.
137  *
138  * @code
139  * I2C_Transaction i2cTransaction = {0};
140  * uint8_t writeBuffer[3];
141  *
142  * writeBuffer[0] = 0xAB;
143  * writeBuffer[1] = 0xCD;
144  * writeBuffer[2] = 0xEF;
145  *
146  * i2cTransaction.targetAddress = 0x50;
147  * i2cTransaction.writeBuf = writeBuffer;
148  * i2cTransaction.writeCount = 3;
149  * i2cTransaction.readBuf = NULL;
150  * i2cTransaction.readCount = 0;
151  *
152  * status = I2C_transfer(i2cHandle, &i2cTransaction);
153  *
154  * if (status == false) {
155  * // Unsuccessful I2C transfer
156  * if (i2cTransaction.status == I2C_STATUS_ADDR_NACK) {
157  * // I2C target address not acknowledged
158  * }
159  * }
160  * @endcode
161  *
162  * @anchor ti_drivers_I2C_Example_read5bytes
163  * ## Reading five bytes of data.
164  *
165  * @code
166  * I2C_Transaction i2cTransaction = {0};
167  * uint8_t readBuffer[5];
168  *
169  * i2cTransaction.targetAddress = 0x50;
170  * i2cTransaction.writeBuf = NULL;
171  * i2cTransaction.writeCount = 0;
172  * i2cTransaction.readBuf = readBuffer;
173  * i2cTransaction.readCount = 5;
174  *
175  * status = I2C_transfer(i2cHandle, &i2cTransaction);
176  *
177  * if (status == false) {
178  * if (i2cTransaction.status == I2C_STATUS_ADDR_NACK) {
179  * // I2C target address not acknowledged
180  * }
181  * }
182  * @endcode
183  *
184  * @anchor ti_drivers_I2C_Example_writeread
185  * ## Writing two bytes and reading four bytes in a single transaction.
186  *
187  * @code
188  * I2C_Transaction i2cTransaction = {0};
189  * uint8_t readBuffer[4];
190  * uint8_t writeBuffer[2];
191  *
192  * writeBuffer[0] = 0xAB;
193  * writeBuffer[1] = 0xCD;
194  *
195  * i2cTransaction.targetAddress = 0x50;
196  * i2cTransaction.writeBuf = writeBuffer;
197  * i2cTransaction.writeCount = 2;
198  * i2cTransaction.readBuf = readBuffer;
199  * i2cTransaction.readCount = 4;
200  *
201  * status = I2C_transfer(i2cHandle, &i2cTransaction);
202  *
203  * if (status == false) {
204  * if (i2cTransaction->status == I2C_STATUS_ADDR_NACK) {
205  * // target address not acknowledged
206  * }
207  * }
208  * @endcode
209  *
210  * @anchor ti_drivers_I2C_Example_callback
211  * ## Using callback mode
212  * This final example shows usage of #I2C_MODE_CALLBACK, with queuing
213  * of multiple transactions. Because multiple transactions are simultaneously
214  * queued, separate #I2C_Transaction structures must be used. Each
215  * #I2C_Transaction will contain a custom application argument of a
216  * semaphore handle. The #I2C_Transaction.arg will point to the semaphore
217  * handle. When the callback function is called, the #I2C_Transaction.arg is
218  * checked for @p NULL. If this value is not @p NULL, then it can be assumed
219  * the @p arg is pointing to a valid semaphore handle. The semaphore handle
220  * is then used to call @p sem_post(). Hypothetically, this can be used to
221  * signal transaction completion to the task(s) that queued the
222  * transaction(s).
223  *
224  * @code
225  * void callbackFxn(I2C_Handle handle, I2C_Transaction *msg, bool status)
226  * {
227  * // if transaction failed.
228  * if (status == false) {
229  * if (msg->status == I2C_STATUS_ADDR_NACK) {
230  * // target address not acknowledged
231  * }
232  * else if (msg->status == I2C_STATUS_CANCEL) {
233  * // transaction canceled by I2C_cancel()
234  * }
235  * }
236  *
237  * // Check for a custom argument.
238  * if (msg->arg != NULL) {
239  *
240  * // In this example, the custom argument is a semaphore handle.
241  * // Perform a semaphore post.
242  * sem_post((sem_t *) (msg->arg));
243  * }
244  * }
245  * @endcode
246  *
247  * Snippets of the thread code that initiates the transactions are shown below.
248  * Note the use of multiple #I2C_Transaction structures. The handle of the
249  * semaphore to be posted is specified via @p i2cTransaction2.arg.
250  * I2C_transfer() is called three times to initiate each transaction.
251  * Since callback mode is used, these functions return immediately. After
252  * the transactions have been queued, other work can be done. Eventually,
253  * @p sem_wait() is called causing the thread to block until the transaction
254  * completes. When the transaction completes, the application's callback
255  * function, @p callbackFxn will be called. Once #I2C_CallbackFxn posts the
256  * semaphore, the thread will be unblocked and can resume execution.
257  *
258  * @code
259  * void thread(arg0, arg1)
260  * {
261  *
262  * I2C_Transaction i2cTransaction0 = {0};
263  * I2C_Transaction i2cTransaction1 = {0};
264  * I2C_Transaction i2cTransaction2 = {0};
265  *
266  * // ...
267  *
268  * i2cTransaction0.arg = NULL;
269  * i2cTransaction1.arg = NULL;
270  * i2cTransaction2.arg = semaphoreHandle;
271  *
272  * // ...
273  *
274  * I2C_transfer(i2c, &i2cTransaction0);
275  * I2C_transfer(i2c, &i2cTransaction1);
276  * I2C_transfer(i2c, &i2cTransaction2);
277  *
278  * // ...
279  *
280  * sem_wait(semaphoreHandle);
281  * }
282  * @endcode
283  *
284  * <hr>
285  ******************************************************************************
286  */
290 #ifndef ti_drivers_I2C__include
291 #define ti_drivers_I2C__include
292 
294 #include <stdbool.h>
295 #include <stddef.h>
296 #include <stdint.h>
297 #include <ti/devices/msp/msp.h>
298 #include <ti/drivers/dpl/HwiP.h>
299 #include <ti/drivers/dpl/SemaphoreP.h>
300 
303 #ifdef __cplusplus
304 extern "C" {
305 #endif
306 
327 #define I2C_STATUS_RESERVED (-32)
328 
333 #define I2C_STATUS_QUEUED (1)
334 
338 #define I2C_STATUS_SUCCESS (0)
339 
343 #define I2C_STATUS_ERROR (-1)
344 
349 #define I2C_STATUS_UNDEFINEDCMD (-2)
350 
354 #define I2C_STATUS_TIMEOUT (-3)
355 
359 #define I2C_STATUS_CLOCK_TIMEOUT (-4)
360 
364 #define I2C_STATUS_ADDR_NACK (-5)
365 
369 #define I2C_STATUS_DATA_NACK (-6)
370 
374 #define I2C_STATUS_ARB_LOST (-7)
375 
379 #define I2C_STATUS_INCOMPLETE (-8)
380 
385 #define I2C_STATUS_BUS_BUSY (-9)
386 
390 #define I2C_STATUS_CANCEL (-10)
391 
401 #define I2C_STATUS_INVALID_TRANS (-11)
402 
407 #define I2C_WAIT_FOREVER (~(0U))
408 
412 typedef struct I2C_Config_ *I2C_Handle;
413 
423 typedef struct {
428  void *writeBuf;
429 
444  size_t writeCount;
445 
450  void *readBuf;
451 
464  size_t readCount;
465 
476  void *arg;
477 
496  volatile int_fast16_t status;
497 
504  uint_least8_t targetAddress;
505 
510  void *nextPtr;
512 
520 typedef enum {
533 
548 
570 typedef void (*I2C_CallbackFxn)(
571  I2C_Handle handle, I2C_Transaction *transaction, bool transferStatus);
572 
581 typedef enum {
585 } I2C_BitRate;
586 
595 typedef struct {
597  I2C_TransferMode transferMode;
598 
604 
610 
612  void *custom;
613 } I2C_Params;
614 
616 #define I2C_BASE_OBJECT \
617  /* I2C control variables */ \
618  I2C_TransferMode transferMode; /* Blocking or Callback mode */ \
619  I2C_CallbackFxn transferCallbackFxn; /* Callback function pointer */ \
620  I2C_Transaction *currentTransaction; /* Ptr to current I2C transaction */ \
621  \
622  /* I2C transaction pointers for I2C_MODE_CALLBACK */ \
623  I2C_Transaction *volatile headPtr; /* Head ptr for queued transactions */ \
624  I2C_Transaction *tailPtr; /* Tail ptr for queued transactions */ \
625  \
626  /* I2C RTOS objects */ \
627  HwiP_Struct hwi; /* Hwi object handle */ \
628  SemaphoreP_Struct mutex; /* Grants exclusive access to I2C */ \
629  SemaphoreP_Struct transferComplete; /* Signal I2C transfer complete */ \
630  \
631  /* Read and write variables */ \
632  const uint8_t *writeBuf; /* Internal inc. writeBuf index */ \
633  size_t writeCount; /* Internal dec. writeCounter */ \
634  uint8_t *readBuf; /* Internal inc. readBuf index */ \
635  size_t readCount; /* Internal dec. readCounter */ \
636  \
637  bool isOpen; /* Flag to show module is open */ \
638 
645 typedef struct {
646  I2C_BASE_OBJECT
647 } I2C_Object;
651 #define I2C_BASE_HWATTRS \
652  \
653  I2C_Regs *i2c; \
654  \
655  uint32_t intNum; \
656  \
669  uint32_t intPriority;
670 
676 typedef struct {
677  I2C_BASE_HWATTRS
678 } I2C_HWAttrs;
688 typedef struct I2C_Config_ {
690  void *object;
691 
694  void const *hwAttrs;
695 } I2C_Config;
696 
700 extern const I2C_Config I2C_config[];
704 extern const uint_least8_t I2C_count;
705 
743 extern void I2C_cancel(I2C_Handle handle);
744 
752 extern void I2C_close(I2C_Handle handle);
753 
775 extern int_fast16_t I2C_control(
776  I2C_Handle handle, uint_fast16_t cmd, void *controlArg);
777 
783 extern void I2C_init(void);
784 
800 extern I2C_Handle I2C_open(uint_least8_t index, I2C_Params *params);
801 
814 extern void I2C_Params_init(I2C_Params *params);
815 
838 extern int_fast16_t I2C_setClockTimeout(I2C_Handle handle, uint32_t timeout);
839 
872 extern bool I2C_transfer(I2C_Handle handle, I2C_Transaction *transaction);
873 
927 extern int_fast16_t I2C_transferTimeout(
928  I2C_Handle handle, I2C_Transaction *transaction, uint32_t timeout);
929 
930 #ifdef __cplusplus
931 }
932 #endif
933 
934 #endif /* ti_drivers_I2C__include */
935 
void * writeBuf
Definition: I2C.h:428
struct I2C_Config_ * I2C_Handle
A handle that is returned from an I2C_open() call.
Definition: I2C.h:412
void * arg
Definition: I2C.h:476
void I2C_Params_init(I2C_Params *params)
Initialize an I2C_Params structure to its default values.
I2C_TransferMode transferMode
Definition: I2C.h:597
void I2C_cancel(I2C_Handle handle)
Cancels all I2C transfers.
void * object
Definition: I2C.h:690
void * readBuf
Definition: I2C.h:450
Definition: I2C.h:584
I2C_Handle I2C_open(uint_least8_t index, I2C_Params *params)
Open an I2C driver instance.
void(* I2C_CallbackFxn)(I2C_Handle handle, I2C_Transaction *transaction, bool transferStatus)
The definition of a callback function.
Definition: I2C.h:570
void const * hwAttrs
Definition: I2C.h:694
I2C_TransferMode
Return behavior of I2C_Transfer() specified in the I2C_Params.
Definition: I2C.h:520
Definition: I2C.h:546
uint_least8_t targetAddress
Definition: I2C.h:504
I2C_CallbackFxn transferCallbackFxn
Definition: I2C.h:603
Definition: I2C.h:583
void I2C_close(I2C_Handle handle)
Function to close an I2C driver instance.
bool I2C_transfer(I2C_Handle handle, I2C_Transaction *transaction)
Perform an I2C transaction with an I2C target peripheral.
int_fast16_t I2C_control(I2C_Handle handle, uint_fast16_t cmd, void *controlArg)
Function performs implementation specific features on a driver instance.
void I2C_init(void)
Function to initialize the I2C driver.
I2C_BitRate bitRate
Definition: I2C.h:609
const I2C_Config I2C_config[]
Instance of I2C driver&#39;s configuration structure.
I2C parameters used with I2C_open().
Definition: I2C.h:595
int_fast16_t I2C_setClockTimeout(I2C_Handle handle, uint32_t timeout)
Set the I2C SCL clock timeout.
void * custom
Definition: I2C.h:612
int_fast16_t I2C_transferTimeout(I2C_Handle handle, I2C_Transaction *transaction, uint32_t timeout)
Perform an I2C transaction with an I2C target peripheral.
struct I2C_Config_ I2C_Config
I2C driver&#39;s configuration structure.
I2C driver&#39;s configuration structure.
Definition: I2C.h:688
const uint_least8_t I2C_count
Count of I2C instance.
volatile int_fast16_t status
Definition: I2C.h:496
Defines a transaction to be used with I2C_transfer() or I2C_transferTimeout().
Definition: I2C.h:423
Definition: I2C.h:582
size_t writeCount
Definition: I2C.h:444
I2C_BitRate
Bit rate for an I2C driver instance specified in the I2C_Params.
Definition: I2C.h:581
Definition: I2C.h:532
size_t readCount
Definition: I2C.h:464
© Copyright 1995-2023, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale