NUCLEO-L496上手USB-host+fatfs打开文件时进入hardfault 并解决
本帖最后由 斜阳__ 于 2017-3-31 22:54 编辑从论坛申请了一块NUCLEO-L496板子。今天到了。由于之前一直用ST的芯片。也玩过NUCLEO板。就直接测试usb-host+fatfs了。
使用CubeMX配置的工程。
CubeMX版本为4.20,库版本为V1.7;
配置如下图
首先是引脚配置
接着是usb_FS的配置 选择host_Only
同时还开启了一个串口3,这个就不上图了。
调试配置为SW
usb和fatfs的配置如下:
以上就是功能配置。
具体的USB配置如下。usb-host和fatfs保持默认配置。
堆栈设置如下:
文件测试操作来自f429-disc的HAL库(v11.4版本)中的测试例程。
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; /* File read buffer */
/* Register the file system object to the FatFs module */
if(f_mount(&USBDISKFatFs, (TCHAR const*)USBH_Path, 0) != FR_OK)
{
/* FatFs Initialization Error */
Error_Handler();
}
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 */
//
}
}
}
}
}
}
/* Unlink the USB disk I/O driver */
FATFS_UnLinkDriver(USBH_Path);
}
作为host需要给从设备供电。通过原理图
要使能PG6输出高电平接通电源供电。
so,进入while循环之前先使能电源;
/* Infinite loop */
/* USER CODE BEGIN WHILE */
HAL_GPIO_WritePin(GPIOG,GPIO_PIN_6,GPIO_PIN_SET);
while (1)
{
/* USER CODE END WHILE */
MX_USB_HOST_Process();
/* USER CODE BEGIN 3 */
/* Mass Storage Application State Machine */
switch(Appli_state)
{
case APPLICATION_START:
MSC_Application();
Appli_state = APPLICATION_IDLE;
break;
case APPLICATION_IDLE:
default:
break;
}
}
工程建立到此结束。
但是呢,本贴既然是讨论帖就肯定是有问题的。
问题:挂上调试,运行。把u盘插入,挂载可以通过。但是会在打开/创建文件这一步卡住。并且进入Error_Handler。
在f_open函数中打断点发现可以进入到open函数,随后就进入Error_Handler.
具体原因正在寻找。在下面附上测试工程。在我继续Debug的同时欢迎任何人莅临指导。
//-----------------------------------我是分割符--------------------------------------------
问题已经解决;
主要得感谢netlhx大大的例程指导。
MSC_Application();应该在APPLICATION_READY的状态下执行,而不是APPLICATION_START;
其次,这里要把电源控制加上。我是初始化的时候就直接拉高了不知道有影响没有。
USBH_StatusTypeDefUSBH_LL_DriverVBUS (USBH_HandleTypeDef *phost, uint8_t state)
{
/* USER CODE BEGIN 0 */
/* USER CODE END 0*/
if (phost->id == HOST_FS)
{
if (state == 0)
{
/* Drive high Charge pump */
/* ToDo: Add IOE driver control */
/* USER CODE BEGIN DRIVE_HIGH_CHARGE_FOR_FS */
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, GPIO_PIN_RESET);
/* USER CODE END DRIVE_HIGH_CHARGE_FOR_FS */
}
else
{
/* Drive low Charge pump */
/* ToDo: Add IOE driver control */
/* USER CODE BEGIN DRIVE_LOW_CHARGE_FOR_FS */
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, GPIO_PIN_SET);
/* USER CODE END DRIVE_LOW_CHARGE_FOR_FS */
}
}
HAL_Delay(200);
return USBH_OK;
}最后:上新版工程文件;
本帖最后由 wenyangzeng 于 2017-3-30 20:01 编辑
楼主:这片板子好像串口通讯是USART1,映像到PG7,PG8,连接到STLINK,不知道有没有关系。。
本帖最后由 斜阳__ 于 2017-3-30 21:44 编辑
wenyangzeng 发表于 2017-3-30 19:44
楼主:这片板子好像串口通讯是USART1,映像到PG7,PG8,连接到STLINK,不知道有没有关系。。
...
我懵了,CubeMX给的提示是PD8和PD9
而且OverCurren也只是配置了一个输入,并没有实质上的功能。即使有过流信号也公道了PG5.并不会送到ST-Link这端。
你好,我现在做usb,也会出现程序一直进Error_Handler内,状态一直是APPLICATION_START,我的vbus是直接接的电源5v,看见你解决这个问题了,请问是怎么解决的 学习学习!!:) yhz280627774-19 发表于 2017-11-16 10:09
你好,我现在做usb,也会出现程序一直进Error_Handler内,状态一直是APPLICATION_START,我的vbus是直接接 ...
这个要先查具体原因。看在哪儿出错的。
页:
[1]