This post was last edited by sylar^z on 2020-5-19 00:38
The NUCLEO-H743ZI board has its own USB interface, which can be used as a device USB port or as a main USB port to connect external USB flash drives, HID devices, etc.
1. Hardware Schematic Diagram
Hardware circuit of the USB interface of the NUCLEO-H743ZI board. USB_VBUS, USB_DM, USB_DP, USB_ID, GND are the 5 USB pins. USB_PowerSwitchOn is the power output control pin. USB_OverCurrent is the power model detection pin.
2. Create a project with STM32CubeMX
First, create a project through STM32CubeMX. Friends who are not familiar with STM32CubeMX can refer to my previous post ( https://en.eeworld.com/bbs/thread-1122042-1-1.html ) for instructions on how to create a NUCLEO-H743ZI project with STM32CubeMX.
Enter the configuration interface of NUCLEO-H743ZI, mainly configure the following function items:
1. USB_OTG_FS configuration
Select USB_OTG_FS in the list on the left, set the configuration mode to Host_Only, and enable VBUS_sensing. Enable global interrupts. Leave other settings as default.
2. USB middle layer function configuration
Select USB_HOST in the list on the left, set the configuration mode to MSC, and the mass storage mode. Select IO output for the power driver, and the pin is PG6. Other settings are as default.
3. FATFS function configuration
Select FATFS in the list on the left, and set the configuration mode to USB Disk. In the configuration, select CODE_PAGE to support simplified Chinese. Other settings are as default.
4. Add key interrupt
The user button on the board is PC13, which is configured by default to trigger the interrupt on the rising edge, that is, it is triggered when the button is pressed.
5. After saving the project, click GENERATE CODE in the upper right corner to automatically generate the project code
3. Programming
1. The source code file generated by STM32CubeMX has made basic configuration for the USB OTG function. After configuration, you need to enable the USB power output, which is the red part in the code.
/**
* Init USB host library, add supported class and start the library
* @retval None
*/
void MX_USB_HOST_Init(void)
{
/* USER CODE BEGIN USB_HOST_Init_PreTreatment */
/* USER CODE END USB_HOST_Init_PreTreatment */
/* Init host Library, add supported class and start the library. */
if (USBH_Init(&hUsbHostFS, USBH_UserProcess, HOST_FS) != USBH_OK)
{
Error_Handler();
}
if (USBH_RegisterClass(&hUsbHostFS, USBH_MSC_CLASS) != USBH_OK)
{
Error_Handler();
}
if (USBH_Start(&hUsbHostFS) != USBH_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USB_HOST_Init_PostTreatment */
/* Activate VBUS on the port */
USBH_LL_DriverVBUS(&hUsbHostFS, 0);
/* USER CODE END USB_HOST_Init_PostTreatment */
}
2. Add FATFS function code in USB connection, disconnection and other events.
void MX_FATFS_Init(void)
{
/*## FatFS: Link the USBH driver ###########################*/
retUSBH = FATFS_LinkDriver(&USBH_Driver, USBHPath);
/* USER CODE BEGIN Init */
/* additional user code for init */
/* USER CODE END Init */
}
/*
* user callback definition
*/
static void USBH_UserProcess (USBH_HandleTypeDef *phost, uint8_t id)
{
/* USER CODE BEGIN CALL_BACK_1 */
switch(id)
{
case HOST_USER_SELECT_CONFIGURATION:
break;
case HOST_USER_DISCONNECTION:
Appli_state = APPLICATION_DISCONNECT;
FATFS_UnLinkDriver(USBHPath);
f_mount(NULL, "", 0);
break;
case HOST_USER_CLASS_ACTIVE:
Appli_state = APPLICATION_READY;
break;
case HOST_USER_CONNECTION:
Appli_state = APPLICATION_START;
f_mount(&USBHFatFS, (TCHAR const*)USBHPath, 0);
break;
default:
break;
}
/* USER CODE END CALL_BACK_1 */
}
3. Key interrupt processing. Set a flag.
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(USER_Btn_Pin == GPIO_Pin)
{
btnClickFlag = 1;
}
}
4. Press the button to create a file. Add the function code in the while loop of the main function. Test to generate a txt file named testFile in the USB flash drive.
if(btnClickFlag)
{
btnClickFlag = 0;
//Create a file
f_open(&testFile, "testFile.txt", FA_CREATE_ALWAYS | FA_WRITE);
f_close(&testFile);
}
4. Test the USB OTG board by connecting it to a USB flash drive. Please note that the USB flash drive must be in FAT format, as NTFS, exFAT, etc. cannot be recognized.
5. File viewing: Generate a testFile.txt file in the USB flash drive. Since there is no content, the file size is 0Kb.