Skip to content

Commit

Permalink
ref(esp_tinyusb): Added tusb_teardown() call
Browse files Browse the repository at this point in the history
  • Loading branch information
roma-jam committed Jun 11, 2024
1 parent a94ccb2 commit 9fbdf0d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
10 changes: 10 additions & 0 deletions device/esp_tinyusb/include/tinyusb.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ typedef struct {
*/
esp_err_t tinyusb_driver_install(const tinyusb_config_t *config);

/**
* @brief This is an all-in-one helper function, including:
* 1. Stops the task to handle usb events
* 2. TinyUSB stack tearing down
* 2. Freeing resources after descriptors preparation
* 3. Deletes USB PHY
*
* @retval ESP_FAIL Uninstall driver or tinyusb stack failed because of internal error
* @retval ESP_OK Uninstall driver, tinyusb stack and USB PHY successfully
*/
esp_err_t tinyusb_driver_uninstall(void);

#ifdef __cplusplus
Expand Down
1 change: 0 additions & 1 deletion device/esp_tinyusb/test_app/main/test_bvalid_sig.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,5 @@ TEST_CASE("bvalid_signal", "[esp_tinyusb][usb_device]")

// Cleanup
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
TEST_ASSERT_EQUAL(ESP_OK, tusb_stop_task());
}
#endif // SOC_USB_OTG_SUPPORTED
6 changes: 0 additions & 6 deletions device/esp_tinyusb/test_app/main/test_descriptors_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ TEST_CASE("descriptors_config_all_default", "[esp_tinyusb][usb_device]")
TEST_ASSERT_EQUAL(ESP_OK, __test_wait_conn());
// Cleanup
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
TEST_ASSERT_EQUAL(ESP_OK, tusb_stop_task());
__test_free();
}

Expand All @@ -144,7 +143,6 @@ TEST_CASE("descriptors_config_device", "[esp_tinyusb][usb_device]")
TEST_ASSERT_EQUAL(ESP_OK, __test_wait_conn());
// Cleanup
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
TEST_ASSERT_EQUAL(ESP_OK, tusb_stop_task());
__test_free();
}

Expand All @@ -166,7 +164,6 @@ TEST_CASE("descriptors_config_device_and_config", "[esp_tinyusb][usb_device]")
TEST_ASSERT_EQUAL(ESP_OK, __test_wait_conn());
// Cleanup
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
TEST_ASSERT_EQUAL(ESP_OK, tusb_stop_task());
__test_free();
}

Expand All @@ -187,7 +184,6 @@ TEST_CASE("descriptors_config_device_and_fs_config_only", "[esp_tinyusb][usb_dev
TEST_ASSERT_EQUAL(ESP_OK, __test_wait_conn());
// Cleanup
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
TEST_ASSERT_EQUAL(ESP_OK, tusb_stop_task());
__test_free();
}

Expand All @@ -207,7 +203,6 @@ TEST_CASE("descriptors_config_device_and_hs_config_only", "[esp_tinyusb][usb_dev
TEST_ASSERT_EQUAL(ESP_OK, __test_wait_conn());
// Cleanup
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
TEST_ASSERT_EQUAL(ESP_OK, tusb_stop_task());
__test_free();
}

Expand All @@ -227,7 +222,6 @@ TEST_CASE("descriptors_config_all_configured", "[esp_tinyusb][usb_device]")
TEST_ASSERT_EQUAL(ESP_OK, __test_wait_conn());
// Cleanup
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
TEST_ASSERT_EQUAL(ESP_OK, tusb_stop_task());
__test_free();
}
#endif // TUD_OPT_HIGH_SPEED
Expand Down
26 changes: 24 additions & 2 deletions device/esp_tinyusb/tinyusb.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -19,6 +19,18 @@
const static char *TAG = "TinyUSB";
static usb_phy_handle_t phy_hdl;

/**
* @brief Teardown device/host TinyUSB stack
*
* @note
* For backward compatibility, when tusb_teardown() call is not implemented.
*/
__attribute__ ((weak)) bool tusb_teardown(void)
{
ESP_LOGW(TAG, "%s has not implemented, please update tinyusb component", __FUNCTION__);
return true;
}

esp_err_t tinyusb_driver_install(const tinyusb_config_t *config)
{
ESP_RETURN_ON_FALSE(config, ESP_ERR_INVALID_ARG, TAG, "Config can't be NULL");
Expand Down Expand Up @@ -66,8 +78,18 @@ esp_err_t tinyusb_driver_install(const tinyusb_config_t *config)
return ESP_OK;
}

esp_err_t tinyusb_driver_uninstall()
esp_err_t tinyusb_driver_uninstall(void)
{
esp_err_t ret = tusb_stop_task();

if (ret != ESP_OK) {
return ret;
}

if (!tusb_teardown()) {
return ESP_ERR_NOT_FINISHED;
}

tinyusb_free_descriptors();
return usb_del_phy(phy_hdl);
}

0 comments on commit 9fbdf0d

Please sign in to comment.