AM62Ax MCU+ SDK  10.01.00
i2c_lld.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 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 
54 #ifndef I2C_LLD_H_
55 #define I2C_LLD_H_
56 
57 /* ========================================================================== */
58 /* Include Files */
59 /* ========================================================================== */
60 
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64 
65 #include <stdint.h>
66 #include <stdbool.h>
67 #include <stdlib.h>
68 #include <stddef.h>
69 #include <drivers/i2c/v0/cslr_i2c.h>
70 #include <drivers/hw_include/soc_config.h>
71 #if defined (DRV_VERSION_I2C_V0)
72 #include <drivers/i2c/v0/soc/i2c_soc.h>
73 #endif
74 
75 /* ========================================================================== */
76 /* Macros & Typedefs */
77 /* ========================================================================== */
78 
79 #define I2C_MAX_NUM_OWN_TARGET_ADDR (4U)
80 
88 #define I2C_STS_SUCCESS ((int32_t) 0)
89 
91 #define I2C_STS_ERR ((int32_t)-1)
92 
94 #define I2C_STS_ERR_TIMEOUT ((int32_t)-2)
95 
96 #define I2C_STS_ERR_INVALID_PARAM ((int32_t)-3)
97 
98 #define I2C_STS_ERR_BUS_BUSY ((int32_t)-4)
99 
101 #define I2C_STS_ERR_NO_ACK ((int32_t)-5)
102 
104 #define I2C_STS_ERR_ARBITRATION_LOST ((int32_t)-6)
105 
106 #define I2C_STS_ERR_ACCESS_ERROR ((int32_t)-7)
107 
108 #define I2C_STS_RESTART ((int32_t)-8)
109 
120 #define I2C_NO_WAIT ((uint32_t)0)
121 
123 #define I2C_WAIT_FOREVER ((uint32_t)-1)
124 
133 #define I2C_STATE_RESET ((uint8_t) 0U)
134 
136 #define I2C_STATE_IDLE ((uint8_t) 1U)
137 
139 #define I2C_STATE_BUSY ((uint8_t) 2U)
140 
142 #define I2C_STATE_ERROR ((uint8_t) 3U)
143 
154 #define I2C_100KHZ ((uint8_t) 0U)
155 
156 #define I2C_400KHZ ((uint8_t) 1U)
157 
158 #define I2C_1P0MHZ ((uint8_t) 2U)
159 
160 #define I2C_3P4MHZ ((uint8_t) 3U)
161 
171 #define I2C_MEM_ADDR_SIZE_8_BITS ((uint8_t) 1U)
172 
173 #define I2C_MEM_ADDR_SIZE_16_BITS ((uint8_t) 2U)
174 
184 #define I2C_WRITE_STATE ((uint8_t) 10U)
185 
186 #define I2C_READ_STATE ((uint8_t) 11U)
187 
188 #define I2C_TARGET_XFER_STATE ((uint8_t) 12U)
189 
190 #define I2C_TARGET_RESTART_STATE ((uint8_t) 13U)
191 
194 /* ========================================================================== */
195 /* Structure Declarations */
196 /* ========================================================================== */
197 
208 typedef struct {
209 
211  uint8_t *writeBuf;
213  uint32_t writeCount;
215  uint8_t *readBuf;
217  uint32_t readCount;
218 
220 
230 typedef struct {
231 
235  uint32_t txnCount;
240  uint32_t targetAddress;
242  void *arg;
244  uint32_t timeout;
249  bool expandSA;
250 
252 
257 typedef struct {
258 
259  uint32_t deviceAddress;
261  uint8_t *buffer;
263  uint32_t size;
265  bool expandSA;
270 
275 typedef struct {
276 
279  uint32_t memAddr;
281  uint8_t memAddrSize;
285 
286 
303 typedef struct {
304 
306  uint8_t *writeBuf;
308  uint32_t writeCount;
310  uint8_t *readBuf;
312  uint32_t readCount;
314  uint32_t timeout;
317  bool expandSA;
318 
320 
321 /* ========================================================================== */
322 /* Function pointers Declarations */
323 /* ========================================================================== */
324 
332 typedef uint32_t (*I2C_Clock_getTicks) (void);
333 
343 typedef uint64_t (*I2C_Clock_usecToTicks) (uint64_t usecs);
344 
352 typedef void (*I2C_Clock_uSleep) (uint64_t usec);
353 
362 typedef void (*I2C_lld_transferCompleteCallback) (void *args,
363  const I2CLLD_Message *msg,
364  int32_t transferStatus);
374 typedef void (*I2C_lld_targetTransferCompleteCallback) (void *args,
375  const I2CLLD_targetTransaction * targetTxn,
376  int32_t transferStatus);
377 
378 /* ========================================================================== */
379 /* Structure Declarations */
380 /* ========================================================================== */
381 
385 typedef struct {
386 
392  uint32_t baseAddr;
394  uint32_t intrNum;
396  uint8_t bitRate;
398  uint32_t funcClk;
400  uint32_t ownTargetAddr[I2C_MAX_NUM_OWN_TARGET_ADDR];
411 
417  uint8_t state;
419  uint32_t intStatusErr;
420 
430  uint8_t *writeBufIdx;
432  uint32_t writeCountIdx;
434  uint8_t *readBufIdx;
436  uint32_t readCountIdx;
438  uint32_t startTicks;
440  uint32_t currentTxnCount;
446  uint8_t *dataArray;
448  uint8_t addBuff[2];
450  uint8_t memAddrSize;
457  void* args;
458 
460 
461 /* ========================================================================== */
462 /* Function Declarations */
463 /* ========================================================================== */
464 
472 int32_t I2C_lld_init(I2CLLD_Handle handle);
473 
482 
491 
500 
513  I2C_ExtendedParams *extendedParams,
514  uint32_t timeout);
515 
526  I2C_ExtendedParams *extendedParams);
527 
540  I2C_ExtendedParams *extendedParams,
541  uint32_t timeout);
542 
553  I2C_ExtendedParams *extendedParams);
554 
567  I2C_Memory_ExtendedParams *mem_extendedParams,
568  uint32_t timeout);
569 
580  I2C_Memory_ExtendedParams *mem_extendedParams);
581 
594  I2C_Memory_ExtendedParams *mem_extendedParams,
595  uint32_t timeout);
596 
607  I2C_Memory_ExtendedParams *mem_extendedParams);
608 
620 
632 
645 
654 int32_t I2C_lld_probe(I2CLLD_Handle handle, uint32_t targetAddr);
655 
664 int32_t I2C_lld_setBusFrequency(I2CLLD_Handle handle, uint32_t busFrequency);
665 
675 int32_t I2C_lld_recoverBus(I2CLLD_Handle handle, uint32_t i2cDelay);
676 
677 /* ========================================================================== */
678 /* ISR Function Declarations */
679 /* ========================================================================== */
680 
687 void I2C_lld_controllerIsr(void *args);
688 
695 void I2C_lld_targetIsr(void *args);
696 
697 #ifdef __cplusplus
698 }
699 #endif
700 
701 #endif /* I2C_LLD_H_ */
702 
I2C_Clock_usecToTicks
uint64_t(* I2C_Clock_usecToTicks)(uint64_t usecs)
The definition of a micro seconds to ticks function used by the I2C driver to get ticks from microsec...
Definition: i2c_lld.h:343
I2CLLD_Object::Clock_getTicks
I2C_Clock_getTicks Clock_getTicks
Definition: i2c_lld.h:402
I2C_MAX_NUM_OWN_TARGET_ADDR
#define I2C_MAX_NUM_OWN_TARGET_ADDR
Definition: i2c_lld.h:79
I2CLLD_Object::startTicks
uint32_t startTicks
Definition: i2c_lld.h:438
I2CLLD_Message::txn
I2CLLD_Transaction * txn
Definition: i2c_lld.h:233
I2CLLD_Transaction::readCount
uint32_t readCount
Definition: i2c_lld.h:217
I2C_lld_Transaction_init
int32_t I2C_lld_Transaction_init(I2CLLD_Transaction *transaction)
API to set default values of I2CLLD_Transaction in transaction.
I2CLLD_Object::currentTargetTransaction
I2CLLD_targetTransaction * currentTargetTransaction
Definition: i2c_lld.h:428
I2CLLD_Object::writeCountIdx
uint32_t writeCountIdx
Definition: i2c_lld.h:432
I2CLLD_targetTransaction::writeBuf
uint8_t * writeBuf
Definition: i2c_lld.h:306
I2CLLD_Object::writeBufIdx
uint8_t * writeBufIdx
Definition: i2c_lld.h:430
I2CLLD_targetTransaction::writeCount
uint32_t writeCount
Definition: i2c_lld.h:308
I2C_lld_Message_init
int32_t I2C_lld_Message_init(I2CLLD_Message *msg)
API to set default values of I2CLLD_Message in msg.
I2CLLD_targetTransaction::readCount
uint32_t readCount
Definition: i2c_lld.h:312
I2CLLD_Message::targetAddress
uint32_t targetAddress
Definition: i2c_lld.h:240
I2C_lld_targetTransferCompleteCallback
void(* I2C_lld_targetTransferCompleteCallback)(void *args, const I2CLLD_targetTransaction *targetTxn, int32_t transferStatus)
The definition of a transfer completion callback function used by the I2C driver when used in Target ...
Definition: i2c_lld.h:374
I2C_lld_write
int32_t I2C_lld_write(I2CLLD_Handle handle, I2C_ExtendedParams *extendedParams, uint32_t timeout)
API to initiate the write transaction in polled mode.
I2C_Clock_uSleep
void(* I2C_Clock_uSleep)(uint64_t usec)
The definition of a sleep function used by the I2C driver for delay.
Definition: i2c_lld.h:352
I2CLLD_Object::funcClk
uint32_t funcClk
Definition: i2c_lld.h:398
I2CLLD_Message
I2C Message.
Definition: i2c_lld.h:230
I2CLLD_Object::Clock_uSleep
I2C_Clock_uSleep Clock_uSleep
Definition: i2c_lld.h:406
I2C_Memory_ExtendedParams::memAddr
uint32_t memAddr
Definition: i2c_lld.h:279
I2CLLD_Message::arg
void * arg
Definition: i2c_lld.h:242
I2CLLD_targetTransaction::readBuf
uint8_t * readBuf
Definition: i2c_lld.h:310
I2C_lld_read
int32_t I2C_lld_read(I2CLLD_Handle handle, I2C_ExtendedParams *extendedParams, uint32_t timeout)
API to initiate the read transaction in polled mode.
I2CLLD_Transaction::writeBuf
uint8_t * writeBuf
Definition: i2c_lld.h:211
I2C_lld_transferCompleteCallback
void(* I2C_lld_transferCompleteCallback)(void *args, const I2CLLD_Message *msg, int32_t transferStatus)
The definition of a transfer completion callback function used by the I2C driver when used in Control...
Definition: i2c_lld.h:362
I2CLLD_Transaction::readBuf
uint8_t * readBuf
Definition: i2c_lld.h:215
I2CLLD_Message::timeout
uint32_t timeout
Definition: i2c_lld.h:244
I2CLLD_Object::i2ctxn
I2CLLD_Transaction i2ctxn
Definition: i2c_lld.h:444
I2CLLD_Message::expandSA
bool expandSA
Definition: i2c_lld.h:249
I2C_lld_readIntr
int32_t I2C_lld_readIntr(I2CLLD_Handle handle, I2C_ExtendedParams *extendedParams)
API to initiate the read transaction in Interrupt mode.
I2C_lld_recoverBus
int32_t I2C_lld_recoverBus(I2CLLD_Handle handle, uint32_t i2cDelay)
Function to recover the bus in case of lockup.
I2C_ExtendedParams::expandSA
bool expandSA
Definition: i2c_lld.h:265
I2CLLD_Object::currentMsg
I2CLLD_Message * currentMsg
Definition: i2c_lld.h:426
I2CLLD_Transaction
I2C Transaction.
Definition: i2c_lld.h:208
I2C_ExtendedParams::buffer
uint8_t * buffer
Definition: i2c_lld.h:261
I2C_lld_mem_read
int32_t I2C_lld_mem_read(I2CLLD_Handle handle, I2C_Memory_ExtendedParams *mem_extendedParams, uint32_t timeout)
Function to initiate a transfer from I2C in interrupt mode.
I2C_lld_mem_write
int32_t I2C_lld_mem_write(I2CLLD_Handle handle, I2C_Memory_ExtendedParams *mem_extendedParams, uint32_t timeout)
Function to initiate a transfer from I2C in interrupt mode.
I2CLLD_targetTransaction::timeout
uint32_t timeout
Definition: i2c_lld.h:314
I2CLLD_Object::dataArray
uint8_t * dataArray
Definition: i2c_lld.h:446
I2CLLD_Object::intrNum
uint32_t intrNum
Definition: i2c_lld.h:394
I2CLLD_Object::baseAddr
uint32_t baseAddr
Definition: i2c_lld.h:392
I2C_lld_transferIntr
int32_t I2C_lld_transferIntr(I2CLLD_Handle handle, I2CLLD_Message *msg)
Function to initiate a transfer from I2C in interrupt mode.
I2CLLD_targetTransaction::expandSA
bool expandSA
Definition: i2c_lld.h:317
I2C_lld_targetIsr
void I2C_lld_targetIsr(void *args)
This is the I2C Target ISR and can be used as IRQ handler in Target mode.
I2CLLD_Message::txnCount
uint32_t txnCount
Definition: i2c_lld.h:235
I2C_lld_transferPoll
int32_t I2C_lld_transferPoll(I2CLLD_Handle handle, I2CLLD_Message *msg)
Function to initiate a transfer from I2C in polled mode.
I2CLLD_Object::readCountIdx
uint32_t readCountIdx
Definition: i2c_lld.h:436
I2C_lld_init
int32_t I2C_lld_init(I2CLLD_Handle handle)
This API Initializes the I2C instance.
I2CLLD_Object::transferCompleteCallback
I2C_lld_transferCompleteCallback transferCompleteCallback
Definition: i2c_lld.h:408
I2C_Memory_ExtendedParams::extendedParams
I2C_ExtendedParams extendedParams
Definition: i2c_lld.h:277
I2CLLD_Transaction::writeCount
uint32_t writeCount
Definition: i2c_lld.h:213
I2C_Clock_getTicks
uint32_t(* I2C_Clock_getTicks)(void)
The definition of a get System Tick function used by the I2C driver to keep track of time.
Definition: i2c_lld.h:332
I2CLLD_Object::memTxnActive
bool memTxnActive
Definition: i2c_lld.h:453
I2CLLD_Object::memAddrSize
uint8_t memAddrSize
Definition: i2c_lld.h:450
I2CLLD_Message::controllerMode
bool controllerMode
Definition: i2c_lld.h:246
I2C_Memory_ExtendedParams
Data structure used with I2C_lld_mem_write(), I2C_lld_mem_writeIntr(), I2C_lld_mem_read(),...
Definition: i2c_lld.h:275
I2C_ExtendedParams::deviceAddress
uint32_t deviceAddress
Definition: i2c_lld.h:259
I2C_lld_writeIntr
int32_t I2C_lld_writeIntr(I2CLLD_Handle handle, I2C_ExtendedParams *extendedParams)
API to initiate the write transaction in Interrupt mode.
I2CLLD_Object::intStatusErr
uint32_t intStatusErr
Definition: i2c_lld.h:419
I2C_ExtendedParams
Data structure used with I2C_lld_write(), I2C_lld_writeIntr(), I2C_lld_read(), I2C_lld_readIntr()
Definition: i2c_lld.h:257
I2C_lld_mem_readIntr
int32_t I2C_lld_mem_readIntr(I2CLLD_Handle handle, I2C_Memory_ExtendedParams *mem_extendedParams)
Function to initiate a transfer from I2C in interrupt mode.
I2CLLD_Object::readBufIdx
uint8_t * readBufIdx
Definition: i2c_lld.h:434
I2CLLD_Object::bitRate
uint8_t bitRate
Definition: i2c_lld.h:396
I2C_lld_probe
int32_t I2C_lld_probe(I2CLLD_Handle handle, uint32_t targetAddr)
Function to probe I2C.
I2C_lld_setBusFrequency
int32_t I2C_lld_setBusFrequency(I2CLLD_Handle handle, uint32_t busFrequency)
Function to set the bus frequency.
I2C_lld_mem_writeIntr
int32_t I2C_lld_mem_writeIntr(I2CLLD_Handle handle, I2C_Memory_ExtendedParams *mem_extendedParams)
Function to initiate a transfer from I2C in interrupt mode.
I2C_lld_controllerIsr
void I2C_lld_controllerIsr(void *args)
This is the I2C Controller ISR and can be used as IRQ handler in Controller mode.
I2CLLD_Object::targetTransferCompleteCallback
I2C_lld_targetTransferCompleteCallback targetTransferCompleteCallback
Definition: i2c_lld.h:410
I2C_lld_deInit
int32_t I2C_lld_deInit(I2CLLD_Handle handle)
This API De-Initializes the I2C instance.
I2CLLD_Object::state
uint8_t state
Definition: i2c_lld.h:417
I2C_ExtendedParams::size
uint32_t size
Definition: i2c_lld.h:263
I2CLLD_Object::i2cTargetTransaction
I2CLLD_targetTransaction i2cTargetTransaction
Definition: i2c_lld.h:455
I2CLLD_Object
I2C Driver Object.
Definition: i2c_lld.h:385
I2CLLD_Object::Clock_usecToTicks
I2C_Clock_usecToTicks Clock_usecToTicks
Definition: i2c_lld.h:404
I2CLLD_Object::i2cMsg
I2CLLD_Message i2cMsg
Definition: i2c_lld.h:442
I2CLLD_targetTransaction
I2C Target Transaction.
Definition: i2c_lld.h:303
I2CLLD_Object::args
void * args
Definition: i2c_lld.h:457
I2CLLD_Handle
struct I2CLLD_Object * I2CLLD_Handle
I2C_lld_targetTransferIntr
int32_t I2C_lld_targetTransferIntr(I2CLLD_Handle handle, I2CLLD_targetTransaction *txn)
Function to initiate a transfer from I2C in target mode.
I2C_Memory_ExtendedParams::memAddrSize
uint8_t memAddrSize
Definition: i2c_lld.h:281
I2CLLD_Object::currentTxnCount
uint32_t currentTxnCount
Definition: i2c_lld.h:440