USBD ROM Stack  2.0
ROM based USB device stack
How to implement simple HID device ?

The HID class consists primarily of devices that are used by humans to control the operation of computer systems. Typical examples of HID class devices include Keyboards and pointing devices — for example, standard mouse devices, trackballs, and joysticks.

Initialization

After successfully initializing the USBD ROM stack i.e. USBD_HW_API_T::Init, the HID class driver needs to be initialized as follows:

Defining USB Descriptors

The following example shows the configuration descriptor for a USB HID Mouse device:

/* All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */
ALIGNED(4) uint8_t USB_FsConfigDescriptor[] = {
/* Configuration 1 */
USB_CONFIGURATION_DESC_SIZE, /* bLength */
USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType */
WBVAL( /* wTotalLength */
USB_CONFIGURATION_DESC_SIZE +
USB_INTERFACE_DESC_SIZE +
HID_DESC_SIZE +
USB_ENDPOINT_DESC_SIZE
),
0x01, /* bNumInterfaces */
0x01, /* bConfigurationValue */
0x00, /* iConfiguration */
USB_CONFIG_SELF_POWERED, /* bmAttributes */
USB_CONFIG_POWER_MA(2), /* bMaxPower */
/* Interface 0, Alternate Setting 0, HID Class */
USB_INTERFACE_DESC_SIZE, /* bLength */
USB_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType */
0x00, /* bInterfaceNumber */
0x00, /* bAlternateSetting */
0x01, /* bNumEndpoints */
USB_DEVICE_CLASS_HUMAN_INTERFACE, /* bInterfaceClass */
HID_SUBCLASS_BOOT, /* bInterfaceSubClass */
HID_PROTOCOL_MOUSE, /* bInterfaceProtocol */
0x04, /* iInterface */
/* HID Class Descriptor */
/* HID_DESC_OFFSET = 0x0012 */
HID_DESC_SIZE, /* bLength */
HID_HID_DESCRIPTOR_TYPE, /* bDescriptorType */
WBVAL(0x0111), /* bcdHID : 1.11*/
0x00, /* bCountryCode */
0x01, /* bNumDescriptors */
HID_REPORT_DESCRIPTOR_TYPE, /* bDescriptorType */
WBVAL(sizeof(Mouse_ReportDescriptor)), /* wDescriptorLength */
/* Endpoint Descriptor, HID Interrupt In */
USB_ENDPOINT_DESC_SIZE, /* bLength */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType */
HID_EP_IN, /* bEndpointAddress */
USB_ENDPOINT_TYPE_INTERRUPT, /* bmAttributes */
WBVAL(0x0008), /* wMaxPacketSize */
HID_MOUSE_REPORT_INTERVAL, /* bInterval */
/* Terminator */
0 /* bLength */
};

The following example shows the Report Descriptor for the USB HID Mouse Device

const uint8_t Mouse_ReportDescriptor[] = {
HID_UsagePage(HID_USAGE_PAGE_GENERIC),
HID_Usage(HID_USAGE_GENERIC_MOUSE),
HID_Collection(HID_Application),
HID_Collection(HID_Physical),
HID_UsagePage(HID_USAGE_PAGE_BUTTON),
HID_UsageMin(1),
HID_UsageMax(3),
HID_LogicalMin(0),
HID_LogicalMax(1),
HID_ReportCount(3), /* Indicates three bit field data for buttons */
HID_ReportSize(1), /* 1 bit data field */
HID_Input(HID_Data | HID_Variable | HID_Absolute), /* Data field is Input */
HID_ReportCount(1), /* Indicates one 5 bit field data for padding */
HID_ReportSize(5),
HID_Input(HID_Constant), /* Constant data for padding */
HID_UsagePage(HID_USAGE_PAGE_GENERIC),
HID_Usage(HID_USAGE_GENERIC_X),
HID_Usage(HID_USAGE_GENERIC_Y),
HID_LogicalMin( (uint8_t) -127),
HID_LogicalMax(127),
HID_ReportSize(8), /* 8 bit data field */
HID_ReportCount(2), /* Indicates 2 bytes for X, Y data */
HID_Input(HID_Data | HID_Variable | HID_Relative), /* Data field is Input */
HID_EndCollection,
HID_EndCollection,
};

Execution

After a successful HID Initialization the stack is ready to receive the packets from host. Even if the device is physically connected to the host using an USB cable the host does not recognize the presence of device until the D+ line is pulled up using 1.5K ohm resistor (LPC device controllers operate in high-speed or full-speed mode only). To enable the connection the application software should call USBD_HW_API_T::Connect with enable set to 1. Before enabling the connection the application should enable the USB interrupt.

NVIC_EnableIRQ(USB0_IRQn); // enable USB0 interrupts
/* now connect */
USBD_API->hw->Connect(g_hUsb, 1);

After connection the HID class executes through callback functions setup during initialization. The application could also have a task for collecting the report data and preparing the HID report, which would be provided whenever the Get Report or EP In call back is called by the stack.

Installing IRQ handlers

USBD ROM stack implements the interrupt handler for USB device controller and invokes the USBD ROM stack routines according to the event type. Hence application should connect this handler to the application vector table. This is done by calling USBD_HW_API_T::ISR from the USB IRQ handler as shown in the code snippet below. The USBD ROM handle passed to the ISR() routine is the one obtained in previous step.

void USB0_IRQHandler(void)
{
USBD_API->hw->ISR(g_hUsb);
}

Implementing callback

The USBD Stack mandates the Get Report and Set report call back to be implemented by the application. These callbacks are provided with the SETUP packet data that was received for the request, a double pointer to the data buffer and the length. With the SETUP packet data the callback should parse the report type and the report ID and provide the appropriate data or act on the data.

The Endpoint Handlers are called upon receiving/requesting data on the HID Endpoint. The stack provides the pointer to the Core Control Structure, HID control structure and the event that occurred on the endpoint. The application could implement these call backs to read or write data to the endpoint depending on the event.

Subsections: