cherry-embedded / CherryUSB

CherryUSB is a tiny, beautiful and portable USB host and device stack for embedded system with USB IP
https://cherryusb.readthedocs.io/
Apache License 2.0
1.21k stars 256 forks source link

USB HID device stays in infinite loop when key pressed, when there is no USB support... #93

Closed matsobdev closed 1 year ago

matsobdev commented 1 year ago

...when plugged in to powered off computer (or restarting from working both computer and USB stack). After computer startup, stack initialises at BIOS USB and repetitive key pressing causes:

while (hid_state == HID_STATE_BUSY) {
}

in hid_keyboard_template.c to loop to infinity and beyond when key pressed between BIOS and OS loading, where is no USB host. Changing it to:

void send_report(uint8_t rep_id, uint8_t modifier, uint8_t key_code)
{
   uint8_t sendbuffer[9] = {rep_id, modifier, 0x00, key_code, 0x00, 0x00, 0x00, 0x00, 0x00};
   int ret = usbd_ep_start_write(HID_INT_EP, sendbuffer, 9);
   if (ret < 0) {
      return;
   }
   hid_state = HID_STATE_BUSY;
   // enough is enough - max time to proceed
   uint64_t start;
   uint64_t duration = 0;
   start = time_us_64();
   while (hid_state == HID_STATE_BUSY && duration < 100000) {
      duration = time_us_64() - start;
   }
}

solves the issue. And it happens on Pico or Pico W.

sakumisu commented 1 year ago

It is user code, i just give a simple template.

matsobdev commented 1 year ago

Yes, it's modified for that purpose of dealing with an issue (only after comment). Everything's great, works nice but it locks in described circumstances. Talking about unmodified hid_keyboard_template.c. hid_state will be always HID_STATE_BUSY then, so bypassing it to try again when if occurs too long - I guess unnaturally long for HID keyboard.

sakumisu commented 1 year ago

Yes, it's modified for that purpose of dealing with an issue (only after comment). Everything's great, works nice but it locks in described circumstances. Talking about unmodified hid_keyboard_template.c. hid_state will be always HID_STATE_BUSY then, so bypassing it to try again when if occurs too long - I guess unnaturally long for HID keyboard.

This should be modified by users, i just tell that the api is ok, how to call the api is user work.