zoukankan      html  css  js  c++  java
  • USB库STM32F0x2移植到STM32F070笔记

    1. 前言

    ST官方提供的USB库STM32F0x2_USB-FS-Device_LibV1.0.0 是基于标准库的,适用于STM32F0x2系列MCU,但是对于STM32F070来说,就需要稍作修改,本文就一直到STM32F070作一个笔记。

    2. 移植

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

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

    接下来修改device为STM32F070RB:

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

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

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

    对比它俩的时钟树:

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

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

    usb_bsp.c 的USB_BSP_Init函数内:

    [cpp] view plain copy
     
    1. RCC_HSEConfig(RCC_HSE_Bypass);  
    2.   
    3.   /* Wait till HSE is ready */  
    4.   while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET)  
    5.   {}  
    6.   
    7.   /*Config the PREDIV for RCC_CFGR2*/  
    8.   RCC_PREDIV1Config(RCC_PREDIV1_Div1);  
    9.     /*HSE/PREDIV selected as PLL input clock*/  
    10.   RCC_PLLConfig(RCC_PLLSource_PREDIV1,RCC_PLLMul_6);  
    11.   /* Enable PLL */  
    12.   RCC_PLLCmd(ENABLE);  
    13.   
    14.   /* Wait till PLL is ready */  
    15.   while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)  
    16.   {}  
    17.     /*use the PLLCLK as system input clock*/  
    18.   RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);  
    19.     /* Wait till PLL is used as system clock source */  
    20.     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)  
    21.     {  
    22.     }  
    23.   RCC_HCLKConfig(RCC_SYSCLK_Div1);  
    24.   RCC_PCLKConfig(RCC_HCLK_Div1);  
    25.   /* Configure USBCLK from PLL clock */  
    26.   RCC_USBCLKConfig(RCC_USBCLK_PLLCLK);  

    在usb_conf.h头文件中注释掉一些宏:

    [cpp] view plain copy
     
    1. //#include "stm32072b_eval.h"  
    2. ...  
    3. //#ifdef USE_STM32072B_EVAL  
    4.  /* When using STM32072B_EVAL board the internal pullup must be enabled */  
    5.  #define INTERNAL_PULLUP  
    6. //#endif  
    7. ...  
    8. //#define USB_DEVICE_LOW_PWR_MGMT_SUPPORT   //关掉低功耗管理  
    9. ...  
    10. //#define USB_CLOCK_SOURCE_CRS          //STM32F070下是没有CRS的  

    接下来整理一下systick:

    [cpp] view plain copy
     
    1. void SysTick_Handler(void)  
    2. {  
    3.   
    4. #if 0  
    5.     uint8_t buf[4] ={0,10,10,0};  
    6.     USBD_HID_SendReport (&USB_Device_dev,  
    7.                          buf,  
    8.                          4);  
    9. #endif  
    10. //#if 0  
    11. //  uint8_t *buf;  
    12. //  
    13. //  /* Get Joystick position */  
    14. //  buf = USBD_HID_GetPos();  
    15. //  
    16. //  /* Update the cursor position */  
    17. //  if((buf[1] != 0) ||(buf[2] != 0))  
    18. //  {  
    19. //    /* Send Report */  
    20. //    USBD_HID_SendReport (&USB_Device_dev,  
    21. //                         buf,  
    22. //                         4);  
    23. //  }  
    24. //#endif  
    25.   TimingDelay_Decrement();  
    26. }  

    这个是延时函数:

    [cpp] view plain copy
     
    1. void HAL_Delay(__IO uint32_t nTime)  
    2. {  
    3.   TimingDelay = nTime;  
    4.   
    5.   while(TimingDelay != 0);  
    6. }  
    7.   
    8. /** 
    9.   * @brief  Decrements the TimingDelay variable. 
    10.   * @param  None 
    11.   * @retval None 
    12.   */  
    13. void TimingDelay_Decrement(void)  
    14. {  
    15.   if (TimingDelay != 0x00)  
    16.   {  
    17.     TimingDelay--;  
    18.   }  
    19. }  

    修改下systick的间隔时间:

    在usbd_usr.c文件中的:

    [cpp] view plain copy
     
    1. void USBD_USR_Init(void)  
    2. {  
    3.   /* SysTick used for periodic check mouse position */  
    4.   SysTick_Config(SystemCoreClock /1000);  
    5. }  

    最后在main函数内定时发送HID消息:

    [cpp] view plain copy
     
    1. int main(void)  
    2. {  
    3.     uint8_t buf[4] ={0,10,10,0};  
    4.   /*!< At this stage the microcontroller clock setting is already configured, 
    5.        this is done through SystemInit() function which is called from startup 
    6.        file (startup_stm32f072.s) before to branch to application main. 
    7.        To reconfigure the default setting of SystemInit() function, refer to 
    8.        system_stm32f0xx.c file 
    9.       */  
    10.   
    11.   /* The Application layer has only to call USBD_Init to 
    12.   initialize the USB low level driver, the USB device library, the USB clock 
    13.   ,pins and interrupt service routine (BSP) to start the Library*/  
    14.   
    15.   USBD_Init(&USB_Device_dev,  
    16.             &USR_desc,  
    17.             &USBD_HID_cb,  
    18.             &USR_cb);  
    19.   
    20.   while (1)  
    21.   {  
    22. #if 1  
    23.       USBD_HID_SendReport (&USB_Device_dev,  
    24.                          buf,  
    25.                          4);  
    26.       //delay  
    27.       HAL_Delay(1000);  
    28. #endif  
    29.   }  
    30. }  

    这样代码部分就完成了,通过以上main函数的代码可知,我们是每隔1S向PC端发送一次鼠标消息,鼠标会向右下角移动10个像素。

    最后在NUCLEO板上测试OK!


    最终移植后的工程下载地址:http://download.csdn.net/detail/flydream0/9590631

    更多资源地址:www.makeru.com.cn/?t=12

  • 相关阅读:
    1084: 计算两点间的距离(多实例测试)
    1083: 数值统计(多实例测试)
    回文数的判断
    1082: 敲7(多实例测试)
    BZOJ 1303 [CQOI2009]中位数图
    计蒜客 Yingchuan Online F题 (Floyd 变形)
    并查集入门题
    常见错点
    单调队列入门题
    线段树入门题
  • 原文地址:https://www.cnblogs.com/huan-huan/p/8616828.html
Copyright © 2011-2022 走看看