Skip to main content
Visitor II
June 6, 2020
Question

In USB Mass storage device class, does both side of handshaking is possible between device and host?

  • June 6, 2020
  • 1 reply
  • 897 views

Hi,

I am working on USB Mass Storage Device application using STM32 -F411E Discovery board . I have generated the USB Mass Storage device code using STM32 Cube Mx tool. I have created the 64Kb drive using internal RAM. The Host can write & read the data from this drive. Now my question is that, Can device also read & write the data into the same drive?

I need the both side of handshaking between device and host for my application. If is it possible then tell me the steps to write & read the data into the drive from device side?

Regards,

Pankaj

    This topic has been closed for replies.

    1 reply

    Graduate II
    June 6, 2020

    On the device you could access memory/files via FATFS.

    Writing can be very problematic due to coherency issues with Windows/Linux caching, etc. Can be addressed by reporting NOT READY or MEDIA CHANGED appropriately, but complexity beyond scope of a forum response.

    Visitor II
    June 6, 2020

    Hi,

    Thanks for the quick reply,

    I am not dealing with Windows/Linux PC OS. I am testing with STM32 USB MSC Host and Device firmware, in that case i don't thing any issue should happens. What do you think? But i am getting issue when i am writing using FATFS in Device Drive, below is my code sequence to write data....

    //******************************************************************************

    static void MSC_Application(void)

    {

     FRESULT res;           /* FatFs function common result code */

     uint32_t byteswritten, bytesread;      /* File write/read counts */

     uint8_t wtext[] = "This is STM32 working with FatFs"; /* File write buffer */

     uint8_t rtext[100];         /* File read buffer */

      

     /* Register the file system object to the FatFs module */

     if(f_mount(&USBDISKFatFs, (TCHAR const*)USBDISKPath, 0) != FR_OK)

     {

     /* FatFs Initialization Error */

     Error_Handler();

     }

     else

     {

     /* Create and Open a new text file object with write access */

     if(f_open(&MyFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK) 

     {

      /* 'STM32.TXT' file Open for write Error */

      Error_Handler();

     }

     else

     {

      /* Write data to the text file */

      res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);

       

      if((byteswritten == 0) || (res != FR_OK))

      {

      /* 'STM32.TXT' file Write or EOF Error */

      Error_Handler();

      }

      else

      {

      /* Close the open text file */

      f_close(&MyFile);

       

      /* Open the text file object with read access */

      if(f_open(&MyFile, "STM32.TXT", FA_READ) != FR_OK)

      {

       /* 'STM32.TXT' file Open for read Error */

       Error_Handler();

      }

      else

      {

       /* Read data from the text file */

       res = f_read(&MyFile, rtext, sizeof(rtext), (void *)&bytesread);

        

       if((bytesread == 0) || (res != FR_OK))

       {

       /* 'STM32.TXT' file Read or EOF Error */

       Error_Handler();

       }

       else

       {

       /* Close the open text file */

       f_close(&MyFile);

        

       /* Compare read data with the expected data */

       if((bytesread != byteswritten))

       {     

        /* Read data is different from the expected data */

        Error_Handler();

       }

       else

       {

        /* Success of the demo: no error occurrence */

        BSP_LED_On(LED4);   

       }

       }

      }

      }

     }

     }

    }

    //*************************************************************

    After USB MSD initialization i am calling MSC_Application() function to check the data can be read or write from the drive. But the code stuck after calling f_open() function.

    Also i am confusing what function should i use to write data into the device drive .... STORAGE_Write_FS() ; function to write data or f_write() FATFS function as mention in above code.

    Reagrds,

    Pankaj