cancel
Showing results for 
Search instead for 
Did you mean: 

How to re-Mount SD-Card after pulling out SD-Card.

spark.72
Associate

I can't re-mount SD-Card after pulling out and inserting SD-Card again.

It shows "FR_DISK_ERR" when error occurs.

Please let me know if there is anything I have to follow.

I have CubeMX ver 4.22, FreeRTOS and HAL Driver.

==============================================================

void MountSDIO(void)

{

 res = f_mount(&SDFatFs, SD_Path, 1);

 if(res != FR_OK)  

  DisplayError();  

}

void UnMountSDIO(void)

{

 res = f_mount(NULL, "", 0);  

 if(res != FR_OK)  

  DisplayError(); 

}

void OpenFile()

{

 res = f_open(&MyFile, "123.txt", FA_OPEN_ALWAYS|FA_WRITE|FA_READ);

 if(res != FR_OK)

 {

  UnMountSDIO();

osDelay(10);

ResetSDIO();

SDFatFs = SDNull;

osDelay(100);

    //MX_FATFS_Init();

MountSDIO();

  DisplayError();  

 }

}

void CloseFile()

{

 res = f_close(&MyFile);

 if(res != FR_OK)

  DisplayError();

}

void ReadFile(void)

 res = f_read(&MyFile, text, sizeof(text), (void*)&bytesRead);

 if(res != FR_OK || bytesRead == 0)

  DisplayError();   

}

void WriteFile(void)

{   

 //Increase Number

 int16_t number = atoi((char const*)text);

 number++;

 memset(text, 0, sizeof(text));

 sprintf((char *)text, "%d", number);       

  

 //Move cursor

 res = f_lseek(&MyFile, 0);

 if(res != FR_OK)

  DisplayError();

  

 //Write Number

 int16_t len = strlen((char const *)text);

 res = f_write(&MyFile, text, len, (void*)&bytesWritten);

 if(res != FR_OK || bytesWritten == 0)

  DisplayError();

}

/* USER CODE END 4 */

/* StartMainTask function */

void StartMainTask(void const * argument)

{

 /* init code for FATFS */

 MX_FATFS_Init();

 /* USER CODE BEGIN 5 */

 /* Infinite loop */

 for(;;)

 {

  OpenFile();

  ReadFile();

  WriteFile();

  CloseFile();

   

  uint16_t len = (uint16_t)strlen(text);

  text[len] = '\r';

  text[len+1] = '\n';

   

  HAL_UART_Transmit(&huart1, text, len+2, 50);    

  DisplayOK();

  osDelay(500);

 }

 /* USER CODE END 5 */ 

}

0 REPLIES 0