Skip to content

Commit

Permalink
refactor(esp_tinyusb): Changed default task configuration name to non…
Browse files Browse the repository at this point in the history
…-inverted logic
  • Loading branch information
roma-jam committed Jan 15, 2025
1 parent ac9b94c commit 5c8b9b9
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 53 deletions.
4 changes: 2 additions & 2 deletions device/esp_tinyusb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
77 changes: 45 additions & 32 deletions device/esp_tinyusb/Kconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
menu "TinyUSB Stack"
menu "ESP TinyUSB"
config TINYUSB_DEBUG_LEVEL
int "TinyUSB log level (0-3)"
default 1
Expand Down Expand Up @@ -48,61 +48,74 @@ 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
bool "CPU1"
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"
Expand Down
4 changes: 2 additions & 2 deletions device/esp_tinyusb/test_apps/default_task/sdkconfig.defaults
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions device/esp_tinyusb/test_apps/external_task/sdkconfig.defaults
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions device/esp_tinyusb/tinyusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand Down
18 changes: 9 additions & 9 deletions device/esp_tinyusb/tusb_tasks.c
Original file line number Diff line number Diff line change
@@ -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
*/
Expand All @@ -16,18 +16,18 @@
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
*/
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");
Expand All @@ -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();
}
Expand All @@ -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;
}
Expand Down

0 comments on commit 5c8b9b9

Please sign in to comment.