@@ -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 );
@@ -103,12 +100,19 @@ static void test_deinit_phy(usb_phy_handle_t phy_hdl)
103100// ============================= Tests =========================================
104101
105102/**
106- * @brief TinyUSB Task specific testcase
103+ * @brief TinyUSB Multitask access test cases
104+ *
105+ * Scenario: Trying to install and uninstall the driver from several tasks
107106 *
108107 * Scenario: Trying to install driver from several tasks
109108 * Note: when phy.skip_setup = false, the task access will be determined by the first task install the phy
109+ *
110+ * Parameter: tusb_cfg.task.blocking_timeout_ms
111+ * - default blocking
112+ * - non-blocking
113+ * - blocking indefinitely (legacy mode)
110114 */
111- TEST_CASE ("Multitask: Install" , "[runtime_config][default]" )
115+ TEST_CASE ("[ Multitask] Install, default blocking task " , "[runtime_config][default]" )
112116{
113117 usb_phy_handle_t phy_hdl = test_init_phy ();
114118
@@ -119,12 +123,102 @@ TEST_CASE("Multitask: Install", "[runtime_config][default]")
119123 // No task are running yet
120124 nb_of_success = 0 ;
121125
126+ // Configure TinyUSB default config
127+ tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG (test_device_event_handler );
128+ tusb_cfg .phy .skip_setup = true; // Skip phy setup to allow multiple tasks to install the driver
129+
122130 // Create tasks that will start the driver
123131 for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
124132 TEST_ASSERT_EQUAL (pdPASS , xTaskCreate (test_task_install ,
125133 "InstallTask" ,
126134 4096 ,
127- NULL ,
135+ (void * ) & tusb_cfg ,
136+ 4 + i ,
137+ & test_task_handles [i ]));
138+ }
139+
140+ // Start all tasks
141+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
142+ xTaskNotifyGive (test_task_handles [i ]);
143+ }
144+
145+ // Wait for all tasks to complete
146+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
147+ TEST_ASSERT_EQUAL_MESSAGE (pdTRUE , xSemaphoreTake (sem_done , pdMS_TO_TICKS (5000 )), "Not all tasks completed in time" );
148+ }
149+
150+ // There should be only one task that was able to install the driver
151+ TEST_ASSERT_EQUAL_MESSAGE (1 , nb_of_success , "Only one task should be able to install the driver" );
152+ // Clean-up
153+ test_deinit_phy (phy_hdl );
154+ vSemaphoreDelete (sem_done );
155+ }
156+
157+ TEST_CASE ("[Multitask] Install, non-blocking task" , "[runtime_config][default]" )
158+ {
159+ usb_phy_handle_t phy_hdl = test_init_phy ();
160+
161+ // Create counting semaphore to wait for all tasks to complete
162+ sem_done = xSemaphoreCreateCounting (MULTIPLE_THREADS_TASKS_NUM , 0 );
163+ TEST_ASSERT_NOT_NULL (sem_done );
164+
165+ // No task are running yet
166+ nb_of_success = 0 ;
167+
168+ // Configure TinyUSB default config
169+ tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG (test_device_event_handler );
170+ tusb_cfg .phy .skip_setup = true; // Skip phy setup to allow multiple tasks to install the driver
171+ tusb_cfg .task .blocking_timeout_ms = 0 ; // Non-blocking mode
172+
173+ // Create tasks that will start the driver
174+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
175+ TEST_ASSERT_EQUAL (pdPASS , xTaskCreate (test_task_install ,
176+ "InstallTask" ,
177+ 4096 ,
178+ (void * ) & tusb_cfg ,
179+ 4 + i ,
180+ & test_task_handles [i ]));
181+ }
182+
183+ // Start all tasks
184+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
185+ xTaskNotifyGive (test_task_handles [i ]);
186+ }
187+
188+ // Wait for all tasks to complete
189+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
190+ TEST_ASSERT_EQUAL_MESSAGE (pdTRUE , xSemaphoreTake (sem_done , pdMS_TO_TICKS (5000 )), "Not all tasks completed in time" );
191+ }
192+
193+ // There should be only one task that was able to install the driver
194+ TEST_ASSERT_EQUAL_MESSAGE (1 , nb_of_success , "Only one task should be able to install the driver" );
195+ // Clean-up
196+ test_deinit_phy (phy_hdl );
197+ vSemaphoreDelete (sem_done );
198+ }
199+
200+ TEST_CASE ("[Multitask] Install, blocking task indefinitely (legacy mode)" , "[runtime_config][default]" )
201+ {
202+ usb_phy_handle_t phy_hdl = test_init_phy ();
203+
204+ // Create counting semaphore to wait for all tasks to complete
205+ sem_done = xSemaphoreCreateCounting (MULTIPLE_THREADS_TASKS_NUM , 0 );
206+ TEST_ASSERT_NOT_NULL (sem_done );
207+
208+ // No task are running yet
209+ nb_of_success = 0 ;
210+
211+ // Configure TinyUSB default config
212+ tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG (test_device_event_handler );
213+ tusb_cfg .phy .skip_setup = true; // Skip phy setup to allow multiple tasks to install the driver
214+ tusb_cfg .task .blocking_timeout_ms = UINT32_MAX ; // Blocking indefinitely
215+
216+ // Create tasks that will start the driver
217+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
218+ TEST_ASSERT_EQUAL (pdPASS , xTaskCreate (test_task_install ,
219+ "InstallTask" ,
220+ 4096 ,
221+ (void * ) & tusb_cfg ,
128222 4 + i ,
129223 & test_task_handles [i ]));
130224 }
@@ -146,7 +240,48 @@ TEST_CASE("Multitask: Install", "[runtime_config][default]")
146240 vSemaphoreDelete (sem_done );
147241}
148242
149- TEST_CASE ("Multitask: Uninstall" , "[runtime_config][default]" )
243+ TEST_CASE ("[Multitask] Uninstall, default blocking task" , "[runtime_config][default]" )
244+ {
245+ usb_phy_handle_t phy_hdl = test_init_phy ();
246+ // Create counting semaphore to wait for all tasks to complete
247+ sem_done = xSemaphoreCreateCounting (MULTIPLE_THREADS_TASKS_NUM , 0 );
248+ TEST_ASSERT_NOT_NULL (sem_done );
249+
250+ // No task are running yet
251+ nb_of_success = 0 ;
252+
253+ // Install the driver once
254+ tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG (test_device_event_handler );
255+ tusb_cfg .phy .skip_setup = true; // Skip phy setup to allow multiple tasks
256+ TEST_ASSERT_EQUAL_MESSAGE (ESP_OK , tinyusb_driver_install (& tusb_cfg ), "Unable to install TinyUSB driver " );
257+ // Create tasks that will uninstall the driver
258+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
259+ TEST_ASSERT_EQUAL (pdPASS , xTaskCreate (test_task_uninstall ,
260+ "UninstallTask" ,
261+ 4096 ,
262+ NULL ,
263+ 4 + i ,
264+ & test_task_handles [i ]));
265+ }
266+
267+ // Start all tasks
268+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
269+ xTaskNotifyGive (test_task_handles [i ]);
270+ }
271+ // Wait for all tasks to complete
272+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
273+ TEST_ASSERT_EQUAL_MESSAGE (pdTRUE , xSemaphoreTake (sem_done , pdMS_TO_TICKS (5000 )), "Not all tasks completed in time" );
274+ }
275+
276+ // There should be only one task that was able to uninstall the driver
277+ TEST_ASSERT_EQUAL_MESSAGE (1 , nb_of_success , "Only one task should be able to uninstall the driver" );
278+
279+ // Clean-up
280+ test_deinit_phy (phy_hdl );
281+ vSemaphoreDelete (sem_done );
282+ }
283+
284+ TEST_CASE ("[Multitask] Uninstall, non-blocking task" , "[runtime_config][default]" )
150285{
151286 usb_phy_handle_t phy_hdl = test_init_phy ();
152287 // Create counting semaphore to wait for all tasks to complete
@@ -159,7 +294,52 @@ TEST_CASE("Multitask: Uninstall", "[runtime_config][default]")
159294 // Install the driver once
160295 tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG (test_device_event_handler );
161296 tusb_cfg .phy .skip_setup = true; // Skip phy setup to allow multiple tasks
297+ tusb_cfg .task .blocking_timeout_ms = 0 ; // Non-blocking mode
162298 TEST_ASSERT_EQUAL_MESSAGE (ESP_OK , tinyusb_driver_install (& tusb_cfg ), "Unable to install TinyUSB driver " );
299+
300+ // Create tasks that will uninstall the driver
301+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
302+ TEST_ASSERT_EQUAL (pdPASS , xTaskCreate (test_task_uninstall ,
303+ "UninstallTask" ,
304+ 4096 ,
305+ NULL ,
306+ 4 + i ,
307+ & test_task_handles [i ]));
308+ }
309+
310+ // Start all tasks
311+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
312+ xTaskNotifyGive (test_task_handles [i ]);
313+ }
314+ // Wait for all tasks to complete
315+ for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
316+ TEST_ASSERT_EQUAL_MESSAGE (pdTRUE , xSemaphoreTake (sem_done , pdMS_TO_TICKS (5000 )), "Not all tasks completed in time" );
317+ }
318+
319+ // There should be only one task that was able to uninstall the driver
320+ TEST_ASSERT_EQUAL_MESSAGE (1 , nb_of_success , "Only one task should be able to uninstall the driver" );
321+
322+ // Clean-up
323+ test_deinit_phy (phy_hdl );
324+ vSemaphoreDelete (sem_done );
325+ }
326+
327+ TEST_CASE ("[Multitask] Uninstall, blocking task indefinitely (legacy mode)" , "[runtime_config][default]" )
328+ {
329+ usb_phy_handle_t phy_hdl = test_init_phy ();
330+ // Create counting semaphore to wait for all tasks to complete
331+ sem_done = xSemaphoreCreateCounting (MULTIPLE_THREADS_TASKS_NUM , 0 );
332+ TEST_ASSERT_NOT_NULL (sem_done );
333+
334+ // No task are running yet
335+ nb_of_success = 0 ;
336+
337+ // Install the driver once
338+ tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG (test_device_event_handler );
339+ tusb_cfg .phy .skip_setup = true; // Skip phy setup to allow multiple tasks
340+ tusb_cfg .task .blocking_timeout_ms = UINT32_MAX ; // Blocking indefinitely
341+ TEST_ASSERT_EQUAL_MESSAGE (ESP_OK , tinyusb_driver_install (& tusb_cfg ), "Unable to install TinyUSB driver " );
342+
163343 // Create tasks that will uninstall the driver
164344 for (int i = 0 ; i < MULTIPLE_THREADS_TASKS_NUM ; i ++ ) {
165345 TEST_ASSERT_EQUAL (pdPASS , xTaskCreate (test_task_uninstall ,
0 commit comments