FatFS f_open always returns FR_DISK_ERR on USB stick
Hello,
I try to implement some kind of data logger which saves values on an usb stick. The board is based on an STM32F767ZGT6.
To do things properly, I have created a fresh new project with STM32CubeMX ( to include later in my main project). Here is what I have configured in CubeMX in few words:
- HSE : 25Mhz / LSE 32,768kHz Crystals
- CPU Icache & Dcache Enabled
- FreeRTOS enabled
- Timebase: Timer 6
- Minimum Heap 0x400 / Minimum Stack 0x800
- FatFS R0.12c
- USB_OTG_HS: Internal FS Phy Host_Only
- Class For HS IP : Mass Storage Host Class
Then I have implemented a way to create, open, fill and close a file based on F7 library "FatFs_USBDisk" and "FatFs_USBDisk_RTOS" examples. To simplify things, in the main task, I do the followings:
/* FatFS: Link the USBH driver */
FATFS_LinkDriver(&USBH_Driver, USBHPath);
/* Init host Library, add supported class and start the library. */
USBH_Init(&hUsbHostHS, USBH_UserProcess, HOST_HS);
USBH_RegisterClass(&hUsbHostHS, USBH_MSC_CLASS);
USBH_Start(&hUsbHostHS);When USBH_UserProcess() is called and when id is HOST_USER_CLASS_ACTIVE, an event is triggered and call my MSC_Application() :
/* FatFs Initialization */
f_mount(&USBHFatFS, (TCHAR const*)USBHPath, 0);
/* 'STM32.TXT' file Open for write */
f_open(&MyFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE);
/* Write data to the text file */
f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);
/* Close the open text file */
f_close(&MyFile);
/* Unmount Logical Drive */
f_mount(NULL, (TCHAR const*)"", 0);When "f_open" is called, it returns "FR_DISK_ERR". The file is sometimes created on USB stick but empty... Which is probably normal, write cannot work if open fails. The problem is that "f_open" always fails...
I have tried several things:
- taskENTER_CRITICAL(); / taskEXIT_CRITICAL(); around f_open
- Change FreeRTOS priority
- Change key from FAT32 to FAT16
- Add delay between each MSC function
- Change of USB stick
- Functions to recall f_open if it fails
I have read and followed tutorials like this one.
And I have also read nearly all posts here concerning FatFs and FR_DISK_ERR (even for uSD) (maybe I have missed the good one ...).
Now I will try to find where is raised this error in ff.c. But if someone has any idea of what I am doing wrong, or which configuration I forget, I would appreciate that so much!
Thanks for your help!
Edit: 17/06/19 11:55
Here are the several functions and their returns that finally end with a FR_DISK_ERROR:
- f_open() in ff.c returns FR_DISK_ERROR due to find_volume()
- find_volume() in ff.c returns FR_DISK_ERROR ( "An error occurred in the disk I/O layer") due to check_fs()
- check_fs() in ff.c returns 4 ("Load boot record" ) due to move_window()
- move_window() in ff.c returns FR_DISK_ERROR ( ? ) due to disk_read()
- disk_read() in diskio.c returns RES_ERROR ( "Read/Write error" )
Edit: 17/06/19 15:50
With an older nasty code (but without FreeRTOS) I am able to write on USB stick... So hardware is good. But I still need a clean code that can be run with FreeRTOS.