cancel
Showing results for 
Search instead for 
Did you mean: 

USB host msc stm32f107 stop writing to usb memory stick after 30-40 times. f_open() return FR_DISK_ERR

Fayaz.1
Associate II

Hi. I'm tring to save data to usb memory stick. this is main fuction :

while (1)
  {
    /* USER CODE END WHILE */
 
 
    MX_USB_HOST_Process();
 
    /* USER CODE BEGIN 3 */
     switch(Appli_state)
                {
                    case APPLICATION_IDLE:
                        break;
                    case APPLICATION_START:
                        counter_start++;
                        if(f_mount(&myUsbFatFS, (TCHAR const*)USBH_Path, 0) == FR_OK)
                            {
                               UsbTest_Write();
                            }
                        break;
                    case APPLICATION_READY:
                        break;
 
                    case APPLICATION_DISCONNECT:
                        break;
                }
 
      }

This is  UsbTest_Write(); function

bool UsbTest_Write(void)
{
    //Open or Create file for writing
    if(f_open(&myFile, "a.csv", FA_WRITE | FA_OPEN_ALWAYS ) != FR_OK)
    {
        return 0;
    }
    //Copy test Text to my temporary read/write buffer
    f_lseek(&myFile,myFile.fsize);
    sprintf(rwtext, "asdfsdfsafdfadf\tasdfsdfsafdfadf\tasdfsdfsafdfadf\n");
    //Write to text file
    res = f_write(&myFile, (const void *)rwtext, strlen(rwtext), &byteswritten);
    if((res != FR_OK) || (byteswritten == 0))
    {
        return 0;
 
        f_close(&myFile);
    }
    f_close(&myFile);
    return 1; //Success
}

When ı connect usb stick it starts to write. After write 30-40 times it stop to writing. If i disconnect usb stick and connect again. It is write again 30-40 times. I looked in debug and put break point after

UsbTest_Write()

function this time it write just 1 time and

UsbTest_Write()

function always return 0 in this line

if(f_open(&myFile, "a.csv", FA_WRITE | FA_OPEN_ALWAYS ) != FR_OK)

{

return 0;

}

Why can be this happen and how can i solve?

3 REPLIES 3

Not going to be able to debug it at the top level.

Will need to instrument the DISKIO and USB-MSC level to understand the interactions. For bus level issues a USB Analyzer

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I don't understand exactly how can i do this can you explain.

Hi @Community member​  I try to look deep and f_open->find_volume->check_fs->disk_read->USBH_MSC_Read

in usbh_msc.c USBH_MSC_Read function return USBH_FAIL with this

if ((phost->device.is_connected == 0U) ||
      (phost->gState != HOST_CLASS) ||
      (MSC_Handle->unit[lun].state != MSC_IDLE))
  {
    return  USBH_FAIL;
  }

phost->device.is_connected = 0x01

 phost->gState= HOST_ENMERATION

 MSC_Handle->unit[lun].state = 0x7F

Do you have any suggestion ?