usbh: add roothub debounncing flag to ignore attach/remove event on the roothub that is currently doing debouncing delay

This commit is contained in:
hathach
2025-04-28 12:59:59 +07:00
parent b5d4d0f623
commit 2c1414b4c1
2 changed files with 21 additions and 20 deletions

View File

@@ -275,6 +275,7 @@ typedef struct {
typedef struct { typedef struct {
uint8_t controller_id; // controller ID uint8_t controller_id; // controller ID
uint8_t enumerating_daddr; // device address of the device being enumerated uint8_t enumerating_daddr; // device address of the device being enumerated
uint8_t attach_debouncing_bm; // bitmask for roothub port attach debouncing
tuh_bus_info_t dev0_bus; // bus info for dev0 in enumeration tuh_bus_info_t dev0_bus; // bus info for dev0 in enumeration
usbh_ctrl_xfer_info_t ctrl_xfer_info; // control transfer usbh_ctrl_xfer_info_t ctrl_xfer_info; // control transfer
} usbh_data_t; } usbh_data_t;
@@ -1021,10 +1022,18 @@ bool tuh_bus_info_get(uint8_t daddr, tuh_bus_info_t* bus_info) {
TU_ATTR_FAST_FUNC void hcd_event_handler(hcd_event_t const* event, bool in_isr) { TU_ATTR_FAST_FUNC void hcd_event_handler(hcd_event_t const* event, bool in_isr) {
switch (event->event_id) { switch (event->event_id) {
case HCD_EVENT_DEVICE_ATTACH: case HCD_EVENT_DEVICE_ATTACH:
break;
case HCD_EVENT_DEVICE_REMOVE: case HCD_EVENT_DEVICE_REMOVE:
// Attach debouncing on roothub: skip attach/remove while debouncing delay
if (event->connection.hub_addr == 0) {
if (tu_bit_test(_usbh_data.attach_debouncing_bm, event->rhport)) {
return;
}
if (event->event_id == HCD_EVENT_DEVICE_ATTACH) {
// No debouncing, set flag if attach event
_usbh_data.attach_debouncing_bm |= TU_BIT(event->rhport);
}
}
break; break;
default: break; default: break;
@@ -1406,6 +1415,11 @@ static bool enum_new_device(hcd_event_t* event) {
// wait until device connection is stable TODO non blocking // wait until device connection is stable TODO non blocking
tusb_time_delay_ms_api(ENUM_DEBOUNCING_DELAY_MS); tusb_time_delay_ms_api(ENUM_DEBOUNCING_DELAY_MS);
// clear roothub debouncing delay
if (dev0_bus->hub_addr == 0) {
_usbh_data.attach_debouncing_bm &= (uint8_t) ~TU_BIT(dev0_bus->rhport);
}
if (dev0_bus->hub_addr == 0) { if (dev0_bus->hub_addr == 0) {
// connected directly to roothub // connected directly to roothub
// USB bus not active and frame number is not available yet. // USB bus not active and frame number is not available yet.

View File

@@ -107,7 +107,6 @@ typedef struct {
typedef struct { typedef struct {
hcd_xfer_t xfer[DWC2_CHANNEL_COUNT_MAX]; hcd_xfer_t xfer[DWC2_CHANNEL_COUNT_MAX];
hcd_endpoint_t edpt[CFG_TUH_DWC2_ENDPOINT_MAX]; hcd_endpoint_t edpt[CFG_TUH_DWC2_ENDPOINT_MAX];
bool attach_debounce; // if true: wait for the debounce delay before issuing new attach events
} hcd_data_t; } hcd_data_t;
hcd_data_t _hcd_data; hcd_data_t _hcd_data;
@@ -423,11 +422,6 @@ uint32_t hcd_frame_number(uint8_t rhport) {
// Get the current connect status of roothub port // Get the current connect status of roothub port
bool hcd_port_connect_status(uint8_t rhport) { bool hcd_port_connect_status(uint8_t rhport) {
// this is called from enum_new_device() - after the debouncing delays
if (_hcd_data.attach_debounce) {
_hcd_data.attach_debounce = false; // allow new attach events again
}
dwc2_regs_t* dwc2 = DWC2_REG(rhport); dwc2_regs_t* dwc2 = DWC2_REG(rhport);
return dwc2->hprt & HPRT_CONN_STATUS; return dwc2->hprt & HPRT_CONN_STATUS;
} }
@@ -1328,12 +1322,9 @@ static void handle_hprt_irq(uint8_t rhport, bool in_isr) {
hprt |= HPRT_CONN_DETECT; hprt |= HPRT_CONN_DETECT;
if (hprt_bm.conn_status) { if (hprt_bm.conn_status) {
if (!_hcd_data.attach_debounce) {
_hcd_data.attach_debounce = true; // block new attach events until the debounce delay is over
hcd_event_device_attach(rhport, in_isr); hcd_event_device_attach(rhport, in_isr);
} }
} }
}
if (hprt_bm.enable_change) { if (hprt_bm.enable_change) {
// Port enable change // Port enable change
@@ -1398,14 +1389,10 @@ void hcd_int_handler(uint8_t rhport, bool in_isr) {
// Device disconnected // Device disconnected
dwc2->gintsts = GINTSTS_DISCINT; dwc2->gintsts = GINTSTS_DISCINT;
// ignore device removal if attach debounce is active
// it will evaluate the port status after the debounce delay
if (!_hcd_data.attach_debounce) {
if (!(dwc2->hprt & HPRT_CONN_STATUS)) { if (!(dwc2->hprt & HPRT_CONN_STATUS)) {
hcd_event_device_remove(rhport, in_isr); hcd_event_device_remove(rhport, in_isr);
} }
} }
}
#if CFG_TUH_DWC2_SLAVE_ENABLE #if CFG_TUH_DWC2_SLAVE_ENABLE
// RxFIFO non-empty interrupt handling // RxFIFO non-empty interrupt handling