Skip to content

Commit 6dc6c0d

Browse files
committed
refactor(esp_tinyusb): Added test cases for different blocking_timeout_ms config
1 parent 2d1fe05 commit 6dc6c0d

File tree

3 files changed

+277
-11
lines changed

3 files changed

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

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

device/esp_tinyusb/test_apps/runtime_config/pytest_runtime_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ def test_cpu_load_task_stat_print(dut: IdfDut) -> None:
4242

4343
line = dut.expect(r'TinyUSB Run time: (\d+) ticks')
4444
run_time = int(line.group(1))
45-
assert 0 < run_time < 1800, f'Unexpected TinyUSB Run time: {run_time}'
45+
assert 0 < run_time < 3000, f'Unexpected TinyUSB Run time: {run_time}'

0 commit comments

Comments
 (0)