Skip to content

Commit 9fbdf0d

Browse files
committed
ref(esp_tinyusb): Added tusb_teardown() call
1 parent a94ccb2 commit 9fbdf0d

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

device/esp_tinyusb/include/tinyusb.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ typedef struct {
6868
*/
6969
esp_err_t tinyusb_driver_install(const tinyusb_config_t *config);
7070

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

7383
#ifdef __cplusplus

device/esp_tinyusb/test_app/main/test_bvalid_sig.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,5 @@ TEST_CASE("bvalid_signal", "[esp_tinyusb][usb_device]")
9898

9999
// Cleanup
100100
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
101-
TEST_ASSERT_EQUAL(ESP_OK, tusb_stop_task());
102101
}
103102
#endif // SOC_USB_OTG_SUPPORTED

device/esp_tinyusb/test_app/main/test_descriptors_config.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ TEST_CASE("descriptors_config_all_default", "[esp_tinyusb][usb_device]")
122122
TEST_ASSERT_EQUAL(ESP_OK, __test_wait_conn());
123123
// Cleanup
124124
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
125-
TEST_ASSERT_EQUAL(ESP_OK, tusb_stop_task());
126125
__test_free();
127126
}
128127

@@ -144,7 +143,6 @@ TEST_CASE("descriptors_config_device", "[esp_tinyusb][usb_device]")
144143
TEST_ASSERT_EQUAL(ESP_OK, __test_wait_conn());
145144
// Cleanup
146145
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
147-
TEST_ASSERT_EQUAL(ESP_OK, tusb_stop_task());
148146
__test_free();
149147
}
150148

@@ -166,7 +164,6 @@ TEST_CASE("descriptors_config_device_and_config", "[esp_tinyusb][usb_device]")
166164
TEST_ASSERT_EQUAL(ESP_OK, __test_wait_conn());
167165
// Cleanup
168166
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
169-
TEST_ASSERT_EQUAL(ESP_OK, tusb_stop_task());
170167
__test_free();
171168
}
172169

@@ -187,7 +184,6 @@ TEST_CASE("descriptors_config_device_and_fs_config_only", "[esp_tinyusb][usb_dev
187184
TEST_ASSERT_EQUAL(ESP_OK, __test_wait_conn());
188185
// Cleanup
189186
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
190-
TEST_ASSERT_EQUAL(ESP_OK, tusb_stop_task());
191187
__test_free();
192188
}
193189

@@ -207,7 +203,6 @@ TEST_CASE("descriptors_config_device_and_hs_config_only", "[esp_tinyusb][usb_dev
207203
TEST_ASSERT_EQUAL(ESP_OK, __test_wait_conn());
208204
// Cleanup
209205
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
210-
TEST_ASSERT_EQUAL(ESP_OK, tusb_stop_task());
211206
__test_free();
212207
}
213208

@@ -227,7 +222,6 @@ TEST_CASE("descriptors_config_all_configured", "[esp_tinyusb][usb_device]")
227222
TEST_ASSERT_EQUAL(ESP_OK, __test_wait_conn());
228223
// Cleanup
229224
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
230-
TEST_ASSERT_EQUAL(ESP_OK, tusb_stop_task());
231225
__test_free();
232226
}
233227
#endif // TUD_OPT_HIGH_SPEED

device/esp_tinyusb/tinyusb.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -19,6 +19,18 @@
1919
const static char *TAG = "TinyUSB";
2020
static usb_phy_handle_t phy_hdl;
2121

22+
/**
23+
* @brief Teardown device/host TinyUSB stack
24+
*
25+
* @note
26+
* For backward compatibility, when tusb_teardown() call is not implemented.
27+
*/
28+
__attribute__ ((weak)) bool tusb_teardown(void)
29+
{
30+
ESP_LOGW(TAG, "%s has not implemented, please update tinyusb component", __FUNCTION__);
31+
return true;
32+
}
33+
2234
esp_err_t tinyusb_driver_install(const tinyusb_config_t *config)
2335
{
2436
ESP_RETURN_ON_FALSE(config, ESP_ERR_INVALID_ARG, TAG, "Config can't be NULL");
@@ -66,8 +78,18 @@ esp_err_t tinyusb_driver_install(const tinyusb_config_t *config)
6678
return ESP_OK;
6779
}
6880

69-
esp_err_t tinyusb_driver_uninstall()
81+
esp_err_t tinyusb_driver_uninstall(void)
7082
{
83+
esp_err_t ret = tusb_stop_task();
84+
85+
if (ret != ESP_OK) {
86+
return ret;
87+
}
88+
89+
if (!tusb_teardown()) {
90+
return ESP_ERR_NOT_FINISHED;
91+
}
92+
7193
tinyusb_free_descriptors();
7294
return usb_del_phy(phy_hdl);
7395
}

0 commit comments

Comments
 (0)