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

USBHID

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 This method can also be used to resume USB operation when USB power is detected after it was suspended via USBDevice::deinit. 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, mbed::Callback< void()> 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)
 Un-stall 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

main.cpp

#include <stdio.h>

#include "mbed.h"
#include "drivers/USBHID.h"

// Declare a USBHID device
USBHID HID(8, 8, 0x1234, 0x0006, 0x0001, true);

HID_REPORT output_report = {
    .length = 8,
    .data = {0}
};
HID_REPORT input_report = {
    .length = 0,
    .data = {0}
};

DigitalOut led_out(LED1);

int main(void)
{
    while (1) {

        // Fill the report
        for (int i = 0; i < output_report.length; i++) {
            output_report.data[i] = rand() & UINT8_MAX;
        }

        // Send the report
        HID.send(&output_report);

        // Try to read a msg
        if (HID.read_nb(&input_report)) {
            led_out = !led_out;
            for (int i = 0; i < input_report.length; i++) {
                printf("%d ", input_report.data[i]);
            }
            printf("\r\n");
        }
    }
}

USBHID.py

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(f"Got message {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_hid_devices = hid.find_all_hid_devices()
mbed_devices = [d for d in all_hid_devices if "mbed" in d.vendor_name]

if mbed_devices is None:
    raise ValueError("No HID devices found")

# A buffer of bytes representing the values 1-8
# The first byte is the report ID which must be 0
buffer = [0, 1, 2, 3, 4, 5, 6, 7, 8]

mbed_devices[0].open()
# Set custom raw data handler
mbed_devices[0].set_raw_data_handler(on_data)

# Send the message to the Mbed board
out_report = mbed_devices[0].find_output_reports()
out_report[0].set_raw_data(buffer)
out_report[0].send()
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.