2018-12-05 19:04:47 +08:00
|
|
|
/*
|
2021-03-17 02:26:35 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2018-12-05 19:04:47 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2021-03-17 02:26:35 +08:00
|
|
|
#include "drv_i2c.h"
|
|
|
|
|
|
|
|
#include "fsl_common.h"
|
2018-12-05 19:04:47 +08:00
|
|
|
#include "fsl_iocon.h"
|
|
|
|
#include "fsl_i2c.h"
|
|
|
|
|
|
|
|
struct lpc_i2c
|
|
|
|
{
|
2021-03-17 02:26:35 +08:00
|
|
|
struct rt_i2c_bus_device bus;
|
|
|
|
|
2018-12-05 19:04:47 +08:00
|
|
|
I2C_Type *base;
|
2021-03-17 02:26:35 +08:00
|
|
|
|
2018-12-05 19:04:47 +08:00
|
|
|
char *device_name;
|
|
|
|
};
|
|
|
|
|
|
|
|
static uint32_t get_i2c_freq(I2C_Type *base)
|
|
|
|
{
|
|
|
|
uint32_t freq = 0;
|
|
|
|
|
|
|
|
#if defined(BSP_USING_I2C4)
|
|
|
|
if(base == I2C4)
|
|
|
|
{
|
|
|
|
freq = CLOCK_GetFreq(kCLOCK_Flexcomm4);
|
|
|
|
}
|
2021-03-17 02:26:35 +08:00
|
|
|
#endif
|
2018-12-05 19:04:47 +08:00
|
|
|
|
2021-03-17 02:26:35 +08:00
|
|
|
return freq;
|
2018-12-05 19:04:47 +08:00
|
|
|
}
|
|
|
|
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t master_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num)
|
2018-12-05 19:04:47 +08:00
|
|
|
{
|
2023-03-10 12:42:58 +08:00
|
|
|
rt_ssize_t ret = (0);
|
2018-12-05 19:04:47 +08:00
|
|
|
|
|
|
|
rt_uint32_t index = 0;
|
|
|
|
struct lpc_i2c *lpc_i2c = RT_NULL;
|
|
|
|
struct rt_i2c_msg *msg = RT_NULL;
|
2019-11-11 22:46:28 +08:00
|
|
|
i2c_direction_t direction;
|
|
|
|
status_t result = kStatus_Success;
|
2018-12-05 19:04:47 +08:00
|
|
|
|
2021-03-17 02:26:35 +08:00
|
|
|
RT_ASSERT(bus != RT_NULL);
|
2018-12-05 19:04:47 +08:00
|
|
|
|
|
|
|
lpc_i2c = (struct lpc_i2c *)bus;
|
|
|
|
|
|
|
|
for(index = 0; index < num; index++)
|
|
|
|
{
|
|
|
|
msg = &msgs[index];
|
2019-11-11 22:46:28 +08:00
|
|
|
direction = ((msg->flags & RT_I2C_RD) ? kI2C_Read : kI2C_Write);
|
|
|
|
|
|
|
|
if (!(msg->flags & RT_I2C_NO_START))
|
2018-12-05 19:04:47 +08:00
|
|
|
{
|
2019-11-11 22:46:28 +08:00
|
|
|
/* Start condition and slave address. */
|
|
|
|
result = I2C_MasterStart(lpc_i2c->base, msg->addr, direction);
|
2018-12-05 19:04:47 +08:00
|
|
|
}
|
2019-11-11 22:46:28 +08:00
|
|
|
|
|
|
|
if (result == kStatus_Success)
|
2018-12-05 19:04:47 +08:00
|
|
|
{
|
2019-11-11 22:46:28 +08:00
|
|
|
if (direction == kI2C_Write)
|
|
|
|
{
|
|
|
|
/* Transmit data. */
|
|
|
|
result = I2C_MasterWriteBlocking(lpc_i2c->base, msg->buf, msg->len, kI2C_TransferDefaultFlag);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Receive Data. */
|
|
|
|
result = I2C_MasterReadBlocking(lpc_i2c->base, msg->buf, msg->len, kI2C_TransferDefaultFlag);
|
|
|
|
}
|
2018-12-05 19:04:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-11 22:46:28 +08:00
|
|
|
if (result == kStatus_Success)
|
|
|
|
{
|
|
|
|
ret = index;
|
|
|
|
}
|
2018-12-05 19:04:47 +08:00
|
|
|
|
2019-11-11 22:46:28 +08:00
|
|
|
I2C_MasterStop(lpc_i2c->base);
|
2018-12-05 19:04:47 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t slave_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num)
|
2018-12-05 19:04:47 +08:00
|
|
|
{
|
2023-03-11 12:44:03 +08:00
|
|
|
return -RT_ENOSYS;
|
2018-12-05 19:04:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t i2c_bus_control(struct rt_i2c_bus_device *bus, rt_uint32_t cmd, rt_uint32_t arg)
|
|
|
|
{
|
2023-03-11 12:44:03 +08:00
|
|
|
return -RT_EINVAL;
|
2018-12-05 19:04:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct rt_i2c_bus_device_ops ops =
|
|
|
|
{
|
2021-03-17 02:26:35 +08:00
|
|
|
master_xfer,
|
2018-12-06 10:01:45 +08:00
|
|
|
slave_xfer,
|
|
|
|
i2c_bus_control,
|
2021-03-17 02:26:35 +08:00
|
|
|
};
|
2018-12-05 19:04:47 +08:00
|
|
|
|
|
|
|
#if defined(BSP_USING_I2C4)
|
2021-03-17 02:26:35 +08:00
|
|
|
static struct lpc_i2c i2c4 = {0};
|
2018-12-05 19:04:47 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
int rt_hw_i2c_init(void)
|
|
|
|
{
|
|
|
|
CLOCK_EnableClock(kCLOCK_Iocon);
|
|
|
|
|
|
|
|
#if defined(BSP_USING_I2C4)
|
|
|
|
CLOCK_AttachClk(kFRO12M_to_FLEXCOMM4);
|
|
|
|
RESET_PeripheralReset(kFC4_RST_SHIFT_RSTn);
|
2021-03-17 02:26:35 +08:00
|
|
|
|
2018-12-06 10:01:45 +08:00
|
|
|
i2c4.base = I2C4;
|
2021-03-17 02:26:35 +08:00
|
|
|
i2c4.device_name = "i2c4";
|
|
|
|
i2c4.bus.ops = &ops;
|
|
|
|
|
2018-12-05 19:04:47 +08:00
|
|
|
IOCON_PinMuxSet(IOCON, 1, 1, IOCON_MODE_PULLUP | IOCON_FUNC5 | IOCON_DIGITAL_EN | IOCON_INPFILT_OFF);
|
|
|
|
IOCON_PinMuxSet(IOCON, 1, 2, IOCON_MODE_PULLUP | IOCON_FUNC5 | IOCON_DIGITAL_EN | IOCON_INPFILT_OFF);
|
|
|
|
|
|
|
|
i2c_master_config_t masterConfig;
|
|
|
|
|
|
|
|
I2C_MasterGetDefaultConfig(&masterConfig);
|
|
|
|
masterConfig.baudRate_Bps = 100*1000U;
|
2021-03-17 02:26:35 +08:00
|
|
|
|
2018-12-05 19:04:47 +08:00
|
|
|
I2C_MasterInit(I2C4, &masterConfig, get_i2c_freq(I2C4));
|
|
|
|
|
|
|
|
rt_i2c_bus_device_register(&i2c4.bus, i2c4.device_name);
|
|
|
|
#endif
|
|
|
|
|
2021-03-17 02:26:35 +08:00
|
|
|
return RT_EOK;
|
2018-12-05 19:04:47 +08:00
|
|
|
}
|
2021-03-17 02:26:35 +08:00
|
|
|
INIT_BOARD_EXPORT(rt_hw_i2c_init);
|