Skip to content

Commit

Permalink
feature(usbd): Added tusb_teardown() implementation for device
Browse files Browse the repository at this point in the history
  • Loading branch information
roma-jam committed Jun 11, 2024
1 parent 33d36cb commit 9445c88
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/device/usbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++ )
Expand Down
3 changes: 3 additions & 0 deletions src/device/usbd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions src/tusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ bool tusb_init(void)
return true;
}

// Teardown device 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;
Expand Down
4 changes: 2 additions & 2 deletions src/tusb.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ bool tusb_init(void);
// Check if stack is initialized
bool tusb_inited(void);

// TODO
// bool tusb_teardown(void);
// Teardown device stack
bool tusb_teardown(void);

#ifdef __cplusplus
}
Expand Down

0 comments on commit 9445c88

Please sign in to comment.