diff --git a/bsp/nxp/mcx/mcxn/Libraries/drivers/drv_spi.c b/bsp/nxp/mcx/mcxn/Libraries/drivers/drv_spi.c index 4762bdad16..37edd3daca 100644 --- a/bsp/nxp/mcx/mcxn/Libraries/drivers/drv_spi.c +++ b/bsp/nxp/mcx/mcxn/Libraries/drivers/drv_spi.c @@ -20,6 +20,11 @@ enum #ifdef BSP_USING_SPI3 SPI3_INDEX, #endif + +#ifdef BSP_USING_SPI6 + SPI6_INDEX, +#endif + #ifdef BSP_USING_SPI7 SPI7_INDEX, #endif @@ -63,7 +68,20 @@ static struct lpc_spi lpc_obj[] = .rx_dma_chl = 3, .name = "spi3", }, -#endif +#endif /* BSP_USING_SPI3 */ +#ifdef BSP_USING_SPI6 + { + .LPSPIx = LPSPI6, + .clock_attach_id = kFRO_HF_DIV_to_FLEXCOMM6, + .clock_div_name = kCLOCK_DivFlexcom6Clk, + .clock_name = kCLOCK_FroHf, + .tx_dma_request = kDmaRequestMuxLpFlexcomm6Tx, + .rx_dma_request = kDmaRequestMuxLpFlexcomm6Rx, + .DMAx = DMA0, + .tx_dma_chl = 4, + .rx_dma_chl = 5, + .name = "spi6", +#endif /* BSP_USING_SPI6 */ #ifdef BSP_USING_SPI7 { .LPSPIx = LPSPI7, @@ -77,7 +95,7 @@ static struct lpc_spi lpc_obj[] = .rx_dma_chl = 3, .name = "spi7", }, -#endif +#endif /* BSP_USING_SPI7 */ }; @@ -146,7 +164,7 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m // if(message->length < MAX_DMA_TRANSFER_SIZE) if(0) { - // SPI_MasterTransferBlocking(spi->SPIx, &transfer); + LPSPI_MasterTransferBlocking(spi->LPSPIx, &transfer); } else { diff --git a/bsp/nxp/mcx/mcxn/Libraries/drivers/drv_spi.h b/bsp/nxp/mcx/mcxn/Libraries/drivers/drv_spi.h new file mode 100644 index 0000000000..cd4aa180bb --- /dev/null +++ b/bsp/nxp/mcx/mcxn/Libraries/drivers/drv_spi.h @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2006-2024, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2024-03-22 Jisheng Zhang The first version for mcxn + */ + +#ifndef __DRV_SPI_H__ +#define __DRV_SPI_H__ + +#include + +rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin); + +#endif /*__DRV_SPI_H__ */ diff --git a/bsp/nxp/mcx/mcxn/frdm-mcxn947/board/Kconfig b/bsp/nxp/mcx/mcxn/frdm-mcxn947/board/Kconfig index 437a701717..ef479082e0 100644 --- a/bsp/nxp/mcx/mcxn/frdm-mcxn947/board/Kconfig +++ b/bsp/nxp/mcx/mcxn/frdm-mcxn947/board/Kconfig @@ -64,6 +64,15 @@ menu "On-chip Peripheral Drivers" bool "Enable Flexcomm3 as SPI" default n + config BSP_USING_SPI6 + bool "Enable Flexcomm6 as SPI" + default n + if BSP_USING_SPI6 + config BSP_USING_SPI6_SAMPLE + bool "Enable SPI6 BUS Sample" + default n + endif + config BSP_USING_SPI7 bool "Enable Flexcomm7 as SPI" default n diff --git a/bsp/nxp/mcx/mcxn/frdm-mcxn947/board/MCUX_Config/board/pin_mux.c b/bsp/nxp/mcx/mcxn/frdm-mcxn947/board/MCUX_Config/board/pin_mux.c index 71809ba47d..0ce94a7694 100644 --- a/bsp/nxp/mcx/mcxn/frdm-mcxn947/board/MCUX_Config/board/pin_mux.c +++ b/bsp/nxp/mcx/mcxn/frdm-mcxn947/board/MCUX_Config/board/pin_mux.c @@ -99,8 +99,12 @@ void BOARD_InitBootPins(void) PORT2->PCR[6] = PORT_PCR_MUX(3) | PORT_PCR_PE(1) | PORT_PCR_PS(1) | PORT_PCR_IBE(1); /* SDHC0_D3 */ PORT2->PCR[7] = PORT_PCR_MUX(3) | PORT_PCR_PE(1) | PORT_PCR_PS(1) | PORT_PCR_IBE(1); /* SDHC0_D2 */ - PORT3->PCR[20] = PORT_PCR_MUX(3) | PORT_PCR_PS(0) | PORT_PCR_IBE(1); /* FC6_P0 */ - PORT3->PCR[21] = PORT_PCR_MUX(3) | PORT_PCR_PS(0) | PORT_PCR_IBE(1); /* FC6_P1 */ + /* mikroBUS SPI6 */ + PORT3->PCR[20] = PORT_PCR_MUX(3) | PORT_PCR_PS(0) | PORT_PCR_PE(0) | PORT_PCR_IBE(1); /* FC6_P0 SDO/D[0], FC6_SPI_MOSI */ + PORT3->PCR[21] = PORT_PCR_MUX(3) | PORT_PCR_PS(0) | PORT_PCR_PE(0) | PORT_PCR_IBE(1); /* FC6_P1 SCK, FC6_SPI_CLK */ + PORT3->PCR[22] = PORT_PCR_MUX(3) | PORT_PCR_PS(0) | PORT_PCR_PE(0) | PORT_PCR_IBE(1); /* FC6_P2 SDI/D[1], FC3_SPI_MISO */ + PORT3->PCR[23] = PORT_PCR_MUX(0) | PORT_PCR_PS(0) | PORT_PCR_PE(0) | PORT_PCR_IBE(1); /* CS */ + // PORT1->PCR[20] = PORT_PCR_MUX(3) | PORT_PCR_PS(0) | PORT_PCR_PE(0) | PORT_PCR_IBE(1); /* FC4_0 */ // PORT1->PCR[21] = PORT_PCR_MUX(3) | PORT_PCR_PS(0) | PORT_PCR_PE(0) | PORT_PCR_IBE(1); /* FC4_1 */ diff --git a/bsp/nxp/mcx/mcxn/frdm-mcxn947/board/SConscript b/bsp/nxp/mcx/mcxn/frdm-mcxn947/board/SConscript index c9a0d56a97..c8b87f8577 100644 --- a/bsp/nxp/mcx/mcxn/frdm-mcxn947/board/SConscript +++ b/bsp/nxp/mcx/mcxn/frdm-mcxn947/board/SConscript @@ -9,6 +9,9 @@ MCUX_Config/board/clock_config.c MCUX_Config/board/pin_mux.c """) +if GetDepend(['BSP_USING_SPI6_SAMPLE']): + src += Glob('ports/spi_sample.c') + CPPPATH = [cwd, cwd + '/MCUX_Config/board'] CPPDEFINES = ['DEBUG', 'CPU_MCXN947VDF_cm33_core0'] diff --git a/bsp/nxp/mcx/mcxn/frdm-mcxn947/board/ports/spi_sample.c b/bsp/nxp/mcx/mcxn/frdm-mcxn947/board/ports/spi_sample.c new file mode 100644 index 0000000000..e2bc2ce932 --- /dev/null +++ b/bsp/nxp/mcx/mcxn/frdm-mcxn947/board/ports/spi_sample.c @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2006-2023, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2022-07-19 Rbbb666 first version + * 2024-03-30 xhackerustc 2nd version for FRDM-MCXN947 + */ + +#include "board.h" + +#include + +#define SPI_NAME "spi60" +#define CS_PIN (3*32+23) +static struct rt_spi_device *spi_dev; + +/* attach spi device */ +static int rt_spi_device_init(void) +{ + struct rt_spi_configuration cfg; + + rt_hw_spi_device_attach("spi6", SPI_NAME, CS_PIN); + + cfg.data_width = 8; + cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB | RT_SPI_NO_CS; + cfg.max_hz = 1 *1000 *1000; + + spi_dev = (struct rt_spi_device *)rt_device_find(SPI_NAME); + + if (RT_NULL == spi_dev) + { + rt_kprintf("spi sample run failed! can't find %s device!\n", SPI_NAME); + return -RT_ERROR; + } + + rt_spi_configure(spi_dev, &cfg); + + return RT_EOK; +} +INIT_APP_EXPORT(rt_spi_device_init); + +/* spi loopback mode test case */ +static int spi_sample(int argc, char **argv) +{ + rt_uint8_t t_buf[32], r_buf[32]; + int i = 0; + static struct rt_spi_message msg1; + + for (i = 0; i < sizeof(t_buf); i++) + { + t_buf[i] = i; + } + + msg1.send_buf = &t_buf; + msg1.recv_buf = &r_buf; + msg1.length = sizeof(t_buf); + msg1.cs_take = 1; + msg1.cs_release = 1; + msg1.next = RT_NULL; + + rt_spi_transfer_message(spi_dev, &msg1); + + rt_kprintf("spi rbuf : "); + for (i = 0; i < sizeof(r_buf); i++) + { + rt_kprintf("%x ", r_buf[i]); + } + + rt_kprintf("\nspi loopback mode test over!\n"); + + return RT_EOK; +} +MSH_CMD_EXPORT(spi_sample, spi loopback test);