USB Human Interface Device Class on an Embedded Host
Total Page:16
File Type:pdf, Size:1020Kb
AN1144 USB Human Interface Device Class on an Embedded Host Author: Amardeep Gupta The class, subclass and protocol designators for an HID bDeviceClass Microchip Technology Inc. device are not contained in the , bDeviceSubClass and bDeviceProtocol fields of the device descriptor. Instead, these fields are all set to INTRODUCTION 0x00 and the designators are specified in the bInterfaceClass, bInterfaceSubClass and With the introduction of Microchip’s microcontrollers with bInterfaceProtocol fields of the interface the USB OTG peripheral, microcontroller applications descriptor. The most common configurations for HID can easily support USB embedded host functionality. class devices are: Traditionally, the PC is used as a host in an USB network. Now, with Microchip’s microcontroller with host capability, • bInterfaceClass – the host can be implemented in an embedded system. 0x03 (HID Class) Some of the most common uses of this capability are to bInterfaceSubClass interface to Human Interface Devices (HIDs). • – 0x00 (No Subclass) USB HUMAN INTERFACE DEVICE 0x01 (Boot Interface Subclass) (HID) CLASS 0x02-0xFF (Reserved) Overview • bInterfaceProtocol – The HID class primarily consists of devices that are 0x00 (None) used to control any particular application. 0x01 (Keyboard) Typical examples of HID class devices include: 0x02 (Mouse) • Keyboard and pointing devices 0x03-0xFF (Reserved) • Control switches, sliders and so on A host communicates with the HID class device using • Joystick, steering and other gaming control inputs either the control (default) pipe or an interrupt pipe. • Point-of-sale bar code scanners and magnetic card The control pipe is used for: readers having an HID Keyboard Emulation mode • Sending and receiving the control transfer data. The HID class can be used for devices without human interface, too; such applications just need to be able to • Transmitting and receiving reports if the interrupt function within the limits of the HID class specifications. endpoint is not used by the device. The interrupt pipe is used for: Key Features of HID Class • Transmitting and receiving reports to and from the • Data is exchanged between the host and the device. device in the form of reports. The format of the • Transmitting fixed latency data to or from the report is defined by the report descriptor defined device. by the device based on device need. The interrupt OUT pipe is optional. If a device declares • An HID interface uses Interrupt Transfer mode to an interrupt OUT endpoint, then the output reports are move the data. transmitted by the host to the device through the • An HID interface must have at least one interrupt IN interrupt OUT endpoint. If no interrupt OUT endpoint is endpoint for sending the input report. The HID declared, then the output reports are transmitted to a interface also has an optional interrupt OUT device through the control endpoint. endpoint. If the interrupt OUT endpoint is not defined, the output report can be sent over the control OUT endpoint. However, the application must ensure that the transfers over the control endpoint are not frequent. • As an HID class uses Interrupt Transfer mode, a maximum of 64 bytes can be transferred in a single frame (i.e., 64 Kbyte/s per endpoint when operating in Full-Speed mode). © 2008 Microchip Technology Inc. DS01144A-page 1 AN1144 THE HID CLIENT DRIVER Architecture of HID Client Driver Applications do not interface directly with the USB HID client driver. Instead, they use an application interface layer which will interface with the client driver, which in turn, will use the host layer. Figure 1 displays the USB HID host application interface functions. FIGURE 1: USB HID HOST ARCHITECTURE Application Layer (Keypad, Joystick...) HID Interface Layer HID Parser/Client Layer USB Embedded Host Layer The HID interface layer comprises the HID parser Note: For detailed information about the USB interfaces and interface functions to send and receive host HID driver API and HID parser, refer reports to and from the device. The report descriptor is to the API documentation provided in the parsed and the data is stored in predefined structures. Help directory. The parser will populate these data structures with information extracted from the report descriptor. The application can use the functions defined in the interface layer to understand the report descriptor. DS01144A-page 2 © 2008 Microchip Technology Inc. AN1144 USING THE HID CLIENT DRIVER Configuring the USB HID Class This section provides a brief overview of the installation Use the configuration tool, USBConfig.exe, to and configuration procedures. For detailed information configure the HID client driver for an application. This on installation and configuration, refer to AN1140, tool is installed in the .\Microchip\USB directory. “USB Embedded Host Stack” and AN1141, “USB The following sections provide a brief description about Embedded Host Stack Programmer’s Guide”. the configuration of USBConfig.exe. Main Tab Installing the HID Client Driver To use the HID client driver for a USB embedded host, The HID client driver is installed as part of the complete the USB Embedded Host radio button in the Main tab USB embedded host support package, available on the will be selected by default, as displayed in Figure 2. Microchip web site (http://www.microchip.com/usb). FIGURE 2: USB CONFIGURATION – MAIN TAB © 2008 Microchip Technology Inc. DS01144A-page 3 AN1144 Host Tab 1. Click the Host tab to configure basic host operation, as displayed in Figure 3. FIGURE 3: USB CONFIGURATION – HOST TAB The HID client driver requires support for control and 3. Some devices also require longer than the USB interrupt endpoints. specification of 100 ms to initialize after 2. Under Transfer Types, select the Support Inter- power-up; it is recommended to increase the rupt Transfers check box and enter the required attach debounce interval. NAKs in the text box. Even though Full-Speed 4. Enter the name of the function in the main USB mode supports a 1 ms communication frame, source file that serves as the application level the fastest transfer in most HID applications is at a event handler. rate of 10 ms. If the report from the device is 5. Select the Generate Transfer Events check box unavailable, the device NAK is the response to utilize transfer events (EVENT_TRANSFER) received by the host. As the Idle rate implemented from the USB host layer. Refer to the “Event on the device can vary from 0.004 sec to Generation” section for more information about 1.020 sec, users should configure the Number of transfer events. NAKs Allowed field in sync with the implementa- tion on the device end. Unselect the Support Bulk Transfers and Support Isochronous Transfers if the application does not contain classes that require bulk or isochronous endpoints. DS01144A-page 4 © 2008 Microchip Technology Inc. AN1144 HID Tab The USB HID client driver can either poll the USB host driver for transfer status or respond to the USB host driver transfer events. 1. Select the HID tab. 2. Select the HID Client is used in Host mode check box to enable support for an HID embedded host, as displayed in Figure 4. FIGURE 4: USB CONFIGURATION – HID TAB The HID report from the device can contain multiple The user application has to collect the information data fields of varied length in bits. stored by the HID parser. The configuration tool 3. Enter the required value in the Maximum Data provides an edit box to enter the function to collect this Field Size (bits) text box. This value informs the information. If the parser information is not required by HID client driver of the maximum size of the data the user application, leave the check box unselected. field received from the device within a report. Note: Refer to AN1212 “Using USB Keyboard with 4. Select the Default Interface radio button to use an Embedded Host” for implementation the default application interface. details. To customize the application interface, select the Custom Interface radio button and provide the initialization and event handler functions. © 2008 Microchip Technology Inc. DS01144A-page 5 AN1144 Defining the Callback Handlers TABLE 1: EVENT CONFIGURATIONS The client driver requires two callback handlers in the USB Host Layer USB Host HID Driver interface layer. Poll for transfer status Poll for HID transfer status Initialization Event Handler – This is called after the Poll for transfer status Generate HID transfer peripheral has been enumerated and initialized by the events host layer. The initialization handler should be of the Generate transfer Poll for HID transfer status type defined by the typedef: events typedef BOOL (*USB_CLIENT_INIT) (BYTE Generate transfer Generate HID transfer address, DWORD flags); events events This function performs initialization specific to the device. If initialization occurs with no error, this routine If the USB embedded host transfer events are used: should return TRUE; otherwise, this routine should • The application would require more program and return FALSE. No transfers to the peripheral will be data memory, but the application processing will allowed. be more efficient. The USB embedded host Event Handler – This is required to handle events that transfer event configuration is transparent to the occur during normal operation. This event handler interface layer. should be of the type defined by the typedef: • More program memory is required and the typedef BOOL (*USB_CLIENT_EVENT_HANDLER) interface layer that handles these events must be (BYTE address, USB_EVENT event, void*data, structured properly. In general, the code DWORD size); architecture required to utilize the transfer events For example, the event, EVENT_DETACH, occurs when is more sophisticated. It may be more difficult for a device has detached from the bus. In this case, the C programming learners to design, develop, interface layer will need to update its status by doing debug and maintain. operations, such as removal of the device from its list The choice of whether or not to utilize the USB of attached devices.