diff --git a/device/esp_tinyusb/CMakeLists.txt b/device/esp_tinyusb/CMakeLists.txt index 98fc960e..6ced87e8 100644 --- a/device/esp_tinyusb/CMakeLists.txt +++ b/device/esp_tinyusb/CMakeLists.txt @@ -4,9 +4,9 @@ set(srcs "usb_descriptors.c" ) -if(NOT CONFIG_TINYUSB_NO_DEFAULT_TASK) +if(CONFIG_TINYUSB_DEFAULT_TASK) list(APPEND srcs "tusb_tasks.c") -endif() # CONFIG_TINYUSB_NO_DEFAULT_TASK +endif() # CONFIG_TINYUSB_DEFAULT_TASK if(CONFIG_TINYUSB_CDC_ENABLED) list(APPEND srcs diff --git a/device/esp_tinyusb/Kconfig b/device/esp_tinyusb/Kconfig index 62383425..49cd94df 100644 --- a/device/esp_tinyusb/Kconfig +++ b/device/esp_tinyusb/Kconfig @@ -1,4 +1,4 @@ -menu "TinyUSB Stack" +menu "ESP TinyUSB" config TINYUSB_DEBUG_LEVEL int "TinyUSB log level (0-3)" default 1 @@ -48,39 +48,31 @@ menu "TinyUSB Stack" endchoice endmenu # "TinyUSB DCD" - menu "TinyUSB task configuration" - config TINYUSB_NO_DEFAULT_TASK - bool "Do not create a TinyUSB task" - default n - help - This option allows to not create the FreeRTOS task during the driver initialization. - User will have to handle TinyUSB events manually. + config TINYUSB_DEFAULT_TASK + bool "Create TinyUSB task" + default y + help + To handle USB event, TinyUSB task tud_task() should be implemented. This option allows create the FreeRTOS task during the driver initialization with default parameters. - config TINYUSB_TASK_PRIORITY - int "TinyUSB task priority" - default 5 - depends on !TINYUSB_NO_DEFAULT_TASK - help - Set the priority of the default TinyUSB main task. + Enable - FreeRTOS TinyUSB task (tud_task()) is implemented by the current component. + Disable - FreeRTOS TinyUSB task (tud_task()) must be implemented outside of the current component. - config TINYUSB_TASK_STACK_SIZE - int "TinyUSB task stack size (bytes)" - default 4096 - depends on !TINYUSB_NO_DEFAULT_TASK - help - Set the stack size of the default TinyUSB main task. + Enabled by default. + menu "Task configuration" + depends on TINYUSB_DEFAULT_TASK choice TINYUSB_TASK_AFFINITY - prompt "TinyUSB task affinity" + prompt "CPU affinity" default TINYUSB_TASK_AFFINITY_CPU1 if !FREERTOS_UNICORE default TINYUSB_TASK_AFFINITY_NO_AFFINITY - depends on !TINYUSB_NO_DEFAULT_TASK help Allows setting TinyUSB tasks affinity, i.e. whether the task is pinned to CPU0, pinned to CPU1, or allowed to run on any CPU. + Default value: No affinity for unicore systems, pinned to CPU1 for multicore. + config TINYUSB_TASK_AFFINITY_NO_AFFINITY - bool "No affinity" + bool "None" config TINYUSB_TASK_AFFINITY_CPU0 bool "CPU0" config TINYUSB_TASK_AFFINITY_CPU1 @@ -88,21 +80,42 @@ menu "TinyUSB Stack" depends on !FREERTOS_UNICORE endchoice + config TINYUSB_INIT_IN_TASK + bool "Init in task" + default n if TINYUSB_DEFAULT_TASK + default y + depends on !TINYUSB_TASK_AFFINITY_NO_AFFINITY + help + Run TinyUSB stack initialization just after starting the default TinyUSB task. + This is especially useful in multicore scenarios, when we need to pin the task + to a specific core and, at the same time initialize TinyUSB stack + (i.e. install interrupts) on the same core. + + Disabled by default, when TINYUSB_DEFAULT_TASK is enabled. + Enabled by default, when TINYUSB_DEFAULT_TASK is disabled. + + config TINYUSB_TASK_PRIORITY + int "Priority" + default 5 + help + Set the priority of the default TinyUSB main task. + + Default value: 5. + + config TINYUSB_TASK_STACK_SIZE + int "Stack size (bytes)" + default 4096 + help + Set the stack size of the default TinyUSB main task. + + Default value: 4096 bytes. + config TINYUSB_TASK_AFFINITY hex default FREERTOS_NO_AFFINITY if TINYUSB_TASK_AFFINITY_NO_AFFINITY default 0x0 if TINYUSB_TASK_AFFINITY_CPU0 default 0x1 if TINYUSB_TASK_AFFINITY_CPU1 - config TINYUSB_INIT_IN_DEFAULT_TASK - bool "Initialize TinyUSB stack within the default TinyUSB task" - default n - depends on !TINYUSB_NO_DEFAULT_TASK - help - Run TinyUSB stack initialization just after starting the default TinyUSB task. - This is especially useful in multicore scenarios, when we need to pin the task - to a specific core and, at the same time initialize TinyUSB stack - (i.e. install interrupts) on the same core. endmenu # "TinyUSB task configuration" menu "Descriptor configuration" diff --git a/device/esp_tinyusb/test_apps/default_task/sdkconfig.defaults b/device/esp_tinyusb/test_apps/default_task/sdkconfig.defaults index 79f0c5ff..d98be092 100644 --- a/device/esp_tinyusb/test_apps/default_task/sdkconfig.defaults +++ b/device/esp_tinyusb/test_apps/default_task/sdkconfig.defaults @@ -1,6 +1,6 @@ # Configure TinyUSB, it will be used to mock USB devices -CONFIG_TINYUSB_NO_DEFAULT_TASK=n -CONFIG_TINYUSB_INIT_IN_DEFAULT_TASK=n +CONFIG_TINYUSB_DEFAULT_TASK=y +CONFIG_TINYUSB_INIT_IN_TASK=n # Disable watchdogs, they'd get triggered during unity interactive menu CONFIG_ESP_INT_WDT=n diff --git a/device/esp_tinyusb/test_apps/default_task_with_init/sdkconfig.defaults b/device/esp_tinyusb/test_apps/default_task_with_init/sdkconfig.defaults index a3bf28c3..69b01a19 100644 --- a/device/esp_tinyusb/test_apps/default_task_with_init/sdkconfig.defaults +++ b/device/esp_tinyusb/test_apps/default_task_with_init/sdkconfig.defaults @@ -1,6 +1,6 @@ # Configure TinyUSB, it will be used to mock USB devices -CONFIG_TINYUSB_NO_DEFAULT_TASK=n -CONFIG_TINYUSB_INIT_IN_DEFAULT_TASK=y +CONFIG_TINYUSB_DEFAULT_TASK=y +CONFIG_TINYUSB_INIT_IN_TASK=y # Disable watchdogs, they'd get triggered during unity interactive menu CONFIG_ESP_INT_WDT=n diff --git a/device/esp_tinyusb/test_apps/external_task/sdkconfig.defaults b/device/esp_tinyusb/test_apps/external_task/sdkconfig.defaults index 73c1a82e..d98be092 100644 --- a/device/esp_tinyusb/test_apps/external_task/sdkconfig.defaults +++ b/device/esp_tinyusb/test_apps/external_task/sdkconfig.defaults @@ -1,6 +1,6 @@ # Configure TinyUSB, it will be used to mock USB devices -CONFIG_TINYUSB_NO_DEFAULT_TASK=y -CONFIG_TINYUSB_INIT_IN_DEFAULT_TASK=n +CONFIG_TINYUSB_DEFAULT_TASK=y +CONFIG_TINYUSB_INIT_IN_TASK=n # Disable watchdogs, they'd get triggered during unity interactive menu CONFIG_ESP_INT_WDT=n diff --git a/device/esp_tinyusb/tinyusb.c b/device/esp_tinyusb/tinyusb.c index e65b69b5..deb9633f 100644 --- a/device/esp_tinyusb/tinyusb.c +++ b/device/esp_tinyusb/tinyusb.c @@ -64,10 +64,10 @@ esp_err_t tinyusb_driver_install(const tinyusb_config_t *config) ESP_RETURN_ON_ERROR(tinyusb_set_descriptors(config), TAG, "Descriptors config failed"); // Init -#if !CONFIG_TINYUSB_INIT_IN_DEFAULT_TASK +#if !CONFIG_TINYUSB_INIT_IN_TASK ESP_RETURN_ON_FALSE(tusb_init(), ESP_FAIL, TAG, "Init TinyUSB stack failed"); #endif -#if !CONFIG_TINYUSB_NO_DEFAULT_TASK +#if CONFIG_TINYUSB_DEFAULT_TASK ESP_RETURN_ON_ERROR(tusb_run_task(), TAG, "Run TinyUSB task failed"); #endif ESP_LOGI(TAG, "TinyUSB Driver installed"); @@ -76,9 +76,9 @@ esp_err_t tinyusb_driver_install(const tinyusb_config_t *config) esp_err_t tinyusb_driver_uninstall(void) { -#if !CONFIG_TINYUSB_NO_DEFAULT_TASK +#if CONFIG_TINYUSB_DEFAULT_TASK ESP_RETURN_ON_ERROR(tusb_stop_task(), TAG, "Unable to stop TinyUSB task"); -#endif // !CONFIG_TINYUSB_NO_DEFAULT_TASK +#endif // CONFIG_TINYUSB_DEFAULT_TASK ESP_RETURN_ON_FALSE(tusb_teardown(), ESP_ERR_NOT_FINISHED, TAG, "Unable to teardown TinyUSB"); tinyusb_free_descriptors(); ESP_RETURN_ON_ERROR(usb_del_phy(phy_hdl), TAG, "Unable to delete PHY"); diff --git a/device/esp_tinyusb/tusb_tasks.c b/device/esp_tinyusb/tusb_tasks.c index cd4add4e..f9c89585 100644 --- a/device/esp_tinyusb/tusb_tasks.c +++ b/device/esp_tinyusb/tusb_tasks.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2020-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -16,10 +16,10 @@ const static char *TAG = "tusb_tsk"; static TaskHandle_t s_tusb_tskh; -#if CONFIG_TINYUSB_INIT_IN_DEFAULT_TASK +#if CONFIG_TINYUSB_INIT_IN_TASK const static int INIT_OK = BIT0; const static int INIT_FAILED = BIT1; -#endif +#endif // CONFIG_TINYUSB_INIT_IN_TASK /** * @brief This top level thread processes all usb events and invokes callbacks @@ -27,7 +27,7 @@ const static int INIT_FAILED = BIT1; static void tusb_device_task(void *arg) { ESP_LOGD(TAG, "tinyusb task started"); -#if CONFIG_TINYUSB_INIT_IN_DEFAULT_TASK +#if CONFIG_TINYUSB_INIT_IN_TASK EventGroupHandle_t *init_flags = arg; if (!tusb_init()) { ESP_LOGI(TAG, "Init TinyUSB stack failed"); @@ -36,7 +36,7 @@ static void tusb_device_task(void *arg) } ESP_LOGD(TAG, "tinyusb task has been initialized"); xEventGroupSetBits(*init_flags, INIT_OK); -#endif // CONFIG_TINYUSB_INIT_IN_DEFAULT_TASK +#endif // CONFIG_TINYUSB_INIT_IN_TASK while (1) { // RTOS forever loop tud_task(); } @@ -49,21 +49,21 @@ esp_err_t tusb_run_task(void) ESP_RETURN_ON_FALSE(!s_tusb_tskh, ESP_ERR_INVALID_STATE, TAG, "TinyUSB main task already started"); void *task_arg = NULL; -#if CONFIG_TINYUSB_INIT_IN_DEFAULT_TASK +#if CONFIG_TINYUSB_INIT_IN_TASK // need to synchronize to potentially report issue if init failed EventGroupHandle_t init_flags = xEventGroupCreate(); ESP_RETURN_ON_FALSE(init_flags, ESP_ERR_NO_MEM, TAG, "Failed to allocate task sync flags"); task_arg = &init_flags; -#endif +#endif // CONFIG_TINYUSB_INIT_IN_TASK // Create a task for tinyusb device stack: xTaskCreatePinnedToCore(tusb_device_task, "TinyUSB", CONFIG_TINYUSB_TASK_STACK_SIZE, task_arg, CONFIG_TINYUSB_TASK_PRIORITY, &s_tusb_tskh, CONFIG_TINYUSB_TASK_AFFINITY); ESP_RETURN_ON_FALSE(s_tusb_tskh, ESP_FAIL, TAG, "create TinyUSB main task failed"); -#if CONFIG_TINYUSB_INIT_IN_DEFAULT_TASK +#if CONFIG_TINYUSB_INIT_IN_TASK // wait until tusb initialization has completed EventBits_t bits = xEventGroupWaitBits(init_flags, INIT_OK | INIT_FAILED, pdFALSE, pdFALSE, portMAX_DELAY); vEventGroupDelete(init_flags); ESP_RETURN_ON_FALSE(bits & INIT_OK, ESP_FAIL, TAG, "Init TinyUSB stack failed"); -#endif +#endif // CONFIG_TINYUSB_INIT_IN_TASK return ESP_OK; }