1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
|
/* ----------------------------------------------------------------------------
* ATMEL Microcontroller Software Support
* ----------------------------------------------------------------------------
* Copyright (c) 2008, Atmel Corporation
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Atmel's name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ----------------------------------------------------------------------------
*/
//------------------------------------------------------------------------------
/// \dir
///
/// !!!Purpose
///
/// This directory provides definitions, structs and functions for a USB HID
/// %device - USB HID Transfer driver, to implement an USB HID compatible
/// %device for customized data transmitting.
///
/// !!!Contents
///
/// There are three things for the implement of the USB HID Transfer driver:
/// - Implement the USB HID driver structs and functions for the %device,
/// to initialize, to handle HID-specific requests and dispach
/// standard requests in USBD callbacks, to read/write through assigned USB
/// endpoints,
/// - Create the HID Transfer device's descriptors that should be passed to
/// the USBDDriver instance on initialization, so that the host can
/// recognize the %device as a USB Transfer %device.
/// - Implement methods to read/write data through interrupt endpoints, so that
/// host and device can exchange data.
///
/// For more information about what a particular group contains, please refer to
/// "USB HID Transfer".
//------------------------------------------------------------------------------
/**
\page "USB HID Transfer"
This page describes how to use the "AT91 USB device framework" to produce a USB
HID Transfer driver, which appears as a USB HID complient device on host.
Details about the USB and the HID class can be found in the }USB specification
2.0} and the }HID specification 1.11}, respectively.
!!!References
- "AT91 USB device framework"
- "USB Device Enumeration"
- <a href="http://www.usb.org/developers/docs/usb_20_040908.zip">
Universal Serial Bus Revision 2.0 specification
</a> (.zip file format, size 9.80 MB)
- <a href="http://www.usb.org/developers/devclass_docs/HID1_11.pdf">
Device Class Definition for HID 1.11</a>
- <a href="http://www.usb.org/developers/devclass_docs/Hut1_12.pdf">
HID Usage Tables 1.12</a>
!!!HID Basic
See "USB HID Basic".
!!!Architecture
See "USB Device Framework Architecture".
!!!Descriptors
...
!!Device Descriptor
The Device descriptor of an HID %device is very basic, since the HID class
code is only specified at the Interface level. Thus, it only contains
standard values, as shown below:
\code
static const USBDeviceDescriptor deviceDescriptor = {
sizeof(USBDeviceDescriptor),
USBGenericDescriptor_DEVICE,
USBDeviceDescriptor_USB2_00,
HIDDeviceDescriptor_CLASS,
HIDDeviceDescriptor_SUBCLASS,
HIDDeviceDescriptor_PROTOCOL,
BOARD_USB_ENDPOINTS_MAXPACKETSIZE(0),
HIDDKeyboardDriverDescriptors_VENDORID,
HIDDKeyboardDriverDescriptors_PRODUCTID,
HIDDKeyboardDriverDescriptors_RELEASE,
1, // Index of manufacturer description
2, // Index of product description
3, // Index of serial number description
1 // One possible configuration
};
\endcode
Note that the Vendor ID is a special value attributed by the USB-IF
organization. The product ID can be chosen freely by the vendor.
!!Configuration Descriptor
Since one interface is required by the HID specification, this must be
specified in the Configuration descriptor. There is no other value of
interest to put here.
\code
// Configuration descriptor
{
sizeof(USBConfigurationDescriptor),
USBGenericDescriptor_CONFIGURATION,
sizeof(HIDDKeyboardDriverConfigurationDescriptors),
1, // One interface in this configuration
1, // This is configuration #1
0, // No associated string descriptor
BOARD_USB_BMATTRIBUTES,
USBConfigurationDescriptor_POWER(100)
},
\endcode
When the Configuration descriptor is requested by the host (by using the
GET_DESCRIPTOR command), the %device must also sent all the related
descriptors, i.e. Interface, Endpoint and Class-Specific descriptors. It is
convenient to create a single structure to hold all this data, for sending
everything in one chunk. In the example software, a
HIDDKeyboardDriverConfigurationDescriptors structure has been declared for
that.
!!HID Class Interface Descriptor
Since a keyboard %device needs to transmit as well as receive data, two
Interrupt (IN & OUT) endpoints are needed. This must be indicated in the
Interface descriptor. Conversely to the mouse example, the Boot protocol is
not implemented here, since there are more constraints on a keyboard %device.
\code
// Interface descriptor
{
sizeof(USBInterfaceDescriptor),
USBGenericDescriptor_INTERFACE,
0, // This is interface #0
0, // This is alternate setting #0
2, // Two endpoints used
HIDInterfaceDescriptor_CLASS,
HIDInterfaceDescriptor_SUBCLASS_NONE,
HIDInterfaceDescriptor_PROTOCOL_NONE,
0 // No associated string descriptor
},
\endcode
!!HID Descriptor
While a HID keyboard produces two different reports, one Input and one Output,
only one Report descriptor can be used to describe them. Since having Physical
descriptors is also useless for a keyboard, there will only be one HID class
descriptor specified here.
For a keyboard, the }bCountryCode} field can be used to specify the language
of the key caps. As this is optional, it is simply set to 00h in the example:
\code
// HID descriptor
{
sizeof(HIDDescriptor),
HIDGenericDescriptor_HID,
HIDDescriptor_HID1_11,
0, // Device is not localized, no country code
1, // One HID-specific descriptor (apart from this one)
HIDGenericDescriptor_REPORT,
HIDDKeyboardDriverDescriptors_REPORTSIZE
},
\endcode
!!Report Descriptor
Two current reports are defined in the Report descriptor. The first one is
used to notify the host of which keys are pressed, with both modifier keys
(alt, ctrl, etc.) and alphanumeric keys. The second report is necessary for
the host to send the LED (num lock, caps lock, etc.) states.
The Report descriptor starts with the global %device functionality, described
with a #Usage Page# and a #Usage# items:
\code
const unsigned char hiddReportDescriptor[] = {
HIDReport_GLOBAL_USAGEPAGE + 2, 0xFF, 0xFF, // Vendor-defined
HIDReport_LOCAL_USAGE + 1, 0xFF, // Vendor-defined
\endcode
An Application collection is then defined to group the reports together:
\code
HIDReport_COLLECTION + 1, HIDReport_COLLECTION_APPLICATION,
\endcode
The first report to be defined is the input report, all data in the buffer
is vendor defined:
\code
// Input report: Vendor-defined
HIDReport_LOCAL_USAGE + 1, 0xFF, // Vendor-defined usage
HIDReport_GLOBAL_REPORTCOUNT + 1, HIDDTransferDriver_REPORTSIZE,
HIDReport_GLOBAL_REPORTSIZE + 1, 8,
HIDReport_GLOBAL_LOGICALMINIMUM + 1, (unsigned char) -128,
HIDReport_GLOBAL_LOGICALMAXIMUM + 1, (unsigned char) 127,
HIDReport_INPUT + 1, 0, // No Modifiers
\endcode
The output report is then defined, data is for the user to decode:
\code
// Output report: vendor-defined
HIDReport_LOCAL_USAGE + 1, 0xFF, // Vendor-defined usage
HIDReport_GLOBAL_REPORTCOUNT + 1, HIDDTransferDriver_REPORTSIZE,
HIDReport_GLOBAL_REPORTSIZE + 1, 8,
HIDReport_GLOBAL_LOGICALMINIMUM + 1, (unsigned char) -128,
HIDReport_GLOBAL_LOGICALMAXIMUM + 1, (unsigned char) 127,
HIDReport_OUTPUT + 1, 0, // No Modifiers
\endcode
The last item, }End Collection}, is necessary to close the previously opened
}Application Collection}.
\code
HIDReport_ENDCOLLECTION
};
\endcode
The input report and output report are all user defined. We define the first
byte as bit map of push buttons and LEDs, remaining bytes as data.
!!Physical Descriptor
A Physical descriptor is useless for a general transfer %device, so none is
defined in this example.
!!Endpoint Descriptor
Following the Interface and HID-specific descriptors, the two necessary
endpoints are defined.
\code
// Interrupt IN endpoint descriptor
{
sizeof(USBEndpointDescriptor),
USBGenericDescriptor_ENDPOINT,
USBEndpointDescriptor_ADDRESS(
USBEndpointDescriptor_IN,
HIDDKeyboardDriverDescriptors_INTERRUPTIN),
USBEndpointDescriptor_INTERRUPT,
sizeof(HIDDKeyboardInputReport),
HIDDKeyboardDriverDescriptors_INTERRUPTIN_POLLING
},
// Interrupt OUT endpoint descriptor
{
sizeof(USBEndpointDescriptor),
USBGenericDescriptor_ENDPOINT,
USBEndpointDescriptor_ADDRESS(
USBEndpointDescriptor_OUT,
HIDDKeyboardDriverDescriptors_INTERRUPTOUT),
USBEndpointDescriptor_INTERRUPT,
sizeof(HIDDKeyboardOutputReport),
HIDDKeyboardDriverDescriptors_INTERRUPTIN_POLLING
}
\endcode
!!String Descriptors
Please refer to "Usage: USBD VID, PID & Strings".
!!!Class-specific requests
A driver request handler should first differentiate between class-specific and
standard requests using the corresponding bits in the }bmRequestType} field.
In most cases, standard requests can be immediately forwarded to the standard
request handler method; class-specific methods must be decoded and treated by
the custom handler.
!!GetDescriptor
Three values have been added by the HID specification for the #GET_DESCRIPTOR#
request. The high byte of the }wValue} field contains the type of the
requested descriptor; in addition to the standard types, the #HID
specification# adds the #HID descriptor# (21h), the #Report descriptor#
(22h) and the #Physical descriptor# (23h) types.
There is no particular action to perform besides sending the descriptor. This
can be done by using the USBD_Write method, after the requested descriptor has
been identified:
\code
switch (USBGenericRequest_GetRequest(request)) {
case USBGenericRequest_GETDESCRIPTOR:
// Check if this is a HID descriptor,
// otherwise forward it to
// the standard driver
if (!HIDDKeyboardDriver_GetDescriptor(
USBGetDescriptorRequest_GetDescriptorType(request),
USBGenericRequest_GetLength(request))) {
USBDDriver_RequestHandler(&(hiddKeyboardDriver.usbdDriver),
request);
}
break;
default:
USBDDriver_RequestHandler(&(hiddKeyboardDriver.usbdDriver),
request);
}
\endcode
A slight complexity of the GET_DESCRIPTOR and SET_DESCRIPTOR requests is that
those are standard requests, but the standard request handler
(USBDDriver_RequestHandler) must not always be called to treat them (since
they may refer to HID descriptors). The solution is to first identify
GET/SET_DESCRIPTOR requests, treat the HID-specific cases and, finally,
forward any other request to the standard handler.
In this case, a GET_DESCRIPTOR request for the Physical descriptor is first
forwarded to the standard handler, and STALLed there because it is not
recognized. This is done because the %device does not have any Physical
descriptors, and thus, does not need to handle the associated request.
!!SetDescriptor
This request is optional and is never issued by most hosts. It is not
implemented in this example.
!!GetReport
Since the HID keyboard defines two different reports, the Report Type value
specified by this request (upper byte of the }wValue} field) must be examined
to decide which report to send. If the type value is 01h, then the Input
report must be returned; if it is 02h, the Output report is requested:
\code
case HIDGenericRequest_GETREPORT:
//-------------------------------
type = HIDReportRequest_GetReportType(request);
length = USBGenericRequest_GetLength(request);
switch (type) {
case HIDReportRequest_INPUT:
// Adjust size and send report
if (length > sizeof(HIDDKeyboardInputReport)) {
length = sizeof(HIDDKeyboardInputReport);
}
USBD_Write(0, // Endpoint #0
&(hiddKeyboardDriver.inputReport),
length,
0, // No callback
0);
break;
case HIDReportRequest_OUTPUT:
// Adjust size and send report
if (length > sizeof(HIDDKeyboardOutputReport)) {
length = sizeof(HIDDKeyboardOutputReport);
}
USBD_Write(0, // Endpoint #0
&(hiddKeyboardDriver.outputReport),
length,
0, // No callback
0);
break;
default:
USBD_Stall(0);
}
break;
\endcode
!!SetReport
For an HID keyboard, the #SET_REPORT# command can be sent by the host to
change the state of the LEDs. Normally, the dedicated Interrupt OUT endpoint
will be used for this; but in some cases, using the default Control endpoint
can save some bandwidth on the host side.
Note that the SET_REPORT request can be directed at the Input report of the
keyboard; in this case, it can be safely discarded, according to the HID
specification. Normally, most host drivers only target the Output report. The
Report Type value is stored in the upper byte of the }wValue} field.
The length of the data phase to follow is stored in the }wLength} field of the
request. It should be equal to the total length of the Output report. If it is
different, the report status must still be updated with the received data as
best as possible.
When the reception of the new data is completed, some processing must be done
to enable/disable the corresponding LEDs. This is done in the callback
function passed as an argument to USBD_Read:
\code
case HIDGenericRequest_SETREPORT:
//-------------------------------
type = HIDReportRequest_GetReportType(request);
length = USBGenericRequest_GetLength(request);
switch(type) {
case HIDReportRequest_INPUT:
// SET_REPORT requests on input reports are ignored
USBD_Stall(0);
break;
case HIDReportRequest_OUTPUT:
// Check report length
if (length != sizeof(HIDDKeyboardOutputReport)) {
USBD_Stall(0);
}
else {
USBD_Read(0, // Endpoint #0
&(hiddKeyboardDriver.outputReport),
length,
(TransferCallback) HIDDKeyboardDriver_ReportReceived,
0); // No argument to the callback function
}
break;
default:
USBD_Stall(0);
}
break;
\endcode
!!SetIdle
In this case study, the #SET_IDLE# request is used to set a delay before a key
is repeated. This is common behavior on keyboard devices. Usually, this delay
is set to about 500 ms by the host.
The only action here is to store the new Idle rate. The management of this
setting must be done in the main function, since Interrupt IN reports are sent
from there.
In practice, it is not necessary to perform any action, apart from sending a
zero-length packet to acknowledge it. The main application however has to make
sure that only new reports are sent by the %device.
\code
case HIDGenericRequest_SETIDLE:
//-----------------------------
hiddKeyboardDriver.inputReportIdleRate =
HIDIdleRequest_GetIdleRate(request);
USBD_Write(0, 0, 0, 0, 0);
break;
\endcode
!!GetIdle
The only necessary operation for this request is to send the previously saved
Idle rate. This is done by calling the USBD_Write method with the one-byte
variable as its parameter:
\code
case HIDGenericRequest_GETIDLE:
//-----------------------------
USBD_Write(0, &(hiddKeyboardDriver.inputReportIdleRate), 1, 0, 0);
break;
\endcode
!!GetProtocol, SetProtocol
This HID keyboard example does not support the Boot protocol, so there is no
need to implement the SET_PROTOCOL and GET_PROTOCOL requests. This means they
can be safely STALLed when received.
!!!Main Application
Like the mouse example, the main program must perform two different
operations. First, it has to monitor the physical inputs used as keys. In the
example software, the buttons present on the evaluation boards are used to
produce several modifier and alphanumeric keys.
Also, the main program is in charge of sending reports as they are modified,
taking into account the Idle rate specified by the host. Idle rate management
can be carried out by firing/resetting a timer once a new report is sent; if
the timer expires, this means the Input report has not changed since.
According to the HID specification, a single instance of the report must be
sent in this case.
Finally, the HID specification also defines that if too many keys are pressed
at the same time, the %device should report an }ErrorRollOver} usage value
(01h) in every byte of the key array. This has to be handled by the main
application as well.
*/
|