STM32的Host U盘
记录下用STMCubeMx生成Host U盘+FATFS的过程
1.CubeMX配置
USB_OTG_FS
配置为U盘
配置FATFS
添加测试代码
/* USER CODE BEGIN Application */
static void MSC_Application(void) //USB大容量存储区测试代码
{
FRESULT res;
uint32_t byteswritten; //file write/read counts
//uint8_t wtext[] = "This is USB Mass Storage Application Example!\r\n";
uint8_t wtext[] = "这是一个STM32CubeMX_FATFS_FREERTOS_USB测试!\r\n";
#if 1
/*以下代码测试ok,可以向STM32_USB.txt中写入数据*/
uint32_t bytesread; // 读文件计数
uint8_t rtext[1024]; // 读取的buff
printf("\r\n MSC_Application USB \r\n");
//1.挂载USB
res = f_mount(&USBHFatFS,(const TCHAR*)USBHPath,1);
if(res != FR_OK)
{
Error_Handler();
printf(" mount USB fail: %d \r\n",res);
}
else
{
printf(" mount USB success!!! \r\n");
//2.打开
res = f_open(&USBHFile, "STM32_USB.txt", FA_CREATE_ALWAYS | FA_WRITE);
if(res != FR_OK)
{
Error_Handler();
printf(" open file error : %d\r\n",res);
}
else
{
printf(" open file success!!! \r\n");
//3.写数据
res = f_write(&USBHFile, wtext, sizeof(wtext), (void *)&byteswritten); //在文件内写入wtext内的内容
if(res != FR_OK)
{
Error_Handler();
printf(" write file error : %d\r\n",res);
}
printf(" write file success!!! writen count: %d \r\n",byteswritten);
printf(" write Data : %s\r\n",wtext);
//4.关闭文件
res = f_close(&USBHFile);
}
}
printf(" read USB mass storage data.\r\n");
//5.打开文件
res = f_open(&USBHFile, "STM32_USB.txt", FA_READ); //打开文件,权限为只读
if(res != FR_OK)
{ //返回值不为0(出现问题)
Error_Handler();
printf(" open file error : %d\r\n",res); //打印问题代码
}
else
{
printf(" open file success!!! \r\n");
//6.读取txt文件数据
res = f_read(&USBHFile, rtext, sizeof(rtext), (UINT*)&bytesread);//读取文件内容放到rtext中
if(res)
{ //返回值不为0(出现问题)
Error_Handler();
printf(" read error!!! %d\r\n",res); //打印问题代码
}
else
{
printf(" read success!!! \r\n");
printf(" read Data : %s\r\n",rtext); //打印读取到的数据
}
//7.读写一致性检测
if(bytesread == byteswritten) //如果读写位数一致
{
printf(" bytesread == byteswritten,写入与读出数据一致。\r\n"); //打印文件系统工作正常
}
//8.关闭文件
res = f_close(&USBHFile);
}
#endif
#if 1
/*以下代码测试ok,可以向STM32_USB.txt中写入数据*/
/*如果要使用下面的测试代码,将#if 0 改为#if 1*/
if(f_mount(&USBHFatFS,(const TCHAR*)USBHPath,1) !=FR_OK)
{
//Fatfs Initialization Error
Error_Handler();
printf(" mount USB fail!!! \r\n");
}
else
{
printf(" mount USB success!!! \r\n");
if(f_open(&USBHFile, "STM32_USB.txt", FA_CREATE_ALWAYS | FA_WRITE) !=FR_OK)
{
// "STM32_USB.txt" file open for write error
Error_Handler();
printf(" open usb file fail!!! \r\n");
}
else
{
printf(" open usb file success!!! \r\n");
//write data to the text file
res = f_write(&USBHFile, wtext, sizeof(wtext), (void *)&byteswritten);
if((byteswritten==0) || (res != FR_OK)){
Error_Handler();
printf(" write usb file fail!!! \r\n");
}
else
{
printf(" write usb file success!!! \r\n");
//close the open text file
f_close(&USBHFile);
}
printf(" operate USB file end \r\n");
}
}
#endif
}
void UsbTest(void)
{
switch(Appli_state)
{
case APPLICATION_READY:
MSC_Application();
Appli_state = APPLICATION_DISCONNECT;
break;
case APPLICATION_DISCONNECT:
f_mount(NULL, (const TCHAR*)"",0);
break;
default:
break;
}
}