Skip to content

Commit

Permalink
feature(esp_tinyusb): Added tusb_teardown() call
Browse files Browse the repository at this point in the history
  • Loading branch information
roma-jam committed Dec 19, 2024
1 parent 555db55 commit a1832e0
Show file tree
Hide file tree
Showing 4 changed files with 27 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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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();
}

Expand All @@ -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();
}

Expand All @@ -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();
}

Expand All @@ -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();
}

Expand All @@ -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
Expand Down
19 changes: 17 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,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");
Expand Down Expand Up @@ -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);
}

0 comments on commit a1832e0

Please sign in to comment.