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

USB库STM32F0x2移植到STM32F070笔记

[复制链接]
aimejia 发布时间:2018-5-23 13:17
1. 前言
ST官方提供的USB库STM32F0x2_USB-FS-Device_LibV1.0.0 是基于标准库的,适用于STM32F0x2系列MCU,但是对于STM32F070来说,就需要稍作修改,本文就一直到STM32F070作一个笔记。

2. 移植
从STM中文官网上下载STM32F0x2 USB库,地址:https://www.stmcu.org.cn/document/detail/index/id-214961。用MDK打开,首先在Manager Project Items下的Project Targets下新增一项 “STM32F070”:

1.png

然后切换到”STM32F070”这个Target: 。此后对所有工程属性的修改都会使用于“STM32F070”,而不再是原先的“USBD_HID-STM32072B-EVAL”了。

接下来修改device为STM32F070RB:

2.png

工程配置弄好了后,接下来我们来修改代码部分。

首先我们来编译一下工程,发现此时是可以编译通过的。但是烧录到STM32F070的板子里(这里使用ST的NUCLEO-F070RB板)去时却不能成功运行。

STM32F072与STM32F070这两个MCU都有USB,且此IP没有什么不同,那么差异是什么呢?

对比它俩的时钟树:

3.png

如上图是STM32F072的时钟树,可知STM32F072是有一个内部48M的晶振,这个晶振是专门给USB提供时钟的。

4.png

如上图是STM32F070的时钟树,对比STM32F072,发现STM32F070是没有那个48M内部晶振的,因此在给USB提供晶振时,需要使用到外部晶振,于是,在代码处找到设置晶振的代码进行修改:

usb_bsp.c 的USB_BSP_Init函数内:
  1. [cpp] view plain copy
  2. RCC_HSEConfig(RCC_HSE_Bypass);  
  3.   
  4.   /* Wait till HSE is ready */  
  5.   while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET)  
  6.   {}  
  7.   
  8.   /*Config the PREDIV for RCC_CFGR2*/  
  9.   RCC_PREDIV1Config(RCC_PREDIV1_Div1);  
  10.     /*HSE/PREDIV selected as PLL input clock*/  
  11.   RCC_PLLConfig(RCC_PLLSource_PREDIV1,RCC_PLLMul_6);  
  12.   /* Enable PLL */  
  13.   RCC_PLLCmd(ENABLE);  
  14.   
  15.   /* Wait till PLL is ready */  
  16.   while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)  
  17.   {}  
  18.     /*use the PLLCLK as system input clock*/  
  19.   RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);  
  20.     /* Wait till PLL is used as system clock source */  
  21.     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)  
  22.     {  
  23.     }  
  24.   RCC_HCLKConfig(RCC_SYSCLK_Div1);  
  25.   RCC_PCLKConfig(RCC_HCLK_Div1);  
  26.   /* Configure USBCLK from PLL clock */  
  27.   RCC_USBCLKConfig(RCC_USBCLK_PLLCLK);
复制代码
在usb_conf.h头文件中注释掉一些宏:
  1. [cpp] view plain copy
  2. //#include "stm32072b_eval.h"  
  3. ...  
  4. //#ifdef USE_STM32072B_EVAL  
  5. /* When using STM32072B_EVAL board the internal pullup must be enabled */  
  6. #define INTERNAL_PULLUP  
  7. //#endif  
  8. ...  
  9. //#define USB_DEVICE_LOW_PWR_MGMT_SUPPORT   //关掉低功耗管理  
  10. ...  
  11. //#define USB_CLOCK_SOURCE_CRS          //STM32F070下是没有CRS的  
复制代码
接下来整理一下systick:
  1. [cpp] view plain copy
  2. void SysTick_Handler(void)  
  3. {  
  4.   
  5. #if 0  
  6.     uint8_t buf[4] ={0,10,10,0};  
  7.     USBD_HID_SendReport (&USB_Device_dev,  
  8.                          buf,  
  9.                          4);  
  10. #endif  
  11. //#if 0  
  12. //  uint8_t *buf;  
  13. //  
  14. //  /* Get Joystick position */  
  15. //  buf = USBD_HID_GetPos();  
  16. //  
  17. //  /* Update the cursor position */  
  18. //  if((buf[1] != 0) ||(buf[2] != 0))  
  19. //  {  
  20. //    /* Send Report */  
  21. //    USBD_HID_SendReport (&USB_Device_dev,  
  22. //                         buf,  
  23. //                         4);  
  24. //  }  
  25. //#endif  
  26.   TimingDelay_Decrement();  
  27. }  
复制代码
这个是延时函数:

  1. [cpp] view plain copy
  2. void HAL_Delay(__IO uint32_t nTime)  
  3. {  
  4.   TimingDelay = nTime;  
  5.   
  6.   while(TimingDelay != 0);  
  7. }  
  8.   
  9. /**
  10.   * @brief  Decrements the TimingDelay variable.
  11.   * @param  None
  12.   * @retval None
  13.   */  
  14. void TimingDelay_Decrement(void)  
  15. {  
  16.   if (TimingDelay != 0x00)  
  17.   {  
  18.     TimingDelay--;  
  19.   }  
  20. }
复制代码
修改下systick的间隔时间:

在usbd_usr.c文件中的:
  1. [cpp] view plain copy
  2. void USBD_USR_Init(void)  
  3. {  
  4.   /* SysTick used for periodic check mouse position */  
  5.   SysTick_Config(SystemCoreClock /1000);  
  6. }  
复制代码
最后在main函数内定时发送HID消息:
  1. [cpp] view plain copy
  2. int main(void)  
  3. {  
  4.     uint8_t buf[4] ={0,10,10,0};  
  5.   /*!< At this stage the microcontroller clock setting is already configured,
  6.        this is done through SystemInit() function which is called from startup
  7.        file (startup_stm32f072.s) before to branch to application main.
  8.        To reconfigure the default setting of SystemInit() function, refer to
  9.        system_stm32f0xx.c file
  10.       */  
  11.   
  12.   /* The Application layer has only to call USBD_Init to
  13.   initialize the USB low level driver, the USB device library, the USB clock
  14.   ,pins and interrupt service routine (BSP) to start the Library*/  
  15.   
  16.   USBD_Init(&USB_Device_dev,  
  17.             &USR_desc,  
  18.             &USBD_HID_cb,  
  19.             &USR_cb);  
  20.   
  21.   while (1)  
  22.   {  
  23. #if 1  
  24.       USBD_HID_SendReport (&USB_Device_dev,  
  25.                          buf,  
  26.                          4);  
  27.       //delay  
  28.       HAL_Delay(1000);  
  29. #endif  
  30.   }  
  31. }  
复制代码
这样代码部分就完成了,通过以上main函数的代码可知,我们是每隔1S向PC端发送一次鼠标消息,鼠标会向右下角移动10个像素。

最后在NUCLEO板上测试OK!






转载自FLYDREAM0

收藏 1 评论0 发布时间:2018-5-23 13:17

举报

0个回答

所属标签

相似分享

官网相关资源

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