From 2c1414b4c18518a179560a097aada40609303693 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 28 Apr 2025 12:59:59 +0700 Subject: [PATCH 1/5] usbh: add roothub debounncing flag to ignore attach/remove event on the roothub that is currently doing debouncing delay --- src/host/usbh.c | 22 ++++++++++++++++++---- src/portable/synopsys/dwc2/hcd_dwc2.c | 19 +++---------------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/host/usbh.c b/src/host/usbh.c index 8469aee83..8852bdda4 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -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. diff --git a/src/portable/synopsys/dwc2/hcd_dwc2.c b/src/portable/synopsys/dwc2/hcd_dwc2.c index 7c29a03cf..2de15068a 100644 --- a/src/portable/synopsys/dwc2/hcd_dwc2.c +++ b/src/portable/synopsys/dwc2/hcd_dwc2.c @@ -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); } } From 42d4f7c81e65a2d39b8e108b1ef8ed58945b7617 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 28 Apr 2025 15:10:03 +0700 Subject: [PATCH 2/5] remove the old attach duplicated logic, debouncing skip should take care of it. --- src/host/usbh.c | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/src/host/usbh.c b/src/host/usbh.c index 8852bdda4..6f875c80b 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -513,28 +513,19 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr) { case HCD_EVENT_DEVICE_ATTACH: // due to the shared control buffer, we must fully complete enumerating one device first. // TODO better to have an separated queue for newly attached devices - if (_usbh_data.enumerating_daddr != TUSB_INDEX_INVALID_8) { - if (event.rhport == _usbh_data.dev0_bus.rhport && - event.connection.hub_addr == _usbh_data.dev0_bus.hub_addr && event.connection.hub_port == _usbh_data.dev0_bus.hub_port) { - // Some device can cause multiple duplicated attach events - // drop current enumerating and start over for a proper port reset - // abort/cancel current enumeration and start new one - TU_LOG1("[%u:] USBH Device Attach (duplicated)\r\n", event.rhport); - tuh_edpt_abort_xfer(0, 0); - enum_new_device(&event); - } else { - TU_LOG_USBH("[%u:] USBH Defer Attach until current enumeration complete\r\n", event.rhport); - bool is_empty = osal_queue_empty(_usbh_q); - queue_event(&event, in_isr); - - if (is_empty) { - // Exit if this is the only event in the queue, otherwise we may loop forever - return; - } - } - } else { + if (_usbh_data.enumerating_daddr == TUSB_INDEX_INVALID_8) { + // New device attached and we are ready TU_LOG1("[%u:] USBH Device Attach\r\n", event.rhport); + _usbh_data.enumerating_daddr = 0; // enumerate new device with address 0 enum_new_device(&event); + } else { + // currently enumerating another device + TU_LOG_USBH("[%u:] USBH Defer Attach until current enumeration complete\r\n", event.rhport); + const bool is_empty = osal_queue_empty(_usbh_q); + queue_event(&event, in_isr); + if (is_empty) { + return; // Exit if this is the only event in the queue, otherwise we loop forever + } } break; @@ -1410,8 +1401,6 @@ static bool enum_new_device(hcd_event_t* event) { dev0_bus->hub_addr = event->connection.hub_addr; dev0_bus->hub_port = event->connection.hub_port; - _usbh_data.enumerating_daddr = 0; - // wait until device connection is stable TODO non blocking tusb_time_delay_ms_api(ENUM_DEBOUNCING_DELAY_MS); From fc43eeddf2b3d045431d56cc50bb0e2d6fdf5b98 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 29 Apr 2025 10:11:20 +0700 Subject: [PATCH 3/5] attach debouncing fixed issue with port1 highspeed on imxrt --- .idea/debugServers/rt1060.xml | 2 +- .idea/debugServers/rt1064.xml | 2 +- .idea/debugServers/sam21.xml | 2 +- .idea/debugServers/sam51.xml | 2 +- .idea/debugServers/stm32f769.xml | 4 ++-- .idea/debugServers/stm32h563.xml | 2 +- .idea/debugServers/stm32h743.xml | 2 +- src/portable/chipidea/ci_hs/hcd_ci_hs.c | 6 ------ 8 files changed, 8 insertions(+), 14 deletions(-) diff --git a/.idea/debugServers/rt1060.xml b/.idea/debugServers/rt1060.xml index 3325cc81f..cbd295b4e 100644 --- a/.idea/debugServers/rt1060.xml +++ b/.idea/debugServers/rt1060.xml @@ -5,7 +5,7 @@ - + diff --git a/.idea/debugServers/rt1064.xml b/.idea/debugServers/rt1064.xml index 4dc38ef63..691066f9d 100644 --- a/.idea/debugServers/rt1064.xml +++ b/.idea/debugServers/rt1064.xml @@ -5,7 +5,7 @@ - + diff --git a/.idea/debugServers/sam21.xml b/.idea/debugServers/sam21.xml index d8763b33b..3f6735bd1 100644 --- a/.idea/debugServers/sam21.xml +++ b/.idea/debugServers/sam21.xml @@ -5,7 +5,7 @@ - + diff --git a/.idea/debugServers/sam51.xml b/.idea/debugServers/sam51.xml index 0d15ff856..99a92c174 100644 --- a/.idea/debugServers/sam51.xml +++ b/.idea/debugServers/sam51.xml @@ -5,7 +5,7 @@ - + diff --git a/.idea/debugServers/stm32f769.xml b/.idea/debugServers/stm32f769.xml index 2fb322a0f..22a452218 100644 --- a/.idea/debugServers/stm32f769.xml +++ b/.idea/debugServers/stm32f769.xml @@ -1,11 +1,11 @@ - + - + diff --git a/.idea/debugServers/stm32h563.xml b/.idea/debugServers/stm32h563.xml index 9bf6db6e9..637839314 100644 --- a/.idea/debugServers/stm32h563.xml +++ b/.idea/debugServers/stm32h563.xml @@ -5,7 +5,7 @@ - + diff --git a/.idea/debugServers/stm32h743.xml b/.idea/debugServers/stm32h743.xml index 63680b78c..b04e4a708 100644 --- a/.idea/debugServers/stm32h743.xml +++ b/.idea/debugServers/stm32h743.xml @@ -5,7 +5,7 @@ - + diff --git a/src/portable/chipidea/ci_hs/hcd_ci_hs.c b/src/portable/chipidea/ci_hs/hcd_ci_hs.c index 22eb22690..b5324a754 100644 --- a/src/portable/chipidea/ci_hs/hcd_ci_hs.c +++ b/src/portable/chipidea/ci_hs/hcd_ci_hs.c @@ -93,12 +93,6 @@ bool hcd_init(uint8_t rhport, const tusb_rhport_init_t* rh_init) { hcd_reg->USBMODE = USBMODE_CM_HOST; #endif - // FIXME force full speed, still have issue with Highspeed enumeration - // probably due to physical connection bouncing when plug/unplug - // 1. Have issue when plug/unplug devices, maybe the port is not reset properly - // 2. Also does not seems to detect disconnection - hcd_reg->PORTSC1 |= PORTSC1_FORCE_FULL_SPEED; - return ehci_init(rhport, (uint32_t) &hcd_reg->CAPLENGTH, (uint32_t) &hcd_reg->USBCMD); } From e7d4b5c9e718b1e72df8aadb884d2f36ab9d158c Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 29 Apr 2025 17:04:10 +0700 Subject: [PATCH 4/5] add enum for set addr recovery --- .idea/cmake.xml | 6 +++--- .idea/debugServers/rt1064.xml | 4 ++-- src/host/usbh.c | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.idea/cmake.xml b/.idea/cmake.xml index b8383a5ff..dd219bc77 100644 --- a/.idea/cmake.xml +++ b/.idea/cmake.xml @@ -12,7 +12,7 @@ - + @@ -94,7 +94,8 @@ - + + @@ -166,7 +167,6 @@ - \ No newline at end of file diff --git a/.idea/debugServers/rt1064.xml b/.idea/debugServers/rt1064.xml index 691066f9d..b908b59e2 100644 --- a/.idea/debugServers/rt1064.xml +++ b/.idea/debugServers/rt1064.xml @@ -1,11 +1,11 @@ - + - + diff --git a/src/host/usbh.c b/src/host/usbh.c index 6f875c80b..fbb61e10e 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -1363,6 +1363,7 @@ enum { // USB 2.0 specs 7.1.7 for timing ENUM_RESET_ROOT_DELAY_MS = 50, // T(DRSTr) minimum 50 ms for reset from root port ENUM_RESET_HUB_DELAY_MS = 20, // T(DRST) 10-20 ms for hub reset ENUM_RESET_RECOVERY_DELAY_MS = 10, // T(RSTRCY) minimum 10 ms for reset recovery + ENUM_SET_ADDRESS_RECOVERY_DELAY_MS = 2, // USB 2.0 Spec 9.2.6.3 min is 2 ms }; enum { @@ -1573,8 +1574,7 @@ static void process_enumeration(tuh_xfer_t* xfer) { } case ENUM_GET_DEVICE_DESC: { - // Allow 2ms for address recovery time, Ref USB Spec 9.2.6.3 - tusb_time_delay_ms_api(2); + tusb_time_delay_ms_api(ENUM_SET_ADDRESS_RECOVERY_DELAY_MS); // set address recovery const uint8_t new_addr = (uint8_t) tu_le16toh(xfer->setup->wValue); usbh_device_t* new_dev = get_device(new_addr); From 2abd3c54c4141e5e6e0bd4fea38849cb150fb161 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 29 Apr 2025 20:49:47 +0700 Subject: [PATCH 5/5] define hcd_devtree_info_t forr backward compatible --- src/host/usbh.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/host/usbh.h b/src/host/usbh.h index 6f09f0c80..6f34d8bb3 100644 --- a/src/host/usbh.h +++ b/src/host/usbh.h @@ -86,6 +86,9 @@ typedef struct { uint8_t speed; } tuh_bus_info_t; +// backward compatibility for hcd_devtree_info_t, maybe removed in the future +#define hcd_devtree_info_t tuh_bus_info_t +#define hcd_devtree_get_info(_daddr, _bus_info) tuh_bus_info_get(_daddr, _bus_info) // ConfigID for tuh_configure() enum {