diff --git a/src/device/usbd.c b/src/device/usbd.c index 44c2530ce6..1be3be3bd6 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -437,6 +437,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 b11c1a09dd..fd1df34d99 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..a7905a6e90 100644 --- a/src/tusb.c +++ b/src/tusb.c @@ -58,6 +58,23 @@ bool tusb_init(void) return true; } +// 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; +} + bool tusb_inited(void) { bool ret = false; 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 }