MSPM0G1X0X_G3X0X TI-Driver Library  1.20.01.06
I2CMSPM0.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022-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 I2CMSPM0.h
34  *
35  * @brief I2C driver implementation for a MSPM0 I2C controller.
36  *
37  * @defgroup I2CMSPM0 I2C driver APIs
38  *
39  * # Overview
40  * Refer to @ref ti_drivers_I2C_Overview for a complete description of APIs and examples
41  * of use.
42  *
43  * The general I2C API is normally used in application code, e.g. I2C_open()
44  * is used instead of I2CMSPM0_open(). The board file will define the device
45  * specific config, and casting in the general API will ensure that the correct
46  * device specific functions are called.
47  *
48  * ## General Behavior #
49  * Before using the I2C:
50  * - The I2C driver is initialized by calling I2C_init().
51  * - The I2C HW is configured and system dependencies are declared (e.g. IOs,
52  * power, etc.) by calling I2C_open().
53  * .
54  * The following is true for receive operation:
55  * - RX is enabled by calling I2C_transfer().
56  * The readCount of the ::I2C_Transaction must be set to a non-zero value.
57  * - If the I2C_transfer() succeeds, the I2C remains enabled.
58  * - The application must check the return value from I2C_transfer()
59  * to verify that the transfer succeeded.
60  * .
61  * The following apply for transmit operation:
62  * - TX is enabled by calling I2C_transfer().
63  * The writeCount of the ::I2C_Transaction must be set to a non-zero value.
64  * - If the I2C_transfer() succeeds, the I2C remains enabled.
65  * - The application must check the return value from I2C_transfer()
66  * to verify that the transfer succeeded.
67  * .
68  * After I2C operation has ended:
69  * - Release system dependencies for I2C by calling I2C_close().
70  *
71  * ## Error handling #
72  * If an error occurs during operation:
73  * - The I2C Controller transmits a stop bit and remains enabled.
74  *
75  * ## Power Management #
76  * The I2C driver sets a power constraint during transactions to keep
77  * the device out of standby; so when all tasks are blocked, the device will
78  * enter idle mode instead of standby. When the transactions have finished,
79  * the power constraint to prohibit standby is released.
80  * The following statements are valid:
81  * - After I2C_open() call: I2C is enabled, there are no active I2C
82  * transactions, the device can enter standby.
83  * - After I2C_transfer() call: active I2C transactions exist, the device
84  * might enter idle, but not standby.
85  * - When I2C_transfer() completes, either after success or error, I2C
86  * remains enabled, and the device can enter standby.
87  * - After I2C_close() call: I2C is disabled
88  * - If the device goes into idle during a transaction, the state of
89  * SDA is undefined in the time between the transaction completing and
90  * the device waking up. SCL will go low until the device wakes up and
91  * starts another transaction or releases the bus. If this is a problem
92  * for another device on the I2C bus, you can set a power constraint for
93  * PowerMSPM0_DISALLOW_IDLE before the transaction and release it
94  * when the transaction completes.
95  *
96  * ## Supported Functions ##
97  * | Generic API Function | API Function | Description |
98  * |--------------------- |---------------------------|---------------------------------------------------|
99  * | I2C_init() | I2CMSPM0_initHw() | Initialize I2C driver |
100  * | I2C_open() | I2C_open() | Initialize I2C HW and set system dependencies |
101  * | I2C_close() | I2C_close() | Disable I2C HW and release system dependencies |
102  * | I2C_transfer() | I2CSupport_primeTransfer()| Start I2C transfer |
103  *
104  * @note All calls should go through the generic API.
105  *
106  * ## Supported Bit Rates ##
107  * - #I2C_100kHz
108  * - #I2C_400kHz
109  * - #I2C_1000kHz
110  * ## Unsupported Functionality #
111  * The I2C driver currently does not support:
112  * - Multi-controller mode
113  * - I2C target mode
114  *
115  * ## Use Cases @anchor I2C_USE_CASES ##
116  * ### Basic Receive #
117  * Receive 10 bytes over I2C in ::I2C_MODE_BLOCKING.
118  * @code
119  * // Locals
120  * I2C_Handle handle;
121  * I2C_Params params;
122  * I2C_Transaction i2cTrans;
123  * uint8_t rxBuf[32]; // Receive buffer
124  * uint8_t txBuf[32]; // Transmit buffer
125  *
126  * // Configure I2C parameters.
127  * I2C_Params_init(&params);
128  *
129  * // Initialize I2C transaction structure
130  * i2cTrans.writeCount = 0;
131  * i2cTrans.writeBuf = txBuf;
132  * i2cTrans.readCount = 10;
133  * i2cTrans.readBuf = rxBuf;
134  * i2cTrans.targetAddress = 0x3C;
135  *
136  * // Open I2C
137  * handle = I2C_open(CONFIG_I2C, &params);
138  *
139  * // Do I2C transfer receive
140  * I2C_transfer(handle, &i2cTrans);
141  * @endcode
142  *
143  * ### Basic Transmit #
144  * Transmit 16 bytes over I2C in ::I2C_MODE_CALLBACK.
145  * @code
146  * uint8_t rxBuffer[32]; // Receive buffer
147  * uint8_t txBuffer[32]; // Transmit buffer
148  * bool transferDone = false;
149  *
150  * static void transferCallback(I2C_Handle handle, I2C_Transaction *transac, bool result)
151  * {
152  * // Set length bytes
153  * if (result) {
154  * transferDone = true;
155  * } else {
156  * // Transaction failed, act accordingly...
157  * .
158  * .
159  * }
160  * }
161  *
162  * static void taskFxn(uintptr_t a0, uintptr_t a1)
163  * {
164  * // Locals
165  * I2C_Handle handle;
166  * I2C_Params params;
167  * I2C_Transaction i2cTrans;
168  *
169  * // Configure I2C parameters.
170  * I2C_Params_init(&params);
171  * params.transferMode = I2C_MODE_CALLBACK;
172  * params.transferCallbackFxn = transferCallback;
173  *
174  * // Prepare data to send, send 0x00, 0x01, 0x02, ...0xFF, 0x00, 0x01...
175  * for(uint32_t i = 0; i < numTxBytes; i++)
176  * txBuffer[i] = (uint8_t) i;
177  *
178  * // Initialize I2C transaction structure
179  * i2cTrans.writeCount = 16;
180  * i2cTrans.writeBuf = txBuffer;
181  * i2cTrans.readCount = 0;
182  * i2cTrans.readBuf = rxBuffer;
183  * i2cTrans.targetAddress = 0x3C;
184  *
185  * // Open I2C
186  * handle = I2C_open(CONFIG_I2C, &params);
187  *
188  * // Do I2C transfer (in callback mode)
189  * I2C_transfer(handle, &i2cTrans);
190  *
191  * // Do other stuff while I2C is handling the transfer.
192  * .
193  * .
194  *
195  * // Do something if I2C transfer is finished.
196  * if(transferDone) {
197  * .
198  * .
199  * }
200  *
201  * // Continue...
202  * .
203  * .
204  * }
205  * @endcode
206  *
207  * ### Chained Transactions #
208  * Transmit 10 bytes and then 32 bytes over I2C in ::I2C_MODE_CALLBACK.
209  * @code
210  * uint8_t rxBuffer[32]; // Receive buffer
211  * uint8_t txBuffer[32]; // Transmit buffer
212  * uint8_t rxBuffer2[64]; // Receive buffer 2
213  * uint8_t txBuffer2[64]; // Transmit buffer 2
214  * bool transferDone = false;
215  *
216  * static void writeCallbackDefault(I2C_Handle handle, I2C_Transaction *transac, bool result)
217  * {
218  * // Set length bytes
219  * if (result) {
220  * transferDone = true;
221  * } else {
222  * // Transaction failed, act accordingly...
223  * .
224  * .
225  * }
226  * }
227  *
228  * static void taskFxn(uintptr_t a0, uintptr_t a1)
229  * {
230  * // Locals
231  * I2C_Handle handle;
232  * I2C_Params params;
233  * I2C_Transaction i2cTrans;
234  * I2C_Transaction i2cTrans2;
235  *
236  * // Configure I2C parameters.
237  * I2C_Params_init(&params);
238  * params.transferMode = I2C_MODE_CALLBACK;
239  * params.transferCallbackFxn = writeCallbackDefault;
240  *
241  * // Prepare data to send, send 0x00, 0x01, 0x02, ...0xFF, 0x00, 0x01...
242  * for(uint32_t i = 0; i < numTxBytes; i++)
243  * txBuffer[i] = (uint8_t) i;
244  *
245  * // Initialize first I2C transaction structure
246  * i2cTrans.writeCount = 10;
247  * i2cTrans.writeBuf = txBuffer;
248  * i2cTrans.readCount = 0;
249  * i2cTrans.readBuf = rxBuffer;
250  * i2cTrans.targetAddress = 0x3C;
251  *
252  * // Second transaction
253  * i2cTrans2.writeCount = 32;
254  * i2cTrans2.writeBuf = txBuffer2;
255  * i2cTrans2.readCount = 0;
256  * i2cTrans2.readBuf = rxBuffer2;
257  * i2cTrans2.targetAddress = 0x2E;
258  *
259  * // Open I2C
260  * handle = I2C_open(CONFIG_I2C, &params);
261  *
262  * // Do chained I2C transfers (in callback mode).
263  * I2C_transfer(handle, &i2cTrans);
264  * I2C_transfer(handle, &i2cTrans2);
265  *
266  * // Do other stuff while I2C is handling the transfers.
267  * .
268  * .
269  *
270  * // Do something if I2C transfers are finished.
271  * if(transferDone) {
272  * .
273  * .
274  * }
275  *
276  * // Continue...
277  * .
278  * .
279  * }
280  * @endcode
281  *
282  * # Instrumentation #
283  * The I2C driver interface produces log statements if instrumentation is
284  * enabled.
285  *
286  * Diagnostics Mask | Log details |
287  * ---------------- | ----------- |
288  * Diags_USER1 | basic I2C operations performed |
289  * Diags_USER2 | detailed I2C operations performed |
290  *
291  ******************************************************************************
292  */
297 #ifndef ti_drivers_i2c_include
298 #define ti_drivers_i2c_include
299 
300 #include <stdbool.h>
301 #include <stdint.h>
302 
303 #include <ti/drivers/I2C.h>
305 
306 #ifdef POWER_MANAGEMENT_MSPM0
307 #include <ti/drivers/Power.h>
308 #endif
309 
310 #ifdef __cplusplus
311 extern "C" {
312 #endif
313 
317 #define I2C_CONTROLLER_INTERRUPTS_MASK 0xFFFF
318 
340 typedef struct {
341  uint8_t pinSDA;
342  uint8_t pinSCL;
344 
384 typedef struct {
385  I2C_BASE_HWATTRS
387  /* SDA pin PINCM index and mux */
388  uint8_t sdaPincm;
389  uint8_t sdaPinIndex;
390  uint32_t sdaPinMux;
391  /* SCL pin PINCM index and mux */
392  uint8_t sclPincm;
393  uint8_t sclPinIndex;
394  uint32_t sclPinMux;
396  DL_I2C_CLOCK clockSource;
397  DL_I2C_CLOCK_DIVIDE
399  DL_I2C_RX_FIFO_LEVEL
401  DL_I2C_TX_FIFO_LEVEL
404  uint16_t i2cClk;
406 
412 typedef struct {
413  I2C_BASE_OBJECT
415  uint16_t burstCount;
417  uint32_t bitRate;
418  /* Pincm: We need to cache these because we might have custom pins */
419  uint8_t sdaPincm;
420  uint8_t sclPincm;
422  void *i2cPostFxn;
424 #ifdef POWER_MANAGEMENT_MSPM0
425  Power_NotifyObj i2cPostObj;
426 #endif
428 
429 #ifdef __cplusplus
430 }
431 #endif
432 
433 #endif /* ti_drivers_i2c_include */
434 
uint32_t sclPinMux
Definition: I2CMSPM0.h:394
uint8_t sclPinIndex
Definition: I2CMSPM0.h:393
I2C Hardware attributes.
Definition: I2CMSPM0.h:384
bool burstStarted
Definition: I2CMSPM0.h:416
uint8_t sclPincm
Definition: I2CMSPM0.h:392
I2C Object.
Definition: I2CMSPM0.h:412
uint8_t sdaPinIndex
Definition: I2CMSPM0.h:389
void * i2cPostFxn
Definition: I2CMSPM0.h:422
bool isClockStretchingEnabled
Definition: I2CMSPM0.h:403
uint8_t pinSDA
Definition: I2CMSPM0.h:341
uint8_t sclPincm
Definition: I2CMSPM0.h:420
DL_I2C_TX_FIFO_LEVEL txIntFifoThr
Definition: I2CMSPM0.h:402
uint8_t sdaPincm
Definition: I2CMSPM0.h:419
uint16_t i2cClk
Definition: I2CMSPM0.h:404
uint8_t pinSCL
Definition: I2CMSPM0.h:342
Holder of common helper functions for the I2C driver.
uint32_t bitRate
Definition: I2CMSPM0.h:417
DL_I2C_CLOCK clockSource
Definition: I2CMSPM0.h:396
I2C Pin Configuration.
Definition: I2CMSPM0.h:340
I2C_BASE_OBJECT uint16_t burstCount
Definition: I2CMSPM0.h:415
I2C_BASE_HWATTRS uint8_t sdaPincm
Definition: I2CMSPM0.h:388
DL_I2C_RX_FIFO_LEVEL rxIntFifoThr
Definition: I2CMSPM0.h:400
I2C Driver.
DL_I2C_CLOCK_DIVIDE clockDivider
Definition: I2CMSPM0.h:398
uint32_t sdaPinMux
Definition: I2CMSPM0.h:390
© Copyright 1995-2023, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale