cancel
Showing results for 
Search instead for 
Did you mean: 

[FATFs] Opening file on USB stick returns FR_DISK_ERR

smrtkai
Senior
Posted on June 27, 2016 at 14:12

Hi,

I want to write some data to a file on a usb stick. I have setup a new project with STM32CubeMX for the STM32F429I discovery board. 

My STM32CubeMX configuration:

USB_OTG_HS: Internal FS Phy - Host Only

USB_HOST: Class for HS IP: Mass Storage Host Class

FATFS: USB Disk

My Code in the main-loop:

while(1) {

  /* USER CODE END WHILE */

    MX_USB_HOST_Process();

  /* USER CODE BEGIN 3 */

    switch(Appli_state) {

      case APPLICATION_START:

        

        if (retUSBH != 0) {

          Error_Handler();

        }

      

        fres = f_mount(&USBFatFs, (TCHAR const*)USBH_Path, 0);

        if(fres != FR_OK) {

          Error_Handler();

        }

        

        fres = f_open(&MyFile, ''STM32.TXT'', FA_READ);

        if(fres != FR_OK) {

          Error_Handler();

        }

        ...

        

        break;

 

      default:

        break;

    }

When I try to open a file, f_open returns FR_DISK_ERR. Can anyone give me some advice, what might be wrong.

1. ''FatFS: Link the USBH driver'' is successful => retUSBH = 0

2. Mount is successful => FR_OK

3. Opening file fails => FR_DISK_ERR

So there might be some configuration issue for FATFS or with USB. 

7 REPLIES 7
troy1818
Senior
Posted on June 27, 2016 at 14:36

Hi,

It might be useful to debug the lower layers. The fatFS library f_open should boil down to some lower layer function in HAL USB lib. For Chan fatFS lib the connection between the fatFS lib and HAL lib is in a file called diskio.c (or similar) if I recall correctly.

When you get the FR_DISK_ERR I think that it is actually the lower layer HAL function that gives error and that the fatFS is just reacting to this.

smrtkai
Senior
Posted on June 27, 2016 at 16:17

Okay, I have analyzed the lower levels.

The call stack is:

f_open => find_volume => check_fs => move_window => disk_read => USBH_read => USBH_MSC_Read

Edit 1:

And USBH_MSC_Read returns USBH_FAIL. In USBH_MSC_Read the pHost->gState and the MSC_Handle->unit[lun].state have the wrong values.

The MSC_Handle unit is MSC_NOT_READY instead of MSC_IDLE. And the gState is HOST_DEV_ATTACHED instead of HOST_CLASS.

Edit 2:

So, the cause of the error is that the USB state does not change from HOST_USER_CONNECTION to HOST_USER_CLASS_ACTIVE. But I have no clue, what's the problem. :(

troy1818
Senior
Posted on June 27, 2016 at 21:05

Ok, sounds like a good debug session.

I would say that it could be something wrong with either the hardware itself, HAL library error or the USB stick not working together with the hardware. Do you have more than one USB stick to test with ?

smrtkai
Senior
Posted on June 28, 2016 at 08:34

I have finally found the error(s). I think I made a second error.

First, the USB stick has to be mounted after the USBH_UserProcess changes its state HOST_USER_CLASS_ACTIVE. The generated code from STM32CubeMX has two ''confusing'' application states APPLICATION_START on HOST_USER_CONNECTION and APPLICATION_READY on HOST_USER_CLASS_ACTIVE. Any FatFs commands should be executed after APPLICATION_STATE_READY is reached.

Second, after some hours of unsuccessful debugging, i have created a fresh project, where I mistakenly chose the wrong USB class.
troy1818
Senior
Posted on June 28, 2016 at 16:07

Great ! I personally discourage the use of auto generated stuff. Better to create the main bulk of the code yourself if possible. Usually pays off in the long run 🙂

ade
Associate II
Posted on September 03, 2016 at 12:42

Can you give the explanation about the way you solve your problem ? I faced the same problem like yours and I already stuck on it for couple of days

thank you

smrtkai
Senior
Posted on September 06, 2016 at 09:48

I have described my solution in a previous post. Hope that helps.

''

First, the USB stick has to be mounted after the USBH_UserProcess changes its state HOST_USER_CLASS_ACTIVE. The generated code from STM32CubeMX has two ''confusing'' application states APPLICATION_START on HOST_USER_CONNECTION and APPLICATION_READY on HOST_USER_CLASS_ACTIVE. Any FatFs commands should be executed after APPLICATION_STATE_READY is reached.''