I just tried the same code on an official LPC1768 board, and both read() and readNB() work perfectly. Therefore, I believe the problem lies in USBHAL_LPC11U.cpp. Since there seems to be some confusion about the behavior I'm expecting, I've modified my test program to illustrate it:
main.cpp
#include "mbed.h"
#include "USBHID.h"
USBHID hid(8, 8);
HID_REPORT report;
DigitalOut led1(LED1);
DigitalOut led4(LED4);
int main(void)
{
while (1) {
//Try to read a message
if(hid.readNB(&report)) {
//Print the message
printf("Got report with length = %i, data = ", report.length);
for(int i = 0; i < report.length; i++) {
printf("%d ", report.data[i]);
}
printf("\r\n");
//Echo the message
hid.send(&report);
//Toggle the LED
led1 = !led1;
}
//Toggle the loop LED
led4 = !led4;
}
}
In this version, LED4 will toggle every time the while loop loops. As such, when using read(), LED4 should only toggle once per report since read() is a blocking call, and won't return until a new report is received. By changing read() to readNB() however, it should toggle continuously since readNB() is a non-blocking call that's returning false when there's no report available, thereby avoiding the if statement but allowing the rest of the loop to run. Now imagine a USB thread that has to respond to RTOS signals/queues/mailboxes in addition to checking for reports, and you can see why readNB() is so important. My problem is that readNB() is broken on the LPC11U24. It still returns true every time I send it a report, but it will randomly fail to copy said report and instead return zero bytes. This is made evident in the Tera Term screen capture; the lines that say "Got report with length = 0, data =" should not be happening, every line should be saying "Got report with length = 8, data = XX XX XX XX XX XX XX XX".
As a side note, I also tried this program on a KL25Z, but it appears to be even more broken. Both read() and readNB() cause SimpleHIDWrite to regularly respond with "WRITE ERROR: The system cannot find the file specified" and "WRITE ERROR: A device attached to the system is not functioning", and after receiving the first report, they both start spitting out a 64B report containing junk every 6 seconds or so regardless of input.
Hey guys. I'm trying to set up a thread that communicates with a PC using USBHID, but I'm finding that readNB() is not working the way I would expect... I was expecting to be able to replace if (hid.read(&report)) with if (hid.readNB(&report)) and have it work straight away, but readNB() seems to be dropping some reports... To test it out, I wrote a small program based on USBHID_HelloWord that echos reports, and used SimpleHIDWrite to send some test reports with the following results:
main.cpp
As you can see, the first and last reports echoed fine, but the middle two didn't... Changing readNB() back to read() solves the problem and the code works again. What I find even more confusing is that the LED toggles even though the echo isn't returned... Am I missing a step here? Thanks!