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

I do have another question - Where does the following code belong:

 

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

 

Does it go before or after the case statements?

if(Appli_state == APPLICATION_READY && usb_status == 0)

 

TRY...debug.. breakpoints at f_mount , and opendir -> see fresult ok ? (= 0 ) ... ???

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

Hmmm..   the Appli_state ... is , what the usb telling us. Not more. ( at end of while loop, after 

/* USER CODE BEGIN 3 */  , as i showed.  )

Then in main, in the "while" loop, we wait (do ...nothing or something..) until usb is ready.

So i use here: "if mounted" -> then we can read or do more , before its mounted (maybe 100ms or so) any access would be useless, crash or at least some error. -> in main while(1) loop then !

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

usb_status = 0

Appli_state = APPLICATION_IDLE

fresult = FR_OK

After I step thru the while() loop a few times the Appli_state changes to APPLICATION_READY. But it never changes from that.

First : debug.. breakpoint at f_mount ; if connect to stick, you landing here . Then mount should work also.

If not ...check your hardware : is usb stick there? is 5V at the plug ? (i dont know...i dont have this board, just my own boards); see circuit of your board, maybe this "activate vbus.." is wrong, or the PF10 (enable vbus) not switches 5v to the plug, or need a jumper setting...is 5V there ?

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

Aaa...you should not "step" the while loop, this usb thing needs enumeration and so on...complex...it will need many times call the usb_host process, so dont break this processing. We only look for the result, just run and use breakpoint at ...mount . If it not coming there, then we look, why.

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

OK, I removed the breakpoint and just let it run for a minute and then I inserted the breakpoint.

Now the variables are:

usb_status = 1

Appli_state = APPLICATION_READY

fresult = FR_OK

Are we on the right track?

One more thing - The VBUS LED is on which I believe indicates that the USB voltage is on and within limits.

Yep. usb_status = 1 -> mount  = ok. 

Should do...what you want , now. But first try to read only, check filename is correct ...

and breakpoints can be at mount or read from beginning on, just let it run , until there, thats all.

 

+ i am on track to my bed now. :)

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