10 years, 3 months ago.

What controls LED1 in the USBHID_HelloWorld example?

Hi

I have imported the USBHID_HelloWorld example (http://mbed.org/users/samux/code/USBHID_HelloWorld/ - thanks Sam!) and I am using it with the FRDM-KL25Z mbed.

For some reason I can't control the LEDs on the FRDM-KL25Z in main() until the USB is plugged in (and the HID enumerates). I can't see any code in the USB libraries which should affect this, please can anyone explain what's going on?

I have stripped main.cpp to the bare bones below to give a clearer picture of my question. When I run this code I expect the blue LED to flash continually, but it initially stays off until I plug in USB whereupon it flashes as expected:

#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);
 
DigitalOut l1(LED1);
 
int main(void) {
    
    while (1) {
        l1 = 0;
        wait(0.3);   
        l1 = 1;
        wait(0.3);   
    }
}

This is also the case with the other LEDs. It's like they are disabled until the USB is connected.

Why is this happening and how do I control the LEDs before USB is connected?

Thanks!

Steve

1 Answer

10 years, 3 months ago.

The problem is that you there got a 'slightly' smaller version of the complete constructor, where the others load with a default value, the complete one from the .h file is:

    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);

And what you need is the last one: The constructor allready tries to connect it. But you cannot make that false without also including everything else.

So your options are:

Make it somewhere while your main code is already running, so first do some LED flashing, then add the USBHID object.

Or set it to not connect on creation, then with .connect() you can connect it later.

USBHID hid(8, 8, 0x1234, 0x0006, 0x0001, false);

Accepted Answer

Thanks for the fast reply!

posted by Steve Hodges 03 Jan 2014