cancel
Showing results for 
Search instead for 
Did you mean: 

FatFS f_open always returns FR_DISK_ERR on USB stick

Geo En
Associate III

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.

5 REPLIES 5
Geo En
Associate III

Anyone?

kqian
Associate II

Hello, Geo,

Have you fix this issue? I got same problem and wonder if you figured it out.

Thanks

Hello @kqian​ ,

Sorry for my late answer...

I was working on this problem with an online support of ST.

In my case, I was using FreeRTOS. So the first thing is that "the malloc implementation is wrongly handling only current stack pointer which is not correct together with rtos. Result is fail in memory allocation. To correct that, usbh_conf.h has to be modified:

/* Memory management macros */
#if (USBH_USE_OS == 1)
 /** Alias for memory allocation. */
#define USBH_malloc     pvPortMalloc
 /** Alias for memory release. */
#define USBH_free      vPortFree
#else
 /** Alias for memory allocation. */
 #define USBH_malloc     malloc
 /** Alias for memory release. */
 #define USBH_free      free
#endif

This will use freertos memory allocation instead of standard C version.

After this change the stack is working because the usb stack have the memory for his structures."

But .... that does not correct my problem... It repairs USBH_UserProcess ( APPLICATION_START, APPLICATION_READY etc...) but does not correct FatFS errors...

Today I still have no news of help support ... I will probably switch of USB port... From HOST_HS

to HOST_FS (this one seems to work correctly).

TFranke
Associate II

Dear Geo En,

thank you for sharing this code snippet.

My FSUSB + Free-RTOS implementation was not going from APPLICATION_START to APPLICATION_READY state and consequently f_mount (depending on opt parameter) or f_open failed.

Using the right memory allocation functions solved these 'mysteries' completely.

MEder
Associate III

Unfortunately this is still not fixed in package STM32Cube_FW_F7_V1.16.0 for the STM32F746 DISCO.

Is there a way to get the Dev team to update this?

Anyone???

Martin