Skip to content

Instantly share code, notes, and snippets.

@dizcza
Last active June 28, 2024 19:33
Show Gist options
  • Save dizcza/a35d8c1d09450369ed2f08f6803b5101 to your computer and use it in GitHub Desktop.
Save dizcza/a35d8c1d09450369ed2f08f6803b5101 to your computer and use it in GitHub Desktop.
ESP32 Arduino format an SD card
#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;
}
@rtek1000
Copy link

It does format the card to FAT32 4096 (see workbuf_size).

Thank you!

@rtek1000
Copy link

That sounds very interesting. The Dwin display is updated via SD card.

And I had already done a test with ESP8266 on a webserver and FTP to manipulate files on the SD card, and the idea was to use the SD card in the DFPlayer Mini.

But now there is the possibility of updating the Dwin display using the ESP32.

The card must be formatted in FAT32 and 4096. Is it possible to format in FAT32?

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment