终于回到了熟悉的岗位。利用STM32系列单片机开发USBHid类设备及Joystick。最开始先来研读程序,近期会随着读程序随笔写出每个函数的分析。下边先说第一部分。
int main(void)
{
uint8_t i = 0;
/* SysTick end of count event each 10ms */
RCC_GetClocksFreq(&RCC_Clocks);
SysTick_Config(RCC_Clocks.HCLK_Frequency / 100);
/* Configure the USB */
USB_Config();
KEY_Init();
while (1)
{
Mouse_Buffer = USBD_HID_GetPos();
}
}
/**
* @brief Configure the USB.
* @param None
* @retval None
*/
void USB_Config(void)
{
Set_System(); --------> 1
Set_USBClock(); --------> 2
USB_Interrupts_Config(); --------> 3
USB_Init(); --------> 4 // while (bDeviceState != CONFIGURED)
// {}
}
1. Set_System() 位于USB_Example\hw_config.c文件中。其主要功能是初始化单片机时钟系统、使能相关的外设电源。
/**
* @brief Configures Main system clocks & power.
* @param None
* @retval None
*/
void Set_System(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f30x.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f30x.c file
*/ /* Enable the PWR clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); --------> 1.1 /* Enable the SYSCFG module clock (used for the USB disconnect feature) */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); --------> 1.2 /* Enable the USB disconnect GPIO clock */
// RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIO_DISCONNECT, ENABLE); /*Set PA11,12 as IN - USB_DM,DP*/
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); --------> 1.3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure); -------- 1.4
/*SET PA11,12 for USB: USB_DM,DP*/
GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_14); --------> 1.5
GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_14); --------> 1.5
#if defined(USB_USE_EXTERNAL_PULLUP)
/* Enable the USB disconnect GPIO clock */
// RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIO_DISCONNECT, ENABLE); // /* USB_DISCONNECT used as USB pull-up */
// GPIO_InitStructure.GPIO_Pin = USB_DISCONNECT_PIN;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
// GPIO_Init(USB_DISCONNECT, &GPIO_InitStructure);
#endif /* USB_USE_EXTERNAL_PULLUP */
/* Configure the EXTI line 18 connected internally to the USB IP */
EXTI_ClearITPendingBit(EXTI_Line18); --------> 1.6
EXTI_InitStructure.EXTI_Line = EXTI_Line18; /*USB resume from suspend mode*/
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); --------> 1.7
EXTI_ClearITPendingBit(USER_BUTTON_EXTI_LINE); --------> 1.6
}
|