AM64x MCU+ SDK  09.02.01
i2c.h
Go to the documentation of this file.
1 /*
2  * Copyright (C)2018-2024 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 #include <kernel/dpl/ClockP.h>
120 /* ========================================================================== */
121 /* Macros & Typedefs */
122 /* ========================================================================== */
123 
124 #define I2C_MAX_NUM_OWN_TARGET_ADDR (4U)
125 
136 #define I2C_MODE_BLOCKING ((uint8_t) 0U)
137 #define I2C_MODE_CALLBACK ((uint8_t) 1U)
138 
150 #define I2C_MEM_TXN_DIR_INVALID ((uint8_t) 0U)
151 
152 #define I2C_MEM_TXN_DIR_TX ((uint8_t) 1U)
153 
154 #define I2C_MEM_TXN_DIR_RX ((uint8_t) 2U)
155 
158 /* ========================================================================== */
159 /* Structure Declarations */
160 /* ========================================================================== */
161 
165 typedef struct I2C_Config_s *I2C_Handle;
166 
170 typedef struct I2C_HwAttrs_s {
172  uint32_t baseAddr;
174  uint32_t intNum;
176  uint32_t eventId;
178  uint32_t funcClk;
185  uint32_t ownTargetAddr[I2C_MAX_NUM_OWN_TARGET_ADDR];
186 } I2C_HwAttrs;
187 
195 typedef struct {
196 
197  uint32_t memAddr;
199  uint8_t memAddrSize;
201  uint8_t *buffer;
203  uint32_t size;
205  uint8_t memDataDir;
208 
231 typedef struct I2C_Transaction_s {
232 
235  void *writeBuf;
238  size_t writeCount;
241  void *readBuf;
244  size_t readCount;
249  uint32_t targetAddress;
251  void *nextPtr;
253  void *arg;
255  uint32_t timeout;
260  bool expandSA;
267  int32_t status;
268 
270 
290 typedef struct I2C_Params_s {
292  uint8_t transferMode;
294  void (*transferCallbackFxn)(I2C_Handle i2cHnd, I2C_Transaction * msg,
295  int32_t transferStatus);
297  uint8_t bitRate;
298 } I2C_Params;
299 
305 typedef struct I2C_Object_s {
306 
316  uint8_t state;
320 /* I2C transaction pointers for I2C_MODE_CALLBACK */
321 /* Head ptr for queued transactions */
323 /* Tail ptr for queued transactions */
325 /* flag to indicate module is open */
326  bool isOpen;
327 /* interrupt status error */
328  uint32_t intStatusErr;
329 /* I2C driver object for lld */
332 
333 } I2C_Object;
334 
335 
346 typedef struct I2C_Config_s {
351 } I2C_Config;
352 
369 typedef void (*I2C_CallbackFxn)(I2C_Handle handle, I2C_Transaction * msg,
370  int32_t transferStatus);
371 
372 /* ========================================================================== */
373 /* Function Declarations */
374 /* ========================================================================== */
375 
379 void I2C_init(void);
380 
384 void I2C_deinit(void);
385 
392 
401 I2C_Handle I2C_open(uint32_t idx, const I2C_Params *params);
402 
412 void I2C_close(I2C_Handle handle);
413 
421 
428 
438 int32_t I2C_transfer(I2C_Handle handle, I2C_Transaction *transaction);
439 
448 int32_t I2C_probe(I2C_Handle handle, uint32_t targetAddr);
449 
458 int32_t I2C_setBusFrequency(I2C_Handle handle, uint32_t busFrequency);
459 
469 int32_t I2C_recoverBus(I2C_Handle handle, uint32_t i2cDelay);
470 
481 
482 #ifdef __cplusplus
483 }
484 #endif
485 
486 #endif /* I2C_H_ */
487 
I2C_Params_init
void I2C_Params_init(I2C_Params *params)
Function to set default values of I2C_Params in params.
I2C_Transaction::arg
void * arg
Definition: i2c.h:253
I2C_Mem_Transaction::memAddrSize
uint8_t memAddrSize
Definition: i2c.h:199
I2C_Mem_Transaction::buffer
uint8_t * buffer
Definition: i2c.h:201
I2C_Memory_Transaction_init
void I2C_Memory_Transaction_init(I2C_Mem_Transaction *memTransaction)
Function to set default values of I2C_Mem_Transaction in memTransaction.
I2C_Object
I2C Object.
Definition: i2c.h:305
I2C_Transaction::status
int32_t status
Definition: i2c.h:267
I2C_Transaction::writeBuf
void * writeBuf
Definition: i2c.h:235
I2C_HwAttrs
I2C Hardware attributes.
Definition: i2c.h:170
I2C_Object::tailPtr
I2C_Transaction * tailPtr
Definition: i2c.h:324
index
uint16_t index
Definition: tisci_rm_proxy.h:3
i2c_lld.h
I2C LLD Driver API/interface file.
I2C_probe
int32_t I2C_probe(I2C_Handle handle, uint32_t targetAddr)
Function to probe I2C.
I2C_Transaction::nextPtr
void * nextPtr
Definition: i2c.h:251
I2C_Object::transferComplete
SemaphoreP_Object transferComplete
Definition: i2c.h:310
I2C_Config
I2C Global configuration.
Definition: i2c.h:346
I2C_Object::isOpen
bool isOpen
Definition: i2c.h:326
I2C_Object::i2cLldObject
I2CLLD_Object i2cLldObject
Definition: i2c.h:330
SemaphoreP.h
I2C_Object::i2cLldHandle
I2CLLD_Handle i2cLldHandle
Definition: i2c.h:331
I2C_Object::headPtr
I2C_Transaction * headPtr
Definition: i2c.h:322
I2C_Config::hwAttrs
I2C_HwAttrs const * hwAttrs
Definition: i2c.h:350
I2C_Handle
struct I2C_Config_s * I2C_Handle
A handle that is returned from a I2C_open() call.
Definition: i2c.h:165
I2C_Mem_Transaction::memAddr
uint32_t memAddr
Definition: i2c.h:197
I2C_HwAttrs::enableIntr
bool enableIntr
Definition: i2c.h:180
I2C_recoverBus
int32_t I2C_recoverBus(I2C_Handle handle, uint32_t i2cDelay)
Function to recover the bus in case of error.
I2C_transfer
int32_t I2C_transfer(I2C_Handle handle, I2C_Transaction *transaction)
Function to initiate a transfer from I2C.
I2C_Transaction::targetAddress
uint32_t targetAddress
Definition: i2c.h:249
ClockP.h
I2C_Object::intStatusErr
uint32_t intStatusErr
Definition: i2c.h:328
I2C_Transaction::memTransaction
I2C_Mem_Transaction * memTransaction
Definition: i2c.h:265
I2C_Transaction
I2C transaction.
Definition: i2c.h:231
I2C_Params
I2C Parameters.
Definition: i2c.h:290
I2C_open
I2C_Handle I2C_open(uint32_t idx, const I2C_Params *params)
Open the I2C at index idx with parameters params.
I2C_Mem_Transaction::size
uint32_t size
Definition: i2c.h:203
I2C_Object::mutex
SemaphoreP_Object mutex
Definition: i2c.h:308
I2C_Mem_Transaction::memDataDir
uint8_t memDataDir
Definition: i2c.h:205
I2C_Transaction::timeout
uint32_t timeout
Definition: i2c.h:255
I2C_Transaction::expandSA
bool expandSA
Definition: i2c.h:260
HwiP.h
I2C_Object::i2cParams
I2C_Params i2cParams
Definition: i2c.h:314
I2C_Config::object
I2C_Object * object
Definition: i2c.h:348
I2C_HwAttrs::intNum
uint32_t intNum
Definition: i2c.h:174
I2C_Transaction_init
void I2C_Transaction_init(I2C_Transaction *transaction)
Function to set default values of I2C_Transaction in transaction.
I2C_Transaction::controllerMode
bool controllerMode
Definition: i2c.h:257
I2C_setBusFrequency
int32_t I2C_setBusFrequency(I2C_Handle handle, uint32_t busFrequency)
Function to set the bus frequency.
I2C_Transaction::readBuf
void * readBuf
Definition: i2c.h:241
I2C_deinit
void I2C_deinit(void)
De-nitialize the I2C module.
HwiP_Object
Opaque Hwi object used with the Hwi APIs.
Definition: HwiP.h:93
I2C_Transaction::writeCount
size_t writeCount
Definition: i2c.h:238
I2C_CallbackFxn
void(* I2C_CallbackFxn)(I2C_Handle handle, I2C_Transaction *msg, int32_t transferStatus)
I2C callback function.
Definition: i2c.h:369
I2C_Mem_Transaction
I2C Memory Transaction.
Definition: i2c.h:195
I2C_HwAttrs::baseAddr
uint32_t baseAddr
Definition: i2c.h:172
SemaphoreP_Object
Opaque semaphore object used with the semaphore APIs.
Definition: SemaphoreP.h:59
I2C_HwAttrs::eventId
uint32_t eventId
Definition: i2c.h:176
I2C_HwAttrs::funcClk
uint32_t funcClk
Definition: i2c.h:178
I2C_Transaction::memTxnEnable
bool memTxnEnable
Definition: i2c.h:263
I2C_Transaction::readCount
size_t readCount
Definition: i2c.h:244
I2CLLD_Object
I2C Driver Object.
Definition: i2c_lld.h:381
I2C_close
void I2C_close(I2C_Handle handle)
Function to close the I2C Peripheral specified by the handle.
I2C_Object::state
uint8_t state
Definition: i2c.h:316
I2C_Params::bitRate
uint8_t bitRate
Definition: i2c.h:297
I2C_Object::currentTransaction
I2C_Transaction * currentTransaction
Definition: i2c.h:319
I2C_Params::transferMode
uint8_t transferMode
Definition: i2c.h:292
I2C_init
void I2C_init(void)
Initialize the I2C module.
I2C_Object::hwiObj
HwiP_Object hwiObj
Definition: i2c.h:312
I2C_MAX_NUM_OWN_TARGET_ADDR
#define I2C_MAX_NUM_OWN_TARGET_ADDR
Definition: i2c.h:124
I2C_getHandle
I2C_Handle I2C_getHandle(uint32_t index)
This function returns the handle of an open I2C instance from the instance index.