From 206bd9a2a50b939cc4021e3d06407925c2b32a4d Mon Sep 17 00:00:00 2001 From: "peter.marcisovsky" Date: Tue, 20 May 2025 14:14:08 +0200 Subject: [PATCH] refactor(usb_host): Change critical section API --- device/esp_tinyusb/tinyusb_cdc_acm.c | 7 ++++--- device/esp_tinyusb/tinyusb_task.c | 7 ++++--- host/class/cdc/usb_host_cdc_acm/cdc_acm_host.c | 7 ++++--- host/class/hid/usb_host_hid/hid_host.c | 7 ++++--- host/class/msc/usb_host_msc/src/msc_host.c | 9 +++++---- host/class/uac/usb_host_uac/uac_host.c | 7 ++++--- .../usb_host_uvc/private_include/uvc_critical_priv.h | 12 +++++++----- host/class/uvc/usb_host_uvc/uvc_host.c | 3 ++- 8 files changed, 34 insertions(+), 25 deletions(-) diff --git a/device/esp_tinyusb/tinyusb_cdc_acm.c b/device/esp_tinyusb/tinyusb_cdc_acm.c index 5d930344..e2540f7b 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_task.c b/device/esp_tinyusb/tinyusb_task.c index 03b538f0..c08eebb4 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" @@ -16,9 +17,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 fe5ea126..85ee484d 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" @@ -30,9 +31,9 @@ static const char *TAG = "cdc_acm"; #define CDC_ACM_CTRL_TIMEOUT_MS (5000) // Every CDC device should be able to respond to CTRL transfer in 5 seconds // 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 6f4f8b32..b8c148ac 100644 --- a/host/class/hid/usb_host_hid/hid_host.c +++ b/host/class/hid/usb_host_hid/hid_host.c @@ -15,14 +15,15 @@ #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" // 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 0e49408e..e44e1625 100644 --- a/host/class/msc/usb_host_msc/src/msc_host.c +++ b/host/class/msc/usb_host_msc/src/msc_host.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -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" @@ -26,9 +27,9 @@ #include "soc/soc_memory_layout.h" // 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 34f68545..f3986f30 100644 --- a/host/class/uac/usb_host_uac/uac_host.c +++ b/host/class/uac/usb_host_uac/uac_host.c @@ -20,14 +20,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 1769d6de..5ffb4bdc 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-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -9,11 +9,13 @@ #include #include "freertos/FreeRTOS.h" -#include "freertos/portmacro.h" +#include "esp_private/critical_section.h" -extern portMUX_TYPE uvc_lock; -#define UVC_ENTER_CRITICAL() portENTER_CRITICAL(&uvc_lock) -#define UVC_EXIT_CRITICAL() portEXIT_CRITICAL(&uvc_lock) +// TODO: Nice to have dedicated critical section macro for declaring extern lock, eg: DECLARE_EXT_CRIT_SECTION_LOCK +extern DECLARE_CRIT_SECTION_LOCK_IN_STRUCT(uvc_lock); + +#define UVC_ENTER_CRITICAL() esp_os_enter_critical(&uvc_lock) +#define UVC_EXIT_CRITICAL() esp_os_exit_critical(&uvc_lock) #define UVC_ATOMIC_LOAD(x) __atomic_load_n(&x, __ATOMIC_SEQ_CST) #define UVC_ATOMIC_SET_IF_NULL(x, new_x) ({ \ diff --git a/host/class/uvc/usb_host_uvc/uvc_host.c b/host/class/uvc/usb_host_uvc/uvc_host.c index a20fc7f0..d6b7426f 100644 --- a/host/class/uvc/usb_host_uvc/uvc_host.c +++ b/host/class/uvc/usb_host_uvc/uvc_host.c @@ -31,11 +31,12 @@ #include "freertos/semphr.h" #include "freertos/queue.h" #include "freertos/event_groups.h" +#include "esp_private/critical_section.h" static const char *TAG = "uvc"; // UVC spinlock -portMUX_TYPE uvc_lock = portMUX_INITIALIZER_UNLOCKED; +DEFINE_CRIT_SECTION_LOCK(uvc_lock); // UVC driver status #define UVC_STARTED BIT0 // UVC driver events handling started