Skip to content

Commit 1990835

Browse files
committed
refactor(esp_tinyusb): Added test cases for different blocking_timeout_ms config
1 parent af6ab85 commit 1990835

File tree

2 files changed

+276
-10
lines changed

2 files changed

+276
-10
lines changed

device/esp_tinyusb/test_apps/runtime_config/main/test_cpu_load.c

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ static void test_cpu_load_measure(void)
149149
* - uninstall driver
150150
* - show results
151151
*/
152-
TEST_CASE("[CPU load] Install & Uninstall, default configuration", "[cpu_load]")
152+
TEST_CASE("[CPU load] Install & Uninstall, default blocking task", "[cpu_load]")
153153
{
154154
#if (!CONFIG_FREERTOS_UNICORE)
155155
// Allow other core to finish initialization
@@ -178,4 +178,90 @@ TEST_CASE("[CPU load] Install & Uninstall, default configuration", "[cpu_load]")
178178
printf("TinyUSB CPU load: %" PRIu32 " %%\n", _tinyusb_cpu_load);
179179
}
180180

181+
/**
182+
* @brief Test TinyUSB CPU load measurement
183+
*
184+
* Scenario:
185+
* - Install TinyUSB driver with default configuration
186+
* - wait for device connection
187+
* - measure CPU load
188+
* - uninstall driver
189+
* - show results
190+
*/
191+
TEST_CASE("[CPU load] Install & Uninstall, non-blocking task", "[cpu_load]")
192+
{
193+
#if (!CONFIG_FREERTOS_UNICORE)
194+
// Allow other core to finish initialization
195+
vTaskDelay(pdMS_TO_TICKS(100));
196+
#endif // (!CONFIG_FREERTOS_UNICORE)
197+
198+
// Install TinyUSB driver
199+
tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG(test_device_event_handler);
200+
// Set the task blocking timeout to 0: non-blocking
201+
tusb_cfg.task.blocking_timeout_ms = 0;
202+
203+
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_install(&tusb_cfg));
204+
205+
// Initialize CPU load measurement
206+
test_cpu_load_init();
207+
208+
// Wait for the device to be mounted and enumerated by the Host
209+
test_device_wait();
210+
printf("\t -> Device connected\n");
211+
212+
// Measure CPU load
213+
test_cpu_load_measure();
214+
215+
// Uninstall TinyUSB driver
216+
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
217+
218+
// Show results
219+
printf("TinyUSB Run time: %" PRIu32 " ticks\n", _tinyusb_run_time);
220+
printf("TinyUSB CPU load: %" PRIu32 " %%\n", _tinyusb_cpu_load);
221+
}
222+
223+
/**
224+
* @brief Test TinyUSB CPU load measurement
225+
*
226+
* Scenario:
227+
* - Install TinyUSB driver with default configuration
228+
* - wait for device connection
229+
* - measure CPU load
230+
* - uninstall driver
231+
* - show results
232+
*/
233+
TEST_CASE("[CPU load] Install & Uninstall, blocking task indefinitely (legacy mode)", "[cpu_load]")
234+
{
235+
#if (!CONFIG_FREERTOS_UNICORE)
236+
// Allow other core to finish initialization
237+
vTaskDelay(pdMS_TO_TICKS(100));
238+
#endif // (!CONFIG_FREERTOS_UNICORE)
239+
240+
// Install TinyUSB driver
241+
tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG(test_device_event_handler);
242+
// Set the task blocking timeout to UINT32_t_MAX: blocking indefinitely
243+
tusb_cfg.task.blocking_timeout_ms = UINT32_MAX;
244+
245+
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_install(&tusb_cfg));
246+
247+
// Initialize CPU load measurement
248+
test_cpu_load_init();
249+
250+
// Wait for the device to be mounted and enumerated by the Host
251+
test_device_wait();
252+
printf("\t -> Device connected\n");
253+
254+
// Measure CPU load
255+
test_cpu_load_measure();
256+
257+
// Uninstall TinyUSB driver
258+
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
259+
260+
// Show results
261+
printf("TinyUSB Run time: %" PRIu32 " ticks\n", _tinyusb_run_time);
262+
printf("TinyUSB CPU load: %" PRIu32 " %%\n", _tinyusb_cpu_load);
263+
}
264+
265+
266+
181267
#endif // SOC_USB_OTG_SUPPORTED

device/esp_tinyusb/test_apps/runtime_config/main/test_multitask_access.c

Lines changed: 189 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,12 @@ static volatile int nb_of_success = 0;
3838

3939
static 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

Comments
 (0)