USB Libraries Help > USB Device Libraries > USB HID Device Library > Using the Library > How the Library Works > Library Initialization
MPLAB Harmony USB Stack
Library Initialization

The HID Function Driver instance for a USB device configuration is initialized by the Device Layer when the configuration is set by the Host. This process does not require application intervention. Each instance of the HID Function be registered with the Device Layer through the Device Layer Function Driver Registration Table. The HID function driver requires a initialization data structure that contains details about the report descriptor and the reports send/receive queue size associated with the specific instance of the HID Function Driver. The funcDriver member of the registration entry must be set to USB_DEVICE_HID_FUNCTION_DRIVER. This object is a global object provided by the HID Function Driver and points to the HID Function Driver - Device Layer interface functions, which are required by the Device Layer. The following code shows an example of how a HID Function Driver instance (implementing a USB HID Mouse) can be registered with the Device Layer.

/* This code shows an example of registering a HID function driver
 * with the Device Layer. While registering the function driver, an initialization
 * data structure must be specified. In this example, hidInit is the HID function
 * driver initialization data structure. */

/* This hid_rpt01 report descriptor describes a 3 button 2
 * axis mouse pointing device */
const uint8_t hid_rpt01[]=
{
    0x06, 0x00, 0xFF,    // Usage Page = 0xFF00 (Vendor Defined Page 1)
    0x09, 0x01,          // Usage (Vendor Usage 1)
    0xA1, 0x01,          // Collection (Application)
    0x19, 0x01,          // Usage Minimum
    0x29, 0x40,          // Usage Maximum     //64 input usages total (0x01 to 0x40)
    0x15, 0x01,          // Logical Minimum (data bytes in the report may have minimum value = 0x00)
    0x25, 0x40,          // Logical Maximum (data bytes in the report may have maximum value = 0x00FF = unsigned 255)
    0x75, 0x08,          // Report Size: 8-bit field size
    0x95, 0x40,          // Report Count: Make sixty-four 8-bit fields (the next time the parser hits an "Input", "Output",
                         // or "Feature" item)
    0x81, 0x00,          // Input (Data, Array, Abs): Instantiates input packet fields based on the previous report size,
                         // count, logical min/max, and usage.
    0x19, 0x01,          // Usage Minimum
    0x29, 0x40,          // Usage Maximum     //64 output usages total (0x01 to 0x40)
    0x91, 0x00,          // Output (Data, Array, Abs): Instantiates output packet fields.  Uses same report size and
                         // count as "Input" fields, since nothing new or different was specified to the parser since
                         // the "Input" item.
    0xC0                 // End Collection
};

/* HID Function Driver Initialization data structure. This
 * contains the size of the report descriptor and a pointer
 * to the report descriptor. If there are multiple HID instances
 * each with different report descriptors, multiple such data
 * structures may be needed */

USB_DEVICE_HID_INITIALIZATION hidInit =
{
    sizeof(hid_rpt01), // Size of the report
    (uint8_t *)&hid_rpt01 // Pointer to the report
    1, // Send queue size is 1. We will not queue up reports.
    0 // Receive queue size 0. We will not receive reports.
};

/* The HID function driver instance is now registered with
 * device layer through the function driver registration
 * table. */

const USB_DEVICE_FUNCTION_REGISTRATION_TABLE funcRegistrationTable[1] =
{
    {
         .speed = USB_SPEED_FULL|USB_SPEED_HIGH,    // Supported speed
         .configurationValue = 1,                   // To be initialized for Configuration 1
         .interfaceNumber = 0,                      // Starting interface number
         .numberOfInterfaces = 1,                   // Number of Interfaces
         .funcDriverIndex = 0,                      // Function Driver instance index is 0
         .funcDriverInit = &hidInit,                // Function Driver Initialization
         .driver = USB_DEVICE_HID_FUNCTION_DRIVER   // Pointer to the function driver - Device Layer Interface functions
     }
};