USB HID Class 的具体应用

本文介绍了USB HID Class协议,其简化了设备实现,通过HID descriptor描述数据包。HID设备通常包括一个控制端点和中断IN端点,数据以报告形式传输。HID在某些情况下用于USB复合设备,提供自定义控制功能。类需求规定,HID设备必须支持一个Interrupt IN端点和控制端点,数据以report descriptor组织。
摘要由CSDN通过智能技术生成

Introduction

wikipedia 上的一段话:

The HID protocol makes implementation of devices very simple. Devices define their data packets and then present a “HID descriptor” to the host. The HID descriptor is a hard coded array of bytes that describe the device’s data packets. This includes: how many packets the device supports, how large are the packets, and the purpose of each byte and bit in the packet. For example, a keyboard with a calculator program button can tell the host that the button’s pressed/released state is stored as the 2nd bit in the 6th byte in data packet number 4 (note: these locations are only illustrative and are device specific). The device typically stores the HID descriptor in ROM and does not need to intrinsically understand or parse the HID descriptor. Some mouse and keyboard hardware in the market today are implemented using only an 8-bit CPU.

HID Class是USB 协议族中的一类,从字面上理解是Human interface Devices,比如常见的键盘,鼠标,通过usb 接口进行通信,其中HID 起到了描述设备按键操作的作用。

其实,HID 很多情况下也可以作为一个USB复合设备与HOST通信的补充,实现自定义的控制功能,比如在UAC 设备中,通过插入HID class,实现音频设备的与host主机的主动控制通信,弥补UAC 设备单向通信的不足。

Class Requirement

  1. All HID devices must have a control endpoint (Endpoint 0) and an interrupt IN endpoint. Many devices also use an interrupt OUT endpoint.
  2. In most cases, HID devices are not allowed to have more than one OUT and one IN endpoint.
  3. All data transferred must be formatted as reports whose structure is defined in the report descriptor.

HID 设备必须要支持一个Interrupt IN endpoint和一个控制节点,并且,大多数情况下不支持多于一个IN 和一个OUT节点,所有的数据以report descriptor的形式组织上报给Host

Report config Descriptor

Report config Descriptor 的组织方式:
在这里插入图片描述

一个典型的mouse HID report descriptor 例子,包含3个button,格式如下:
在这里插入图片描述

USAGE_PAGE (Button)
USAGE_MINIMUM (Button 1)
USAGE_MAXIMUM (Button 3)
LOGICAL_MINIMUM (0)
LOGICAL_MAXIMUM (1)
REPORT_COUNT (3)
REPORT_SIZE (1)
INPUT (Data,Var,Abs)
REPORT_COUNT (1)
REPORT_SIZE (5)
INPUT (Cnst,Var,Abs)
USAGE_PAGE (Generic Desktop)
USAGE (X)
USAGE (Y)
LOGICAL_MINIMUM (-127)
LOGICAL_MAXIMUM (127)
REPORT_SIZE (8)
REPORT_COUNT (2)
INPUT (Data,Var,Rel)

一个自定义HID report class的例子:

const uint8_t CustomHID_ReportDescriptor[CUSTOMHID_SIZ_REPORT_DESC] =
{
   
  0x06, 0xFF, 0x00,      /* USAGE_PAGE (Vendor Page: 0xFF00) */                       
  0x09, 0x01,            /* USAGE (Demo Kit)               */    
  0xa1, 0x01,            /* COLLECTION (Application)       */            
  /* 6 */
  
  /* Led 1 */        
  0x85, 0x01,            /*     REPORT_ID (1)		     */
  0x09, 0x01,            /*     USAGE (LED 1)	             */
  0x15, 0x00,            /*     LOGICAL_MINIMUM (0)        */          
  0x25, 0x01
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值