AM64x MCU+ SDK  08.04.00
i2c/v0/i2c.h
Go to the documentation of this file.
1 /*
2  * Copyright (C)2018-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 
123 #define I2C_MAX_NUM_OWN_SLAVE_ADDR (4U)
124 
135 #define I2C_MODE_BLOCKING ((uint8_t) 0U)
136 #define I2C_MODE_CALLBACK ((uint8_t) 1U)
137 
145 #define I2C_100KHZ ((uint8_t) 0U)
146 #define I2C_400KHZ ((uint8_t) 1U)
147 #define I2C_1P0MHZ ((uint8_t) 2U)
148 #define I2C_3P4MHZ ((uint8_t) 3U)
149 
156 #define I2C_STS_SUCCESS ( 0)
157 #define I2C_STS_ERR (-1)
158 #define I2C_STS_ERR_TIMEOUT (-2)
159 #define I2C_STS_ERR_BUS_BUSY (-3)
160 #define I2C_STS_ERR_NO_ACK (-4)
161 #define I2C_STS_ERR_ARBITRATION_LOST (-5)
162 #define I2C_STS_ERR_ACCESS_ERROR (-6)
163 #define I2C_STS_ERR_COMMAND_FAILURE (-7)
164 #define I2C_STS_ERR_INVALID_COMMAND (-8)
165 #define I2C_STS_RESTART (-9)
166 
168 /* ========================================================================== */
169 /* Structure Declarations */
170 /* ========================================================================== */
171 
175 typedef struct I2C_Config_s *I2C_Handle;
176 
180 typedef struct I2C_HwAttrs_s {
182  uint32_t baseAddr;
184  uint32_t intNum;
186  uint32_t eventId;
188  uint32_t funcClk;
195  uint32_t ownSlaveAddr[I2C_MAX_NUM_OWN_SLAVE_ADDR];
196 } I2C_HwAttrs;
197 
220 typedef struct I2C_Transaction_s {
221 
225  const void *writeBuf;
226 
230  size_t writeCount;
231 
235  void *readBuf;
236 
240  size_t readCount;
241 
247  uint32_t slaveAddress;
248 
250  void *nextPtr;
251 
253  void *arg;
254 
256  uint32_t timeout;
257 
260 
262  bool expandSA;
264 
284 typedef struct I2C_Params_s {
286  uint8_t transferMode;
288  void (*transferCallbackFxn)(I2C_Handle i2cHnd, I2C_Transaction * msg, int32_t transferStatus);
290  uint8_t bitRate;
291 } I2C_Params;
292 
298 typedef struct I2C_Object_s {
299 
309  uint8_t state;
314  uint8_t *writeBufIdx;
316  uint32_t writeCountIdx;
318  uint8_t *readBufIdx;
320  uint32_t readCountIdx;
321 
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 
332  uint32_t baseAddr;
334 } I2C_Object;
335 
336 
347 typedef struct I2C_Config_s {
352 } I2C_Config;
353 
370 typedef void (*I2C_CallbackFxn)(I2C_Handle handle, I2C_Transaction * msg, 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 
409 
419 int32_t I2C_transfer(I2C_Handle handle,
420  I2C_Transaction *transaction);
421 
430 int32_t I2C_probe(I2C_Handle handle, uint32_t slaveAddr);
431 
440 int32_t I2C_setBusFrequency(I2C_Handle handle, uint32_t busFrequency);
441 
451 int32_t I2C_recoverBus(I2C_Handle handle, uint32_t i2cDelay);
452 
458 void I2C_close(I2C_Handle handle);
459 
470 
471 #ifdef __cplusplus
472 }
473 #endif
474 
475 #endif /* I2C_H_ */
476 
I2C_Transaction::arg
void * arg
Definition: i2c/v0/i2c.h:253
I2C_Object
I2C Object.
Definition: i2c/v0/i2c.h:298
I2C_HwAttrs
I2C Hardware attributes.
Definition: i2c/v0/i2c.h:180
I2C_Object::tailPtr
I2C_Transaction * tailPtr
Definition: i2c/v0/i2c.h:326
index
uint16_t index
Definition: tisci_rm_proxy.h:3
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/v0/i2c.h:250
I2C_Object::readBufIdx
uint8_t * readBufIdx
Definition: i2c/v0/i2c.h:318
I2C_Object::transferComplete
SemaphoreP_Object transferComplete
Definition: i2c/v0/i2c.h:303
I2C_Config
I2C Global configuration.
Definition: i2c/v0/i2c.h:347
I2C_Object::isOpen
bool isOpen
Definition: i2c/v0/i2c.h:328
I2C_Transaction::slaveAddress
uint32_t slaveAddress
Definition: i2c/v0/i2c.h:247
I2C_close
void I2C_close(I2C_Handle handle)
Function to close the I2C.
I2C_Transaction::writeBuf
const void * writeBuf
Definition: i2c/v0/i2c.h:225
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/v0/i2c.h:324
I2C_Config::hwAttrs
I2C_HwAttrs const * hwAttrs
Definition: i2c/v0/i2c.h:351
I2C_recoverBus
int32_t I2C_recoverBus(I2C_Handle handle, uint32_t i2cDelay)
Function to recover the bus in case of error.
I2C_HwAttrs::enableIntr
bool enableIntr
Definition: i2c/v0/i2c.h:190
I2C_CallbackFxn
void(* I2C_CallbackFxn)(I2C_Handle handle, I2C_Transaction *msg, int32_t transferStatus)
I2C callback function.
Definition: i2c/v0/i2c.h:370
I2C_Object::intStatusErr
uint32_t intStatusErr
Definition: i2c/v0/i2c.h:330
I2C_Transaction
I2C transaction.
Definition: i2c/v0/i2c.h:220
I2C_Params
I2C Parameters.
Definition: i2c/v0/i2c.h:284
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/v0/i2c.h:314
I2C_Object::mutex
SemaphoreP_Object mutex
Definition: i2c/v0/i2c.h:301
I2C_probe
int32_t I2C_probe(I2C_Handle handle, uint32_t slaveAddr)
Function to probe I2C.
I2C_Object::baseAddr
uint32_t baseAddr
Definition: i2c/v0/i2c.h:332
I2C_Transaction::timeout
uint32_t timeout
Definition: i2c/v0/i2c.h:256
I2C_Transaction::expandSA
bool expandSA
Definition: i2c/v0/i2c.h:262
HwiP.h
I2C_Handle
struct I2C_Config_s * I2C_Handle
A handle that is returned from a I2C_open() call.
Definition: i2c/v0/i2c.h:175
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/v0/i2c.h:307
I2C_MAX_NUM_OWN_SLAVE_ADDR
#define I2C_MAX_NUM_OWN_SLAVE_ADDR
Definition: i2c/v0/i2c.h:123
I2C_Config::object
I2C_Object * object
Definition: i2c/v0/i2c.h:349
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/v0/i2c.h:184
I2C_Object::writeCountIdx
uint32_t writeCountIdx
Definition: i2c/v0/i2c.h:316
I2C_Object::readCountIdx
uint32_t readCountIdx
Definition: i2c/v0/i2c.h:320
I2C_Transaction::readBuf
void * readBuf
Definition: i2c/v0/i2c.h:235
HwiP_Object
Opaque Hwi object used with the Hwi APIs.
Definition: HwiP.h:91
I2C_Transaction::writeCount
size_t writeCount
Definition: i2c/v0/i2c.h:230
I2C_Transaction::masterMode
bool masterMode
Definition: i2c/v0/i2c.h:259
I2C_HwAttrs::baseAddr
uint32_t baseAddr
Definition: i2c/v0/i2c.h:182
SemaphoreP_Object
Opaque semaphore object used with the semaphore APIs.
Definition: SemaphoreP.h:59
I2C_HwAttrs::eventId
uint32_t eventId
Definition: i2c/v0/i2c.h:186
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/v0/i2c.h:188
I2C_Transaction::readCount
size_t readCount
Definition: i2c/v0/i2c.h:240
I2C_Object::state
uint8_t state
Definition: i2c/v0/i2c.h:309
I2C_init
void I2C_init(void)
Initialize the I2C module.
I2C_Params::bitRate
uint8_t bitRate
Definition: i2c/v0/i2c.h:290
I2C_Object::currentTransaction
I2C_Transaction * currentTransaction
Definition: i2c/v0/i2c.h:312
I2C_Params::transferMode
uint8_t transferMode
Definition: i2c/v0/i2c.h:286
I2C_Object::hwiObj
HwiP_Object hwiObj
Definition: i2c/v0/i2c.h:305