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 #include <stdio.h>
22 #include <stddef.h>
23 #include <stdint.h>
24
25 #include "mxc_assert.h"
26 #include "mxc_device.h"
27 #include "mxc_delay.h"
28 #include "mxc_lock.h"
29 #include "mxc_pins.h"
30 #include "mxc_sys.h"
31
32 #include "dma_regs.h"
33 #include "gpio.h"
34 #include "i2c.h"
35 #include "i2c_regs.h"
36 #include "i2c_reva.h"
37
38 /* **** Definitions **** */
39 #define MXC_I2C_FASTPLUS_SPEED 1000000
40
41 /* **** Variable Declaration **** */
42 uint32_t interruptCheck = MXC_F_I2C_INTFL0_ADDR_MATCH | MXC_F_I2C_INTFL0_DNR_ERR;
43
44 /* **** Function Prototypes **** */
45
46 /* ************************************************************************* */
47 /* Control/Configuration functions */
48 /* ************************************************************************* */
MXC_I2C_Init(mxc_i2c_regs_t * i2c,int masterMode,unsigned int slaveAddr)49 int MXC_I2C_Init(mxc_i2c_regs_t *i2c, int masterMode, unsigned int slaveAddr)
50 {
51 if (i2c == NULL) {
52 return E_NULL_PTR;
53 }
54
55 #ifndef MSDK_NO_GPIO_CLK_INIT
56 MXC_I2C_Shutdown(i2c); // Clear everything out
57
58 if (i2c == MXC_I2C0) {
59 MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_I2C0);
60 MXC_GPIO_Config(&gpio_cfg_i2c0);
61 } else if (i2c == MXC_I2C1) {
62 MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_I2C1);
63 MXC_GPIO_Config(&gpio_cfg_i2c1);
64 } else {
65 return E_NO_DEVICE;
66 }
67 #endif // MSDK_NO_GPIO_CLK_INIT
68
69 return MXC_I2C_RevA_Init((mxc_i2c_reva_regs_t *)i2c, masterMode, slaveAddr);
70 }
71
MXC_I2C_SetSlaveAddr(mxc_i2c_regs_t * i2c,unsigned int slaveAddr,int idx)72 int MXC_I2C_SetSlaveAddr(mxc_i2c_regs_t *i2c, unsigned int slaveAddr, int idx)
73 {
74 return MXC_I2C_RevA_SetSlaveAddr((mxc_i2c_reva_regs_t *)i2c, slaveAddr, idx);
75 }
76
MXC_I2C_Shutdown(mxc_i2c_regs_t * i2c)77 int MXC_I2C_Shutdown(mxc_i2c_regs_t *i2c)
78 {
79 // Configure GPIO for I2C
80 if (i2c == MXC_I2C0) {
81 MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_I2C0);
82 MXC_SYS_Reset_Periph(MXC_SYS_RESET0_I2C0);
83 } else if (i2c == MXC_I2C1) {
84 MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_I2C1);
85 MXC_SYS_Reset_Periph(MXC_SYS_RESET1_I2C1);
86 } else {
87 return E_NO_DEVICE;
88 }
89
90 return MXC_I2C_RevA_Shutdown((mxc_i2c_reva_regs_t *)i2c);
91 }
92
MXC_I2C_Reset(mxc_i2c_regs_t * i2c)93 int MXC_I2C_Reset(mxc_i2c_regs_t *i2c)
94 {
95 // Configure GPIO for I2C
96 if (i2c == MXC_I2C0) {
97 MXC_SYS_Reset_Periph(MXC_SYS_RESET0_I2C0);
98 } else if (i2c == MXC_I2C1) {
99 MXC_SYS_Reset_Periph(MXC_SYS_RESET1_I2C1);
100 } else {
101 return E_NO_DEVICE;
102 }
103
104 return E_NO_ERROR;
105 }
106
MXC_I2C_SetFrequency(mxc_i2c_regs_t * i2c,unsigned int hz)107 int MXC_I2C_SetFrequency(mxc_i2c_regs_t *i2c, unsigned int hz)
108 {
109 return MXC_I2C_RevA_SetFrequency((mxc_i2c_reva_regs_t *)i2c, hz);
110 }
111
MXC_I2C_GetFrequency(mxc_i2c_regs_t * i2c)112 int MXC_I2C_GetFrequency(mxc_i2c_regs_t *i2c)
113 {
114 return MXC_I2C_RevA_GetFrequency((mxc_i2c_reva_regs_t *)i2c);
115 }
116
MXC_I2C_ReadyForSleep(mxc_i2c_regs_t * i2c)117 int MXC_I2C_ReadyForSleep(mxc_i2c_regs_t *i2c)
118 {
119 return MXC_I2C_RevA_ReadyForSleep((mxc_i2c_reva_regs_t *)i2c);
120 }
121
MXC_I2C_SetClockStretching(mxc_i2c_regs_t * i2c,int enable)122 int MXC_I2C_SetClockStretching(mxc_i2c_regs_t *i2c, int enable)
123 {
124 return MXC_I2C_RevA_SetClockStretching((mxc_i2c_reva_regs_t *)i2c, enable);
125 }
126
MXC_I2C_GetClockStretching(mxc_i2c_regs_t * i2c)127 int MXC_I2C_GetClockStretching(mxc_i2c_regs_t *i2c)
128 {
129 return MXC_I2C_RevA_GetClockStretching((mxc_i2c_reva_regs_t *)i2c);
130 }
131
MXC_I2C_DMA_Init(mxc_i2c_regs_t * i2c,mxc_dma_regs_t * dma,bool use_dma_tx,bool use_dma_rx)132 int MXC_I2C_DMA_Init(mxc_i2c_regs_t *i2c, mxc_dma_regs_t *dma, bool use_dma_tx, bool use_dma_rx)
133 {
134 return MXC_I2C_RevA_DMA_Init((mxc_i2c_reva_regs_t *)i2c, (mxc_dma_reva_regs_t *)dma, use_dma_tx,
135 use_dma_rx);
136 }
137
MXC_I2C_DMA_GetTXChannel(mxc_i2c_regs_t * i2c)138 int MXC_I2C_DMA_GetTXChannel(mxc_i2c_regs_t *i2c)
139 {
140 return MXC_I2C_RevA_DMA_GetTXChannel((mxc_i2c_reva_regs_t *)i2c);
141 }
142
MXC_I2C_DMA_GetRXChannel(mxc_i2c_regs_t * i2c)143 int MXC_I2C_DMA_GetRXChannel(mxc_i2c_regs_t *i2c)
144 {
145 return MXC_I2C_RevA_DMA_GetRXChannel((mxc_i2c_reva_regs_t *)i2c);
146 }
147
MXC_I2C_DMA_SetRequestSelect(mxc_i2c_regs_t * i2c,uint8_t * txData,uint8_t * rxData)148 int MXC_I2C_DMA_SetRequestSelect(mxc_i2c_regs_t *i2c, uint8_t *txData, uint8_t *rxData)
149 {
150 int i2cNum;
151 int txReqSel = -1;
152 int rxReqSel = -1;
153
154 if (i2c == NULL) {
155 return E_NULL_PTR;
156 }
157
158 i2cNum = MXC_I2C_GET_IDX((mxc_i2c_regs_t *)i2c);
159
160 if (txData != NULL) {
161 switch (i2cNum) {
162 case 0:
163 txReqSel = MXC_DMA_REQUEST_I2C0TX;
164 break;
165
166 case 1:
167 txReqSel = MXC_DMA_REQUEST_I2C1TX;
168 break;
169
170 default:
171 return E_BAD_PARAM;
172 }
173 }
174
175 if (rxData != NULL) {
176 switch (i2cNum) {
177 case 0:
178 rxReqSel = MXC_DMA_REQUEST_I2C0RX;
179 break;
180
181 case 1:
182 rxReqSel = MXC_DMA_REQUEST_I2C1RX;
183 break;
184
185 default:
186 return E_BAD_PARAM;
187 }
188 }
189
190 return MXC_I2C_RevA_DMA_SetRequestSelect((mxc_i2c_reva_regs_t *)i2c,
191 (mxc_dma_reva_regs_t *)MXC_DMA, txReqSel, rxReqSel);
192 }
193
194 /* ************************************************************************* */
195 /* Low-level functions */
196 /* ************************************************************************* */
MXC_I2C_Start(mxc_i2c_regs_t * i2c)197 int MXC_I2C_Start(mxc_i2c_regs_t *i2c)
198 {
199 return MXC_I2C_RevA_Start((mxc_i2c_reva_regs_t *)i2c);
200 }
201
MXC_I2C_Stop(mxc_i2c_regs_t * i2c)202 int MXC_I2C_Stop(mxc_i2c_regs_t *i2c)
203 {
204 return MXC_I2C_RevA_Stop((mxc_i2c_reva_regs_t *)i2c);
205 }
206
MXC_I2C_WriteByte(mxc_i2c_regs_t * i2c,unsigned char byte)207 int MXC_I2C_WriteByte(mxc_i2c_regs_t *i2c, unsigned char byte)
208 {
209 return MXC_I2C_RevA_WriteByte((mxc_i2c_reva_regs_t *)i2c, byte);
210 }
211
MXC_I2C_ReadByte(mxc_i2c_regs_t * i2c,unsigned char * byte,int ack)212 int MXC_I2C_ReadByte(mxc_i2c_regs_t *i2c, unsigned char *byte, int ack)
213 {
214 return MXC_I2C_RevA_ReadByte((mxc_i2c_reva_regs_t *)i2c, byte, ack);
215 }
216
MXC_I2C_ReadByteInteractive(mxc_i2c_regs_t * i2c,unsigned char * byte,mxc_i2c_getAck_t getAck)217 int MXC_I2C_ReadByteInteractive(mxc_i2c_regs_t *i2c, unsigned char *byte, mxc_i2c_getAck_t getAck)
218 {
219 return MXC_I2C_RevA_ReadByteInteractive((mxc_i2c_reva_regs_t *)i2c, byte,
220 (mxc_i2c_reva_getAck_t)getAck);
221 }
222
MXC_I2C_Write(mxc_i2c_regs_t * i2c,unsigned char * bytes,unsigned int * len)223 int MXC_I2C_Write(mxc_i2c_regs_t *i2c, unsigned char *bytes, unsigned int *len)
224 {
225 return MXC_I2C_RevA_Write((mxc_i2c_reva_regs_t *)i2c, bytes, len);
226 }
227
MXC_I2C_Read(mxc_i2c_regs_t * i2c,unsigned char * bytes,unsigned int * len,int ack)228 int MXC_I2C_Read(mxc_i2c_regs_t *i2c, unsigned char *bytes, unsigned int *len, int ack)
229 {
230 return MXC_I2C_RevA_Read((mxc_i2c_reva_regs_t *)i2c, bytes, len, ack);
231 }
232
MXC_I2C_ReadRXFIFO(mxc_i2c_regs_t * i2c,volatile unsigned char * bytes,unsigned int len)233 int MXC_I2C_ReadRXFIFO(mxc_i2c_regs_t *i2c, volatile unsigned char *bytes, unsigned int len)
234 {
235 return MXC_I2C_RevA_ReadRXFIFO((mxc_i2c_reva_regs_t *)i2c, bytes, len);
236 }
237
MXC_I2C_ReadRXFIFODMA(mxc_i2c_regs_t * i2c,unsigned char * bytes,unsigned int len,mxc_i2c_dma_complete_cb_t callback)238 int MXC_I2C_ReadRXFIFODMA(mxc_i2c_regs_t *i2c, unsigned char *bytes, unsigned int len,
239 mxc_i2c_dma_complete_cb_t callback)
240 {
241 // The callback parameter was previously unused but keeping it for backwards-compatibility.
242 return MXC_I2C_RevA_ReadRXFIFODMA((mxc_i2c_reva_regs_t *)i2c, bytes, len, MXC_DMA);
243 }
244
MXC_I2C_GetRXFIFOAvailable(mxc_i2c_regs_t * i2c)245 int MXC_I2C_GetRXFIFOAvailable(mxc_i2c_regs_t *i2c)
246 {
247 return MXC_I2C_RevA_GetRXFIFOAvailable((mxc_i2c_reva_regs_t *)i2c);
248 }
249
MXC_I2C_WriteTXFIFO(mxc_i2c_regs_t * i2c,volatile unsigned char * bytes,unsigned int len)250 int MXC_I2C_WriteTXFIFO(mxc_i2c_regs_t *i2c, volatile unsigned char *bytes, unsigned int len)
251 {
252 return MXC_I2C_RevA_WriteTXFIFO((mxc_i2c_reva_regs_t *)i2c, bytes, len);
253 }
254
MXC_I2C_WriteTXFIFODMA(mxc_i2c_regs_t * i2c,unsigned char * bytes,unsigned int len,mxc_i2c_dma_complete_cb_t callback)255 int MXC_I2C_WriteTXFIFODMA(mxc_i2c_regs_t *i2c, unsigned char *bytes, unsigned int len,
256 mxc_i2c_dma_complete_cb_t callback)
257 {
258 // The callback parameter was previously unused but keeping it for backwards-compatibility.
259 return MXC_I2C_RevA_WriteTXFIFODMA((mxc_i2c_reva_regs_t *)i2c, bytes, len, MXC_DMA);
260 }
261
MXC_I2C_GetTXFIFOAvailable(mxc_i2c_regs_t * i2c)262 int MXC_I2C_GetTXFIFOAvailable(mxc_i2c_regs_t *i2c)
263 {
264 return MXC_I2C_RevA_GetTXFIFOAvailable((mxc_i2c_reva_regs_t *)i2c);
265 }
266
MXC_I2C_ClearRXFIFO(mxc_i2c_regs_t * i2c)267 void MXC_I2C_ClearRXFIFO(mxc_i2c_regs_t *i2c)
268 {
269 MXC_I2C_RevA_ClearRXFIFO((mxc_i2c_reva_regs_t *)i2c);
270 }
271
MXC_I2C_ClearTXFIFO(mxc_i2c_regs_t * i2c)272 void MXC_I2C_ClearTXFIFO(mxc_i2c_regs_t *i2c)
273 {
274 MXC_I2C_RevA_ClearTXFIFO((mxc_i2c_reva_regs_t *)i2c);
275 }
276
MXC_I2C_GetFlags(mxc_i2c_regs_t * i2c,unsigned int * flags0,unsigned int * flags1)277 int MXC_I2C_GetFlags(mxc_i2c_regs_t *i2c, unsigned int *flags0, unsigned int *flags1)
278 {
279 return MXC_I2C_RevA_GetFlags((mxc_i2c_reva_regs_t *)i2c, flags0, flags1);
280 }
281
MXC_I2C_ClearFlags(mxc_i2c_regs_t * i2c,unsigned int flags0,unsigned int flags1)282 void MXC_I2C_ClearFlags(mxc_i2c_regs_t *i2c, unsigned int flags0, unsigned int flags1)
283 {
284 MXC_I2C_RevA_ClearFlags((mxc_i2c_reva_regs_t *)i2c, flags0, flags1);
285 }
286
MXC_I2C_EnableInt(mxc_i2c_regs_t * i2c,unsigned int flags0,unsigned int flags1)287 void MXC_I2C_EnableInt(mxc_i2c_regs_t *i2c, unsigned int flags0, unsigned int flags1)
288 {
289 MXC_I2C_RevA_EnableInt((mxc_i2c_reva_regs_t *)i2c, flags0, flags1);
290 }
291
MXC_I2C_DisableInt(mxc_i2c_regs_t * i2c,unsigned int flags0,unsigned int flags1)292 void MXC_I2C_DisableInt(mxc_i2c_regs_t *i2c, unsigned int flags0, unsigned int flags1)
293 {
294 MXC_I2C_RevA_DisableInt((mxc_i2c_reva_regs_t *)i2c, flags0, flags1);
295 }
296
MXC_I2C_EnablePreload(mxc_i2c_regs_t * i2c)297 void MXC_I2C_EnablePreload(mxc_i2c_regs_t *i2c)
298 {
299 MXC_I2C_RevA_EnablePreload((mxc_i2c_reva_regs_t *)i2c);
300 }
301
MXC_I2C_DisablePreload(mxc_i2c_regs_t * i2c)302 void MXC_I2C_DisablePreload(mxc_i2c_regs_t *i2c)
303 {
304 MXC_I2C_RevA_DisablePreload((mxc_i2c_reva_regs_t *)i2c);
305 }
306
MXC_I2C_EnableGeneralCall(mxc_i2c_regs_t * i2c)307 void MXC_I2C_EnableGeneralCall(mxc_i2c_regs_t *i2c)
308 {
309 MXC_I2C_RevA_EnableGeneralCall((mxc_i2c_reva_regs_t *)i2c);
310 }
311
MXC_I2C_DisableGeneralCall(mxc_i2c_regs_t * i2c)312 void MXC_I2C_DisableGeneralCall(mxc_i2c_regs_t *i2c)
313 {
314 MXC_I2C_RevA_DisableGeneralCall((mxc_i2c_reva_regs_t *)i2c);
315 }
316
MXC_I2C_SetTimeout(mxc_i2c_regs_t * i2c,unsigned int timeout)317 void MXC_I2C_SetTimeout(mxc_i2c_regs_t *i2c, unsigned int timeout)
318 {
319 MXC_I2C_RevA_SetTimeout((mxc_i2c_reva_regs_t *)i2c, timeout);
320 }
321
MXC_I2C_GetTimeout(mxc_i2c_regs_t * i2c)322 unsigned int MXC_I2C_GetTimeout(mxc_i2c_regs_t *i2c)
323 {
324 return MXC_I2C_RevA_GetTimeout((mxc_i2c_reva_regs_t *)i2c);
325 }
326
MXC_I2C_Recover(mxc_i2c_regs_t * i2c,unsigned int retries)327 int MXC_I2C_Recover(mxc_i2c_regs_t *i2c, unsigned int retries)
328 {
329 return MXC_I2C_RevA_Recover((mxc_i2c_reva_regs_t *)i2c, retries);
330 }
331
332 /* ************************************************************************* */
333 /* Transaction level functions */
334 /* ************************************************************************* */
335
MXC_I2C_MasterTransaction(mxc_i2c_req_t * req)336 int MXC_I2C_MasterTransaction(mxc_i2c_req_t *req)
337 {
338 return MXC_I2C_RevA_MasterTransaction((mxc_i2c_reva_req_t *)req);
339 }
340
MXC_I2C_MasterTransactionAsync(mxc_i2c_req_t * req)341 int MXC_I2C_MasterTransactionAsync(mxc_i2c_req_t *req)
342 {
343 return MXC_I2C_RevA_MasterTransactionAsync((mxc_i2c_reva_req_t *)req);
344 }
345
MXC_I2C_MasterTransactionDMA(mxc_i2c_req_t * req)346 int MXC_I2C_MasterTransactionDMA(mxc_i2c_req_t *req)
347 {
348 return MXC_I2C_RevA_MasterTransactionDMA((mxc_i2c_reva_req_t *)req, MXC_DMA);
349 }
350
MXC_I2C_SlaveTransaction(mxc_i2c_regs_t * i2c,mxc_i2c_slave_handler_t callback)351 int MXC_I2C_SlaveTransaction(mxc_i2c_regs_t *i2c, mxc_i2c_slave_handler_t callback)
352 {
353 return MXC_I2C_RevA_SlaveTransaction((mxc_i2c_reva_regs_t *)i2c,
354 (mxc_i2c_reva_slave_handler_t)callback, interruptCheck);
355 }
356
MXC_I2C_SlaveTransactionAsync(mxc_i2c_regs_t * i2c,mxc_i2c_slave_handler_t callback)357 int MXC_I2C_SlaveTransactionAsync(mxc_i2c_regs_t *i2c, mxc_i2c_slave_handler_t callback)
358 {
359 return MXC_I2C_RevA_SlaveTransactionAsync(
360 (mxc_i2c_reva_regs_t *)i2c, (mxc_i2c_reva_slave_handler_t)callback, interruptCheck);
361 }
362
MXC_I2C_SetRXThreshold(mxc_i2c_regs_t * i2c,unsigned int numBytes)363 int MXC_I2C_SetRXThreshold(mxc_i2c_regs_t *i2c, unsigned int numBytes)
364 {
365 return MXC_I2C_RevA_SetRXThreshold((mxc_i2c_reva_regs_t *)i2c, numBytes);
366 }
367
MXC_I2C_GetRXThreshold(mxc_i2c_regs_t * i2c)368 int MXC_I2C_GetRXThreshold(mxc_i2c_regs_t *i2c)
369 {
370 return MXC_I2C_RevA_GetRXThreshold((mxc_i2c_reva_regs_t *)i2c);
371 }
372
MXC_I2C_SetTXThreshold(mxc_i2c_regs_t * i2c,unsigned int numBytes)373 int MXC_I2C_SetTXThreshold(mxc_i2c_regs_t *i2c, unsigned int numBytes)
374 {
375 return MXC_I2C_RevA_SetTXThreshold((mxc_i2c_reva_regs_t *)i2c, numBytes);
376 }
377
MXC_I2C_GetTXThreshold(mxc_i2c_regs_t * i2c)378 int MXC_I2C_GetTXThreshold(mxc_i2c_regs_t *i2c)
379 {
380 return MXC_I2C_RevA_GetTXThreshold((mxc_i2c_reva_regs_t *)i2c);
381 }
382
MXC_I2C_AsyncStop(mxc_i2c_regs_t * i2c)383 void MXC_I2C_AsyncStop(mxc_i2c_regs_t *i2c)
384 {
385 MXC_I2C_RevA_AsyncStop((mxc_i2c_reva_regs_t *)i2c);
386 }
387
MXC_I2C_AbortAsync(mxc_i2c_regs_t * i2c)388 void MXC_I2C_AbortAsync(mxc_i2c_regs_t *i2c)
389 {
390 MXC_I2C_RevA_AbortAsync((mxc_i2c_reva_regs_t *)i2c);
391 }
392
MXC_I2C_AsyncHandler(mxc_i2c_regs_t * i2c)393 void MXC_I2C_AsyncHandler(mxc_i2c_regs_t *i2c)
394 {
395 MXC_I2C_RevA_AsyncHandler((mxc_i2c_reva_regs_t *)i2c, interruptCheck);
396 }
397
MXC_I2C_DMACallback(int ch,int error)398 void MXC_I2C_DMACallback(int ch, int error)
399 {
400 MXC_I2C_RevA_DMACallback(ch, error);
401 }
402