Skip to main content
SPesc.1
Associate II
March 9, 2023
Solved

STM32 Can't remount SD after replacing SD card

  • March 9, 2023
  • 3 replies
  • 3339 views

Dear community,

I'm still facing an issue with my SD card slot on my STM32F410RB Nucleo Board while using the CUBE IDE. After resetting the microcontroller, I'm able to mount the SD card successfully again, but as soon as I remove the card, I'm not able to mount it anymore.

When trying to mount the card after removing it, I'm getting an error message of FR_NOT_READY from the f_mount function. I've tried various things to fix the issue, such as testing the SD card in different slots and trying different pull-up and pull-down configurations, but unfortunately, nothing has worked so far.

Since I'm using the CUBE IDE, I was wondering if anyone in the community has had a similar experience or has any tips on how to fix this problem within the IDE. Any help would be greatly appreciated.

void process_SD_card( void )

{

if (SdCardConnected == 1)

{

printf("SD Card found\r\n");

FATFS    FatFs;      //Fatfs handle

 FIL     fil;         //File handle

 FRESULT   fres;         //Result after operations

 char    buf[100];

 do

 {

  //Mount the SD Card

  fres = f_mount(&FatFs, "", 1);  //1=mount now

  if (fres != FR_OK)

  {

   //MX_FATFS_DeInit();

   printf("No SD Card found : (%i)\r\n", fres);

   break;

  }

  printf("SD Card Mounted Successfully!!!\r\n");

  //Read the SD Card Total size and Free Size

  FATFS *pfs;

  DWORD fre_clust;

  uint32_t totalSpace, freeSpace;

  f_getfree("", &fre_clust, &pfs);

  totalSpace = (uint32_t)((pfs->n_fatent - 2) * pfs->csize * 0.5);

  freeSpace = (uint32_t)(fre_clust * pfs->csize * 0.5);

  printf("TotalSpace : %lu bytes, FreeSpace = %lu bytes\n", totalSpace, freeSpace);

  //Open the file

  fres = f_open(&fil, "EmbeTronicX.txt", FA_WRITE | FA_READ | FA_CREATE_ALWAYS);

  if(fres != FR_OK)

  {

   printf("File creation/open Error : (%i)\r\n", fres);

   break;

  }

  printf("Writing data!!!\r\n");

  //write the data

  f_puts("Welcome to EmbeTronicX", &fil);

  //close your file

  f_close(&fil);

  //Open the file

  fres = f_open(&fil, "EmbeTronicX.txt", FA_READ);

  if(fres != FR_OK)

  {

   printf("File opening Error : (%i)\r\n", fres);

   break;

  }

  //read the data

  f_gets(buf, sizeof(buf), &fil);

  printf("Read Data : %s\n", buf);

  //close your file

  f_close(&fil);

  printf("Closing File!!!\r\n");

#if 0

  //Delete the file.

  fres = f_unlink(EmbeTronicX.txt);

  if (fres != FR_OK)

  {

   printf("Cannot able to delete the file\n");

  }

#endif

 } while( false );

 //We're done, so de-mount the drive

 fres = f_mount(0, "", 0);

 printf("SD Card Unmounted Successfully!!!\r\n");

}else printf("No SD Card found \r\n");

}

This topic has been closed for replies.
Best answer by FBL

Hello @SPesc.1​ 

This could be due to a miss communication between the microcontroller and the SD card, or it could be because the SD card is not responding correctly.

According to your code, it would be helpful to check whether the SD Card is still mounted before enabling SD Card interface.

3 replies

alister
Senior III
March 9, 2023

Search the community, e.g. Does anything in https://community.st.com/s/question/0D53W000011w9RBSAY/sdio-microsd-card-fmount-returns-frnotready help?

Otherwise, the code would be layered:

  • app
  • FatFs
  • diskio
  • drivers.

Checking options and clicking generate in cube should not be expected to produce a complete, reliable and quality-featured app. All the code is your responsibility. Reorganizing, improving and/or fixing problems in cube-generated code is normal.

FatFs is third-party code. That's unlikely to need to changes other than configuration.

Pay attention the diskio layer.

Read the source code, step it in a debugger and/or instrument it.

FBLBest answer
ST Technical Moderator
March 14, 2023

Hello @SPesc.1​ 

This could be due to a miss communication between the microcontroller and the SD card, or it could be because the SD card is not responding correctly.

According to your code, it would be helpful to check whether the SD Card is still mounted before enabling SD Card interface.

To give better visibility on the answered topics, please click on "Best answer" on the reply which solved your issue or answered your question.Best regards,FBL
AScha.3
Super User
March 14, 2023

maybe...did you:

fresult = HAL_SD_DeInit(&hsd1);  

when removing ?

and

fresult = HAL_SD_InitCard(&hsd1);  

+

fresult = f_mount(&SDfs, (TCHAR const*)SDPath, 1);

when new card in ?

If you feel a post has answered your question, please click on " Best Answer ".