关注+星标公众号,不错过精彩内容
编排 | 一个早起的程序员
一个早起的程序员
1
USB描述符 USB描述符有设备描述符、标准配置描述符、接口描述符、端点描述符、字符串描述符,HID设备有HID描述符、报告描述符和物理描述符。 今天主要是学习USB接口描述符的组成。一个早起的程序员2
接口描述符组成 前面讲了 设备描述符 和 标准 配置描述符 ,本篇我们讲解 接口描述符 。首先要明确的一点是接口描述符不能单独返回给USB主机,主机会请求获得配置描述符集合,配置描述符集合主要由标准配置描述符、接口描述符、端点描述符、HID描述符,报告描述符和物理描述符是单独返回给USB主机。接口描述符包含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。
免责声明: 本文素材来源网络,版权归原作者所有。如涉及作品版权问题,请与我联系删除。 ------------ END ------------点击“阅读原文”查看更多分享,欢迎点分享、收藏、点赞、在看。