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函數內:
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 0
uint8_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