sd card unmounting and remounting not working

bob.mozart
Posts: 10
Joined: Wed Jun 18, 2025 8:29 pm

sd card unmounting and remounting not working

Postby bob.mozart » Wed Jul 02, 2025 9:22 pm

Hello everyone,

I've decided to create a webserver that lets you wirelessly upload files to an sd card connected to the ESP board. I need to have a mechanism where the sd card's lines are switched to another device briefly, meaning it won't be connected to the ESP board. I'm testing the code right now, and it works only for the first upload. If I upload another file, the sd card isn't mounted properly.

Here are the logs for that:

Code: Select all

Website File Uploader Running...

I (2911) espressif: Starting ESP-NOW responder...
I (2911) ESPNOW: espnow [version: 2.0] init
I (2921) main_task: Returned from app_main()
I (7091) wifi:<ba-add>idx:1 (ifx:0, 94:2a:6f:a6:7c:cb), tid:6, ssn:2, winSize:64
I (8391) espressif: Uploading file: cat.png
I (9391) gpio: GPIO[2]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (9391) gpio: GPIO[35]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (9391) gpio: GPIO[37]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (9401) espressif: Initializing SD card
I (9401) espressif: Using SDMMC peripheral
I (9411) espressif: Mounting filesystem
I (9411) gpio: GPIO[2]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (9421) gpio: GPIO[35]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (9431) gpio: GPIO[37]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (9531) espressif: Filesystem mounted
Name: USDU1
Type: SDHC
Speed: 40.00 MHz (limit: 40.00 MHz)
Size: 60290MB
CSD: ver=2, sector_size=512, capacity=123473920 read_bl_len=9
SSR: bus_width=1
I (11211) espressif: Finished copying to SD File
I (11211) gpio: GPIO[7]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (11211) gpio: GPIO[2]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (11221) gpio: GPIO[35]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (11231) gpio: GPIO[37]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (11241) espressif: Card unmounted
I (16241) espressif: Uploading file: cat.png
I (17241) gpio: GPIO[2]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (17241) gpio: GPIO[35]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (17241) gpio: GPIO[37]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (17251) espressif: Initializing SD card
I (17251) espressif: Using SDMMC peripheral
I (17261) espressif: Mounting filesystem
I (17261) gpio: GPIO[2]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (17271) gpio: GPIO[35]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (17281) gpio: GPIO[37]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
E (17291) vfs_fat_sdmmc: sdmmc_card_init failed (0x105).
I (17291) gpio: GPIO[7]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (17301) gpio: GPIO[2]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (17311) gpio: GPIO[35]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (17321) gpio: GPIO[37]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
E (17331) espressif: Failed to initialize the card (ESP_ERR_NOT_FOUND). Make sure SD card lines have pull-up resistors in place.
I (17341) SD_TEST: Testing SD pin connections and pullup strength
I (17341) gpio: GPIO[2]| InputEn: 1| OutputEn: 1| OpenDrain: 1| Pullup: 0| Pulldown: 0| Intr:0 
I (17351) gpio: GPIO[35]| InputEn: 1| OutputEn: 1| OpenDrain: 1| Pullup: 0| Pulldown: 0| Intr:0
I (17361) gpio: GPIO[37]| InputEn: 1| OutputEn: 1| OpenDrain: 1| Pullup: 0| Pulldown: 0| Intr:0 

**** PIN recovery time ****

PIN  2 CLK  43 cycles
PIN 35 CMD  45 cycles
PIN 37  D0  44 cycles

**** PIN recovery time with weak pullup ****

PIN  2 CLK  43 cycles
PIN 35 CMD  44 cycles
PIN 37  D0  45 cycles
E (17391) espressif: Failed to open /sdcard/cat.png for writing
The sd card module I'm using has pull-ups already, so I know that is not the problem.


Any help would be appreciated! Attached is my code.
Attachments
hello_world_main.c
(22.62 KiB) Downloaded 29 times

RathiSonika
Espressif staff
Espressif staff
Posts: 30
Joined: Thu Jun 22, 2023 2:58 pm

Re: sd card unmounting and remounting not working

Postby RathiSonika » Thu Jul 03, 2025 8:53 am

Hello, from your question, what I understand is that your setup includes a mechanism to temporarily switch the SD card lines to another device, effectively disconnecting it from the ESP32. If that’s correct, then the behavior you're observing is expected. When the SD card is mounted using esp_vfs_fat_sdmmc_mount(), ESP-IDF initialises internal driver structures and assumes that the card will remain powered, electrically connected and accessed by the ESP32. If the SD lines are physically disconnected such as when switched to another microcontroller or peripheral, the driver's internal state becomes invalid. As a result, any attempt to access or remount the SD card without properly reinitialising the host (SDMMC or SPI) can fail.

To handle this properly, you should first unmount the SD card using esp_vfs_fat_sdcard_unmount(), and then explicitly deinitialise the SDMMC host by calling sdmmc_host_deinit(). This can be done in your deinit_sd_card() function before switching the lines. When reconnecting the SD card to the ESP32, esp_vfs_fat_sdmmc_mount() function will reinitialise the host and remount the card.
I hope this helps.

bob.mozart
Posts: 10
Joined: Wed Jun 18, 2025 8:29 pm

Re: sd card unmounting and remounting not working

Postby bob.mozart » Thu Jul 03, 2025 2:48 pm

Hello, from your question, what I understand is that your setup includes a mechanism to temporarily switch the SD card lines to another device, effectively disconnecting it from the ESP32. If that’s correct, then the behavior you're observing is expected. When the SD card is mounted using esp_vfs_fat_sdmmc_mount(), ESP-IDF initialises internal driver structures and assumes that the card will remain powered, electrically connected and accessed by the ESP32. If the SD lines are physically disconnected such as when switched to another microcontroller or peripheral, the driver's internal state becomes invalid. As a result, any attempt to access or remount the SD card without properly reinitialising the host (SDMMC or SPI) can fail.

To handle this properly, you should first unmount the SD card using esp_vfs_fat_sdcard_unmount(), and then explicitly deinitialise the SDMMC host by calling sdmmc_host_deinit(). This can be done in your deinit_sd_card() function before switching the lines. When reconnecting the SD card to the ESP32, esp_vfs_fat_sdmmc_mount() function will reinitialise the host and remount the card.
I hope this helps.

Hello. I added the line

Code: Select all

sdmmc_host_deinit()
after

Code: Select all

card=NULL
in the code from before. It still gives me the same errors however:

Code: Select all

Website File Uploader Running...

I (5865) espressif: Starting ESP-NOW responder...
I (5865) ESPNOW: espnow [version: 2.0] init
I (5865) main_task: Returned from app_main()
I (8485) espressif: Uploading file: cat.png
I (9485) espressif: Initializing SD card
I (9485) espressif: Using SDMMC peripheral
I (9485) espressif: Mounting filesystem
I (9485) gpio: GPIO[2]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (9485) gpio: GPIO[35]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (9495) gpio: GPIO[37]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (9605) espressif: Filesystem mounted
Name: USDU1
Type: SDHC
Speed: 40.00 MHz (limit: 40.00 MHz)
Size: 60290MB
CSD: ver=2, sector_size=512, capacity=123473920 read_bl_len=9
SSR: bus_width=1
I (9785) espressif: Finished copying to SD File
I (9785) gpio: GPIO[7]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (9795) gpio: GPIO[2]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (9795) gpio: GPIO[35]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (9805) gpio: GPIO[37]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (9815) espressif: Card unmounted
I (9815) espressif: Card deinitiallized
I (12535) espressif: Uploading file: cat.png
I (13535) espressif: Initializing SD card
I (13535) espressif: Using SDMMC peripheral
I (13535) espressif: Mounting filesystem
I (13535) gpio: GPIO[2]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (13535) gpio: GPIO[35]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (13545) gpio: GPIO[37]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
E (13555) vfs_fat_sdmmc: sdmmc_card_init failed (0x105).
I (13565) gpio: GPIO[7]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (13565) gpio: GPIO[2]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (13575) gpio: GPIO[35]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (13585) gpio: GPIO[37]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
E (13595) espressif: Failed to initialize the card (ESP_ERR_NOT_FOUND). Make sure SD card lines have pull-up resistors in place.
I (13605) SD_TEST: Testing SD pin connections and pullup strength
I (13615) gpio: GPIO[2]| InputEn: 1| OutputEn: 1| OpenDrain: 1| Pullup: 0| Pulldown: 0| Intr:0 
I (13625) gpio: GPIO[35]| InputEn: 1| OutputEn: 1| OpenDrain: 1| Pullup: 0| Pulldown: 0| Intr:0 
I (13625) gpio: GPIO[37]| InputEn: 1| OutputEn: 1| OpenDrain: 1| Pullup: 0| Pulldown: 0| Intr:0

**** PIN recovery time ****

PIN  2 CLK  261 cycles
PIN 35 CMD  45 cycles
PIN 37  D0  45 cycles

**** PIN recovery time with weak pullup ****

PIN  2 CLK  43 cycles
PIN 35 CMD  44 cycles
PIN 37  D0  45 cycles
E (13655) espressif: Failed to open /sdcard/cat.png for writing
W (13665) httpd_uri: httpd_uri: uri handler execution failed
I (13685) espressif: Uploading file: cat.png
I (14685) espressif: Initializing SD card
I (14685) espressif: Using SDMMC peripheral
I (14685) espressif: Mounting filesystem
I (14685) gpio: GPIO[2]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (14685) gpio: GPIO[35]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (14695) gpio: GPIO[37]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
E (14705) vfs_fat_sdmmc: sdmmc_card_init failed (0x105).
I (14715) gpio: GPIO[7]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (14715) gpio: GPIO[2]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (14725) gpio: GPIO[35]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (14735) gpio: GPIO[37]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
E (14745) espressif: Failed to initialize the card (ESP_ERR_NOT_FOUND). Make sure SD card lines have pull-up resistors in place.
I (14755) SD_TEST: Testing SD pin connections and pullup strength
I (14765) gpio: GPIO[2]| InputEn: 1| OutputEn: 1| OpenDrain: 1| Pullup: 0| Pulldown: 0| Intr:0
I (14775) gpio: GPIO[35]| InputEn: 1| OutputEn: 1| OpenDrain: 1| Pullup: 0| Pulldown: 0| Intr:0
I (14775) gpio: GPIO[37]| InputEn: 1| OutputEn: 1| OpenDrain: 1| Pullup: 0| Pulldown: 0| Intr:0

**** PIN recovery time ****

PIN  2 CLK  261 cycles
PIN 35 CMD  45 cycles
PIN 37  D0  45 cycles

**** PIN recovery time with weak pullup ****

PIN  2 CLK  43 cycles
PIN 35 CMD  44 cycles
PIN 37  D0  45 cycles
E (14805) espressif: Failed to open /sdcard/cat.png for writing
The

Code: Select all

 Card deinitiallized
log is from a ESP_LOGI I added right after sdmmc_host_deinit(). Not sure what else to do now. :/ any ideas?

RathiSonika
Espressif staff
Espressif staff
Posts: 30
Joined: Thu Jun 22, 2023 2:58 pm

Re: sd card unmounting and remounting not working

Postby RathiSonika » Fri Jul 04, 2025 5:11 pm

Hi, My mistake — there's no need to explicitly deinitialize SDMMC, as it's already handled internally by the esp_vfs_fat_sdcard_unmount() function. I tested the example https://github.com/espressif/esp-idf/tr ... card/sdmmc by mounting and remounting the SD card in a loop, physically removing and reinserting the card each time. It worked well overall, but I noticed that if there's a liitle more delay in reinserting the card, an error is returned — which is expected. So, my suggestion would be to try adding a bit more delay between when your CD pin detects the card and when you start initializing it.

bob.mozart
Posts: 10
Joined: Wed Jun 18, 2025 8:29 pm

Re: sd card unmounting and remounting not working

Postby bob.mozart » Mon Jul 07, 2025 2:27 pm

Hi, My mistake — there's no need to explicitly deinitialize SDMMC, as it's already handled internally by the esp_vfs_fat_sdcard_unmount() function. I tested the example https://github.com/espressif/esp-idf/tr ... card/sdmmc by mounting and remounting the SD card in a loop, physically removing and reinserting the card each time. It worked well overall, but I noticed that if there's a liitle more delay in reinserting the card, an error is returned — which is expected. So, my suggestion would be to try adding a bit more delay between when your CD pin detects the card and when you start initializing it.
Hello,

The delay wasn't an issue. I tested the looping with the SDMMC example like you did and it worked! I must've copied over the init_sd_card function over wrong, because when I re-copied it from the SDMMC example, it remounts correctly. Thanks!

Who is online

Users browsing this forum: ChatGPT-User and 1 guest