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 {
uint8_t controller_id; // controller ID
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
usbh_ctrl_xfer_info_t ctrl_xfer_info; // control transfer
} usbh_data_t;
@@ -349,7 +350,7 @@ bool tuh_rhport_is_active(uint8_t rhport) {
bool tuh_rhport_reset_bus(uint8_t rhport, bool active) {
TU_VERIFY(tuh_rhport_is_active(rhport));
if ( active ) {
if (active) {
hcd_port_reset(rhport);
} else {
hcd_port_reset_end(rhport);
@@ -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) {
switch (event->event_id) {
case HCD_EVENT_DEVICE_ATTACH:
break;
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;
default: break;
@@ -1406,6 +1415,11 @@ static bool enum_new_device(hcd_event_t* event) {
// wait until device connection is stable TODO non blocking
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) {
// connected directly to roothub
// USB bus not active and frame number is not available yet.

View File

@@ -107,7 +107,6 @@ typedef struct {
typedef struct {
hcd_xfer_t xfer[DWC2_CHANNEL_COUNT_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;
@@ -423,11 +422,6 @@ uint32_t hcd_frame_number(uint8_t rhport) {
// Get the current connect status of roothub port
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);
return dwc2->hprt & HPRT_CONN_STATUS;
}
@@ -1328,10 +1322,7 @@ static void handle_hprt_irq(uint8_t rhport, bool in_isr) {
hprt |= HPRT_CONN_DETECT;
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);
}
}
@@ -1398,12 +1389,8 @@ void hcd_int_handler(uint8_t rhport, bool in_isr) {
// Device disconnected
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)) {
hcd_event_device_remove(rhport, in_isr);
}
if (!(dwc2->hprt & HPRT_CONN_STATUS)) {
hcd_event_device_remove(rhport, in_isr);
}
}