STM32-USB-HID复合设备实现方法

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

STM32USB-HID复合设备(鼠标+键盘双接口)的实现方法Author:卢晓铭联系方法:lsj9383@163.com1.首先更改配置描述符,在usb_desc.c中.这是我使用的配置描述符,第一个接口是键盘功能,第二个接口是鼠标功能constu8Joystick_ConfigDescriptor[JOYSTICK_SIZ_CONFIG_DESC]={/***************配置描述符***********************/0x09,//bLength字段USB_CONFIGURATION_DESCRIPTOR_TYPE,//bDescriptorType字段//wTotalLength字段JOYSTICK_SIZ_CONFIG_DESC,/*wTotalLength:Bytesreturned*/0x00,0x02,//bNumInterfaces字段0x01,//bConfiguration字段0x00,//iConfigurationz字段0x80,//bmAttributes字段0x32,//bMaxPower字段/*******************第一个接口描述符*********************/0x09,//bLength字段0x04,//bDescriptorType字段0x00,//bInterfaceNumber字段0x00,//bAlternateSetting字段0x02,//bNumEndpoints字段0x03,//bInterfaceClass字段0x01,//bInterfaceSubClass字段0x01,//bInterfaceProtocol字段0x00,//iConfiguration字段/******************HID描述符************************/0x09,//bLength字段0x21,//bDescriptorType字段0x10,//bcdHID字段0x01,0x21,//bCountyCode字段0x01,//bNumDescriptors字段0x22,//bDescriptorType字段//bDescriptorLength字段。//下级描述符的长度。下级描述符为键盘报告描述符。sizeof(KeyboardReportDescriptor)&0xFF,(sizeof(KeyboardReportDescriptor)8)&0xFF,/**********************输入端点描述符***********************/0x07,//bLength字段0x05,//bDescriptorType字段0x81,//bEndpointAddress字段0x03,//bmAttributes字段0x10,//wMaxPacketSize字段0x00,0x0A,//bInterval字段/**********************输出端点描述符***********************/0x07,//bLength字段0x05,//bDescriptorType字段0x01,//bEndpointAddress字段0x03,//bmAttributes字段0x10,//wMaxPacketSize字段0x00,0x0A,//bInterval字段/*******************第二个接口描述符*********************/0x09,//bLength字段0x04,//bDescriptorType字段0x01,//bInterfaceNumber字段0x00,//bAlternateSetting字段0x01,//bNumEndpoints字段0x03,//bInterfaceClass字段0x01,//bInterfaceSubClass字段0x02,//bInterfaceProtocol字段0x00,//iConfiguration字段/******************HID描述符************************/0x09,//bLength字段0x21,//bDescriptorType字段0x10,//bcdHID字段0x01,0x21,//bCountyCode字段0x01,//bNumDescriptors字段0x22,//bDescriptorType字段sizeof(MouseReportDescriptor)&0xFF,//bDescriptorLength字段(sizeof(MouseReportDescriptor)8)&0xFF,/**********************输入端点描述符***********************/0x07,//bLength字段0x05,//bDescriptorType字段0x82,//bEndpointAddress字段0x03,//bmAttributes字段。D1~D0为端点传输类型选择0x40,//wMaxPacketSize字段0x00,0x0A//bInterval字段};2.写键盘和鼠标报告描述符//USB键盘报告描述符的定义constu8KeyboardReportDescriptor[KP_ReportDescriptor_Size]={0x05,0x01,//USAGE_PAGE(GenericDesktop)//630x09,0x06,//USAGE(Keyboard)0xa1,0x01,//COLLECTION(Application)0x05,0x07,//USAGE_PAGE(Keyboard)0x19,0xe0,//USAGE_MINIMUM(KeyboardLeftControl)0x29,0xe7,//USAGE_MAXIMUM(KeyboardRightGUI)0x15,0x00,//LOGICAL_MINIMUM(0)0x25,0x01,//LOGICAL_MAXIMUM(1)0x75,0x01,//REPORT_SIZE(1)0x95,0x08,//REPORT_COUNT(8)0x81,0x02,//INPUT(Data,Var,Abs)0x95,0x01,//REPORT_COUNT(1)0x75,0x08,//REPORT_SIZE(8)0x81,0x03,//INPUT(Cnst,Var,Abs)0x95,0x05,//REPORT_COUNT(5)0x75,0x01,//REPORT_SIZE(1)0x05,0x08,//USAGE_PAGE(LEDs)0x19,0x01,//USAGE_MINIMUM(NumLock)0x29,0x05,//USAGE_MAXIMUM(Kana)0x91,0x02,//OUTPUT(Data,Var,Abs)0x95,0x01,//REPORT_COUNT(1)0x75,0x03,//REPORT_SIZE(3)0x91,0x03,//OUTPUT(Cnst,Var,Abs)0x95,0x06,//REPORT_COUNT(6)0x75,0x08,//REPORT_SIZE(8)0x15,0x00,//LOGICAL_MINIMUM(0)0x25,0x65,//LOGICAL_MAXIMUM(101)0x05,0x07,//USAGE_PAGE(Keyboard)0x19,0x00,//USAGE_MINIMUM(Reserved(noeventindicated))0x29,0x65,//USAGE_MAXIMUM(KeyboardApplication)0x81,0x00,//INPUT(Data,Ary,Abs)0xc0,//END_COLLECTION//0xc0,};/*Joystick_ReportDescriptor*////////////////////////////键盘报告描述符完毕//////////////////////////////USB鼠标报告描述符的定义constu8MouseReportDescriptor[Mouse_ReportDescriptor_Size]={0x05,0x01,//USAGE_PAGE(GenericDesktop)0x09,0x02,//USAGE(Mouse)0xa1,0x01,//COLLECTION(Application)0x85,0x01,//ReportID(1)0x09,0x01,//USAGE(Pointer)0xa1,0x00,//COLLECTION(Physical)0x05,0x09,//USAGE_PAGE(Button)0x19,0x01,//USAGE_MINIMUM(Button1)0x29,0x03,//USAGE_MAXIMUM(Button3)0x15,0x00,//LOGICAL_MINIMUM(0)0x25,0x01,//LOGICAL_MAXIMUM(1)0x95,0x03,//REPORT_COUNT(3)0x75,0x01,//REPORT_SIZE(1)0x81,0x02,//INPUT(Data,Var,Abs)0x95,0x01,//REPORT_COUNT(1)0x75,0x05,//REPORT_SIZE(5)0x81,0x03,//INPUT(Cnst,Var,Abs)0x05,0x01,//USAGE_PAGE(GenericDesktop)0x09,0x30,//USAGE(X)0x09,0x31,//USAGE(Y)0x09,0x38,//USAGE(Wheel)0x15,0x81,//LOGICAL_MINIMUM(-127)0x25,0x7f,//LOGICAL_MAXIMUM(127)0x75,0x08,//REPORT_SIZE(8)0x95,0x03,//REPORT_COUNT(3)0x81,0x06,//INPUT(Data,Var,Rel)0xc0,//END_COLLECTION0xc0//END_COLLECTION};以上是usb_desc.c中的修改,接着是需要修改usb_desc.h中的宏定义3.由于我们用了新的配置描述符,所以,我们需要重新定义配置描述符的大小#defineJOYSTICK_SIZ_CONFIG_DESC664.由于我们用了键盘鼠标报告描述符,所以,我们需要定义键盘报告描述符的大小#defineKP_ReportDescriptor_Size63#defineMouse_ReportDescriptor_Size545.报告描述符的大小,但是我们还有增加键盘鼠标报告描述符的定义externconstu8KeyboardReportDescriptor[KP_ReportDescriptor_Size];externconstu8MouseReportDescriptor[Mouse_ReportDescriptor_Size];(如果有不用的报告描述符,就给删掉吧)6.好,接着让我们更改usb_prop.c中的代码,我们可以在usb_prop.c中找到类似一下的代码ONE_DESCRIPTORJoystick_Report_Descriptor=//用于获得报告描述符{(u8*)Joystick_ReportDescriptor,JOYSTICK_SIZ_REPORT_DESC};ONE_DESCRIPTORMouse_Hid_Descriptor=//用于获得配置描述符中的Hid描述{(u8*)Joystick_ConfigDescriptor+JOYSTICK_OFF_HID_DESC,//JOYSTICK_SIZ_HID_DESC};//JOYSTICK_OFF_HID_DESC是HID描述在配置描述符中的偏移量由于我们更改了报告描述符和配置描述符,所以该处应该修改,并且我们有两个报告描述符

1 / 11
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功