AM243x MCU+ SDK  11.01.00
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;
182  uint8_t intrPriority;
187  uint32_t ownTargetAddr[I2C_MAX_NUM_OWN_TARGET_ADDR];
188 } I2C_HwAttrs;
189 
197 typedef struct {
198 
199  uint32_t memAddr;
201  uint8_t memAddrSize;
203  uint8_t *buffer;
205  uint32_t size;
207  uint8_t memDataDir;
210 
233 typedef struct I2C_Transaction_s {
234 
237  void *writeBuf;
240  size_t writeCount;
243  void *readBuf;
246  size_t readCount;
251  uint32_t targetAddress;
253  void *nextPtr;
255  void *arg;
257  uint32_t timeout;
262  bool expandSA;
269  int32_t status;
270 
272 
292 typedef struct I2C_Params_s {
294  uint8_t transferMode;
296  void (*transferCallbackFxn)(I2C_Handle i2cHnd, I2C_Transaction * msg,
297  int32_t transferStatus);
299  uint8_t bitRate;
300 } I2C_Params;
301 
307 typedef struct I2C_Object_s {
308 
318  uint8_t state;
322 /* I2C transaction pointers for I2C_MODE_CALLBACK */
323 /* Head ptr for queued transactions */
325 /* Tail ptr for queued transactions */
327 /* flag to indicate module is open */
328  bool isOpen;
329 /* interrupt status error */
330  uint32_t intStatusErr;
331 /* I2C driver object for lld */
334 
335 } I2C_Object;
336 
337 
348 typedef struct I2C_Config_s {
353 } I2C_Config;
354 
371 typedef void (*I2C_CallbackFxn)(I2C_Handle handle, I2C_Transaction * msg,
372  int32_t transferStatus);
373 
374 /* ========================================================================== */
375 /* Function Declarations */
376 /* ========================================================================== */
377 
381 void I2C_init(void);
382 
386 void I2C_deinit(void);
387 
394 
403 I2C_Handle I2C_open(uint32_t idx, const I2C_Params *params);
404 
414 void I2C_close(I2C_Handle handle);
415 
423 
430 
440 int32_t I2C_transfer(I2C_Handle handle, I2C_Transaction *transaction);
441 
450 int32_t I2C_probe(I2C_Handle handle, uint32_t targetAddr);
451 
460 int32_t I2C_setBusFrequency(I2C_Handle handle, uint32_t busFrequency);
461 
471 int32_t I2C_recoverBus(I2C_Handle handle, uint32_t i2cDelay);
472 
483 
484 #ifdef __cplusplus
485 }
486 #endif
487 
488 #endif /* I2C_H_ */
489 
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:255
I2C_Mem_Transaction::memAddrSize
uint8_t memAddrSize
Definition: i2c.h:201
I2C_Mem_Transaction::buffer
uint8_t * buffer
Definition: i2c.h:203
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:307
I2C_Transaction::status
int32_t status
Definition: i2c.h:269
I2C_Transaction::writeBuf
void * writeBuf
Definition: i2c.h:237
I2C_HwAttrs
I2C Hardware attributes.
Definition: i2c.h:170
I2C_Object::tailPtr
I2C_Transaction * tailPtr
Definition: i2c.h:326
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:253
I2C_Object::transferComplete
SemaphoreP_Object transferComplete
Definition: i2c.h:312
I2C_Config
I2C Global configuration.
Definition: i2c.h:348
I2C_Object::isOpen
bool isOpen
Definition: i2c.h:328
I2C_Object::i2cLldObject
I2CLLD_Object i2cLldObject
Definition: i2c.h:332
I2C_HwAttrs::intrPriority
uint8_t intrPriority
Definition: i2c.h:182
SemaphoreP.h
I2C_Object::i2cLldHandle
I2CLLD_Handle i2cLldHandle
Definition: i2c.h:333
I2C_Object::headPtr
I2C_Transaction * headPtr
Definition: i2c.h:324
I2C_Config::hwAttrs
I2C_HwAttrs const * hwAttrs
Definition: i2c.h:352
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:199
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:251
ClockP.h
I2C_Object::intStatusErr
uint32_t intStatusErr
Definition: i2c.h:330
I2C_Transaction::memTransaction
I2C_Mem_Transaction * memTransaction
Definition: i2c.h:267
I2C_Transaction
I2C transaction.
Definition: i2c.h:233
I2C_Params
I2C Parameters.
Definition: i2c.h:292
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:205
I2C_Object::mutex
SemaphoreP_Object mutex
Definition: i2c.h:310
I2C_Mem_Transaction::memDataDir
uint8_t memDataDir
Definition: i2c.h:207
I2C_Transaction::timeout
uint32_t timeout
Definition: i2c.h:257
I2C_Transaction::expandSA
bool expandSA
Definition: i2c.h:262
HwiP.h
I2C_Object::i2cParams
I2C_Params i2cParams
Definition: i2c.h:316
I2C_Config::object
I2C_Object * object
Definition: i2c.h:350
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:259
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:243
I2C_deinit
void I2C_deinit(void)
De-nitialize the I2C module.
HwiP_Object
Opaque Hwi object used with the Hwi APIs.
Definition: HwiP.h:89
I2C_Transaction::writeCount
size_t writeCount
Definition: i2c.h:240
I2C_CallbackFxn
void(* I2C_CallbackFxn)(I2C_Handle handle, I2C_Transaction *msg, int32_t transferStatus)
I2C callback function.
Definition: i2c.h:371
I2C_Mem_Transaction
I2C Memory Transaction.
Definition: i2c.h:197
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:265
I2C_Transaction::readCount
size_t readCount
Definition: i2c.h:246
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:318
I2C_Params::bitRate
uint8_t bitRate
Definition: i2c.h:299
I2C_Object::currentTransaction
I2C_Transaction * currentTransaction
Definition: i2c.h:321
I2C_Params::transferMode
uint8_t transferMode
Definition: i2c.h:294
I2C_init
void I2C_init(void)
Initialize the I2C module.
I2C_Object::hwiObj
HwiP_Object hwiObj
Definition: i2c.h:314
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.