直接使用官方例程,有如下注意点:
1、USB的时钟是48MHz,需要根据自己的系统主频来分频。否则时钟频率不对,USB会枚举失败,电脑提示设备描述符获取失败。
在如下文件和位置设置宏定义来选择系统主频。
然后根据自己的主频来设置分频:
/*!
\brief configure the different system clocks
\param[in] none
\param[out] none
\retval none
*/
void rcu_config(void)
{
/* enable USB pull-up pin clock */
rcu_periph_clock_enable(RCU_GPIOA);
/* enable the power clock */
rcu_periph_clock_enable(RCU_PMU);
/* configure USB model clock from PLL clock */
rcu_usb_clock_config(RCU_CKUSB_CKPLL_DIV2); //主频96M,二分频就是48M
/* enable USB APB1 clock */
rcu_periph_clock_enable(RCU_USBD);
}
如上,主频是96M,二分频就是48M。
如果主频是72M,就是1.5分频。
/* use HXTAL (XD series CK_HXTAL = 8M, CL series CK_HXTAL = 25M) */
//#define __SYSTEM_CLOCK_HXTAL (uint32_t)(__HXTAL)
//#define __SYSTEM_CLOCK_24M_PLL_HXTAL (uint32_t)(24000000)
//#define __SYSTEM_CLOCK_36M_PLL_HXTAL (uint32_t)(36000000)
//#define __SYSTEM_CLOCK_48M_PLL_HXTAL (uint32_t)(48000000)
//#define __SYSTEM_CLOCK_56M_PLL_HXTAL (uint32_t)(56000000)
#define __SYSTEM_CLOCK_72M_PLL_HXTAL (uint32_t)(72000000)
//#define __SYSTEM_CLOCK_96M_PLL_HXTAL (uint32_t)(96000000)
//#define __SYSTEM_CLOCK_108M_PLL_HXTAL (uint32_t)(108000000)
/* enable USB pull-up pin clock */
rcu_periph_clock_enable(RCC_AHBPeriph_GPIO_PULLUP);
/* enable the power clock */
rcu_periph_clock_enable(RCU_PMU);
/* configure USB model clock from PLL clock */
rcu_usb_clock_config(RCU_CKUSB_CKPLL_DIV1_5);
/* enable USB APB1 clock */
rcu_periph_clock_enable(RCU_USBD);
|