你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

NUCLEO-L496上手USB-host+fatfs打开文件时进入hardfault 并解决

[复制链接]
斜阳 提问时间:2017-3-30 17:59 /
本帖最后由 斜阳__ 于 2017-3-31 22:54 编辑

从论坛申请了一块NUCLEO-L496板子。今天到了。由于之前一直用ST的芯片。也玩过NUCLEO板。就直接测试usb-host+fatfs了。
使用CubeMX配置的工程。
    CubeMX版本为4.20,库版本为V1.7;

配置如下图
  首先是引脚配置
snipaste_20170330_173918.png

接着是usb_FS的配置     选择host_Only
snipaste_20170330_173947.png

同时还开启了一个串口3,这个就不上图了。
调试配置为SW
   snipaste_20170330_174000.png
   
  usb和fatfs的配置如下:
   snipaste_20170330_174042.png

以上就是功能配置。
具体的USB配置如下。usb-host和fatfs保持默认配置。
   snipaste_20170330_174057.png
堆栈设置如下:
    snipaste_20170330_174123.png
文件测试操作来自f429-disc的HAL库(v11.4版本)中的测试例程。

  1. static void MSC_Application(void)
  2. {
  3.   FRESULT res;                                          /* FatFs function common result code */
  4.   uint32_t byteswritten, bytesread;                     /* File write/read counts */
  5.   uint8_t wtext[] = "This is STM32 working with FatFs"; /* File write buffer */
  6.   uint8_t rtext[100];                                   /* File read buffer */
  7.   
  8.   /* Register the file system object to the FatFs module */
  9.   if(f_mount(&USBDISKFatFs, (TCHAR const*)USBH_Path, 0) != FR_OK)
  10.   {
  11.     /* FatFs Initialization Error */
  12.     Error_Handler();
  13.   }
  14.   else
  15.   {
  16.       /* Create and Open a new text file object with write access */
  17.       if(f_open(&MyFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
  18.       {
  19.         /* 'STM32.TXT' file Open for write Error */
  20.         Error_Handler();
  21.       }
  22.       else
  23.       {
  24.         /* Write data to the text file */
  25.         res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);
  26.         
  27.         if((byteswritten == 0) || (res != FR_OK))
  28.         {
  29.           /* 'STM32.TXT' file Write or EOF Error */
  30.           Error_Handler();
  31.         }
  32.         else
  33.         {
  34.           /* Close the open text file */
  35.           f_close(&MyFile);
  36.          
  37.         /* Open the text file object with read access */
  38.         if(f_open(&MyFile, "STM32.TXT", FA_READ) != FR_OK)
  39.         {
  40.           /* 'STM32.TXT' file Open for read Error */
  41.           Error_Handler();
  42.         }
  43.         else
  44.         {
  45.           /* Read data from the text file */
  46.           res = f_read(&MyFile, rtext, sizeof(rtext), (void *)&bytesread);
  47.          
  48.           if((bytesread == 0) || (res != FR_OK))
  49.           {
  50.             /* 'STM32.TXT' file Read or EOF Error */
  51.             Error_Handler();
  52.           }
  53.           else
  54.           {
  55.             /* Close the open text file */
  56.             f_close(&MyFile);
  57.             
  58.             /* Compare read data with the expected data */
  59.             if((bytesread != byteswritten))
  60.             {               
  61.               /* Read data is different from the expected data */
  62.               Error_Handler();
  63.             }
  64.             else
  65.             {
  66.               /* Success of the demo: no error occurrence */
  67.               //
  68.             }
  69.           }
  70.         }
  71.       }
  72.     }
  73.   }
  74.   
  75.   /* Unlink the USB disk I/O driver */
  76.   FATFS_UnLinkDriver(USBH_Path);
  77. }
复制代码
作为host需要给从设备供电。通过原理图
snipaste_20170330_175106.png snipaste_20170330_175145.png snipaste_20170330_175159.png

要使能PG6输出高电平接通电源供电。
so,进入while循环之前先使能电源;
  1.   /* Infinite loop */
  2.   /* USER CODE BEGIN WHILE */
  3.   HAL_GPIO_WritePin(GPIOG,GPIO_PIN_6,GPIO_PIN_SET);
  4.   while (1)
  5.   {
  6.   /* USER CODE END WHILE */
  7.     MX_USB_HOST_Process();

  8.   /* USER CODE BEGIN 3 */
  9.   /* Mass Storage Application State Machine */
  10.       switch(Appli_state)
  11.       {
  12.       case APPLICATION_START:
  13.         MSC_Application();
  14.         Appli_state = APPLICATION_IDLE;
  15.         break;
  16.         
  17.       case APPLICATION_IDLE:
  18.       default:
  19.         break;      
  20.       }
  21.   }
复制代码
工程建立到此结束。
但是呢,本贴既然是讨论帖就肯定是有问题的。
问题:挂上调试,运行。把u盘插入,挂载可以通过。但是会在打开/创建文件这一步卡住。并且进入Error_Handler。
在f_open函数中打断点发现可以进入到open函数,随后就进入Error_Handler.
具体原因正在寻找。在下面附上测试工程。在我继续Debug的同时欢迎任何人莅临指导。
usb_host.zip (6.07 MB, 下载次数: 7)
snipaste_20170330_174032.png
收藏 1 评论5 发布时间:2017-3-30 17:59

举报

5个回答
wenyangzeng 回答时间:2017-3-30 19:44:46
本帖最后由 wenyangzeng 于 2017-3-30 20:01 编辑

楼主:这片板子好像串口通讯是USART1,映像到PG7,PG8,连接到STLINK,不知道有没有关系。。 无标题.png

斜阳 回答时间:2017-3-30 21:36:30
本帖最后由 斜阳__ 于 2017-3-30 21:44 编辑
wenyangzeng 发表于 2017-3-30 19:44
楼主:这片板子好像串口通讯是USART1,映像到PG7,PG8,连接到STLINK,不知道有没有关系。。

...
snipaste_20170330_213537.png
我懵了,CubeMX给的提示是PD8和PD9
而且OverCurren也只是配置了一个输入,并没有实质上的功能。即使有过流信号也公道了PG5.并不会送到ST-Link这端。
yhz280627774-19 回答时间:2017-11-16 10:09:53
你好,我现在做usb,也会出现程序一直进Error_Handler内,状态一直是APPLICATION_START,我的vbus是直接接的电源5v,看见你解决这个问题了,请问是怎么解决的
彬~~ 回答时间:2017-11-16 14:05:10
学习学习!!
斜阳 回答时间:2017-11-16 15:07:54
yhz280627774-19 发表于 2017-11-16 10:09
你好,我现在做usb,也会出现程序一直进Error_Handler内,状态一直是APPLICATION_START,我的vbus是直接接 ...

这个要先查具体原因。看在哪儿出错的。

所属标签

相似问题

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版