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

目录

1.USB描述符

2.接口描述符组成

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

4.接口描述符组成详解


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。描述符的结构开头是一样的,都是先说描述符长度,然后说类型,每种描述符的类型是不一样的,如下表格,可速查。

图片

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。

 

1.版权归本作者(一个早起的程序员)所有;

2.未经原作者允许不得转载本文内容,否则将视为侵权;

3.转载或者引用本文内容请注明来源及原作者;

4.对于不遵守此声明或者其他违法使用本文内容者,本人依法保留追究权等。

下面是我的个人微信公众号,关注【一个早起的程序员】精彩文章不断。

  • 19
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个早起的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值