Skip to content

Commit 3be35fe

Browse files
committed
Update i2c driver, add read ACK and BUS status check
1 parent 2e15ec2 commit 3be35fe

File tree

1 file changed

+121
-26
lines changed

1 file changed

+121
-26
lines changed

bsp/imxrt/libraries/drivers/drv_i2c.c

Lines changed: 121 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#define LOG_TAG "drv.i2c"
1919
#include <drv_log.h>
2020

21-
#if !defined(BSP_USING_I2C1) && !defined(BSP_USING_I2C2) && !defined(BSP_USING_I2C3) && !defined(BSP_USING_I2C4)
21+
#if !defined(BSP_USING_I2C1) && !defined(BSP_USING_I2C2) && !defined(BSP_USING_I2C3) && !defined(BSP_USING_I2C4) && !defined(BSP_USING_I2C5)&& !defined(BSP_USING_I2C6)
2222
#error "Please define at least one BSP_USING_I2Cx"
2323
#endif
2424

@@ -35,6 +35,9 @@ struct imxrt_i2c_bus
3535
volatile rt_uint32_t msg_ptr;
3636
volatile rt_uint32_t dptr;
3737
char *device_name;
38+
#ifdef SOC_IMXRT1170_SERIES
39+
clock_root_t clock_root;
40+
#endif
3841
};
3942

4043
#if defined (BSP_USING_I2C1)
@@ -55,13 +58,28 @@ struct imxrt_i2c_bus
5558
#define I2C4BUS_NAME "i2c4"
5659
#endif /*BSP_USING_I2C4*/
5760

61+
#if defined (BSP_USING_I2C5)
62+
#define I2C5BUS_NAME "i2c5"
63+
#endif /*BSP_USING_I2C5*/
64+
65+
#if defined (BSP_USING_I2C6)
66+
#define I2C6BUS_NAME "i2c6"
67+
#endif /*BSP_USING_I2C6*/
68+
5869
#endif /* MIMXRT1015_SERIES */
5970

60-
#define LPI2C_CLOCK_SOURCE_DIVIDER 4
71+
/* Select USB1 PLL (360 MHz) as master lpi2c clock source */
72+
#define LPI2C_CLOCK_SOURCE_SELECT (1U)
73+
#ifdef SOC_IMXRT1170_SERIES
74+
/* Clock divider for master lpi2c clock source */
75+
#define LPI2C_CLOCK_SOURCE_DIVIDER (12U)
76+
#else
77+
#define LPI2C_CLOCK_SOURCE_DIVIDER (0U)
6178

6279
/* Get frequency of lpi2c clock */
63-
#define LPI2C_CLOCK_FREQUENCY ((CLOCK_GetFreq(kCLOCK_Usb1PllClk) / 8) / (LPI2C_CLOCK_SOURCE_DIVIDER))
80+
#define LPI2C_CLOCK_FREQUENCY ((CLOCK_GetFreq(kCLOCK_Usb1PllClk) / 8) / (LPI2C_CLOCK_SOURCE_DIVIDER + 1U))
6481

82+
#endif
6583
#ifdef BSP_USING_I2C1
6684
static struct imxrt_i2c_bus lpi2c1 =
6785
{
@@ -96,9 +114,25 @@ static struct imxrt_i2c_bus lpi2c4 =
96114
};
97115
#endif /* RT_USING_HW_I2C4 */
98116

117+
#ifdef BSP_USING_I2C5
118+
static struct imxrt_i2c_bus lpi2c5 =
119+
{
120+
.I2C = LPI2C5,
121+
.device_name = I2C5BUS_NAME,
122+
};
123+
#endif /* RT_USING_HW_I2C5 */
124+
125+
#ifdef BSP_USING_I2C6
126+
static struct imxrt_i2c_bus lpi2c6 =
127+
{
128+
.I2C = LPI2C6,
129+
.device_name = I2C6BUS_NAME,
130+
};
131+
#endif /* RT_USING_HW_I2C6 */
132+
99133
#endif /* MIMXRT1015_SERIES */
100134

101-
#if (defined(BSP_USING_I2C1) || defined(BSP_USING_I2C2) || defined(BSP_USING_I2C3) || defined(BSP_USING_I2C4))
135+
#if (defined(BSP_USING_I2C1) || defined(BSP_USING_I2C2) || defined(BSP_USING_I2C3) || defined(BSP_USING_I2C4) ||defined(BSP_USING_I2C5) || defined(BSP_USING_I2C6))
102136

103137
static rt_size_t imxrt_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
104138
struct rt_i2c_msg msgs[],
@@ -123,7 +157,19 @@ static rt_err_t imxrt_lpi2c_configure(struct imxrt_i2c_bus *bus, lpi2c_master_co
123157
RT_ASSERT(cfg != RT_NULL);
124158

125159
bus->parent.ops = &imxrt_i2c_ops;
160+
#ifdef SOC_IMXRT1170_SERIES
161+
clock_root_config_t rootCfg = {0};
162+
rootCfg.mux = LPI2C_CLOCK_SOURCE_SELECT;
163+
rootCfg.div = LPI2C_CLOCK_SOURCE_DIVIDER + 1;
164+
CLOCK_SetRootClock(bus->clock_root, &rootCfg);
165+
volatile uint32_t freq = CLOCK_GetRootClockFreq(bus->clock_root);
166+
LPI2C_MasterInit(bus->I2C, cfg, freq);
167+
#else
168+
CLOCK_SetMux(kCLOCK_Lpi2cMux, LPI2C_CLOCK_SOURCE_SELECT);
169+
CLOCK_SetDiv(kCLOCK_Lpi2cDiv, LPI2C_CLOCK_SOURCE_DIVIDER);
126170
LPI2C_MasterInit(bus->I2C, cfg, LPI2C_CLOCK_FREQUENCY);
171+
#endif
172+
127173
return RT_EOK;
128174
}
129175

@@ -221,21 +267,36 @@ static rt_size_t imxrt_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
221267
{
222268
if (imxrt_i2c->msg[i].flags & RT_I2C_RD)
223269
{
224-
if (LPI2C_MasterStart(imxrt_i2c->I2C, imxrt_i2c->msg[i].addr, kLPI2C_Write) != kStatus_Success)
225-
{
226-
i = 0;
227-
break;
228-
}
229-
230-
while (LPI2C_MasterGetStatusFlags(imxrt_i2c->I2C) & kLPI2C_MasterNackDetectFlag)
231-
{
232-
}
233-
234-
if (LPI2C_MasterRepeatedStart(imxrt_i2c->I2C, imxrt_i2c->msg[i].addr, kLPI2C_Read) != kStatus_Success)
235-
{
236-
i = 0;
237-
break;
238-
}
270+
if ((imxrt_i2c->msg[i].flags & RT_I2C_NO_START) != RT_I2C_NO_START)
271+
{
272+
if (LPI2C_MasterStart(imxrt_i2c->I2C, imxrt_i2c->msg[i].addr, kLPI2C_Write) != kStatus_Success)
273+
{
274+
i = 0;
275+
break;
276+
}
277+
278+
while (LPI2C_MasterGetStatusFlags(imxrt_i2c->I2C) & kLPI2C_MasterNackDetectFlag)
279+
{
280+
}
281+
282+
if (LPI2C_MasterRepeatedStart(imxrt_i2c->I2C, imxrt_i2c->msg[i].addr, kLPI2C_Read) != kStatus_Success)
283+
{
284+
i = 0;
285+
break;
286+
}
287+
}
288+
else
289+
{
290+
if (LPI2C_MasterStart(imxrt_i2c->I2C, imxrt_i2c->msg[i].addr, kLPI2C_Read) != kStatus_Success)
291+
{
292+
i = 0;
293+
break;
294+
}
295+
296+
while (LPI2C_MasterGetStatusFlags(imxrt_i2c->I2C) & kLPI2C_MasterNackDetectFlag)
297+
{
298+
}
299+
}
239300

240301
if (LPI2C_MasterReceive(imxrt_i2c->I2C, imxrt_i2c->msg[i].buf, imxrt_i2c->msg[i].len) != kStatus_Success)
241302
{
@@ -250,9 +311,17 @@ static rt_size_t imxrt_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
250311
i = 0;
251312
break;
252313
}
253-
254-
while (LPI2C_MasterGetStatusFlags(imxrt_i2c->I2C) & kLPI2C_MasterNackDetectFlag)
314+
315+
if(LPI2C_MasterWaitForTxFifoAllEmpty(imxrt_i2c->I2C) != kStatus_Success)
316+
{
317+
i = 0;
318+
break;
319+
}
320+
321+
if (LPI2C_MasterGetStatusFlags(imxrt_i2c->I2C) & kLPI2C_MasterNackDetectFlag)
255322
{
323+
i = 0;
324+
break;
256325
}
257326

258327
if (LPI2C_MasterSend(imxrt_i2c->I2C, imxrt_i2c->msg[i].buf, imxrt_i2c->msg[i].len) != kStatus_Success)
@@ -267,11 +336,12 @@ static rt_size_t imxrt_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
267336
break;
268337
}
269338
}
270-
}
271-
272-
if (LPI2C_MasterStop(imxrt_i2c->I2C) != kStatus_Success)
273-
{
274-
i = 0;
339+
340+
if (LPI2C_MasterStop(imxrt_i2c->I2C) != kStatus_Success)
341+
{
342+
i = 0;
343+
}
344+
275345
}
276346

277347
imxrt_i2c->msg = RT_NULL;
@@ -347,6 +417,31 @@ int rt_hw_i2c_init(void)
347417
rt_i2c_bus_device_register(&lpi2c4.parent, lpi2c4.device_name);
348418
#endif /* BSP_USING_I2C4 */
349419

420+
#if defined(BSP_USING_I2C5)
421+
LPI2C_MasterGetDefaultConfig(&masterConfig);
422+
#if defined(HW_I2C5_BADURATE_400kHZ)
423+
masterConfig.baudRate_Hz = 400000U;
424+
#elif defined(HW_I2C5_BADURATE_100kHZ)
425+
masterConfig.baudRate_Hz = 100000U;
426+
#endif /* HW_I2C5_BADURATE_400kHZ */
427+
lpi2c5.clock_root = kCLOCK_Root_Lpi2c5;
428+
imxrt_lpi2c_configure(&lpi2c5, &masterConfig);
429+
rt_i2c_bus_device_register(&lpi2c5.parent, lpi2c5.device_name);
430+
#endif /* BSP_USING_I2C5 */
431+
432+
#if defined(BSP_USING_I2C6)
433+
LPI2C_MasterGetDefaultConfig(&masterConfig);
434+
#if defined(HW_I2C6_BADURATE_400kHZ)
435+
masterConfig.baudRate_Hz = 400000U;
436+
#elif defined(HW_I2C6_BADURATE_100kHZ)
437+
masterConfig.baudRate_Hz = 100000U;
438+
#endif /* HW_I2C6_BADURATE_400kHZ */
439+
lpi2c6.clock_root = kCLOCK_Root_Lpi2c6;
440+
imxrt_lpi2c_configure(&lpi2c6, &masterConfig);
441+
rt_i2c_bus_device_register(&lpi2c6.parent, lpi2c6.device_name);
442+
#endif /* BSP_USING_I2C6 */
443+
444+
350445
#endif /* MIMXRT1015_SERIES */
351446

352447
return 0;

0 commit comments

Comments
 (0)