Forum: Mikrocontroller und Digitale Elektronik USB-HID Report Descriptor


von Sascha D. (plutoonline)


Lesenswert?

Hallo,
Ich probier gerade ein USB-HID zu progammieren, ich hab mir auch schon 
die entsprechenden Dokumente von usb.org geholt.
Ich blicke da aber nicht so recht Durch.
Ich wollte die Libarys von http://www.obdev.at/avrusb/ verwenden.
Ich hab jetzt doch mal ein paar Fragen zum USB-HID Reportdescriptor und 
zur Übertragung.

Ist es richtig das im Reportdescriptor beschrieben wird wie die Daten 
sich beim Übertragen zusammensetzen?

Bsp.:

Gamepad mit 24 Buttons...

0x05,0x01      ;Usage_Page (Generic Desktop)
0x09,0x05      ;Usage (Game Pad)
0xA1,0x01      ;Collection (Application)
0x05,0x09      ;Usage_Page (Button)
0x19,0x01      ;Usage_Minimum (Button 1)
0x29,0x18      ;Usage_Maximum (Button 24)
0x15,0x00      ;Logical_Minimum (0)
0x25,0x01      ;Logical_Maximum (1)
0x75,0x01      ;Report_Size (1)
0x95,0x18      ;Report_Count (24)
0x55,0x00      ;Unit_Exponent (0)
0x65,0x00      ;Unit (None)
0x81,0x02      ;Input (Data, Var, Abs)
0xC0                      ;End_Collection

24 Buttons * 1Bit = 3 Bytes

Ist das im Prinzip so das ich jetzt die 24 Bits für die Buttons zu 3 
Bytes zusammenfasse und dann über den Befehl

usbSetInterrupt(reportBuffer, sizeof(reportBuffer));

zum PC rausschicke?

Ich hoffe ich hab mich jetzt nicht allzu Dämlich ausgedrückt.

Aber Vielen Dank schon mal!

von Benjamin S. (recycler)


Lesenswert?

Ich habe deinen Descriptor nicht geprüft, aber versuch mal ein einfaches 
Beispiel.
Du kannst mit USB nix schicken, der Host (PC) holt sich die Daten ab. Du 
musst auf die Anfragen reagieren, um das klarzustellen.

von Sascha D. (plutoonline)


Lesenswert?

Also stellt die Funktion "usbSetInterrupt(reportBuffer, 
sizeof(reportBuffer))" die Daten bereit die sich der PC abholt?

Was wäre so ein einfaches Beispiel? Hast Du da einen Tip oder ein 
Beispiel?

von Benjamin S. (recycler)


Lesenswert?

Ich hab mit AVR USB noch nicht gearbeitet. Nimm ein einfaches Beispiel, 
zB bei Tastendruck wird was auf dem Bildschirm (PC) geschrieben, wenns 
eine Tastaturemulation ist.
So einfach wie RS232 ist USB leider nicht.

von Sascha D. (plutoonline)


Lesenswert?

Kann mir keiner ein kleines Beispiel für ein Gamepad mit z.B. einer 
Taste geben, auf das ich dann entsprechend aufbauen kann?
Ich blicke bei den großen Projekten nicht so recht Durch.
Ich möchte nicht jeden Schrit vorgekaut bekommen nur so ein paar Tips 
zum grundsätzlichen Aufbau und Ablauf. In den Dokumenten von usb.org ist 
zwar alles haarklein beschrieben aber Irgendwie bekomme ich die nötigen 
Infos nicht zusammen.

von Εrnst B. (ernst)


Lesenswert?

Hier aus dem EzHID-Projekt der Descriptor für ein SNES-Gamepad:
1
/* the device descriptor */
2
static code Byte dev_desc[] = {
3
   0x12, // bLength             : Length of Descriptor
4
   0x01, // bDescriptorType     : Descriptor Type = Device
5
   0x10, // bcdUSB (L)          : USB Specification Version 1.10 (L)
6
   0x01, // bcdUSB (H)          : USB Specification Version 1.10 (H)
7
   0x00, // bDeviceClass        : Device Class (0xff is Vendor Specific)
8
   0x00, // bDeviceSubClass     : Device Sub-Class (0xff is Vendor Specific)
9
   0x00, // bDeviceProtocol     : Device Protocol (0xff is Vendor Specific)
10
   0x40, // bMaxPacketSize0     : Maximum Packet Size for EP0
11
   0xcd, // idVendor (L)        : Vendor ID (L)
12
   0x06, // idVendor (H)        : Vendor ID (H)
13
   0x05, // idProduct (L)       : Product ID (L)
14
   0xc0, // idProduct (H)       : Product ID (H)
15
   0x00, // bcdDevice (L)       : Device Release Number (BCD,L)
16
   0x01, // bcdDevice (H)       : Device Release Number (BCD,H)
17
   0x01, // iManufacturer       : Manufacturer Index String
18
   0x02, // iProduct            : Product Index String
19
   0x00, // iSerialNumber       : Serial Number Index String
20
   0x01  // bNumConfigurations  : Number of Configurations in this Device
21
};
22
23
/* the configuration descriptor */
24
static code Byte conf_desc[] = {
25
   0x09, // bLength             : Length of Descriptor
26
   0x02, // bDescriptorType     : Descriptor Type = Configuration
27
   0x3b, // wTotalLength (L)    : Total Length (L) including Interface and Endpoint
28
   0x00, // wTotalLength (H)    : Total Length (H)
29
30
   0x02, // bNumInterfaces      : One Interface in this Configuration
31
32
   0x01, // bConfigurationValue : Configuration Value Used by Set_Configuration Request
33
         //                       to Select this Configuration
34
   0x00, // iConfiguration      : Index of String Describing this Configuration
35
   0x80, // bmAttributes        : Attributes
36
   0x32, // MaxPower            : Maximum Power
37
38
39
   /*
40
    * The interface descriptor for Pad 1
41
    */
42
   // Interface 0, alternate setting 0
43
   0x09, // bLength             : Length of Descriptor
44
   0x04, // bDescriptorType     : Descriptor Type = Interface
45
   0x00, // bInterfaceNumber    : Zero-based index of this Interface
46
   0x00, // bAlternateSetting   : Alternate Setting
47
   0x01, // bNumEndpoints       : Number of Endpoints in this Interface
48
   0x03, // bInterfaceClass     : Interface Class
49
   0x00, // bInterfaceSubClass  : Interface Sub-Class (boot interface)
50
   0x00, // bInterfaceProtocol  : Interface Protocol (keyboard)
51
   0x03, // iInterface          : Index to String Descriptor for this Interface
52
   
53
   // HID Descriptor
54
   0x09, // bLength             : Length of Descriptor
55
   0x21, // bDescriptorType     : Descriptor Type = HID
56
   0x00, // bcdHID (L)          : HID Class Specification release number (L)
57
   0x01, // bcdHID (H)          : HID Class Specification release number (H)
58
   0x00, // bCountryCode        : Hardware target country = not specific
59
   0x01, // bNumDescriptors     : Number of HID class descriptors to follow
60
   0x22, // bDescriptorType     : Report descriptor type
61
   0x36, // wDescriptorLength(L): Total length of Report descriptor (L)
62
   0x00, // wDescriptorLength(H): Total length of Report descriptor (H)
63
   
64
   // Endpoint Descriptor
65
   0x07, // bLength             : Length of Descriptor
66
   0x05, // bDescriptorType     : Descriptor Type = Endpoint
67
   0x81, // bEndpointAddress    : Endpoint Address
68
   0x03, // bmAttributes        : Endpoint Attributes = INT
69
   0x02, // wMaxPacketSize (L)  : Maximum Packet Size (L)
70
   0x00, // wMaxPacketSize (H)  : Maximum Packet Size (H)
71
   0x0a, // bInterval           : Polling intervall in Milliseconds
72
73
74
   /*
75
    * The interface descriptor for Pad 2
76
    */
77
   // Interface 1, alternate setting 0
78
   0x09, // bLength             : Length of Descriptor
79
   0x04, // bDescriptorType     : Descriptor Type = Interface
80
   0x01, // bInterfaceNumber    : Zero-based index of this Interface
81
   0x00, // bAlternateSetting   : Alternate Setting
82
   0x01, // bNumEndpoints       : Number of Endpoints in this Interface
83
   0x03, // bInterfaceClass     : Interface Class
84
   0x00, // bInterfaceSubClass  : Interface Sub-Class (boot interface)
85
   0x00, // bInterfaceProtocol  : Interface Protocol (keyboard)
86
   0x03, // iInterface          : Index to String Descriptor for this Interface
87
   
88
   // HID Descriptor
89
   0x09, // bLength             : Length of Descriptor
90
   0x21, // bDescriptorType     : Descriptor Type = HID
91
   0x00, // bcdHID (L)          : HID Class Specification release number (L)
92
   0x01, // bcdHID (H)          : HID Class Specification release number (H)
93
   0x00, // bCountryCode        : Hardware target country = not specific
94
   0x01, // bNumDescriptors     : Number of HID class descriptors to follow
95
   0x22, // bDescriptorType     : Report descriptor type
96
   0x36, // wDescriptorLength(L): Total length of Report descriptor (L)
97
   0x00, // wDescriptorLength(H): Total length of Report descriptor (H)
98
   
99
   // Endpoint Descriptor
100
   0x07, // bLength             : Length of Descriptor
101
   0x05, // bDescriptorType     : Descriptor Type = Endpoint
102
   0x82, // bEndpointAddress    : Endpoint Address
103
   0x03, // bmAttributes        : Endpoint Attributes = INT
104
   0x02, // wMaxPacketSize (L)  : Maximum Packet Size (L)
105
   0x00, // wMaxPacketSize (H)  : Maximum Packet Size (H)
106
   0x0a  // bInterval           : Polling intervall in Milliseconds
107
};
108
109
110
/* The Pad Report Descriptor */
111
static code Byte report_desc_pad[] = {
112
   0x05, 0x01, // Usage Page (Generic, Desktop),
113
   0x09, 0x05, // Usage (Game Pad),
114
   0xa1, 0x01, // Collection (Application),
115
116
   /* Pointer */
117
   0x09, 0x01, //   Usage (Pointer),
118
   0xa1, 0x00, //   Collection (Physical),
119
   0x09, 0x30, //     Usage (X),
120
   0x09, 0x31, //     Usage (Y),
121
   0x15, 0xff, //     Logical Minimum (-1),
122
   0x25, 0x01, //     Logical Maximum (+1),
123
   0x95, 0x02, //     Report Count (2),
124
   0x75, 0x02, //     Report Size (2),
125
   0x81, 0x02, //     Input (Data, Variable, Absolute, No Null),
126
   0xc0,       //   End Collection,
127
128
   /* Padding */
129
   0x95, 0x04, //   Report Count (4),
130
   0x75, 0x01, //   Report Size (1),
131
   0x81, 0x03, //   Input (Constant, Variable, Absolute),
132
133
   /* 8 Buttons */
134
   0x05, 0x09, //   Usage Page (Buttons),
135
   0x19, 0x01, //   Usage Minimum (Button 1),
136
   0x29, 0x08, //   Usage Maximum (Button 8),
137
   0x15, 0x00, //   Logical Minimum (0),
138
   0x25, 0x01, //   Logical Maximum (1),
139
   0x95, 0x08, //   Report Count (8),
140
   0x75, 0x01, //   Report Size (1),
141
   0x81, 0x02, //   Input (Data, Variable, Absolute, No Null),
142
143
   0xc0        // End Collection
144
};

von Guido Körber (Gast)


Lesenswert?

Beispiele für Descriptoren stehen auch in der USB HID Spec.

von Potter68 (Gast)


Lesenswert?

Schau mal bei obdev auf der Seite. Da sollte das passende Beispiel für 
Dich dabei sein.

Gruss Potter68

von Sascha D. (plutoonline)


Lesenswert?

Hallo,
ist zwar schon ein Paar Tage her, aber jetzt habe ich wieder ein bischen 
Zeit mich mit der Thematik USB-HID auseinanderzusetzen.
Mittlerweile habe ich mir auch eine kleine Platine mit Atmega8 und der 
Notwendigen Beschaltung für USB erstellt.
Getestet hab ich die Schaltun mit HID-Keys von der Obdev-Seite.
Also die Hardware zu testen setht bereits.
Nach einigem erfolglosem rumbasteln habe ich jetzt verschiedene Fragen:

- Kann man ein Gamepad Reportdescriptor mit nur 8 Buttons und sonst 
keinen Steuerelementen erstellen der unter Windows funktioniert? (also 
Ohne Steuerkreuz)

z.B.
    0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
    0x09, 0x05,                    // USAGE (Game Pad)
    0xA1, 0x01,                    // Collection (Application)
    0x05, 0x09,                    // USAGE_PAGE (Button)
    0x19, 0x01,                    // USAGE_MINIMUM (Button 1)
    0x29, 0x08,                    // USAGE_MAXIMUM (Button 8)
    0x15, 0x00,                    // LOGICAL_MINIMUM (0)
    0x25, 0x01,                    // LOGICAL_MAXIMUM (1)
    0x75, 0x01,                    // REPORT_SIZE (1)
    0x95, 0x08,                    // REPORT_COUNT (8)
    0x55, 0x00,                    // Unit_Exponent (0)
    0x65, 0x00,                    // Unit (None)
    0x81, 0x02,                    // INPUT (Data,Var,Abs)
    0xc0                           // END_COLLECTION

- Kann mir Jemand den Unterschied zwischen Report_Size und Report_Count 
erklären?

- Gibt es ein beispiel für ein Gampad USB-HID in C mit Atmega8? (Ich 
habe bisher nur Assembler Beispiele gefunden.)

- Gibt es Tools um einen Reportdescriptor zu Überprüfen. (ausser HID 
Descriptor Tool (DT))

- Würde es reichen z.B in HID.Keys erstmal den report Descriptor zu 
ändern das Windows das entsprechende Gerät erkennt.
Ddas da natürlich nichts an Daten ankommt ist mir klar das wäre dann 
Step2.

Viele Viele Fragen.
Ich Hoffe mir kann jemand Helfen.

Vielen Dank schonmal.

von Sascha D. (plutoonline)


Lesenswert?

Gibt es eigentlich eine USB-HID Dokumentation in Deutsch?

von Sascha D. (plutoonline)


Lesenswert?

Hat den niemand einen Tip?

von Potter68 (Gast)


Lesenswert?

Hi,

lies das Buch von Jan Axelson. Das ist eine sehr ausführliche 
Rundum-Einführung in USB.

Gruß Potter68

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.