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  *
95  * ## Supported Functions ##
96  * | Generic API Function | API Function | Description |
97  * |--------------------- |------------------------- |---------------------------------------------------|
98  * | I2C_init() | I2CCC26XX_init() | Initialize I2C driver |
99  * | I2C_open() | I2CCC26XX_open() | Initialize I2C HW and set system dependencies |
100  * | I2C_close() | I2CCC26XX_close() | Disable I2C HW and release system dependencies |
101  * | I2C_transfer() | I2CCC26XX_transfer() | Start I2C transfer |
102  *
103  * @note All calls should go through the generic API.
104  *
105  * ## Supported Bit Rates ##
106  * - #I2C_100kHz
107  * - #I2C_400kHz
108  *
109  * ## Unsupported Functionality #
110  * The CC26XX I2C driver currently does not support:
111  * - Multi-master mode
112  * - I2C slave mode
113  *
114  * ## Use Cases @anchor I2C_USE_CASES ##
115  * ### Basic Receive #
116  * Receive 10 bytes over I2C in ::I2C_MODE_BLOCKING.
117  * @code
118  * // Locals
119  * I2C_Handle handle;
120  * I2C_Params params;
121  * I2C_Transaction i2cTrans;
122  * uint8_t rxBuf[32]; // Receive buffer
123  * uint8_t txBuf[32]; // Transmit buffer
124  *
125  * // Configure I2C parameters.
126  * I2C_Params_init(&params);
127  *
128  * // Initialize master I2C transaction structure
129  * i2cTrans.writeCount = 0;
130  * i2cTrans.writeBuf = txBuf;
131  * i2cTrans.readCount = 10;
132  * i2cTrans.readBuf = rxBuf;
133  * i2cTrans.slaveAddress = 0x3C;
134  *
135  * // Open I2C
136  * handle = I2C_open(Board_I2C, &params);
137  *
138  * // Do I2C transfer receive
139  * I2C_transfer(handle, &i2cTrans);
140  * @endcode
141  *
142  * ### Basic Transmit #
143  * Transmit 16 bytes over I2C in ::I2C_MODE_CALLBACK.
144  * @code
145  * uint8_t rxBuffer[32]; // Receive buffer
146  * uint8_t txBuffer[32]; // Transmit buffer
147  * bool transferDone = false;
148  *
149  * static void transferCallback(I2C_Handle handle, I2C_Transaction *transac, bool result)
150  * {
151  * // Set length bytes
152  * if (result) {
153  * transferDone = true;
154  * } else {
155  * // Transaction failed, act accordingly...
156  * .
157  * .
158  * }
159  * }
160  *
161  * static void taskFxn(uintptr_t a0, uintptr_t a1)
162  * {
163  * // Locals
164  * I2C_Handle handle;
165  * I2C_Params params;
166  * I2C_Transaction i2cTrans;
167  *
168  * // Configure I2C parameters.
169  * I2C_Params_init(&params);
170  * params.transferMode = I2C_MODE_CALLBACK;
171  * params.transferCallbackFxn = transferCallback;
172  *
173  * // Prepare data to send, send 0x00, 0x01, 0x02, ...0xFF, 0x00, 0x01...
174  * for(uint32_t i = 0; i < numTxBytes; i++)
175  * txBuffer[i] = (uint8_t) i;
176  *
177  * // Initialize master I2C transaction structure
178  * i2cTrans.writeCount = 16;
179  * i2cTrans.writeBuf = txBuffer;
180  * i2cTrans.readCount = 0;
181  * i2cTrans.readBuf = rxBuffer;
182  * i2cTrans.slaveAddress = 0x3C;
183  *
184  * // Open I2C
185  * handle = I2C_open(Board_I2C, &params);
186  *
187  * // Do I2C transfer (in callback mode)
188  * I2C_transfer(handle, &i2cTrans);
189  *
190  * // Do other stuff while I2C is handling the transfer
191  * .
192  * .
193  *
194  * // Do something if I2C transfer is finished
195  * if(transferDone) {
196  * .
197  * .
198  * }
199  *
200  * // Continue...
201  * .
202  * .
203  * }
204  * @endcode
205  *
206  * ### Chained Transactions #
207  * Transmit 10 bytes and then 32 bytes over I2C in ::I2C_MODE_CALLBACK.
208  * @code
209  * uint8_t rxBuffer[32]; // Receive buffer
210  * uint8_t txBuffer[32]; // Transmit buffer
211  * uint8_t rxBuffer2[64]; // Receive buffer 2
212  * uint8_t txBuffer2[64]; // Transmit buffer 2
213  * bool transferDone = false;
214  *
215  * static void writeCallbackDefault(I2C_Handle handle, I2C_Transaction *transac, bool result)
216  * {
217  * // Set length bytes
218  * if (result) {
219  * transferDone = true;
220  * } else {
221  * // Transaction failed, act accordingly...
222  * .
223  * .
224  * }
225  * }
226  *
227  * static void taskFxn(uintptr_t a0, uintptr_t a1)
228  * {
229  * // Locals
230  * I2C_Handle handle;
231  * I2C_Params params;
232  * I2C_Transaction i2cTrans;
233  * I2C_Transaction i2cTrans2;
234  *
235  * // Configure I2C parameters.
236  * I2C_Params_init(&params);
237  * params.transferMode = I2C_MODE_CALLBACK;
238  * params.transferCallbackFxn = writeCallbackDefault;
239  *
240  * // Prepare data to send, send 0x00, 0x01, 0x02, ...0xFF, 0x00, 0x01...
241  * for(uint32_t i = 0; i < numTxBytes; i++)
242  * txBuffer[i] = (uint8_t) i;
243  *
244  * // Initialize first master I2C transaction structure
245  * i2cTrans.writeCount = 10;
246  * i2cTrans.writeBuf = txBuffer;
247  * i2cTrans.readCount = 0;
248  * i2cTrans.readBuf = rxBuffer;
249  * i2cTrans.slaveAddress = 0x3C;
250  *
251  * // Second transaction
252  * i2cTrans2.writeCount = 32;
253  * i2cTrans2.writeBuf = txBuffer2;
254  * i2cTrans2.readCount = 0;
255  * i2cTrans2.readBuf = rxBuffer2;
256  * i2cTrans2.slaveAddress = 0x2E;
257  *
258  * // Open I2C
259  * handle = I2C_open(Board_I2C, &params);
260  *
261  * // Do chained I2C transfers (in callback mode).
262  * I2C_transfer(handle, &i2cTrans);
263  * I2C_transfer(handle, &i2cTrans2);
264  *
265  * // Do other stuff while I2C is handling the transfers
266  * .
267  * .
268  *
269  * // Do something if I2C transfers are finished
270  * if(transferDone) {
271  * .
272  * .
273  * }
274  *
275  * // Continue...
276  * .
277  * .
278  * }
279  * @endcode
280  *
281  * # Instrumentation #
282  * The I2C driver interface produces log statements if instrumentation is
283  * enabled.
284  *
285  * Diagnostics Mask | Log details |
286  * ---------------- | ----------- |
287  * Diags_USER1 | basic I2C operations performed |
288  * Diags_USER2 | detailed I2C operations performed |
289  *
290  ******************************************************************************
291  */
292 
293 #ifndef ti_drivers_i2c_I2CCC26XX__include
294 #define ti_drivers_i2c_I2CCC26XX__include
295 
296 #ifdef __cplusplus
297 extern "C" {
298 #endif
299 
300 #include <stdint.h>
301 #include <stdbool.h>
302 #include <ti/drivers/I2C.h>
304 #include <ti/drivers/Power.h>
305 
306 #include <ti/drivers/dpl/HwiP.h>
307 #include <ti/drivers/dpl/SwiP.h>
308 #include <ti/drivers/dpl/SemaphoreP.h>
309 
320 /* Add I2CCC26XX_STATUS_* macros here */
321 
334 /* Add I2CCC26XX_CMD_* macros here */
335 
339 typedef unsigned long I2CBaseAddrType;
340 /* \cond */
341 typedef unsigned long I2CDataType;
342 /* \endcond */
343 
345 extern const I2C_FxnTable I2CCC26XX_fxnTable;
346 
368 typedef struct I2CCC26XX_I2CPinCfg {
369  uint8_t pinSDA;
370  uint8_t pinSCL;
372 
380 typedef enum I2CCC26XX_Mode {
381  I2CCC26XX_IDLE_MODE = 0, /* I2C is not performing a transaction */
382  I2CCC26XX_WRITE_MODE, /* I2C is currently performing write operations */
383  I2CCC26XX_READ_MODE, /* I2C is currently performing read operations */
384  I2CCC26XX_BUSBUSY_MODE, /* I2C Bus is currently busy */
385  I2CCC26XX_ERROR = 0xFF /* I2C error has occurred, exit gracefully */
386 } I2CCC26XX_Mode;
428 typedef struct I2CCC26XX_HWAttrsV1 {
430  I2CBaseAddrType baseAddr;
432  unsigned long powerMngrId;
434  int intNum;
448  uint8_t intPriority;
454  uint32_t swiPriority;
456  uint8_t sdaPin;
458  uint8_t sclPin;
460 
466 typedef struct I2CCC26XX_Object {
467  /* I2C control variables */
468  I2C_TransferMode transferMode;
469  I2C_CallbackFxn transferCallbackFxn;
470  volatile I2CCC26XX_Mode mode;
471  uint32_t bitRate;
473  /* I2C SYS/BIOS objects */
474  HwiP_Struct hwi;
475  SwiP_Struct swi;
476  SemaphoreP_Struct mutex;
477  SemaphoreP_Struct transferComplete;
479  /* PIN driver state object and handle */
480  PIN_State pinState;
481  PIN_Handle hPin;
482 
483  /* I2C current transaction */
484  I2C_Transaction *currentTransaction;
485  uint8_t *writeBufIdx;
486  unsigned int writeCountIdx;
487  uint8_t *readBufIdx;
488  unsigned int readCountIdx;
490  /* I2C transaction pointers for I2C_MODE_CALLBACK */
491  I2C_Transaction *headPtr;
492  I2C_Transaction *tailPtr;
494  /* I2C power notification */
495  void *i2cPostFxn;
496  Power_NotifyObj i2cPostObj;
498  bool isOpen;
499 } I2CCC26XX_Object;
500 
503 #ifdef __cplusplus
504 }
505 #endif
506 
507 #endif /* ti_drivers_i2c_I2CCC26XX__include */
uint8_t pinSDA
Definition: I2CCC26XX.h:369
int intNum
Definition: I2CCC26XX.h:434
This structure defines the I2C slave address, pointers to write and read buffers, and their associate...
Definition: I2C.h:411
struct I2CCC26XX_I2CPinCfg I2CCC26XX_I2CPinCfg
I2CCC26XX Pin Configuration.
struct I2CCC26XX_HWAttrsV1 I2CCC26XX_HWAttrsV1
I2CCC26XX Hardware attributes.
I2CCC26XX Pin Configuration.
Definition: I2CCC26XX.h:368
Power Manager interface.
Power notify object structure.
Definition: Power.h:121
I2CCC26XX Hardware attributes.
Definition: I2CCC26XX.h:428
uint8_t sclPin
Definition: I2CCC26XX.h:458
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:432
I2CBaseAddrType baseAddr
Definition: I2CCC26XX.h:430
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:370
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:475
uint8_t sdaPin
Definition: I2CCC26XX.h:456
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:454
Inter-Intergrated Circuit driver interface.
unsigned long I2CBaseAddrType
Definition: I2CCC26XX.h:339
uint8_t intPriority
I2C Peripheral&#39;s interrupt priority.
Definition: I2CCC26XX.h:448
Copyright 2018, Texas Instruments Incorporated