@@ -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_MESSAGE (ESP_OK , tinyusb_driver_uninstall (), "Unable to uninstall driver after install in worker" );
5249 taskENTER_CRITICAL (& _spinlock );
@@ -101,12 +98,19 @@ 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 *
106105 * Scenario: Trying to install driver from several tasks
107106 * Note: when phy.skip_setup = false, the task access will be determined by the first task install the phy
107+ *
108+ * Parameter: tusb_cfg.task.blocking_timeout_ms
109+ * - default blocking
110+ * - non-blocking
111+ * - blocking indefinitely (legacy mode)
108112 */
109- TEST_CASE ("Multitask: Install" , "[runtime_config][default]" )
113+ TEST_CASE ("[ Multitask] Install, default blocking task " , "[runtime_config][default]" )
110114{
111115 usb_phy_handle_t phy_hdl = test_init_phy ();
112116
@@ -117,12 +121,102 @@ TEST_CASE("Multitask: Install", "[runtime_config][default]")
117121 // No task are running yet
118122 nb_of_success = 0 ;
119123
124+ // Configure TinyUSB default config
125+ tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG (test_device_event_handler );
126+ tusb_cfg .phy .skip_setup = true; // Skip phy setup to allow multiple tasks to install the driver
127+
120128 // Create tasks that will start the driver
121129 for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
122130 TEST_ASSERT_EQUAL (pdPASS , xTaskCreate (test_task_install ,
123131 "InstallTask" ,
124132 4096 ,
125- NULL ,
133+ (void * ) & tusb_cfg ,
134+ 4 + i ,
135+ & test_task_handles [i ]));
136+ }
137+
138+ // Start all tasks
139+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
140+ xTaskNotifyGive (test_task_handles [i ]);
141+ }
142+
143+ // Wait for all tasks to complete
144+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
145+ TEST_ASSERT_EQUAL_MESSAGE (pdTRUE , xSemaphoreTake (sem_done , pdMS_TO_TICKS (5000 )), "Not all tasks completed in time" );
146+ }
147+
148+ // There should be only one task that was able to install the driver
149+ TEST_ASSERT_EQUAL_MESSAGE (1 , nb_of_success , "Only one task should be able to install the driver" );
150+ // Clean-up
151+ test_deinit_phy (phy_hdl );
152+ vSemaphoreDelete (sem_done );
153+ }
154+
155+ TEST_CASE ("[Multitask] Install, non-blocking task" , "[runtime_config][default]" )
156+ {
157+ usb_phy_handle_t phy_hdl = test_init_phy ();
158+
159+ // Create counting semaphore to wait for all tasks to complete
160+ sem_done = xSemaphoreCreateCounting (MULTIPLE_THREADS_TASKS_NUM , 0 );
161+ TEST_ASSERT_NOT_NULL (sem_done );
162+
163+ // No task are running yet
164+ nb_of_success = 0 ;
165+
166+ // Configure TinyUSB default config
167+ tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG (test_device_event_handler );
168+ tusb_cfg .phy .skip_setup = true; // Skip phy setup to allow multiple tasks to install the driver
169+ tusb_cfg .task .blocking_timeout_ms = 0 ; // Non-blocking mode
170+
171+ // Create tasks that will start the driver
172+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
173+ TEST_ASSERT_EQUAL (pdPASS , xTaskCreate (test_task_install ,
174+ "InstallTask" ,
175+ 4096 ,
176+ (void * ) & tusb_cfg ,
177+ 4 + i ,
178+ & test_task_handles [i ]));
179+ }
180+
181+ // Start all tasks
182+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
183+ xTaskNotifyGive (test_task_handles [i ]);
184+ }
185+
186+ // Wait for all tasks to complete
187+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
188+ TEST_ASSERT_EQUAL_MESSAGE (pdTRUE , xSemaphoreTake (sem_done , pdMS_TO_TICKS (5000 )), "Not all tasks completed in time" );
189+ }
190+
191+ // There should be only one task that was able to install the driver
192+ TEST_ASSERT_EQUAL_MESSAGE (1 , nb_of_success , "Only one task should be able to install the driver" );
193+ // Clean-up
194+ test_deinit_phy (phy_hdl );
195+ vSemaphoreDelete (sem_done );
196+ }
197+
198+ TEST_CASE ("[Multitask] Install, blocking task indefinitely (legacy mode)" , "[runtime_config][default]" )
199+ {
200+ usb_phy_handle_t phy_hdl = test_init_phy ();
201+
202+ // Create counting semaphore to wait for all tasks to complete
203+ sem_done = xSemaphoreCreateCounting (MULTIPLE_THREADS_TASKS_NUM , 0 );
204+ TEST_ASSERT_NOT_NULL (sem_done );
205+
206+ // No task are running yet
207+ nb_of_success = 0 ;
208+
209+ // Configure TinyUSB default config
210+ tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG (test_device_event_handler );
211+ tusb_cfg .phy .skip_setup = true; // Skip phy setup to allow multiple tasks to install the driver
212+ tusb_cfg .task .blocking_timeout_ms = UINT32_MAX ; // Blocking indefinitely
213+
214+ // Create tasks that will start the driver
215+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
216+ TEST_ASSERT_EQUAL (pdPASS , xTaskCreate (test_task_install ,
217+ "InstallTask" ,
218+ 4096 ,
219+ (void * ) & tusb_cfg ,
126220 4 + i ,
127221 & test_task_handles [i ]));
128222 }
@@ -144,7 +238,48 @@ TEST_CASE("Multitask: Install", "[runtime_config][default]")
144238 vSemaphoreDelete (sem_done );
145239}
146240
147- TEST_CASE ("Multitask: Uninstall" , "[runtime_config][default]" )
241+ TEST_CASE ("[Multitask] Uninstall, default blocking task" , "[runtime_config][default]" )
242+ {
243+ usb_phy_handle_t phy_hdl = test_init_phy ();
244+ // Create counting semaphore to wait for all tasks to complete
245+ sem_done = xSemaphoreCreateCounting (MULTIPLE_THREADS_TASKS_NUM , 0 );
246+ TEST_ASSERT_NOT_NULL (sem_done );
247+
248+ // No task are running yet
249+ nb_of_success = 0 ;
250+
251+ // Install the driver once
252+ tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG (test_device_event_handler );
253+ tusb_cfg .phy .skip_setup = true; // Skip phy setup to allow multiple tasks
254+ TEST_ASSERT_EQUAL_MESSAGE (ESP_OK , tinyusb_driver_install (& tusb_cfg ), "Unable to install TinyUSB driver " );
255+ // Create tasks that will uninstall the driver
256+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
257+ TEST_ASSERT_EQUAL (pdPASS , xTaskCreate (test_task_uninstall ,
258+ "UninstallTask" ,
259+ 4096 ,
260+ NULL ,
261+ 4 + i ,
262+ & test_task_handles [i ]));
263+ }
264+
265+ // Start all tasks
266+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
267+ xTaskNotifyGive (test_task_handles [i ]);
268+ }
269+ // Wait for all tasks to complete
270+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
271+ TEST_ASSERT_EQUAL_MESSAGE (pdTRUE , xSemaphoreTake (sem_done , pdMS_TO_TICKS (5000 )), "Not all tasks completed in time" );
272+ }
273+
274+ // There should be only one task that was able to uninstall the driver
275+ TEST_ASSERT_EQUAL_MESSAGE (1 , nb_of_success , "Only one task should be able to uninstall the driver" );
276+
277+ // Clean-up
278+ test_deinit_phy (phy_hdl );
279+ vSemaphoreDelete (sem_done );
280+ }
281+
282+ TEST_CASE ("[Multitask] Uninstall, non-blocking task" , "[runtime_config][default]" )
148283{
149284 usb_phy_handle_t phy_hdl = test_init_phy ();
150285 // Create counting semaphore to wait for all tasks to complete
@@ -157,7 +292,52 @@ TEST_CASE("Multitask: Uninstall", "[runtime_config][default]")
157292 // Install the driver once
158293 tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG (test_device_event_handler );
159294 tusb_cfg .phy .skip_setup = true; // Skip phy setup to allow multiple tasks
295+ tusb_cfg .task .blocking_timeout_ms = 0 ; // Non-blocking mode
160296 TEST_ASSERT_EQUAL_MESSAGE (ESP_OK , tinyusb_driver_install (& tusb_cfg ), "Unable to install TinyUSB driver " );
297+
298+ // Create tasks that will uninstall the driver
299+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
300+ TEST_ASSERT_EQUAL (pdPASS , xTaskCreate (test_task_uninstall ,
301+ "UninstallTask" ,
302+ 4096 ,
303+ NULL ,
304+ 4 + i ,
305+ & test_task_handles [i ]));
306+ }
307+
308+ // Start all tasks
309+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
310+ xTaskNotifyGive (test_task_handles [i ]);
311+ }
312+ // Wait for all tasks to complete
313+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
314+ TEST_ASSERT_EQUAL_MESSAGE (pdTRUE , xSemaphoreTake (sem_done , pdMS_TO_TICKS (5000 )), "Not all tasks completed in time" );
315+ }
316+
317+ // There should be only one task that was able to uninstall the driver
318+ TEST_ASSERT_EQUAL_MESSAGE (1 , nb_of_success , "Only one task should be able to uninstall the driver" );
319+
320+ // Clean-up
321+ test_deinit_phy (phy_hdl );
322+ vSemaphoreDelete (sem_done );
323+ }
324+
325+ TEST_CASE ("[Multitask] Uninstall, blocking task indefinitely (legacy mode)" , "[runtime_config][default]" )
326+ {
327+ usb_phy_handle_t phy_hdl = test_init_phy ();
328+ // Create counting semaphore to wait for all tasks to complete
329+ sem_done = xSemaphoreCreateCounting (MULTIPLE_THREADS_TASKS_NUM , 0 );
330+ TEST_ASSERT_NOT_NULL (sem_done );
331+
332+ // No task are running yet
333+ nb_of_success = 0 ;
334+
335+ // Install the driver once
336+ tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG (test_device_event_handler );
337+ tusb_cfg .phy .skip_setup = true; // Skip phy setup to allow multiple tasks
338+ tusb_cfg .task .blocking_timeout_ms = UINT32_MAX ; // Blocking indefinitely
339+ TEST_ASSERT_EQUAL_MESSAGE (ESP_OK , tinyusb_driver_install (& tusb_cfg ), "Unable to install TinyUSB driver " );
340+
161341 // Create tasks that will uninstall the driver
162342 for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
163343 TEST_ASSERT_EQUAL (pdPASS , xTaskCreate (test_task_uninstall ,
0 commit comments