Skip to content

Commit

Permalink
Fix USB bus state handling
Browse files Browse the repository at this point in the history
- Restore lost USB speed
- Prevent that a spammed bus state can fill up the event queue
  • Loading branch information
RockyZeroFour committed Feb 26, 2024
1 parent 912f5db commit 898f9d8
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/device/usbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,6 @@ void tud_task_ext(uint32_t timeout_ms, bool in_isr)
{
case DCD_EVENT_BUS_RESET:
TU_LOG2(": %s Speed\r\n", tu_str_speed[event.bus_reset.speed]);
_usbd_dev.speed = event.bus_reset.speed;
TU_ATTR_FALLTHROUGH;

case DCD_EVENT_UNPLUGGED:
Expand All @@ -514,6 +513,16 @@ void tud_task_ext(uint32_t timeout_ms, bool in_isr)

// Completely clear the current USB state
usbd_reset(event.rhport);

// Recover the intended bus speed
if (DCD_EVENT_BUS_RESET == event.event_id)
{
_usbd_dev.speed = event.bus_reset.speed;
}
else if (DCD_EVENT_UNPLUGGED == event.event_id)
{
_usbd_dev.speed = DCD_EVENT_INVALID;
}
break;

case DCD_EVENT_SETUP_RECEIVED:
Expand Down Expand Up @@ -1096,6 +1105,16 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
//--------------------------------------------------------------------+
void dcd_event_handler(dcd_event_t const * event, bool in_isr)
{
volatile static uint8_t last_event_id = DCD_EVENT_INVALID;

// Skip the useless repeating bus states
if (last_event_id == event->event_id && DCD_EVENT_UNPLUGGED <= event->event_id && DCD_EVENT_RESUME >= event->event_id)
{
return;
}

last_event_id = event->event_id;

switch (event->event_id)
{
case DCD_EVENT_SUSPEND:
Expand Down

0 comments on commit 898f9d8

Please sign in to comment.