Fork me on GitHub

USB协议详解第5讲(USB描述符-接口描述符)

1.USB描述符

USB描述符有设备描述符、标准配置描述符接口描述符端点描述符字符串描述符,HID设备有HID描述符报告描述符物理描述符今天主要是学习USB接口描述符的组成。

2.接口描述符组成

前面讲了设备描述符和标准配置描述符,本篇我们讲解接口描述符。首先要明确的一点是接口描述符不能单独返回给USB主机,主机会请求获得配置描述符集合,配置描述符集合主要由标准配置描述符、接口描述符、端点描述符、HID描述符,报告描述符和物理描述符单独返回给主机。接口描述符包含9个字节,组成如下:

3.STM32配置描述符集合代码(必须按照顺序)

/* USB Configuration Descriptor */
const uint8_t CustomHID_ConfigDescriptor[CUSTOMHID_SIZ_CONFIG_DESC] =
{
//
// 标准配置描述符
//	
    0x09, /* bLength: Configuation Descriptor size */
    USB_CONFIGURATION_DESCRIPTOR_TYPE, 	/* bDescriptorType: Configuration */
    CUSTOMHID_SIZ_CONFIG_DESC,			/* wTotalLength low : Bytes returned */
    0x00,								/* wTotalLength high: Bytes returned */
    0x01,         /* bNumInterfaces: 1 interface */
    0x01,         /* bConfigurationValue: Configuration value */
    0x00,         /* iConfiguration: Index of string descriptor describing the configuration*/
    0xC0,         /* bmAttributes: Bus powered */
                  /*Bus powered: 7th bit, Self Powered: 6th bit, Remote wakeup: 5th bit, reserved: 4..0 bits */
    0x96,         /* MaxPower 300 mA: this current is used for detecting Vbus */
    
//
// 接口描述符
//		
	/************** Descriptor of Custom HID interface ****************/
    /* 09 */
    0x09,         						/* bLength: Interface Descriptor size */
    USB_INTERFACE_DESCRIPTOR_TYPE,		/* bDescriptorType: Interface descriptor type */
    0x00,         /* bInterfaceNumber: Number of Interface */
    0x00,         /* bAlternateSetting: Alternate setting */
    0x02,         /* bNumEndpoints 此接口有两个端点 */
    0x03,         /* bInterfaceClass: HID */
    0x00,         /* bInterfaceSubClass : 1=BOOT, 0=no boot */
    0x00,         /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
    0,            /* iInterface: Index of string descriptor */
    
//
// HID描述符(后续讲解)
//		
	/******************** Descriptor of Custom HID HID ********************/
    /* 18 */
    0x09,         /* bLength: HID Descriptor size */
    HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */
    0x10,         /* bcdHID: HID Class Spec release number */
    0x01,
    0x00,         /* bCountryCode: Hardware target country 国家代码 */
    
	0x01,         /* bNumDescriptors: Number of HID class descriptors to follow
					 类别描述符数目(至少有一个报表描述符)*/
    0x22,         /* bDescriptorType 报告描述符 */
    CUSTOMHID_SIZ_REPORT_DESC,	/* wItemLength: Total length of Report descriptor 报告描述符大小 */
    0x00,						/* 标志类别描述符说明结束 */ 
	
	
//
// 端点1描述符
//		
	/******************** Descriptor of Custom HID endpoints ******************/
    /* 27 */
    0x07,          /* bLength: Endpoint Descriptor size */
    USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: */
 
    0x82,          /* bEndpointAddress: Endpoint Address (IN) */               
                   // bit 3...0 : the endpoint number
                   // bit 6...4 : reserved
                    // bit 7     : 0(OUT), 1(IN)
    0x03,          /* bmAttributes: Interrupt endpoint */
    0x40,          /* wMaxPacketSize: 64 Bytes max */
    0x00,
    0x02,          /* bInterval: Polling Interval (2 ms) */
    /* 34 */
 
//
// 端点2描述符
//		
    0x07,	/* bLength: Endpoint Descriptor size */
    USB_ENDPOINT_DESCRIPTOR_TYPE,	/* bDescriptorType: */
			/*	Endpoint descriptor type */
    0x01,	/* bEndpointAddress: */
			/*	Endpoint Address (OUT) */
    0x03,	/* bmAttributes: Interrupt endpoint */
    0x40,	/* wMaxPacketSize: 64 Bytes max  */
    0x00,
    0x02,	/* bInterval: Polling Interval (2 ms) */
    /* 41 */
}; /* CustomHID_ConfigDescriptor */

4.接口描述符组成详解

1.bLength

配置描述符的长度。

2.bDescriptorType

描述符类型,接口描述符为0x04。描述符的结构开头是一样的,都是先说描述符长度,然后说类型,每种描述符的类型是不一样的,如下表格,可速查。

image

3.bInterfaceNumber

接口编号。如果一个配置有多个接口的话,那么每个接口的编号都有一个独立的编号,编号从0开始递增。这里可以设置为0。

4.bAlternateSetting

备用接口编号,一般很少用,设置为0。

5.bNumEndpoints

该接口使用的端点个数,前面讲过一个接口就是一种功能,每个接口需要用户为其分配端点来实现对应的功能,注意一点,这个端点个数不包括端点0。

6.bInterfaceClass、bInterfaceSubClass、bInterfaceProtocol

当设备描述符设备类型bDeviceClass为0时,也就是指示用接口描述符来标识类别,此时用接口类、接口子类、接口协议来说明USB设备此功能所属的类别。如下图显示设备和接口的各种类别,这个类别给设备描述符用还是给接口描述符用要看Descriptor Usage标识(如图中所示)。我们如果单纯用作数据传输的话,直接写FFh就好,代表着用户自定义。USB类信息更详细内容可进入https://www.usb.org/defined-class-codes查看。

7.iInterface

描述此接口的字串索引值,没有的话一般都是0。

posted @ 2024-08-11 20:35  yooooooo  阅读(7)  评论(0编辑  收藏  举报