hathach / tinyusb

An open source cross-platform USB stack for embedded system
https://www.tinyusb.org
MIT License
4.82k stars 1.02k forks source link

tuh_hid_set_report to set keyboard LED causes unstable behaviour #2323

Closed ObligedTester closed 9 months ago

ObligedTester commented 9 months ago

Operating System

Linux

Board

Raspberry Pi Pico

Firmware

0.15.0

What happened ?

When using tuh_hid_set_report to set leds, like this sample:

leds = KEYBOARD_LED_NUMLOCK;
tuh_hid_set_report(deviceadress, deviceinstance, 0, HID_REPORT_TYPE_OUTPUT, &leds, sizeof(leds));

It causes erratic behavior with two different keyboards.

I implemented a short program to toggle the leds while pressing a button. It initially seems to work. But after some toggles, seemingly a random amount, it causes the LEDs to stop toggling. Program is still running and key codes are sent back to the Pico.

However, disconnecting the cable causes "Panic" "Invalid speed" to show up, and after that its completely locked.

To try and diagnose I did a very crude routine that will just toggle the leds back and forth forever:

while (1) 
    {

    tuh_task(); //This needs to be called at high frequency to process all commands on usb buffers

    now = board_millis();
    if (now >= (prev + 200))
    {
        if (toggle == 0)
        {
        leds = KEYBOARD_LED_NUMLOCK;
        tuh_hid_set_report(deviceadress, deviceinstance, 0, HID_REPORT_TYPE_OUTPUT, &leds, sizeof(leds));
        prev = board_millis();
        toggle = 1;
        } 
        else 
        {
            leds = KEYBOARD_LED_CAPSLOCK;
            tuh_hid_set_report(deviceadress, deviceadress, 0, HID_REPORT_TYPE_OUTPUT, &leds, sizeof(leds));
            prev = board_millis();
            toggle = 0;
            count++;
            printf("%d\n",count);
        }

    }

It causes the same behavior, after a random amount of toggles, the LEDs stop to respond. Keyboard still sends out key codes, and if I disconnect, it panics.

Disconnects and reconnect works normally before this happens.

How to reproduce ?

Use tuh_hid_set_report over and over to set LEDs on a keyboard

Debug Log as txt file (LOG/CFG_TUSB_DEBUG=2)

*** Ignore the values of 8xx for example in below log, its my own debug to see how many toggles have been done HID Set Report complete
HID Set Report: id = 0, type = 2, len = 1
[0:1] Class Request: 21 09 00 02 00 00 01 00
Transfer complete
on EP 00 with 8 bytes
Buffer complete
Transfer complete
on EP 00 with 1 bytes
[0:1] Control data:
0000: 01 |.|
Buffer complete
Transfer complete
on EP 80 with 0 bytes

HID Set Report complete
HID Set Report: id = 0, type = 2, len = 1
[0:1] Class Request: 21 09 00 02 00 00 01 00
8Transfer complete
02
on EP 00 with 8 bytes
Buffer complete
Transfer complete
on EP 00 with 1 bytes
[0:1] Control data:
0000: 02 |.|
Buffer complete
Transfer complete
on EP 80 with 0 bytes

HID Set Report complete
HID Set Report: id = 0, type = 2, len = 1
[0:1] Class Request: 21 09 00 02 00 00 01 00
HID Set Report: id = 0, type = 2, len = 1
tuh_hid_set_report 212: ASSERT FAILED
803
HID Set Report: id = 0, type = 2, len = 1
tuh_hid_set_report 212: ASSERT FAILED
HID Set Report: id = 0, type = 2, len = 1
tuh_hid_set_report 212: ASSERT FAILED
804
HID Set Report: id = 0, type = 2, len = 1
tuh_hid_set_report 212: ASSERT FAILED
HID Set Report: id = 0, type = 2, len = 1
tuh_hid_set_report 212: ASSERT FAILED
805

Screenshots

No response

I have checked existing issues, dicussion and documentation

ObligedTester commented 9 months ago

My issue seem to be exactly the same as this one: https://github.com/hathach/tinyusb/pull/2024

This looks to be already merged and what I can see is that I am already running the latest version of tinyUSB? Am I missing something else?

ObligedTester commented 9 months ago

Looks like the pico-sdk is using a old version of tinyUSB. I will close this