Last active
March 2, 2025 12:19
-
-
Save dizcza/a35d8c1d09450369ed2f08f6803b5101 to your computer and use it in GitHub Desktop.
ESP32 Arduino format an SD card
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ff.h" | |
#include "vfs_fat_internal.h" | |
/** | |
* Usage: | |
* | |
* Arduino: | |
* SD.begin(); | |
* format_sdcard(); | |
* | |
* ESP-IDF: | |
* See https://github.com/espressif/esp-idf/blob/b63ec47238fd6aa6eaa59f7ad3942cbdff5fcc1f/examples/storage/sd_card/sdmmc/main/sd_card_example_main.c#L75 | |
* esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config, &mount_config, &card); | |
* format_sdcard(); | |
* | |
* Then proceed without remounting. | |
*/ | |
esp_err_t format_sdcard() { | |
char drv[3] = {'0', ':', 0}; | |
const size_t workbuf_size = 4096; | |
void* workbuf = NULL; | |
esp_err_t err = ESP_OK; | |
ESP_LOGW("sdcard", "Formatting the SD card"); | |
size_t allocation_unit_size = 16 * 1024; | |
int sector_size_default = 512; | |
workbuf = ff_memalloc(workbuf_size); | |
if (workbuf == NULL) { | |
return ESP_ERR_NO_MEM; | |
} | |
size_t alloc_unit_size = esp_vfs_fat_get_allocation_unit_size( | |
sector_size_default, | |
allocation_unit_size); | |
#if (ESP_IDF_VERSION_MAJOR < 5) | |
FRESULT res = f_mkfs(drv, FM_ANY, alloc_unit_size, workbuf, workbuf_size); | |
#else | |
const MKFS_PARM opt = {(BYTE)FM_ANY, 0, 0, 0, alloc_unit_size}; | |
FRESULT res = f_mkfs(drv, &opt, workbuf, workbuf_size); | |
#endif /* ESP_IDF_VERSION_MAJOR */ | |
if (res != FR_OK) { | |
err = ESP_FAIL; | |
ESP_LOGE("sdcard", "f_mkfs failed (%d)", res); | |
} | |
free(workbuf); | |
ESP_LOGI("sdcard", "Successfully formatted the SD card"); | |
return err; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just out of curiosity, I found a W25Q128 memory on the Dwin display (DMG80480Y070_02NN T5L0 IC). The data is saved in this memory in virtual partitions of 256kB, so it might be interesting for the ESP32 to program the data from the W25Q128 memory, which is SPI (if the data is not encrypted, which I believe it is not, so as not to reduce performance, I'm still going to see if I can check that).