cancel
Showing results for 
Search instead for 
Did you mean: 

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

Tim Russell
Associate III

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?

20 REPLIES 20
Lctasca
Associate II

Hello

Have the same problem here, managed to solve the issue Tim?

If you remove power from the card, whether it is interfaced via SPI or SDIO, you're going to want to reinitialize thing to ensure you are talking to the same card, or that it is configured for optimal rates.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Tim Russell
Associate III

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);

Thanks Tim...

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

Best Regards,

Giampaolo

Many thanks Tim

Same problem with me (with SPI communication) and your solution work great

:grinning_face:

Lionel

With +FreeRTOS, the same solution way is usefull to recover only from unplug/plug SDCard if things are ok. In order to recover from any error that occurs during access to SDCard I had add HAL_SD_DeInit(&hsd) after the line FATFS_UnLinkDriver(SDPath).

Also after a number of mounting-unmounting cycle (the number depends on the configTOTAL_HEAP_SIZE with heap_4.c), causes stack overflow!

:face_without_mouth:

Selcuk

dbgarasiya
Senior II

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

best of luck

Hi dbgarasiya,

I would like to try it.

Do you have any pinpoint suggestions to start from :)

BB
Associate

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
Associate II

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