Skip to content

Commit fa9ddc5

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

File tree

2 files changed

+276
-11
lines changed

2 files changed

+276
-11
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
@@ -138,7 +138,7 @@ static void test_cpu_load_measure(void)
138138
* - uninstall driver
139139
* - show results
140140
*/
141-
TEST_CASE("[CPU load] Install & Uninstall, default configuration", "[cpu_load]")
141+
TEST_CASE("[CPU load] Install & Uninstall, default blocking task", "[cpu_load]")
142142
{
143143
#if (!CONFIG_FREERTOS_UNICORE)
144144
// Allow other core to finish initialization
@@ -167,4 +167,90 @@ TEST_CASE("[CPU load] Install & Uninstall, default configuration", "[cpu_load]")
167167
printf("TinyUSB CPU load: %" PRIu32 " %%\n", _tinyusb_cpu_load);
168168
}
169169

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

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

Lines changed: 189 additions & 10 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(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

Comments
 (0)