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

NUCLEO_144-F767 USBHost简单例程

[复制链接]
斜阳 提问时间:2017-6-8 17:35 /
本帖最后由 斜阳__ 于 2017-6-8 17:35 编辑

基于CubeMX 4.21.0.
stm32F7库版本1.7.0
stm32f7的usb测试比之前f4/l4测试时候简单很多了.几乎所有代码都可以自动生成了。
usb配置:USB-OTG-FS:host_only
snipaste_20170608_165502.png
usb-host选择Mass Fatfs都选USB Disk
snipaste_20170608_165529.png
下面是时钟:
snipaste_20170608_165556.png
初始化顺序修改为  //usart一行可以忽略。
snipaste_20170608_170342.png

之后生成项目;


下面是代码修改:
在main文件中添加一下声明
  1. extern ApplicationTypeDef Appli_state;
  2. FATFS USBDISKFatFs;           /* File system object for USB disk logical drive */
  3. FIL MyFile;                   /* File object */
  4. static void MSC_Application(void);
复制代码

添加一下函数
  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.             //  BSP_LED_On(LED_GREEN);
  68.             }
  69.           }
  70.         }
  71.       }
  72.     }
  73.   }
  74.   
  75.   /* Unlink the USB disk I/O driver */
  76.   FATFS_UnLinkDriver(USBH_Path);
  77. }
复制代码

在while循环中添加:
  1. switch(Appli_state)
  2.       {
  3.       case APPLICATION_READY:
  4.         MSC_Application();
  5.         Appli_state = APPLICATION_IDLE;
  6.         break;
  7.          
  8.       case APPLICATION_IDLE:
  9.       default:
  10.         break;      
  11.       }
复制代码
之后就可以make执行了。查上u盘之后会创建STM32.TXT文件。并写相应内容。
如果一次不成功,请多尝试几次

上工程
usbKey.rar (6.26 MB, 下载次数: 39)
收藏 2 评论10 发布时间:2017-6-8 17:35

举报

10个回答
creep 回答时间:2017-6-8 20:44:55
沙发,学习了。。
Paderboy 回答时间:2017-6-8 22:00:22
搬个板凳学习。。。。
斜阳 回答时间:2017-6-8 22:03:23
creep 发表于 2017-6-8 20:44
沙发,学习了。。

大神莫要谦虚
斜阳 回答时间:2017-6-8 22:04:01
Paderboy 发表于 2017-6-8 22:00
搬个板凳学习。。。。

大神莫要谦虚
Bowen 回答时间:2017-6-9 09:50:17
感谢分享,学习了
wolfgang 回答时间:2017-6-9 10:41:15
排队学习~~~
weiwei4 回答时间:2017-7-5 11:58:43
学习学习,回去把板子测试下
枫天123 回答时间:2017-11-29 10:49:05
咦好奇怪,为什么我生成的代码只能检测到插入和断开,没有Ready呢?
枫天123 回答时间:2017-11-29 14:27:49
呵呵,搞定了,我的问题出在D+/D-短路了。。。
samhong 回答时间:2019-5-1 07:01:18
感谢分享!
关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版