(USB HID) Configuration Descriptor
最近完成了HID的基本收發,使用的配置用了2個Endpoint,把一些特別重要要的地方紀錄下來
整個Configuration 分成4大部分 :
1. Configuration
2. Interface
3. HID descriptor
4. Endpoint
以下分散開來記錄,首先紀錄Configurations
1 #define USB_CONFIGURATION_DESCRIPTOR_TYPE 0x02 2 #define USB_HID_CONFIG_DESC_SIZ 41 3 4 0x09, /* bLength: Configuration Descriptor size */ 5 USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType: Configuration */ 6 USB_HID_CONFIG_DESC_SIZ, /* wTotalLength: Bytes returned */ 7 0x00, 8 0x01, /*bNumInterfaces: 1 interface*/ 9 0x01, /*bConfigurationValue: Configuration value*/ 10 0x00, /*iConfiguration: Index of string descriptor describing the configuration*/ 11 0x80, /*bmAttributes: bus powered and Support Remote Wake-up */ 12 0xFA, /*MaxPower 100 mA: this current is used for detecting Vbus*/
Interface Descriptor,這邊interface class要配成 HID, 而且protocol不能配成keyboard & mouse會影響收發
1 0x09, /*bLength: Interface Descriptor size*/ 2 USB_INTERFACE_DESCRIPTOR_TYPE,/*bDescriptorType: Interface descriptor type*/ 3 0x00, /*bInterfaceNumber: Number of Interface*/ 4 0x00, /*bAlternateSetting: Alternate setting*/ 5 0x02, /*bNumEndpoints*/ 6 0x03, /*bInterfaceClass: HID*/ 7 0x00, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/ 8 0x00, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ 9 0x04, /*iInterface: Index of string descriptor || origianl = 0x04*/
在來是HID descriptor
1 0x09, /*bLength: HID Descriptor size*/ 2 HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/ 3 0x00, /*bcdHID: HID Class Spec release number*/ 4 0x02, 5 0x00, /*bCountryCode: Hardware target country*/ 6 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ 7 0x22, /*bDescriptorType*/ 8 CUSTOMHID_SIZ_REPORT_DESC,/*wItemLength: Total length of Report descriptor*/ 9 0x00,
最後就是Endpoint Descriptor,這部分重複性也非常高,但是也是收發的關鍵。
1 0x07, /*bLength: Endpoint Descriptor size*/ 2 USB_ENDPOINT_DESCRIPTOR_TYPE, /*bDescriptorType:*/ 3 HID_IN_EP, /*bEndpointAddress: Endpoint Address (IN)*/ 4 0x03, /*bmAttributes: Interrupt endpoint*/ 5 HID_IN_PACKET, /*wMaxPacketSize: 4 Byte max */ 6 0x00, 7 0x0a, /*bInterval: Polling Interval (10 ms)*/ 8 /* 34 */ 9 0x07, /*bLength: Endpoint Descriptor size*/ 10 USB_ENDPOINT_DESCRIPTOR_TYPE, /*bDescriptorType:*/ 11 HID_OUT_EP, /*bEndpointAddress: Endpoint Address (OUT)*/ 12 0x03, /*bmAttributes: Interrupt endpoint*/ 13 HID_OUT_PACKET, /*wMaxPacketSize: 4 Byte max */ 14 0x00, 15 0x0a, /*bInterval: Polling Interval (10 ms)*/
整段Configuration Descriptor
1 __ALIGN_BEGIN static uint8_t USBD_HID_CfgDesc[USB_HID_CONFIG_DESC_SIZ] __ALIGN_END = 2 { 3 0x09, /* bLength: Configuration Descriptor size */ 4 USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType: Configuration */ 5 USB_HID_CONFIG_DESC_SIZ, /* wTotalLength: Bytes returned */ 6 0x00, 7 0x01, /*bNumInterfaces: 1 interface*/ 8 0x01, /*bConfigurationValue: Configuration value*/ 9 0x00, /*iConfiguration: Index of string descriptor describing the configuration*/ 10 0x80, /*bmAttributes: bus powered and Support Remote Wake-up */ 11 0xFA, /*MaxPower 100 mA: this current is used for detecting Vbus*/ 12 13 /************** Descriptor of Joystick Mouse interface ****************/ 14 /* 09 */ 15 0x09, /*bLength: Interface Descriptor size*/ 16 USB_INTERFACE_DESCRIPTOR_TYPE,/*bDescriptorType: Interface descriptor type*/ 17 0x00, /*bInterfaceNumber: Number of Interface*/ 18 0x00, /*bAlternateSetting: Alternate setting*/ 19 0x02, /*bNumEndpoints*/ 20 0x03, /*bInterfaceClass: HID*/ 21 0x00, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/ 22 0x00, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ 23 0x04, /*iInterface: Index of string descriptor || origianl = 0x04*/ 24 /******************** Descriptor of Joystick Mouse HID ********************/ 25 /* 18 */ 26 0x09, /*bLength: HID Descriptor size*/ 27 HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/ 28 0x00, /*bcdHID: HID Class Spec release number*/ 29 0x02, 30 0x00, /*bCountryCode: Hardware target country*/ 31 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ 32 0x22, /*bDescriptorType*/ 33 CUSTOMHID_SIZ_REPORT_DESC,/*wItemLength: Total length of Report descriptor*/ 34 0x00, 35 /******************** Descriptor of Mouse endpoint ********************/ 36 /* 27 */ 37 0x07, /*bLength: Endpoint Descriptor size*/ 38 USB_ENDPOINT_DESCRIPTOR_TYPE, /*bDescriptorType:*/ 39 HID_IN_EP, /*bEndpointAddress: Endpoint Address (IN)*/ 40 0x03, /*bmAttributes: Interrupt endpoint*/ 41 HID_IN_PACKET, /*wMaxPacketSize: 4 Byte max */ 42 0x00, 43 0x0a, /*bInterval: Polling Interval (10 ms)*/ 44 /* 34 */ 45 0x07, /*bLength: Endpoint Descriptor size*/ 46 USB_ENDPOINT_DESCRIPTOR_TYPE, /*bDescriptorType:*/ 47 HID_OUT_EP, /*bEndpointAddress: Endpoint Address (OUT)*/ 48 0x03, /*bmAttributes: Interrupt endpoint*/ 49 HID_OUT_PACKET, /*wMaxPacketSize: 4 Byte max */ 50 0x00, 51 0x0a, /*bInterval: Polling Interval (10 ms)*/ 52 };