USB庫STM32F0x2移植到STM32F070筆記

來源:互聯網
上載者:User

標籤:

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的時鐘樹,可只STM32F070是有一個內部48M的晶振,這個晶振是專門給USB提供時鐘的。


如是STM32F070的時鐘樹,對比STM32F072,發現STM32F070是沒有那個48M內部晶振的,因此在給USB提供晶振時,需要使用到外部晶振,於是,在代碼處找到設定晶振的代碼進行修改:

usb_bsp.c 的USB_BSP_Init函數內:

RCC_HSEConfig(RCC_HSE_Bypass);  /* Wait till HSE is ready */  while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET)  {}  /*Config the PREDIV for RCC_CFGR2*/  RCC_PREDIV1Config(RCC_PREDIV1_Div1);/*HSE/PREDIV selected as PLL input clock*/  RCC_PLLConfig(RCC_PLLSource_PREDIV1,RCC_PLLMul_6);  /* Enable PLL */  RCC_PLLCmd(ENABLE);  /* Wait till PLL is ready */  while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)  {}/*use the PLLCLK as system input clock*/  RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);/* Wait till PLL is used as system clock source */while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL){}  RCC_HCLKConfig(RCC_SYSCLK_Div1);  RCC_PCLKConfig(RCC_HCLK_Div1);  /* Configure USBCLK from PLL clock */  RCC_USBCLKConfig(RCC_USBCLK_PLLCLK);

在usb_conf.h標頭檔中注釋掉一些宏:

//#include "stm32072b_eval.h"...//#ifdef USE_STM32072B_EVAL /* When using STM32072B_EVAL board the internal pullup must be enabled */ #define INTERNAL_PULLUP//#endif...//#define USB_DEVICE_LOW_PWR_MGMT_SUPPORT   //關掉低功耗管理...//#define USB_CLOCK_SOURCE_CRS//STM32F070下是沒有CRS的

接下來整理一下systick:

void SysTick_Handler(void){#if 0uint8_t buf[4] ={0,10,10,0};USBD_HID_SendReport (&USB_Device_dev,                         buf,                         4);#endif//#if 0//  uint8_t *buf;////  /* Get Joystick position *///  buf = USBD_HID_GetPos();////  /* Update the cursor position *///  if((buf[1] != 0) ||(buf[2] != 0))//  {//    /* Send Report *///    USBD_HID_SendReport (&USB_Device_dev,//                         buf,//                         4);//  }//#endif  TimingDelay_Decrement();}

這個是延時函數:

void HAL_Delay(__IO uint32_t nTime){  TimingDelay = nTime;  while(TimingDelay != 0);}/**  * @brief  Decrements the TimingDelay variable.  * @param  None  * @retval None  */void TimingDelay_Decrement(void){  if (TimingDelay != 0x00)  {    TimingDelay--;  }}

修改下systick的間隔時間:

在usbd_usr.c檔案中的:

void USBD_USR_Init(void){  /* SysTick used for periodic check mouse position */  SysTick_Config(SystemCoreClock /1000);}

最好在main函數內定時發送HID訊息:

int main(void){uint8_t buf[4] ={0,10,10,0};  /*!< At this stage the microcontroller clock setting is already configured,       this is done through SystemInit() function which is called from startup       file (startup_stm32f072.s) before to branch to application main.       To reconfigure the default setting of SystemInit() function, refer to       system_stm32f0xx.c file      */  /* The Application layer has only to call USBD_Init to  initialize the USB low level driver, the USB device library, the USB clock  ,pins and interrupt service routine (BSP) to start the Library*/  USBD_Init(&USB_Device_dev,            &USR_desc,            &USBD_HID_cb,            &USR_cb);  while (1)  {#if 1  USBD_HID_SendReport (&USB_Device_dev,                         buf,                         4);  //delay  HAL_Delay(1000);#endif  }}

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

 

最後在NUCLEO板上測試OK!


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



USB庫STM32F0x2移植到STM32F070筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.