找到StartDefaultTask函数,写如下代码测试U盘读取
void StartDefaultTask(void const * argument)
{
/* init code for FATFS */
MX_FATFS_Init();
/* init code for USB_HOST */
MX_USB_HOST_Init();
/* USER CODE BEGIN 5 */
osDelay(1000); //!!必须要有这个延时,不然f_mount不会成功
int res = f_mount(&USBHFatFS,USBHPath,1);//挂载U盘
//USBHFatFS、USBHPath这两个参数系统在fatfs.c里面已经定义好了,直接用
if(res == FR_OK)
{
printf("f_mount SUCCESSFUL: %s\r\n",USBHPath);
}
else
{
printf("f_mount ERROR: %d\r\n",res);
}
osDelay(1000);
res = f_open(&USBHFile,"0:/123.txt",FA_READ);//打开文件
if(res == FR_OK)
{
printf("f_open SUCCESSFUL\r\n");
}
else
{
printf("f_open ERROR: %d\r\n",res);
}
char buffer[10] = { 0 };
res = f_read(&USBHFile,buffer,10,NULL);
printf("f_read buffer = %s\r\n",buffer);
/* Infinite loop */
for(;;)
{
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_RESET);
osDelay(500);
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_SET);
osDelay(500);
if(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_3) == 0)
break;
}
/* USER CODE END 5 */
}
|