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.cavalcanti@espressif.com>
This commit is contained in:
Filipe Cavalcanti
2025-05-28 14:09:59 -03:00
committed by Xiang Xiao
parent ba38432a0e
commit 452292159b
10 changed files with 292 additions and 4 deletions
@@ -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 <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: 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 */
@@ -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
@@ -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 <nuttx/config.h>
#include <syslog.h>
#include <nuttx/mmcsd.h>
#include <nuttx/spi/spi.h>
#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;
}
@@ -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;
}
@@ -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.
@@ -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 */
#endif /* __BOARDS_XTENSA_ESP32S3_COMMON_INCLUDE_ESP32S3_BOARD_SDMMC_H */
@@ -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
@@ -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 */
@@ -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;
}
@@ -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)