Use STM32CubeMX software to generate USB_HOST to read and write U disk

1. Test platform:
MCU: STM32F429IGT6
Tool: STM32CubeMX software
Compilation software: MDK

2. Configuration steps
(1). Open the STM32CubeMX software, create a new project file, and click the ACCESS TO MCU SELECTOR option.
insert image description here
(2). Select the chip model, here select the STM32F429IGT6 model.
insert image description here
(3). Configure the clock, click the RCC column under the System Core item, because there is an external crystal oscillator on the circuit board, configure the external crystal oscillator here, and set the High Speed ​​Clock (HSE) and Low Speed ​​Clock (LSE) to Crystal/Ceramic Resonator.
insert image description here
(4) Configure the number of clocks (Clock Configuration). Since the crystal oscillators on the circuit board are 25MHZ and 32.768KHZ, here we enter 25MHZ, enter 180 in the HCLK column and press the Enter key to let the software automatically complete the configuration.
insert image description here
(5). Configure serial port 1 for debugging, select Asynchronous in the Mode column, select PA9, PA10 for pins, serial port 1 on the circuit board corresponds to PA9, PA10, and check USART1 global interrupt interrupt.
insert image description here
(6). Fill in the project file information, and set it as shown in the figure below.
insert image description here
insert image description here
(7). Generate code to test whether the project generated by serial port 1 is available, and click GENERATE CODE.
(8). To test the code, add the code int fputc(int ch,FILE *f) to the project file, check the Use MicroLIB option, and use the printf function to test the function of serial port 1.
insert image description here
insert image description here
(9). Configure USB_OTG. Since the pins of USB on the circuit board are PB14 and PB15, USB_OTG_HS needs to be configured here. Since there is no external Phy on the circuit board, it is configured here as internal FS Phy, Internal FS Phy Under the option Host Only.
insert image description here
(10). Configure USB_HOST, since we are using it for reading and writing of U disk, so configure it here as Class For HS IP and select Mass Storage Host Class.
insert image description here
(11). Configure FATFS, check USB Disk, because we need to use Chinese, so CODE PAGE select Simplified Chinese (DBCS), configure it as a long file name, USE_LFN select Enabled with static working buffer on... (12). After
insert image description here
configuration After USB, go back to the Clock Configuration interface to update the clock, and you can see that the 48MHZ clock is now in effect. For operating the U disk, generate the code GENERATE CODE again.
insert image description here
(13). Open the project file and replace the USBH_UsrLog(…) macro definition with
#define USBH_UsrLog(…) do {
printf(“USBH_UsrLog: “);
printf( VA_ARGS );
printf(”\n”);
} while (0)
insert image description here
(14). After the compilation is completed, download it to the circuit board, insert the U disk, and the serial port 1 outputs the following information, indicating that the generated USB Host is successful.

insert image description here
(15), if you need to generate a project with FreeRtos, configure FreeRtos.
insert image description here
(16).USB_Host needs to be reset. USBH_USE_OS Enable, USBH_PROCESS_STACK_SIZE 512 in CMSIS_RTOS, the default stack size is 128. After the experiment, when the U disk is inserted, the program will enter HardFault_Handler, and it is normal to modify it to 512.
insert image description here

Guess you like

Origin blog.csdn.net/qizhi321123/article/details/126363052