2019-06-17 01:32 AM
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:
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:
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:
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.
2019-06-19 04:58 AM
Anyone?
2019-07-25 10:30 AM
Hello, Geo,
Have you fix this issue? I got same problem and wonder if you figured it out.
Thanks
2019-08-26 12:13 AM
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).
2020-01-26 04:01 PM
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.
2020-06-10 12:26 PM
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