Application Note 1163, USB HID Class On An Embedded Device

2y ago
36 Views
2 Downloads
469.95 KB
26 Pages
Last View : 15d ago
Last Download : 3m ago
Upload by : Casen Newsome
Transcription

AN1163USB HID Class on an Embedded DeviceAuthor:Sean JusticeMicrochip Technology Inc.INTRODUCTIONThe Human Interface Device (HID) is a class for usewith Universal Serial Bus (USB). The HID class consists of devices that a human may use to control theoperation of computer systems. Some of these devicesinclude, but are not limited to: a mouse; a keyboard; ajoystick; buttons; and switches. Along with providinginformation from human interfaces, there are provisions for various types of output to indicate action bythe computer system.This application note discusses and provides a HIDdevice function driver that can be integrated withalmost any application running on Microchip 32-bitPIC microcontroller products with USB peripheral.The HID function driver uses the Microchip PIC32 USBdevice stack.Along with the HID application note, a demo applicationis included that demonstrates the HID device functiondriver. This HID application demo simulates a mouse.ASSUMPTIONSThe author assumes that the reader is familiar with thefollowing Microchip development tools: MPLAB IDEand MPLAB REAL ICE in-circuit emulator. It is alsoassumed that the reader is familiar with C programminglanguage and USB device protocol and descriptors.Terminology from these technologies is used in thisdocument and only brief overviews of the concepts areprovided. Advanced users are encouraged to read theassociated specifications.FEATURESThis application note provides key components of aHID device class driver. The Microchip HID deviceclass driver incorporates the following features: Functions independently of RTOS or application Supports Microchip MPLAB IDE tool suite Supports the HID 1.1 specification, as stated in“Universal Serial Bus (USB) Device Class Definition for Human Devices (HID), Version 1.11”(available on the Internet at the following URL:http://www.usb.org/developers/hidpage/) Uses only one USB endpoint Macros are provided to create HID Reports Handles multiple HID Reports Handles standard HID USB configurationrequests, as stated in Chapter 9 of the “UniversalSerial Bus Specification, Revision 2.0” (availableat ce the HID is developed for use in embeddedsystems, the limitations are those that are inherited bythe USB device stack (refer to Microchip ApplicationNote AN1176, “USB Device Stack for PIC32 Programmer’s Guide”).SYSTEM HARDWAREThis application and firmware was developed for thefollowing hardware: PIC32 Family Microcontroller PIM (ProcessorInterface Module), supporting USB Microchip Explorer 16 Development Board USB PICtail Plus Daughter BoardThe USB device and HID function driver source filescan be modified to use an alternative developmentboard and accommodate most hardware differences. 2008 Microchip Technology Inc.DS01163A-page 1

AN1163PIC MCU MEMORY RESOURCEREQUIREMENTSThe HID function driver inherits all of the memoryrequirements of the USB device stack. Refer to the“Memory Resource Requirements” section ofAN1176, “USB Device Stack for PIC32 Programmer’sGuide” for more information.The HID function driver consumes Flash and RAMmemory as shown in the following table:TABLE 1:INSTALLING SOURCE FILESThe complete source for the Microchip HID functiondriver is available for download from the Microchip website (Appendix F: “Source Code for the HID Function Driver”). The source code is distributed in a singleMicrosoft Windows installation file.Perform the following steps to complete the installation:1.MEMORY REQUIREMENTSMemorySizeFlash3,284 BytesRAM160 Bytes2.3.The HID function driver application defines thefollowing items: USB descriptor table HID report structureThe USB descriptor table and HID report structure, arerequired for any HID function driver application.USB descriptor table memory requirements are shownin the following table:TABLE 2:USB DESCRIPTOR TABLEMemorySizeFlash56 BytesHID report structure memory requirements are shownin the following table:TABLE 3:HID REPORT STRUCTUREMemorySizeFlash1076 Bytes4.Execute the installation file. A Windows installation wizard will guide you through the installationprocess.Before continuing with the installation process,you must accept the software license agreementby clicking I accept.After completion of the installation process, youshould see the following directory structure:a) hid device driverdirectoryunder\PIC32 Solutions\Microchip\USB.This directory contains the source files anddocumentation for the HID function driver.b) usb func hiddirectoryunderPIC32 Solutions\Microchip\Include\USB. This directorycontains the include files for the PIC32 USBdevice stack and HID function driver.c) usb hid mouse device demo directoryunder \PIC32 Solutions. This directorycontains the demo project and source filesfor the HID function driver mouse demo.Refer to the release notes for the latestversion-specific features and limitations.The amount of memory resources consumed by theUSB descriptor table and HID report may vary, depending on various factors, including, but not limited to, thefollowing circumstances: Whether the user wishes to use multiple USBfunction drivers- Number of configurations, interfaces andendpoint configurations Size and definition of HID reportDS01163A-page 2 2008 Microchip Technology Inc.

AN1163SOURCE FILE ORGANIZATIONThe HID device class consists of multiple files that areorganized in multiple directories. Table 4 shows thedirectory structure.TABLE 4:HID SOURCE FILE DIRECTORY STRUCTUREFileDirectoryDescriptionhid.c\PIC32 Solutions\Microchip\USB\hid device driver USB HID device class driverhiddsc.tmpl\PIC32 Solutions\Microchip\USB\hid device driver HID descriptor templatehidreport.tmpl\PIC32 Solutions\Microchip\USB\hid device driver HID report templateusb device hid.h \PIC32 Solutions\Microchip\Include\USBAPI defines and modifiablemacroshidpri.h\PIC32 Solutions\Microchip\USB\hid device driver Private function and macrodefineshiddesc.h\PIC32 Solutions\Microchip\Include\USBUSB HID descriptorshidreport.h\PIC32 Solutions\Microchip\Include\USBHID report definesDEMO APPLICATIONAn application that demonstrates the HID functiondriver by simulating a computer mouse is included withthe Microchip HID function driver. This application isdesigned to run on the Explorer 16 development boardwith Microchip USB device stack software. However,the application can be modified to support any board.The mouse-simulation demo application performs thefollowing services: USB device enumeration for HID function driver Emulates a mouse when switch SW3 (RD6) onthe Explorer 16 board is pressed – the followingactions result in the specified movements:- Press switch SW6 (RD7) to move the mousetoward the right.- Press switches SW6 (RD7) and SW4 (RD13)to move the mouse toward the left.- Press switch SW5 (RA7) to move the mousein an upward direction.- Press switches SW5 (RA7) and SW4 (RD13)to move the mouse in a downward direction. 2008 Microchip Technology Inc.DS01163A-page 3

AN1163Programming the Demo ApplicationBuilding the Demo ApplicationTo program a target with the demo application, youmust have access to an MPLAB REAL ICE in-circuitemulator. The following procedure assumes that youwill be using MPLAB IDE . If not, please refer to yourspecific programmer’s instructions.The demo application included in this application notecan be built using the Microchip C32 C compiler. Ifrequired, port the source to the compiler that youcustomarily use with Microchip microcontrollerproducts.1.This application note includes a predefined mouse HIDproject file for use with MPLAB IDE. The project wascreated using a PIC32 device with USB. If a differentdevice is used, the appropriate device must beselected through the MPLAB IDE menu command.2.3.4.5.6.7.8.9.10.11.12.13.Connect MPLAB REAL ICE in-circuit emulatorto the Explorer 16 board or your target board.Apply power to the target board.Launch MPLAB IDE.Select the PIC32 device supporting USB of yourchoice (required only if you are importing a hexfile previously built).Enable MPLAB REAL ICE as a programmer.Import the previously build hex file into MPLAB,if you wish to use it.If you are rebuilding the hex file, open the projectfile and follow the build procedure to create theapplication hex file.The demo application contains necessary configuration options required for the Explorer 16board. If you are programming another type ofboard, make sure that you select the appropriateoscillator mode from the MPLAB IDEconfiguration settings menu.Select the “Programmer” menu option inMPLAB IDE, and click “Select Programmer- 6REAL ICE.”When MPLAB IDE has detected the REAL ICEin-circuit emulator and the PIC MCU, select the“Programmer” menu option and click “Program”to program the device.After a few seconds, the message “Programming successful” is displayed. If it is not, checkthe board and MPLAB REAL ICE connections.Refer to MPLAB IDE and REAL ICE online helpfor further assistance.Remove power from the board and disconnectthe MPLAB REAL ICE cable from the targetboard.Reapply power to the board and make sure thatthe LCD reads “PIC32 HID Device”. If it doesnot, check your programming steps and repeat,if necessary.DS01163A-page 4In addition, the demo application project uses additional include paths as defined in the “Build Options” ofMPLAB IDE.The following include paths are required: .\ .\Microchip\Include .\.\Microchip\Include 2008 Microchip Technology Inc.

AN1163Table 5 lists the source files that are necessary to buildthe demo application.TABLE 5:DEMO APPLICATION PROJECT FILESFileDirectorymouse demo.c\PIC32 Solutions\usb hid mouse device demoMain demo source fileDescriptionmouse dsc.c\PIC32 Solutions\usb hid mouse device demoMouse HID USB descriptorsmouse report.c\PIC32 Solutions\usb hid mouse device demoHID report for the mousedemoHardwareProfile.h \PIC32 Solutions\usb hid mouse device demoHardware defines for thePIC32usb config.h\PIC32 Solutions\usb hid mouse device demoUSB specific defines forhelper functionshid.c\PIC32 Solutions\Microchip\USB\hid device driver USB HID source filehidpri.h\PIC32 Solutions\Microchip\USB\hid device driver Private function andmacro definitionshid.h\PIC32 Solutions\Microchip\Include\USBUSB HID include filehiddesc.h\PIC32 Solutions\Microchip\Include\USBHID specific descriptordefineshidreport.h\PIC32 Solutions\Microchip\Include\USBHID report structuremacros and definesusb device.c\PIC32 Solutions\Microchip\USBUSB device APIsusb hal.c\PIC32 Solutions\Microchip\USBUSB hardware APIsusb hal core.c\PIC32 Solutions\Microchip\USBUSB hardware core APIsusb.h\PIC32 Solutions\Microchip\USBUSB top-level include fileusb ch9.h\PIC32 Solutions\Microchip\Include\USBUSB defines and support,as in Chapter 9 of the“Universal Serial BusSpecification, Revision2.0”usb common.h\PIC32 Solutions\Microchip\Include\USBUSB common definesusb device.h\PIC32 Solutions\Microchip\Include\USBUSB device defines andAPI prototypesusb hal.h\PIC32 Solutions\Microchip\Include\USBUSB hardware supportmstimer.c\PIC32 Solutions\Microchip\Common1 millisecond timerex16lcd.c\PIC32 Solutions\Microchip\CommonExplorer 16 DevelopmentBoard LCDmstimer.h\PIC32 Solutions\Microchip\Include1 millisecond timerdefinesex16lcd.h\PIC32 Solutions\Microchip\IncludeExplorer 16 DevelopmentBoard LCD defines 2008 Microchip Technology Inc.DS01163A-page 5

AN1163The following is a high-level procedure for building thedemo application. This procedure assumes that youare familiar with MPLAB IDE and will be using MPLABIDE to build the application. If not, refer to the instructions for your programmer to create and build theproject.1.2.3.4.Make sure that source files for the MicrochipHID function driver are installed. If not, pleaserefer to the “Installing Source Files” section.Launch MPLAB IDE and open the project file.Use MPLAB IDE menu commands to build theproject. Note that the demo project is created tocompile properly when the source files arelocated in the wizard-recommended directorystructure. If you have moved or installed thesource files to another location, you must recreate or modify existing project settings to build.See “Building the Demo Application” formore information.The build process should finish successfully. Ifnot, make sure that your MPLAB IDE andcompiler are setup correctly.Application-Specific USB SupportIn using the Microchip PIC32 USB device firmwarestack, the HID demo implements the followingapplication-specific tables: USB Descriptor Table Endpoint Configuration Table Function Driver TableTHE USB DESCRIPTOR TABLEEvery USB device must provide a set of descriptors(data structures) that describe the device and providedetails to the USB host about which class drivers touse. These descriptors are provided, and the information they contain, are clearly defined in Chapter 9 ofthe “Universal Serial Bus Specification, Revision 2.0”and “Universal Serial Bus (USB) Device Class Definition for Human Devices (HID), Version 1.11”. Refer tothese documents for complete details.The USB device descriptors can be organized intothree groups: Device Configuration StringsThe device descriptor identifies the type of device andgives the number of possible configurations.The configuration descriptors describe the types ofinterfaces and endpoints used. This group alsoincludes class-specific descriptors.The string descriptors, although generally optional,provide user-readable information that the host maydisplay.Demo Application Descriptor TableThe descriptor table that is provided by the demo application is included in the source file mouse dsc.c andis outlined in Appendix E: “USB Descriptor TableDefinitions”.The demo application descriptor table can be modifiedto add other interfaces or configurations. However, it isadvisable that a thorough understanding of Chapter 9of the “Universal Serial Bus Specification, Revision 2.0”and other applicable device function-driver-specificspecifications is achieved before attempting to modifya descriptor table.DS01163A-page 6 2008 Microchip Technology Inc.

AN1163THE ENDPOINT CONFIGURATION TABLEThe endpoint configuration table is used by the USBdevice stack to properly configure all endpoints byinterface and alternate setting as defined by thedescriptor table. The table identifies which functiondriver will be used to service events that occur on eachendpoint.Each table entry contains the following information: Maximum packet sizeConfiguration flagsConfiguration numberEndpoint numberInterface numberAlternate settingIndex in device function table for the endpointhandlerThe endpoint configuration table for the demo application contains one entry because the HID function driveronly requires a single endpoint (Interrupt-In). The following table is found in the source file mouse dsc.c.Refer to AN1176, “USB Device Stack for PIC32 Programmer’s Guide” for further information on the endpoint configuration table.EXAMPLE 1:ENDPOINT CONFIGURATION TABLEconst EP CONFIG EpConfigTlb[] {{HID MAX REPORT SIZE,// max pack sizeUSB EP TRANSMIT USB EP HANDSHAKE, // configure for Tx and enable// handshaking1,// configuration number1,// endpoint number0,// interface number0,// alternate setting0// handler funciton index}}; 2008 Microchip Technology Inc.DS01163A-page 7

AN1163FUNCTION DRIVER TABLESince a device may implement more then one class orvendor-specific USB device function driver, theMicrochip PIC32 USB device stack uses a table tomanage access to support the function driver(s). Eachtable entry contains the information necessary to manage a single function driver.Each table entry contains the following information: Initialization routine Event handler routine Initialization flagsThe function driver table for the demo applicationscontains one entry because there is only one functiondriver, HID. The following table can be found in thesource file mouse dsc.c. Refer to MicrochipApplication Note AN1176, “USB Device Stack forPIC32 Programmer’s Guide” for further information onthe function driver table.EXAMPLE 2:FUNCTION DRIVER TABLEconst FUNC DRV DevFuncTbl[] {{HIDInit,// Initialization routineHIDEventHandler,// Event handler routine0// Initialization flags}};DS01163A-page 8 2008 Microchip Technology Inc.

AN1163HID FUNCTION DRIVER OVERVIEWHID DescriptorEvery USB device has descriptor structure associatedwith it. Each device may contain multiple classes, e.g.,HID, which are defined in the interface layer. Figure 1shows a descriptor structure tree that would describean HID class device.The HID descriptor indicates how many other HIDclass-specific descriptors follow it. There must be atleast one report descriptor. Physical descriptors areoptional. A report descriptor describes the format andmeaning of a data report generated by the device. TheHID report descriptor is loaded by the host's HID classdriver using a class-specific request. After being initialized, the device generates reports to indicate when aperson interacts with it.FIGURE 1:USB DESCRIPTOR FOR HID FUNCTION DRIVERDevice DescriptorString orEndpointDescriptorHID DescriptorReport Descriptor 2008 Microchip Technology Inc.PhysicalDescriptor(optional)DS01163A-page 9

AN1163The HID function driver (USB device) communicateswith the HID class driver (USB host) using default pipe(Control) or an Interrupt-In pipe. The Interrupt-In pipe isrequired for the HID function driver to transmit its data,but an optional Interrupt-Out endpoint can also beused, if needed.TABLE 6:Reports generated by the host to the device will betransmitted either through the Interrupt-Out endpoint orthe default endpoint (Control) using the Set Reportrequest.HID ENDPOINT CONFIGURATIONPipeDescriptionRequiredControl (Endpoint 0)USB control, class request codes, and polled data (Message data)Interrupt-InData-in, data from deviceYYInterrupt-OutData-out, data to the deviceNHID Report DescriptorREPORT ITEM TYPESThe HID report descriptor consists of pieces of information called items. Each item describes one aspect ofthe of the report data.Items are divided into the following types:A report item follows a generic format of a one-byteprefix and then a payload. The prefix byte contains thetag, type, and size of the payload. Figure 2 shows theformat of an item’s prefix byte.A short item has an optional data size that may be 0, 1,2 or 4 bytes. Figure 3 shows an example of a short itempayload size of 2 bytes.FIGURE 2:FIGURE 3:3 21 0TagTypeSizeTypeSize0[data][data]12HID REPORT LONG ITEM FORMAT1111DS01163A-page 107 6 5 4HID REPORT SHORT ITEM OF 2 BYTESBytes:Bytes:A long item can have a size of up to 255 bytes. The format of the long type is the one-byte prefix, which is hardcoded at 0xFE, one-byte size of the payload, a onebyte tag, and the payload. Figure 4 shows the format ofa long item.HID REPORT ITEM PREFIX BYTETagFIGURE 4: Short Items Long Items11010Data SizeLong Item Tag[data]123.258 2008 Microchip Technology Inc.

AN1163Report ItemsThe items are divided into the following classes: Input Item Output Item Feature ItemsInput Item class describes information about data provided by a physical control, like a keyboard or mouse.Output Item class describes the data sent to the device,e.g., the LEDs on the keyboard that indicate whetherthe Caps Lock key is toggled on.Feature Item class describes configuration informationthat can be sent to the device.The following example presents a report descriptor thatis included in the HID function driver demo:Usage Page (Generic Desktop),Usage (Mouse),Collection (Application),Usage (Pointer),Collection (Physical),Usage Page (Button Page),Usage Minimum (1),Usage Maximum (3),Logical Minimum (0),Logical Maximum (1),Report Count (3),Report Size (1),Input (Data, Variable, Absolute),Report Count (1),Report Size (5),Input (Constant),Usage Page (Generic Desktop),Usage (X),Usage (Y),Logical Minimum (-127),Logical Maximum (127),Report Count (2),Report Size (8),Input (Data, Variable, Relativ

hidpri.h \PIC32 Solutions\Microchip\USB\hid_device_driver Private function and macro definitions hid.h \PIC32 Solutions\Microchip\Include\USB USB HID include file hiddesc.h \PIC32 Solutions\Microchip\Include\USB HID specific descriptor defines hidreport.h \PIC32 Solutions\Microchip

Related Documents:

Mode Identifier USB Serial Description Notes 000 HID KEY BYGRT HID keyboard, sends B for blue button, Y for yellow, etc. 001 HID KEY 12345 HID keyboard, sends 1,2,3, etc. 002 HID NAR BYGRT HID keyboard, same as mode 000, except keys do not auto-release 003 HID NAR 12345 HID keyboard, same as mode 001, except keys do not auto-release

HID-Device – The device providing the service of human data input and output to and from the host. HID-Host – The device using or requesting the services of a Device. The HID Bluetooth Profile uses USB definition of a HID device in order to leverage the existing class drivers for USB HID devices.

OM-USB-TC User's Guide Introducing the OM-USB-TC 8 Connecting a OM-USB-TC to your computer is easy Installing a data acquisition device has never been easier. The OM-USB-TC relies upon the Microsoft Human Interface Device (HID) class drivers. The HID class drivers ship with every copy of Windows that is designed to work with USB ports.

Inside HID Mobile Access HID Mobile Access users are efficiently enrolled via an easy-to-use, online management portal. From mobile-enabled readers and secure Mobile IDs, HID Mobile Access technology is built to work seamlessly together. Scalability and Cost-Savings HID Mobile Access Portal features:

4. Replace the HID Once satisfied that all other components are OK, the D2S can be replaced. Using the following guide. Diagnosing HID Faults Before replacing the Xenon HID Lamp, it is important to rule out other factors may causing the lighting issues. Flickering or failed lights can be caused by several other components within the HID .

HID SAFE Enterprise is a modern, scalable off-the-shelf software solution that enables organizations to manage identities — employees, contractors, tenants, and visitors — . from a data center down to a single door Physical Identity management family of products Along with HID SAFE Enterprise, the family includes: HID SAFE Visitor Manager

In-box 1m cable or Jabra 1.8m cable How to connect using Anker PowerLine/Newnex cable USB-A USB-C 3m USB-A to USB-C cable How to connect using Startech USB 3.0/USB 2.0 Startech USB Extender Power Adapter USB-A USB-A USB-C In-box

ORGANIZATIONAL BEHAVIOR AND HUMAN PERFORMANCE 18, 131--145 (1977) Hierarchical Level and Leadership Style ARTHUR G. JAGO AND VICTOR H. VROOM School of Organization and Management, Yale University This research investigates the relationship between the hierarchical level of managerial personnel and individual differences in their leadership styles, specifically the degree to which they are .