From 6a0eeb1b3e3044cd1bcbc4876f03c853374d4b06 Mon Sep 17 00:00:00 2001 From: w2016561536 <40821031+w2016561536@users.noreply.github.com> Date: Wed, 17 Jan 2024 10:46:09 +0800 Subject: [PATCH] esp32s3/spi: Add SPI bus init in bringup and fix SPI bus 2 and 3 conflict 1. Add spi bus init in esp32s3_bringup.c 2. Fix IOMUX conflict between spi bus 2 and 3 3. Add spi defconfig 4. Follow the standard of NuttX --- arch/xtensa/src/esp32s3/esp32s3_spi.c | 16 ++-- .../common/include/esp32s3_board_spidev.h | 79 +++++++++++++++++++ boards/xtensa/esp32s3/common/src/Make.defs | 4 + .../esp32s3/common/src/esp32s3_board_spidev.c | 79 +++++++++++++++++++ .../esp32s3-devkit/configs/spi/defconfig | 49 ++++++++++++ .../esp32s3-devkit/src/esp32s3_bringup.c | 23 ++++++ 6 files changed, 242 insertions(+), 8 deletions(-) create mode 100644 boards/xtensa/esp32s3/common/include/esp32s3_board_spidev.h create mode 100644 boards/xtensa/esp32s3/common/src/esp32s3_board_spidev.c create mode 100644 boards/xtensa/esp32s3/esp32s3-devkit/configs/spi/defconfig diff --git a/arch/xtensa/src/esp32s3/esp32s3_spi.c b/arch/xtensa/src/esp32s3/esp32s3_spi.c index 80b5689fe9c..847602e0eb4 100644 --- a/arch/xtensa/src/esp32s3/esp32s3_spi.c +++ b/arch/xtensa/src/esp32s3/esp32s3_spi.c @@ -346,14 +346,14 @@ static const struct esp32s3_spi_config_s esp32s3_spi3_config = .dma_clk_bit = SYSTEM_SPI3_DMA_CLK_EN, .dma_rst_bit = SYSTEM_SPI3_DMA_RST, #endif - .cs_insig = FSPICS0_IN_IDX, - .cs_outsig = FSPICS0_OUT_IDX, - .mosi_insig = FSPID_IN_IDX, - .mosi_outsig = FSPID_OUT_IDX, - .miso_insig = FSPIQ_IN_IDX, - .miso_outsig = FSPIQ_OUT_IDX, - .clk_insig = FSPICLK_IN_IDX, - .clk_outsig = FSPICLK_OUT_IDX + .cs_insig = SPI3_CS0_IN_IDX, + .cs_outsig = SPI3_CS0_OUT_IDX, + .mosi_insig = SPI3_D_IN_IDX, + .mosi_outsig = SPI3_D_OUT_IDX, + .miso_insig = SPI3_Q_IN_IDX, + .miso_outsig = SPI3_Q_OUT_IDX, + .clk_insig = SPI3_CLK_IN_IDX, + .clk_outsig = SPI3_CLK_OUT_IDX }; static const struct spi_ops_s esp32s3_spi3_ops = diff --git a/boards/xtensa/esp32s3/common/include/esp32s3_board_spidev.h b/boards/xtensa/esp32s3/common/include/esp32s3_board_spidev.h new file mode 100644 index 00000000000..4f33d11e2c7 --- /dev/null +++ b/boards/xtensa/esp32s3/common/include/esp32s3_board_spidev.h @@ -0,0 +1,79 @@ +/**************************************************************************** + * boards/xtensa/esp32s3/common/include/esp32s3_board_spidev.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __BOARDS_XTENSA_ESP32S3_COMMON_INCLUDE_ESP32S3_BOARD_SPIDEV_H +#define __BOARDS_XTENSA_ESP32S3_COMMON_INCLUDE_ESP32S3_BOARD_SPIDEV_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef __ASSEMBLY__ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#undef EXTERN +#if defined(__cplusplus) +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifdef CONFIG_SPI_DRIVER +/**************************************************************************** + * Name: board_spidev_initialize + * + * Description: + * Initialize SPI driver and register the /dev/spi device. + * + * Input Parameters: + * port - The SPI bus number, used to build the device path as /dev/spiN + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ + +int board_spidev_initialize(int port); + +#endif /* CONFIG_SPI_DRIVER */ + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* __BOARDS_XTENSA_ESP32S3_COMMON_INCLUDE_ESP32S3_BOARD_SPIDEV_H */ diff --git a/boards/xtensa/esp32s3/common/src/Make.defs b/boards/xtensa/esp32s3/common/src/Make.defs index 68c7f067e10..e22852a055a 100644 --- a/boards/xtensa/esp32s3/common/src/Make.defs +++ b/boards/xtensa/esp32s3/common/src/Make.defs @@ -32,6 +32,10 @@ ifeq ($(CONFIG_ESP32S3_SPIFLASH),y) CSRCS += esp32s3_board_spiflash.c endif +ifeq ($(CONFIG_SPI_DRIVER),y) + CSRCS += esp32s3_board_spidev.c +endif + ifeq ($(CONFIG_ESP32S3_WIFI),y) CSRCS += esp32s3_board_wlan.c endif diff --git a/boards/xtensa/esp32s3/common/src/esp32s3_board_spidev.c b/boards/xtensa/esp32s3/common/src/esp32s3_board_spidev.c new file mode 100644 index 00000000000..c5d391e9be4 --- /dev/null +++ b/boards/xtensa/esp32s3/common/src/esp32s3_board_spidev.c @@ -0,0 +1,79 @@ +/**************************************************************************** + * boards/xtensa/esp32s3/common/src/esp32s3_board_spidev.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include + +#include + +#include "esp32s3_spi.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_spidev_initialize + * + * Description: + * Initialize SPI driver and register the /dev/spi device. + * + * Input Parameters: + * port - The SPI bus number, used to build the device path as /dev/spiN + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ + +int board_spidev_initialize(int port) +{ + int ret; + struct spi_dev_s *spi; + + spiinfo("Initializing /dev/spi%d...\n", port); + + /* Initialize SPI device */ + + spi = esp32s3_spibus_initialize(port); + if (spi == NULL) + { + spierr("Failed to initialize SPI%d.\n", port); + return -ENODEV; + } + + ret = spi_register(spi, port); + if (ret < 0) + { + spierr("Failed to register /dev/spi%d: %d\n", port, ret); + + esp32s3_spibus_uninitialize(spi); + } + + return ret; +} diff --git a/boards/xtensa/esp32s3/esp32s3-devkit/configs/spi/defconfig b/boards/xtensa/esp32s3/esp32s3-devkit/configs/spi/defconfig new file mode 100644 index 00000000000..9b4462a2a20 --- /dev/null +++ b/boards/xtensa/esp32s3/esp32s3-devkit/configs/spi/defconfig @@ -0,0 +1,49 @@ +# +# This file is autogenerated: PLEASE DO NOT EDIT IT. +# +# You can use "make menuconfig" to make any modifications to the installed .config file. +# You can then do "make savedefconfig" to generate a new defconfig file that includes your +# modifications. +# +# CONFIG_ARCH_LEDS is not set +# CONFIG_NSH_ARGCAT is not set +# CONFIG_NSH_CMDOPT_HEXDUMP is not set +CONFIG_ARCH="xtensa" +CONFIG_ARCH_BOARD="esp32s3-devkit" +CONFIG_ARCH_BOARD_COMMON=y +CONFIG_ARCH_BOARD_ESP32S3_DEVKIT=y +CONFIG_ARCH_CHIP="esp32s3" +CONFIG_ARCH_CHIP_ESP32S3=y +CONFIG_ARCH_CHIP_ESP32S3WROOM1=y +CONFIG_ARCH_INTERRUPTSTACK=2048 +CONFIG_ARCH_STACKDUMP=y +CONFIG_ARCH_XTENSA=y +CONFIG_BOARD_LOOPSPERMSEC=16717 +CONFIG_BUILTIN=y +CONFIG_ESP32S3_SPI2=y +CONFIG_ESP32S3_SPI3=y +CONFIG_ESP32S3_UART0=y +CONFIG_FS_PROCFS=y +CONFIG_HAVE_CXX=y +CONFIG_HAVE_CXXINITIALIZE=y +CONFIG_IDLETHREAD_STACKSIZE=3072 +CONFIG_INIT_ENTRYPOINT="nsh_main" +CONFIG_INTELHEX_BINARY=y +CONFIG_NSH_ARCHINIT=y +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_LINELEN=64 +CONFIG_NSH_READLINE=y +CONFIG_PREALLOC_TIMERS=4 +CONFIG_RAM_SIZE=114688 +CONFIG_RAM_START=0x20000000 +CONFIG_RR_INTERVAL=200 +CONFIG_SCHED_WAITPID=y +CONFIG_SPITOOL_MINBUS=2 +CONFIG_START_DAY=6 +CONFIG_START_MONTH=12 +CONFIG_START_YEAR=2011 +CONFIG_SYSLOG_BUFFER=y +CONFIG_SYSTEM_NSH=y +CONFIG_SYSTEM_SPITOOL=y +CONFIG_UART0_SERIAL_CONSOLE=y diff --git a/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c index bdeca8b1af7..bb9531e487b 100644 --- a/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c +++ b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c @@ -100,6 +100,11 @@ # include "esp32s3_board_rmt.h" #endif +#ifdef CONFIG_ESP32S3_SPI +#include "esp32s3_spi.h" +#include "esp32s3_board_spidev.h" +#endif + #include "esp32s3-devkit.h" /**************************************************************************** @@ -138,6 +143,24 @@ int esp32s3_bringup(void) } #endif +#if defined(CONFIG_ESP32S3_SPI) && defined(CONFIG_SPI_DRIVER) + #ifdef CONFIG_ESP32S3_SPI2 + ret = board_spidev_initialize(ESP32S3_SPI2); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to init spidev 2: %d\n", ret); + } + #endif + + #ifdef CONFIG_ESP32S3_SPI3 + ret = board_spidev_initialize(ESP32S3_SPI3); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to init spidev 3: %d\n", ret); + } + #endif +#endif + #if defined(CONFIG_ESP32S3_EFUSE) ret = esp32s3_efuse_initialize("/dev/efuse"); if (ret < 0)