7 years, 8 months ago.

trying to implement usbhid with Nucleo F429ZI

Hello all,

iam trying to build a USBHid Device with the provided examples, that can be found here: https://developer.mbed.org/handbook/USBHID.

In general the example is working, but there arrives no data at the controller.

The statement if(hid.read(&recv_report)) in line 33 evaluates to true, when i send messages with the python code attached below, but the report always has a length of 0 and it contains no data.

The problem is the same with mbed 2 and mbed 5.

Does anybody got an idea of what might be wrong with my code?

slightly modified version of example

#include "mbed.h"
#include "USBHID.h"
 
//We declare a USBHID device. Input out output reports have a length of 8 bytes
USBHID hid(8, 8);
 
//This report will contain data to be sent
HID_REPORT send_report;
HID_REPORT recv_report;
 
Serial pc(USBTX, USBRX);
Timer debugTimer;

int main(void) {
    debugTimer.start();

    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.read(&recv_report)) 
        {
            pc.printf("received at %d, length of report %d: ", debugTimer.read_us(), recv_report.length);
            for(int i = 0; i < 8; i++) 
            {
                pc.printf("%d ", recv_report.data[i]);
            }
            pc.printf("\r\n");
        }
        wait(0.2);
    }
}

python side with pywinusb

import pywinusb.hid as hid
from time import sleep
import random

# handler called when a report is received
def rx_handler(data):
    print 'recv: ', data

def findHIDDevice(mbed_usage, mbed_vendor_id):
    # Find all devices connected
    all_devices = hid.HidDeviceFilter(vendor_id = mbed_vendor_id).get_devices()

    if not all_devices:
        print "No device connected"
    else:
        # search for the Mbed
        for HIDdevice in all_devices:
            try:
                HIDdevice.open()
                # browse output reports
                for report in HIDdevice.find_output_reports():
                    if mbed_usage in report:

                        #MBED found
                        print 'Mbed detected'

                        print report

                        #Attach a custom handler when a data is received
                        HIDdevice.set_raw_data_handler(rx_handler)

                        #send a report each 0.2 second. The report is a random array of 8 bytes
                        while True:
                            for i in range(8):
                                report[mbed_usage][i] = random.randint(0, 255)
                            report.send()
                            sleep(0.2)
            except:
                print 'close'
                print traceback.format_exc()
                HIDdevice.close()


if __name__ == '__main__':
    # The vendor ID used in the Mbed program
    mbed_vendor_id = 0x1234

    # Vendor page and usage_id = 2
    mbed_usage = hid.get_full_usage_id(0xffab, 0x02)

    # Search the Mbed, attach rx handler and send data
    findHIDDevice(mbed_usage, mbed_vendor_id)

Maybe there is nothing wrong with the code. Have you checked using Putty with a second PC that the python is indeed sending the information. This could indicate that the hardware has a connection problem. Check for a good ground between the two devices. All the usual things.

posted by Darren Ulrich 01 Mar 2017

Thanks a lot for your suggestion. My hardwaresetup is, that i connect the Nucleo board with one USB cable to the ST-Link, that powers the board and a second USB-cable to the CN13 USB port. No other cables are connected. I also tried it with the ST-Link cable unplugged and external power supply, but that does not help. I dont know, how i could improve the hardware connections so far.

Another thing: I dont know if you maybe missunderstood my setup concerning the suggestion with putty. Iam not talking about a serial communication, but a communication with raw data over a USB-HID interface. Or am i missing the fact that putty can listen to an usb connection, maybe?

posted by Step Mor 01 Mar 2017
Be the first to answer this question.