summaryrefslogtreecommitdiff
path: root/bsp
diff options
context:
space:
mode:
authorhywing <[email protected]>2024-08-21 22:20:26 +0800
committerMeco Man <[email protected]>2024-08-21 17:35:19 -0400
commitd3b9480658b79800b78b6281af473d2f4933cef2 (patch)
tree6cae99d9145f79b2f7518ad1933386cc6ac15ee4 /bsp
parentf5156774b2b7bc73278d602dd027ac4d61349e3f (diff)
[bsp][nxp][mcxa153] add i2c driver
Diffstat (limited to 'bsp')
-rw-r--r--bsp/nxp/mcx/mcxa/Libraries/drivers/drv_i2c.c146
-rw-r--r--bsp/nxp/mcx/mcxa/frdm-mcxa153/board/Kconfig9
-rw-r--r--bsp/nxp/mcx/mcxa/frdm-mcxa153/board/MCUX_Config/board/pin_mux.c54
3 files changed, 203 insertions, 6 deletions
diff --git a/bsp/nxp/mcx/mcxa/Libraries/drivers/drv_i2c.c b/bsp/nxp/mcx/mcxa/Libraries/drivers/drv_i2c.c
new file mode 100644
index 0000000000..5f79a1828f
--- /dev/null
+++ b/bsp/nxp/mcx/mcxa/Libraries/drivers/drv_i2c.c
@@ -0,0 +1,146 @@
+/*
+ * Copyright (c) 2006-2024 RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2023-08-21 hywing The first version
+ */
+
+#include <rtdevice.h>
+#include "fsl_lpi2c.h"
+#include "fsl_lpi2c_edma.h"
+#include "fsl_edma.h"
+
+
+#ifdef RT_USING_I2C
+
+enum
+{
+#ifdef BSP_USING_I2C0
+ I2C0_INDEX,
+#endif
+};
+
+
+#define i2c_dbg rt_kprintf
+
+struct lpc_i2c_bus
+{
+ struct rt_i2c_bus_device parent;
+ LPI2C_Type *I2C;
+ clock_attach_id_t clock_attach_id;
+ clock_div_name_t clock_div_name;
+ clock_name_t clock_src;
+ uint32_t baud;
+ char *name;
+};
+
+
+struct lpc_i2c_bus lpc_obj[] =
+{
+#ifdef BSP_USING_I2C0
+ {
+ .I2C = LPI2C0,
+ .baud = 100000U,
+ .clock_attach_id = kFRO12M_to_LPI2C0,
+ .clock_div_name = kCLOCK_DivLPI2C0,
+ .clock_src = kCLOCK_Fro12M,
+ .name = "i2c0",
+ },
+#endif
+};
+
+static rt_ssize_t lpc_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num)
+{
+ struct rt_i2c_msg *msg;
+ lpi2c_master_transfer_t xfer = {0};
+ rt_uint32_t i;
+ rt_ssize_t ret = 0;
+
+ struct lpc_i2c_bus *lpc_i2c = (struct lpc_i2c_bus *)bus;
+
+ for (i = 0; i < num; i++)
+ {
+ msg = &msgs[i];
+
+ if (msg->flags & RT_I2C_RD)
+ {
+ xfer.slaveAddress = msg->addr;
+ xfer.direction = kLPI2C_Read;
+ xfer.subaddress = 0;
+ xfer.subaddressSize = 0;
+ xfer.data = msg->buf;
+ xfer.dataSize = msg->len;
+ if(i != 0)
+ xfer.flags = kLPI2C_TransferRepeatedStartFlag;
+ else
+ xfer.flags = kLPI2C_TransferDefaultFlag;
+
+ if (LPI2C_MasterTransferBlocking(lpc_i2c->I2C, &xfer) != kStatus_Success)
+ {
+ i2c_dbg("i2c bus read failed!\n");
+ return i;
+ }
+ }
+ else
+ {
+ xfer.slaveAddress = msg->addr;
+ xfer.direction = kLPI2C_Write;
+ xfer.subaddress = 0;
+ xfer.subaddressSize = 0;
+ xfer.data = msg->buf;
+ xfer.dataSize = msg->len;
+ if(i == 0)
+ xfer.flags = kLPI2C_TransferNoStopFlag;
+ else
+ xfer.flags = kLPI2C_TransferDefaultFlag;
+
+ if (LPI2C_MasterTransferBlocking(lpc_i2c->I2C, &xfer) != kStatus_Success)
+ {
+ i2c_dbg("i2c bus write failed!\n");
+ return i;
+ }
+ }
+ }
+ ret = i;
+
+ return ret;
+}
+
+static const struct rt_i2c_bus_device_ops i2c_ops =
+{
+ lpc_i2c_xfer,
+ RT_NULL,
+ RT_NULL
+};
+
+int rt_hw_i2c_init(void)
+{
+ int i;
+ lpi2c_master_config_t masterConfig;
+
+ for(i=0; i<ARRAY_SIZE(lpc_obj); i++)
+ {
+ CLOCK_SetClockDiv(lpc_obj[i].clock_div_name, 1u);
+ CLOCK_AttachClk(lpc_obj[i].clock_attach_id);
+
+ LPI2C_MasterGetDefaultConfig(&masterConfig);
+ masterConfig.baudRate_Hz = lpc_obj[i].baud;
+
+ LPI2C_MasterInit(lpc_obj[i].I2C, &masterConfig, CLOCK_GetFreq(lpc_obj[i].clock_src));
+
+ lpc_obj[i].parent.ops = &i2c_ops;
+
+ rt_i2c_bus_device_register(&lpc_obj[i].parent, lpc_obj[i].name);
+ }
+
+ return RT_EOK;
+}
+INIT_DEVICE_EXPORT(rt_hw_i2c_init);
+
+#endif /* RT_USING_I2C */
+
+
+
diff --git a/bsp/nxp/mcx/mcxa/frdm-mcxa153/board/Kconfig b/bsp/nxp/mcx/mcxa/frdm-mcxa153/board/Kconfig
index dcb9a8e199..e3f3e3b738 100644
--- a/bsp/nxp/mcx/mcxa/frdm-mcxa153/board/Kconfig
+++ b/bsp/nxp/mcx/mcxa/frdm-mcxa153/board/Kconfig
@@ -35,15 +35,12 @@ menu "On-chip Peripheral Drivers"
config BSP_USING_I2C
bool "Enable I2C"
select RT_USING_I2C
- default y
+ default n
if BSP_USING_I2C
config BSP_USING_I2C0
- bool "Enable Flexcomm0 I2C"
- default y
- config BSP_USING_I2C1
- bool "Enable Flexcomm1 I2C"
- default y
+ bool "Enable I2C0"
+ default n
endif
menuconfig BSP_USING_SPI
diff --git a/bsp/nxp/mcx/mcxa/frdm-mcxa153/board/MCUX_Config/board/pin_mux.c b/bsp/nxp/mcx/mcxa/frdm-mcxa153/board/MCUX_Config/board/pin_mux.c
index d5001e36e3..d27d7a783b 100644
--- a/bsp/nxp/mcx/mcxa/frdm-mcxa153/board/MCUX_Config/board/pin_mux.c
+++ b/bsp/nxp/mcx/mcxa/frdm-mcxa153/board/MCUX_Config/board/pin_mux.c
@@ -54,6 +54,8 @@ void BOARD_InitPins(void)
RESET_ReleasePeripheralReset(kCTIMER1_RST_SHIFT_RSTn);
RESET_ReleasePeripheralReset(kLPSPI0_RST_SHIFT_RSTn);
+ RESET_ReleasePeripheralReset(kLPI2C0_RST_SHIFT_RSTn);
+
RESET_ReleasePeripheralReset(kPORT0_RST_SHIFT_RSTn);
RESET_ReleasePeripheralReset(kPORT1_RST_SHIFT_RSTn);
RESET_ReleasePeripheralReset(kPORT2_RST_SHIFT_RSTn);
@@ -244,4 +246,56 @@ void BOARD_InitPins(void)
/* PORT1_3 (pin 59) is configured as LPSPI0_PCS0 */
PORT_SetPinConfig(PORT1, 3U, &port1_3_pin59_config);
#endif
+
+#ifdef BSP_USING_I2C0
+ const port_pin_config_t port3_27_pin34_config = {/* Internal pull-up resistor is enabled */
+ kPORT_PullUp,
+ /* Low internal pull resistor value is selected. */
+ kPORT_LowPullResistor,
+ /* Fast slew rate is configured */
+ kPORT_FastSlewRate,
+ /* Passive input filter is disabled */
+ kPORT_PassiveFilterDisable,
+ /* Open drain output is enabled */
+ kPORT_OpenDrainEnable,
+ /* Low drive strength is configured */
+ kPORT_LowDriveStrength,
+ /* Normal drive strength is configured */
+ kPORT_NormalDriveStrength,
+ /* Pin is configured as LPI2C0_SCL */
+ kPORT_MuxAlt2,
+ /* Digital input enabled */
+ kPORT_InputBufferEnable,
+ /* Digital input is not inverted */
+ kPORT_InputNormal,
+ /* Pin Control Register fields [15:0] are not locked */
+ kPORT_UnlockRegister};
+ /* PORT3_27 (pin 34) is configured as LPI2C0_SCL */
+ PORT_SetPinConfig(PORT3, 27U, &port3_27_pin34_config);
+
+ const port_pin_config_t port3_28_pin33_config = {/* Internal pull-up resistor is enabled */
+ kPORT_PullUp,
+ /* Low internal pull resistor value is selected. */
+ kPORT_LowPullResistor,
+ /* Fast slew rate is configured */
+ kPORT_FastSlewRate,
+ /* Passive input filter is disabled */
+ kPORT_PassiveFilterDisable,
+ /* Open drain output is enabled */
+ kPORT_OpenDrainEnable,
+ /* Low drive strength is configured */
+ kPORT_LowDriveStrength,
+ /* Normal drive strength is configured */
+ kPORT_NormalDriveStrength,
+ /* Pin is configured as LPI2C0_SDA */
+ kPORT_MuxAlt2,
+ /* Digital input enabled */
+ kPORT_InputBufferEnable,
+ /* Digital input is not inverted */
+ kPORT_InputNormal,
+ /* Pin Control Register fields [15:0] are not locked */
+ kPORT_UnlockRegister};
+ /* PORT3_28 (pin 33) is configured as LPI2C0_SDA */
+ PORT_SetPinConfig(PORT3, 28U, &port3_28_pin33_config);
+#endif
}