I2CCC26XX.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 I2CCC26XX.h
34  *
35  * @brief I2C driver implementation for a CC26XX I2C controller.
36  *
37  * # Driver Include #
38  * The I2C header file should be included in an application as follows:
39  * @code
40  * #include <ti/drivers/I2C.h>
41  * #include <ti/drivers/i2c/I2CCC26XX.h>
42  * @endcode
43  *
44  * Refer to @ref I2C.h for a complete description of APIs.
45  *
46  * # Overview #
47  * The general I2C API is normally used in application code, e.g. I2C_open()
48  * is used instead of I2CCC26XX_open(). The board file will define the device
49  * specific config, and casting in the general API will ensure that the correct
50  * device specific functions are called.
51  * This is also reflected in the example code in [Use Cases](@ref I2C_USE_CASES).
52  *
53  * ## General Behavior #
54  * Before using the I2C in CC26XX:
55  * - The I2C driver is initialized by calling I2C_init().
56  * - The I2C HW is configured and system dependencies are declared (e.g. IOs,
57  * power, etc.) by calling I2C_open().
58  * .
59  * The following is true for receive operation:
60  * - RX is enabled by calling I2C_transfer().
61  * The readCount of the ::I2C_Transaction must be set to a non-zero value.
62  * - If the I2C_transfer() succeeds, the I2C remains enabled.
63  * - The application must check the return value from I2C_transfer()
64  * to verify that the transfer succeeded.
65  * .
66  * The following apply for transmit operation:
67  * - TX is enabled by calling I2C_transfer().
68  * The writeCount of the ::I2C_Transaction must be set to a non-zero value.
69  * - If the I2C_transfer() succeeds, the I2C remains enabled.
70  * - The application must check the return value from I2C_transfer()
71  * to verify that the transfer succeeded.
72  * .
73  * After I2C operation has ended:
74  * - Release system dependencies for I2C by calling I2C_close().
75  *
76  * ### Known Issue #
77  * @warning The I2C may transmit a single data byte in the event that the
78  * I2C slave address is not acknowledged (NACK'd). This is due to a known
79  * hardware bug.
80  *
81  * ## Error handling #
82  * If an error occurs during operation:
83  * - The I2C Master transmits a stop bit and remains enabled.
84  * .
85  *
86  * ## Power Management #
87  * The I2CCC26XX driver sets a power constraint during transactions to keep
88  * the device out of standby; so when all tasks are blocked, the device will
89  * enter idle mode instead of standby. When the transactions have finished,
90  * the power constraint to prohibit standby is released.
91  * The following statements are valid:
92  * - After I2C_open() call: I2C is enabled, there are no active I2C
93  * transactions, the device can enter standby.
94  * - After I2C_transfer() call: active I2C transactions exist, the device
95  * might enter idle, but not standby.
96  * - When I2C_transfer() completes, either after success or error, I2C
97  * remains enabled, and the device can enter standby.
98  * - After I2C_close() call: I2C is disabled
99  * - If the device goes into idle during a transaction, the state of
100  * SDA is undefined in the time between the transaction completing and
101  * the device waking up. SCL will go low until the device wakes up and
102  * starts another transaction or releases the bus. If this is a problem
103  * for another device on the I2C bus, you can set a power constraint for
104  * #PowerCC26XX_DISALLOW_IDLE before the transaction and release it
105  * when the transaction completes.
106  *
107  * ## Supported Functions ##
108  * | Generic API Function | API Function | Description |
109  * |--------------------- |------------------------- |---------------------------------------------------|
110  * | I2C_init() | I2CCC26XX_init() | Initialize I2C driver |
111  * | I2C_open() | I2CCC26XX_open() | Initialize I2C HW and set system dependencies |
112  * | I2C_close() | I2CCC26XX_close() | Disable I2C HW and release system dependencies |
113  * | I2C_transfer() | I2CCC26XX_transfer() | Start I2C transfer |
114  *
115  * @note All calls should go through the generic API.
116  *
117  * ## Supported Bit Rates ##
118  * - #I2C_100kHz
119  * - #I2C_400kHz
120  *
121  * ## Unsupported Functionality #
122  * The CC26XX I2C driver currently does not support:
123  * - Multi-master mode
124  * - I2C slave mode
125  *
126  * ## Use Cases @anchor I2C_USE_CASES ##
127  * ### Basic Receive #
128  * Receive 10 bytes over I2C in ::I2C_MODE_BLOCKING.
129  * @code
130  * // Locals
131  * I2C_Handle handle;
132  * I2C_Params params;
133  * I2C_Transaction i2cTrans;
134  * uint8_t rxBuf[32]; // Receive buffer
135  * uint8_t txBuf[32]; // Transmit buffer
136  *
137  * // Configure I2C parameters.
138  * I2C_Params_init(&params);
139  *
140  * // Initialize master I2C transaction structure
141  * i2cTrans.writeCount = 0;
142  * i2cTrans.writeBuf = txBuf;
143  * i2cTrans.readCount = 10;
144  * i2cTrans.readBuf = rxBuf;
145  * i2cTrans.slaveAddress = 0x3C;
146  *
147  * // Open I2C
148  * handle = I2C_open(Board_I2C, &params);
149  *
150  * // Do I2C transfer receive
151  * I2C_transfer(handle, &i2cTrans);
152  * @endcode
153  *
154  * ### Basic Transmit #
155  * Transmit 16 bytes over I2C in ::I2C_MODE_CALLBACK.
156  * @code
157  * uint8_t rxBuffer[32]; // Receive buffer
158  * uint8_t txBuffer[32]; // Transmit buffer
159  * bool transferDone = false;
160  *
161  * static void transferCallback(I2C_Handle handle, I2C_Transaction *transac, bool result)
162  * {
163  * // Set length bytes
164  * if (result) {
165  * transferDone = true;
166  * } else {
167  * // Transaction failed, act accordingly...
168  * .
169  * .
170  * }
171  * }
172  *
173  * static void taskFxn(uintptr_t a0, uintptr_t a1)
174  * {
175  * // Locals
176  * I2C_Handle handle;
177  * I2C_Params params;
178  * I2C_Transaction i2cTrans;
179  *
180  * // Configure I2C parameters.
181  * I2C_Params_init(&params);
182  * params.transferMode = I2C_MODE_CALLBACK;
183  * params.transferCallbackFxn = transferCallback;
184  *
185  * // Prepare data to send, send 0x00, 0x01, 0x02, ...0xFF, 0x00, 0x01...
186  * for(uint32_t i = 0; i < numTxBytes; i++)
187  * txBuffer[i] = (uint8_t) i;
188  *
189  * // Initialize master I2C transaction structure
190  * i2cTrans.writeCount = 16;
191  * i2cTrans.writeBuf = txBuffer;
192  * i2cTrans.readCount = 0;
193  * i2cTrans.readBuf = rxBuffer;
194  * i2cTrans.slaveAddress = 0x3C;
195  *
196  * // Open I2C
197  * handle = I2C_open(Board_I2C, &params);
198  *
199  * // Do I2C transfer (in callback mode)
200  * I2C_transfer(handle, &i2cTrans);
201  *
202  * // Do other stuff while I2C is handling the transfer
203  * .
204  * .
205  *
206  * // Do something if I2C transfer is finished
207  * if(transferDone) {
208  * .
209  * .
210  * }
211  *
212  * // Continue...
213  * .
214  * .
215  * }
216  * @endcode
217  *
218  * ### Chained Transactions #
219  * Transmit 10 bytes and then 32 bytes over I2C in ::I2C_MODE_CALLBACK.
220  * @code
221  * uint8_t rxBuffer[32]; // Receive buffer
222  * uint8_t txBuffer[32]; // Transmit buffer
223  * uint8_t rxBuffer2[64]; // Receive buffer 2
224  * uint8_t txBuffer2[64]; // Transmit buffer 2
225  * bool transferDone = false;
226  *
227  * static void writeCallbackDefault(I2C_Handle handle, I2C_Transaction *transac, bool result)
228  * {
229  * // Set length bytes
230  * if (result) {
231  * transferDone = true;
232  * } else {
233  * // Transaction failed, act accordingly...
234  * .
235  * .
236  * }
237  * }
238  *
239  * static void taskFxn(uintptr_t a0, uintptr_t a1)
240  * {
241  * // Locals
242  * I2C_Handle handle;
243  * I2C_Params params;
244  * I2C_Transaction i2cTrans;
245  * I2C_Transaction i2cTrans2;
246  *
247  * // Configure I2C parameters.
248  * I2C_Params_init(&params);
249  * params.transferMode = I2C_MODE_CALLBACK;
250  * params.transferCallbackFxn = writeCallbackDefault;
251  *
252  * // Prepare data to send, send 0x00, 0x01, 0x02, ...0xFF, 0x00, 0x01...
253  * for(uint32_t i = 0; i < numTxBytes; i++)
254  * txBuffer[i] = (uint8_t) i;
255  *
256  * // Initialize first master I2C transaction structure
257  * i2cTrans.writeCount = 10;
258  * i2cTrans.writeBuf = txBuffer;
259  * i2cTrans.readCount = 0;
260  * i2cTrans.readBuf = rxBuffer;
261  * i2cTrans.slaveAddress = 0x3C;
262  *
263  * // Second transaction
264  * i2cTrans2.writeCount = 32;
265  * i2cTrans2.writeBuf = txBuffer2;
266  * i2cTrans2.readCount = 0;
267  * i2cTrans2.readBuf = rxBuffer2;
268  * i2cTrans2.slaveAddress = 0x2E;
269  *
270  * // Open I2C
271  * handle = I2C_open(Board_I2C, &params);
272  *
273  * // Do chained I2C transfers (in callback mode).
274  * I2C_transfer(handle, &i2cTrans);
275  * I2C_transfer(handle, &i2cTrans2);
276  *
277  * // Do other stuff while I2C is handling the transfers
278  * .
279  * .
280  *
281  * // Do something if I2C transfers are finished
282  * if(transferDone) {
283  * .
284  * .
285  * }
286  *
287  * // Continue...
288  * .
289  * .
290  * }
291  * @endcode
292  *
293  * # Instrumentation #
294  * The I2C driver interface produces log statements if instrumentation is
295  * enabled.
296  *
297  * Diagnostics Mask | Log details |
298  * ---------------- | ----------- |
299  * Diags_USER1 | basic I2C operations performed |
300  * Diags_USER2 | detailed I2C operations performed |
301  *
302  ******************************************************************************
303  */
304 
305 #ifndef ti_drivers_i2c_I2CCC26XX__include
306 #define ti_drivers_i2c_I2CCC26XX__include
307 
308 #ifdef __cplusplus
309 extern "C" {
310 #endif
311 
312 #include <stdint.h>
313 #include <stdbool.h>
314 #include <ti/drivers/I2C.h>
316 #include <ti/drivers/Power.h>
317 
318 #include <ti/drivers/dpl/HwiP.h>
319 #include <ti/drivers/dpl/SwiP.h>
320 #include <ti/drivers/dpl/SemaphoreP.h>
321 
332 /* Add I2CCC26XX_STATUS_* macros here */
333 
346 /* Add I2CCC26XX_CMD_* macros here */
347 
351 typedef unsigned long I2CBaseAddrType;
352 /* \cond */
353 typedef unsigned long I2CDataType;
354 /* \endcond */
355 
357 extern const I2C_FxnTable I2CCC26XX_fxnTable;
358 
380 typedef struct I2CCC26XX_I2CPinCfg {
381  uint8_t pinSDA;
382  uint8_t pinSCL;
384 
392 typedef enum I2CCC26XX_Mode {
393  I2CCC26XX_IDLE_MODE = 0, /* I2C is not performing a transaction */
394  I2CCC26XX_WRITE_MODE, /* I2C is currently performing write operations */
395  I2CCC26XX_READ_MODE, /* I2C is currently performing read operations */
396  I2CCC26XX_BUSBUSY_MODE, /* I2C Bus is currently busy */
397  I2CCC26XX_ERROR = 0xFF /* I2C error has occurred, exit gracefully */
398 } I2CCC26XX_Mode;
440 typedef struct I2CCC26XX_HWAttrsV1 {
442  I2CBaseAddrType baseAddr;
444  unsigned long powerMngrId;
446  int intNum;
460  uint8_t intPriority;
466  uint32_t swiPriority;
468  uint8_t sdaPin;
470  uint8_t sclPin;
472 
478 typedef struct I2CCC26XX_Object {
479  /* I2C control variables */
480  I2C_TransferMode transferMode;
481  I2C_CallbackFxn transferCallbackFxn;
482  volatile I2CCC26XX_Mode mode;
483  uint32_t bitRate;
485  /* I2C SYS/BIOS objects */
486  HwiP_Struct hwi;
487  SwiP_Struct swi;
488  SemaphoreP_Struct mutex;
489  SemaphoreP_Struct transferComplete;
491  /* PIN driver state object and handle */
492  PIN_State pinState;
493  PIN_Handle hPin;
494 
495  /* I2C current transaction */
496  I2C_Transaction *currentTransaction;
497  uint8_t *writeBufIdx;
498  unsigned int writeCountIdx;
499  uint8_t *readBufIdx;
500  unsigned int readCountIdx;
502  /* I2C transaction pointers for I2C_MODE_CALLBACK */
503  I2C_Transaction *headPtr;
504  I2C_Transaction *tailPtr;
506  /* I2C power notification */
507  void *i2cPostFxn;
508  Power_NotifyObj i2cPostObj;
510  bool isOpen;
511 } I2CCC26XX_Object;
512 
515 #ifdef __cplusplus
516 }
517 #endif
518 
519 #endif /* ti_drivers_i2c_I2CCC26XX__include */
uint8_t pinSDA
Definition: I2CCC26XX.h:381
int intNum
Definition: I2CCC26XX.h:446
struct I2CCC26XX_I2CPinCfg I2CCC26XX_I2CPinCfg
I2CCC26XX Pin Configuration.
struct I2CCC26XX_HWAttrsV1 I2CCC26XX_HWAttrsV1
I2CCC26XX Hardware attributes.
I2CCC26XX Pin Configuration.
Definition: I2CCC26XX.h:380
Power Manager.
I2CCC26XX Hardware attributes.
Definition: I2CCC26XX.h:440
I2C_TransferMode
Return behavior of I2C_Transfer() specified in the I2C_Params.
Definition: I2C.h:457
uint8_t sclPin
Definition: I2CCC26XX.h:470
unsigned long powerMngrId
Definition: I2CCC26XX.h:444
I2CBaseAddrType baseAddr
Definition: I2CCC26XX.h:442
Device-specific pin & GPIO driver for CC26xx family [def].
The definition of an I2C function table that contains the required set of functions to control a spec...
Definition: I2C.h:603
Power notify object structure.
Definition: Power.h:443
const I2C_FxnTable I2CCC26XX_fxnTable
underlying data structure for type PIN_State
Definition: PIN.h:707
uint8_t pinSCL
Definition: I2CCC26XX.h:382
void(* I2C_CallbackFxn)(I2C_Handle handle, I2C_Transaction *transaction, bool transferStatus)
The definition of a callback function.
Definition: I2C.h:507
Defines a transaction to be used with I2C_transfer()
Definition: I2C.h:380
uint8_t sdaPin
Definition: I2CCC26XX.h:468
uint32_t swiPriority
I2C Swi priority. The higher the number, the higher the priority. The minimum is 0 and the maximum is...
Definition: I2CCC26XX.h:466
Inter-Integrated Circuit (I2C) Driver.
unsigned long I2CBaseAddrType
Definition: I2CCC26XX.h:351
uint8_t intPriority
I2C Peripheral&#39;s interrupt priority.
Definition: I2CCC26XX.h:460
© Copyright 1995-2019, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale