diff --git a/device/esp_tinyusb/test_apps/runtime_config/main/test_multitask_access.c b/device/esp_tinyusb/test_apps/runtime_config/main/test_multitask_access.c index 9e34e1e70..544e92be8 100644 --- a/device/esp_tinyusb/test_apps/runtime_config/main/test_multitask_access.c +++ b/device/esp_tinyusb/test_apps/runtime_config/main/test_multitask_access.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2025-2026 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -19,6 +19,7 @@ #include "esp_log.h" #include "esp_err.h" #include "esp_private/usb_phy.h" +#include "esp_private/critical_section.h" // #include "unity.h" #include "tinyusb.h" @@ -33,7 +34,10 @@ static SemaphoreHandle_t sem_done = NULL; TaskHandle_t test_task_handles[MULTIPLE_THREADS_TASKS_NUM]; // Unlocked spinlock, ready to use -static portMUX_TYPE _spinlock = portMUX_INITIALIZER_UNLOCKED; +DEFINE_CRIT_SECTION_LOCK_STATIC(multitask_test_lock); +#define TINYUSB_TASK_ENTER_CRITICAL() esp_os_enter_critical(&multitask_test_lock) +#define TINYUSB_TASK_EXIT_CRITICAL() esp_os_exit_critical(&multitask_test_lock) + static volatile int nb_of_success = 0; static void test_task_install(void *arg) @@ -49,9 +53,9 @@ static void test_task_install(void *arg) if (tinyusb_driver_install(&tusb_cfg) == ESP_OK) { test_device_wait(); TEST_ASSERT_EQUAL_MESSAGE(ESP_OK, tinyusb_driver_uninstall(), "Unable to uninstall driver after install in worker"); - taskENTER_CRITICAL(&_spinlock); + TINYUSB_TASK_ENTER_CRITICAL(); nb_of_success++; - taskEXIT_CRITICAL(&_spinlock); + TINYUSB_TASK_EXIT_CRITICAL(); } // Notify the parent task that the task completed the job @@ -68,9 +72,9 @@ static void test_task_uninstall(void *arg) ulTaskNotifyTake(pdTRUE, portMAX_DELAY); if (tinyusb_driver_uninstall() == ESP_OK) { - taskENTER_CRITICAL(&_spinlock); + TINYUSB_TASK_ENTER_CRITICAL(); nb_of_success++; - taskEXIT_CRITICAL(&_spinlock); + TINYUSB_TASK_EXIT_CRITICAL(); } // Notify the parent task that the task completed the job diff --git a/device/esp_tinyusb/tinyusb_cdc_acm.c b/device/esp_tinyusb/tinyusb_cdc_acm.c index 5d930344b..e2540f7b4 100644 --- a/device/esp_tinyusb/tinyusb_cdc_acm.c +++ b/device/esp_tinyusb/tinyusb_cdc_acm.c @@ -10,6 +10,7 @@ #include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" +#include "esp_private/critical_section.h" #include "tusb.h" #include "tinyusb_cdc_acm.h" #include "cdc.h" @@ -24,9 +25,9 @@ #endif // CDC-ACM spinlock -static portMUX_TYPE cdc_acm_lock = portMUX_INITIALIZER_UNLOCKED; -#define CDC_ACM_ENTER_CRITICAL() portENTER_CRITICAL(&cdc_acm_lock) -#define CDC_ACM_EXIT_CRITICAL() portEXIT_CRITICAL(&cdc_acm_lock) +DEFINE_CRIT_SECTION_LOCK_STATIC(cdc_acm_lock); +#define CDC_ACM_ENTER_CRITICAL() esp_os_enter_critical(&cdc_acm_lock) +#define CDC_ACM_EXIT_CRITICAL() esp_os_exit_critical(&cdc_acm_lock) typedef struct { tusb_cdcacm_callback_t callback_rx; diff --git a/device/esp_tinyusb/tinyusb_msc.c b/device/esp_tinyusb/tinyusb_msc.c index a313b0085..4ff7590f6 100644 --- a/device/esp_tinyusb/tinyusb_msc.c +++ b/device/esp_tinyusb/tinyusb_msc.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2025-2026 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -11,6 +11,7 @@ #include "esp_vfs_fat.h" #include "esp_partition.h" #include "esp_memory_utils.h" +#include "esp_private/critical_section.h" #include "soc/soc_caps.h" #include "sdkconfig.h" #include "vfs_fat_internal.h" @@ -103,9 +104,9 @@ typedef struct { static tinyusb_msc_driver_t *p_msc_driver; -static portMUX_TYPE msc_lock = portMUX_INITIALIZER_UNLOCKED; -#define MSC_ENTER_CRITICAL() portENTER_CRITICAL(&msc_lock) -#define MSC_EXIT_CRITICAL() portEXIT_CRITICAL(&msc_lock) +DEFINE_CRIT_SECTION_LOCK_STATIC(msc_lock); +#define MSC_ENTER_CRITICAL() esp_os_enter_critical(&msc_lock) +#define MSC_EXIT_CRITICAL() esp_os_exit_critical(&msc_lock) #define MSC_GOTO_ON_FALSE_CRITICAL(cond, err) \ do { \ diff --git a/device/esp_tinyusb/tinyusb_task.c b/device/esp_tinyusb/tinyusb_task.c index c27f5a9ca..cf63b45a0 100644 --- a/device/esp_tinyusb/tinyusb_task.c +++ b/device/esp_tinyusb/tinyusb_task.c @@ -7,6 +7,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" +#include "esp_private/critical_section.h" #include "soc/soc_caps.h" #include "esp_log.h" #include "esp_check.h" @@ -20,9 +21,9 @@ const static char *TAG = "tinyusb_task"; -static portMUX_TYPE tusb_task_lock = portMUX_INITIALIZER_UNLOCKED; -#define TINYUSB_TASK_ENTER_CRITICAL() portENTER_CRITICAL(&tusb_task_lock) -#define TINYUSB_TASK_EXIT_CRITICAL() portEXIT_CRITICAL(&tusb_task_lock) +DEFINE_CRIT_SECTION_LOCK_STATIC(tusb_task_lock); +#define TINYUSB_TASK_ENTER_CRITICAL() esp_os_enter_critical(&tusb_task_lock) +#define TINYUSB_TASK_EXIT_CRITICAL() esp_os_exit_critical(&tusb_task_lock) #define TINYUSB_TASK_CHECK(cond, ret_val) ({ \ if (!(cond)) { \ diff --git a/host/class/cdc/usb_host_cdc_acm/cdc_acm_host.c b/host/class/cdc/usb_host_cdc_acm/cdc_acm_host.c index 782861672..6e42e8d97 100644 --- a/host/class/cdc/usb_host_cdc_acm/cdc_acm_host.c +++ b/host/class/cdc/usb_host_cdc_acm/cdc_acm_host.c @@ -12,6 +12,7 @@ #include "freertos/task.h" #include "freertos/semphr.h" #include "freertos/event_groups.h" +#include "esp_private/critical_section.h" #include "soc/soc_caps.h" #include "esp_log.h" #include "esp_check.h" @@ -35,9 +36,9 @@ static const char *TAG = "cdc_acm"; #endif // CONFIG_IDF_TARGET_LINUX // CDC-ACM spinlock -static portMUX_TYPE cdc_acm_lock = portMUX_INITIALIZER_UNLOCKED; -#define CDC_ACM_ENTER_CRITICAL() portENTER_CRITICAL(&cdc_acm_lock) -#define CDC_ACM_EXIT_CRITICAL() portEXIT_CRITICAL(&cdc_acm_lock) +DEFINE_CRIT_SECTION_LOCK_STATIC(cdc_acm_lock); +#define CDC_ACM_ENTER_CRITICAL() esp_os_enter_critical(&cdc_acm_lock) +#define CDC_ACM_EXIT_CRITICAL() esp_os_exit_critical(&cdc_acm_lock) // CDC-ACM events #define CDC_ACM_TEARDOWN BIT0 diff --git a/host/class/hid/usb_host_hid/hid_host.c b/host/class/hid/usb_host_hid/hid_host.c index 8355f75de..711a6602f 100644 --- a/host/class/hid/usb_host_hid/hid_host.c +++ b/host/class/hid/usb_host_hid/hid_host.c @@ -15,6 +15,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" +#include "esp_private/critical_section.h" #include "usb/usb_host.h" #include "usb/hid_host.h" @@ -25,9 +26,9 @@ #define HID_MAX_REPORT_DESC_LEN 2048u // HID spinlock -static portMUX_TYPE hid_lock = portMUX_INITIALIZER_UNLOCKED; -#define HID_ENTER_CRITICAL() portENTER_CRITICAL(&hid_lock) -#define HID_EXIT_CRITICAL() portEXIT_CRITICAL(&hid_lock) +DEFINE_CRIT_SECTION_LOCK_STATIC(hid_lock); +#define HID_ENTER_CRITICAL() esp_os_enter_critical(&hid_lock) +#define HID_EXIT_CRITICAL() esp_os_exit_critical(&hid_lock) // HID verification macros #define HID_GOTO_ON_FALSE_CRITICAL(exp, err) \ diff --git a/host/class/msc/usb_host_msc/src/msc_host.c b/host/class/msc/usb_host_msc/src/msc_host.c index 47429530e..d5ed033ae 100644 --- a/host/class/msc/usb_host_msc/src/msc_host.c +++ b/host/class/msc/usb_host_msc/src/msc_host.c @@ -16,6 +16,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" +#include "esp_private/critical_section.h" #include "usb/usb_host.h" #include "diskio_usb.h" #include "msc_common.h" @@ -29,9 +30,9 @@ #endif // MSC driver spin lock -static portMUX_TYPE msc_lock = portMUX_INITIALIZER_UNLOCKED; -#define MSC_ENTER_CRITICAL() portENTER_CRITICAL(&msc_lock) -#define MSC_EXIT_CRITICAL() portEXIT_CRITICAL(&msc_lock) +DEFINE_CRIT_SECTION_LOCK_STATIC(msc_lock); +#define MSC_ENTER_CRITICAL() esp_os_enter_critical(&msc_lock) +#define MSC_EXIT_CRITICAL() esp_os_exit_critical(&msc_lock) #define MSC_GOTO_ON_FALSE_CRITICAL(exp, err) \ do { \ diff --git a/host/class/uac/usb_host_uac/uac_host.c b/host/class/uac/usb_host_uac/uac_host.c index d1d7dee19..64803b4a4 100644 --- a/host/class/uac/usb_host_uac/uac_host.c +++ b/host/class/uac/usb_host_uac/uac_host.c @@ -23,14 +23,15 @@ #include "freertos/task.h" #include "freertos/semphr.h" #include "freertos/ringbuf.h" +#include "esp_private/critical_section.h" #include "usb/usb_host.h" #include "usb/uac_host.h" #include "usb/usb_types_ch9.h" // UAC spinlock -static portMUX_TYPE uac_lock = portMUX_INITIALIZER_UNLOCKED; -#define UAC_ENTER_CRITICAL() portENTER_CRITICAL(&uac_lock) -#define UAC_EXIT_CRITICAL() portEXIT_CRITICAL(&uac_lock) +DEFINE_CRIT_SECTION_LOCK_STATIC(uac_lock); +#define UAC_ENTER_CRITICAL() esp_os_enter_critical(&uac_lock) +#define UAC_EXIT_CRITICAL() esp_os_exit_critical(&uac_lock) // UAC verification macros #define UAC_GOTO_ON_FALSE_CRITICAL(exp, err) \ diff --git a/host/class/uvc/usb_host_uvc/private_include/uvc_critical_priv.h b/host/class/uvc/usb_host_uvc/private_include/uvc_critical_priv.h index 1769d6de8..7d0e6d70d 100644 --- a/host/class/uvc/usb_host_uvc/private_include/uvc_critical_priv.h +++ b/host/class/uvc/usb_host_uvc/private_include/uvc_critical_priv.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024-2026 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -11,10 +11,12 @@ #include "freertos/FreeRTOS.h" #include "freertos/portmacro.h" +// TODO: Fix extern uvc_lock by using dedicated macro IDF-13950 extern portMUX_TYPE uvc_lock; #define UVC_ENTER_CRITICAL() portENTER_CRITICAL(&uvc_lock) #define UVC_EXIT_CRITICAL() portEXIT_CRITICAL(&uvc_lock) + #define UVC_ATOMIC_LOAD(x) __atomic_load_n(&x, __ATOMIC_SEQ_CST) #define UVC_ATOMIC_SET_IF_NULL(x, new_x) ({ \ __typeof__(x) expected = NULL; \ diff --git a/type_c/usb_tcpm/src/usb_tcpm.c b/type_c/usb_tcpm/src/usb_tcpm.c index 86530100b..5e163622c 100644 --- a/type_c/usb_tcpm/src/usb_tcpm.c +++ b/type_c/usb_tcpm/src/usb_tcpm.c @@ -13,6 +13,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/semphr.h" #include "freertos/task.h" +#include "esp_private/critical_section.h" #include "usb/usb_tcpm.h" #include "usb_tcpm_backend.h" @@ -22,6 +23,13 @@ static const char *TAG = "usb_tcpm"; /* Define the event base */ ESP_EVENT_DEFINE_BASE(USB_TCPM_EVENT); +// tcpm spinlock +DEFINE_CRIT_SECTION_LOCK_STATIC(tcpm_lock); +#define TCPM_ENTER_CRITICAL_ISR() esp_os_enter_critical_isr(&tcpm_lock) +#define TCPM_EXIT_CRITICAL_ISR() esp_os_exit_critical_isr(&tcpm_lock) +#define TCPM_ENTER_CRITICAL(); esp_os_enter_critical(&tcpm_lock) +#define TCPM_EXIT_CRITICAL(); esp_os_exit_critical(&tcpm_lock) + /* ---------- Per-port context ---------- */ /** * @brief Per-port runtime context. @@ -70,7 +78,6 @@ typedef struct typec_port { */ #define USB_TCPM_SRC_DETACH_GRACE_MS (200) -static portMUX_TYPE s_lock = portMUX_INITIALIZER_UNLOCKED; static typec_port_t *s_ports; static TaskHandle_t s_task; @@ -78,15 +85,15 @@ static void usb_tcpm_task(void *arg); static void usb_tcpm_port_list_add(typec_port_t *ctx) { - portENTER_CRITICAL(&s_lock); + TCPM_ENTER_CRITICAL(); ctx->dynamic.next = s_ports; s_ports = ctx; - portEXIT_CRITICAL(&s_lock); + TCPM_EXIT_CRITICAL(); } static void usb_tcpm_port_list_remove(typec_port_t *ctx) { - portENTER_CRITICAL(&s_lock); + TCPM_ENTER_CRITICAL(); typec_port_t **port_cursor = &s_ports; while (*port_cursor && *port_cursor != ctx) { port_cursor = &(*port_cursor)->dynamic.next; @@ -95,7 +102,7 @@ static void usb_tcpm_port_list_remove(typec_port_t *ctx) *port_cursor = ctx->dynamic.next; } ctx->dynamic.next = NULL; - portEXIT_CRITICAL(&s_lock); + TCPM_EXIT_CRITICAL(); } #define USB_TCPM_STATUS_LOCK(ctx) do { \ @@ -462,12 +469,12 @@ static void IRAM_ATTR usb_tcpm_gpio_isr(void *arg) BaseType_t higher_priority_task_woken = pdFALSE; TaskHandle_t task = NULL; - portENTER_CRITICAL_ISR(&s_lock); + TCPM_ENTER_CRITICAL_ISR(); if (!ctx->dynamic.deleting) { ctx->dynamic.irq_pending = true; task = s_task; } - portEXIT_CRITICAL_ISR(&s_lock); + TCPM_EXIT_CRITICAL_ISR(); if (task) { vTaskNotifyGiveFromISR(task, &higher_priority_task_woken); @@ -488,7 +495,7 @@ static void usb_tcpm_task(void *arg) for (;;) { typec_port_t *ctx = NULL; - portENTER_CRITICAL(&s_lock); + TCPM_ENTER_CRITICAL(); for (typec_port_t *it = s_ports; it; it = it->dynamic.next) { if (it->dynamic.irq_pending && !it->dynamic.irq_processing && !it->dynamic.deleting) { it->dynamic.irq_pending = false; @@ -497,7 +504,7 @@ static void usb_tcpm_task(void *arg) break; } } - portEXIT_CRITICAL(&s_lock); + TCPM_EXIT_CRITICAL(); if (!ctx) { break; @@ -527,11 +534,11 @@ static void usb_tcpm_task(void *arg) bool deleting = false; gpio_num_t gpio_int = GPIO_NUM_NC; - portENTER_CRITICAL(&s_lock); + TCPM_ENTER_CRITICAL(); deleting = ctx->dynamic.deleting; gpio_int = ctx->constant.gpio_int; ctx->dynamic.irq_processing = false; - portEXIT_CRITICAL(&s_lock); + TCPM_EXIT_CRITICAL(); if (!deleting) { gpio_intr_enable(gpio_int); @@ -550,9 +557,9 @@ esp_err_t usb_tcpm_install(const usb_tcpm_install_config_t *config) ESP_RETURN_ON_ERROR(err, TAG, "Failed to create default event loop: %s", esp_err_to_name(err)); } - portENTER_CRITICAL(&s_lock); + TCPM_ENTER_CRITICAL(); const bool already_installed = (s_task != NULL); - portEXIT_CRITICAL(&s_lock); + TCPM_EXIT_CRITICAL(); if (already_installed) { return ESP_OK; } @@ -573,12 +580,12 @@ esp_err_t usb_tcpm_install(const usb_tcpm_install_config_t *config) return ESP_ERR_NO_MEM; } - portENTER_CRITICAL(&s_lock); + TCPM_ENTER_CRITICAL(); if (!s_task) { s_task = task; task = NULL; } - portEXIT_CRITICAL(&s_lock); + TCPM_EXIT_CRITICAL(); if (task) { vTaskDelete(task); } @@ -588,15 +595,15 @@ esp_err_t usb_tcpm_install(const usb_tcpm_install_config_t *config) esp_err_t usb_tcpm_uninstall(void) { - portENTER_CRITICAL(&s_lock); + TCPM_ENTER_CRITICAL(); if (s_ports != NULL) { - portEXIT_CRITICAL(&s_lock); + TCPM_EXIT_CRITICAL(); ESP_LOGE(TAG, "Cannot uninstall while ports are active"); return ESP_ERR_INVALID_STATE; } const TaskHandle_t task = s_task; s_task = NULL; - portEXIT_CRITICAL(&s_lock); + TCPM_EXIT_CRITICAL(); if (task) { vTaskDelete(task); @@ -613,9 +620,9 @@ esp_err_t typec_port_new(const typec_port_backend_t *backend, typec_port_t *ctx = NULL; ESP_RETURN_ON_FALSE(backend && backend_ctx && port_cfg && out, ESP_ERR_INVALID_ARG, TAG, "bad args"); - portENTER_CRITICAL(&s_lock); + TCPM_ENTER_CRITICAL(); const bool installed = (s_task != NULL); - portEXIT_CRITICAL(&s_lock); + TCPM_EXIT_CRITICAL(); if (!installed) { ret = ESP_ERR_INVALID_STATE; goto fail; @@ -748,10 +755,10 @@ esp_err_t usb_tcpm_port_destroy(usb_tcpm_port_handle_t port_hdl) return ESP_ERR_INVALID_ARG; } - portENTER_CRITICAL(&s_lock); + TCPM_ENTER_CRITICAL(); ctx->dynamic.deleting = true; ctx->dynamic.irq_pending = false; - portEXIT_CRITICAL(&s_lock); + TCPM_EXIT_CRITICAL(); gpio_intr_disable(ctx->constant.gpio_int); gpio_isr_handler_remove(ctx->constant.gpio_int); @@ -760,9 +767,9 @@ esp_err_t usb_tcpm_port_destroy(usb_tcpm_port_handle_t port_hdl) for (;;) { bool busy = false; - portENTER_CRITICAL(&s_lock); + TCPM_ENTER_CRITICAL(); busy = ctx->dynamic.irq_processing; - portEXIT_CRITICAL(&s_lock); + TCPM_EXIT_CRITICAL(); if (!busy) { break; }