static void __mount_udisk(void)
{
uint32_t free;
uint8_t res = f_mount(&USBHFatFS, USBHPath, 1);
if (res == FR_OK)
{
printf("usb disk mount successed.\n");
res = f_error(&USBHFile);
printf("usb disk error %d\n", res);
res = f_getfree(USBHPath, (DWORD *)&free, (FATFS **)&USBHFatFS);
if (FR_OK == res)
{
res = f_error(&USBHFile);
printf("usb disk error %d\n", res);
printf("usb disk free space %d\n", free);
res = f_open(&USBHFile, "1:/usb.txt", FA_WRITE | FA_CREATE_ALWAYS); //此处尝试了usb.txt和1:/usb.txt的文件名,1:/usb.txt会进入hard fault
if (FR_OK == res)
{
printf("usb disk put string test\n");
int32_t num = f_puts("usb disk put string test.\n", &USBHFile);
printf("usb disk write %d into file\n", num);
f_close(&USBHFile);
}
else
{
printf("test open file failed %d.\n", res);
}
}
else
{
printf("usb disk get free failed.\n");
}
}
else
{
printf("usb disk mount failed.\n");
}
}
static void __application_state_handler(void)
{
switch (Appli_state)
{
case APPLICATION_IDLE:
break;
case APPLICATION_START:
Appli_state = APPLICATION_IDLE;
break;
case APPLICATION_READY:
__mount_udisk();
Appli_state = APPLICATION_IDLE;
break;
case APPLICATION_DISCONNECT:
f_mount(NULL, USBHPath, 0);
Appli_state = APPLICATION_IDLE;
break;
default:
break;
}
}
/* USER CODE END 1 */
/**
* 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 */
// HAL_PWREx_EnableUSBVoltageDetector();
/* 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_AUDIO_CLASS) != USBH_OK)
{
Error_Handler();
}
if (USBH_RegisterClass(&hUsbHostFS, USBH_CDC_CLASS) != USBH_OK)
{
Error_Handler();
}
if (USBH_RegisterClass(&hUsbHostFS, USBH_MSC_CLASS) != USBH_OK)
{
Error_Handler();
}
if (USBH_RegisterClass(&hUsbHostFS, USBH_HID_CLASS) != USBH_OK)
{
Error_Handler();
}
if (USBH_RegisterClass(&hUsbHostFS, USBH_MTP_CLASS) != USBH_OK)
{
Error_Handler();
}
if (USBH_Start(&hUsbHostFS) != USBH_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USB_HOST_Init_PostTreatment */
/* USER CODE END USB_HOST_Init_PostTreatment */
}
/*
* Background task
*/
void MX_USB_HOST_Process(void)
{
/* USB Host Background task */
USBH_Process(&hUsbHostFS);
__application_state_handler();
}
/*
* 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;
break;
case HOST_USER_CLASS_ACTIVE:
if ((USBH_MSC_CLASS)->ClassCode == USBH_GetActiveClass(phost))
{
Appli_state = APPLICATION_READY;
}
else
{
Appli_state = APPLICATION_IDLE;
}
break;
case HOST_USER_CONNECTION:
Appli_state = APPLICATION_START;
break;
default:
break;
}
/* USER CODE END CALL_BACK_1 */
}