SDMMC Best Practices for Continual Writing
Posted: Mon Aug 18, 2025 3:50 am
I have a setup where I will continually write data to the SD card whenever there is an SD card inserted (187.5 KB/sec written in chunks of ~9600 every 50 milliseconds). The SD card can be removed at any point and reinserted later. The data between then is unimportant, and losing a few chunks when removed/inserted is not important either. But, we want to make sure that we don't damage the SD card itself or do anything to corrupt the filesystem making everything inaccessble.
I know that to get going for an SD card the following must be done:
1. `sd_pwr_ctrl_new_on_chip_ldo()`
2. `sdmmc_host_init()`
3. `sdmmc_host_init_slot()`
4. `esp_vfs_fat_register_cfg()`
5. `sdmmc_card_init()`
6. `ff_diskio_register_sdmmc()`
7. `f_mount()`
However, it seems that #1-3 definitely can happen any time. Do they have to be re-done if a card is removed and reinserted? What are the consequences of leaving the LDO, host, and slot setup? (More power drain? How much? Frying a card that is inserted?)
Additionally, #4 can also happen early and only once with no consequences. Is this true? Can the resulting FATFS object be shared between mounts (is it reset when re-mounted to a possibly different SD card)?
I know that 5-7 have to be done with the card inserted.
While I am writing data in chunks, can I keep a `FILE*` open the entire time, or should I close and re-open it every 50 ms? If I don't close it, I will at least `fflush()` it after each chunk written. If I keep it open, should I still call `fclose()` after I know the card is gone?
What happens if I don't call `f_mount(NULL, drv, 0);` to unmount? Is there anything else that must be done to unmount (e.g. do I need to use `esp_vfs_fat_unregister_path()` or can I leave it perpetually registered?)? Can/should I unmount after the card is removed? Or do I just mount again when I see another card?
Once I know a card is ejected, can I poll `sdmmc_get_status()` with the same card object (for a card that isn't there anymore) to see when another card is inserted? Or is there a better way to check for an inserted card (I do not have access to a CD pin)?
I know that to get going for an SD card the following must be done:
1. `sd_pwr_ctrl_new_on_chip_ldo()`
2. `sdmmc_host_init()`
3. `sdmmc_host_init_slot()`
4. `esp_vfs_fat_register_cfg()`
5. `sdmmc_card_init()`
6. `ff_diskio_register_sdmmc()`
7. `f_mount()`
However, it seems that #1-3 definitely can happen any time. Do they have to be re-done if a card is removed and reinserted? What are the consequences of leaving the LDO, host, and slot setup? (More power drain? How much? Frying a card that is inserted?)
Additionally, #4 can also happen early and only once with no consequences. Is this true? Can the resulting FATFS object be shared between mounts (is it reset when re-mounted to a possibly different SD card)?
I know that 5-7 have to be done with the card inserted.
While I am writing data in chunks, can I keep a `FILE*` open the entire time, or should I close and re-open it every 50 ms? If I don't close it, I will at least `fflush()` it after each chunk written. If I keep it open, should I still call `fclose()` after I know the card is gone?
What happens if I don't call `f_mount(NULL, drv, 0);` to unmount? Is there anything else that must be done to unmount (e.g. do I need to use `esp_vfs_fat_unregister_path()` or can I leave it perpetually registered?)? Can/should I unmount after the card is removed? Or do I just mount again when I see another card?
Once I know a card is ejected, can I poll `sdmmc_get_status()` with the same card object (for a card that isn't there anymore) to see when another card is inserted? Or is there a better way to check for an inserted card (I do not have access to a CD pin)?