|
|
%!s(int64=8) %!d(string=hai) anos | |
|---|---|---|
| .. | ||
| main | %!s(int64=8) %!d(string=hai) anos | |
| Makefile | %!s(int64=9) %!d(string=hai) anos | |
| README.md | %!s(int64=8) %!d(string=hai) anos | |
This example demonstrates how to use an SD card with ESP32. 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.Note: despite the name, sdmmc component doesn't support MMC/eMMC cards yet. It is also possible to extend sdmmc component to support SPI mode with SD cards via SPI peripheral.
To run this example, ESP32 development board needs 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 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 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. In SPI mode, pins can be customized. See the initialization of sdspi_slot_config_t structure 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.
By default, example code uses the following initializer for SDMMC host peripheral configuration:
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
Among other things, this sets host.flags to SDMMC_HOST_FLAG_4BIT, which means that SD/MMC driver 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 host.flags to SDMMC_HOST_FLAG_1BIT — 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 SD card. To use SPI peripheral instead, uncomment #define USE_SPI_MODE in the example code.
Here is an example console output. In this case a 128MB SDSC card was connected, and format_if_mount_failed parameter was set to true in the source code. Card was unformatted, so the initial mount has failed. Card was then partitioned, formatted, and mounted again.
I (1776) example: Initializing SD card
W (1856) vfs_fat_sdmmc: failed to mount card (13)
W (1856) vfs_fat_sdmmc: partitioning card
W (1856) vfs_fat_sdmmc: formatting card
W (2726) vfs_fat_sdmmc: mounting again
I (2736) example: Card info:
I (2736) example: Name: SU128
I (2736) example: Type: SDSC
I (2736) example: Capacity: 120 MB
I (2736) example: Max clock speed: 25 MHz
I (2736) example: Opening file
I (2756) example: File written
I (2756) example: Renaming file
I (2756) example: Reading file
I (2756) example: Read from file: 'Hello SU128!'
I (2756) example: Card unmounted