I’m trying to add more error handling to my project. Right now I’m looking at the SDMMC interface.
I’d like to recover from the case when the SD card is removed while mounted. I’m trying to detect this state by querying the free space on the card. If that operation fails, then I assume the card has been removed.
At that point I try to reinitialize the SDMMC interface and FatFS. However, I’m not able to access the SD card after attempting to reinitialize. I get an FR_NOT_READY error.
This is my initialization code:
FatFileSystem::FatFileSystem()
{
m_curPath[0] = '\0';
m_available = false;
}
bool FatFileSystem::initialize()
{
// Initialize the SDMMC interface and the FAT file system
m_sdConfig.Defaults();
m_sdConfig.speed = daisy::SdmmcHandler::Speed::FAST;
m_sdHandler.Init(m_sdConfig);
m_fsi.Init(daisy::FatFSInterface::Config::MEDIA_SD);
// Try to mount the SD card
strcpy(m_curPath, m_fsi.GetSDPath());
strcpy(m_rootPath, m_fsi.GetSDPath());
FRESULT res = f_mount(&m_fsi.GetSDFileSystem(), m_curPath, 1);
infoPrint("f_mount( %s ), result: %s\r\n", m_curPath, getFatFSErrorCode(res));
m_available = (res == FR_OK);
return m_available;
}
void FatFileSystem::deinitialize()
{
m_fsi.DeInit();
new (this) FatFileSystem;
}
If I perform the de-init/re-init sequence without removing the SD card then things continue to work properly. But removing the SD card and then attempting to access it puts something in a bad state that I’m not sure how to recover from.
I think using the CD pin on your SDMMC inteface could be a simpler solution here.
The pin needs to be pulled up but you can initialize the GPIO to do so. Then, when the SD card is not inserted, the CD pin will be a high level and vice versa.
Thanks, Takumi. I’m using the same circuit from the Daisy Patch, with the 47K pull-up resistors on the data lines. Is it possible to use the CD pin while also using the 4-bit interface? I tried reading the pin, but it always reads HIGH, even when the card is not inserted.
Thanks. I had seen that thread and many others like it. It looks like I will need to completely re-initialized the SDMMC for it to work after it gets into the bad state.
Apparently the low-level initialization is handled by the bootloader. Is the source for the bootloader available so I can see what needs to be done?
From my understanding, we still want to polish it more. And there seems to be other steps/processes involved before it can be shared.
We hope to update you all on this soon! I appreciate your waiting.