Skip to main content
SSin
Associate II
August 3, 2020
Question

How to recover from FR_DISK_ERR during f_write in STM32

  • August 3, 2020
  • 2 replies
  • 2794 views

Hi all,

I am now using SYM32F103RE to read data from codec via I2S & write into SD card. I use the HAL library in STM32CubeMX w/ Chen's fatfs & I2S DMA. Unlucky, f_write will sometime return FR_DISK_ERR. According to fatfs, I can only f_close the file in this case. But it only return FR_INVAILD_OBJECT no matter what I do. If I add a 1ms polling delay (HAL_Delay(1)) between each f_write, it works fine. So is there any way to avoid or recover from the problem?

Here is my code:

FRESULT u8_sd_fat_status = FR_DISK_ERR; // 0 = SD card FAT file system is ok, otherwise 1.
uint16_t u32_array_codec_in[512];
 
f_mount(&SDFatFS, (TCHAR const*)SDPath, 0);
 
f_unlink("record.wav");
u8_sd_fat_status = f_open(&SDFile, "record.wav", FA_CREATE_NEW | FA_READ | FA_WRITE);
for(unsigned char i = 0; i < 10 && u8_sd_fat_status != FR_OK; i++)
{
 HAL_Delay(100);
 u8_sd_fat_status = f_open(&SDFile, "record.wav", FA_CREATE_NEW | FA_READ | FA_WRITE);
}
if(u8_sd_fat_status == FR_OK)
{
 uint16_t u16_array_mic_adc[512];
 uint32_t u32_rec_file_ptr = 0;
 uint32_t u32_i2s_ptr = 22;
 
 f_sync(&SDFile);
 
 HAL_I2S_Receive_DMA(&hi2s2, u32_array_codec_in, sizeof(u32_array_codec_in)/sizeof(uint16_t));
 
 while(u32_rec_file_ptr < 512000)
 {
 UINT u32_i = 0;
 
 u32_i2s_ptr += CodecRead(u16_array_mic_adc, sizeof(u16_array_mic_adc)/sizeof(uint16_t) - u32_i2s_ptr);
 
 if(u32_i2s_ptr >= sizeof(u16_array_mic_adc)/sizeof(uint16_t))
 {
 u8_sd_fat_status = f_write (&SDFile, u16_array_mic_adc, sizeof(u16_array_mic_adc), &u32_i);
 if(u8_sd_fat_status != FR_OK)
 {
 u8_sd_fat_status = f_close(&SDFile);
 if(u8_sd_fat_status != FR_OK)
 __asm("nop");
 u8_sd_fat_status = f_open(&SDFile, "record.wav", FA_OPEN_ALWAYS | FA_READ | FA_WRITE);
 if(u8_sd_fat_status != FR_OK)
 __asm("nop");
 u8_sd_fat_status = f_lseek (&SDFile, u32_rec_file_ptr);
 if(u8_sd_fat_status != FR_OK)
 __asm("nop");
 break;
 }
 f_sync(&SDFile);
 }
 u32_rec_file_ptr += u32_i;
 u32_i2s_ptr = 0;
 }
 }
 f_close(&SDFile);
}

Thanks,

Sin 

This topic has been closed for replies.

2 replies

Tesla DeLorean
Guru
August 3, 2020

Sounds like the DISKIO layer isn't waiting on the card to complete the write.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
SSin
SSinAuthor
Associate II
August 3, 2020

Thanks for your reply. But it sounds that I need to go into fatfs driver level. It's not a good news

Just have a look with debugger. The code freeze in disk_write @ ff.c (line 2823). It returns RES_ERROR. But it seems no more detail can be provided

Seems that the data write beyond SD sector block boundary. But I write 1024 bytes each time, which shall not be the case. & this should not be solve by adding delay

ARSL.1
Associate
July 17, 2021

Hi SSin

I have the same problem as you... when I try to F_Write my Wave data to SDcard it gives me FR_DISK_ERR some times .... Do you found the solution?

Regards

Associate III
November 11, 2024

Hello,

I am facing similar error in my application. After few hundreds of successful writes, the FATFS gives me FR_DISK_ERR for f_mount itself. Did you find any solution?