STM32--USB详细使用说明
硬件原理图
在开始枚举设备的一些初始化
void bsp_USBInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_USB_PULL_UP, ENABLE);
USB_CABLE_DISABLE();
GPIO_InitStructure.GPIO_Pin = PIN_USB_PULL_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPrio
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
USB_Init();
}
现在开始分析真正的初始化
第一步:初始化,总线复位及向默认地址 0发送 GET_DESCRIPTOR 指令包,请求设备描述
1)Index[4 - 5]:表示 USB插入总线复位;
2)Index[7 - 8]:表示主机向默认地址发送GET_DESCRIPTOR指令包,详细信
息也抓出来了,如(图二)所示
3)Index[15 - 17]:表示设备向主机发送设备描述数据 Index[16]
4)Index[18 - 19]:表示主机完成 GET_DESCRIPTOR指令后,给设备发送一个
空应答
现在具体的分析103的usb的执行过程 按顺序向下执行
***************(1)**************
DEVICE_INFO *pInformation;
DEVICE_PROP *pProperty;
DEVICE_PROP Device_Property =
{
Joystick_init,
Joystick_Reset,
Joystick_Status_In,
Joystick_Status_Out,
Joystick_Data_Setup,
Joystick_NoData_Setup,
Joystick_Get_Interface_Setting,
Joystick_GetDeviceDescriptor,
Joystick_GetConfigDescriptor,
Joystick_GetStringDescriptor,
0,
0x40
};
USER_STANDARD_REQUESTS User_Standard_Requests =
{
Joystick_GetConfiguration,
Joystick_SetConfiguration,
Joystick_GetInterface,
Joystick_SetInterface,
Joystick_GetStatus,
Joystick_ClearFeature,
Joystick_SetEndPointFeature,
Joystick_SetDeviceFeature,
Joystick_SetDeviceAddress
};
//USB内核将主机发送过来的用于实现USB设备的设置包保存在设备信息结构表中
typedef struct _DEVICE_INFO
{
uint8_t USBbmRequestType;
uint8_t USBbRequest;
uint16_t_uint8_t USBwValues;
uint16_t_uint8_t USBwIndexs;
uint16_t_uint8_t USBwLengths;
uint8_t ControlState;
uint8_t Current_Feature;
uint8_t Current_Configuration;
uint8_t Current_Interface;
uint8_t Current_AlternateSetting;
ENDPOINT_INFO Ctrl_Info;
}DEVICE_INFO;
usb_init.c文件里面的
void USB_Init(void)
{
pInformation = &Device_Info;
pInformation->ControlState = 2;
pProperty = &Device_Property;
pUser_Standard_Requests = &User_Standard_Requests;
pProperty->Init();
}
***************(2)**************通过函数指针指向这个初始化函数pProperty 在usb_prop.c文件里面
void Joystick_init(void)
{
Get_SerialNum();//得到串行号
pInformation->Current_Configuration = 0;//
PowerOn();//将USB上电 连接设备
USB_SIL_Init();//主要是CNTR寄存器的初始化
bDeviceState = UNCONNECTED;//设备状态标志 当前状态未连接
}
hw_config.c文件里面 这个和标准的不一样有改动,获取设备版本号,将其存入到版本号字符串。
void Get_SerialNum(void) //得到串行号
{
uint32_t Device_Serial0, Device_Serial1, Device_Serial2;
Device_Serial0 = *(__IO uint32_t*)(0x1FFFF7E8);
Device_Serial1 = *(__IO uint32_t*)(0x1FFFF7EC);
Device_Serial2 = *(__IO uint32_t*)(0x1FFFF7F0);
Device_Serial0 += Device_Serial2;
if (Device_Serial0 != 0)
{
IntToUnicode (Device_Serial0, &Joystick_StringSerial[2] , 8);
IntToUnicode (Device_Serial1, &Joystick_StringSerial[18], 4);
}
}
usb_pwr.c文件里面 在这个文件里面只是使能了复位,挂起,唤醒中断,在PowerOn函数使能了复位中断以后,将进入到USB的复位中断里面去。
然后再执行函数USB_SIL_Init 将所有的USB中断都打开。在D+被接通上拉以后,设备就能被主机检测到。
RESULT PowerOn(void)
{
#ifndef STM32F10X_CL
uint16_t wRegVal;
USB_Cable_Config(ENABLE);//将US
STM32USB使用说 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)