1 /****************************************************************************** 2 * 3 * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by 4 * Analog Devices, Inc.), 5 * Copyright (C) 2023-2024 Analog Devices, Inc. 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * 19 ******************************************************************************/ 20 21 #ifndef LIBRARIES_PERIPHDRIVERS_SOURCE_I2C_I2C_REVA_H_ 22 #define LIBRARIES_PERIPHDRIVERS_SOURCE_I2C_I2C_REVA_H_ 23 24 #include <stdio.h> 25 #include <stddef.h> 26 #include <stdint.h> 27 #include "mxc_device.h" 28 #include "mxc_assert.h" 29 #include "mxc_lock.h" 30 #include "mxc_sys.h" 31 #include "mxc_delay.h" 32 #include "i2c_regs.h" 33 #include "i2c_reva_regs.h" 34 #include "dma.h" 35 #include "dma_reva_regs.h" 36 37 /* **** Definitions **** */ 38 #define MXC_I2C_REVA_MAX_ADDR_WIDTH 0x7F 39 #define MXC_I2C_REVA_STD_MODE 100000 40 #define MXC_I2C_REVA_FAST_SPEED 400000 41 #define MXC_I2C_REVA_FASTPLUS_SPEED 1000000 42 #define MXC_I2C_REVA_HIGH_SPEED 3400000 43 44 #define MXC_I2C_REVA_INTFL0_MASK 0x00FFFFFF 45 #define MXC_I2C_REVA_INTFL1_MASK 0x00000007 46 47 #define MXC_I2C_REVA_MAX_FIFO_TRANSACTION 256 48 49 #define MXC_I2C_REVA_ERROR \ 50 (MXC_F_I2C_REVA_INTFL0_ARB_ERR | MXC_F_I2C_REVA_INTFL0_TO_ERR | \ 51 MXC_F_I2C_REVA_INTFL0_ADDR_NACK_ERR | MXC_F_I2C_REVA_INTFL0_DATA_ERR | \ 52 MXC_F_I2C_REVA_INTFL0_DNR_ERR | MXC_F_I2C_REVA_INTFL0_START_ERR | \ 53 MXC_F_I2C_REVA_INTFL0_STOP_ERR) 54 55 typedef struct _i2c_reva_req_t mxc_i2c_reva_req_t; 56 typedef int (*mxc_i2c_reva_getAck_t)(mxc_i2c_reva_regs_t *i2c, unsigned char byte); 57 typedef void (*mxc_i2c_reva_complete_cb_t)(mxc_i2c_reva_req_t *req, int result); 58 typedef void (*mxc_i2c_reva_dma_complete_cb_t)(int len, int result); 59 struct _i2c_reva_req_t { 60 mxc_i2c_reva_regs_t *i2c; 61 unsigned int addr; 62 unsigned char *tx_buf; 63 unsigned int tx_len; 64 unsigned char *rx_buf; 65 unsigned int rx_len; 66 int restart; 67 mxc_i2c_reva_complete_cb_t callback; 68 }; 69 typedef enum { 70 MXC_I2C_REVA_EVT_MASTER_WR, 71 MXC_I2C_REVA_EVT_MASTER_RD, 72 MXC_I2C_REVA_EVT_RX_THRESH, 73 MXC_I2C_REVA_EVT_TX_THRESH, 74 MXC_I2C_REVA_EVT_TRANS_COMP, 75 MXC_I2C_REVA_EVT_UNDERFLOW, 76 MXC_I2C_REVA_EVT_OVERFLOW, 77 } mxc_i2c_reva_slave_event_t; 78 typedef int (*mxc_i2c_reva_slave_handler_t)(mxc_i2c_reva_regs_t *i2c, 79 mxc_i2c_reva_slave_event_t event, void *data); 80 /* **** Variable Declaration **** */ 81 82 extern void *AsyncRequests[MXC_I2C_INSTANCES]; 83 84 /* **** Function Prototypes **** */ 85 86 /* ************************************************************************* */ 87 /* Control/Configuration functions */ 88 /* ************************************************************************* */ 89 int MXC_I2C_RevA_Init(mxc_i2c_reva_regs_t *i2c, int masterMode, unsigned int slaveAddr); 90 int MXC_I2C_RevA_SetSlaveAddr(mxc_i2c_reva_regs_t *i2c, unsigned int slaveAddr, int idx); 91 int MXC_I2C_RevA_Shutdown(mxc_i2c_reva_regs_t *i2c); 92 int MXC_I2C_RevA_SetFrequency(mxc_i2c_reva_regs_t *i2c, unsigned int hz); 93 unsigned int MXC_I2C_RevA_GetFrequency(mxc_i2c_reva_regs_t *i2c); 94 int MXC_I2C_RevA_ReadyForSleep(mxc_i2c_reva_regs_t *i2c); 95 int MXC_I2C_RevA_SetClockStretching(mxc_i2c_reva_regs_t *i2c, int enable); 96 int MXC_I2C_RevA_GetClockStretching(mxc_i2c_reva_regs_t *i2c); 97 int MXC_I2C_RevA_DMA_Init(mxc_i2c_reva_regs_t *i2c, mxc_dma_reva_regs_t *dma, bool use_dma_tx, 98 bool use_dma_rx); 99 int MXC_I2C_RevA_DMA_GetTXChannel(mxc_i2c_reva_regs_t *i2c); 100 int MXC_I2C_RevA_DMA_GetRXChannel(mxc_i2c_reva_regs_t *i2c); 101 int MXC_I2C_RevA_DMA_SetRequestSelect(mxc_i2c_reva_regs_t *i2c, mxc_dma_reva_regs_t *dma, 102 uint32_t txReqSel, uint32_t rxReqSel); 103 104 /* ************************************************************************* */ 105 /* Low-level functions */ 106 /* ************************************************************************* */ 107 int MXC_I2C_RevA_Start(mxc_i2c_reva_regs_t *i2c); 108 int MXC_I2C_RevA_Stop(mxc_i2c_reva_regs_t *i2c); 109 int MXC_I2C_RevA_WriteByte(mxc_i2c_reva_regs_t *i2c, unsigned char byte); 110 int MXC_I2C_RevA_ReadByte(mxc_i2c_reva_regs_t *i2c, unsigned char *byte, int ack); 111 int MXC_I2C_RevA_ReadByteInteractive(mxc_i2c_reva_regs_t *i2c, unsigned char *byte, 112 mxc_i2c_reva_getAck_t getAck); 113 int MXC_I2C_RevA_Write(mxc_i2c_reva_regs_t *i2c, unsigned char *bytes, unsigned int *len); 114 int MXC_I2C_RevA_Read(mxc_i2c_reva_regs_t *i2c, unsigned char *bytes, unsigned int *len, int ack); 115 int MXC_I2C_RevA_ReadRXFIFO(mxc_i2c_reva_regs_t *i2c, volatile unsigned char *bytes, 116 unsigned int len); 117 int MXC_I2C_RevA_ReadRXFIFODMA(mxc_i2c_reva_regs_t *i2c, unsigned char *bytes, unsigned int len, 118 mxc_dma_regs_t *dma); 119 int MXC_I2C_RevA_GetRXFIFOAvailable(mxc_i2c_reva_regs_t *i2c); 120 int MXC_I2C_RevA_WriteTXFIFO(mxc_i2c_reva_regs_t *i2c, volatile unsigned char *bytes, 121 unsigned int len); 122 int MXC_I2C_RevA_WriteTXFIFODMA(mxc_i2c_reva_regs_t *i2c, unsigned char *bytes, unsigned int len, 123 mxc_dma_regs_t *dma); 124 int MXC_I2C_RevA_GetTXFIFOAvailable(mxc_i2c_reva_regs_t *i2c); 125 void MXC_I2C_RevA_ClearRXFIFO(mxc_i2c_reva_regs_t *i2c); 126 void MXC_I2C_RevA_ClearTXFIFO(mxc_i2c_reva_regs_t *i2c); 127 int MXC_I2C_RevA_GetFlags(mxc_i2c_reva_regs_t *i2c, unsigned int *flags0, unsigned int *flags1); 128 void MXC_I2C_RevA_ClearFlags(mxc_i2c_reva_regs_t *i2c, unsigned int flags0, unsigned int flags1); 129 void MXC_I2C_RevA_EnableInt(mxc_i2c_reva_regs_t *i2c, unsigned int flags0, unsigned int flags1); 130 void MXC_I2C_RevA_DisableInt(mxc_i2c_reva_regs_t *i2c, unsigned int flags0, unsigned int flags1); 131 void MXC_I2C_RevA_EnablePreload(mxc_i2c_reva_regs_t *i2c); 132 void MXC_I2C_RevA_DisablePreload(mxc_i2c_reva_regs_t *i2c); 133 void MXC_I2C_RevA_EnableGeneralCall(mxc_i2c_reva_regs_t *i2c); 134 void MXC_I2C_RevA_DisableGeneralCall(mxc_i2c_reva_regs_t *i2c); 135 void MXC_I2C_RevA_SetTimeout(mxc_i2c_reva_regs_t *i2c, unsigned int timeout); 136 unsigned int MXC_I2C_RevA_GetTimeout(mxc_i2c_reva_regs_t *i2c); 137 int MXC_I2C_RevA_Recover(mxc_i2c_reva_regs_t *i2c, unsigned int retries); 138 139 /* ************************************************************************* */ 140 /* Transaction level functions */ 141 /* ************************************************************************* */ 142 int MXC_I2C_RevA_MasterTransaction(mxc_i2c_reva_req_t *req); 143 int MXC_I2C_RevA_MasterTransactionAsync(mxc_i2c_reva_req_t *req); 144 int MXC_I2C_RevA_MasterTransactionDMA(mxc_i2c_reva_req_t *req, mxc_dma_regs_t *dma); 145 int MXC_I2C_RevA_SlaveTransaction(mxc_i2c_reva_regs_t *i2c, mxc_i2c_reva_slave_handler_t callback, 146 uint32_t interruptCheck); 147 int MXC_I2C_RevA_SlaveTransactionAsync(mxc_i2c_reva_regs_t *i2c, 148 mxc_i2c_reva_slave_handler_t callback, 149 uint32_t interruptCheck); 150 int MXC_I2C_RevA_SetRXThreshold(mxc_i2c_reva_regs_t *i2c, unsigned int numBytes); 151 unsigned int MXC_I2C_RevA_GetRXThreshold(mxc_i2c_reva_regs_t *i2c); 152 int MXC_I2C_RevA_SetTXThreshold(mxc_i2c_reva_regs_t *i2c, unsigned int numBytes); 153 unsigned int MXC_I2C_RevA_GetTXThreshold(mxc_i2c_reva_regs_t *i2c); 154 void MXC_I2C_RevA_AsyncCallback(mxc_i2c_reva_regs_t *i2c, int retVal); 155 void MXC_I2C_RevA_AsyncStop(mxc_i2c_reva_regs_t *i2c); 156 void MXC_I2C_RevA_AbortAsync(mxc_i2c_reva_regs_t *i2c); 157 void MXC_I2C_RevA_MasterAsyncHandler(int i2cNum); 158 void MXC_I2C_RevA_SlaveAsyncHandler(mxc_i2c_reva_regs_t *i2c, mxc_i2c_reva_slave_handler_t callback, 159 uint32_t *int_en, int *retVal); 160 void MXC_I2C_RevA_AsyncHandler(mxc_i2c_reva_regs_t *i2c, uint32_t interruptCheck); 161 void MXC_I2C_RevA_DMACallback(int ch, int error); 162 163 #endif // LIBRARIES_PERIPHDRIVERS_SOURCE_I2C_I2C_REVA_H_ 164