2024-01-16 05:11 PM
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
2024-01-17 12:56 PM
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 ?
2024-01-17 12:58 PM
Yes. It compiles with no errors.
2024-01-17 01:10 PM - edited 2024-01-17 01:23 PM
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.
2024-01-17 01:17 PM
Got a couple of errors.
Where is fresult and Appli-state defined?
2024-01-17 01:21 PM
...reload. I edit...
2024-01-17 01:26 PM
I don't think we have defined "fresult" yet.
2024-01-17 01:31 PM
Now we have . :)
2024-01-17 01:45 PM
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.
2024-01-17 01:50 PM
No trouble. My error.. add:
FILINFO file_info;
FIL MyFile;
DIR dir;
2024-01-17 01:55 PM
OK. It compiles without any errors.