forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c_imx.c
400 lines (328 loc) · 9.38 KB
/
i2c_imx.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
/*
* Copyright (c) 2018 Diego Sueiro, <[email protected]>
* Copyright 2022 NXP
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT fsl_imx21_i2c
#include <errno.h>
#include <zephyr/drivers/i2c.h>
#include <soc.h>
#include <i2c_imx.h>
#include <zephyr/sys/util.h>
#include <zephyr/drivers/pinctrl.h>
#include <zephyr/logging/log.h>
#include <zephyr/irq.h>
LOG_MODULE_REGISTER(i2c_imx);
#include "i2c-priv.h"
#define DEV_BASE(dev) \
((I2C_Type *)((const struct i2c_imx_config * const)(dev)->config)->base)
struct i2c_imx_config {
I2C_Type *base;
void (*irq_config_func)(const struct device *dev);
uint32_t bitrate;
const struct pinctrl_dev_config *pincfg;
};
struct i2c_master_transfer {
const uint8_t *txBuff;
volatile uint8_t *rxBuff;
volatile uint32_t cmdSize;
volatile uint32_t txSize;
volatile uint32_t rxSize;
volatile bool isBusy;
volatile uint32_t currentDir;
volatile uint32_t currentMode;
volatile bool ack;
};
struct i2c_imx_data {
struct i2c_master_transfer transfer;
struct k_sem device_sync_sem;
};
static bool i2c_imx_write(const struct device *dev, uint8_t *txBuffer,
uint32_t txSize)
{
I2C_Type *base = DEV_BASE(dev);
struct i2c_imx_data *data = dev->data;
struct i2c_master_transfer *transfer = &data->transfer;
transfer->isBusy = true;
/* Clear I2C interrupt flag to avoid spurious interrupt */
I2C_ClearStatusFlag(base, i2cStatusInterrupt);
/* Set I2C work under Tx mode */
I2C_SetDirMode(base, i2cDirectionTransmit);
transfer->currentDir = i2cDirectionTransmit;
transfer->txBuff = txBuffer;
transfer->txSize = txSize;
I2C_WriteByte(base, *transfer->txBuff);
transfer->txBuff++;
transfer->txSize--;
/* Enable I2C interrupt, subsequent data transfer will be handled
* in ISR.
*/
I2C_SetIntCmd(base, true);
/* Wait for the transfer to complete */
k_sem_take(&data->device_sync_sem, K_FOREVER);
return transfer->ack;
}
static void i2c_imx_read(const struct device *dev, uint8_t *rxBuffer,
uint32_t rxSize)
{
I2C_Type *base = DEV_BASE(dev);
struct i2c_imx_data *data = dev->data;
struct i2c_master_transfer *transfer = &data->transfer;
transfer->isBusy = true;
/* Clear I2C interrupt flag to avoid spurious interrupt */
I2C_ClearStatusFlag(base, i2cStatusInterrupt);
/* Change to receive state. */
I2C_SetDirMode(base, i2cDirectionReceive);
transfer->currentDir = i2cDirectionReceive;
transfer->rxBuff = rxBuffer;
transfer->rxSize = rxSize;
if (transfer->rxSize == 1U) {
/* Send Nack */
I2C_SetAckBit(base, false);
} else {
/* Send Ack */
I2C_SetAckBit(base, true);
}
/* dummy read to clock in 1st byte */
I2C_ReadByte(base);
/* Enable I2C interrupt, subsequent data transfer will be handled
* in ISR.
*/
I2C_SetIntCmd(base, true);
/* Wait for the transfer to complete */
k_sem_take(&data->device_sync_sem, K_FOREVER);
}
static int i2c_imx_configure(const struct device *dev,
uint32_t dev_config_raw)
{
I2C_Type *base = DEV_BASE(dev);
struct i2c_imx_data *data = dev->data;
struct i2c_master_transfer *transfer = &data->transfer;
uint32_t baudrate;
if (!(I2C_MODE_CONTROLLER & dev_config_raw)) {
return -EINVAL;
}
if (I2C_ADDR_10_BITS & dev_config_raw) {
return -EINVAL;
}
/* Initialize I2C state structure content. */
transfer->txBuff = 0;
transfer->rxBuff = 0;
transfer->cmdSize = 0U;
transfer->txSize = 0U;
transfer->rxSize = 0U;
transfer->isBusy = false;
transfer->currentDir = i2cDirectionReceive;
transfer->currentMode = i2cModeSlave;
switch (I2C_SPEED_GET(dev_config_raw)) {
case I2C_SPEED_STANDARD:
baudrate = KHZ(100);
break;
case I2C_SPEED_FAST:
baudrate = KHZ(400);
break;
case I2C_SPEED_FAST_PLUS:
baudrate = MHZ(1);
break;
default:
return -EINVAL;
}
/* Setup I2C init structure. */
i2c_init_config_t i2cInitConfig = {
.baudRate = baudrate,
.slaveAddress = 0x00
};
i2cInitConfig.clockRate = get_i2c_clock_freq(base);
I2C_Init(base, &i2cInitConfig);
I2C_Enable(base);
return 0;
}
static int i2c_imx_send_addr(const struct device *dev, uint16_t addr,
uint8_t flags)
{
uint8_t byte0 = addr << 1;
byte0 |= (flags & I2C_MSG_RW_MASK) == I2C_MSG_READ;
return i2c_imx_write(dev, &byte0, 1);
}
static int i2c_imx_transfer(const struct device *dev, struct i2c_msg *msgs,
uint8_t num_msgs, uint16_t addr)
{
I2C_Type *base = DEV_BASE(dev);
struct i2c_imx_data *data = dev->data;
struct i2c_master_transfer *transfer = &data->transfer;
uint16_t timeout = UINT16_MAX;
int result = -EIO;
if (!num_msgs) {
return 0;
}
/* Wait until bus not busy */
while ((I2C_I2SR_REG(base) & i2cStatusBusBusy) && (--timeout)) {
}
if (timeout == 0U) {
return result;
}
/* Make sure we're in a good state so slave recognises the Start */
I2C_SetWorkMode(base, i2cModeSlave);
transfer->currentMode = i2cModeSlave;
/* Switch back to Rx direction. */
I2C_SetDirMode(base, i2cDirectionReceive);
transfer->currentDir = i2cDirectionReceive;
/* Start condition */
I2C_SetDirMode(base, i2cDirectionTransmit);
transfer->currentDir = i2cDirectionTransmit;
I2C_SetWorkMode(base, i2cModeMaster);
transfer->currentMode = i2cModeMaster;
/* Send address after any Start condition */
if (!i2c_imx_send_addr(dev, addr, msgs->flags)) {
goto finish; /* No ACK received */
}
do {
if (msgs->flags & I2C_MSG_RESTART) {
I2C_SendRepeatStart(base);
if (!i2c_imx_send_addr(dev, addr, msgs->flags)) {
goto finish; /* No ACK received */
}
}
/* Transfer data */
if (msgs->len) {
if ((msgs->flags & I2C_MSG_RW_MASK) == I2C_MSG_READ) {
i2c_imx_read(dev, msgs->buf, msgs->len);
} else {
if (!i2c_imx_write(dev, msgs->buf, msgs->len)) {
goto finish; /* No ACK received */
}
}
}
if (msgs->flags & I2C_MSG_STOP) {
I2C_SetWorkMode(base, i2cModeSlave);
transfer->currentMode = i2cModeSlave;
I2C_SetDirMode(base, i2cDirectionReceive);
transfer->currentDir = i2cDirectionReceive;
}
/* Next message */
msgs++;
num_msgs--;
} while (num_msgs);
/* Complete without error */
result = 0;
return result;
finish:
I2C_SetWorkMode(base, i2cModeSlave);
transfer->currentMode = i2cModeSlave;
I2C_SetDirMode(base, i2cDirectionReceive);
transfer->currentDir = i2cDirectionReceive;
return result;
}
static void i2c_imx_isr(const struct device *dev)
{
I2C_Type *base = DEV_BASE(dev);
struct i2c_imx_data *data = dev->data;
struct i2c_master_transfer *transfer = &data->transfer;
/* Clear interrupt flag. */
I2C_ClearStatusFlag(base, i2cStatusInterrupt);
/* Exit the ISR if no transfer is happening for this instance. */
if (!transfer->isBusy) {
return;
}
if (i2cModeMaster == transfer->currentMode) {
if (i2cDirectionTransmit == transfer->currentDir) {
/* Normal write operation. */
transfer->ack =
!(I2C_GetStatusFlag(base, i2cStatusReceivedAck));
if (transfer->txSize == 0U) {
/* Close I2C interrupt. */
I2C_SetIntCmd(base, false);
/* Release I2C Bus. */
transfer->isBusy = false;
k_sem_give(&data->device_sync_sem);
} else {
I2C_WriteByte(base, *transfer->txBuff);
transfer->txBuff++;
transfer->txSize--;
}
} else {
/* Normal read operation. */
if (transfer->rxSize == 2U) {
/* Send Nack */
I2C_SetAckBit(base, false);
} else {
/* Send Ack */
I2C_SetAckBit(base, true);
}
if (transfer->rxSize == 1U) {
/* Switch back to Tx direction to avoid
* additional I2C bus read.
*/
I2C_SetDirMode(base, i2cDirectionTransmit);
transfer->currentDir = i2cDirectionTransmit;
}
*transfer->rxBuff = I2C_ReadByte(base);
transfer->rxBuff++;
transfer->rxSize--;
/* receive finished. */
if (transfer->rxSize == 0U) {
/* Close I2C interrupt. */
I2C_SetIntCmd(base, false);
/* Release I2C Bus. */
transfer->isBusy = false;
k_sem_give(&data->device_sync_sem);
}
}
}
}
static int i2c_imx_init(const struct device *dev)
{
const struct i2c_imx_config *config = dev->config;
struct i2c_imx_data *data = dev->data;
uint32_t bitrate_cfg;
int error;
k_sem_init(&data->device_sync_sem, 0, K_SEM_MAX_LIMIT);
error = pinctrl_apply_state(config->pincfg, PINCTRL_STATE_DEFAULT);
if (error) {
return error;
}
bitrate_cfg = i2c_map_dt_bitrate(config->bitrate);
error = i2c_imx_configure(dev, I2C_MODE_CONTROLLER | bitrate_cfg);
if (error) {
return error;
}
config->irq_config_func(dev);
return 0;
}
static const struct i2c_driver_api i2c_imx_driver_api = {
.configure = i2c_imx_configure,
.transfer = i2c_imx_transfer,
};
#define I2C_IMX_INIT(n) \
PINCTRL_DT_INST_DEFINE(n); \
static void i2c_imx_config_func_##n(const struct device *dev); \
\
static const struct i2c_imx_config i2c_imx_config_##n = { \
.base = (I2C_Type *)DT_INST_REG_ADDR(n), \
.irq_config_func = i2c_imx_config_func_##n, \
.bitrate = DT_INST_PROP(n, clock_frequency), \
.pincfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
}; \
\
static struct i2c_imx_data i2c_imx_data_##n; \
\
I2C_DEVICE_DT_INST_DEFINE(n, \
i2c_imx_init, \
NULL, \
&i2c_imx_data_##n, &i2c_imx_config_##n, \
POST_KERNEL, \
CONFIG_I2C_INIT_PRIORITY, \
&i2c_imx_driver_api); \
\
static void i2c_imx_config_func_##n(const struct device *dev) \
{ \
ARG_UNUSED(dev); \
\
IRQ_CONNECT(DT_INST_IRQN(n), \
DT_INST_IRQ(n, priority), \
i2c_imx_isr, DEVICE_DT_INST_GET(n), 0); \
\
irq_enable(DT_INST_IRQN(n)); \
}
DT_INST_FOREACH_STATUS_OKAY(I2C_IMX_INIT)