在设备描述符的最后一个字节bNumConfigurations表明设备有几个配置描述符,从而主机能够从设备描述符得知需要获取几个配置描述符。一个标准的配置描述符只有9个字节
偏移量 | 名称 | 大小 | 说明 |
0 | bLength | 1 | 描述符长度(9字节) |
1 | bDescriptorType | 1 | 描述符类型(配置描述符为0x02) |
2 | wTotalLength | 2 | 配置描述符集合总长度 |
4 | bNumInterfaces | 1 | 配置支持的接口数 |
5 | bConfigurationValue | 1 | 配置的值 |
6 | iConfiguration | 1 | 描述该配置的字符串的索引值 |
7 | bmAttributes | 1 | 该设备的属性 |
8 | bMaxPower | 1 | 设备所需的电流(单位是2ma) |
wTotalLength: 配置描述符不会单独发送给USB主机,通常会以集合的形式发送给主机。集合通常包含:配置描述符,接口描述符,端点描述符,HID描述符。这几个描述符组成一个大数组。wTotalLength就是这个大数组的字节数。
bConfigurationValue:配置的值。一个USB设备可以有多个配置,配置值就是每个配置的标识。设置配置请求时,会发送配置值。如果发送的值和这里值一样,说明这个值被激活。
iConfiguration:描述该配置的字符串的索引值,如果没有字符串,那这个值就是0.
bmAttributes: 本设备的属性。D7是保留位,默认为1;D6表示供电方式,1是自供电,0是总线供电;D5表示是否支持远程唤醒;D4~D0保留,默认为0.
bMaxPower:设备需要的电流,单位是2ma。如果一个设备耗电量100ma,那么本子节就设置为50即可。
一个完整的集合描述符如下:
/* USB Configuration Descriptor */
/* All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */
const uint8_t usbDeviceConfigDescriptor[USB_CONFIG_DESC_LEN] =
{
0x09, /* bLength: Configuration Descriptor size */
USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType: Configuration */
USB_CONFIG_DESC_LEN,
/* wTotalLength: Bytes returned */
0x00,
0x01, /*bNumInterfaces: 1 interface*/
0x01, /*bConfigurationValue: Configuration value*/
0x00, /*iConfiguration: Index of string descriptor describing
the configuration*/
0xE0, /*bmAttributes: Self powered */
0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/
/************** Descriptor of Joystick Mouse interface ****************/
/* 09 */
0x09, /*bLength: Interface Descriptor size*/
USB_INTERFACE_DESCRIPTOR_TYPE,/*bDescriptorType: Interface descriptor type*/
0x00, /*bInterfaceNumber: Number of Interface*/
0x00, /*bAlternateSetting: Alternate setting*/
0x01, /*bNumEndpoints*/
0x03, /*bInterfaceClass: HID*/
0x01, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/
0x02, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/
0, /*iInterface: Index of string descriptor*/
/******************** Descriptor of Joystick Mouse HID ********************/
/* 18 */
0x09, /*bLength: HID Descriptor size*/
HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/
0x00, /*bcdHID: HID Class Spec release number*/
0x01,
0x00, /*bCountryCode: Hardware target country*/
0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/
0x22, /*bDescriptorType*/
USB_REPORT_DESC_LEN,/*wItemLength: Total length of Report descriptor*/
0x00,
/******************** Descriptor of Joystick Mouse endpoint ********************/
/* 27 */
0x07, /*bLength: Endpoint Descriptor size*/
USB_ENDPOINT_DESCRIPTOR_TYPE, /*bDescriptorType:*/
0x81, /*bEndpointAddress: Endpoint Address (IN)*/
0x03, /*bmAttributes: Interrupt endpoint*/
0x04, /*wMaxPacketSize: 4 Byte max */
0x00,
0x20, /*bInterval: Polling Interval (32 ms)*/
/* 34 */
};
结构Device_Property获取配置描述符的函数是usbClassGetConfigDescriptor
ONE_DESCRIPTOR stUsbConfigDescriptor =
{
(uint8_t*)usbDeviceConfigDescriptor,
USB_CONFIG_DESC_LEN
};
uint8_t *usbClassGetConfigDescriptor(uint16_t Length)
{
return Standard_GetDescriptorData(Length, &stUsbConfigDescriptor);
}