USBHID

The USBHID class can be used to send and receive messages over USB. For instance, you can define your own protocol and communicate between your computer and the Mbed with all capabilities of a USB communication. To use USBHID, you need a script running on the host side (computer). For instance on a 32 bits Windows 7 machine, you can use pywinusb.

The USB connector should be attached to

  • p31 (D+), p32 (D-) and GND for the LPC1768 and the LPC11U24
  • The on-board USB connector of the FRDM-KL25Z

Hello World

» Import this program

#include "mbed.h"
#include "USBHID.h"
 
//We declare a USBHID device. By default input and output reports are 64 bytes long.
USBHID hid(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
        hid.send(&send_report);
 
        //try to read a msg
        if(hid.readNB(&recv_report)) {
            l1 = !l1;
            for(int i = 1; i < recv_report.length; i++) {
                pc.printf("%d ", recv_report.data[i]);
            }
            pc.printf("\r\n");
        }
    }
}

API

» Import this library into a program

Public Member Functions

  USBHID (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, bool connect=true)
  Constructor.
bool  send (HID_REPORT *report)
  Send a Report.
bool  sendNB (HID_REPORT *report)
  Send a Report.
bool  read (HID_REPORT *report)
  Read a report: blocking.
bool  readNB (HID_REPORT *report)
  Read a report: non blocking.

Details

You can choose the length of exchanged packets. In this example, mbed leds are controlled by four switches. When you press a button, there is a message sent containing buttons state. According to this message, the mbed will receive back a new message to light on leds.

We need one byte to control leds and one byte to send buttons state.

USBHID, buttons and leds

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

//We declare a USBHID device: it can send 1 byte and receive 1 byte
USBHID hid(1, 1);

//Two reports where will be stored values to send and received
HID_REPORT recv_report;
HID_REPORT send_report;

//Bus of leds
BusOut leds(LED1,LED2,LED3,LED4);

//Bus of buttons
BusInOut buttons(p21, p22, p23, p24);

int main(void) {
    uint8_t p_bus = 0;
    send_report.length = 1;

    while (1) {
        //If a data is received, update led bus
        if (hid.readNB(&recv_report)) {
            leds = recv_report.data[0];
        }

        //if the bus of buttons has changed, send a report
        if (buttons.read() != p_bus) {
            p_bus = buttons.read();
            send_report.data[0] = p_bus;
            hid.send(&send_report);
        }
        wait(0.01);
    }
}

Contribute to the USBHID bindings webpage!

A great thing would be to develop in several languages running on different platforms, programs able to communicate with the mbed over USB. Visit the USBHID bindings webpage and develop your own USBHID device!




5 comments:

20 Dec 2012

I have imported the USBHID_HelloWorld to my online Compiler. Because nothing was displayed on my Term, I don't know if the code is really running on my mbed. So I added a printf() at the first line of main:

[...]
int main(void) {
    pc.printf("Start"); // insert, should print a message on my Term
    send_report.length = 8;
[...]

Also this doesn't show any messages on my Term. The settings on my PC are Baud 9600, Data 8, stop 1, Parity none. Normally it is working with those settings.

Besides I also can't see the Blue Lights of Death, when using:

int main(void) {
    error(""); // insert, Blue Lights of Death
    send_report.length = 8;
[...]

Anybody know why the code is not working on my mbed?

20 Dec 2012

The interface is HID, not serial

have a look at SimpleHID,

Scroll down a little and download SimpleHIDWrite.

Probably the best stand alone HID programm out there !!

Enjoy!

Ceri

20 Dec 2012

Thank you for your answer. Unfortunately now I have more questions than before. When the interface is HID and not serial, does this means it disables any kind of communication from the on the board mini-USB port to the computer? Also it disables Blue Lights of Death? The programm is also not really usefull to me. I think I need to know more about HID, maybe this isn't what I'm searching for. When I start SimpleHID, I can select my standard HID Devices Mouse and Keyboard. But can't interact with them, because the lower buttons are disabled permanently.

20 Dec 2012

These libraries use the mbeds USB pins, not the USB connector. The USB connector is via a serial <> USB bridge connected to a serial port of the mbed, and can only be used for that. The USB pins can be used for alot more, such as this.

That nothing happens in your code is most likely because in the constructor of the USB HID device there is a 'connect' option, which is by default true. So it tries to connect the USB, but since there is nothing it probably hangs for ethernity there.

20 Dec 2012

Thank you Erik. That was my problem. The programm can't finish connection. For test purposes I negated the if statement, the code works and it shows the Blue Lights of Death. I had a device connected to D+/D-, but it is not a computer. Also it needs Ethernet communication over USB, so the Ethernet frames will be the data of the USB pakets. Is a connection on the USB level needed before sending 'data'? Always sending broadcasts at USB is ok to me, because I need to connect on Ethernet level with the ARP. Do you think it is clever to use USBHID? If not what would you recommend for this purpose?

Posting comments for this page has been disabled