This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 452292159bb595fac34790cc4307d184f33cd194
Author: Filipe Cavalcanti <filipe.cavalca...@espressif.com>
AuthorDate: Wed May 28 14:09:59 2025 -0300

    boards/xtensa: support SDMMC over SPI on ESP32-S2|S3
    
    Adds support for SDMMC over SPI on ESP32S2 and ESP32S3.
    
    Signed-off-by: Filipe Cavalcanti <filipe.cavalca...@espressif.com>
---
 .../common/include/esp32s2_board_sdmmc.h}          |  17 ++--
 boards/xtensa/esp32s2/common/src/Make.defs         |   4 +
 .../common/src/esp32s2_board_sdmmc.c}              | 113 +++++++--------------
 .../xtensa/esp32s2/common/src/esp32s2_board_spi.c  |  10 ++
 .../esp32s2/esp32s2-saola-1/src/esp32s2_bringup.c  |  12 +++
 .../esp32s3/common/include/esp32s3_board_sdmmc.h   |  22 +++-
 boards/xtensa/esp32s3/common/src/Make.defs         |   4 +
 .../esp32s3/common/src/esp32s3_board_sdmmc.c       |  57 ++++++++++-
 .../esp32s3/esp32s3-devkit/src/esp32s3_board_spi.c |  10 ++
 .../esp32s3/esp32s3-devkit/src/esp32s3_bringup.c   |  10 +-
 10 files changed, 172 insertions(+), 87 deletions(-)

diff --git a/boards/xtensa/esp32s3/common/include/esp32s3_board_sdmmc.h 
b/boards/xtensa/esp32s2/common/include/esp32s2_board_sdmmc.h
similarity index 85%
copy from boards/xtensa/esp32s3/common/include/esp32s3_board_sdmmc.h
copy to boards/xtensa/esp32s2/common/include/esp32s2_board_sdmmc.h
index fcea06484b..851a297d27 100644
--- a/boards/xtensa/esp32s3/common/include/esp32s3_board_sdmmc.h
+++ b/boards/xtensa/esp32s2/common/include/esp32s2_board_sdmmc.h
@@ -1,5 +1,5 @@
 /****************************************************************************
- * boards/xtensa/esp32s3/common/include/esp32s3_board_sdmmc.h
+ * boards/xtensa/esp32s2/common/include/esp32s2_board_sdmmc.h
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -20,8 +20,8 @@
  *
  ****************************************************************************/
 
-#ifndef __BOARDS_XTENSA_ESP32S3_COMMON_INCLUDE_ESP32S3_BOARD_SDMMC_H
-#define __BOARDS_XTENSA_ESP32S3_COMMON_INCLUDE_ESP32S3_BOARD_SDMMC_H
+#ifndef __BOARDS_XTENSA_ESP32S2_COMMON_INCLUDE_ESP32S2_BOARD_SDMMC_H
+#define __BOARDS_XTENSA_ESP32S2_COMMON_INCLUDE_ESP32S2_BOARD_SDMMC_H
 
 /****************************************************************************
  * Included Files
@@ -48,13 +48,14 @@ extern "C"
  * Public Function Prototypes
  ****************************************************************************/
 
-#ifdef CONFIG_ESP32S3_SDMMC
-
 /****************************************************************************
  * Name: board_sdmmc_initialize
  *
  * Description:
- *   Configure the sdmmc subsystem.
+ *   Initialize SPI-based SD card.
+ *
+ * Input Parameters:
+ *   None.
  *
  * Returned Value:
  *   Zero (OK) is returned on success; A negated errno value is returned
@@ -64,12 +65,10 @@ extern "C"
 
 int board_sdmmc_initialize(void);
 
-#endif /* CONFIG_ESP32S3_SDMMC */
-
 #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_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 ad51dfd7a2..359fba5fed 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/esp32s3/esp32s3-devkit/src/esp32s3_board_spi.c 
b/boards/xtensa/esp32s2/common/src/esp32s2_board_sdmmc.c
similarity index 55%
copy from boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_board_spi.c
copy to boards/xtensa/esp32s2/common/src/esp32s2_board_sdmmc.c
index 2975555328..d92a9b8325 100644
--- a/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_board_spi.c
+++ b/boards/xtensa/esp32s2/common/src/esp32s2_board_sdmmc.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_board_spi.c
+ * boards/xtensa/esp32s2/common/src/esp32s2_board_sdmmc.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -25,104 +25,69 @@
  ****************************************************************************/
 
 #include <nuttx/config.h>
-
-#include <stdint.h>
-#include <stdbool.h>
-#include <debug.h>
-
+#include <syslog.h>
+#include <nuttx/mmcsd.h>
 #include <nuttx/spi/spi.h>
 
-#include "esp32s3_gpio.h"
-#include "esp32s3-devkit.h"
+#include "esp32s2_spi.h"
 
 /****************************************************************************
- * Private Functions
+ * Pre-processor Definitions
  ****************************************************************************/
 
 /****************************************************************************
- * Public Functions
+ * Private Definitions
  ****************************************************************************/
 
 /****************************************************************************
- * Name: esp32s3_spi2_status
+ * Private Functions
  ****************************************************************************/
 
-#ifdef CONFIG_ESP32S3_SPI2
-
-uint8_t esp32s3_spi2_status(struct spi_dev_s *dev, uint32_t devid)
-{
-  uint8_t status = 0;
-
-  return status;
-}
-
-#endif
-
 /****************************************************************************
- * Name: esp32s3_spi2_cmddata
+ * Public Functions
  ****************************************************************************/
 
-#if defined(CONFIG_ESP32S3_SPI2) && defined(CONFIG_SPI_CMDDATA)
-
-int esp32s3_spi2_cmddata(struct spi_dev_s *dev, uint32_t devid, bool cmd)
-{
-  if (devid == SPIDEV_DISPLAY(0))
-    {
-      /*  This is the Data/Command control pad which determines whether the
-       *  data bits are data or a command.
-       */
-
-      esp32s3_gpiowrite(GPIO_LCD_DC, !cmd);
-
-      return OK;
-    }
-
-  spiinfo("devid: %" PRIu32 " CMD: %s\n", devid, cmd ? "command" :
-          "data");
-
-  return -ENODEV;
-}
-
-#endif
-
 /****************************************************************************
- * Name: esp32s3_spi3_status
+ * 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.
+ *
  ****************************************************************************/
 
-#ifdef CONFIG_ESP32S3_SPI3
-
-uint8_t esp32s3_spi3_status(struct spi_dev_s *dev, uint32_t devid)
+int board_sdmmc_initialize(void)
 {
-  uint8_t status = 0;
-
-  return status;
-}
+  struct spi_dev_s *spi;
+  int ret;
 
-#endif
+  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);
 
-/****************************************************************************
- * Name: esp32s3_spi3_cmddata
- ****************************************************************************/
+  spi = esp32s2_spibus_initialize(CONFIG_NSH_MMCSDSPIPORTNO);
 
-#if defined(CONFIG_ESP32S3_SPI3) && defined(CONFIG_SPI_CMDDATA)
-
-int esp32s3_spi3_cmddata(struct spi_dev_s *dev, uint32_t devid, bool cmd)
-{
-  if (devid == SPIDEV_DISPLAY(0))
+  if (spi == NULL)
     {
-      /*  This is the Data/Command control pad which determines whether the
-       *  data bits are data or a command.
-       */
+      syslog(LOG_ERR, "ERROR: failed to initialize SPI%d.\n",
+             CONFIG_NSH_MMCSDSPIPORTNO);
+      return -ENODEV;
+    }
 
-      esp32s3_gpiowrite(CONFIG_ESP32S3_SPI3_MISOPIN, !cmd);
+  /* Mounts to /dev/mmcsdN where N in the minor number */
 
-      return OK;
+  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;
     }
 
-  spiinfo("devid: %" PRIu32 " CMD: %s\n", devid, cmd ? "command" :
-          "data");
-
-  return -ENODEV;
+  syslog(LOG_INFO, "INFO: MMCSD initialized\n");
+  return OK;
 }
-
-#endif
diff --git a/boards/xtensa/esp32s2/common/src/esp32s2_board_spi.c 
b/boards/xtensa/esp32s2/common/src/esp32s2_board_spi.c
index 871afa5c39..ed1292768f 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 cd090470fb..03ec565d7c 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 fcea06484b..097bd69895 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 51864aa215..100c9a84a7 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 7378b527a4..ac55f26f6e 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 <nuttx/sdio.h>
 #include <nuttx/mmcsd.h>
 
+#ifdef CONFIG_MMCSD_SPI
+#include <nuttx/spi/spi.h>
+#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 2975555328..45298b4ef8 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 63a70c7b06..d254bde29f 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)

Reply via email to