环境依赖
Win10
ESP-IDF: v4.4.6
开发板: ESP32S3
VSCode: 1.91.1 (system setup)
背景介绍
我们在学习 file_serving 例程的过程中,如果使用了 Flash
的方式,会很轻松地跑通例程。但如果我们的开发板上有SD卡,希望使用SD卡作为存储介质,就会遭遇SD卡挂载失败的错误,且程序会频繁重启。
解决方案
1. 创建例程项目
首先我们在 VSCode
中,按下快捷键 F1
,输入 Welcome
,进入欢迎页面。选择 New project
:
在新建项目页中,我们输入好项目名称、项目所在的文件夹、芯片型号…… 单击右下角的 Choose Template
我们筛选出 file_serving
例程,然后新建:
在新建好的项目中,我们选择好芯片型号,然后插件会自动帮我们去初始化 sdkconfig
,现在我们单击左下角的设置图标:
只选择上 Use SD card for file storage
,其它不动。
2. 修改 Kconfig.projbuild
这个时候,如果我们编译,我们会发现,例程中默认你是使用 SPI
去读取SD卡的,但是 README.md
中有这么一段话:
SD cards can be used either over SPI interface (on all ESP chips) or over SDMMC interface (on ESP32 and ESP32-S3). To use SDMMC interface, enable "Use SDMMC host" (
CONFIG_EXAMPLE_USE_SDMMC_HOST
) option. To use SPI interface, disable this option.
也就是说,SPI
支持所有芯片, SDMMC
仅支持 ESP32 and ESP32-S3
。
很巧合的是,大部分开发板都会选择 SDMMC
这种更快的方式。 所以我们必须使能 CONFIG_EXAMPLE_USE_SDMMC_HOST
。
由于官方例程中的 Kconfig.projbuild
文件总是在考虑 SOC_SDMMC_HOST_SUPPORTED
还有 SOC_SDMMC_USE_GPIO_MATRIX
,也就是更加智能地帮你列出选项,导致它现在4.4版本已经无法正常列出选项,所以我们第一步就是修改它的 Kconfig.projbuild
使得 sdkconfig
可以正常配置。
menu "HTTP file_serving example menu"
config EXAMPLE_MOUNT_SD_CARD
bool "Use SD card for file storage"
default n
help
If this config item is set, the file you upload to server can be chosen to save in the SDcard.
config EXAMPLE_FORMAT_IF_MOUNT_SDCARD_FAILED
bool "Format the card if mount failed"
default n
depends on EXAMPLE_MOUNT_SD_CARD
help
If this config item is set, the card will be formatted if mount has failed.
config USE_SDMMC_HOST
bool "Use SDMMC host"
default n
depends on EXAMPLE_MOUNT_SD_CARD
help
If this config item is set, SDMMC is used to mount the SDcard.
Otherwise, will use SPI host to access and mount the SDcard.
config USE_SPI_HOST
bool "Use SPI host"
default n
depends on EXAMPLE_MOUNT_SD_CARD
help
If this config item is set, SPI is used to mount the SDcard.
Otherwise, will use SDMMC host to access and mount the SDcard.
menu "SD card pin configuration (SPI)"
depends on USE_SPI_HOST
config SPI_PIN_MOSI
int "MOSI GPIO number"
default 15 if IDF_TARGET_ESP32
default 35 if IDF_TARGET_ESP32S2
default 35 if IDF_TARGET_ESP32S3
default 4 # C3 and others
config SPI_PIN_MISO
int "MISO GPIO number"
default 2 if IDF_TARGET_ESP32
default 37 if IDF_TARGET_ESP32S2
default 37 if IDF_TARGET_ESP32S3
default 6 # C3 and others
config SPI_PIN_CLK
int "CLK GPIO number"
default 14 if IDF_TARGET_ESP32
default 36 if IDF_TARGET_ESP32S2
default 36 if IDF_TARGET_ESP32S3
default 5 # C3 and others
config SPI_PIN_CS
int "CS GPIO number"
default 13 if IDF_TARGET_ESP32
default 34 if IDF_TARGET_ESP32S2
default 34 if IDF_TARGET_ESP32S3
default 1 # C3 and others
endmenu
menu "SD card pin configuration (SDMMC)"
depends on USE_SDMMC_HOST
config SLOT_WIDTH
int "To use 4-line SD mode, change this to 4:"
default 1
config SDMMC_PIN_CMD
int "CMD GPIO number"
default 35 if IDF_TARGET_ESP32S3
default 1
config SDMMC_PIN_CLK
int "CLK GPIO number"
default 36 if IDF_TARGET_ESP32S3
default 2
config SDMMC_PIN_D0
int "D0 GPIO number"
default 37 if IDF_TARGET_ESP32S3
default 3
if SLOT_WIDTH = 4
config SDMMC_PIN_D1
int "D1 GPIO number"
default 38 if IDF_TARGET_ESP32S3
default 4
config SDMMC_PIN_D2
int "D2 GPIO number"
default 33 if IDF_TARGET_ESP32S3
default 5
config SDMMC_PIN_D3
int "D3 GPIO number"
default 34 if IDF_TARGET_ESP32S3
default 6
endif
endmenu
config EXAMPLE_HTTPD_CONN_CLOSE_HEADER
bool "Send connection close header from request handlers"
default y
help
If this config item is set, Connection: close header will be set in handlers.
This closes HTTP connection and frees the server socket instantly.
endmenu
修改完毕后,我们点击配置图标进行配置:
由于每个开发板都不同,所以我们需要对照原理图来确定GPIO口.首先,我们打开原理图,搜索 SD
,然后我们就能看到高亮显示的字符,最后找到类似于下面的图:
显然,我手里这个开发板,对应的 GPIO
口定义就可以改为:
💡 4线的 SDMMC
,需要把 Slot Width
改为4,并按照原理图定义好D0到D3的 GPIO
口。
另外,这个例程严重依赖WIFI配置,大家不要忘记配置WIFI的SSID和密码,且要保证电脑和Wi-Fi在同一路由器下。
ESP32S3系列芯片都是默认支持2.4 GHz Wi-Fi,别连错了。
3.修改 mount.c
文件
由于我们改写了配置文件,所以最终生成的宏常量名字也就会发生变化,所以要对应修改一下 mount.c
文件,使之与配置相匹配。
由于主要修改的是从第59行到第70行左右,我就不贴出全部代码了:
#ifdef CONFIG_USE_SDMMC_HOST
ESP_LOGI(TAG, "Using SDMMC peripheral");
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
// This initializes the slot without card detect (CD) and write protect (WP) signals.
// Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
// To use 1-line SD mode, change this to 1:
slot_config.width = CONFIG_SLOT_WIDTH;
#ifdef SOC_SDMMC_USE_GPIO_MATRIX
// For chips which support GPIO Matrix for SDMMC peripheral, specify the pins.
slot_config.clk = CONFIG_SDMMC_PIN_CLK;
slot_config.cmd = CONFIG_SDMMC_PIN_CMD;
slot_config.d0 = CONFIG_SDMMC_PIN_D0;
#if CONFIG_SLOT_WIDTH > 1
slot_config.d1 = CONFIG_SDMMC_PIN_D1;
slot_config.d2 = CONFIG_SDMMC_PIN_D2;
slot_config.d3 = CONFIG_SDMMC_PIN_D3;
#endif
#endif // SOC_SDMMC_USE_GPIO_MATRIX
4. 烧录运行
完成以上的修改之后,我们编译烧录运行,得到类似于下面这样的打印后,我们浏览器打开对应ip地址即可看到文件服务器了:
I (5453) example_connect: Connected to example_connect: sta
I (5463) example_connect: - IPv4 address: 192.168.8.88
结语
好了,以上就是ESP32S3如何使用SD卡运行file_serving例程的所有步骤了,如果您和我的环境配置高度一致,您可以拷贝我的开源项目: esp32s3-file-serving-sd 。
欢迎大家在评论区华山论剑!
修改记录
时间 | 内容 |
---|---|
2024-08-02 | 完成初稿 |