cancel
Showing results for 
Search instead for 
Did you mean: 

Is there a "simple" example of how to write/read a USB drive using STM32CubeIDE?

RCooke88
Senior

Hi Folks,

I'd like to find an example of accessing a USB drive using the STM32CubeIDE. I'm currently using the Nucleo-H7A3Zi-Q board.

I believe I have setup the hardware correctly using the device configuration tool but I can't seem to find a simple or concise example on how to develop the code.

I have downloaded the STM32Cube_FW_H7_V1.11.0 code and examples but it still seems more complicated than it should. I've looked at STM32Cube_FW_H7_V1.11.0\Projects\NUCLEO-H7A3ZI-Q\Applications\USB_Host\MSC_Standalone\ but I don't understand how (or what) to import into my workspace.

For a start I just want to be able to write a "Hello World" into a file on the USB key.

Anybody have a good tutorial or example I can use?

Thanks,

Richard

44 REPLIES 44

Now you have a working drive - so standard fat commands are your friends now...

 

read from master Chan:

http://elm-chan.org/fsw/ff/00index_e.html

 

+ examples...

look here:

https://evenlund.blogspot.com/2016/10/usb-storage-with-stm32f4-discovery-and_58.html

 

 /* Create and Open a new text file object with write access */
      if(f_open(&MyFile, "Even.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
      {
        /* 'STM32.TXT' file Open for write Error */
        USB_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 */
          USB_Error_Handler();
        }
        else
        {
          /* Close the open text file */
          f_close(&MyFile);

        /* Open the text file object with read access */
        if(f_open(&MyFile, "Even.TXT", FA_READ) != FR_OK)
        {
          /* 'STM32.TXT' file Open for read Error */
          USB_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 */
            USB_Error_Handler();
          }
          else
          {
            /* Close the open text file */
            f_close(&MyFile);

 

--- BUT dont this in endless loop , only one time. :)

 

If you feel a post has answered your question, please click "Accept as Solution".

I got it reading and writing to the USB drive. At least with the small text sample from the code above. But that brings up another question - the sample code was able to create a text file named "Even.TXT" that contains the proper text "This is STM32 working with FatFs" but the file doesn't have a Date Modified value. There is no date or time associated with the file. How do I get a date and time for the file?

How would you write a 1Kb text file to the USB drive? Would you write chunks of data?

Thanks again,

Richard

Good ! :thumbs_up:

There is no date or time

I dont know - you have RTC running, with actual date on your system ? (How it should know it otherwise ?)

see cube :

AScha3_0-1705873561959.png

+

>How would you write a 1Kb text file to the USB drive?

open file, as in example, then write the kB , then close file. 

/*-----------------------------------------------------------------------*/
/* Write File                                                            */
/*-----------------------------------------------------------------------*/

FRESULT f_write (
	FIL* fp,			/* Pointer to the file object */
	const void* buff,	/* Pointer to the data to be written */
	UINT btw,			/* Number of bytes to write */
	UINT* bw			/* Pointer to number of bytes written */
)
{
If you feel a post has answered your question, please click "Accept as Solution".

I got the timestamp to work. I had the proper settings in the IOC file but it still wasn't working. I looked around and found a variable in ffconf.h that needed to be set to 1. Line 220 =  #define _FS_NORTC 1. It was set to zero. Now I am getting the file created with the time stamp.

Actually it's not working correctly. Turns out setting FS_NORTC = 1 bypasses the RTC and uses values set in the ffconf.h file.

I need to figure out how to use the

get_fattime();