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  * ## Error handling #
77  * If an error occurs during operation:
78  * - The I2C Master transmits a stop bit and remains enabled.
79  * .
80  *
81  * ## Power Management #
82  * The I2CCC26XX driver sets a power constraint during transactions to keep
83  * the device out of standby; so when all tasks are blocked, the device will
84  * enter idle mode instead of standby. When the transactions have finished,
85  * the power constraint to prohibit standby is released.
86  * The following statements are valid:
87  * - After I2C_open() call: I2C is enabled, there are no active I2C
88  * transactions, the device can enter standby.
89  * - After I2C_transfer() call: active I2C transactions exist, the device
90  * might enter idle, but not standby.
91  * - When I2C_transfer() completes, either after success or error, I2C
92  * remains enabled, and the device can enter standby.
93  * - After I2C_close() call: I2C is disabled
94  * - If the device goes into idle during a transaction, the state of
95  * SDA is undefined in the time between the transaction completing and
96  * the device waking up. SCL will go low until the device wakes up and
97  * starts another transaction or releases the bus. If this is a problem
98  * for another device on the I2C bus, you can set a power constraint for
99  * #PowerCC26XX_DISALLOW_IDLE before the transaction and release it
100  * when the transaction completes.
101  *
102  * ## Supported Functions ##
103  * | Generic API Function | API Function | Description |
104  * |--------------------- |------------------------- |---------------------------------------------------|
105  * | I2C_init() | I2CCC26XX_init() | Initialize I2C driver |
106  * | I2C_open() | I2CCC26XX_open() | Initialize I2C HW and set system dependencies |
107  * | I2C_close() | I2CCC26XX_close() | Disable I2C HW and release system dependencies |
108  * | I2C_transfer() | I2CCC26XX_transfer() | Start I2C transfer |
109  *
110  * @note All calls should go through the generic API.
111  *
112  * ## Supported Bit Rates ##
113  * - #I2C_100kHz
114  * - #I2C_400kHz
115  *
116  * ## Unsupported Functionality #
117  * The CC26XX I2C driver currently does not support:
118  * - Multi-master mode
119  * - I2C slave mode
120  *
121  * ## Use Cases @anchor I2C_USE_CASES ##
122  * ### Basic Receive #
123  * Receive 10 bytes over I2C in ::I2C_MODE_BLOCKING.
124  * @code
125  * // Locals
126  * I2C_Handle handle;
127  * I2C_Params params;
128  * I2C_Transaction i2cTrans;
129  * uint8_t rxBuf[32]; // Receive buffer
130  * uint8_t txBuf[32]; // Transmit buffer
131  *
132  * // Configure I2C parameters.
133  * I2C_Params_init(&params);
134  *
135  * // Initialize master I2C transaction structure
136  * i2cTrans.writeCount = 0;
137  * i2cTrans.writeBuf = txBuf;
138  * i2cTrans.readCount = 10;
139  * i2cTrans.readBuf = rxBuf;
140  * i2cTrans.slaveAddress = 0x3C;
141  *
142  * // Open I2C
143  * handle = I2C_open(Board_I2C, &params);
144  *
145  * // Do I2C transfer receive
146  * I2C_transfer(handle, &i2cTrans);
147  * @endcode
148  *
149  * ### Basic Transmit #
150  * Transmit 16 bytes over I2C in ::I2C_MODE_CALLBACK.
151  * @code
152  * uint8_t rxBuffer[32]; // Receive buffer
153  * uint8_t txBuffer[32]; // Transmit buffer
154  * bool transferDone = false;
155  *
156  * static void transferCallback(I2C_Handle handle, I2C_Transaction *transac, bool result)
157  * {
158  * // Set length bytes
159  * if (result) {
160  * transferDone = true;
161  * } else {
162  * // Transaction failed, act accordingly...
163  * .
164  * .
165  * }
166  * }
167  *
168  * static void taskFxn(uintptr_t a0, uintptr_t a1)
169  * {
170  * // Locals
171  * I2C_Handle handle;
172  * I2C_Params params;
173  * I2C_Transaction i2cTrans;
174  *
175  * // Configure I2C parameters.
176  * I2C_Params_init(&params);
177  * params.transferMode = I2C_MODE_CALLBACK;
178  * params.transferCallbackFxn = transferCallback;
179  *
180  * // Prepare data to send, send 0x00, 0x01, 0x02, ...0xFF, 0x00, 0x01...
181  * for(uint32_t i = 0; i < numTxBytes; i++)
182  * txBuffer[i] = (uint8_t) i;
183  *
184  * // Initialize master I2C transaction structure
185  * i2cTrans.writeCount = 16;
186  * i2cTrans.writeBuf = txBuffer;
187  * i2cTrans.readCount = 0;
188  * i2cTrans.readBuf = rxBuffer;
189  * i2cTrans.slaveAddress = 0x3C;
190  *
191  * // Open I2C
192  * handle = I2C_open(Board_I2C, &params);
193  *
194  * // Do I2C transfer (in callback mode)
195  * I2C_transfer(handle, &i2cTrans);
196  *
197  * // Do other stuff while I2C is handling the transfer
198  * .
199  * .
200  *
201  * // Do something if I2C transfer is finished
202  * if(transferDone) {
203  * .
204  * .
205  * }
206  *
207  * // Continue...
208  * .
209  * .
210  * }
211  * @endcode
212  *
213  * ### Chained Transactions #
214  * Transmit 10 bytes and then 32 bytes over I2C in ::I2C_MODE_CALLBACK.
215  * @code
216  * uint8_t rxBuffer[32]; // Receive buffer
217  * uint8_t txBuffer[32]; // Transmit buffer
218  * uint8_t rxBuffer2[64]; // Receive buffer 2
219  * uint8_t txBuffer2[64]; // Transmit buffer 2
220  * bool transferDone = false;
221  *
222  * static void writeCallbackDefault(I2C_Handle handle, I2C_Transaction *transac, bool result)
223  * {
224  * // Set length bytes
225  * if (result) {
226  * transferDone = true;
227  * } else {
228  * // Transaction failed, act accordingly...
229  * .
230  * .
231  * }
232  * }
233  *
234  * static void taskFxn(uintptr_t a0, uintptr_t a1)
235  * {
236  * // Locals
237  * I2C_Handle handle;
238  * I2C_Params params;
239  * I2C_Transaction i2cTrans;
240  * I2C_Transaction i2cTrans2;
241  *
242  * // Configure I2C parameters.
243  * I2C_Params_init(&params);
244  * params.transferMode = I2C_MODE_CALLBACK;
245  * params.transferCallbackFxn = writeCallbackDefault;
246  *
247  * // Prepare data to send, send 0x00, 0x01, 0x02, ...0xFF, 0x00, 0x01...
248  * for(uint32_t i = 0; i < numTxBytes; i++)
249  * txBuffer[i] = (uint8_t) i;
250  *
251  * // Initialize first master I2C transaction structure
252  * i2cTrans.writeCount = 10;
253  * i2cTrans.writeBuf = txBuffer;
254  * i2cTrans.readCount = 0;
255  * i2cTrans.readBuf = rxBuffer;
256  * i2cTrans.slaveAddress = 0x3C;
257  *
258  * // Second transaction
259  * i2cTrans2.writeCount = 32;
260  * i2cTrans2.writeBuf = txBuffer2;
261  * i2cTrans2.readCount = 0;
262  * i2cTrans2.readBuf = rxBuffer2;
263  * i2cTrans2.slaveAddress = 0x2E;
264  *
265  * // Open I2C
266  * handle = I2C_open(Board_I2C, &params);
267  *
268  * // Do chained I2C transfers (in callback mode).
269  * I2C_transfer(handle, &i2cTrans);
270  * I2C_transfer(handle, &i2cTrans2);
271  *
272  * // Do other stuff while I2C is handling the transfers
273  * .
274  * .
275  *
276  * // Do something if I2C transfers are finished
277  * if(transferDone) {
278  * .
279  * .
280  * }
281  *
282  * // Continue...
283  * .
284  * .
285  * }
286  * @endcode
287  *
288  * # Instrumentation #
289  * The I2C driver interface produces log statements if instrumentation is
290  * enabled.
291  *
292  * Diagnostics Mask | Log details |
293  * ---------------- | ----------- |
294  * Diags_USER1 | basic I2C operations performed |
295  * Diags_USER2 | detailed I2C operations performed |
296  *
297  ******************************************************************************
298  */
299 
300 #ifndef ti_drivers_i2c_I2CCC26XX__include
301 #define ti_drivers_i2c_I2CCC26XX__include
302 
303 #ifdef __cplusplus
304 extern "C" {
305 #endif
306 
307 #include <stdint.h>
308 #include <stdbool.h>
309 #include <ti/drivers/I2C.h>
311 #include <ti/drivers/Power.h>
312 
313 #include <ti/drivers/dpl/HwiP.h>
314 #include <ti/drivers/dpl/SwiP.h>
315 #include <ti/drivers/dpl/SemaphoreP.h>
316 
327 /* Add I2CCC26XX_STATUS_* macros here */
328 
341 /* Add I2CCC26XX_CMD_* macros here */
342 
346 typedef unsigned long I2CBaseAddrType;
347 /* \cond */
348 typedef unsigned long I2CDataType;
349 /* \endcond */
350 
352 extern const I2C_FxnTable I2CCC26XX_fxnTable;
353 
375 typedef struct I2CCC26XX_I2CPinCfg {
376  uint8_t pinSDA;
377  uint8_t pinSCL;
379 
387 typedef enum I2CCC26XX_Mode {
388  I2CCC26XX_IDLE_MODE = 0, /* I2C is not performing a transaction */
389  I2CCC26XX_WRITE_MODE, /* I2C is currently performing write operations */
390  I2CCC26XX_READ_MODE, /* I2C is currently performing read operations */
391  I2CCC26XX_BUSBUSY_MODE, /* I2C Bus is currently busy */
392  I2CCC26XX_ERROR = 0xFF /* I2C error has occurred, exit gracefully */
393 } I2CCC26XX_Mode;
435 typedef struct I2CCC26XX_HWAttrsV1 {
437  I2CBaseAddrType baseAddr;
439  unsigned long powerMngrId;
441  int intNum;
455  uint8_t intPriority;
461  uint32_t swiPriority;
463  uint8_t sdaPin;
465  uint8_t sclPin;
467 
473 typedef struct I2CCC26XX_Object {
474  /* I2C control variables */
475  I2C_TransferMode transferMode;
476  I2C_CallbackFxn transferCallbackFxn;
477  volatile I2CCC26XX_Mode mode;
478  uint32_t bitRate;
480  /* I2C SYS/BIOS objects */
481  HwiP_Struct hwi;
482  SwiP_Struct swi;
483  SemaphoreP_Struct mutex;
484  SemaphoreP_Struct transferComplete;
486  /* PIN driver state object and handle */
487  PIN_State pinState;
488  PIN_Handle hPin;
489 
490  /* I2C current transaction */
491  I2C_Transaction *currentTransaction;
492  uint8_t *writeBufIdx;
493  unsigned int writeCountIdx;
494  uint8_t *readBufIdx;
495  unsigned int readCountIdx;
497  /* I2C transaction pointers for I2C_MODE_CALLBACK */
498  I2C_Transaction *headPtr;
499  I2C_Transaction *tailPtr;
501  /* I2C power notification */
502  void *i2cPostFxn;
503  Power_NotifyObj i2cPostObj;
505  bool isOpen;
506 } I2CCC26XX_Object;
507 
510 #ifdef __cplusplus
511 }
512 #endif
513 
514 #endif /* ti_drivers_i2c_I2CCC26XX__include */
uint8_t pinSDA
Definition: I2CCC26XX.h:376
int intNum
Definition: I2CCC26XX.h:441
This structure defines the I2C slave address, pointers to write and read buffers, and their associate...
Definition: I2C.h:410
struct I2CCC26XX_I2CPinCfg I2CCC26XX_I2CPinCfg
I2CCC26XX Pin Configuration.
struct I2CCC26XX_HWAttrsV1 I2CCC26XX_HWAttrsV1
I2CCC26XX Hardware attributes.
I2CCC26XX Pin Configuration.
Definition: I2CCC26XX.h:375
Power Manager interface.
Power notify object structure.
Definition: Power.h:121
I2CCC26XX Hardware attributes.
Definition: I2CCC26XX.h:435
uint8_t sclPin
Definition: I2CCC26XX.h:465
enum I2C_TransferMode_ I2C_TransferMode
This I2C driver supports two transfer modes of operation: blocking and callback. The transfer mode is...
unsigned long powerMngrId
Definition: I2CCC26XX.h:439
I2CBaseAddrType baseAddr
Definition: I2CCC26XX.h:437
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:554
const I2C_FxnTable I2CCC26XX_fxnTable
underlying data structure for type PIN_State
Definition: PIN.h:707
uint8_t pinSCL
Definition: I2CCC26XX.h:377
void(* I2C_CallbackFxn)(I2C_Handle handle, I2C_Transaction *transaction, bool transferStatus)
I2C callback function prototype. The application is responsible for declaring a callback function whe...
Definition: I2C.h:474
uint8_t sdaPin
Definition: I2CCC26XX.h:463
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:461
Inter-Intergrated Circuit driver interface.
unsigned long I2CBaseAddrType
Definition: I2CCC26XX.h:346
uint8_t intPriority
I2C Peripheral&#39;s interrupt priority.
Definition: I2CCC26XX.h:455
Copyright 2018, Texas Instruments Incorporated