Skip to main content
Tim Russell
Associate II
Aug 29, 2018 4:19 PM UTC
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
Jan 25, 2019 2:17 PM UTC

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
Jan 25, 2019 2:17 PM UTC

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
Feb 8, 2019 8:30 AM UTC

Thanks Tim...

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

Best Regards,

Giampaolo

dbgarasiya
Senior
Oct 10, 2019 8:42 AM UTC

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

best of luck

TimeDigger
Associate
Oct 10, 2019 2:36 PM UTC

Hi dbgarasiya,

I would like to try it.

Do you have any pinpoint suggestions to start from :)

BB_it
Visitor II
Jan 9, 2021 8:53 AM UTC

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
Jan 12, 2021 6:57 PM UTC

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