fix(examples): Disable formatting SD cards in storage examples by default

This commit is contained in:
Adam Múdry 2024-02-07 13:40:40 +01:00
parent 7380f96017
commit 910c82b27c
9 changed files with 33 additions and 8 deletions

View File

@ -5,6 +5,8 @@
(See the README.md file in the upper level 'examples' directory for more information about examples.) (See the README.md file in the upper level 'examples' directory for more information about examples.)
__WARNING:__ This example can potentially delete all data from your SD card (when formatting is enabled). Back up your data first before proceeding.
This example demonstrates how to use an SD card with an ESP device. Example does the following steps: This example demonstrates how to use an SD card with an ESP device. Example does the following steps:
1. Use an "all-in-one" `esp_vfs_fat_sdmmc_mount` function to: 1. Use an "all-in-one" `esp_vfs_fat_sdmmc_mount` function to:
@ -12,10 +14,11 @@ This example demonstrates how to use an SD card with an ESP device. Example does
- probe and initialize an SD card, - probe and initialize an SD card,
- mount FAT filesystem using FATFS library (and format card, if the filesystem cannot be mounted), - mount FAT filesystem using FATFS library (and format card, if the filesystem cannot be mounted),
- register FAT filesystem in VFS, enabling C standard library and POSIX functions to be used. - register FAT filesystem in VFS, enabling C standard library and POSIX functions to be used.
2. Print information about the card, such as name, type, capacity, and maximum supported frequency. 1. Print information about the card, such as name, type, capacity, and maximum supported frequency.
3. Create a file using `fopen` and write to it using `fprintf`. 1. Create a file using `fopen` and write to it using `fprintf`.
4. Rename the file. Before renaming, check if destination file already exists using `stat` function, and remove it using `unlink` function. 1. Rename the file. Before renaming, check if destination file already exists using `stat` function, and remove it using `unlink` function.
5. Open renamed file for reading, read back the line, and print it to the terminal. 1. Open renamed file for reading, read back the line, and print it to the terminal.
1. __OPTIONAL:__ Format the SD card, check if the file doesn't exist anymore.
This example supports SD (SDSC, SDHC, SDXC) cards and eMMC chips. This example supports SD (SDSC, SDHC, SDXC) cards and eMMC chips.

View File

@ -7,6 +7,12 @@ menu "SD/MMC Example Configuration"
If this config item is set, format_if_mount_failed will be set to true and the card will be formatted if If this config item is set, format_if_mount_failed will be set to true and the card will be formatted if
the mount has failed. the mount has failed.
config EXAMPLE_FORMAT_SD_CARD
bool "Format the card as a part of the example"
default n
help
If this config item is set, the card will be formatted as a part of the example.
choice EXAMPLE_SDMMC_BUS_WIDTH choice EXAMPLE_SDMMC_BUS_WIDTH
prompt "SD/MMC bus width" prompt "SD/MMC bus width"
default EXAMPLE_SDMMC_BUS_WIDTH_4 default EXAMPLE_SDMMC_BUS_WIDTH_4

View File

@ -170,6 +170,7 @@ void app_main(void)
} }
// Format FATFS // Format FATFS
#ifdef CONFIG_EXAMPLE_FORMAT_SD_CARD
ret = esp_vfs_fat_sdcard_format(mount_point, card); ret = esp_vfs_fat_sdcard_format(mount_point, card);
if (ret != ESP_OK) { if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to format FATFS (%s)", esp_err_to_name(ret)); ESP_LOGE(TAG, "Failed to format FATFS (%s)", esp_err_to_name(ret));
@ -182,6 +183,7 @@ void app_main(void)
} else { } else {
ESP_LOGI(TAG, "file doesnt exist, format done"); ESP_LOGI(TAG, "file doesnt exist, format done");
} }
#endif // CONFIG_EXAMPLE_FORMAT_SD_CARD
const char *file_nihao = MOUNT_POINT"/nihao.txt"; const char *file_nihao = MOUNT_POINT"/nihao.txt";
memset(data, 0, EXAMPLE_MAX_CHAR_SIZE); memset(data, 0, EXAMPLE_MAX_CHAR_SIZE);

View File

@ -1,3 +1,4 @@
CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED=y CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED=y
CONFIG_FATFS_VFS_FSTAT_BLKSIZE=4096 CONFIG_FATFS_VFS_FSTAT_BLKSIZE=4096
CONFIG_ESP_TASK_WDT_EN=n CONFIG_ESP_TASK_WDT_EN=n
CONFIG_EXAMPLE_FORMAT_SD_CARD=y

View File

@ -5,6 +5,8 @@
(See the README.md file in the upper level 'examples' directory for more information about examples.) (See the README.md file in the upper level 'examples' directory for more information about examples.)
__WARNING:__ This example can potentially delete all data from your SD card (when formatting is enabled). Back up your data first before proceeding.
This example demonstrates how to use an SD card with an ESP device over an SPI interface. Example does the following steps: This example demonstrates how to use an SD card with an ESP device over an SPI interface. Example does the following steps:
1. Use an "all-in-one" `esp_vfs_fat_sdspi_mount` function to: 1. Use an "all-in-one" `esp_vfs_fat_sdspi_mount` function to:
@ -12,10 +14,11 @@ This example demonstrates how to use an SD card with an ESP device over an SPI i
- probe and initialize the card connected to SPI bus (DMA channel 1, MOSI, MISO and CLK lines, chip-specific SPI host id), - probe and initialize the card connected to SPI bus (DMA channel 1, MOSI, MISO and CLK lines, chip-specific SPI host id),
- mount FAT filesystem using FATFS library (and format card, if the filesystem cannot be mounted), - mount FAT filesystem using FATFS library (and format card, if the filesystem cannot be mounted),
- register FAT filesystem in VFS, enabling C standard library and POSIX functions to be used. - register FAT filesystem in VFS, enabling C standard library and POSIX functions to be used.
2. Print information about the card, such as name, type, capacity, and maximum supported frequency. 1. Print information about the card, such as name, type, capacity, and maximum supported frequency.
3. Create a file using `fopen` and write to it using `fprintf`. 1. Create a file using `fopen` and write to it using `fprintf`.
4. Rename the file. Before renaming, check if destination file already exists using `stat` function, and remove it using `unlink` function. 1. Rename the file. Before renaming, check if destination file already exists using `stat` function, and remove it using `unlink` function.
5. Open renamed file for reading, read back the line, and print it to the terminal. 1. Open renamed file for reading, read back the line, and print it to the terminal.
1. __OPTIONAL:__ Format the SD card, check if the file doesn't exist anymore.
This example support SD (SDSC, SDHC, SDXC) cards. This example support SD (SDSC, SDHC, SDXC) cards.

View File

@ -7,6 +7,12 @@ menu "SD SPI Example Configuration"
If this config item is set, format_if_mount_failed will be set to true and the card will be formatted if If this config item is set, format_if_mount_failed will be set to true and the card will be formatted if
the mount has failed. the mount has failed.
config EXAMPLE_FORMAT_SD_CARD
bool "Format the card as a part of the example"
default n
help
If this config item is set, the card will be formatted as a part of the example.
config EXAMPLE_PIN_MOSI config EXAMPLE_PIN_MOSI
int "MOSI GPIO number" int "MOSI GPIO number"
default 15 if IDF_TARGET_ESP32 default 15 if IDF_TARGET_ESP32

View File

@ -166,6 +166,7 @@ void app_main(void)
} }
// Format FATFS // Format FATFS
#ifdef CONFIG_EXAMPLE_FORMAT_SD_CARD
ret = esp_vfs_fat_sdcard_format(mount_point, card); ret = esp_vfs_fat_sdcard_format(mount_point, card);
if (ret != ESP_OK) { if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to format FATFS (%s)", esp_err_to_name(ret)); ESP_LOGE(TAG, "Failed to format FATFS (%s)", esp_err_to_name(ret));
@ -178,6 +179,7 @@ void app_main(void)
} else { } else {
ESP_LOGI(TAG, "file doesnt exist, format done"); ESP_LOGI(TAG, "file doesnt exist, format done");
} }
#endif // CONFIG_EXAMPLE_FORMAT_SD_CARD
const char *file_nihao = MOUNT_POINT"/nihao.txt"; const char *file_nihao = MOUNT_POINT"/nihao.txt";
memset(data, 0, EXAMPLE_MAX_CHAR_SIZE); memset(data, 0, EXAMPLE_MAX_CHAR_SIZE);

View File

@ -1,3 +1,4 @@
CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED=y CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED=y
CONFIG_FATFS_VFS_FSTAT_BLKSIZE=4096 CONFIG_FATFS_VFS_FSTAT_BLKSIZE=4096
CONFIG_ESP_TASK_WDT_EN=n CONFIG_ESP_TASK_WDT_EN=n
CONFIG_EXAMPLE_FORMAT_SD_CARD=y

View File

@ -0,0 +1 @@
CONFIG_FATFS_VFS_FSTAT_BLKSIZE=4096