Skip to content

Commit 2962a86

Browse files
committed
2 parents 9b44734 + 8b0f50b commit 2962a86

File tree

1 file changed

+122
-11
lines changed

1 file changed

+122
-11
lines changed

bsp/imxrt/libraries/drivers/drv_i2c.c

Lines changed: 122 additions & 11 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,6 +267,37 @@ 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
{
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+
}
300+
224301
if (LPI2C_MasterStart(imxrt_i2c->I2C, imxrt_i2c->msg[i].addr, kLPI2C_Read) != kStatus_Success)
225302
{
226303
i = 0;
@@ -244,9 +321,17 @@ static rt_size_t imxrt_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
244321
i = 0;
245322
break;
246323
}
247-
248-
while (LPI2C_MasterGetStatusFlags(imxrt_i2c->I2C) & kLPI2C_MasterNackDetectFlag)
324+
325+
if(LPI2C_MasterWaitForTxFifoAllEmpty(imxrt_i2c->I2C) != kStatus_Success)
249326
{
327+
i = 0;
328+
break;
329+
}
330+
331+
if (LPI2C_MasterGetStatusFlags(imxrt_i2c->I2C) & kLPI2C_MasterNackDetectFlag)
332+
{
333+
i = 0;
334+
break;
250335
}
251336

252337
if (LPI2C_MasterSend(imxrt_i2c->I2C, imxrt_i2c->msg[i].buf, imxrt_i2c->msg[i].len) != kStatus_Success)
@@ -261,11 +346,12 @@ static rt_size_t imxrt_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
261346
break;
262347
}
263348
}
264-
}
265-
266-
if (LPI2C_MasterStop(imxrt_i2c->I2C) != kStatus_Success)
267-
{
268-
i = 0;
349+
350+
if (LPI2C_MasterStop(imxrt_i2c->I2C) != kStatus_Success)
351+
{
352+
i = 0;
353+
}
354+
269355
}
270356

271357
imxrt_i2c->msg = RT_NULL;
@@ -341,6 +427,31 @@ int rt_hw_i2c_init(void)
341427
rt_i2c_bus_device_register(&lpi2c4.parent, lpi2c4.device_name);
342428
#endif /* BSP_USING_I2C4 */
343429

430+
#if defined(BSP_USING_I2C5)
431+
LPI2C_MasterGetDefaultConfig(&masterConfig);
432+
#if defined(HW_I2C5_BADURATE_400kHZ)
433+
masterConfig.baudRate_Hz = 400000U;
434+
#elif defined(HW_I2C5_BADURATE_100kHZ)
435+
masterConfig.baudRate_Hz = 100000U;
436+
#endif /* HW_I2C5_BADURATE_400kHZ */
437+
lpi2c5.clock_root = kCLOCK_Root_Lpi2c5;
438+
imxrt_lpi2c_configure(&lpi2c5, &masterConfig);
439+
rt_i2c_bus_device_register(&lpi2c5.parent, lpi2c5.device_name);
440+
#endif /* BSP_USING_I2C5 */
441+
442+
#if defined(BSP_USING_I2C6)
443+
LPI2C_MasterGetDefaultConfig(&masterConfig);
444+
#if defined(HW_I2C6_BADURATE_400kHZ)
445+
masterConfig.baudRate_Hz = 400000U;
446+
#elif defined(HW_I2C6_BADURATE_100kHZ)
447+
masterConfig.baudRate_Hz = 100000U;
448+
#endif /* HW_I2C6_BADURATE_400kHZ */
449+
lpi2c6.clock_root = kCLOCK_Root_Lpi2c6;
450+
imxrt_lpi2c_configure(&lpi2c6, &masterConfig);
451+
rt_i2c_bus_device_register(&lpi2c6.parent, lpi2c6.device_name);
452+
#endif /* BSP_USING_I2C6 */
453+
454+
344455
#endif /* MIMXRT1015_SERIES */
345456

346457
return 0;

0 commit comments

Comments
 (0)