@@ -38,15 +38,12 @@ static volatile int nb_of_success = 0;
3838
3939static void test_task_install (void * arg )
4040{
41- ( void ) arg ;
41+ tinyusb_config_t * tusb_cfg = ( tinyusb_config_t * ) arg ;
4242 // Install TinyUSB driver
43- tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG (test_device_event_handler );
44- tusb_cfg .phy .skip_setup = true; // Skip phy setup to allow multiple tasks to install the driver
45-
4643 // Wait to be started by main thread
4744 ulTaskNotifyTake (pdTRUE , portMAX_DELAY );
4845
49- if (tinyusb_driver_install (& tusb_cfg ) == ESP_OK ) {
46+ if (tinyusb_driver_install (tusb_cfg ) == ESP_OK ) {
5047 test_device_wait ();
5148 TEST_ASSERT_EQUAL (ESP_OK , tinyusb_driver_uninstall ());
5249 taskENTER_CRITICAL (& _spinlock );
@@ -101,12 +98,18 @@ static void test_deinit_phy(usb_phy_handle_t phy_hdl)
10198// ============================= Tests =========================================
10299
103100/**
104- * @brief TinyUSB Task specific testcase
101+ * @brief TinyUSB Multitask access test cases
102+ *
103+ * Scenario: Trying to install and uninstall the driver from several tasks
105104 *
106- * Scenario: Trying to install driver from several tasks
107105 * Note: when skip_phy_setup = false, the task access will be determined by the first task install the phy
106+ *
107+ * Parameter: tusb_cfg.task.blocking_timeout_ms
108+ * - default blocking
109+ * - non-blocking
110+ * - blocking indefinitely (legacy mode)
108111 */
109- TEST_CASE ("Multitask: Install" , "[runtime_config][full_speed][high_speed]" )
112+ TEST_CASE ("[ Multitask] Install, default blocking task " , "[runtime_config][full_speed][high_speed]" )
110113{
111114 usb_phy_handle_t phy_hdl = test_init_phy ();
112115
@@ -117,12 +120,102 @@ TEST_CASE("Multitask: Install", "[runtime_config][full_speed][high_speed]")
117120 // No task are running yet
118121 nb_of_success = 0 ;
119122
123+ // Configure TinyUSB default config
124+ tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG (test_device_event_handler );
125+ tusb_cfg .phy .skip_setup = true; // Skip phy setup to allow multiple tasks to install the driver
126+
120127 // Create tasks that will start the driver
121128 for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
122129 TEST_ASSERT_EQUAL (pdPASS , xTaskCreate (test_task_install ,
123130 "InstallTask" ,
124131 4096 ,
125- NULL ,
132+ (void * ) & tusb_cfg ,
133+ 4 + i ,
134+ & test_task_handles [i ]));
135+ }
136+
137+ // Start all tasks
138+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
139+ xTaskNotifyGive (test_task_handles [i ]);
140+ }
141+
142+ // Wait for all tasks to complete
143+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
144+ TEST_ASSERT_EQUAL_MESSAGE (pdTRUE , xSemaphoreTake (sem_done , pdMS_TO_TICKS (5000 )), "Not all tasks completed in time" );
145+ }
146+
147+ // There should be only one task that was able to install the driver
148+ TEST_ASSERT_EQUAL_MESSAGE (1 , nb_of_success , "Only one task should be able to install the driver" );
149+ // Clean-up
150+ test_deinit_phy (phy_hdl );
151+ vSemaphoreDelete (sem_done );
152+ }
153+
154+ TEST_CASE ("[Multitask] Install, non-blocking task" , "[runtime_config][full_speed][high_speed]" )
155+ {
156+ usb_phy_handle_t phy_hdl = test_init_phy ();
157+
158+ // Create counting semaphore to wait for all tasks to complete
159+ sem_done = xSemaphoreCreateCounting (MULTIPLE_THREADS_TASKS_NUM , 0 );
160+ TEST_ASSERT_NOT_NULL (sem_done );
161+
162+ // No task are running yet
163+ nb_of_success = 0 ;
164+
165+ // Configure TinyUSB default config
166+ tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG (test_device_event_handler );
167+ tusb_cfg .phy .skip_setup = true; // Skip phy setup to allow multiple tasks to install the driver
168+ tusb_cfg .task .blocking_timeout_ms = 0 ; // Non-blocking mode
169+
170+ // Create tasks that will start the driver
171+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
172+ TEST_ASSERT_EQUAL (pdPASS , xTaskCreate (test_task_install ,
173+ "InstallTask" ,
174+ 4096 ,
175+ (void * ) & tusb_cfg ,
176+ 4 + i ,
177+ & test_task_handles [i ]));
178+ }
179+
180+ // Start all tasks
181+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
182+ xTaskNotifyGive (test_task_handles [i ]);
183+ }
184+
185+ // Wait for all tasks to complete
186+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
187+ TEST_ASSERT_EQUAL_MESSAGE (pdTRUE , xSemaphoreTake (sem_done , pdMS_TO_TICKS (5000 )), "Not all tasks completed in time" );
188+ }
189+
190+ // There should be only one task that was able to install the driver
191+ TEST_ASSERT_EQUAL_MESSAGE (1 , nb_of_success , "Only one task should be able to install the driver" );
192+ // Clean-up
193+ test_deinit_phy (phy_hdl );
194+ vSemaphoreDelete (sem_done );
195+ }
196+
197+ TEST_CASE ("[Multitask] Install, blocking task indefinitely (legacy mode)" , "[runtime_config][full_speed][high_speed]" )
198+ {
199+ usb_phy_handle_t phy_hdl = test_init_phy ();
200+
201+ // Create counting semaphore to wait for all tasks to complete
202+ sem_done = xSemaphoreCreateCounting (MULTIPLE_THREADS_TASKS_NUM , 0 );
203+ TEST_ASSERT_NOT_NULL (sem_done );
204+
205+ // No task are running yet
206+ nb_of_success = 0 ;
207+
208+ // Configure TinyUSB default config
209+ tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG (test_device_event_handler );
210+ tusb_cfg .phy .skip_setup = true; // Skip phy setup to allow multiple tasks to install the driver
211+ tusb_cfg .task .blocking_timeout_ms = UINT32_MAX ; // Blocking indefinitely
212+
213+ // Create tasks that will start the driver
214+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
215+ TEST_ASSERT_EQUAL (pdPASS , xTaskCreate (test_task_install ,
216+ "InstallTask" ,
217+ 4096 ,
218+ (void * ) & tusb_cfg ,
126219 4 + i ,
127220 & test_task_handles [i ]));
128221 }
@@ -144,7 +237,48 @@ TEST_CASE("Multitask: Install", "[runtime_config][full_speed][high_speed]")
144237 vSemaphoreDelete (sem_done );
145238}
146239
147- TEST_CASE ("Multitask: Uninstall" , "[runtime_config][full_speed][high_speed]" )
240+ TEST_CASE ("[Multitask] Uninstall, default blocking task" , "[runtime_config][full_speed][high_speed]" )
241+ {
242+ usb_phy_handle_t phy_hdl = test_init_phy ();
243+ // Create counting semaphore to wait for all tasks to complete
244+ sem_done = xSemaphoreCreateCounting (MULTIPLE_THREADS_TASKS_NUM , 0 );
245+ TEST_ASSERT_NOT_NULL (sem_done );
246+
247+ // No task are running yet
248+ nb_of_success = 0 ;
249+
250+ // Install the driver once
251+ tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG (test_device_event_handler );
252+ tusb_cfg .phy .skip_setup = true; // Skip phy setup to allow multiple tasks
253+ TEST_ASSERT_EQUAL_MESSAGE (ESP_OK , tinyusb_driver_install (& tusb_cfg ), "Unable to install TinyUSB driver " );
254+ // Create tasks that will uninstall the driver
255+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
256+ TEST_ASSERT_EQUAL (pdPASS , xTaskCreate (test_task_uninstall ,
257+ "UninstallTask" ,
258+ 4096 ,
259+ NULL ,
260+ 4 + i ,
261+ & test_task_handles [i ]));
262+ }
263+
264+ // Start all tasks
265+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
266+ xTaskNotifyGive (test_task_handles [i ]);
267+ }
268+ // Wait for all tasks to complete
269+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
270+ TEST_ASSERT_EQUAL_MESSAGE (pdTRUE , xSemaphoreTake (sem_done , pdMS_TO_TICKS (5000 )), "Not all tasks completed in time" );
271+ }
272+
273+ // There should be only one task that was able to uninstall the driver
274+ TEST_ASSERT_EQUAL_MESSAGE (1 , nb_of_success , "Only one task should be able to uninstall the driver" );
275+
276+ // Clean-up
277+ test_deinit_phy (phy_hdl );
278+ vSemaphoreDelete (sem_done );
279+ }
280+
281+ TEST_CASE ("[Multitask] Uninstall, non-blocking task" , "[runtime_config][full_speed][high_speed]" )
148282{
149283 usb_phy_handle_t phy_hdl = test_init_phy ();
150284 // Create counting semaphore to wait for all tasks to complete
@@ -157,7 +291,52 @@ TEST_CASE("Multitask: Uninstall", "[runtime_config][full_speed][high_speed]")
157291 // Install the driver once
158292 tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG (test_device_event_handler );
159293 tusb_cfg .phy .skip_setup = true; // Skip phy setup to allow multiple tasks
294+ tusb_cfg .task .blocking_timeout_ms = 0 ; // Non-blocking mode
160295 TEST_ASSERT_EQUAL_MESSAGE (ESP_OK , tinyusb_driver_install (& tusb_cfg ), "Unable to install TinyUSB driver " );
296+
297+ // Create tasks that will uninstall the driver
298+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
299+ TEST_ASSERT_EQUAL (pdPASS , xTaskCreate (test_task_uninstall ,
300+ "UninstallTask" ,
301+ 4096 ,
302+ NULL ,
303+ 4 + i ,
304+ & test_task_handles [i ]));
305+ }
306+
307+ // Start all tasks
308+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
309+ xTaskNotifyGive (test_task_handles [i ]);
310+ }
311+ // Wait for all tasks to complete
312+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
313+ TEST_ASSERT_EQUAL_MESSAGE (pdTRUE , xSemaphoreTake (sem_done , pdMS_TO_TICKS (5000 )), "Not all tasks completed in time" );
314+ }
315+
316+ // There should be only one task that was able to uninstall the driver
317+ TEST_ASSERT_EQUAL_MESSAGE (1 , nb_of_success , "Only one task should be able to uninstall the driver" );
318+
319+ // Clean-up
320+ test_deinit_phy (phy_hdl );
321+ vSemaphoreDelete (sem_done );
322+ }
323+
324+ TEST_CASE ("[Multitask] Uninstall, blocking task indefinitely (legacy mode)" , "[runtime_config][full_speed][high_speed]" )
325+ {
326+ usb_phy_handle_t phy_hdl = test_init_phy ();
327+ // Create counting semaphore to wait for all tasks to complete
328+ sem_done = xSemaphoreCreateCounting (MULTIPLE_THREADS_TASKS_NUM , 0 );
329+ TEST_ASSERT_NOT_NULL (sem_done );
330+
331+ // No task are running yet
332+ nb_of_success = 0 ;
333+
334+ // Install the driver once
335+ tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG (test_device_event_handler );
336+ tusb_cfg .phy .skip_setup = true; // Skip phy setup to allow multiple tasks
337+ tusb_cfg .task .blocking_timeout_ms = UINT32_MAX ; // Blocking indefinitely
338+ TEST_ASSERT_EQUAL_MESSAGE (ESP_OK , tinyusb_driver_install (& tusb_cfg ), "Unable to install TinyUSB driver " );
339+
161340 // Create tasks that will uninstall the driver
162341 for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
163342 TEST_ASSERT_EQUAL (pdPASS , xTaskCreate (test_task_uninstall ,
0 commit comments