AM273x MCU+ SDK  08.03.00
i2c/v1/i2c.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2021 Texas Instruments Incorporated
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * 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
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
102 #ifndef I2C_H_
103 #define I2C_H_
104 
105 /* ========================================================================== */
106 /* Include Files */
107 /* ========================================================================== */
108 
109 #ifdef __cplusplus
110 extern "C" {
111 #endif
112 
113 #include <stdint.h>
114 #include <stdbool.h>
115 #include <stddef.h>
116 #include <kernel/dpl/SemaphoreP.h>
117 #include <kernel/dpl/HwiP.h>
118 
119 /* ========================================================================== */
120 /* Macros & Typedefs */
121 /* ========================================================================== */
122 
133 #define I2C_MODE_BLOCKING ((uint8_t) 0U)
134 #define I2C_MODE_CALLBACK ((uint8_t) 1U)
135 
143 #define I2C_100KHZ ((uint8_t) 0U)
144 #define I2C_400KHZ ((uint8_t) 1U)
145 #define I2C_1P0MHZ ((uint8_t) 2U)
146 #define I2C_3P4MHZ ((uint8_t) 3U)
147 
154 #define I2C_STS_SUCCESS ( 0)
155 #define I2C_STS_ERR (-1)
156 #define I2C_STS_ERR_TIMEOUT (-2)
157 #define I2C_STS_ERR_BUS_BUSY (-3)
158 #define I2C_STS_ERR_NO_ACK (-4)
159 #define I2C_STS_ERR_ARBITRATION_LOST (-5)
160 #define I2C_STS_ERR_ACCESS_ERROR (-6)
161 #define I2C_STS_ERR_COMMAND_FAILURE (-7)
162 #define I2C_STS_ERR_INVALID_COMMAND (-8)
163 #define I2C_STS_RESTART (-9)
164 
166 /* ========================================================================== */
167 /* Structure Declarations */
168 /* ========================================================================== */
169 
173 typedef struct I2C_Config_s *I2C_Handle;
174 
178 typedef struct I2C_HwAttrs_s {
180  uint32_t baseAddr;
182  uint32_t intNum;
184  uint32_t eventId;
186  uint32_t funcClk;
190  uint32_t ownSlaveAddr;
191 } I2C_HwAttrs;
192 
215 typedef struct I2C_Transaction_s {
216 
220  const void *writeBuf;
221 
225  size_t writeCount;
226 
230  void *readBuf;
231 
235  size_t readCount;
236 
242  uint32_t slaveAddress;
243 
245  void *nextPtr;
246 
248  void *arg;
249 
251  uint32_t timeout;
252 
255 
257  bool expandSA;
259 
279 typedef struct I2C_Params_s {
281  uint8_t transferMode;
283  void (*transferCallbackFxn)(I2C_Handle i2cHnd, I2C_Transaction * msg, int32_t transferStatus);
285  uint8_t bitRate;
286 } I2C_Params;
287 
293 typedef struct I2C_Object_s {
294 
304  uint8_t state;
309  uint8_t *writeBufIdx;
311  uint32_t writeCountIdx;
313  uint8_t *readBufIdx;
315  uint32_t readCountIdx;
316 
317 /* I2C transaction pointers for I2C_MODE_CALLBACK */
318 /* Head ptr for queued transactions */
320 /* Tail ptr for queued transactions */
322 /* flag to indicate module is open */
323  bool isOpen;
324 /* interrupt status error */
325  uint32_t intStatusErr;
326 
327  uint32_t baseAddr;
329 } I2C_Object;
330 
331 
342 typedef struct I2C_Config_s {
347 } I2C_Config;
348 
365 typedef void (*I2C_CallbackFxn)(I2C_Handle handle, I2C_Transaction * msg, int32_t transferStatus);
366 
367 /* ========================================================================== */
368 /* Function Declarations */
369 /* ========================================================================== */
370 
374 void I2C_init(void);
375 
379 void I2C_deinit(void);
380 
387 
396 I2C_Handle I2C_open(uint32_t idx, const I2C_Params *params);
397 
404 
414 int32_t I2C_transfer(I2C_Handle handle,
415  I2C_Transaction *transaction);
416 
425 int32_t I2C_probe(I2C_Handle handle, uint32_t slaveAddr);
426 
435 int32_t I2C_setBusFrequency(I2C_Handle handle, uint32_t busFrequency);
436 
442 void I2C_close(I2C_Handle handle);
443 
453 I2C_Handle I2C_getHandle(uint32_t index);
454 
455 #ifdef __cplusplus
456 }
457 #endif
458 
459 #endif /* I2C_H_ */
460 
I2C_Transaction::arg
void * arg
Definition: i2c/v1/i2c.h:248
I2C_Object
I2C Object.
Definition: i2c/v1/i2c.h:293
I2C_HwAttrs
I2C Hardware attributes.
Definition: i2c/v1/i2c.h:178
I2C_Object::tailPtr
I2C_Transaction * tailPtr
Definition: i2c/v1/i2c.h:321
I2C_transfer
int32_t I2C_transfer(I2C_Handle handle, I2C_Transaction *transaction)
Function to initiate a transfer from I2C.
I2C_Transaction::nextPtr
void * nextPtr
Definition: i2c/v1/i2c.h:245
I2C_Object::readBufIdx
uint8_t * readBufIdx
Definition: i2c/v1/i2c.h:313
I2C_Object::transferComplete
SemaphoreP_Object transferComplete
Definition: i2c/v1/i2c.h:298
I2C_Config
I2C Global configuration.
Definition: i2c/v1/i2c.h:342
I2C_Object::isOpen
bool isOpen
Definition: i2c/v1/i2c.h:323
I2C_Transaction::slaveAddress
uint32_t slaveAddress
Definition: i2c/v1/i2c.h:242
I2C_close
void I2C_close(I2C_Handle handle)
Function to close the I2C.
I2C_Transaction::writeBuf
const void * writeBuf
Definition: i2c/v1/i2c.h:220
SemaphoreP.h
I2C_Transaction_init
void I2C_Transaction_init(I2C_Transaction *transaction)
Function to set default values of I2C_Transaction in transaction.
I2C_Object::headPtr
I2C_Transaction * headPtr
Definition: i2c/v1/i2c.h:319
I2C_Config::hwAttrs
I2C_HwAttrs const * hwAttrs
Definition: i2c/v1/i2c.h:346
I2C_HwAttrs::ownSlaveAddr
uint32_t ownSlaveAddr
Definition: i2c/v1/i2c.h:190
I2C_HwAttrs::enableIntr
bool enableIntr
Definition: i2c/v1/i2c.h:188
I2C_CallbackFxn
void(* I2C_CallbackFxn)(I2C_Handle handle, I2C_Transaction *msg, int32_t transferStatus)
I2C callback function.
Definition: i2c/v1/i2c.h:365
I2C_Object::intStatusErr
uint32_t intStatusErr
Definition: i2c/v1/i2c.h:325
I2C_Transaction
I2C transaction.
Definition: i2c/v1/i2c.h:215
I2C_Params
I2C Parameters.
Definition: i2c/v1/i2c.h:279
I2C_open
I2C_Handle I2C_open(uint32_t idx, const I2C_Params *params)
Open the I2C at index idx with parameters params.
I2C_deinit
void I2C_deinit(void)
De-nitialize the I2C module.
I2C_Object::writeBufIdx
uint8_t * writeBufIdx
Definition: i2c/v1/i2c.h:309
I2C_Object::mutex
SemaphoreP_Object mutex
Definition: i2c/v1/i2c.h:296
I2C_probe
int32_t I2C_probe(I2C_Handle handle, uint32_t slaveAddr)
Function to probe I2C.
I2C_Object::baseAddr
uint32_t baseAddr
Definition: i2c/v1/i2c.h:327
I2C_Transaction::timeout
uint32_t timeout
Definition: i2c/v1/i2c.h:251
I2C_Transaction::expandSA
bool expandSA
Definition: i2c/v1/i2c.h:257
HwiP.h
I2C_Handle
struct I2C_Config_s * I2C_Handle
A handle that is returned from a I2C_open() call.
Definition: i2c/v1/i2c.h:173
I2C_setBusFrequency
int32_t I2C_setBusFrequency(I2C_Handle handle, uint32_t busFrequency)
Function to set the bus frequency.
I2C_Object::i2cParams
I2C_Params i2cParams
Definition: i2c/v1/i2c.h:302
I2C_Config::object
I2C_Object * object
Definition: i2c/v1/i2c.h:344
I2C_getHandle
I2C_Handle I2C_getHandle(uint32_t index)
This function returns the handle of an open I2C instance from the instance index.
I2C_HwAttrs::intNum
uint32_t intNum
Definition: i2c/v1/i2c.h:182
I2C_Object::writeCountIdx
uint32_t writeCountIdx
Definition: i2c/v1/i2c.h:311
I2C_Object::readCountIdx
uint32_t readCountIdx
Definition: i2c/v1/i2c.h:315
I2C_Transaction::readBuf
void * readBuf
Definition: i2c/v1/i2c.h:230
HwiP_Object
Opaque Hwi object used with the Hwi APIs.
Definition: HwiP.h:91
I2C_Transaction::writeCount
size_t writeCount
Definition: i2c/v1/i2c.h:225
I2C_Transaction::masterMode
bool masterMode
Definition: i2c/v1/i2c.h:254
I2C_HwAttrs::baseAddr
uint32_t baseAddr
Definition: i2c/v1/i2c.h:180
SemaphoreP_Object
Opaque semaphore object used with the semaphore APIs.
Definition: SemaphoreP.h:59
I2C_HwAttrs::eventId
uint32_t eventId
Definition: i2c/v1/i2c.h:184
I2C_Params_init
void I2C_Params_init(I2C_Params *params)
Function to set default values of I2C_Params in params.
I2C_HwAttrs::funcClk
uint32_t funcClk
Definition: i2c/v1/i2c.h:186
I2C_Transaction::readCount
size_t readCount
Definition: i2c/v1/i2c.h:235
I2C_Object::state
uint8_t state
Definition: i2c/v1/i2c.h:304
I2C_init
void I2C_init(void)
Initialize the I2C module.
I2C_Params::bitRate
uint8_t bitRate
Definition: i2c/v1/i2c.h:285
I2C_Object::currentTransaction
I2C_Transaction * currentTransaction
Definition: i2c/v1/i2c.h:307
I2C_Params::transferMode
uint8_t transferMode
Definition: i2c/v1/i2c.h:281
I2C_Object::hwiObj
HwiP_Object hwiObj
Definition: i2c/v1/i2c.h:300