1. 程式人生 > >USB庫STM32F0x2移植到STM32F070筆記

USB庫STM32F0x2移植到STM32F070筆記

cte pbo pla ifd ret don cor 網上 部分

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. /* Wait till HSE is ready */
  3. while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET)
  4. {}
  5. /*Config the PREDIV for RCC_CFGR2*/
  6. RCC_PREDIV1Config(RCC_PREDIV1_Div1);
  7. /*HSE/PREDIV selected as PLL input clock*/
  8. RCC_PLLConfig(RCC_PLLSource_PREDIV1,RCC_PLLMul_6);
  9. /* Enable PLL */
  10. RCC_PLLCmd(ENABLE);
  11. /* Wait till PLL is ready */
  12. while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  13. {}
  14. /*use the PLLCLK as system input clock*/
  15. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  16. /* Wait till PLL is used as system clock source */
  17. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
  18. {
  19. }
  20. RCC_HCLKConfig(RCC_SYSCLK_Div1);
  21. RCC_PCLKConfig(RCC_HCLK_Div1);
  22. /* Configure USBCLK from PLL clock */
  23. 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. #if 0
  4. uint8_t buf[4] ={0,10,10,0};
  5. USBD_HID_SendReport (&USB_Device_dev,
  6. buf,
  7. 4);
  8. #endif
  9. //#if 0
  10. // uint8_t *buf;
  11. //
  12. // /* Get Joystick position */
  13. // buf = USBD_HID_GetPos();
  14. //
  15. // /* Update the cursor position */
  16. // if((buf[1] != 0) ||(buf[2] != 0))
  17. // {
  18. // /* Send Report */
  19. // USBD_HID_SendReport (&USB_Device_dev,
  20. // buf,
  21. // 4);
  22. // }
  23. //#endif
  24. TimingDelay_Decrement();
  25. }

這個是延時函數:

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

修改下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. /* The Application layer has only to call USBD_Init to
  11. initialize the USB low level driver, the USB device library, the USB clock
  12. ,pins and interrupt service routine (BSP) to start the Library*/
  13. USBD_Init(&USB_Device_dev,
  14. &USR_desc,
  15. &USBD_HID_cb,
  16. &USR_cb);
  17. while (1)
  18. {
  19. #if 1
  20. USBD_HID_SendReport (&USB_Device_dev,
  21. buf,
  22. 4);
  23. //delay
  24. HAL_Delay(1000);
  25. #endif
  26. }
  27. }

這樣代碼部分就完成了,通過以上main函數的代碼可知,我們是每隔1S向PC端發送一次鼠標消息,鼠標會向右下角移動10個像素。

最後在NUCLEO板上測試OK!


最終移植後的工程下載地址:http://download.csdn.net/detail/flydream0/9590631

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

USB庫STM32F0x2移植到STM32F070筆記