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

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

commit 6f19c59d26f0e1bc672b082259980f87b29f9b44
Author: Filipe Cavalcanti <filipe.cavalca...@espressif.com>
AuthorDate: Tue Jun 17 08:45:54 2025 -0300

    boards/risc-v: add support for SD Card over SPI on ESP32-C3|C6|H2
    
    Add board support for SD Card over SPI for ESP32C3, ESP32C6 and ESP32H2.
    
    Signed-off-by: Filipe Cavalcanti <filipe.cavalca...@espressif.com>
---
 .../common/include/esp_board_mmcsd.h}              | 79 ++++++++++-----------
 boards/risc-v/esp32c3/common/src/Make.defs         |  4 ++
 .../common/src/esp_board_mmcsd.c}                  | 80 +++++++++++++---------
 boards/risc-v/esp32c3/common/src/esp_board_spi.c   |  5 ++
 .../esp32c3/esp32c3-generic/src/esp32c3_bringup.c  | 12 ++++
 .../common/include/esp_board_mmcsd.h}              | 79 ++++++++++-----------
 boards/risc-v/esp32c6/common/src/Make.defs         |  4 ++
 .../common/src/esp_board_mmcsd.c}                  | 80 +++++++++++++---------
 boards/risc-v/esp32c6/common/src/esp_board_spi.c   |  5 ++
 .../esp32c6/esp32c6-devkitc/src/esp32c6_bringup.c  | 12 ++++
 .../esp_board_spi.c => include/esp_board_mmcsd.h}  | 79 ++++++++++-----------
 boards/risc-v/esp32h2/common/src/Make.defs         |  4 ++
 .../src/{esp_board_spi.c => esp_board_mmcsd.c}     | 80 +++++++++++++---------
 boards/risc-v/esp32h2/common/src/esp_board_spi.c   |  5 ++
 .../esp32h2/esp32h2-devkit/src/esp32h2_bringup.c   | 12 ++++
 15 files changed, 306 insertions(+), 234 deletions(-)

diff --git a/boards/risc-v/esp32h2/common/src/esp_board_spi.c 
b/boards/risc-v/esp32c3/common/include/esp_board_mmcsd.h
similarity index 61%
copy from boards/risc-v/esp32h2/common/src/esp_board_spi.c
copy to boards/risc-v/esp32c3/common/include/esp_board_mmcsd.h
index 4e22625e3e8..5b1291dfe25 100644
--- a/boards/risc-v/esp32h2/common/src/esp_board_spi.c
+++ b/boards/risc-v/esp32c3/common/include/esp_board_mmcsd.h
@@ -1,5 +1,5 @@
 /****************************************************************************
- * boards/risc-v/esp32h2/common/src/esp_board_spi.c
+ * boards/risc-v/esp32c3/common/include/esp_board_mmcsd.h
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -20,66 +20,57 @@
  *
  ****************************************************************************/
 
+#ifndef __BOARDS_RISCV_ESP32C3_COMMON_INCLUDE_ESP_BOARD_MMCSD_H
+#define __BOARDS_RISCV_ESP32C3_COMMON_INCLUDE_ESP_BOARD_MMCSD_H
+
 /****************************************************************************
  * Included Files
  ****************************************************************************/
 
 #include <nuttx/config.h>
 
-#include <stdint.h>
-#include <stdbool.h>
-#include <debug.h>
-
-#include <nuttx/spi/spi.h>
-
-#include "espressif/esp_gpio.h"
-
 /****************************************************************************
- * Private Functions
+ * Pre-processor Definitions
  ****************************************************************************/
 
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
+#ifndef __ASSEMBLY__
 
-/****************************************************************************
- * Name: esp_spi2_status
- ****************************************************************************/
-
-#ifdef CONFIG_ESPRESSIF_SPI2
-
-uint8_t esp_spi2_status(struct spi_dev_s *dev, uint32_t devid)
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
 {
-  uint8_t status = 0;
-
-  return status;
-}
-
+#else
+#define EXTERN extern
 #endif
 
 /****************************************************************************
- * Name: esp_spi2_cmddata
+ * Public Function Prototypes
  ****************************************************************************/
 
-#if defined(CONFIG_ESPRESSIF_SPI2) && defined(CONFIG_SPI_CMDDATA)
-
-int esp_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.
-       */
-
-      esp_gpiowrite(CONFIG_ESPRESSIF_SPI2_MISOPIN, !cmd);
-
-      return OK;
-    }
+/****************************************************************************
+ * Name: esp_mmcsd_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.
+ *
+ ****************************************************************************/
 
-  spiinfo("devid: %" PRIu32 " CMD: %s\n", devid, cmd ? "command" :
-          "data");
+#ifdef CONFIG_MMCSD_SPI
+int esp_mmcsd_spi_initialize(void);
+#endif
 
-  return -ENODEV;
+#undef EXTERN
+#if defined(__cplusplus)
 }
-
 #endif
+
+#endif /* __ASSEMBLY__ */
+#endif /* __BOARDS_RISCV_ESP32C3_COMMON_INCLUDE_ESP_BOARD_MMCSD_H */
diff --git a/boards/risc-v/esp32c3/common/src/Make.defs 
b/boards/risc-v/esp32c3/common/src/Make.defs
index 845649943c8..aa5947c74e2 100644
--- a/boards/risc-v/esp32c3/common/src/Make.defs
+++ b/boards/risc-v/esp32c3/common/src/Make.defs
@@ -70,6 +70,10 @@ ifeq ($(CONFIG_SENSORS_BMP180),y)
   CSRCS += esp_board_bmp180.c
 endif
 
+ifeq ($(CONFIG_MMCSD_SPI),y)
+  CSRCS += esp_board_mmcsd.c
+endif
+
 DEPPATH += --dep-path src
 VPATH += :src
 CFLAGS += 
${INCDIR_PREFIX}$(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)src
diff --git a/boards/risc-v/esp32h2/common/src/esp_board_spi.c 
b/boards/risc-v/esp32c3/common/src/esp_board_mmcsd.c
similarity index 55%
copy from boards/risc-v/esp32h2/common/src/esp_board_spi.c
copy to boards/risc-v/esp32c3/common/src/esp_board_mmcsd.c
index 4e22625e3e8..6f356130231 100644
--- a/boards/risc-v/esp32h2/common/src/esp_board_spi.c
+++ b/boards/risc-v/esp32c3/common/src/esp_board_mmcsd.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * boards/risc-v/esp32h2/common/src/esp_board_spi.c
+ * boards/risc-v/esp32c3/common/src/esp_board_mmcsd.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -24,15 +24,20 @@
  * Included Files
  ****************************************************************************/
 
+#include <debug.h>
 #include <nuttx/config.h>
+#include <nuttx/mmcsd.h>
+#include <nuttx/spi/spi.h>
 
-#include <stdint.h>
-#include <stdbool.h>
-#include <debug.h>
+#include "espressif/esp_spi.h"
 
-#include <nuttx/spi/spi.h>
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
 
-#include "espressif/esp_gpio.h"
+/****************************************************************************
+ * Private Definitions
+ ****************************************************************************/
 
 /****************************************************************************
  * Private Functions
@@ -43,43 +48,50 @@
  ****************************************************************************/
 
 /****************************************************************************
- * Name: esp_spi2_status
+ * Name: esp_mmcsd_spi_initialize
+ *
+ * Description:
+ *   Initialize SPI-based SD card.
+ *
+ * Input Parameters:
+ *   minor - The MMC/SD minor device number. The MMC/SD device will be
+ *     registered as /dev/mmcsdN where N is the minor number
+ *
+ * Returned Value:
+ *   Zero (OK) is returned on success; A negated errno value is returned
+ *   to indicate the nature of any failure.
+ *
  ****************************************************************************/
 
-#ifdef CONFIG_ESPRESSIF_SPI2
-
-uint8_t esp_spi2_status(struct spi_dev_s *dev, uint32_t devid)
+int esp_mmcsd_spi_initialize(void)
 {
-  uint8_t status = 0;
-
-  return status;
-}
+  struct spi_dev_s *spi;
+  int ret;
 
-#endif
-
-/****************************************************************************
- * Name: esp_spi2_cmddata
- ****************************************************************************/
+  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);
 
-#if defined(CONFIG_ESPRESSIF_SPI2) && defined(CONFIG_SPI_CMDDATA)
+  spi = esp_spibus_initialize(CONFIG_NSH_MMCSDSPIPORTNO);
 
-int esp_spi2_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;
+    }
 
-      esp_gpiowrite(CONFIG_ESPRESSIF_SPI2_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/risc-v/esp32c3/common/src/esp_board_spi.c 
b/boards/risc-v/esp32c3/common/src/esp_board_spi.c
index 5b14eff67bf..e3cd5e2aae1 100644
--- a/boards/risc-v/esp32c3/common/src/esp_board_spi.c
+++ b/boards/risc-v/esp32c3/common/src/esp_board_spi.c
@@ -52,6 +52,11 @@ uint8_t esp_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;
 }
 
diff --git a/boards/risc-v/esp32c3/esp32c3-generic/src/esp32c3_bringup.c 
b/boards/risc-v/esp32c3/esp32c3-generic/src/esp32c3_bringup.c
index 0aec1ffc155..79284f8ab39 100644
--- a/boards/risc-v/esp32c3/esp32c3-generic/src/esp32c3_bringup.c
+++ b/boards/risc-v/esp32c3/esp32c3-generic/src/esp32c3_bringup.c
@@ -122,6 +122,10 @@
 #  include "espressif/esp_sha.h"
 #endif
 
+#ifdef CONFIG_MMCSD_SPI
+#  include "esp_board_mmcsd.h"
+#endif
+
 #include "esp32c3-generic.h"
 
 /****************************************************************************
@@ -261,6 +265,14 @@ int esp_bringup(void)
 #  endif /* CONFIG_ESPRESSIF_SPI_BITBANG */
 #endif /* CONFIG_ESPRESSIF_SPI */
 
+#if defined(CONFIG_ESPRESSIF_SPI) && defined(CONFIG_MMCSD_SPI)
+  ret = esp_mmcsd_spi_initialize();
+  if (ret < 0)
+    {
+      syslog(LOG_ERR, "ERROR: failed to init MMCSD SPI\n");
+    }
+#endif
+
 #ifdef CONFIG_ESPRESSIF_SPIFLASH
   ret = board_spiflash_init();
   if (ret)
diff --git a/boards/risc-v/esp32h2/common/src/esp_board_spi.c 
b/boards/risc-v/esp32c6/common/include/esp_board_mmcsd.h
similarity index 61%
copy from boards/risc-v/esp32h2/common/src/esp_board_spi.c
copy to boards/risc-v/esp32c6/common/include/esp_board_mmcsd.h
index 4e22625e3e8..9b3dda94ce6 100644
--- a/boards/risc-v/esp32h2/common/src/esp_board_spi.c
+++ b/boards/risc-v/esp32c6/common/include/esp_board_mmcsd.h
@@ -1,5 +1,5 @@
 /****************************************************************************
- * boards/risc-v/esp32h2/common/src/esp_board_spi.c
+ * boards/risc-v/esp32c6/common/include/esp_board_mmcsd.h
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -20,66 +20,57 @@
  *
  ****************************************************************************/
 
+#ifndef __BOARDS_RISCV_ESP32C6_COMMON_INCLUDE_ESP_BOARD_MMCSD_H
+#define __BOARDS_RISCV_ESP32C6_COMMON_INCLUDE_ESP_BOARD_MMCSD_H
+
 /****************************************************************************
  * Included Files
  ****************************************************************************/
 
 #include <nuttx/config.h>
 
-#include <stdint.h>
-#include <stdbool.h>
-#include <debug.h>
-
-#include <nuttx/spi/spi.h>
-
-#include "espressif/esp_gpio.h"
-
 /****************************************************************************
- * Private Functions
+ * Pre-processor Definitions
  ****************************************************************************/
 
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
+#ifndef __ASSEMBLY__
 
-/****************************************************************************
- * Name: esp_spi2_status
- ****************************************************************************/
-
-#ifdef CONFIG_ESPRESSIF_SPI2
-
-uint8_t esp_spi2_status(struct spi_dev_s *dev, uint32_t devid)
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
 {
-  uint8_t status = 0;
-
-  return status;
-}
-
+#else
+#define EXTERN extern
 #endif
 
 /****************************************************************************
- * Name: esp_spi2_cmddata
+ * Public Function Prototypes
  ****************************************************************************/
 
-#if defined(CONFIG_ESPRESSIF_SPI2) && defined(CONFIG_SPI_CMDDATA)
-
-int esp_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.
-       */
-
-      esp_gpiowrite(CONFIG_ESPRESSIF_SPI2_MISOPIN, !cmd);
-
-      return OK;
-    }
+/****************************************************************************
+ * Name: esp_mmcsd_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.
+ *
+ ****************************************************************************/
 
-  spiinfo("devid: %" PRIu32 " CMD: %s\n", devid, cmd ? "command" :
-          "data");
+#ifdef CONFIG_MMCSD_SPI
+int esp_mmcsd_spi_initialize(void);
+#endif
 
-  return -ENODEV;
+#undef EXTERN
+#if defined(__cplusplus)
 }
-
 #endif
+
+#endif /* __ASSEMBLY__ */
+#endif /* __BOARDS_RISCV_ESP32C6_COMMON_INCLUDE_ESP_BOARD_MMCSD_H */
diff --git a/boards/risc-v/esp32c6/common/src/Make.defs 
b/boards/risc-v/esp32c6/common/src/Make.defs
index f54fdb41ed4..cf3c06a7b79 100644
--- a/boards/risc-v/esp32c6/common/src/Make.defs
+++ b/boards/risc-v/esp32c6/common/src/Make.defs
@@ -90,6 +90,10 @@ ifeq ($(CONFIG_NCV7410),y)
   CSRCS += esp_board_ncv7410.c
 endif
 
+ifeq ($(CONFIG_MMCSD_SPI),y)
+  CSRCS += esp_board_mmcsd.c
+endif
+
 DEPPATH += --dep-path src
 VPATH += :src
 CFLAGS += 
${INCDIR_PREFIX}$(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)src
diff --git a/boards/risc-v/esp32h2/common/src/esp_board_spi.c 
b/boards/risc-v/esp32c6/common/src/esp_board_mmcsd.c
similarity index 55%
copy from boards/risc-v/esp32h2/common/src/esp_board_spi.c
copy to boards/risc-v/esp32c6/common/src/esp_board_mmcsd.c
index 4e22625e3e8..688d2213136 100644
--- a/boards/risc-v/esp32h2/common/src/esp_board_spi.c
+++ b/boards/risc-v/esp32c6/common/src/esp_board_mmcsd.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * boards/risc-v/esp32h2/common/src/esp_board_spi.c
+ * boards/risc-v/esp32c6/common/src/esp_board_mmcsd.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -24,15 +24,20 @@
  * Included Files
  ****************************************************************************/
 
+#include <debug.h>
 #include <nuttx/config.h>
+#include <nuttx/mmcsd.h>
+#include <nuttx/spi/spi.h>
 
-#include <stdint.h>
-#include <stdbool.h>
-#include <debug.h>
+#include "espressif/esp_spi.h"
 
-#include <nuttx/spi/spi.h>
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
 
-#include "espressif/esp_gpio.h"
+/****************************************************************************
+ * Private Definitions
+ ****************************************************************************/
 
 /****************************************************************************
  * Private Functions
@@ -43,43 +48,50 @@
  ****************************************************************************/
 
 /****************************************************************************
- * Name: esp_spi2_status
+ * Name: esp_mmcsd_spi_initialize
+ *
+ * Description:
+ *   Initialize SPI-based SD card.
+ *
+ * Input Parameters:
+ *   minor - The MMC/SD minor device number. The MMC/SD device will be
+ *     registered as /dev/mmcsdN where N is the minor number
+ *
+ * Returned Value:
+ *   Zero (OK) is returned on success; A negated errno value is returned
+ *   to indicate the nature of any failure.
+ *
  ****************************************************************************/
 
-#ifdef CONFIG_ESPRESSIF_SPI2
-
-uint8_t esp_spi2_status(struct spi_dev_s *dev, uint32_t devid)
+int esp_mmcsd_spi_initialize(void)
 {
-  uint8_t status = 0;
-
-  return status;
-}
+  struct spi_dev_s *spi;
+  int ret;
 
-#endif
-
-/****************************************************************************
- * Name: esp_spi2_cmddata
- ****************************************************************************/
+  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);
 
-#if defined(CONFIG_ESPRESSIF_SPI2) && defined(CONFIG_SPI_CMDDATA)
+  spi = esp_spibus_initialize(CONFIG_NSH_MMCSDSPIPORTNO);
 
-int esp_spi2_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;
+    }
 
-      esp_gpiowrite(CONFIG_ESPRESSIF_SPI2_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/risc-v/esp32c6/common/src/esp_board_spi.c 
b/boards/risc-v/esp32c6/common/src/esp_board_spi.c
index 32f92f354ee..006738293bf 100644
--- a/boards/risc-v/esp32c6/common/src/esp_board_spi.c
+++ b/boards/risc-v/esp32c6/common/src/esp_board_spi.c
@@ -52,6 +52,11 @@ uint8_t esp_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;
 }
 
diff --git a/boards/risc-v/esp32c6/esp32c6-devkitc/src/esp32c6_bringup.c 
b/boards/risc-v/esp32c6/esp32c6-devkitc/src/esp32c6_bringup.c
index 911de2278da..7f78882d53c 100644
--- a/boards/risc-v/esp32c6/esp32c6-devkitc/src/esp32c6_bringup.c
+++ b/boards/risc-v/esp32c6/esp32c6-devkitc/src/esp32c6_bringup.c
@@ -133,6 +133,10 @@
 #  include "esp_board_ncv7410.h"
 #endif
 
+#ifdef CONFIG_MMCSD_SPI
+#  include "esp_board_mmcsd.h"
+#endif
+
 #include "esp32c6-devkitc.h"
 
 /****************************************************************************
@@ -296,6 +300,14 @@ int esp_bringup(void)
 #  endif /* CONFIG_ESPRESSIF_SPI_BITBANG */
 #endif /* CONFIG_ESPRESSIF_SPI */
 
+#if defined(CONFIG_ESPRESSIF_SPI) && defined(CONFIG_MMCSD_SPI)
+  ret = esp_mmcsd_spi_initialize();
+  if (ret < 0)
+    {
+      syslog(LOG_ERR, "ERROR: failed to init MMCSD SPI\n");
+    }
+#endif
+
 #ifdef CONFIG_ESPRESSIF_SPIFLASH
   ret = board_spiflash_init();
   if (ret)
diff --git a/boards/risc-v/esp32h2/common/src/esp_board_spi.c 
b/boards/risc-v/esp32h2/common/include/esp_board_mmcsd.h
similarity index 61%
copy from boards/risc-v/esp32h2/common/src/esp_board_spi.c
copy to boards/risc-v/esp32h2/common/include/esp_board_mmcsd.h
index 4e22625e3e8..d52f8a62142 100644
--- a/boards/risc-v/esp32h2/common/src/esp_board_spi.c
+++ b/boards/risc-v/esp32h2/common/include/esp_board_mmcsd.h
@@ -1,5 +1,5 @@
 /****************************************************************************
- * boards/risc-v/esp32h2/common/src/esp_board_spi.c
+ * boards/risc-v/esp32h2/common/include/esp_board_mmcsd.h
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -20,66 +20,57 @@
  *
  ****************************************************************************/
 
+#ifndef __BOARDS_RISCV_ESP32H2_COMMON_INCLUDE_ESP_BOARD_MMCSD_H
+#define __BOARDS_RISCV_ESP32H2_COMMON_INCLUDE_ESP_BOARD_MMCSD_H
+
 /****************************************************************************
  * Included Files
  ****************************************************************************/
 
 #include <nuttx/config.h>
 
-#include <stdint.h>
-#include <stdbool.h>
-#include <debug.h>
-
-#include <nuttx/spi/spi.h>
-
-#include "espressif/esp_gpio.h"
-
 /****************************************************************************
- * Private Functions
+ * Pre-processor Definitions
  ****************************************************************************/
 
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
+#ifndef __ASSEMBLY__
 
-/****************************************************************************
- * Name: esp_spi2_status
- ****************************************************************************/
-
-#ifdef CONFIG_ESPRESSIF_SPI2
-
-uint8_t esp_spi2_status(struct spi_dev_s *dev, uint32_t devid)
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
 {
-  uint8_t status = 0;
-
-  return status;
-}
-
+#else
+#define EXTERN extern
 #endif
 
 /****************************************************************************
- * Name: esp_spi2_cmddata
+ * Public Function Prototypes
  ****************************************************************************/
 
-#if defined(CONFIG_ESPRESSIF_SPI2) && defined(CONFIG_SPI_CMDDATA)
-
-int esp_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.
-       */
-
-      esp_gpiowrite(CONFIG_ESPRESSIF_SPI2_MISOPIN, !cmd);
-
-      return OK;
-    }
+/****************************************************************************
+ * Name: esp_mmcsd_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.
+ *
+ ****************************************************************************/
 
-  spiinfo("devid: %" PRIu32 " CMD: %s\n", devid, cmd ? "command" :
-          "data");
+#ifdef CONFIG_MMCSD_SPI
+int esp_mmcsd_spi_initialize(void);
+#endif
 
-  return -ENODEV;
+#undef EXTERN
+#if defined(__cplusplus)
 }
-
 #endif
+
+#endif /* __ASSEMBLY__ */
+#endif /* __BOARDS_RISCV_ESP32H2_COMMON_INCLUDE_ESP_BOARD_MMCSD_H */
diff --git a/boards/risc-v/esp32h2/common/src/Make.defs 
b/boards/risc-v/esp32h2/common/src/Make.defs
index 2bbc9c67d3f..61b8b2bb3fa 100644
--- a/boards/risc-v/esp32h2/common/src/Make.defs
+++ b/boards/risc-v/esp32h2/common/src/Make.defs
@@ -74,6 +74,10 @@ ifeq ($(CONFIG_ESP_PCNT),y)
   CSRCS += esp_board_pcnt.c
 endif
 
+ifeq ($(CONFIG_MMCSD_SPI),y)
+  CSRCS += esp_board_mmcsd.c
+endif
+
 DEPPATH += --dep-path src
 VPATH += :src
 CFLAGS += 
${INCDIR_PREFIX}$(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)src
diff --git a/boards/risc-v/esp32h2/common/src/esp_board_spi.c 
b/boards/risc-v/esp32h2/common/src/esp_board_mmcsd.c
similarity index 55%
copy from boards/risc-v/esp32h2/common/src/esp_board_spi.c
copy to boards/risc-v/esp32h2/common/src/esp_board_mmcsd.c
index 4e22625e3e8..9f21ab33ad3 100644
--- a/boards/risc-v/esp32h2/common/src/esp_board_spi.c
+++ b/boards/risc-v/esp32h2/common/src/esp_board_mmcsd.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * boards/risc-v/esp32h2/common/src/esp_board_spi.c
+ * boards/risc-v/esp32h2/common/src/esp_board_mmcsd.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -24,15 +24,20 @@
  * Included Files
  ****************************************************************************/
 
+#include <debug.h>
 #include <nuttx/config.h>
+#include <nuttx/mmcsd.h>
+#include <nuttx/spi/spi.h>
 
-#include <stdint.h>
-#include <stdbool.h>
-#include <debug.h>
+#include "espressif/esp_spi.h"
 
-#include <nuttx/spi/spi.h>
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
 
-#include "espressif/esp_gpio.h"
+/****************************************************************************
+ * Private Definitions
+ ****************************************************************************/
 
 /****************************************************************************
  * Private Functions
@@ -43,43 +48,50 @@
  ****************************************************************************/
 
 /****************************************************************************
- * Name: esp_spi2_status
+ * Name: esp_mmcsd_spi_initialize
+ *
+ * Description:
+ *   Initialize SPI-based SD card.
+ *
+ * Input Parameters:
+ *   minor - The MMC/SD minor device number. The MMC/SD device will be
+ *     registered as /dev/mmcsdN where N is the minor number
+ *
+ * Returned Value:
+ *   Zero (OK) is returned on success; A negated errno value is returned
+ *   to indicate the nature of any failure.
+ *
  ****************************************************************************/
 
-#ifdef CONFIG_ESPRESSIF_SPI2
-
-uint8_t esp_spi2_status(struct spi_dev_s *dev, uint32_t devid)
+int esp_mmcsd_spi_initialize(void)
 {
-  uint8_t status = 0;
-
-  return status;
-}
+  struct spi_dev_s *spi;
+  int ret;
 
-#endif
-
-/****************************************************************************
- * Name: esp_spi2_cmddata
- ****************************************************************************/
+  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);
 
-#if defined(CONFIG_ESPRESSIF_SPI2) && defined(CONFIG_SPI_CMDDATA)
+  spi = esp_spibus_initialize(CONFIG_NSH_MMCSDSPIPORTNO);
 
-int esp_spi2_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;
+    }
 
-      esp_gpiowrite(CONFIG_ESPRESSIF_SPI2_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/risc-v/esp32h2/common/src/esp_board_spi.c 
b/boards/risc-v/esp32h2/common/src/esp_board_spi.c
index 4e22625e3e8..a94995cd761 100644
--- a/boards/risc-v/esp32h2/common/src/esp_board_spi.c
+++ b/boards/risc-v/esp32h2/common/src/esp_board_spi.c
@@ -52,6 +52,11 @@ uint8_t esp_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;
 }
 
diff --git a/boards/risc-v/esp32h2/esp32h2-devkit/src/esp32h2_bringup.c 
b/boards/risc-v/esp32h2/esp32h2-devkit/src/esp32h2_bringup.c
index 57b27de4a1a..ae47ae9ddb1 100644
--- a/boards/risc-v/esp32h2/esp32h2-devkit/src/esp32h2_bringup.c
+++ b/boards/risc-v/esp32h2/esp32h2-devkit/src/esp32h2_bringup.c
@@ -119,6 +119,10 @@
 #  include "espressif/esp_sha.h"
 #endif
 
+#ifdef CONFIG_MMCSD_SPI
+#  include "esp_board_mmcsd.h"
+#endif
+
 #include "esp32h2-devkit.h"
 
 /****************************************************************************
@@ -282,6 +286,14 @@ int esp_bringup(void)
 #  endif /* CONFIG_ESPRESSIF_SPI_BITBANG */
 #endif /* CONFIG_ESPRESSIF_SPI */
 
+#if defined(CONFIG_ESPRESSIF_SPI) && defined(CONFIG_MMCSD_SPI)
+  ret = esp_mmcsd_spi_initialize();
+  if (ret < 0)
+    {
+      syslog(LOG_ERR, "ERROR: failed to init MMCSD SPI\n");
+    }
+#endif
+
 #ifdef CONFIG_ESPRESSIF_SPIFLASH
   ret = board_spiflash_init();
   if (ret)

Reply via email to