Generic Configuration
Some constants are available to customize the class. These constants are located in the USB device configuration file, usbd_cfg.h
. shows their description.
The HID class uses an internal task to manage periodic input reports. The task priority and stack size shown in are defined in the application configuration file, app_cfg.h
. Refer to the HID Periodic Input Reports Task page for more details about the HID internal task.
Class Instance Configuration
Before starting the communication phase, your application needs to initialize and configure the class to suit its needs. summarizes the initialization functions provided by the HID class. For more details about the functions parameters, refer to the HID API Reference.
You need to call these functions in the order shown below to successfully initialize the HID class:
- Call
USBD_HID_Init()
This is the first function you should call and you should do it only once even if you use multiple class instances. This function initializes all internal structures and variables that the class needs and also the HID OS layer. - Call
USBD_HID_Add()
This function allocates an HID class instance. It also allows you to specify the following instance characteristics: - The country code of the localized HID hardware.
- The Report descriptor content and size.
- The Physical descriptor content and size.
- The polling internal for the interrupt IN endpoint.
- The polling internal for the interrupt OUT endpoint.
- A flag enabling or disabling the Output reports reception with the control endpoint. When the control endpoint is not used, the interrupt OUT endpoint is used instead to receive Output reports.
- A structure that contains 4 application callbacks used for class-specific requests processing.
- Call
USBD_HID_CfgAdd()
Finally, once the HID class instance has been created, you must add it to a specific configuration.
illustrates the use of the previous functions for initializing the HID class.
also illustrates an example of multiple configurations. The functions USBD_HID_Add()
and USBD_HID_CfgAdd()
allow you to create multiple configurations and multiple instances architecture. Refer to for more details about multiple class instances.
presents an example of table declaration defining a Report descriptor corresponding to a mouse. The example matches the mouse report descriptor viewed by the host HID parser in . The mouse report represents an Input report. Refer to the Report section for more details about the Report descriptor format. The items inside a collection are intentionally indented for code clarity.
Class Instance Communication
The HID class offers the following functions to communicate with the host. For more details about the functions parameters, refer to the HID API Reference.
Synchronous Communication
Synchronous communication means that the transfer is blocking. Upon function call, the applications blocks until the transfer completion with or without an error. A timeout can be specified to avoid waiting forever.
presents a read and write example to receive data from the host using the interrupt OUT endpoint and to send data to the host using the interrupt IN endpoint.
Asynchronous Communication
Asynchronous communication means that the transfer is non-blocking. Upon function call, the application passes the transfer information to the device stack and does not block. Other application processing can be done while the transfer is in progress over the USB bus. Once the transfer is completed, a callback is called by the device stack to inform the application about the transfer completion.
shows an example of an asynchronous read and write.