Skip to content

Commit

Permalink
refactor(usb): Change critical section API
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-marcisovsky committed Oct 7, 2024
1 parent 9be78b9 commit e8f4b39
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 19 deletions.
9 changes: 5 additions & 4 deletions device/esp_tinyusb/tusb_cdc_acm.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -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 "tusb_cdc_acm.h"
#include "cdc.h"
Expand All @@ -20,9 +21,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;
Expand Down
7 changes: 4 additions & 3 deletions host/class/cdc/usb_host_cdc_acm/cdc_acm_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/event_groups.h"
#include "esp_private/critical_section.h"
#include "esp_log.h"
#include "esp_check.h"
#include "esp_system.h"
Expand All @@ -27,9 +28,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
Expand Down
7 changes: 4 additions & 3 deletions host/class/hid/usb_host_hid/hid_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down
7 changes: 4 additions & 3 deletions host/class/msc/usb_host_msc/src/msc_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 { \
Expand Down
7 changes: 4 additions & 3 deletions host/class/uac/usb_host_uac/uac_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,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) \
Expand Down
8 changes: 5 additions & 3 deletions host/class/uvc/usb_host_uvc/src/libusb_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_private/critical_section.h"
#include "esp_log.h"
#include "esp_check.h"
#include "usb/usb_host.h"
Expand Down Expand Up @@ -42,8 +43,10 @@
} \
} while(0)

#define UVC_ENTER_CRITICAL() portENTER_CRITICAL(&s_uvc_lock)
#define UVC_EXIT_CRITICAL() portEXIT_CRITICAL(&s_uvc_lock)
// UVC spinlock
DEFINE_CRIT_SECTION_LOCK_STATIC(uvc_lock);
#define UVC_ENTER_CRITICAL() esp_os_enter_critical(&uvc_lock)
#define UVC_EXIT_CRITICAL() esp_os_exit_critical(&uvc_lock)

#define COUNT_OF(array) (sizeof(array) / sizeof(array[0]))

Expand Down Expand Up @@ -71,7 +74,6 @@ typedef struct {
STAILQ_HEAD(opened_devs, opened_camera) opened_devices_tailq;
} uvc_driver_t;

static portMUX_TYPE s_uvc_lock = portMUX_INITIALIZER_UNLOCKED;
static uvc_driver_t *s_uvc_driver;

static libuvc_adapter_config_t s_config = {
Expand Down

0 comments on commit e8f4b39

Please sign in to comment.