Skip to content

Commit 4c78584

Browse files
committed
refactor(esp_tinyusb): Added test cases for different blocking_timeout_ms config
1 parent 429ba93 commit 4c78584

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);
@@ -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,

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)