1-选USB-FS,Host_Only
2-USB_HOST,Mass Storage Host Class配置为大容量存储设备,调试为3,512个words,即2K字节。
3-FATFS,勾选USB Disk,CODE_PAGE设置Latin1改S-C-(DBCS),LFN为----STACK.
4-工程堆栈全为1000。
5-在main.c文件中 代码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
static void MSC_Application(void)
{
FRESULT fres; // FatFs function common result code
uint32_t byteswrite;
uint8_t str[] = "hello world!";
/* 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, "test.txt", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
{
Error_Handler(); //'STM32.TXT' file Open for write Error
}
else
{
fres = f_write(&myFile, str, sizeof(str), (void *)&byteswrite);
if(byteswrite == 0 || (fres != FR_OK))
{
Error_Handler();
}
else
{
f_close(&myFile); //Close the open text file
}
}
}
}
6-在main.c代码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盘后新建了tast文件内容hello word。