diff --git a/boards/xtensa/esp32s2/common/include/esp32s2_board_sdmmc.h b/boards/xtensa/esp32s2/common/include/esp32s2_board_sdmmc.h new file mode 100644 index 00000000000..851a297d27c --- /dev/null +++ b/boards/xtensa/esp32s2/common/include/esp32s2_board_sdmmc.h @@ -0,0 +1,74 @@ +/**************************************************************************** + * boards/xtensa/esp32s2/common/include/esp32s2_board_sdmmc.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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_ESP32S2_COMMON_INCLUDE_ESP32S2_BOARD_SDMMC_H +#define __BOARDS_XTENSA_ESP32S2_COMMON_INCLUDE_ESP32S2_BOARD_SDMMC_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef __ASSEMBLY__ + +#undef EXTERN +#if defined(__cplusplus) +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: board_sdmmc_initialize + * + * Description: + * Initialize SPI-based SD card. + * + * Input Parameters: + * None. + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ + +int board_sdmmc_initialize(void); + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* __BOARDS_XTENSA_ESP32S2_COMMON_INCLUDE_ESP32S2_BOARD_SDMMC_H */ diff --git a/boards/xtensa/esp32s2/common/src/Make.defs b/boards/xtensa/esp32s2/common/src/Make.defs index ad51dfd7a27..359fba5fed5 100644 --- a/boards/xtensa/esp32s2/common/src/Make.defs +++ b/boards/xtensa/esp32s2/common/src/Make.defs @@ -86,6 +86,10 @@ ifeq ($(CONFIG_ESP_PCNT),y) CSRCS += esp32s2_board_pcnt.c endif +ifeq ($(CONFIG_MMCSD_SPI), y) + CSRCS += esp32s2_board_sdmmc.c +endif + ifeq ($(CONFIG_ESP32S2_TWAI)$(CONFIG_ARCH_BUTTONS),y) CHIP_SERIES = $(patsubst "%",%,$(CONFIG_ESPRESSIF_CHIP_SERIES)) CHIPHALDIR = $(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)chip$(DELIM)esp-hal-3rdparty diff --git a/boards/xtensa/esp32s2/common/src/esp32s2_board_sdmmc.c b/boards/xtensa/esp32s2/common/src/esp32s2_board_sdmmc.c new file mode 100644 index 00000000000..d92a9b8325f --- /dev/null +++ b/boards/xtensa/esp32s2/common/src/esp32s2_board_sdmmc.c @@ -0,0 +1,93 @@ +/**************************************************************************** + * boards/xtensa/esp32s2/common/src/esp32s2_board_sdmmc.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 "esp32s2_spi.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: esp32_mmcsd_initialize + * + * Description: + * Configure a SPI subsystem peripheral to communicate with SD card. + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ + +int board_sdmmc_initialize(void) +{ + struct spi_dev_s *spi; + int ret; + + syslog(LOG_INFO, "INFO: init MMC/SD slot %d on SPI%d: /dev/mmcsd%d\n", + CONFIG_NSH_MMCSDSLOTNO, CONFIG_NSH_MMCSDSPIPORTNO, + CONFIG_NSH_MMCSDMINOR); + + spi = esp32s2_spibus_initialize(CONFIG_NSH_MMCSDSPIPORTNO); + + if (spi == NULL) + { + syslog(LOG_ERR, "ERROR: failed to initialize SPI%d.\n", + CONFIG_NSH_MMCSDSPIPORTNO); + return -ENODEV; + } + + /* Mounts to /dev/mmcsdN where N in the minor number */ + + ret = mmcsd_spislotinitialize(CONFIG_NSH_MMCSDMINOR, + CONFIG_NSH_MMCSDSLOTNO, spi); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: failed to bind SPI%d to SD slot %d\n", + CONFIG_NSH_MMCSDSPIPORTNO, CONFIG_NSH_MMCSDSLOTNO); + return ret; + } + + syslog(LOG_INFO, "INFO: MMCSD initialized\n"); + return OK; +} diff --git a/boards/xtensa/esp32s2/common/src/esp32s2_board_spi.c b/boards/xtensa/esp32s2/common/src/esp32s2_board_spi.c index 871afa5c398..ed1292768fa 100644 --- a/boards/xtensa/esp32s2/common/src/esp32s2_board_spi.c +++ b/boards/xtensa/esp32s2/common/src/esp32s2_board_spi.c @@ -52,6 +52,11 @@ uint8_t esp32s2_spi2_status(struct spi_dev_s *dev, uint32_t devid) { uint8_t status = 0; + if (devid == SPIDEV_MMCSD(0)) + { + return SPI_STATUS_PRESENT; + } + return status; } @@ -94,6 +99,11 @@ uint8_t esp32s2_spi3_status(struct spi_dev_s *dev, uint32_t devid) { uint8_t status = 0; + if (devid == SPIDEV_MMCSD(0)) + { + return SPI_STATUS_PRESENT; + } + return status; } diff --git a/boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2_bringup.c b/boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2_bringup.c index cd090470fbb..03ec565d7c6 100644 --- a/boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2_bringup.c +++ b/boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2_bringup.c @@ -117,6 +117,10 @@ # include "espressif/esp_sdm.h" #endif +#ifdef CONFIG_MMCSD_SPI +# include "esp32s2_board_sdmmc.h" +#endif + #include "esp32s2-saola-1.h" /**************************************************************************** @@ -480,6 +484,14 @@ int esp32s2_bringup(void) } #endif +#ifdef CONFIG_MMCSD_SPI + ret = board_sdmmc_initialize(); + if (ret < 0) + { + syslog(LOG_ERR, "Failed to initialize SDMMC: %d\n", ret); + } +#endif + /* If we got here then perhaps not all initialization was successful, but * at least enough succeeded to bring-up NSH with perhaps reduced * capabilities. diff --git a/boards/xtensa/esp32s3/common/include/esp32s3_board_sdmmc.h b/boards/xtensa/esp32s3/common/include/esp32s3_board_sdmmc.h index fcea06484b1..097bd698959 100644 --- a/boards/xtensa/esp32s3/common/include/esp32s3_board_sdmmc.h +++ b/boards/xtensa/esp32s3/common/include/esp32s3_board_sdmmc.h @@ -54,7 +54,7 @@ extern "C" * Name: board_sdmmc_initialize * * Description: - * Configure the sdmmc subsystem. + * Configure the SDMMC peripheral to communicate with SDIO or MMC card. * * Returned Value: * Zero (OK) is returned on success; A negated errno value is returned @@ -66,10 +66,28 @@ int board_sdmmc_initialize(void); #endif /* CONFIG_ESP32S3_SDMMC */ +/**************************************************************************** + * Name: board_sdmmc_spi_initialize + * + * Description: + * Initialize SPI-based SD card. + * + * Input Parameters: + * None. + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ +#ifdef CONFIG_MMCSD_SPI +int board_sdmmc_spi_initialize(void); +#endif /* CONFIG_MMCSD_SPI */ + #undef EXTERN #if defined(__cplusplus) } #endif #endif /* __ASSEMBLY__ */ -#endif /* __BOARDS_XTENSA_ESP32S3_COMMON_INCLUDE_ESP32S3_BOARD_SDMMC_H */ \ No newline at end of file +#endif /* __BOARDS_XTENSA_ESP32S3_COMMON_INCLUDE_ESP32S3_BOARD_SDMMC_H */ diff --git a/boards/xtensa/esp32s3/common/src/Make.defs b/boards/xtensa/esp32s3/common/src/Make.defs index 51864aa215a..100c9a84a74 100644 --- a/boards/xtensa/esp32s3/common/src/Make.defs +++ b/boards/xtensa/esp32s3/common/src/Make.defs @@ -86,6 +86,10 @@ ifeq ($(CONFIG_ESP_MCPWM),y) CSRCS += esp32s3_board_mcpwm.c endif +ifeq ($(CONFIG_MMCSD_SPI),y) + CSRCS += esp32s3_board_sdmmc.c +endif + ifeq ($(CONFIG_ESP32S3_SDMMC),y) CSRCS += esp32s3_board_sdmmc.c endif diff --git a/boards/xtensa/esp32s3/common/src/esp32s3_board_sdmmc.c b/boards/xtensa/esp32s3/common/src/esp32s3_board_sdmmc.c index 7378b527a41..ac55f26f6e6 100644 --- a/boards/xtensa/esp32s3/common/src/esp32s3_board_sdmmc.c +++ b/boards/xtensa/esp32s3/common/src/esp32s3_board_sdmmc.c @@ -32,6 +32,11 @@ #include #include +#ifdef CONFIG_MMCSD_SPI +#include +#include "esp32s3_spi.h" +#endif + extern struct sdio_dev_s *sdio_initialize(int slotno); /**************************************************************************** * Public Functions @@ -41,7 +46,7 @@ extern struct sdio_dev_s *sdio_initialize(int slotno); * Name: board_sdmmc_initialize * * Description: - * Configure the sdmmc subsystem. + * Configure the SDMMC peripheral to communicate with SDIO or MMC card. * * Input Parameters: * None. @@ -73,3 +78,53 @@ int board_sdmmc_initialize(void) return OK; } + +/**************************************************************************** + * Name: board_sdmmc_spi_initialize + * + * Description: + * Initialize SPI-based SD card. + * + * Input Parameters: + * None. + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ + +#ifdef CONFIG_MMCSD_SPI +int board_sdmmc_spi_initialize(void) +{ + struct spi_dev_s *spi; + int ret; + + syslog(LOG_INFO, "INFO: init MMC/SD slot %d on SPI%d: /dev/mmcsd%d\n", + CONFIG_NSH_MMCSDSLOTNO, CONFIG_NSH_MMCSDSPIPORTNO, + CONFIG_NSH_MMCSDMINOR); + + spi = esp32s3_spibus_initialize(CONFIG_NSH_MMCSDSPIPORTNO); + + if (spi == NULL) + { + syslog(LOG_ERR, "ERROR: failed to initialize SPI%d.\n", + CONFIG_NSH_MMCSDSPIPORTNO); + return -ENODEV; + } + + /* Mounts to /dev/mmcsdN where N in the minor number */ + + ret = mmcsd_spislotinitialize(CONFIG_NSH_MMCSDMINOR, + CONFIG_NSH_MMCSDSLOTNO, spi); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: failed to bind SPI%d to SD slot %d\n", + CONFIG_NSH_MMCSDSPIPORTNO, CONFIG_NSH_MMCSDSLOTNO); + return ret; + } + + syslog(LOG_INFO, "INFO: MMCSD initialized\n"); + return OK; +} +#endif /* CONFIG_MMCSD_SPI */ diff --git a/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_board_spi.c b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_board_spi.c index 2975555328b..45298b4ef8e 100644 --- a/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_board_spi.c +++ b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_board_spi.c @@ -53,6 +53,11 @@ uint8_t esp32s3_spi2_status(struct spi_dev_s *dev, uint32_t devid) { uint8_t status = 0; + if (devid == SPIDEV_MMCSD(0)) + { + return SPI_STATUS_PRESENT; + } + return status; } @@ -95,6 +100,11 @@ uint8_t esp32s3_spi3_status(struct spi_dev_s *dev, uint32_t devid) { uint8_t status = 0; + if (devid == SPIDEV_MMCSD(0)) + { + return SPI_STATUS_PRESENT; + } + return status; } diff --git a/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c index 63a70c7b06a..d254bde29f3 100644 --- a/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c +++ b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c @@ -116,7 +116,7 @@ # endif #endif -#ifdef CONFIG_ESP32S3_SDMMC +#if defined(CONFIG_ESP32S3_SDMMC) || defined(CONFIG_MMCSD_SPI) #include "esp32s3_board_sdmmc.h" #endif @@ -512,6 +512,14 @@ int esp32s3_bringup(void) } #endif +#ifdef CONFIG_MMCSD_SPI + ret = board_sdmmc_spi_initialize(); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to initialize SDMMC: %d\n", ret); + } +#endif + #ifdef CONFIG_ESP32S3_AES_ACCELERATOR ret = esp32s3_aes_init(); if (ret < 0)