1. IO初始化
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//P12: USB DP, P11: USB DM
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
初始化USB Enable的IO口为输出脚
2. USB中断初始化
void usbIntInit(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* 2 bit for pre-emption priority, 2 bits for subpriority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* Enable the USB interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the USB Wake-up interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USBWakeUp_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
实际USB有3个中断,分别为USB_HP_CAN1_TX_IRQn(USB高优先级中断)、USB_LP_CAN1_RX0_IRQn(USB低优先级中断)和USBWakeUp_IRQn(USB唤醒中断)。
USB_HP_CAN1_TX_IRQn仅由USB同步(Isochronous)模式传输或双缓冲块(Bulk)传输模式下的正确传输事件产生,大部分的例子没有用到,先不考虑使用。
3. 设置USB频率
/* Select USBCLK source */
RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
/* Enable the USB clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
4. 调用USB库的API函数USB_Init()
此时编译会有错误提示,undefined reference to `Device_Property'和undefined reference to `User_Standard_Requests'
Device_Property是一个结构,参考例程Custom_HID中建立一个空的结构。
DEVICE_PROP Device_Property =
{
usbClassInit,
usbClassReset,
usbClassStatusIn,
usbClassStatusOut,
usbClassDataSetup,
usbClassNoDataSetup,
usbClassGetInterfaceSetting,
usbClassGetDeviceDescriptor,
usbClassGetConfigDescriptor,
usbClassGetStringDescriptor,
0,
0x40 /*MAX PACKET SIZE*/
};
所有处理的函数都是空的,例如
void usbClassInit(void)
{
}
同样对User_Standard_Requests
USER_STANDARD_REQUESTS User_Standard_Requests =
{
usbClassGetConfiguration,
usbClassSetConfiguration,
usbClassGetInterface,
usbClassSetInterface,
usbClassGetStatus,
usbClassClearFeature,
usbClassSetEndPointFeature,
usbClassSetDeviceFeature,
usbClassSetDeviceAddress
};
编译通过。
5. 在USB库的API函数USB_Init()中可以看到会调用Device_Property的usbClassInit。
参考例程JoyStickMouse中Joystick_init,看起来应该都是一样的处理,
void Joystick_init(void)
{
/* Update the serial number string descriptor with the data from the unique ID*/
Get_SerialNum();
pInformation->Current_Configuration = 0;
/* Connect the device */
PowerOn();
/* Perform basic device initialization operations */
USB_SIL_Init();
bDeviceState = UNCONNECTED;
}
第一步是根据MCU的96位ID生成唯一的Serial Number,然后PowerOn,PowerOn里面会控制USB_EN脚,USB_SIL_Init是初始化端点(EndPoint)的读写。
1. PowerOn是USB IP的上电复位过程。
RESULT PowerOn(void)
{
uint16_t wRegVal;
/*** cable plugged-in ? ***/
USB_Cable_Config(ENABLE);
/*** CNTR_PWDN = 0 ***/
wRegVal = CNTR_FRES;
_SetCNTR(wRegVal);
/* The following sequence is recommended:
1- FRES = 0
2- Wait until RESET flag = 1 (polling)
3- clear ISTR register */
/*** CNTR_FRES = 0 ***/
wInterrupt_Mask = 0;
_SetCNTR(wInterrupt_Mask);
/* Wait until RESET flag = 1 (polling) */
while((_GetISTR()&ISTR_RESET) == 1);
/*** Clear pending interrupts ***/
SetISTR(0);
/*** Set interrupt mask ***/
wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM;
_SetCNTR(wInterrupt_Mask);
return USB_SUCCESS;
}
USB_CNTR是USB控制寄存器(),USB_ISTR是USB中断状态寄存器。
a) PDWN = 0, 退出断电模式;FRES = 1, 强制复位
wRegVal = CNTR_FRES;
_SetCNTR(wRegVal);
wInterrupt_Mask = 0;
_SetCNTR(wInterrupt_Mask);
b) 等待复位结束
while((_GetISTR()&ISTR_RESET) == 1);
c) 清除USB中断
SetISTR(0);
d) 设置USB中断使能,CNTR_WKUPM(唤醒请求)/ CNTR_SUSPM(挂起模块请求)/ CNTR_RESETM(复位中断)
wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM;
_SetCNTR(wInterrupt_Mask);
2. USB_SIL_Init是初始化USB IP和端点0
uint32_t USB_SIL_Init(void)
{
/* USB interrupts initialization */
/* clear pending interrupts */
_SetISTR(0);
wInterrupt_Mask = IMR_MSK;
/* set interrupts mask */
_SetCNTR(wInterrupt_Mask);
return 0;
}
a) 清除所有中断
_SetISTR(0);
b)设置中断源
wInterrupt_Mask = IMR_MSK;
_SetCNTR(wInterrupt_Mask);
#define IMR_MSK (CNTR_CTRM | CNTR_WKUPM | CNTR_SUSPM | CNTR_ERRM | CNTR_SOFM \
| CNTR_ESOFM | CNTR_RESETM )
设置到这里USB的中断处理函数USB_LP_CAN1_RX0_IRQn已经可以进了。