From e2da3a4259768dd4d115ccd74541c6409a932688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20M=C3=BAdry?= Date: Wed, 7 Feb 2024 13:40:40 +0100 Subject: [PATCH] fix(examples): Disable formatting SD cards in storage examples by default --- examples/storage/sd_card/sdmmc/README.md | 11 +++++++---- examples/storage/sd_card/sdmmc/main/Kconfig.projbuild | 6 ++++++ .../storage/sd_card/sdmmc/main/sd_card_example_main.c | 2 ++ examples/storage/sd_card/sdmmc/sdkconfig.ci | 1 + examples/storage/sd_card/sdspi/README.md | 11 +++++++---- examples/storage/sd_card/sdspi/main/Kconfig.projbuild | 6 ++++++ .../storage/sd_card/sdspi/main/sd_card_example_main.c | 2 ++ examples/storage/sd_card/sdspi/sdkconfig.ci | 1 + examples/storage/sd_card/sdspi/sdkconfig.defaults | 1 + 9 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 examples/storage/sd_card/sdspi/sdkconfig.defaults diff --git a/examples/storage/sd_card/sdmmc/README.md b/examples/storage/sd_card/sdmmc/README.md index 8c4c8ad7a3..3e5006020b 100644 --- a/examples/storage/sd_card/sdmmc/README.md +++ b/examples/storage/sd_card/sdmmc/README.md @@ -5,6 +5,8 @@ (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: 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, - 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. -2. 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`. -4. 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. Print information about the card, such as name, type, capacity, and maximum supported frequency. +1. Create a file using `fopen` and write to it using `fprintf`. +1. Rename the file. Before renaming, check if destination file already exists using `stat` function, and remove it using `unlink` function. +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. diff --git a/examples/storage/sd_card/sdmmc/main/Kconfig.projbuild b/examples/storage/sd_card/sdmmc/main/Kconfig.projbuild index a5ce9e41b0..61a5de0a33 100644 --- a/examples/storage/sd_card/sdmmc/main/Kconfig.projbuild +++ b/examples/storage/sd_card/sdmmc/main/Kconfig.projbuild @@ -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 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 prompt "SD/MMC bus width" default EXAMPLE_SDMMC_BUS_WIDTH_4 diff --git a/examples/storage/sd_card/sdmmc/main/sd_card_example_main.c b/examples/storage/sd_card/sdmmc/main/sd_card_example_main.c index 9129353d38..46c863d137 100644 --- a/examples/storage/sd_card/sdmmc/main/sd_card_example_main.c +++ b/examples/storage/sd_card/sdmmc/main/sd_card_example_main.c @@ -208,6 +208,7 @@ void app_main(void) } // Format FATFS +#ifdef CONFIG_EXAMPLE_FORMAT_SD_CARD ret = esp_vfs_fat_sdcard_format(mount_point, card); if (ret != ESP_OK) { ESP_LOGE(TAG, "Failed to format FATFS (%s)", esp_err_to_name(ret)); @@ -220,6 +221,7 @@ void app_main(void) } else { ESP_LOGI(TAG, "file doesnt exist, format done"); } +#endif // CONFIG_EXAMPLE_FORMAT_SD_CARD const char *file_nihao = MOUNT_POINT"/nihao.txt"; memset(data, 0, EXAMPLE_MAX_CHAR_SIZE); diff --git a/examples/storage/sd_card/sdmmc/sdkconfig.ci b/examples/storage/sd_card/sdmmc/sdkconfig.ci index 9bab0e01a2..c70f68e892 100644 --- a/examples/storage/sd_card/sdmmc/sdkconfig.ci +++ b/examples/storage/sd_card/sdmmc/sdkconfig.ci @@ -1,3 +1,4 @@ CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED=y CONFIG_FATFS_VFS_FSTAT_BLKSIZE=4096 CONFIG_ESP_TASK_WDT_EN=n +CONFIG_EXAMPLE_FORMAT_SD_CARD=y diff --git a/examples/storage/sd_card/sdspi/README.md b/examples/storage/sd_card/sdspi/README.md index eb51c1ba08..4e52527838 100644 --- a/examples/storage/sd_card/sdspi/README.md +++ b/examples/storage/sd_card/sdspi/README.md @@ -5,6 +5,8 @@ (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: 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), - 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. -2. 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`. -4. 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. Print information about the card, such as name, type, capacity, and maximum supported frequency. +1. Create a file using `fopen` and write to it using `fprintf`. +1. Rename the file. Before renaming, check if destination file already exists using `stat` function, and remove it using `unlink` function. +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. diff --git a/examples/storage/sd_card/sdspi/main/Kconfig.projbuild b/examples/storage/sd_card/sdspi/main/Kconfig.projbuild index 777fc1e8c2..64f1c561e0 100644 --- a/examples/storage/sd_card/sdspi/main/Kconfig.projbuild +++ b/examples/storage/sd_card/sdspi/main/Kconfig.projbuild @@ -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 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 int "MOSI GPIO number" default 15 if IDF_TARGET_ESP32 diff --git a/examples/storage/sd_card/sdspi/main/sd_card_example_main.c b/examples/storage/sd_card/sdspi/main/sd_card_example_main.c index fd8f6953c6..d5edc7503e 100644 --- a/examples/storage/sd_card/sdspi/main/sd_card_example_main.c +++ b/examples/storage/sd_card/sdspi/main/sd_card_example_main.c @@ -194,6 +194,7 @@ void app_main(void) } // Format FATFS +#ifdef CONFIG_EXAMPLE_FORMAT_SD_CARD ret = esp_vfs_fat_sdcard_format(mount_point, card); if (ret != ESP_OK) { ESP_LOGE(TAG, "Failed to format FATFS (%s)", esp_err_to_name(ret)); @@ -206,6 +207,7 @@ void app_main(void) } else { ESP_LOGI(TAG, "file doesnt exist, format done"); } +#endif // CONFIG_EXAMPLE_FORMAT_SD_CARD const char *file_nihao = MOUNT_POINT"/nihao.txt"; memset(data, 0, EXAMPLE_MAX_CHAR_SIZE); diff --git a/examples/storage/sd_card/sdspi/sdkconfig.ci b/examples/storage/sd_card/sdspi/sdkconfig.ci index 9bab0e01a2..c70f68e892 100644 --- a/examples/storage/sd_card/sdspi/sdkconfig.ci +++ b/examples/storage/sd_card/sdspi/sdkconfig.ci @@ -1,3 +1,4 @@ CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED=y CONFIG_FATFS_VFS_FSTAT_BLKSIZE=4096 CONFIG_ESP_TASK_WDT_EN=n +CONFIG_EXAMPLE_FORMAT_SD_CARD=y diff --git a/examples/storage/sd_card/sdspi/sdkconfig.defaults b/examples/storage/sd_card/sdspi/sdkconfig.defaults new file mode 100644 index 0000000000..88020f762a --- /dev/null +++ b/examples/storage/sd_card/sdspi/sdkconfig.defaults @@ -0,0 +1 @@ +CONFIG_FATFS_VFS_FSTAT_BLKSIZE=4096