|
|
5 лет назад | |
|---|---|---|
| .. | ||
| main | 5 лет назад | |
| CMakeLists.txt | 5 лет назад | |
| Makefile | 9 лет назад | |
| README.md | 5 лет назад | |
| sd_card_example_test.py | 5 лет назад | |
| sdkconfig.ci | 5 лет назад | |
(See the README.md file in the upper level 'examples' directory for more information about examples.)
This example demonstrates how to use an SD card with ESP32 or ESP32-S2. Example does the following steps:
esp_vfs_fat_sdmmc_mount function to:
fopen and write to it using fprintf.stat function, and remove it using unlink function.This example support SD (SDSC, SDHC, SDXC) cards and eMMC chips.
This example runs on ESP-WROVER-KIT boards without any extra modifications required, only the SD card needs to be inserted into the slot.
Other ESP32 development boards need to be connected to SD card as follows:
| ESP32 pin | SD card pin | SPI pin | Notes |
|---|---|---|---|
| GPIO14 (MTMS) | CLK | SCK | 10k pullup in SD mode |
| GPIO15 (MTDO) | CMD | MOSI | 10k pullup, both in SD and SPI modes |
| GPIO2 | D0 | MISO | 10k pullup in SD mode, pull low to go into download mode (see Note about GPIO2 below!) |
| GPIO4 | D1 | N/C | not used in 1-line SD mode; 10k pullup in 4-line SD mode |
| GPIO12 (MTDI) | D2 | N/C | not used in 1-line SD mode; 10k pullup in 4-line SD mode (see Note about GPIO12 below!) |
| GPIO13 (MTCK) | D3 | CS | not used in 1-line SD mode, but card's D3 pin must have a 10k pullup |
| N/C | CD | optional, not used in the example | |
| N/C | WP | optional, not used in the example |
This example doesn't utilize card detect (CD) and write protect (WP) signals from SD card slot.
With the given pinout for SPI mode, same connections between the SD card and ESP32 can be used to test both SD and SPI modes, provided that the appropriate pullups are in place. See the document about pullup requirements for more details about pullup support and compatibility of modules and development boards.
In SPI mode, pins can be customized. See the initialization of spi_bus_config_t and sdspi_slot_config_t structures in the example code.
Note that ESP32-S2 doesn't include SD Host peripheral and only supports SD over SPI. Therefore only SCK, MOSI, MISO, CS and ground pins need to be connected.
| ESP32-S2 pin | SD card pin | SPI pin | Notes |
|---|---|---|---|
| GPIO14 | CLK | SCK | 10k pullup |
| GPIO15 | CMD | MOSI | 10k pullup |
| GPIO2 | D0 | MISO | 10k pullup |
| GPIO13 | D3 | CS | 10k pullup |
| N/C | CD | optional, not used in the example | |
| N/C | WP | optional, not used in the example |
In SPI mode, pins can be customized. See the initialization of spi_bus_config_t and sdspi_slot_config_t structures in the example code.
GPIO2 pin is used as a bootstrapping pin, and should be low to enter UART download mode. One way to do this is to connect GPIO0 and GPIO2 using a jumper, and then the auto-reset circuit on most development boards will pull GPIO2 low along with GPIO0, when entering download mode.
GPIO12 is used as a bootstrapping pin to select output voltage of an internal regulator which powers the flash chip (VDD_SDIO). This pin has an internal pulldown so if left unconnected it will read low at reset (selecting default 3.3V operation). When adding a pullup to this pin for SD card operation, consider the following:
gpio_pullup_en(GPIO_NUM_12); call. Most SD cards work fine when an internal pullup on GPIO12 line is enabled. Note that if ESP32 experiences a power-on reset while the SD card is sending data, high level on GPIO12 can be latched into the bootstrapping register, and ESP32 will enter a boot loop until external reset with correct GPIO12 level is applied.The following command can be used to program flash voltage selection efuses to 3.3V:
components/esptool_py/esptool/espefuse.py set_flash_voltage 3.3V
This command will burn the XPD_SDIO_TIEH, XPD_SDIO_FORCE, and XPD_SDIO_REG efuses. With all three burned to value 1, the internal VDD_SDIO flash voltage regulator is permanently enabled at 3.3V. See the technical reference manual for more details.
espefuse.py has a --do-not-confirm option if running from an automated flashing script.
SD card can be used in various modes. See below on choosing the mode to be used.
By default, example code uses the following initializer for SDMMC slot configuration:
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
Among other things, this sets slot_config.width = 0, which means that SD/MMC driver will use the maximum bus width supported by the slot. For slot 1, it will switch to 4-line mode when initializing the card (initial communication always happens in 1-line mode). If some of the card's D1, D2, D3 pins are not connected to the ESP32, set slot_config.width = 1 — then the SD/MMC driver will not attempt to switch to 4-line mode.
Note that even if card's D3 line is not connected to the ESP32, it still has to be pulled up, otherwise the card will go into SPI protocol mode.
By default, the example uses SDMMC Host peripheral to access the card using SD protocol. To use SPI peripheral instead (and SPI protocol), uncomment #define USE_SPI_MODE line in the example code.
Build the project and flash it to the board, then run monitor tool to view serial output:
idf.py -p PORT flash monitor
(Replace PORT with serial port name.)
(To exit the serial monitor, type Ctrl-].)
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
Here is an example console output. In this case a 128MB SDSC card was connected, and EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option enabled. Card was unformatted, so the initial mount has failed. Card was then partitioned, formatted, and mounted again.
I (336) example: Initializing SD card
I (336) example: Using SDMMC peripheral
I (336) gpio: GPIO[13]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
W (596) vfs_fat_sdmmc: failed to mount card (13)
W (596) vfs_fat_sdmmc: partitioning card
W (596) vfs_fat_sdmmc: formatting card, allocation unit size=16384
W (7386) vfs_fat_sdmmc: mounting again
Name: XA0E5
Type: SDHC/SDXC
Speed: 20 MHz
Size: 61068MB
I (7386) example: Opening file
I (7396) example: File written
I (7396) example: Renaming file
I (7396) example: Reading file
I (7396) example: Read from file: 'Hello XA0E5!'
I (7396) example: Card unmounted
Connecting........_____....._____....._____....._____....._____....._____....._____
A fatal error occurred: Failed to connect to Espressif device: Invalid head of packet (0x34)
Disconnect the SD card D0/MISO line from GPIO2 and try uploading again. Read "Note about GPIO2" above.
sdmmc_init_sd_scr: send_scr (1) returned 0x107 errorCheck connections between the card and the ESP32. For example, if you have disconnected GPIO2 to work around the flashing issue, connect it back and reset the ESP32 (using a button on the development board, or by pressing Ctrl-T Ctrl-R in IDF Monitor).
sdmmc_check_scr: send_scr returned 0xffffffff errorConnections between the card and the ESP32 are too long for the frequency used. Try using shorter connections, or try reducing the clock speed of SD interface.
example: Failed to mount filesystem. If you want the card to be formatted, set the EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.
The example will be able to mount only cards formatted using FAT32 filesystem. If the card is formatted as exFAT or some other filesystem, you have an option to format it in the example code. Enable the EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option, then build and flash the example.