天天看點

(USB HID) Report Descriptor 了解

在這理整理一下基本 Report Descriptor 對於入門基礎的了解。

在很多檔案、Blog都有提到HID report 總共分為3種 : Input、Output、Feature reports。

在這些 In/Out 方向提的是Host的方向(PC),則 Feature是雙向傳輸的。

USB的網站找得到一隻小工具主要是幫忙編輯USB HID Report Descriptor用的

如果少了這隻Tool,做為一個USB HID開發的工程師就必須去HID Usage Tables查表填對應的代碼

寫 report descriptor,這是一件很累的事情是以有了他可以減少很多查表的時間

(USB HID) Report Descriptor 了解

上圖是我一邊了解一邊編輯的report,利用tool另存一個Report descriptor head file,內容如下 :

1 0x06, 0x00, 0xff,              // USAGE_PAGE (Vendor Defined Page 1)
 2 0x09, 0x01,                    // USAGE (Vendor Usage 1)
 3 0xa1, 0x01,                    // COLLECTION (Application)
 4 0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
 5 0x26, 0x00, 0xff,              //   LOGICAL_MAXIMUM (255)
 6 0x85, 0x01,                    //   REPORT_ID (1)
 7 0x75, 0x08,                    //   REPORT_SIZE (8)
 8 0x95, 0x3f,                    //   REPORT_COUNT (63)
 9 0x09, 0x01,                    //   USAGE (Vendor Usage 1)
10 0x91, 0x00,                    //   OUTPUT (Data,Ary,Abs)
11 0x85, 0x02,                    //   REPORT_ID (2)
12 0x75, 0x08,                    //   REPORT_SIZE (8)
13 0x95, 0x08,                    //   REPORT_COUNT (8)
14 0x09, 0x01,                    //   USAGE (Vendor Usage 1)
15 0x81, 0x00,                    //   INPUT (Data,Ary,Abs)
16 0xc0                           // END_COLLECTION      

這個report我僅定義了in/out report 但我的目的是可以做到雙向傳輸,是以實際使用上我還會加上一個Feature Report

了解整個組成,把想要的Report 定義宣告好最後在用一個USAGE_PAGE/USAGE/COLLECTION/COLLECTION_END包起來

USAGE_PAGE (Generic Desktop)
USAGE (Keyboard)
COLLECTION (Application)
  ... 把我們已經寫好的東西放在這
END_COLLECTION      

而LOGIC_MIN/LOGIC_MAX顧名思義就是Report接收的資料範圍

REPORT_SIZE 我的了解,單位是bit

REPORT_COUNT 單位是次數

是以對於這個Report描述,能做的事情是可以 In/Out 8Bytes 的功能

以下是我實驗時配置的Report Descripot

1 __ALIGN_BEGIN static uint8_t CustomHID_ReportDescriptor[CUSTOMHID_SIZ_REPORT_DESC] __ALIGN_END =
 2 {
 3     0x06, 0x00, 0xFF,                   // USAGE PAGE
 4     0x09, 0x01,                         // USAGE (Vendor Usage 1)
 5     0xa1, 0x01,                         // COLLECTION (Application)
 6     /* 7 */
 7     0x15, 0x00,                         //   LOGICAL_MINIMUM (0)
 8     0x25, 0xff,                         //   LOGICAL_MAXIMUM (255)
 9     0x75, 0x08,                         //   REPORT_SIZE (8)
10     /* 13 */
11     0x85, 0x01,                         //   REPORT_ID (1). This defines input to STM32 (received from host)
12     0x95, USB_HID_RECEIVE_FRAME_SIZE - 1, //   REPORT_COUNT (63)
13     0x09, 0x01,                         //   USAGE (Vendor Usage 1)
14     0x91, 0x02,                         //   OUTPUT (Data,Var,Abs) (note: output from host)
15     /* 21 */
16     0x85, 0x02,                         //   REPORT_ID (2). This defines output from STM32 (sent to host)
17     0x95, USB_HID_SEND_FRAME_SIZE-1,    //   REPORT_COUNT (63)
18     0x09, 0x01,                         //   USAGE (Vendor Usage 1)
19     0x81, 0x02,                         //   INPUT (Data,Var,Abs) (note: input to host)
20     /* 29 */
21     0xc0                             // END_COLLECTION
22 }; /* CustomHID_ReportDescriptor */      

參考資料:

猛哥的軌跡: 簡單講講USB

Device Class Definition for HID - USB.org

HID Usage Tables 1.12 - USB.org

Tutorial about USB HID Report Descriptor

轉載于:https://www.cnblogs.com/ollie-lin/p/10188001.html