Fundamentals



USB HID devices are widely used today. They are well know as mouse, keyboard or joystick.

But you can also use them for individual data transfers, similar to serial interfaces like RS232. You do not need an additional driver for Windows and that is a great benefit comparing to USB virtual COM ports.

Only the development on Windows is not that simple. Extensiv knowledge about USB is required to write your own HID driver. Alternatively, you could use one of the many Open Source Projects. But that ain't easy too, because those API's are often really overwhelming. Additionally, these API's almost only support C++. And who can nowadays program C++? It seams you just have a streak of bad luck.

But all these problems are solved by AHid.dll!

The following sections discuss the groundings of AHid.dll and describe how your HID devices can be used with it. We specially look to the few requirements that your device have to comply with.

Reports, Descriptors and ID's


As a matter of principle, HID devices are associated with several properties. A mouse transfers its positions and button states and a keyboard provides information about keypress.

These data values are transfered in so called reports. A report itself is defined in a report descriptor.

Report descriptors describe the data that is transfered in a report. Mouse and keyboard, suchlike many other HID devices, are using descriptors that you can find in the official HID specification. With the information provided therein, the PC knows how to handle input from those devices.

The concept used by reports and the way to define them in descriptors allows to add more devices with new descriptors. The possibilities are virtually endless. And this is what we do when we are going to use a HID device like a simple serial connection.

Vendor-defined devices

Figure 1: Report descriptor of a vendor-defined HID device

The elements of such a descriptor are defined pairwise: The first byte is the element type, followed by one or more bytes for its value. In the above example, all values have a size of one byte except the Usage Table (type = 0x06, 2 bytes), the Logical Maximum (type = 0x26, 2 bytes) and End Collection (type = 0xC0, no byte).

The fundamental elements required by AHid.dll are Report ID, Report Count, Report Size and Input and Output. They all are discussed now.

More information about HID report descriptors can be found in the HID specification on usb.org.

Report ID, Size and Type

You need some parameters to use your device together with AHid.dll. Some of them are found in the report descriptor: Report ID, the size of the report (Report Size and Report Count), and the transfer type / direction (Input or Output).

The ID of the output report in Figure 1 has a value of 1. The value of the input report ID is 2.

The total size of the report is calculated by Report Size and Report Count. Report Count is the number of data elements (bytes, integer,...) and the Report Size is their bit count (8, 16, ..., here: 8). That results in a size of 63 * 8 bits = 63 bytes for the output report and 31 bytes for the input report.

The report type also describes the transfer direction (in case of input and output). The directions are seen from the Host (PC) site. The output report sends data from PC to the HID device, the input report transfers data from the device to PC.

Three parameters to use with AHid.dll are identified now: Report ID, size and type.
Two more parameters to go.

No report ID

You can also use report descriptors without report ID's. Just remove in Figure 1 the lines with element types of 0x85. The report does now no longer belong to an individual report ID.

In normal cases, the ID is send in the very first byte of the report. But now you can gain that byte for user data.

VID and PID

As mentioned before, there are two more parameters to go: VID (vendor ID) and PID (product ID).

Figure 2: HID device in Windows Hardware Manager

The VID is given by the USB IF and differs from vendor to vendor. The PID is set by the vendor and it identifies the device. Both values can be found in the Windows Hardware Manager. Just open the Windows Hardware Manager, select your device and take a look at the Details slider.

The VID of the HID device in Figure 2 has a value of 0xFFFF and the PID is 0x0005.

Summary

We now have all parameters together and can pass them to the AHid_Register() function. As a result, we get two data pipes to use for data transfers:

AHid_Register(&output, 0xFFFF, 0x0005, -1, 1, 63, AHID_REPTYPE_INT_OUT)
AHid_Register(&input, 0xFFFF, 0x0005, -1, 2, 31, AHID_REPTYPE_INT_IN)

when using the simplified report descriptor (without report ID's), the function calls would be done as follows:

AHid_Register(&output, 0xFFFF, 0x0005, -1, 0, 63, AHID_REPTYPE_INT_OUT)
AHid_Register(&input, 0xFFFF, 0x0005, -1, 0, 31, AHID_REPTYPE_INT_IN)


If you have further questions on this topic, please drop us a line.