I have a problem in FatFS f_write gives FR_DISK_ERR & FR_INVALID_OBJECT
Hello,
I'm sorry if this is the wrong section
I'm using STM32CubeMX on custom STM32F407VG
I'm using 1-Bit mode SD card with FatFS middleware (4-Bit didn't work at all)
I'm writing 500-1000 bytes to a txt file every ~30ms
Once the file size is > 2MB I close it and open another txt file
It works fine for few minutes and creates multiple files as well, but sometimes the f_write fails giving a FR_DISK_ERR, when I try to close it it'll give the same error
When I try to write or close again after the previous error it'll give FR_INVALID_OBJECT
Once I see this error only a HW reset of the MCU will solve it
I tried deinit/init of the HAL_SD or FatFS but nothing worked
Attached the code I'm using, this function is currently called every ~100ms with these problems, but the goal is to call it every ~30ms
void PIR_to_SD(char myData[]){
uint8_t status = FR_OK;
//mount SD if it's not already mounted
if(!sd_mount){
status = f_mount(&myFATAFS, SDPath, 0);
if(status != FR_OK){
printf("SD: Failed to mount, status = %d\r\n", status);
return;
}
sd_mount = 1;
}
//create new file if the previous file is closed
if(new_file){
sprintf(fileName, "%s%d%s", myPath, f_number++, format);
printf("filename: %s\r\n", fileName);
status = f_open(&myFILE, fileName, FA_READ | FA_WRITE | FA_OPEN_APPEND);
if(status != FR_OK){
printf("SD: Failed to open file, status = %d\r\n", status);
return;
}
new_file = 0;
}
//write data if file is successfully opened
if(write_flag){
status = f_write(&myFILE, myData, strlen(myData), &testByte);
if(status != FR_OK){
printf("SD: Failed to write data, status = %d\r\n", status);
// write_flag = 0;
}
}
//close file if size>2MB OR f_write had an error
if(f_size(&myFILE) > 2000000 || status != FR_OK || !write_flag){
status = f_close(&myFILE);
if(status != FR_OK){
printf("SD: Failed to close file, status = %d\r\n", status);
}
else{
write_flag = 1;
new_file = 1;
}
}
//none of these worked to solve the problem
/*
if(status != FR_OK){
status = f_mount(0, SDPath, 0);
printf("SD: unmount, status = %d\r\n", status);
sd_mount = 0;
printf("unlink = %d\r\n", FATFS_UnLinkDriver(SDPath));
printf("SD: deinit = %d\r\n", HAL_SD_DeInit(&hsd));
//PD3 is power enable for the SD
HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_3);
HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_3);
printf("SD: init = %d\r\n", HAL_SD_Init(&hsd));
MX_FATFS_Init();
}
*/
}