1、打开USB_OTG_HS
2、使能USB_HOST
3、使能FATFS
4、生成基本工程模版
5、打开程序工程文件,添加测试代码:
①main.c中添加MSC_Application()
/* USER CODE BEGIN 0 */
extern ApplicationTypeDef Appli_state;
extern USBH_HandleTypeDef hUsbHostFS;
extern char USBHPath[4]; // USBH logical drive path
FATFS FatfsUDisk; // File system object for USB disk logical drive
FIL MyFile; // File object
char USBDISKPath[4]; /* USB Host logical drive path */
USBH_HandleTypeDef hUSBHost; /* USB Host handle */
static void MSC_Application(void)
{
FRESULT res; /* FatFs function common result code */
uint32_t byteswritten, bytesread; /* File write/read counts */
uint8_t wtext[] = "This is STM32 working with FatFs"; /* File write buffer */
uint8_t rtext[100]; /* File read buffer */
/* Register the file system object to the FatFs module */
if( f_mount(&FatfsUDisk, (TCHAR const*)USBHPath, 0) != FR_OK)
{
Error_Handler(); //FatFs Initialization Error
}
else
{
/* Create and Open a new text file object with write access */
if(f_open(&MyFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
{
/* 'STM32.TXT' file Open for write Error */
Error_Handler();
}
else
{
/* Write data to the text file */
res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);
if((byteswritten == 0) || (res != FR_OK))
{
/* 'STM32.TXT' file Write or EOF Error */
Error_Handler();
}
else
{
/* Close the open text file */
f_close(&MyFile);
/* Open the text file object with read access */
if(f_open(&MyFile, "STM32.TXT", FA_READ) != FR_OK)
{
/* 'STM32.TXT' file Open for read Error */
Error_Handler();
}
else
{
/* Read data from the text file */
res = f_read(&MyFile, rtext, sizeof(rtext), (void *)&bytesread);
if((bytesread == 0) || (res != FR_OK))
{
/* 'STM32.TXT' file Read or EOF Error */
Error_Handler();
}
else
{
/* Close the open text file */
f_close(&MyFile);
/* Compare read data with the expected data */
if((bytesread != byteswritten))
{
/* Read data is different from the expected data */
Error_Handler();
}
else
{
/* Success of the demo: no error occurrence */
BSP_LED_On(LED3);
}
}
}
}
}
} /* Unlink the USB disk I/O driver */
FATFS_UnLinkDriver(USBDISKPath);
}
/* USER CODE END 0 */
②主循环添加应用程序
/* USER CODE BEGIN 3 */
switch(Appli_state)
{
case APPLICATION_READY:
MSC_Application();
Appli_state = APPLICATION_DISCONNECT;
break;
case APPLICATION_DISCONNECT:
f_mount(NULL, "", 0);
break;
default:
break;
③添加U盘插拔指示灯功能
->初始化位置:
/* USER CODE BEGIN 2 */
BSP_LED_Init(LED3);
BSP_LED_Init(LED4);
/* USER CODE END 2 */
-->MSC_Application函数中添加亮灯程序(上面代码中已含)
---->usb_host.c中添加连接断开灭灯功能
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;
BSP_LED_Off(LED3);
BSP_LED_Off(LED4);
// f_mount(NULL, (TCHAR const*)"", 0);
break;
case HOST_USER_CLASS_ACTIVE:
Appli_state = APPLICATION_READY;
break;
case HOST_USER_CONNECTION:
Appli_state = APPLICATION_START;
break;
default:
break;
}
6、编译,完成
插上U盘后能够在U盘里面生成txt,并写入内容