HID output reports being received twice?

Post here all questions related to LibMaple core if you can't find a relevant section!
Post Reply
hjalfi
Posts: 3
Joined: Tue Feb 14, 2023 7:59 pm

HID output reports being received twice?

Post by hjalfi »

I'm trying to send data _to_ a HID device (it's an LCD and I'm trying to make it work with the Auxiliary Display HID protocol). I'm finding that each incoming report is reported twice, which is really strange. I've checked with Wireshark and the USB packet is sent once, so whatever's going on is happening at the STM32 end.

Because I have multiple report types (a FEATURE for fetching the LCD configuration, and an OUTPUT for blitting) I have to bypass HIDReporter and use low-level functions in a lot of places --- HIDReporter assumes exactly one report. That means it's very likely I'm just doing it wrong...

So, I create an output buffer in my component's begin().

Code: Select all

        blitBuffer.buffer = blitBufferBytes;
        blitBuffer.bufferSize = sizeof(blitBufferBytes);
        blitBuffer.reportID = BLIT_REPORT_ID;
        HID.addOutputBuffer(&blitBuffer);
Then, every iteration of loop(), I check to see if there's anything there and print the result.

Code: Select all

        if (usb_hid_get_data(HID_REPORT_TYPE_OUTPUT,
                BLIT_REPORT_ID,
                (uint8_t*)&blitReport,
                true))
        {
            USBSerial.print(blitReport.x1, HEX);
            USBSerial.print(" ");
            USBSerial.print(blitReport.y1, HEX);
            USBSerial.print(" ");
            USBSerial.print(blitReport.x2, HEX);
            USBSerial.print(" ");
            USBSerial.println(blitReport.y2, HEX);
        }
This works, but as I said above, I get each packet twice. After a new packet is received, I first get a report of the _previous_ packet, and then another report of the new one.

Looking at the code, when a packet arrives, hidUSBDataSetup() is supposed to find an OUTPUT buffer, copy the data in, and mark it as UNREAD. Then, usb_hid_get_data is supposed to find an UNREAD buffer, copy the data out, and mark it as READ. The next incoming packet then goes into the buffer.

I can't find _any_ references to people actually using OUTPUT reports with USBComposite, so it might be possible that I'm the very first person to try this... I'm so special. Any advice?
hjalfi
Posts: 3
Joined: Tue Feb 14, 2023 7:59 pm

Re: HID output reports being received twice?

Post by hjalfi »

Got it. It's a core bug.

In usb_hid.c:hidUSBDataSetup(), there's this code:

Code: Select all

usb_generic_control_rx_setup(buffer->buffer, buffer->bufferSize, &(buffer->state));
buffer->state = HID_BUFFER_UNREAD;
That assignment to buffer->state shouldn't be there. usb_generic_control_rx_setup() will set the state itself if the retrieval completes, and sometimes it doesn't. It so happens that in my case it hasn't. However, the code above causes the buffer to be marked unread, so my code picks it up, and then the code above immediately runs again to set the real value, and my code sees it again.

Just commenting out the assignment seems (so far) to fix things.

I've filed https://github.com/rogerclarkmelbourne/ ... issues/897.
Post Reply

Return to “General discussion”