@@ -100,10 +100,10 @@ static void tinyusb_device_task(void *arg)
100100 // Non-blocking check to stop the task
101101 uint32_t notif = 0 ;
102102 if (xTaskNotifyWait (
103- 0 , // don't clear any bits on entry
103+ 0 , // don't clear any bits on entry
104104 TASK_NOTIF_STOP_BIT , // clear STOP bit on exit if it was set
105105 & notif ,
106- 0 // no wait; just poll the bit
106+ 0 // no wait; just poll the bit
107107 ) == pdTRUE && (notif & TASK_NOTIF_STOP_BIT )) {
108108 // Stop requested
109109 stop_in_pending = true;
@@ -112,14 +112,14 @@ static void tinyusb_device_task(void *arg)
112112 // TinyUSB Device task
113113 tud_task_ext (2000 /* TODO: make dynamically configurable */ , false);
114114 }
115-
115+
116116 // Stop TinyUSB stack
117117 if (!tusb_deinit (task_ctx -> rhport )) {
118118 ESP_LOGE (TAG , "Unable to deinit TinyUSB stack" );
119119 }
120-
120+
121121 ESP_LOGD (TAG , "TinyUSB task has stopped" );
122-
122+
123123desc_free :
124124 tinyusb_descriptors_free ();
125125del :
@@ -226,24 +226,33 @@ esp_err_t tinyusb_task_stop(void)
226226 _task_is_pending = true; // Task is pending to stop
227227 TINYUSB_TASK_EXIT_CRITICAL ();
228228
229- /* Request TinyUSB task to stop */
230- if (xTaskNotify (task_ctx -> handle , TASK_NOTIF_STOP_BIT , eSetBits ) != pdPASS ) {
231- ESP_LOGE (TAG , "Unable to notify TinyUSB task to stop" );
232- return ESP_ERR_INVALID_STATE ;
233- }
229+ if (true /* tinyusb task polling time ms != 0 */ ) {
230+ /* Request TinyUSB task to stop */
231+ if (xTaskNotify (task_ctx -> handle , TASK_NOTIF_STOP_BIT , eSetBits ) != pdPASS ) {
232+ ESP_LOGE (TAG , "Unable to notify TinyUSB task to stop" );
233+ return ESP_ERR_INVALID_STATE ;
234+ }
235+ /* Wait for "stop" semaphore with timeout */
236+ if (xSemaphoreTake (task_ctx -> start_stop_sem , pdMS_TO_TICKS (TINYUSB_TASK_SEM_TAKE_TIMEOUT_MS )) != pdTRUE ) {
237+ ESP_LOGE (TAG , "Timeout while TinyUSB task stop" );
238+ return ESP_ERR_TIMEOUT ;
239+ }
234240
235- /* Wait for "stop" semaphore with timeout */
236- if (xSemaphoreTake (task_ctx -> start_stop_sem , pdMS_TO_TICKS (TINYUSB_TASK_SEM_TAKE_TIMEOUT_MS )) != pdTRUE ) {
237- ESP_LOGE (TAG , "Timeout while TinyUSB task stop" );
238- return ESP_ERR_TIMEOUT ;
241+ /* Check the global task context pointer and pending state */
242+ TINYUSB_TASK_ENTER_CRITICAL ();
243+ TINYUSB_TASK_CHECK_FROM_CRIT (!_task_is_pending , ESP_ERR_INVALID_STATE );
244+ TINYUSB_TASK_CHECK_FROM_CRIT (p_tusb_task_ctx == NULL , ESP_ERR_INVALID_STATE );
245+ TINYUSB_TASK_EXIT_CRITICAL ();
246+ } else {
247+ /* TinyUSB Task is forever loop, delete the task the old way */
248+ vTaskDelete (task_ctx -> handle );
249+ task_ctx -> handle = NULL ;
250+ /* Free descriptors */
251+ tinyusb_descriptors_free ();
252+ /* Deinit TinyUSB Stask */
253+ ESP_RETURN_ON_FALSE (tusb_deinit (task_ctx -> rhport ), ESP_ERR_INVALID_STATE , TAG , "Unable to deinit TinyUSB stack" );
239254 }
240255
241- /* Check the global task context pointer and pending state */
242- TINYUSB_TASK_ENTER_CRITICAL ();
243- TINYUSB_TASK_CHECK_FROM_CRIT (!_task_is_pending , ESP_ERR_INVALID_STATE );
244- TINYUSB_TASK_CHECK_FROM_CRIT (p_tusb_task_ctx == NULL , ESP_ERR_INVALID_STATE );
245- TINYUSB_TASK_EXIT_CRITICAL ();
246-
247256 /* Cleanup */
248257 vSemaphoreDelete (task_ctx -> start_stop_sem );
249258 heap_caps_free (task_ctx );
0 commit comments