cancel
Showing results for 
Search instead for 
Did you mean: 

How To access a USB Pen Drive with stm32u545 using ST Usb_Host MSC and FatFs

NFern.1
Associate III

This article documents step by step the changes to be done in a CUBEMX generated U545 project to access a USB Pen Drive using ST Usb_Host MSC and FatFs 

Working Code for the NUCLEO-U545RE-Q board is available at  

https://github.com/NereusF1/stm32u545-classic-coremw-usb-host-fatfs

Built using embedded software package STM32U5 v1.8 and STM32CUDEIDE v1.19.0
This project is tested on the NUCLEO-U545RE-Q board.
USB Pen drive must be formatted to FAT32.

The classic Middleware versions are ST USB Host Lib V3.5.1. and FatFs VR0.12c (ST modified 2023-08-18). The classic Middleware are copied over from an F439 project built using embedded software package STM32F4 v1.28.3

1. Create a project

Start a new project in STM32CubeIDE by selecting File → New → STM32 Project. Navigate to the [Board Selector] tab and choose [NUCLEO-U545RE-Q].

BoardSelector.png

2. Keep the default board options

BoardOpts.png

3. After creating the project, navigate to the [Connectivity] section, enable the USB peripheral [USB_DRD_FS] in mode [Host_Only_FS], and activate the USB global interrupt in the NVIC settings.

MxUsbConfig.png

4.  According to the reference manual, the USB peripheral requires a minimum clock frequency of 48 MHz to avoid data overrun and underrun issues. 
 

RM0456 Rev 6 Reference manual STM32U5 series Arm®-based 32-bit MCUs  

pg. 3087/3653 The USB peripheral implements an interface between a full-speed USB 2.0 bus and the APB2 bus.

pg. 3094/3653 Note: Due to USB data rate and packet memory interface requirements, the APB2 clock must have a minimum frequency of 12 MHz to avoid data overrun/underrun problems. 

pg. 3141/3653 Caution: To guarantee a correct operation for the USB OTG FS peripheral, the AHB frequency must  be higher than 14.2 MHz.

This being a low power device I would have liked to run SYSCLK & APB2 at lower frequency to save power but for good measure, I set SYSCLK, APB2 and USB clocks all to 48MHz. 

MxClk.png

5. For good measure set the Minimum Heap and Stack Sizes as below.

MxProj.png

6. No changes should be required in Advanced Settings

MxAdv.png

7. Save the Mx configuration and Generate Code
 
8. In File Explorer, copy FatFs, Muddlewares and USB_Host folders from the sample project on github (refer the link provided at the start of this article).
Note that I have renamed the folder as "Muddlewares" because "Middlewares" could be deleted if CubeMX is used for further round-trip code-modifications.  I am using STM32CUDEIDE v1.19.0 as I like the round-trip feature.
CopyFiles2.png
9. Refresh the Project in CubeIDE to view the copied folders
 
10. Add the Paths related to the source files
AddSrcPaths.png
11. Add header paths
AddIncPaths.png
 
12. Save the MX configuration and Generate Code
 
13. Add to main.c, the USER CODE sections shown below.
From the link at the start, copy the function definitions for MyUSBUserFnc() and ETX_MSC_ProcessUsbDevice() in the /* USER CODE BEGIN 4 */ section.
/* USER CODE BEGIN Includes */
#include "fatfs.h"
#include "usb_host.h"
#include <stdio.h>
/* USER CODE END Includes */

/* USER CODE BEGIN PV */
extern ApplicationTypeDef Appli_state;
uint8_t USBWriteOpEnable; // set from the watch window
extern UART_HandleTypeDef huart3;
/* USER CODE END PV */

/* USER CODE BEGIN PFP */
void MX_USB_HOST_Process(void);
void MyUSBUserFnc(void);
static FRESULT ETX_MSC_ProcessUsbDevice(void);
/* USER CODE END PFP */

  /* USER CODE BEGIN 2 */
  MX_FATFS_Init();
  MX_USB_HOST_Init();
  /* USER CODE END 2 */

    /* USER CODE BEGIN 3 */
	  MX_USB_HOST_Process();
	  MyUSBUserFnc();
  }
  /* USER CODE END 3 */
 
14. Where are my USB Pins - USB_DM_Pin, USB_DP_Pin defined? 
They should have been in the main.h or the stm32u5xx_nucleo_conf.h but are not found in either. Looks like they are defined and used internally.  Anyway, we do not have to directly use them in the program.
The USB peripheral is initialized in the file stm32u5xx_hal_msp.c
 
15. Build the Project.
 
16. Debug the project.
 
17. Add Appli_state and USBWriteOpEnable to the Live Expressions Watch Window.
Appli_state will be APPLICATION_DISCONNECT initially when the USB Pen Drive is not connected.
WatchWindow1.png
 
18. On Connecting the USB Pen Drive, the Appli_state changes to APPLICATION_READY
From the Watch Window set USBWriteOpEnable to 1. It will be immediately set back to 0 indicating successful operation.
WatchWindow2.png
 
19. Unplug the USB Pen Drive and the Appli_state changes back to APPLICATION_DISCONNECT .
 
20. Plug-in the USB Pen drive into the PC and check its contents as below
USBDriveContents.png
 
 
 
1 ACCEPTED SOLUTION

Accepted Solutions
NFern.1
Associate III

Thanks @FBL and @T_Hamdi for your support.

View solution in original post

1 REPLY 1
NFern.1
Associate III

Thanks @FBL and @T_Hamdi for your support.