Skip to content

Commit 4d5dbf7

Browse files
committed
feature(esp_tinyusb): Added tusb_teardown() call
1 parent e5feb3f commit 4d5dbf7

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-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_apps/cdc_and_usb_device/main/test_bvalid_sig.c

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

122122
// Cleanup
123123
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
124-
TEST_ASSERT_EQUAL(ESP_OK, tusb_stop_task());
125124
}
126125
#endif // SOC_USB_OTG_SUPPORTED

device/esp_tinyusb/test_apps/cdc_and_usb_device/main/test_descriptors_config.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ TEST_CASE("descriptors_config_all_default", "[esp_tinyusb][usb_device]")
140140
TEST_ASSERT_EQUAL(ESP_OK, __test_wait_conn());
141141
// Cleanup
142142
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
143-
TEST_ASSERT_EQUAL(ESP_OK, tusb_stop_task());
144143
__test_free();
145144
}
146145

@@ -163,7 +162,6 @@ TEST_CASE("descriptors_config_device", "[esp_tinyusb][usb_device]")
163162
TEST_ASSERT_EQUAL(ESP_OK, __test_wait_conn());
164163
// Cleanup
165164
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
166-
TEST_ASSERT_EQUAL(ESP_OK, tusb_stop_task());
167165
__test_free();
168166
}
169167

@@ -186,7 +184,6 @@ TEST_CASE("descriptors_config_device_and_config", "[esp_tinyusb][usb_device]")
186184
TEST_ASSERT_EQUAL(ESP_OK, __test_wait_conn());
187185
// Cleanup
188186
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
189-
TEST_ASSERT_EQUAL(ESP_OK, tusb_stop_task());
190187
__test_free();
191188
}
192189

@@ -208,7 +205,6 @@ TEST_CASE("descriptors_config_device_and_fs_config_only", "[esp_tinyusb][usb_dev
208205
TEST_ASSERT_EQUAL(ESP_OK, __test_wait_conn());
209206
// Cleanup
210207
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
211-
TEST_ASSERT_EQUAL(ESP_OK, tusb_stop_task());
212208
__test_free();
213209
}
214210

@@ -229,7 +225,6 @@ TEST_CASE("descriptors_config_device_and_hs_config_only", "[esp_tinyusb][usb_dev
229225
TEST_ASSERT_EQUAL(ESP_OK, __test_wait_conn());
230226
// Cleanup
231227
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
232-
TEST_ASSERT_EQUAL(ESP_OK, tusb_stop_task());
233228
__test_free();
234229
}
235230

@@ -250,7 +245,6 @@ TEST_CASE("descriptors_config_all_configured", "[esp_tinyusb][usb_device]")
250245
TEST_ASSERT_EQUAL(ESP_OK, __test_wait_conn());
251246
// Cleanup
252247
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
253-
TEST_ASSERT_EQUAL(ESP_OK, tusb_stop_task());
254248
__test_free();
255249
}
256250
#endif // TUD_OPT_HIGH_SPEED

device/esp_tinyusb/tinyusb.c

Lines changed: 17 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,11 @@
1919
const static char *TAG = "TinyUSB";
2020
static usb_phy_handle_t phy_hdl;
2121

22+
// For the tinyusb component without tusb_teardown() implementation
23+
#ifndef tusb_teardown
24+
# define tusb_teardown() (true)
25+
#endif // tusb_teardown
26+
2227
esp_err_t tinyusb_driver_install(const tinyusb_config_t *config)
2328
{
2429
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)
6671
return ESP_OK;
6772
}
6873

69-
esp_err_t tinyusb_driver_uninstall()
74+
esp_err_t tinyusb_driver_uninstall(void)
7075
{
76+
esp_err_t ret = tusb_stop_task();
77+
78+
if (ret != ESP_OK) {
79+
return ret;
80+
}
81+
82+
if (!tusb_teardown()) {
83+
return ESP_ERR_NOT_FINISHED;
84+
}
85+
7186
tinyusb_free_descriptors();
7287
return usb_del_phy(phy_hdl);
7388
}

0 commit comments

Comments
 (0)