-
Notifications
You must be signed in to change notification settings - Fork 78
/
i2c_me14.c
422 lines (345 loc) · 12.4 KB
/
i2c_me14.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/******************************************************************************
*
* Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
* Analog Devices, Inc.),
* Copyright (C) 2023-2024 Analog Devices, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include "mxc_device.h"
#include "mxc_assert.h"
#include "mxc_lock.h"
#include "mxc_sys.h"
#include "mxc_pins.h"
#include "mxc_delay.h"
#include "i2c_regs.h"
#include "dma_regs.h"
#include "i2c.h"
#include "i2c_reva.h"
/* **** Variable Declaration **** */
uint32_t interruptCheck = MXC_F_I2C_INT_FL0_RD_ADDR_MATCH | MXC_F_I2C_INT_FL0_WR_ADDR_MATCH |
MXC_F_I2C_INT_FL0_ADDR_MATCH | MXC_F_I2C_INT_FL0_DO_NOT_RESP_ER;
/* **** Function Prototypes **** */
/* ************************************************************************* */
/* Control/Configuration functions */
/* ************************************************************************* */
int MXC_I2C_Init(mxc_i2c_regs_t *i2c, int masterMode, unsigned int slaveAddr)
{
if (i2c == NULL) {
return E_NULL_PTR;
}
#ifndef MSDK_NO_GPIO_CLK_INIT
MXC_I2C_Shutdown(i2c); // Clear everything out
if ((i2c == MXC_I2C0_BUS0) || (i2c == MXC_I2C0_BUS1)) {
MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_I2C0);
MXC_GPIO_Config(&gpio_cfg_i2c0);
} else if ((i2c == MXC_I2C1_BUS0) || (i2c == MXC_I2C1_BUS1)) {
MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_I2C1);
MXC_GPIO_Config(&gpio_cfg_i2c1);
} else if ((i2c == MXC_I2C2_BUS0) || (i2c == MXC_I2C2_BUS1)) {
MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_I2C2);
MXC_GPIO_Config(&gpio_cfg_i2c2);
} else {
return E_NO_DEVICE;
}
#endif // MSDK_NO_GPIO_CLK_INIT
return MXC_I2C_RevA_Init((mxc_i2c_reva_regs_t *)i2c, masterMode, slaveAddr);
}
int MXC_I2C_SetSlaveAddr(mxc_i2c_regs_t *i2c, unsigned int slaveAddr, int idx)
{
if (idx != 0) {
// MAX32665 only supports one slave address
return E_NOT_SUPPORTED;
}
return MXC_I2C_RevA_SetSlaveAddr((mxc_i2c_reva_regs_t *)i2c, slaveAddr, idx);
}
int MXC_I2C_Shutdown(mxc_i2c_regs_t *i2c)
{
// Configure GPIO for I2C
if ((i2c == MXC_I2C0_BUS0) || (i2c == MXC_I2C0_BUS1)) {
MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_I2C0);
} else if ((i2c == MXC_I2C1_BUS0) || (i2c == MXC_I2C1_BUS1)) {
MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_I2C1);
} else if ((i2c == MXC_I2C2_BUS0) || (i2c == MXC_I2C2_BUS1)) {
MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_I2C2);
} else {
return E_NO_DEVICE;
}
int i2cNum = MXC_I2C_GET_IDX(i2c);
// Reconcile this with MXC_SYS_I2C_Init when we figure out what to do abotu system level things
switch (i2cNum) {
case 0:
MXC_GCR->rstr0 |= MXC_F_GCR_RSTR0_I2C0;
break;
case 1:
MXC_GCR->rstr1 |= MXC_F_GCR_RSTR1_I2C1;
break;
case 2:
MXC_GCR->rstr1 |= MXC_F_GCR_RSTR1_I2C2;
break;
default:
return E_BAD_PARAM;
}
return MXC_I2C_RevA_Shutdown((mxc_i2c_reva_regs_t *)i2c);
}
int MXC_I2C_SetFrequency(mxc_i2c_regs_t *i2c, unsigned int hz)
{
return MXC_I2C_RevA_SetFrequency((mxc_i2c_reva_regs_t *)i2c, hz);
}
unsigned int MXC_I2C_GetFrequency(mxc_i2c_regs_t *i2c)
{
return MXC_I2C_RevA_GetFrequency((mxc_i2c_reva_regs_t *)i2c);
}
int MXC_I2C_ReadyForSleep(mxc_i2c_regs_t *i2c)
{
return MXC_I2C_RevA_ReadyForSleep((mxc_i2c_reva_regs_t *)i2c);
}
int MXC_I2C_SetClockStretching(mxc_i2c_regs_t *i2c, int enable)
{
return MXC_I2C_RevA_SetClockStretching((mxc_i2c_reva_regs_t *)i2c, enable);
}
int MXC_I2C_GetClockStretching(mxc_i2c_regs_t *i2c)
{
return MXC_I2C_RevA_GetClockStretching((mxc_i2c_reva_regs_t *)i2c);
}
int MXC_I2C_DMA_Init(mxc_i2c_regs_t *i2c, mxc_dma_regs_t *dma, bool use_dma_tx, bool use_dma_rx)
{
return MXC_I2C_RevA_DMA_Init((mxc_i2c_reva_regs_t *)i2c, (mxc_dma_reva_regs_t *)dma, use_dma_tx,
use_dma_rx);
}
int MXC_I2C_DMA_GetTXChannel(mxc_i2c_regs_t *i2c)
{
return MXC_I2C_RevA_DMA_GetTXChannel((mxc_i2c_reva_regs_t *)i2c);
}
int MXC_I2C_DMA_GetRXChannel(mxc_i2c_regs_t *i2c)
{
return MXC_I2C_RevA_DMA_GetRXChannel((mxc_i2c_reva_regs_t *)i2c);
}
int MXC_I2C_DMA_SetRequestSelect(mxc_i2c_regs_t *i2c, uint8_t *txData, uint8_t *rxData)
{
int i2cNum;
int txReqSel = -1;
int rxReqSel = -1;
if (i2c == NULL) {
return E_NULL_PTR;
}
i2cNum = MXC_I2C_GET_IDX((mxc_i2c_regs_t *)i2c);
if (txData != NULL) {
switch (i2cNum) {
case 0:
txReqSel = MXC_DMA_REQUEST_I2C0TX;
break;
case 1:
txReqSel = MXC_DMA_REQUEST_I2C1TX;
break;
case 2:
txReqSel = MXC_DMA_REQUEST_I2C2TX;
break;
default:
return E_BAD_PARAM;
}
}
if (rxData != NULL) {
switch (i2cNum) {
case 0:
rxReqSel = MXC_DMA_REQUEST_I2C0RX;
break;
case 1:
rxReqSel = MXC_DMA_REQUEST_I2C1RX;
break;
case 2:
rxReqSel = MXC_DMA_REQUEST_I2C2RX;
break;
default:
return E_BAD_PARAM;
}
}
return MXC_I2C_RevA_DMA_SetRequestSelect((mxc_i2c_reva_regs_t *)i2c,
(mxc_dma_reva_regs_t *)MXC_DMA, txReqSel, rxReqSel);
}
/* ************************************************************************* */
/* Low-level functions */
/* ************************************************************************* */
int MXC_I2C_Start(mxc_i2c_regs_t *i2c)
{
return MXC_I2C_RevA_Start((mxc_i2c_reva_regs_t *)i2c);
}
int MXC_I2C_Stop(mxc_i2c_regs_t *i2c)
{
return MXC_I2C_RevA_Stop((mxc_i2c_reva_regs_t *)i2c);
}
int MXC_I2C_WriteByte(mxc_i2c_regs_t *i2c, unsigned char byte)
{
return MXC_I2C_RevA_WriteByte((mxc_i2c_reva_regs_t *)i2c, byte);
}
int MXC_I2C_ReadByte(mxc_i2c_regs_t *i2c, unsigned char *byte, int ack)
{
return MXC_I2C_RevA_ReadByte((mxc_i2c_reva_regs_t *)i2c, byte, ack);
}
int MXC_I2C_ReadByteInteractive(mxc_i2c_regs_t *i2c, unsigned char *byte, mxc_i2c_getAck_t getAck)
{
return MXC_I2C_RevA_ReadByteInteractive((mxc_i2c_reva_regs_t *)i2c, byte,
(mxc_i2c_reva_getAck_t)getAck);
}
int MXC_I2C_Write(mxc_i2c_regs_t *i2c, unsigned char *bytes, unsigned int *len)
{
return MXC_I2C_RevA_Write((mxc_i2c_reva_regs_t *)i2c, bytes, len);
}
int MXC_I2C_Read(mxc_i2c_regs_t *i2c, unsigned char *bytes, unsigned int *len, int ack)
{
return MXC_I2C_RevA_Read((mxc_i2c_reva_regs_t *)i2c, bytes, len, ack);
}
int MXC_I2C_ReadRXFIFO(mxc_i2c_regs_t *i2c, volatile unsigned char *bytes, unsigned int len)
{
return MXC_I2C_RevA_ReadRXFIFO((mxc_i2c_reva_regs_t *)i2c, bytes, len);
}
int MXC_I2C_ReadRXFIFODMA(mxc_i2c_regs_t *i2c, unsigned char *bytes, unsigned int len,
mxc_i2c_dma_complete_cb_t callback, mxc_dma_regs_t *dma)
{
// The callback parameter was previously unused but keeping it for backwards-compatibility.
return MXC_I2C_RevA_ReadRXFIFODMA((mxc_i2c_reva_regs_t *)i2c, bytes, len, dma);
}
int MXC_I2C_GetRXFIFOAvailable(mxc_i2c_regs_t *i2c)
{
return MXC_I2C_RevA_GetRXFIFOAvailable((mxc_i2c_reva_regs_t *)i2c);
}
int MXC_I2C_WriteTXFIFO(mxc_i2c_regs_t *i2c, volatile unsigned char *bytes, unsigned int len)
{
return MXC_I2C_RevA_WriteTXFIFO((mxc_i2c_reva_regs_t *)i2c, bytes, len);
}
int MXC_I2C_WriteTXFIFODMA(mxc_i2c_regs_t *i2c, unsigned char *bytes, unsigned int len,
mxc_i2c_dma_complete_cb_t callback, mxc_dma_regs_t *dma)
{
// The callback parameter was previously unused but keeping it for backwards-compatibility.
return MXC_I2C_RevA_WriteTXFIFODMA((mxc_i2c_reva_regs_t *)i2c, bytes, len, dma);
}
int MXC_I2C_GetTXFIFOAvailable(mxc_i2c_regs_t *i2c)
{
return MXC_I2C_RevA_GetTXFIFOAvailable((mxc_i2c_reva_regs_t *)i2c);
}
void MXC_I2C_ClearRXFIFO(mxc_i2c_regs_t *i2c)
{
MXC_I2C_RevA_ClearRXFIFO((mxc_i2c_reva_regs_t *)i2c);
}
void MXC_I2C_ClearTXFIFO(mxc_i2c_regs_t *i2c)
{
MXC_I2C_RevA_ClearTXFIFO((mxc_i2c_reva_regs_t *)i2c);
}
int MXC_I2C_GetFlags(mxc_i2c_regs_t *i2c, unsigned int *flags0, unsigned int *flags1)
{
return MXC_I2C_RevA_GetFlags((mxc_i2c_reva_regs_t *)i2c, flags0, flags1);
}
void MXC_I2C_ClearFlags(mxc_i2c_regs_t *i2c, unsigned int flags0, unsigned int flags1)
{
MXC_I2C_RevA_ClearFlags((mxc_i2c_reva_regs_t *)i2c, flags0, flags1);
}
void MXC_I2C_EnableInt(mxc_i2c_regs_t *i2c, unsigned int flags0, unsigned int flags1)
{
MXC_I2C_RevA_EnableInt((mxc_i2c_reva_regs_t *)i2c, flags0, flags1);
}
void MXC_I2C_DisableInt(mxc_i2c_regs_t *i2c, unsigned int flags0, unsigned int flags1)
{
MXC_I2C_RevA_DisableInt((mxc_i2c_reva_regs_t *)i2c, flags0, flags1);
}
void MXC_I2C_EnablePreload(mxc_i2c_regs_t *i2c)
{
MXC_I2C_RevA_EnablePreload((mxc_i2c_reva_regs_t *)i2c);
}
void MXC_I2C_DisablePreload(mxc_i2c_regs_t *i2c)
{
MXC_I2C_RevA_DisablePreload((mxc_i2c_reva_regs_t *)i2c);
}
void MXC_I2C_EnableGeneralCall(mxc_i2c_regs_t *i2c)
{
MXC_I2C_RevA_EnableGeneralCall((mxc_i2c_reva_regs_t *)i2c);
}
void MXC_I2C_DisableGeneralCall(mxc_i2c_regs_t *i2c)
{
MXC_I2C_RevA_DisableGeneralCall((mxc_i2c_reva_regs_t *)i2c);
}
void MXC_I2C_SetTimeout(mxc_i2c_regs_t *i2c, unsigned int timeout)
{
MXC_I2C_RevA_SetTimeout((mxc_i2c_reva_regs_t *)i2c, timeout);
}
unsigned int MXC_I2C_GetTimeout(mxc_i2c_regs_t *i2c)
{
return MXC_I2C_RevA_GetTimeout((mxc_i2c_reva_regs_t *)i2c);
}
int MXC_I2C_Recover(mxc_i2c_regs_t *i2c, unsigned int retries)
{
return MXC_I2C_RevA_Recover((mxc_i2c_reva_regs_t *)i2c, retries);
}
/* ************************************************************************* */
/* Transaction level functions */
/* ************************************************************************* */
int MXC_I2C_MasterTransaction(mxc_i2c_req_t *req)
{
return MXC_I2C_RevA_MasterTransaction((mxc_i2c_reva_req_t *)req);
}
int MXC_I2C_MasterTransactionAsync(mxc_i2c_req_t *req)
{
return MXC_I2C_RevA_MasterTransactionAsync((mxc_i2c_reva_req_t *)req);
}
int MXC_I2C_MasterTransactionDMA(mxc_i2c_req_t *req, mxc_dma_regs_t *dma)
{
if (MXC_DMA_GET_IDX(dma) == -1) {
return E_BAD_PARAM;
}
return MXC_I2C_RevA_MasterTransactionDMA((mxc_i2c_reva_req_t *)req, dma);
}
int MXC_I2C_SlaveTransaction(mxc_i2c_regs_t *i2c, mxc_i2c_slave_handler_t callback)
{
return MXC_I2C_RevA_SlaveTransaction((mxc_i2c_reva_regs_t *)i2c,
(mxc_i2c_reva_slave_handler_t)callback, interruptCheck);
}
int MXC_I2C_SlaveTransactionAsync(mxc_i2c_regs_t *i2c, mxc_i2c_slave_handler_t callback)
{
return MXC_I2C_RevA_SlaveTransactionAsync(
(mxc_i2c_reva_regs_t *)i2c, (mxc_i2c_reva_slave_handler_t)callback, interruptCheck);
}
int MXC_I2C_SetRXThreshold(mxc_i2c_regs_t *i2c, unsigned int numBytes)
{
return MXC_I2C_RevA_SetRXThreshold((mxc_i2c_reva_regs_t *)i2c, numBytes);
}
unsigned int MXC_I2C_GetRXThreshold(mxc_i2c_regs_t *i2c)
{
return MXC_I2C_RevA_GetRXThreshold((mxc_i2c_reva_regs_t *)i2c);
}
int MXC_I2C_SetTXThreshold(mxc_i2c_regs_t *i2c, unsigned int numBytes)
{
return MXC_I2C_RevA_SetTXThreshold((mxc_i2c_reva_regs_t *)i2c, numBytes);
}
unsigned int MXC_I2C_GetTXThreshold(mxc_i2c_regs_t *i2c)
{
return MXC_I2C_RevA_GetTXThreshold((mxc_i2c_reva_regs_t *)i2c);
}
void MXC_I2C_AsyncStop(mxc_i2c_regs_t *i2c)
{
MXC_I2C_RevA_AsyncStop((mxc_i2c_reva_regs_t *)i2c);
}
void MXC_I2C_AbortAsync(mxc_i2c_regs_t *i2c)
{
MXC_I2C_RevA_AbortAsync((mxc_i2c_reva_regs_t *)i2c);
}
void MXC_I2C_AsyncHandler(mxc_i2c_regs_t *i2c)
{
MXC_I2C_RevA_AsyncHandler((mxc_i2c_reva_regs_t *)i2c, interruptCheck);
}
void MXC_I2C_DMACallback(int ch, int error)
{
MXC_I2C_RevA_DMACallback(ch, error);
}