From 779547d708b3e0d41505e50410b73101fe418582 Mon Sep 17 00:00:00 2001 From: Yilin Sun Date: Thu, 12 Sep 2024 18:55:50 +0800 Subject: [PATCH] bsp: nxp/mcx/mcxc/mcxc444: Add SPI driver support. Signed-off-by: Yilin Sun --- bsp/nxp/mcx/mcxc/Libraries/drivers/drv_spi.c | 122 +++++++++++++++++++ bsp/nxp/mcx/mcxc/Libraries/drivers/drv_spi.h | 11 ++ bsp/nxp/mcx/mcxc/frdm-mcxc444/board/Kconfig | 10 +- 3 files changed, 140 insertions(+), 3 deletions(-) create mode 100644 bsp/nxp/mcx/mcxc/Libraries/drivers/drv_spi.c create mode 100644 bsp/nxp/mcx/mcxc/Libraries/drivers/drv_spi.h diff --git a/bsp/nxp/mcx/mcxc/Libraries/drivers/drv_spi.c b/bsp/nxp/mcx/mcxc/Libraries/drivers/drv_spi.c new file mode 100644 index 00000000000..7e489916af1 --- /dev/null +++ b/bsp/nxp/mcx/mcxc/Libraries/drivers/drv_spi.c @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2006-2024 RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2024-09-04 Yilin Sun Initial version + */ + +#include + +#include "fsl_spi.h" + +#include "drv_spi.h" + +#define BSP_DEFAULT_SPI_FREQUENCY (500000U) + +typedef struct +{ + struct rt_spi_bus parent; + SPI_Type *instance; + uint32_t input_frequency; +} mcx_spi_obj_t; + +typedef struct +{ + SPI_Type *instance; + uint8_t id; +} mcx_spi_instance_t; + +static const mcx_spi_instance_t mcx_spi_instances[] = +{ +#ifdef BSP_USING_SPI0 + {SPI0, 0}, +#endif +#ifdef BSP_USING_SPI1 + {SPI1, 1}, +#endif +}; + +static mcx_spi_obj_t mcx_spi_list[ARRAY_SIZE(mcx_spi_instances)]; + +static rt_ssize_t mcx_spi_xfer(struct rt_spi_device *device, struct rt_spi_message *message) +{ + spi_transfer_t xfer = + { + .txData = message->send_buf, + .rxData = message->recv_buf, + .dataSize = message->length, + .flags = 0UL, + }; + + mcx_spi_obj_t *spi = device->bus->parent.user_data; + + if (message->cs_take) + { + rt_pin_write(device->cs_pin, PIN_LOW); + } + + SPI_MasterTransferBlocking(spi->instance, &xfer); + + if (message->cs_release) + { + rt_pin_write(device->cs_pin, PIN_HIGH); + } + + return message->length; +} + +static rt_err_t mcx_spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg) +{ + mcx_spi_obj_t *spi = device->bus->parent.user_data; + + SPI_MasterSetBaudRate(spi->instance, cfg->max_hz, spi->input_frequency); + + return RT_EOK; +} + +static struct rt_spi_ops mcx_spi_ops = +{ + .configure = mcx_spi_configure, + .xfer = mcx_spi_xfer, +}; + +rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t pin) +{ + struct rt_spi_device *spi_device = rt_malloc(sizeof(struct rt_spi_device)); + if (!spi_device) + { + return -RT_ENOMEM; + } + + return rt_spi_bus_attach_device_cspin(spi_device, device_name, bus_name, pin, NULL); +} + +int rt_hw_spi_init(void) +{ + for (size_t i = 0; i < ARRAY_SIZE(mcx_spi_instances); i++) + { + char name_buf[16]; + + /* TODO: Initialize SPI interface here. */ + + mcx_spi_list[i].instance = mcx_spi_instances[i].instance; + mcx_spi_list[i].input_frequency = CLOCK_GetCoreSysClkFreq(); + + mcx_spi_list[i].parent.parent.user_data = &mcx_spi_list[i]; + + spi_master_config_t cfg; + SPI_MasterGetDefaultConfig(&cfg); + + SPI_MasterInit(mcx_spi_list[i].instance, &cfg, mcx_spi_list[i].input_frequency); + + rt_snprintf(name_buf, sizeof(name_buf), "spi%d", mcx_spi_instances[i].id); + rt_spi_bus_register(&mcx_spi_list[i].parent, name_buf, &mcx_spi_ops); + } + + return 0; +} + +INIT_DEVICE_EXPORT(rt_hw_spi_init); diff --git a/bsp/nxp/mcx/mcxc/Libraries/drivers/drv_spi.h b/bsp/nxp/mcx/mcxc/Libraries/drivers/drv_spi.h new file mode 100644 index 00000000000..b274848cb0a --- /dev/null +++ b/bsp/nxp/mcx/mcxc/Libraries/drivers/drv_spi.h @@ -0,0 +1,11 @@ +#ifndef DRV_SPI_H +#define DRV_SPI_H + + +#include +#include + +rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t pin); +int rt_hw_spi_init(void); + +#endif //DRV_SPI_H diff --git a/bsp/nxp/mcx/mcxc/frdm-mcxc444/board/Kconfig b/bsp/nxp/mcx/mcxc/frdm-mcxc444/board/Kconfig index c7f87bc7499..7800112a42c 100644 --- a/bsp/nxp/mcx/mcxc/frdm-mcxc444/board/Kconfig +++ b/bsp/nxp/mcx/mcxc/frdm-mcxc444/board/Kconfig @@ -54,12 +54,16 @@ menu "On-chip Peripheral Drivers" default y if BSP_USING_SPI - config BSP_USING_SPI1 - bool "Enable Flexcomm1 as SPI" + config BSP_USING_SPI0 + bool "Enable SPI0" default n - endif + if BSP_USING_SPI + config BSP_USING_SPI1 + bool "Enable SPI1" + default n + endif config BSP_USING_RTC bool "Enable RTC"