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 new) main , in H7A3-test (or ...your name ) .

Try compile ( ..the hammer symbol) , should generate a (useless) program without errors.

Fine - you are here ?

 

 

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

Yes. It compiles with no errors.

Now we should have a working usb msc host - but its doing ...nothing.

So we do something:

 

 

 

 

    /* USER CODE BEGIN 3 */
    if(Appli_state == APPLICATION_READY && usb_status == 0) 	// USB stick ready->
    {
    	printf("USB stick -> mount \n");						// show start
        retUSBH = f_mount(&USBfs, (TCHAR const*)USBHPath, 1); 	// mount...?
        if (retUSBH==0) usb_status = 1;                     	// mount ok.   // mounted ok.
    }

    if(Appli_state ==  APPLICATION_DISCONNECT && usb_status > 0)// USB stick out-> mount in use ?
    {
    	printf("USB stick -> unmount \n");						// show stop
        retUSBH = f_mount(NULL, (TCHAR const*)USBHPath, 1); 	// unmount...?
        if (retUSBH==0) usb_status = 0;                         // unmounted .
    }

 

 

 

 

 (insert after ...user...3 */  )

I made a global symbol, to know is a usb there or not:

 

 

 

 

volatile int16_t  usb_status=0 ;
// and ...
FRESULT fresult;  		// result
extern ApplicationTypeDef Appli_state ;
FATFS USBfs;  	// file system
extern char USBHPath[]; 	/* disk logical drive path */

 

 

 

 

Then you can do a first read (never write, or format ! until you know, its working fine . ) in main..

 

 

 

 

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

 

 

 

And use a usb stick, with a simple text file on it...if you are on windows, maybe the first file is this useless windows drive thing. 

 (I have no H7a3-nuc , so maybe the usb is not powered...you have to look for your hardware. If 5V at the usb port, the usb should work now and you can see the readed filename in debug, just make a breakpoint there. )

I hope, i didnt forget something... just tell me.

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

Got a couple of errors.

Where is fresult and Appli-state defined?

 

...reload. I edit...

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

I don't think we have defined "fresult" yet.

Now we have . :)

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

I hope I'm not too much trouble here but the compiler doesn't like this:

 

 

/* USER CODE BEGIN 2 */
  if(usb_status>0)	// if usb mounted
  	{
  		fresult = f_opendir(&dir,USBHPath);	// main dir open
  		fresult = f_readdir (&dir, &file_info);
          }
  /* USER CODE END 2 */

 

The &dir and &file_info are throwing errors.

No trouble. My error.. add:

FILINFO file_info;
FIL MyFile;
DIR dir; 				

 

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

OK. It compiles without any errors.