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

Thank you for you help. Maybe we can continue tomorrow.

We do.

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

Hi AScha, Just wanted to update you on my "issue". I have tried a new Nucleo-H7A3Zi-Q board, and a new USB stick. Still got the same results:

usb_status = 1

Appli_state = APPLICATION_READY

fresult = FR_OK

When I set up the project I used the default clock settings. The SYSCLK = 96MHz and the USB clock = 48MHz. Are these values acceptable?

Thanks again,

Richard

Update#1

I lowered the USB clock to 12MHz and now the variables are:

usb_status = 0

Appli_state = APPLICATION_START

fresult = FR_OK

So I think we can go to the next step. I'm not sure what that is.

Hi,

I think, it was ok before :

>>

usb_status = 1

Appli_state = APPLICATION_READY

fresult = FR_OK

SYSCLK = 96MHz and the USB clock = 48MHz

<<

USB needs fix clock 48M , from crystal source (HSE , 8MHz , i think) . 48 .

+

After (usb_status = 1) you should see in debug, when moving mouse to " USBfs " , the readed file info.

 

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

I changed the USB clock back to 48MHz.

I can see the USBfs variable. It shows the correct name of the USB stick = "USB_Drive". How do I see the file name that is on the USB stick? There is only one file. It is named kocour.txt.

As i said : should work now.

You can now use it, i give an example :

 

if(usb_status>0)			// if usb mounted
   {
		fresult = f_opendir(&dir,USBHPath);		// main dir open
		fresult = f_readdir (&dir, &file_info);      // read first filename
   }

 

 -> see in debug, when moving mouse to " dir " , the readed dir info.

+

-> see in debug, when moving mouse to " file_info " , the readed file info.

 

( i wrote this already .... didnt you try ? )

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

I did try what you suggested. It is not making sense to me. There is only one file on the USB stick. It is named "kocour.txt". When I stop the program and use the debugger to look into the &file_info variable I see this for the fname:

Details:"System Volume Information", '\0' <repeats 230 times>

Default:0x24001232 <file_info+26>

Decimal:603984434

Hex:0x24001232

Binary:100100000000000001001000110010

Octal:04400011062

 

The USB stick does not have a directory name so the "dir" variable looks like this:

dir

Details:0x24000294 <USBfs+188> "ål"

Default:0x24000294 <USBfs+188> "ål"

Decimal:603980436

Hex:0x24000294

Binary:100100000000000000001010010100

Octal:04400001224

 

 

Haaaaa - your silly windows system doing this...always put this useless directory on everything it gets .

so read more :

 

if(usb_status>0)			// if usb mounted
   {
		fresult = f_opendir(&dir,USBHPath);		// main dir open
		fresult = f_readdir (&dir, &file_info);      // read first filename
	        fresult = f_readdir (&dir, &file_info);      // read next filename
   }

 

- and do this only once !   

 

 

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

That was it. I can now see the correct file name. Now that I can read a file name how hard is it to write data to the USB drive?

I assume that I have to "Open" the drive, then "Write data" to the drive and then "Close" the drive.

Do you have an example I can look at?