Skip to main content
Tim Russell
Associate II
August 29, 2018
Solved

What is the proper method to re-initialize SD card interface to support removal/reinsertion of SD card?

My SD card interface (using SDMMC1) stops working after the SD card has been removed/re-inserted.

I have tried un-mounting using f_mount(0, &SDPath, 0) and then re-mounting with no success.

As a test, I executed the following for each transaction:

f_mount(&SDFatFs, &SDPath, 0);

f_open(&SDFile,.....);

f_write(&SDFile, .....);

f_close(&SDFile);

f_mount(0, &SDPath, 0);

If I remove/re-insert the SD card after the 1st transaction, I get an FR_DISK_ERR error on the f_open() on the second transaction (following the reinsertion of the SD card). I have traced this error down into the HAL_SD_ReadBlocks_DMA() functions which gets a timeout on the call to SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE).

Any suggestions?

This topic has been closed for replies.
Tim Russell
Tim RussellBest answer
Associate II
January 25, 2019

Lctasca,

My SD code is functioning properly now.

The code I am using is as follows,

FATFS_UnLinkDriver(SDPath);

FATFS_LinkDriver(&SD_Driver, SDPath);

f_mount(&SDFatFs, &SDPath, 0);

f_open(&SDFile,.....);

f_write(&SDFile, .....);

--- if write error, go back to top to re-init ---

f_sync(&SDFile);

20 replies

Tim Russell
Tim RussellAuthorBest answer
Associate II
January 25, 2019

Lctasca,

My SD code is functioning properly now.

The code I am using is as follows,

FATFS_UnLinkDriver(SDPath);

FATFS_LinkDriver(&SD_Driver, SDPath);

f_mount(&SDFatFs, &SDPath, 0);

f_open(&SDFile,.....);

f_write(&SDFile, .....);

--- if write error, go back to top to re-init ---

f_sync(&SDFile);

iw2lsi
Associate III
February 8, 2019

Thanks Tim...

I was stuck on the same problem and your solution works for me.

Best Regards,

Giampaolo

dbgarasiya
Senior
October 10, 2019

just make changes configuration of sd card , problem definately in configuration of sd card

best of luck

TimeDigger
Associate
October 10, 2019

Hi dbgarasiya,

I would like to try it.

Do you have any pinpoint suggestions to start from :)

BB_it
Visitor II
January 9, 2021

I see this topic is quite old, but no good solution provided yet and this is most related thread on my search. For me solution which is not causing memory leak is:

to add HAL_SD_Init(&hsd) just before f_mount(&SDFatFs, &SDPath, 1). Otherwise I was able to mount SD only first time after card insertion and on all subsequent reinsertions receiving timeouts.

Lctasca
Visitor II
January 12, 2021

The code that worked for me at that time was:

extern SD_HandleTypeDef hsd;
HAL_SD_Init(&hsd);
HAL_SD_InitCard(&hsd);
 
if(f_mount(&SDFatFS, SDPath, 1) == FR_OK)
{
	if(f_open(&SDFile, file_pah, (FA_WRITE | FA_READ | FA_CREATE_ALWAYS)) == FR_OK)
	{ 
		// DO SOMETHING WITH THE FILE
	
		f_close(&SDFile);
		f_mount(NULL, SDPath, 1);
		HAL_SD_DeInit(&hsd);
	}
}

Unfortunately, I don't have the hardware anymore to test if it still works in the newest versions of STM Cube IDE