diff --git a/device/esp_tinyusb/include/tinyusb.h b/device/esp_tinyusb/include/tinyusb.h index 484c86e..887c63f 100644 --- a/device/esp_tinyusb/include/tinyusb.h +++ b/device/esp_tinyusb/include/tinyusb.h @@ -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 diff --git a/device/esp_tinyusb/test_apps/cdc_and_usb_device/main/test_bvalid_sig.c b/device/esp_tinyusb/test_apps/cdc_and_usb_device/main/test_bvalid_sig.c index 1bf865a..9bce381 100644 --- a/device/esp_tinyusb/test_apps/cdc_and_usb_device/main/test_bvalid_sig.c +++ b/device/esp_tinyusb/test_apps/cdc_and_usb_device/main/test_bvalid_sig.c @@ -121,6 +121,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 diff --git a/device/esp_tinyusb/test_apps/cdc_and_usb_device/main/test_descriptors_config.c b/device/esp_tinyusb/test_apps/cdc_and_usb_device/main/test_descriptors_config.c index a7c74bd..673761d 100644 --- a/device/esp_tinyusb/test_apps/cdc_and_usb_device/main/test_descriptors_config.c +++ b/device/esp_tinyusb/test_apps/cdc_and_usb_device/main/test_descriptors_config.c @@ -140,7 +140,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(); } @@ -163,7 +162,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(); } @@ -186,7 +184,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(); } @@ -208,7 +205,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(); } @@ -229,7 +225,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(); } @@ -250,7 +245,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 diff --git a/device/esp_tinyusb/tinyusb.c b/device/esp_tinyusb/tinyusb.c index 52bc2ae..2315ab5 100644 --- a/device/esp_tinyusb/tinyusb.c +++ b/device/esp_tinyusb/tinyusb.c @@ -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 */ @@ -19,6 +19,11 @@ const static char *TAG = "TinyUSB"; static usb_phy_handle_t phy_hdl; +// For the tinyusb component without tusb_teardown() implementation +#ifndef tusb_teardown +# define tusb_teardown() (true) +#endif // tusb_teardown + 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"); @@ -66,8 +71,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); }