From d19666d8e5c32232b828c1befb61d99cd798384e Mon Sep 17 00:00:00 2001 From: Peter Marcisovsky Date: Mon, 7 Oct 2024 13:51:09 +0200 Subject: [PATCH] refactor(usb_host): Update the USB component to new critical section API --- components/usb/ext_hub.c | 13 ++++++------- components/usb/hcd_dwc.c | 11 ++++++----- components/usb/hub.c | 17 ++++++++--------- components/usb/usb_host.c | 16 ++++++++-------- components/usb/usb_phy.c | 31 +++++++++++++++++-------------- components/usb/usbh.c | 18 ++++++++---------- 6 files changed, 53 insertions(+), 53 deletions(-) diff --git a/components/usb/ext_hub.c b/components/usb/ext_hub.c index 149993070c..1c821677f4 100644 --- a/components/usb/ext_hub.c +++ b/components/usb/ext_hub.c @@ -9,8 +9,7 @@ #include "esp_err.h" #include "esp_log.h" #include "esp_heap_caps.h" -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" +#include "esp_private/critical_section.h" #include "usb_private.h" #include "ext_hub.h" #include "ext_port.h" @@ -152,7 +151,6 @@ typedef struct { } ext_hub_driver_t; static ext_hub_driver_t *p_ext_hub_driver = NULL; -static portMUX_TYPE ext_hub_driver_lock = portMUX_INITIALIZER_UNLOCKED; const char *EXT_HUB_TAG = "EXT_HUB"; @@ -160,10 +158,11 @@ const char *EXT_HUB_TAG = "EXT_HUB"; // ------------------------------- Helpers ------------------------------------- // ----------------------------------------------------------------------------- -#define EXT_HUB_ENTER_CRITICAL() portENTER_CRITICAL(&ext_hub_driver_lock) -#define EXT_HUB_EXIT_CRITICAL() portEXIT_CRITICAL(&ext_hub_driver_lock) -#define EXT_HUB_ENTER_CRITICAL_SAFE() portENTER_CRITICAL_SAFE(&ext_hub_driver_lock) -#define EXT_HUB_EXIT_CRITICAL_SAFE() portEXIT_CRITICAL_SAFE(&ext_hub_driver_lock) +DEFINE_CRIT_SECTION_LOCK_STATIC(ext_hub_driver_lock); +#define EXT_HUB_ENTER_CRITICAL() esp_os_enter_critical(&ext_hub_driver_lock) +#define EXT_HUB_EXIT_CRITICAL() esp_os_exit_critical(&ext_hub_driver_lock) +#define EXT_HUB_ENTER_CRITICAL_SAFE() esp_os_enter_critical_safe(&ext_hub_driver_lock) +#define EXT_HUB_EXIT_CRITICAL_SAFE() esp_os_exit_critical_safe(&ext_hub_driver_lock) #define EXT_HUB_CHECK(cond, ret_val) ({ \ if (!(cond)) { \ diff --git a/components/usb/hcd_dwc.c b/components/usb/hcd_dwc.c index a5ea7d736f..76f031ae21 100644 --- a/components/usb/hcd_dwc.c +++ b/components/usb/hcd_dwc.c @@ -10,6 +10,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" +#include "esp_private/critical_section.h" #include "esp_heap_caps.h" #include "esp_intr_alloc.h" #include "soc/interrupts.h" // For interrupt index @@ -84,10 +85,11 @@ const char *HCD_DWC_TAG = "HCD DWC"; -#define HCD_ENTER_CRITICAL_ISR() portENTER_CRITICAL_ISR(&hcd_lock) -#define HCD_EXIT_CRITICAL_ISR() portEXIT_CRITICAL_ISR(&hcd_lock) -#define HCD_ENTER_CRITICAL() portENTER_CRITICAL(&hcd_lock) -#define HCD_EXIT_CRITICAL() portEXIT_CRITICAL(&hcd_lock) +DEFINE_CRIT_SECTION_LOCK_STATIC(hcd_lock); +#define HCD_ENTER_CRITICAL_ISR() esp_os_enter_critical_isr(&hcd_lock) +#define HCD_EXIT_CRITICAL_ISR() esp_os_exit_critical_isr(&hcd_lock) +#define HCD_ENTER_CRITICAL() esp_os_enter_critical(&hcd_lock) +#define HCD_EXIT_CRITICAL() esp_os_exit_critical(&hcd_lock) #define HCD_CHECK(cond, ret_val) ({ \ if (!(cond)) { \ @@ -270,7 +272,6 @@ typedef struct { intr_handle_t isr_hdl; } hcd_obj_t; -static portMUX_TYPE hcd_lock = portMUX_INITIALIZER_UNLOCKED; static hcd_obj_t *s_hcd_obj = NULL; // Note: "s_" is for the static pointer // ------------------------------------------------- Forward Declare --------------------------------------------------- diff --git a/components/usb/hub.c b/components/usb/hub.c index 079a18f351..2d328bcf39 100644 --- a/components/usb/hub.c +++ b/components/usb/hub.c @@ -9,10 +9,9 @@ #include #include #include -#include "freertos/FreeRTOS.h" -#include "freertos/portmacro.h" #include "esp_err.h" #include "esp_heap_caps.h" +#include "esp_private/critical_section.h" #include "esp_log.h" #include "usb_private.h" #include "hcd.h" @@ -107,16 +106,16 @@ typedef struct { } hub_driver_t; static hub_driver_t *p_hub_driver_obj = NULL; -static portMUX_TYPE hub_driver_lock = portMUX_INITIALIZER_UNLOCKED; const char *HUB_DRIVER_TAG = "HUB"; -#define HUB_DRIVER_ENTER_CRITICAL_ISR() portENTER_CRITICAL_ISR(&hub_driver_lock) -#define HUB_DRIVER_EXIT_CRITICAL_ISR() portEXIT_CRITICAL_ISR(&hub_driver_lock) -#define HUB_DRIVER_ENTER_CRITICAL() portENTER_CRITICAL(&hub_driver_lock) -#define HUB_DRIVER_EXIT_CRITICAL() portEXIT_CRITICAL(&hub_driver_lock) -#define HUB_DRIVER_ENTER_CRITICAL_SAFE() portENTER_CRITICAL_SAFE(&hub_driver_lock) -#define HUB_DRIVER_EXIT_CRITICAL_SAFE() portEXIT_CRITICAL_SAFE(&hub_driver_lock) +DEFINE_CRIT_SECTION_LOCK_STATIC(hub_driver_lock); +#define HUB_DRIVER_ENTER_CRITICAL_ISR() esp_os_enter_critical_isr(&hub_driver_lock) +#define HUB_DRIVER_EXIT_CRITICAL_ISR() esp_os_exit_critical_isr(&hub_driver_lock) +#define HUB_DRIVER_ENTER_CRITICAL() esp_os_enter_critical(&hub_driver_lock) +#define HUB_DRIVER_EXIT_CRITICAL() esp_os_exit_critical(&hub_driver_lock) +#define HUB_DRIVER_ENTER_CRITICAL_SAFE() esp_os_enter_critical_safe(&hub_driver_lock) +#define HUB_DRIVER_EXIT_CRITICAL_SAFE() esp_os_exit_critical_safe(&hub_driver_lock) #define HUB_DRIVER_CHECK(cond, ret_val) ({ \ if (!(cond)) { \ diff --git a/components/usb/usb_host.c b/components/usb/usb_host.c index 644a5e777e..bc41bab908 100644 --- a/components/usb/usb_host.c +++ b/components/usb/usb_host.c @@ -16,6 +16,7 @@ Warning: The USB Host Library API is still a beta version and may be subject to #include "freertos/task.h" #include "freertos/queue.h" #include "freertos/semphr.h" +#include "esp_private/critical_section.h" #include "esp_err.h" #include "esp_log.h" #include "esp_heap_caps.h" @@ -26,14 +27,13 @@ Warning: The USB Host Library API is still a beta version and may be subject to #include "esp_private/usb_phy.h" #include "usb/usb_host.h" -static portMUX_TYPE host_lock = portMUX_INITIALIZER_UNLOCKED; - -#define HOST_ENTER_CRITICAL_ISR() portENTER_CRITICAL_ISR(&host_lock) -#define HOST_EXIT_CRITICAL_ISR() portEXIT_CRITICAL_ISR(&host_lock) -#define HOST_ENTER_CRITICAL() portENTER_CRITICAL(&host_lock) -#define HOST_EXIT_CRITICAL() portEXIT_CRITICAL(&host_lock) -#define HOST_ENTER_CRITICAL_SAFE() portENTER_CRITICAL_SAFE(&host_lock) -#define HOST_EXIT_CRITICAL_SAFE() portEXIT_CRITICAL_SAFE(&host_lock) +DEFINE_CRIT_SECTION_LOCK_STATIC(host_lock); +#define HOST_ENTER_CRITICAL_ISR() esp_os_enter_critical_isr(&host_lock) +#define HOST_EXIT_CRITICAL_ISR() esp_os_exit_critical_isr(&host_lock) +#define HOST_ENTER_CRITICAL() esp_os_enter_critical(&host_lock) +#define HOST_EXIT_CRITICAL() esp_os_exit_critical(&host_lock) +#define HOST_ENTER_CRITICAL_SAFE() esp_os_enter_critical_safe(&host_lock) +#define HOST_EXIT_CRITICAL_SAFE() esp_os_exit_critical_safe(&host_lock) #define HOST_CHECK(cond, ret_val) ({ \ if (!(cond)) { \ diff --git a/components/usb/usb_phy.c b/components/usb/usb_phy.c index 7ae5e5f382..220e631732 100644 --- a/components/usb/usb_phy.c +++ b/components/usb/usb_phy.c @@ -6,11 +6,11 @@ #include #include -#include "freertos/FreeRTOS.h" #include "esp_log.h" #include "esp_check.h" #include "esp_private/periph_ctrl.h" #include "esp_private/usb_phy.h" +#include "esp_private/critical_section.h" #include "soc/usb_dwc_periph.h" #include "hal/usb_wrap_hal.h" #include "hal/usb_serial_jtag_hal.h" @@ -61,7 +61,10 @@ typedef struct { } usb_iopin_dsc_t; static phy_ctrl_obj_t *p_phy_ctrl_obj = NULL; -static portMUX_TYPE phy_spinlock = portMUX_INITIALIZER_UNLOCKED; + +DEFINE_CRIT_SECTION_LOCK_STATIC(phy_spinlock); +#define PHY_ENTER_CRITICAL() esp_os_enter_critical(&phy_spinlock) +#define PHY_EXIT_CRITICAL() esp_os_exit_critical(&phy_spinlock) static esp_err_t phy_iopins_configure(const usb_iopin_dsc_t *usb_periph_iopins, int iopins_num) { @@ -235,29 +238,29 @@ esp_err_t usb_phy_action(usb_phy_handle_t handle, usb_phy_action_t action) static esp_err_t usb_phy_install(void) { - portENTER_CRITICAL(&phy_spinlock); + PHY_ENTER_CRITICAL(); if (p_phy_ctrl_obj) { // p_phy_ctrl_obj already installed, return immediately - portEXIT_CRITICAL(&phy_spinlock); + PHY_EXIT_CRITICAL(); return ESP_OK; } - portEXIT_CRITICAL(&phy_spinlock); + PHY_EXIT_CRITICAL(); esp_err_t ret = ESP_OK; phy_ctrl_obj_t *phy_ctrl_obj = (phy_ctrl_obj_t *) calloc(1, sizeof(phy_ctrl_obj_t)); ESP_GOTO_ON_FALSE(phy_ctrl_obj, ESP_ERR_NO_MEM, cleanup, USBPHY_TAG, "no mem for USB_PHY driver"); - portENTER_CRITICAL(&phy_spinlock); + PHY_ENTER_CRITICAL(); if (!p_phy_ctrl_obj) { p_phy_ctrl_obj = phy_ctrl_obj; p_phy_ctrl_obj->ref_count = 0; } else { // p_phy_ctrl_obj already installed, need to free resource - portEXIT_CRITICAL(&phy_spinlock); + PHY_EXIT_CRITICAL(); goto cleanup; } // Enable USB peripheral and reset the register - portEXIT_CRITICAL(&phy_spinlock); + PHY_EXIT_CRITICAL(); USB_WRAP_RCC_ATOMIC() { usb_wrap_ll_enable_bus_clock(true); usb_wrap_ll_reset_register(); @@ -281,7 +284,7 @@ esp_err_t usb_new_phy(const usb_phy_config_t *config, usb_phy_handle_t *handle_r phy_context_t *phy_context = (phy_context_t *) calloc(1, sizeof(phy_context_t)); ESP_GOTO_ON_FALSE(phy_context, ESP_ERR_NO_MEM, cleanup, USBPHY_TAG, "no mem for phy context"); - portENTER_CRITICAL(&phy_spinlock); + PHY_ENTER_CRITICAL(); usb_phy_get_phy_status(config->target, &phy_context->status); if (phy_context->status == USB_PHY_STATUS_FREE) { new_phy = true; @@ -292,7 +295,7 @@ esp_err_t usb_new_phy(const usb_phy_config_t *config, usb_phy_handle_t *handle_r p_phy_ctrl_obj->internal_phy = phy_context; } } - portEXIT_CRITICAL(&phy_spinlock); + PHY_EXIT_CRITICAL(); ESP_GOTO_ON_FALSE(new_phy, ESP_ERR_INVALID_STATE, cleanup, USBPHY_TAG, "selected PHY is in use"); phy_context->target = config->target; @@ -354,7 +357,7 @@ cleanup: static void phy_uninstall(void) { phy_ctrl_obj_t *p_phy_ctrl_obj_free = NULL; - portENTER_CRITICAL(&phy_spinlock); + PHY_ENTER_CRITICAL(); if (p_phy_ctrl_obj->ref_count == 0) { p_phy_ctrl_obj_free = p_phy_ctrl_obj; p_phy_ctrl_obj = NULL; @@ -363,7 +366,7 @@ static void phy_uninstall(void) usb_wrap_ll_enable_bus_clock(false); } } - portEXIT_CRITICAL(&phy_spinlock); + PHY_EXIT_CRITICAL(); free(p_phy_ctrl_obj_free); } @@ -371,7 +374,7 @@ esp_err_t usb_del_phy(usb_phy_handle_t handle) { ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_ARG, USBPHY_TAG, "handle argument is invalid"); - portENTER_CRITICAL(&phy_spinlock); + PHY_ENTER_CRITICAL(); p_phy_ctrl_obj->ref_count--; if (handle->target == USB_PHY_TARGET_EXT) { p_phy_ctrl_obj->external_phy = NULL; @@ -380,7 +383,7 @@ esp_err_t usb_del_phy(usb_phy_handle_t handle) usb_wrap_hal_phy_disable_pull_override(&handle->wrap_hal); p_phy_ctrl_obj->internal_phy = NULL; } - portEXIT_CRITICAL(&phy_spinlock); + PHY_EXIT_CRITICAL(); free(handle->iopins); free(handle); phy_uninstall(); diff --git a/components/usb/usbh.c b/components/usb/usbh.c index c515fc01ca..54355ace68 100644 --- a/components/usb/usbh.c +++ b/components/usb/usbh.c @@ -10,9 +10,8 @@ #include #include #include "freertos/FreeRTOS.h" -#include "freertos/portmacro.h" -#include "freertos/task.h" #include "freertos/semphr.h" +#include "esp_private/critical_section.h" #include "esp_err.h" #include "esp_log.h" #include "esp_heap_caps.h" @@ -117,16 +116,15 @@ typedef struct { static usbh_t *p_usbh_obj = NULL; -static portMUX_TYPE usbh_lock = portMUX_INITIALIZER_UNLOCKED; - const char *USBH_TAG = "USBH"; -#define USBH_ENTER_CRITICAL_ISR() portENTER_CRITICAL_ISR(&usbh_lock) -#define USBH_EXIT_CRITICAL_ISR() portEXIT_CRITICAL_ISR(&usbh_lock) -#define USBH_ENTER_CRITICAL() portENTER_CRITICAL(&usbh_lock) -#define USBH_EXIT_CRITICAL() portEXIT_CRITICAL(&usbh_lock) -#define USBH_ENTER_CRITICAL_SAFE() portENTER_CRITICAL_SAFE(&usbh_lock) -#define USBH_EXIT_CRITICAL_SAFE() portEXIT_CRITICAL_SAFE(&usbh_lock) +DEFINE_CRIT_SECTION_LOCK_STATIC(usbh_lock); +#define USBH_ENTER_CRITICAL_ISR() esp_os_enter_critical_isr(&usbh_lock) +#define USBH_EXIT_CRITICAL_ISR() esp_os_exit_critical_isr(&usbh_lock) +#define USBH_ENTER_CRITICAL() esp_os_enter_critical(&usbh_lock) +#define USBH_EXIT_CRITICAL() esp_os_exit_critical(&usbh_lock) +#define USBH_ENTER_CRITICAL_SAFE() esp_os_enter_critical_safe(&usbh_lock) +#define USBH_EXIT_CRITICAL_SAFE() esp_os_exit_critical_safe(&usbh_lock) #define USBH_CHECK(cond, ret_val) ({ \ if (!(cond)) { \