2 个接口的自定义 HID 设备

已经在项目中应用,无私分享给大家。

本代码实现了一个 USB device 使用两个接口描述符成功枚举,并使用 2 对端点和 PC 通讯。PC 上的上位机可以正常识别两个接口设备。使用 0x01 0x81 和 0x02 0x82 端点通讯

/** 设备描述符
*/
#define SIZEOF_DEVICE_DESCRIPTOR        0x12
#define DESC_TYPE_DEVICE                0x01
#define USB_VID                         0x351E
#define USB_PID                         0x025a
#define USB_STR_INDEX_SERNUM            3
#define VER_FW_H                        0x02          // Device release number, in binary-coded decimal
#define VER_FW_L                        0x00          // Device release number, in binary-coded decimal#define USB1_ENDP0_SIZE                 0x40/*-----------------------------------------------------------------------------+
| Device Descriptor
|-----------------------------------------------------------------------------*/
const unsigned char usb1HIDDevDescr[SIZEOF_DEVICE_DESCRIPTOR] = {SIZEOF_DEVICE_DESCRIPTOR,               // Length of this descriptorDESC_TYPE_DEVICE,                       // Type code of this descriptor0x00, 0x02,                             // Release of USB spec0xEF,                                   // Device's base class code0x02,                                   // Device's sub class code0x01,                                   // Device's protocol type codeUSB1_ENDP0_SIZE,                        // End point 0's packet sizeUSB_VID&0xFF, USB_VID>>8,               // Vendor ID for device, TI=0x0451// You can order your own VID at www.usb.org"USB_PID&0xFF, USB_PID>>8,               // Product ID for device,// this ID is to only with this exampleVER_FW_L, VER_FW_H,                     // Revision level of device1,                                      // Index of manufacturer name string desc2,                                      // Index of product name string descUSB_STR_INDEX_SERNUM,                   // Index of serial number string desc1                                       //  Number of configurations supported
};#define HID_NUM_INTERFACES                   2           //  Total Number of HIDs implemented. should set to 0 if there are no HIDs implemented.
//***********************************************************************************************
// DESCRIPTOR CONSTANTS
//***********************************************************************************************#define MAX_STRING_DESCRIPTOR_INDEX     6
#define report_desc_size_HID0           36
#define report_desc_size_HID1           36
#define CONFIG_STRING_INDEX             4
#define INTF_STRING_INDEX               5
#define USB_CONFIG_VALUE                0x01#define USB_SUPPORT_REM_WAKE 0x00
// Controls whether the application is self-powered to any degree.  Should be
// set to 0x40, unless the USB device is fully supplied by the bus.
#define USB_SUPPORT_SELF_POWERED 0x80
// Controls what the device reports to the host regarding how much power it will
// consume from VBUS.  Expressed in 2mA units; that is, the number of mA
// communicated is twice the value of this field.
#define USB_MAX_POWER 0x32#define SIZEOF_INTERFACE_DESCRIPTOR  0x09
#define DESC_TYPE_INTERFACE          0x04#define DESC_TYPE_CONFIG             0x02
#define SIZEOF_CONFIG_DESCRIPTOR     0x09#define SIZEOF_ENDPOINT_DESCRIPTOR   0x07
#define DESC_TYPE_ENDPOINT           0x05
#define EP_DESC_ATTR_TYPE_INT        0x03/* Structure for generic part of configuration descriptor */
struct abromConfigurationDescriptorGenric
{unsigned char sizeof_config_descriptor;            // bLengthunsigned char desc_type_config;                    // bDescriptorType: 2unsigned char sizeof_configuration_descriptor1;    // wTotalLengthunsigned char sizeof_configuration_descriptor2;unsigned char usb_num_configurations;              // bNumInterfacesunsigned char bconfigurationvalue;                 // bConfigurationValueunsigned char config_string_index;                // iConfiguration Description offsetunsigned char mattributes;                         // bmAttributes, bus power, remote wakeupunsigned char usb_max_power;                       // Max. Power Consumption at 2mA unit
};
/**************************************HID descriptor structure *************************/
struct abromConfigurationDescriptorHid
{//INTERFACE DESCRIPTOR (9 bytes)unsigned char sizeof_interface_descriptor;        // Desc Lengthunsigned char desc_type_interface;                // DescriptorTypeunsigned char interface_number_hid;               // Interface numberunsigned char balternatesetting;                  // Any alternate settings if supportedunsigned char bnumendpoints;                      // Number of end points requiredunsigned char binterfaceclass;                    // Class IDunsigned char binterfacesubclass;                 // Sub class IDunsigned char binterfaceprotocol;                 // Protocolunsigned char intf_string_index;                  // String Index//hid descriptor (9 bytes)unsigned char blength_hid_descriptor;             // HID Desc lengthunsigned char hid_descriptor_type;                // HID Desc Typeunsigned char hidrevno1;                          // Rev no unsigned char hidrevno2;                          // Rev no - 2nd partunsigned char tcountry;                              // Country code unsigned char numhidclasses;                      // Number of HID classes to follow    unsigned char report_descriptor_type;             // Report desc type unsigned char tlength;                            // Total length of report descriptorunsigned char size_rep_desc;//input end point descriptor (7 bytes)unsigned char size_inp_endpoint_descriptor;       // End point desc sizeunsigned char desc_type_inp_endpoint;             // Desc typeunsigned char hid_inep_addr;                      // Input end point addressunsigned char ep_desc_attr_type_inp_int;          // Type of end pointunsigned char  inp_wmaxpacketsize1;               // Max packet sizeunsigned char  inp_wmaxpacketsize2;unsigned char inp_binterval;                      // bInterval in ms// Output end point descriptor; (7 bytes)unsigned char size_out_endpoint_descriptor;       // Output endpoint desc sizeunsigned char desc_type_out_endpoint;             // Desc typeunsigned char hid_outep_addr;                     // Output end point addressunsigned char ep_desc_attr_type_out_int;          // End point typeunsigned char out_wmaxpacketsize1;                // Max packet sizeunsigned char out_wmaxpacketsize2;unsigned char out_binterval;                      // bInterval in ms
};/* Global structure having Generic,CDC,HID, MSC structures */
struct  abromConfigurationDescriptorGroup
{/* Generic part of config descriptor */const struct abromConfigurationDescriptorGenric abromConfigurationDescriptorGenric;/* HID descriptor structure */const struct abromConfigurationDescriptorHid stHid[HID_NUM_INTERFACES];
};#ifndef WBVAL
#define WBVAL(x) (unsigned char)((x) & 0xFF), (unsigned char)(((x) >> 8) & 0xFF)
#endif/*!< USBD CONFIG */
#define USBD_MAX_POWER 0xfa
#define USBD_CONFIG_DESCRIPTOR_SIZE (65+9-8 + 7)#define HID_IF0_STRING             (0x04)
const unsigned char HID_Interface0String[] = {// String index6, Interface String26,        // Length of this string descriptor3,        // bDescriptorType'H',0x00,'I',0x00,'D',0x00,' ',0x00,'P',0x00,'a',0x00,'c',0x00,'k',0x00,'D',0x00,'a',0x00,'t',0x00,'a',0x00
};
#define HID_IF1_STRING             (0x05)
const unsigned char HID_Interface1String[] = {// String index5, Interface String16,        // Length of this string descriptor3,        // bDescriptorType'H',0x00,'I',0x00,'D',0x00,' ',0x00,'C',0x00,'M',0x00,'D',0x00,
};/*!< USBD ENDPOINT CONFIG */
#define USBD_IF0_AL0_EP0_ADDR       0x01
#define USBD_IF0_AL0_EP0_SIZE       (512)
#define USBD_IF0_AL0_EP0_INTERVAL   0x01#define USBD_IF0_AL0_EP1_ADDR       0x81
#define USBD_IF0_AL0_EP1_SIZE       0x40
#define USBD_IF0_AL0_EP1_INTERVAL   0x01#define USBD_IF1_AL0_EP0_ADDR       0x02
#define USBD_IF1_AL0_EP0_SIZE       (512)
#define USBD_IF1_AL0_EP0_INTERVAL   0x01#define USBD_IF1_AL0_EP1_ADDR       0x82
#define USBD_IF1_AL0_EP1_SIZE       0x40
#define USBD_IF1_AL0_EP1_INTERVAL   0x01/*!< USBD HID CONFIG */
#define USBD_HID_VERSION                    0x0111
#define USBD_HID_COUNTRY_CODE               0
#define USBD_IF0_AL0_HID_REPORT_DESC_SIZE   (sizeof(HidReportDescr0))
#define USBD_IF1_AL0_HID_REPORT_DESC_SIZE   (sizeof(HidReportDescr1))
#define EPIN_SIZE                           (0x40)unsigned char const HidReportDescr0[]=
{//vendor0x06, 0x00, 0xFF,      //Usage Page (Vendor-Defined 29) 0x09, 0x92,        //Usage (Vendor-Defined 146) 0xA1, 0x01,            //Collection (Application)   0x19, 0x00,        //Usage Minimum (Undefined) 0x2A, 0xFF, 0x00,   //Usage Maximum (Vendor-Defined 255)   0x15, 0x00,          //Logical Minimum (0)   0x26, 0xFF, 0x00,   //Logical Maximum (255)   0x75, 0x08,           //Report Size (8)   0x95, 0x40,         //Report Count (63)  0x91, 0x00,        //Output (Data,Ary,Abs,NWrp,Lin,Pref,NNul,NVol,Bit)  0x19, 0x00,        //Usage Minimum (Undefined) 0x2a, 0xFF, 0x00,   //Usage Maximum (Vendor-Defined 255)   0x81, 0x00,          //Input (Data,Ary,Abs) 0xC0,                //End Collection };
unsigned char const HidReportDescr1[]=
{0x06, 0x00, 0xFF,      //Usage Page (Vendor-Defined 29) 0x09, 0x92,        //Usage (Vendor-Defined 146) 0xA1, 0x01,            //Collection (Application)   0x19, 0x00,        //Usage Minimum (Undefined) 0x2A, 0xFF, 0x00,   //Usage Maximum (Vendor-Defined 255)   0x15, 0x00,          //Logical Minimum (0)   0x26, 0xFF, 0x00,   //Logical Maximum (255)   0x75, 0x08,           //Report Size (8)   0x95, 0x40,         //Report Count (63)  0x91, 0x00,        //Output (Data,Ary,Abs,NWrp,Lin,Pref,NNul,NVol,Bit)  0x19, 0x00,        //Usage Minimum (Undefined) 0x2a, 0xFF, 0x00,   //Usage Maximum (Vendor-Defined 255)   0x81, 0x00,          //Input (Data,Ary,Abs) 0xC0,                //End Collection };const unsigned char HIDCfgDescr[] = {//配置描述符
/********************************************** Config Descriptor */0x09,                                       /*!< bLength */0x02,                                       /*!< bDescriptorType */
//整个数组的长度WBVAL(USBD_CONFIG_DESCRIPTOR_SIZE),         /*!< wTotalLength */
//设备接口的数目 本设备有2个接口描述符0x02,                                       /*!< bNumInterfaces */0x01,                                       /*!< bConfigurationValue */0x00,                                       /*!< iConfiguration */0xa0,                                       /*!< bmAttributes */USBD_MAX_POWER,                             /*!< bMaxPower */// 接口描述符 0
/********************************************** Interface 0 Alternate 0 Descriptor */0x09,                                       /*!< bLength */0x04,                                       /*!< bDescriptorType */0x00,                                       /*!< bInterfaceNumber */0x00,                                       /*!< bAlternateSetting */0x02,                                       /*!< bNumEndpoints */
//接口类型,USB规范有明确定义 03 为 HID 类设备0x03,                                       /*!< bInterfaceClass */0x00,                                       /*!< bInterfaceSubClass */0x00,                                       /*!< bInterfaceProtocol */HID_IF0_STRING,                             /*!< iInterface */
//HID 类接口需要配置 HID 设备类型,所以需要 HID 描述符
/********************************************** Class Specific Descriptor of HID */0x09,                                       /*!< bLength */0x21,                                       /*!< bDescriptorType */WBVAL(USBD_HID_VERSION),                    /*!< bcdHID */USBD_HID_COUNTRY_CODE,                      /*!< bCountryCode */0x01,                                       /*!< bNumDescriptors */0x22,                                       /*!< bDescriptorType */
// 表明 HID report 描述符的长度WBVAL(USBD_IF0_AL0_HID_REPORT_DESC_SIZE),   /*!< wItemLength */
// 端点描述符
/********************************************** Endpoint 0 Descriptor */0x07,                                       /*!< bLength */0x05,                                       /*!< bDescriptorType */
//端点地址USBD_IF0_AL0_EP0_ADDR,                      /*!< bEndpointAddress */0x03,                                       /*!< bmAttributes */WBVAL(USBD_IF0_AL0_EP0_SIZE),               /*!< wMaxPacketSize */USBD_IF0_AL0_EP0_INTERVAL,                  /*!< bInterval */
/********************************************** Endpoint 1 Descriptor */0x07,                                       /*!< bLength */0x05,                                       /*!< bDescriptorType */USBD_IF0_AL0_EP1_ADDR,                      /*!< bEndpointAddress */0x03,                                       /*!< bmAttributes */WBVAL(USBD_IF0_AL0_EP1_SIZE),               /*!< wMaxPacketSize */USBD_IF0_AL0_EP1_INTERVAL,                  /*!< bInterval */// 接口描述符 1
/********************************************** Interface 1 Alternate 0 Descriptor */0x09,                                       /*!< bLength */0x04,                                       /*!< bDescriptorType */0x01,                                       /*!< bInterfaceNumber */0x00,                                       /*!< bAlternateSetting */0x02,                                       /*!< bNumEndpoints */0x03,                                       /*!< bInterfaceClass */0x00,                                       /*!< bInterfaceSubClass */0x00,                                       /*!< bInterfaceProtocol */HID_IF1_STRING,                             /*!< iInterface */
/********************************************** Class Specific Descriptor of HID */0x09,                                       /*!< bLength */0x21,                                       /*!< bDescriptorType */WBVAL(USBD_HID_VERSION),                    /*!< bcdHID */USBD_HID_COUNTRY_CODE,                      /*!< bCountryCode */0x01,                                       /*!< bNumDescriptors */0x22,                                       /*!< bDescriptorType */WBVAL(USBD_IF1_AL0_HID_REPORT_DESC_SIZE),   /*!< wItemLength */
/********************************************** Endpoint 0 Descriptor */0x07,                                       /*!< bLength */0x05,                                       /*!< bDescriptorType */USBD_IF1_AL0_EP0_ADDR,                      /*!< bEndpointAddress */0x03,                                       /*!< bmAttributes */WBVAL(USBD_IF1_AL0_EP0_SIZE),               /*!< wMaxPacketSize */USBD_IF1_AL0_EP0_INTERVAL,                  /*!< bInterval */
/********************************************** Endpoint 0 Descriptor */0x07,                                       /*!< bLength */0x05,                                       /*!< bDescriptorType */USBD_IF1_AL0_EP1_ADDR,                      /*!< bEndpointAddress */0x03,                                       /*!< bmAttributes */WBVAL(USBD_IF1_AL0_EP1_SIZE),               /*!< wMaxPacketSize */USBD_IF1_AL0_EP1_INTERVAL,                  /*!< bInterval */
};

吐血分享 HID 2个接口设备的描述符代码相关推荐

  1. USB描述符(附加USB HID报告描述符 )

    USB描述符介绍 USB描述符是主机识别USB设备的依据,主机根据设备的描述符来加载相应的驱动 USB描述符的作用 USB描述符信息存储在USB设备中,在枚举过程中,USB主机会向USB设备发送Get ...

  2. DIY蓝牙键盘(2) - 理解HID报文描述符

    1. 前情回顾 上篇主要讲了键盘报文的分类与格式,并留下了一个问题:那主机为什么知道我这些报文的格式?那肯定是主机要提前知道我们发的报文的格式,那么问题就变成了:在发送报文前我们要怎么通知主机,让它知 ...

  3. 蓝牙HID规范的报告描述符【另外一篇文章】

    SYD8801是一款低功耗高性能蓝牙低功耗SOC,集成了高性能2.4GHz射频收发机.32位ARM Cortex-M0处理器.128kB Flash存储器.以及丰富的数字接口.SYD8801片上集成了 ...

  4. USB HID设备报告描述符详解

    概述: 报告在这里意思是数据传输(data transfer),而报告描述符是对这些传输的数据作用途(usage)上的说明. USB通讯协议的规范是以1ms产生一个USB帧(Frame),USB设备可 ...

  5. USB描述符【整理】

    USB描述符 USB描述符信息存储在USB设备中,在枚举过程中,USB主机会向USB设备发送GetDescriptor请求,USB设备在收到这个请求之后,会将USB描述符信息返回给USB主机,USB主 ...

  6. python特性描述_详解 Python 最优雅的特性之一 — 描述符

    本篇选自 Python黑魔法指南 -> 第四章 -> 第2节. github仓库: https://github.com/iswbm/magic-python magic-python 目 ...

  7. USB鼠标-字符串描述符(七)

    USB 鼠标详解阅读顺序 1.枚举 2.设备描述符 3.设置地址 4.配置描述符 5.接口描述符 6.HID 描述符 7.端点描述符 8.字符串描述符 9.HID 报告描述符 10.HID 报告的返回 ...

  8. USB鼠标-配置描述符(三)

    USB 鼠标详解阅读顺序 1.枚举 2.设备描述符 3.设置地址 4.配置描述符 5.接口描述符 6.HID 描述符 7.端点描述符 8.字符串描述符 9.HID 报告描述符 10.HID 报告的返回 ...

  9. 深入理解 Python 描述符

    学习 Python 这么久了,说起 Python 的优雅之处,能让我脱口而出的, Descriptor(描述符)特性可以排得上号. 描述符 是Python 语言独有的特性,它不仅在应用层使用,在语言语 ...

最新文章

  1. POJ 3180 Tarjan
  2. 【数字信号处理】相关函数 ( 周期信号 | 周期信号的自相关函数 )
  3. MFC 获取命令行参数
  4. 用xml画水平虚线和竖直虚线.md
  5. linux系统oracle启动过程,Linux主机下配置Oracle 10G自动启动过程记
  6. python程序分块_怎么用python实现文件的分块下载
  7. 单场淘汰制场次计算方法_淘汰赛、单循环赛和双循环赛的计算方法分别是什么?...
  8. 网络流(最大流)基础入门
  9. YOLOv5、v7改进之三十九:引入改进遮挡检测的Tri-Layer插件 | BMVC 2022
  10. 采用中断模式编程并使用杜邦线模拟开关实现LED灯的亮灭
  11. java 短信从申请到实现(阿里云)
  12. Linux系列(五)、Vim编辑器的使用、账号用户组的管理、磁盘管理、进程管理
  13. 梦幻模拟战pc版更新服务器正在维护,梦幻模拟战PC版免CD(更新2代跳出补丁 一些实用说明)...
  14. 史上最全阿里 Java 面试题总结及答案
  15. Java中你最擅长什么_你最擅长的领域是什么
  16. Android字体样式修改
  17. Carsim 与 Simulink 联合仿真用到的ABS.mdl模型文件
  18. BIGEMAP如何导出含高程值的标注点(线路高程提取)
  19. java 转 js_无需访问源码!Java迅速转为JavaScript
  20. 搜索引擎(蜘蛛)抓取内容规则

热门文章

  1. 【预测模型】基于卷积神经网络CNN实现预测单输入单输出预测模型matlab源码
  2. Node进阶—事无巨细手写Koa源码
  3. 压力容器标准规范资料全文知识库
  4. MySQL面试2:一张学生表,一张教师表,里面都有Name和Code,写出张三的老师有多少名学生的SQL语句。
  5. labview2020图文教程LabVIEW2020
  6. 计算机科学与技术专业前景分析,计算机科学与技术专业前景分析
  7. android按home键再启动程序camera黑屏屏蔽home键
  8. android progressbar 加载数据,ProgressBar的使用实例
  9. 西南航空 jetblue shape 逆向
  10. C语言实现:见缝插针游戏!代码思路+源码分享