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

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

commit cafc1cc905c3c32aaa8c111703ec0eb290ff78b9
Author: Filipe Cavalcanti <[email protected]>
AuthorDate: Thu Aug 20 10:28:50 2026 -0300

    boards/risc-v: SD Card support on esp32p4-tab5
    
    Adds support for using SD Card on esp32p4-tab5 board.
    Common source is added to esp32p4 common board directory which can be used
    on different boards.
    
    Signed-off-by: Filipe Cavalcanti <[email protected]>
---
 .../esp32p4/common/include/esp_board_mmcsd.h       | 76 +++++++++++++++++
 boards/risc-v/esp32p4/common/src/CMakeLists.txt    | 10 ++-
 boards/risc-v/esp32p4/common/src/Make.defs         |  4 +
 boards/risc-v/esp32p4/common/src/esp_board_mmcsd.c | 97 ++++++++++++++++++++++
 boards/risc-v/esp32p4/common/src/esp_board_spi.c   |  7 +-
 .../esp32p4-tab5/configs/sdmmc_spi/defconfig       | 70 ++++++++++++++++
 boards/risc-v/esp32p4/esp32p4-tab5/src/Make.defs   |  4 +
 .../risc-v/esp32p4/esp32p4-tab5/src/esp32p4-tab5.h | 20 +++++
 .../esp32p4/esp32p4-tab5/src/esp32p4_bringup.c     | 36 ++++++++
 .../esp32p4/esp32p4-tab5/src/esp32p4_sd_power.c    | 89 ++++++++++++++++++++
 10 files changed, 409 insertions(+), 4 deletions(-)

diff --git a/boards/risc-v/esp32p4/common/include/esp_board_mmcsd.h 
b/boards/risc-v/esp32p4/common/include/esp_board_mmcsd.h
new file mode 100644
index 00000000000..db06e39110d
--- /dev/null
+++ b/boards/risc-v/esp32p4/common/include/esp_board_mmcsd.h
@@ -0,0 +1,76 @@
+/****************************************************************************
+ * boards/risc-v/esp32p4/common/include/esp_board_mmcsd.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_RISCV_ESP32P4_COMMON_INCLUDE_ESP_BOARD_MMCSD_H
+#define __BOARDS_RISCV_ESP32P4_COMMON_INCLUDE_ESP_BOARD_MMCSD_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+/****************************************************************************
+ * 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: 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.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_MMCSD_SPI
+int esp_mmcsd_spi_initialize(void);
+#endif
+
+#undef EXTERN
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* __ASSEMBLY__ */
+#endif /* __BOARDS_RISCV_ESP32P4_COMMON_INCLUDE_ESP_BOARD_MMCSD_H */
diff --git a/boards/risc-v/esp32p4/common/src/CMakeLists.txt 
b/boards/risc-v/esp32p4/common/src/CMakeLists.txt
index 6c5b53d0e04..e552b40888e 100644
--- a/boards/risc-v/esp32p4/common/src/CMakeLists.txt
+++ b/boards/risc-v/esp32p4/common/src/CMakeLists.txt
@@ -76,10 +76,16 @@ if(CONFIG_ARCH_BOARD_COMMON)
     list(APPEND SRCS esp_board_pcnt.c)
   endif()
 
-  if(CONFIG_ESPRESSIF_ULP_USE_TEST_BIN)
-    include(${NUTTX_DIR}/arch/risc-v/src/common/espressif/ulp.cmake)
+  if(CONFIG_MMCSD_SPI)
+    list(APPEND SRCS esp_board_mmcsd.c)
   endif()
 
 endif()
 
+if(CONFIG_ESPRESSIF_ULP_USE_TEST_BIN)
+  include(${NUTTX_DIR}/arch/risc-v/src/common/espressif/ulp.cmake)
+endif()
+
+endif()
+
 target_sources(board PRIVATE ${SRCS})
diff --git a/boards/risc-v/esp32p4/common/src/Make.defs 
b/boards/risc-v/esp32p4/common/src/Make.defs
index 7436d237a20..f321ba528b6 100644
--- a/boards/risc-v/esp32p4/common/src/Make.defs
+++ b/boards/risc-v/esp32p4/common/src/Make.defs
@@ -76,6 +76,10 @@ ifeq ($(CONFIG_ESP_PCNT),y)
   CSRCS += esp_board_pcnt.c
 endif
 
+ifeq ($(CONFIG_MMCSD_SPI),y)
+  CSRCS += esp_board_mmcsd.c
+endif
+
 ifeq ($(CONFIG_ESPRESSIF_ULP_USE_TEST_BIN),y)
   include 
$(TOPDIR)$(DELIM)arch$(DELIM)risc-v$(DELIM)src$(DELIM)common$(DELIM)espressif$(DELIM)ulp_makefile
 endif
diff --git a/boards/risc-v/esp32p4/common/src/esp_board_mmcsd.c 
b/boards/risc-v/esp32p4/common/src/esp_board_mmcsd.c
new file mode 100644
index 00000000000..94d507321e3
--- /dev/null
+++ b/boards/risc-v/esp32p4/common/src/esp_board_mmcsd.c
@@ -0,0 +1,97 @@
+/****************************************************************************
+ * boards/risc-v/esp32p4/common/src/esp_board_mmcsd.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 <nuttx/debug.h>
+#include <nuttx/config.h>
+#include <nuttx/mmcsd.h>
+#include <nuttx/spi/spi.h>
+
+#include "espressif/esp_spi.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * 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.
+ *
+ ****************************************************************************/
+
+int esp_mmcsd_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 = esp_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/risc-v/esp32p4/common/src/esp_board_spi.c 
b/boards/risc-v/esp32p4/common/src/esp_board_spi.c
index 057725b5230..4319a665eec 100644
--- a/boards/risc-v/esp32p4/common/src/esp_board_spi.c
+++ b/boards/risc-v/esp32p4/common/src/esp_board_spi.c
@@ -103,9 +103,12 @@ int esp_spi2_cmddata(struct spi_dev_s *dev, uint32_t 
devid, bool cmd)
 
 uint8_t esp_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;
+  return 0;
 }
 
 #endif
diff --git a/boards/risc-v/esp32p4/esp32p4-tab5/configs/sdmmc_spi/defconfig 
b/boards/risc-v/esp32p4/esp32p4-tab5/configs/sdmmc_spi/defconfig
new file mode 100644
index 00000000000..fd3293b7ff8
--- /dev/null
+++ b/boards/risc-v/esp32p4/esp32p4-tab5/configs/sdmmc_spi/defconfig
@@ -0,0 +1,70 @@
+#
+# 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_MMCSD_HAVE_CARDDETECT is not set
+# CONFIG_MMCSD_HAVE_WRITEPROTECT is not set
+# CONFIG_NSH_ARGCAT is not set
+# CONFIG_NSH_CMDOPT_HEXDUMP is not set
+CONFIG_ARCH="risc-v"
+CONFIG_ARCH_BOARD="esp32p4-tab5"
+CONFIG_ARCH_BOARD_COMMON=y
+CONFIG_ARCH_BOARD_ESP32P4_TAB5=y
+CONFIG_ARCH_CHIP="esp32p4"
+CONFIG_ARCH_CHIP_ESP32P4=y
+CONFIG_ARCH_INTERRUPTSTACK=2048
+CONFIG_ARCH_IRQ_TO_NDX=y
+CONFIG_ARCH_MINIMAL_VECTORTABLE_DYNAMIC=y
+CONFIG_ARCH_NUSER_INTERRUPTS=17
+CONFIG_ARCH_RISCV=y
+CONFIG_ARCH_STACKDUMP=y
+CONFIG_BOARDCTL_RESET=y
+CONFIG_BOARD_LOOPSPERMSEC=15000
+CONFIG_BUILTIN=y
+CONFIG_ESP32P4_REV_MIN_100=y
+CONFIG_ESP32P4_SELECTS_REV_LESS_V3=y
+CONFIG_ESPRESSIF_FLASH_16M=y
+CONFIG_ESPRESSIF_FLASH_MODE_QIO=y
+CONFIG_ESPRESSIF_GPIO_IRQ=y
+CONFIG_ESPRESSIF_I2C0=y
+CONFIG_ESPRESSIF_I2C0_SCLPIN=32
+CONFIG_ESPRESSIF_I2C0_SDAPIN=31
+CONFIG_ESPRESSIF_LDO=y
+CONFIG_ESPRESSIF_SPI3=y
+CONFIG_ESPRESSIF_SPI3_CLKPIN=43
+CONFIG_ESPRESSIF_SPI3_CSPIN=42
+CONFIG_ESPRESSIF_SPI3_MISOPIN=39
+CONFIG_ESPRESSIF_SPI3_MOSIPIN=44
+CONFIG_ESPRESSIF_SPIRAM=y
+CONFIG_ESPRESSIF_USBSERIAL=y
+CONFIG_EXPERIMENTAL=y
+CONFIG_FAT_LCNAMES=y
+CONFIG_FAT_LFN=y
+CONFIG_FS_FAT=y
+CONFIG_FS_PROCFS=y
+CONFIG_IDLETHREAD_STACKSIZE=2048
+CONFIG_INIT_ENTRYPOINT="nsh_main"
+CONFIG_INTELHEX_BINARY=y
+CONFIG_LIBC_PERROR_STDOUT=y
+CONFIG_LIBC_STRERROR=y
+CONFIG_MMCSD=y
+CONFIG_MM_REGIONS=3
+CONFIG_NFILE_DESCRIPTORS_PER_BLOCK=6
+CONFIG_NSH_BUILTIN_APPS=y
+CONFIG_NSH_FILEIOSIZE=512
+CONFIG_NSH_MMCSDSPIPORTNO=3
+CONFIG_NSH_READLINE=y
+CONFIG_NSH_STRERROR=y
+CONFIG_PREALLOC_TIMERS=0
+CONFIG_RR_INTERVAL=200
+CONFIG_SCHED_BACKTRACE=y
+CONFIG_SCHED_WAITPID=y
+CONFIG_START_DAY=29
+CONFIG_START_MONTH=11
+CONFIG_START_YEAR=2019
+CONFIG_SYSTEM_DUMPSTACK=y
+CONFIG_SYSTEM_I2CTOOL=y
+CONFIG_SYSTEM_NSH=y
diff --git a/boards/risc-v/esp32p4/esp32p4-tab5/src/Make.defs 
b/boards/risc-v/esp32p4/esp32p4-tab5/src/Make.defs
index 2a64509de49..34610b8add7 100644
--- a/boards/risc-v/esp32p4/esp32p4-tab5/src/Make.defs
+++ b/boards/risc-v/esp32p4/esp32p4-tab5/src/Make.defs
@@ -32,6 +32,10 @@ ifeq ($(CONFIG_ESP32P4_TAB5_HMI_POWER),y)
   CSRCS += esp32p4_hmi_power.c
 endif
 
+ifeq ($(CONFIG_MMCSD_SPI),y)
+  CSRCS += esp32p4_sd_power.c
+endif
+
 ifeq ($(CONFIG_ESP32P4_TAB5_LCD),y)
   CSRCS += esp32p4_display.c esp32p4_mipi_dpi.c
   ifeq ($(CONFIG_ESP32P4_TAB5_LCD_ST7121),y)
diff --git a/boards/risc-v/esp32p4/esp32p4-tab5/src/esp32p4-tab5.h 
b/boards/risc-v/esp32p4/esp32p4-tab5/src/esp32p4-tab5.h
index 7fc4a47672f..f9d94bc21a5 100644
--- a/boards/risc-v/esp32p4/esp32p4-tab5/src/esp32p4-tab5.h
+++ b/boards/risc-v/esp32p4/esp32p4-tab5/src/esp32p4-tab5.h
@@ -255,5 +255,25 @@ int tab5_touchscreen_power_init(void);
 int tab5_touchscreen_init(void);
 #endif /* CONFIG_ESP32P4_TAB5_TOUCHSCREEN */
 
+#ifdef CONFIG_MMCSD_SPI
+
+/****************************************************************************
+ * Name: tab5_sd_card_power
+ *
+ * Description:
+ *   Enable or disable SD card power.
+ *
+ * Input Parameters:
+ *   on - True to enable the SD card power, false to disable it.
+ *
+ * Returned Value:
+ *   Zero on success, -1 on failure.
+ *
+ ****************************************************************************/
+
+int tab5_sd_card_power(bool on);
+
+#endif /* CONFIG_MMCSD_SPI */
+
 #endif /* __ASSEMBLY__ */
 #endif /* __BOARDS_RISCV_ESP32P4_ESP32P4_TAB5_SRC_ESP32P4_TAB5_H */
diff --git a/boards/risc-v/esp32p4/esp32p4-tab5/src/esp32p4_bringup.c 
b/boards/risc-v/esp32p4/esp32p4-tab5/src/esp32p4_bringup.c
index bf7fb985d7b..de8a71032b3 100644
--- a/boards/risc-v/esp32p4/esp32p4-tab5/src/esp32p4_bringup.c
+++ b/boards/risc-v/esp32p4/esp32p4-tab5/src/esp32p4_bringup.c
@@ -50,6 +50,10 @@
 #  include <nuttx/video/fb.h>
 #endif
 
+#ifdef CONFIG_MMCSD_SPI
+#  include "esp_board_mmcsd.h"
+#endif
+
 #include <arch/board/board.h>
 
 #include "esp32p4-tab5.h"
@@ -192,6 +196,38 @@ int esp_bringup(void)
     }
 #endif
 
+#if defined(CONFIG_ESPRESSIF_SPI) && defined(CONFIG_MMCSD_SPI)
+  ret = tab5_sd_card_power(true);
+  if (ret < 0)
+    {
+      syslog(LOG_ERR, "ERROR: failed to init SD card power\n");
+    }
+  else
+    {
+      syslog(LOG_INFO, "INFO: SD card power initialized\n");
+    }
+
+  ret = esp_mmcsd_spi_initialize();
+  if (ret < 0)
+    {
+      syslog(LOG_ERR, "ERROR: failed to init MMCSD SPI\n");
+    }
+
+#if defined(CONFIG_FS_FAT)
+  /* Mount the VFAT volume /mnt (2 attempts) */
+
+  ret = nx_mount("/dev/mmcsd0", "/mnt", "vfat", 0, NULL);
+  if (ret < 0)
+    {
+      ret = nx_mount("/dev/mmcsd0", "/mnt", "vfat", 0, NULL);
+      if (ret < 0)
+        {
+          syslog(LOG_ERR, "ERROR: Failed to mount the FS volume: %d\n", ret);
+        }
+    }
+#endif /* CONFIG_FS_FAT */
+#endif
+
 #ifdef CONFIG_FS_TMPFS
   /* Mount the tmpfs file system */
 
diff --git a/boards/risc-v/esp32p4/esp32p4-tab5/src/esp32p4_sd_power.c 
b/boards/risc-v/esp32p4/esp32p4-tab5/src/esp32p4_sd_power.c
new file mode 100644
index 00000000000..4804ad78592
--- /dev/null
+++ b/boards/risc-v/esp32p4/esp32p4-tab5/src/esp32p4_sd_power.c
@@ -0,0 +1,89 @@
+/****************************************************************************
+ * boards/risc-v/esp32p4/esp32p4-tab5/src/esp32p4_sd_power.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 <nuttx/config.h>
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <syslog.h>
+
+#include <errno.h>
+#include "espressif/esp_ldo.h"
+
+#include "esp32p4-tab5.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* MIPI PHY LDO channel and voltage */
+
+#define ESP_LDO_SD_CARD_CHAN       4
+#define ESP_LDO_SD_CARD_VOLTAGE_MV 3300
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct esp_ldo_config_t g_sd_card_ldo_config =
+  {
+    .chan_id = ESP_LDO_SD_CARD_CHAN,
+    .voltage_mv = ESP_LDO_SD_CARD_VOLTAGE_MV,
+    .handler = NULL,
+  };
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tab5_sd_card_power
+ *
+ * Description:
+ *   Enable or disable SD card power.
+ *
+ * Input Parameters:
+ *   on - True to enable the SD card power, false to disable it.
+ *
+ * Returned Value:
+ *   Zero on success, -1 on failure.
+ *
+ ****************************************************************************/
+
+int tab5_sd_card_power(bool on)
+{
+  int ret = OK;
+  if (on)
+    {
+      ret = esp_ldo_channel_acquire(&g_sd_card_ldo_config);
+    }
+  else
+    {
+      ret = esp_ldo_channel_release(&g_sd_card_ldo_config);
+    }
+
+  return ret;
+}

Reply via email to