Mistake on this page?
Report an issue in GitHub or email us


USBHID class hierarchy

You can use the USBHID class to turn an Mbed board into an HID (Human Interface Device) that can send and receive messages over USB. For example, you can define your own protocol and communicate between your computer and Mbed with all capabilities of USB communication. To use USBHID, you need a script running on the host side (computer). For example, on a Windows machine, you can use pywinusb. For convenience, there is a Python script attached below that uses pywinusb that can be run on the host computer to send and receive data from the Mbed board.

USBHID class reference

Public Member Functions
 USBHID (bool connect_blocking=true, uint8_t output_report_length=64, uint8_t input_report_length=64, uint16_t vendor_id=0x1234, uint16_t product_id=0x0006, uint16_t product_release=0x0001)
 Basic constructor. More...
 USBHID (USBPhy *phy, uint8_t output_report_length, uint8_t input_report_length, uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
 Fully featured constructor. More...
virtual ~USBHID ()
 Destroy this object. More...
bool ready ()
 Check if this class is ready. More...
void wait_ready ()
 Block until this HID device is in the configured state. More...
bool send (const HID_REPORT *report)
 Send a Report. More...
bool send_nb (const HID_REPORT *report)
 Send a Report. More...
bool read (HID_REPORT *report)
 Read a report: blocking. More...
bool read_nb (HID_REPORT *report)
 Read a report: non blocking. More...
void init ()
 Initialize this instance. More...
void deinit ()
 Power down this instance. More...
bool configured ()
 Check if the device is configured. More...
void connect ()
 Connect a device. More...
void disconnect ()
 Disconnect a device. More...
void sof_enable ()
 Enable the start of frame interrupt. More...
void sof_disable ()
 Disable the start of frame interrupt. More...
bool endpoint_add (usb_ep_t endpoint, uint32_t max_packet, usb_ep_type_t type, ep_cb_t callback=NULL)
 Add an endpoint. More...
template<typename T >
bool endpoint_add (usb_ep_t endpoint, uint32_t max_packet, usb_ep_type_t type, void(T::*callback)())
 Add an endpoint. More...
void endpoint_remove (usb_ep_t endpoint)
 Remove an endpoint. More...
void endpoint_remove_all ()
 Remove all non-zero endpoints. More...
void endpoint_stall (usb_ep_t endpoint)
 Stall an endpoint. More...
void endpoint_unstall (usb_ep_t endpoint)
 Unstall an endpoint. More...
uint32_t endpoint_max_packet_size (usb_ep_t endpoint)
 Get the current maximum size for this endpoint. More...
void endpoint_abort (usb_ep_t endpoint)
 Abort the current transfer on this endpoint. More...
bool read_start (usb_ep_t endpoint, uint8_t *buffer, uint32_t size)
 start a read on the given endpoint More...
uint32_t read_finish (usb_ep_t endpoint)
 Get the status of a read. More...
bool write_start (usb_ep_t endpoint, uint8_t *buffer, uint32_t size)
 Write a data to the given endpoint. More...
uint32_t write_finish (usb_ep_t endpoint)
 Get the status of a write. More...
Protected Member Functions
virtual void callback_state_change (DeviceState new_state)
 Called when USB changes state. More...
virtual void callback_request (const setup_packet_t *setup)
 Called by USBDevice on Endpoint0 request. More...
virtual void callback_request_xfer_done (const setup_packet_t *setup, bool aborted)
 Called by USBDevice on data stage completion. More...
virtual void callback_power (bool powered)
 Called by USBDevice layer on power state change. More...
virtual void callback_sof (int frame_number)
 Called by USBDevice layer on each new USB frame. More...
virtual void callback_reset ()
 Called by USBDevice layer on bus reset. More...
void complete_request (RequestResult result, uint8_t *data=NULL, uint32_t size=0)
 Called to complete the setup stage of a callback request. More...
void complete_request_xfer_done (bool success)
 Called to complete the data stage of a callback request. More...
void complete_set_configuration (bool success)
 Called to complete a set configuration command. More...
void complete_set_interface (bool success)
 Called to complete a set interface command. More...
uint8_t * find_descriptor (uint8_t descriptor_type, uint8_t index=0)
 Find a descriptor type inside the configuration descriptor. More...
const usb_ep_table_tendpoint_table ()
 Get the endpoint table of this device. More...
virtual void start_process ()
 Callback called to indicate the USB processing needs to be done. More...
virtual void lock ()
 Acquire exclusive access to this instance USBDevice. More...
virtual void unlock ()
 Release exclusive access to this instance USBDevice. More...
virtual void assert_locked ()
 Assert that the current thread of execution holds the lock. More...

USBHID example


#include "mbed.h"
#include "USBHID.h"

//We declare a USBHID device. By default input and output reports are 64 bytes long.
USBHID hid(true, 8, 8);

Serial pc(USBTX, USBRX);

//This report will contain data to be sent
HID_REPORT send_report;
HID_REPORT recv_report;

DigitalOut l1(LED1);

int main(void) {
    send_report.length = 8;

    while (1) {

        //Fill the report
        for (int i = 0; i < send_report.length; i++)
            send_report.data[i] = rand() & 0xff;

        //Send the report

        //try to read a msg
        if(hid.read_nb(&recv_report)) {
            l1 = !l1;
            for(int i = 0; i < recv_report.length; i++) {
                pc.printf("%d ", recv_report.data[i]);


To use this script, first flash the Mbed board with the code above. Connect the target's USB to the host computer. Then run this script. The script will send 8 bytes of data (1 2 3 4 5 6 7 8) to the Mbed board and will read and print the data sent to the host computer by the Mbed board.

from pywinusb import hid

# Whenever the host computer receives data from the
# Mbed board, the received data is printed
def on_data(data):
    print("Got message %s" % data)

Gets all HIDs currently connected to host computer,
and sets the first device with string "mbed" in its
vendor name equal to variable mbed. This variable
will be used to send data to the Mbed board.
all_devices = hid.find_all_hid_devices()
mbeds = [dev for dev in all_devices if dev.vendor_name.find("mbed") >= 0]
if len(mbeds) == 0:
    print("No HID devices found")
mbed = mbeds[0]

# Sends 8 bytes of data to the Mbed board
# The Mbed board should receive the data "1 2 3 4 5 6 7 8"
message = bytearray(9)
message[1] = 1
message[2] = 2
message[3] = 3
message[4] = 4
message[5] = 5
message[6] = 6
message[7] = 7
message[8] = 8


Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.