greiman / SdFat-beta

Beta SdFat for test of new features

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SD Card hot swap

SyXavier opened this issue · comments

Hi, Mr greiman. I use ESP32 to make an MP3 player. When implementing the SD card hot-swapping function, I found that the SD card cannot be read or written after being unplugged and then inserted. I suspect that SdFat sd needs to be reinitialized. If the begin function is called again, the MCU will get stuck. Is it possible to add an end function so that begin is called again to initialize sd.

An end function is not possible since SdFat has no internal record of open files. If you close all files you should be able to call sd.begin() again. You might also call SPI.end() before calling sd.begin().

The bench example allows cards to be removed/changed between tests. It closes the test file at the end of a test and calls sd.begin() before every test run.

Line 158 of bench:
if (!sd.begin(SD_CONFIG)) {

Line 275 of bench:
file.close();

All SD cards require initialization after cycling power. Any card with a file open for write may be corrupted if files are not closed before removing power.

Edit: The state of files is kept in the file class, not the sd class so you must close files by calling close with the file class instance that was used to open the file.