From b9bd0aefc2b4afd122e8ba79297f6fa84fd103ac Mon Sep 17 00:00:00 2001 From: Shogo Sugihara Date: Tue, 3 Dec 2024 16:17:04 +0900 Subject: [PATCH] feature(usbd): Added tud_teardown() I imported the following PR. https://github.com/espressif/tinyusb/pull/27 --- src/device/usbd.c | 25 +++++++++++++++++++++++++ src/device/usbd.h | 3 +++ src/tusb.c | 17 +++++++++++++++++ src/tusb.h | 4 ++-- 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/device/usbd.c b/src/device/usbd.c index e759b8732b..3121f2efcb 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -466,6 +466,31 @@ bool tud_init (uint8_t rhport) return true; } +bool tud_teardown (uint8_t rhport) +{ + // skip if nothing to teardown + if ( !tud_inited() ) return true; + + TU_LOG(USBD_DBG, "USBD teardown on controller %u\r\n", rhport); + + // Disable interrupt + dcd_int_disable(rhport); + + // Disable class devices + for (uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++) + { + usbd_class_driver_t const * driver = get_driver(i); + TU_ASSERT(driver); + TU_LOG(USBD_DBG, "%s reset\r\n", driver->name); + driver->reset(rhport); + } + + // clean port + _usbd_rhport = RHPORT_INVALID; + + return true; +} + static void configuration_reset(uint8_t rhport) { for ( uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++ ) diff --git a/src/device/usbd.h b/src/device/usbd.h index ec06212eb4..e08b5c785e 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -43,6 +43,9 @@ bool tud_init (uint8_t rhport); // Check if device stack is already initialized bool tud_inited(void); +// Teardown device stack +bool tud_teardown (uint8_t rhport); + // Task function should be called in main/rtos loop, extended version of tud_task() // - timeout_ms: millisecond to wait, zero = no wait, 0xFFFFFFFF = wait forever // - in_isr: if function is called in ISR diff --git a/src/tusb.c b/src/tusb.c index 465b608b03..19e4d66395 100644 --- a/src/tusb.c +++ b/src/tusb.c @@ -73,6 +73,23 @@ bool tusb_inited(void) return ret; } +// Teardown device/host stack +bool tusb_teardown(void) +{ +#if CFG_TUD_ENABLED && defined(TUD_OPT_RHPORT) + // teardown device stack CFG_TUSB_RHPORTx_MODE must be defined + TU_ASSERT ( tud_teardown(TUD_OPT_RHPORT) ); +#endif + +#if CFG_TUH_ENABLED && defined(TUH_OPT_RHPORT) + // teardown host stack CFG_TUSB_RHPORTx_MODE must be defined + // not implemented + return false; +#endif + + return true; +} + //--------------------------------------------------------------------+ // Descriptor helper //--------------------------------------------------------------------+ diff --git a/src/tusb.h b/src/tusb.h index 37a521fa8f..6a661f98a8 100644 --- a/src/tusb.h +++ b/src/tusb.h @@ -133,8 +133,8 @@ bool tusb_init(void); // Check if stack is initialized bool tusb_inited(void); -// TODO -// bool tusb_teardown(void); +// Teardown device/host stack +bool tusb_teardown(void); #ifdef __cplusplus }