rename osal_critcal to osal_spinlock
add spinlock implementation for most rtos
This commit is contained in:
@@ -75,6 +75,10 @@ typedef void (*osal_task_func_t)( void * );
|
|||||||
// OSAL Porting API
|
// OSAL Porting API
|
||||||
// Should be implemented as static inline function in osal_port.h header
|
// Should be implemented as static inline function in osal_port.h header
|
||||||
/*
|
/*
|
||||||
|
void osal_spin_init(osal_spinlock_t *ctx);
|
||||||
|
void osal_spin_lock(osal_spinlock_t *ctx, bool in_isr)
|
||||||
|
void osal_spin_unlock(osal_spinlock_t *ctx, bool in_isr);
|
||||||
|
|
||||||
osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef);
|
osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef);
|
||||||
bool osal_semaphore_delete(osal_semaphore_t semd_hdl);
|
bool osal_semaphore_delete(osal_semaphore_t semd_hdl);
|
||||||
bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr);
|
bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr);
|
||||||
|
@@ -100,38 +100,38 @@ TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// Critical API
|
// Spinlock API
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
#define OSAL_CRITIAL_DEF(_name, _int_set) \
|
#define OSAL_SPINLOCK_DEF(_name, _int_set) \
|
||||||
osal_critical_t _name
|
osal_spinlock_t _name
|
||||||
|
|
||||||
#if TUSB_MCU_VENDOR_ESPRESSIF
|
#if TUSB_MCU_VENDOR_ESPRESSIF
|
||||||
// Espressif critical take spinlock as argument and does not use in_isr
|
// Espressif critical take spinlock as argument and does not use in_isr
|
||||||
typedef portMUX_TYPE osal_critical_t;
|
typedef portMUX_TYPE osal_spinlock_t;
|
||||||
|
|
||||||
TU_ATTR_ALWAYS_INLINE static inline void osal_critical_init(osal_critical_t *ctx) {
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_init(osal_spinlock_t *ctx) {
|
||||||
spinlock_initialize(ctx);
|
spinlock_initialize(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
TU_ATTR_ALWAYS_INLINE static inline void osal_critical_enter(osal_critical_t *ctx, bool in_isr) {
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_lock(osal_spinlock_t *ctx, bool in_isr) {
|
||||||
(void) in_isr;
|
(void) in_isr;
|
||||||
portENTER_CRITICAL(ctx);
|
portENTER_CRITICAL(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
TU_ATTR_ALWAYS_INLINE static inline void osal_critical_exit(osal_critical_t *ctx, bool in_isr) {
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_unlock(osal_spinlock_t *ctx, bool in_isr) {
|
||||||
(void) in_isr;
|
(void) in_isr;
|
||||||
portEXIT_CRITICAL(ctx);
|
portEXIT_CRITICAL(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
typedef UBaseType_t osal_critical_t;
|
typedef UBaseType_t osal_spinlock_t;
|
||||||
|
|
||||||
TU_ATTR_ALWAYS_INLINE static inline void osal_critical_init(osal_critical_t *ctx) {
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_init(osal_spinlock_t *ctx) {
|
||||||
(void) ctx;
|
(void) ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
TU_ATTR_ALWAYS_INLINE static inline void osal_critical_enter(osal_critical_t *ctx, bool in_isr) {
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_lock(osal_spinlock_t *ctx, bool in_isr) {
|
||||||
if (in_isr) {
|
if (in_isr) {
|
||||||
*ctx = taskENTER_CRITICAL_FROM_ISR();
|
*ctx = taskENTER_CRITICAL_FROM_ISR();
|
||||||
} else {
|
} else {
|
||||||
@@ -139,7 +139,7 @@ TU_ATTR_ALWAYS_INLINE static inline void osal_critical_enter(osal_critical_t *ct
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TU_ATTR_ALWAYS_INLINE static inline void osal_critical_exit(osal_critical_t *ctx, bool in_isr) {
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_unlock(osal_spinlock_t *ctx, bool in_isr) {
|
||||||
(void) ctx;
|
(void) ctx;
|
||||||
if (in_isr) {
|
if (in_isr) {
|
||||||
taskEXIT_CRITICAL_FROM_ISR(*ctx);
|
taskEXIT_CRITICAL_FROM_ISR(*ctx);
|
||||||
|
@@ -40,6 +40,28 @@ TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) {
|
|||||||
os_time_delay( os_time_ms_to_ticks32(msec) );
|
os_time_delay( os_time_ms_to_ticks32(msec) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------+
|
||||||
|
// Spinlock API
|
||||||
|
//--------------------------------------------------------------------+
|
||||||
|
typedef os_sr_t osal_spinlock_t;
|
||||||
|
|
||||||
|
#define OSAL_SPINLOCK_DEF(_name, _int_set) \
|
||||||
|
osal_spinlock_t _name
|
||||||
|
|
||||||
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_init(osal_spinlock_t *ctx) {
|
||||||
|
(void) ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_lock(osal_spinlock_t *ctx, bool in_isr) {
|
||||||
|
(void) in_isr;
|
||||||
|
OS_ENTER_CRITICAL(*ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_unlock(osal_spinlock_t *ctx, bool in_isr) {
|
||||||
|
(void) in_isr;
|
||||||
|
OS_ENTER_CRITICAL(*ctx);
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// Semaphore API
|
// Semaphore API
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
@@ -41,26 +41,27 @@ TU_ATTR_WEAK void osal_task_delay(uint32_t msec);
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// Critical API
|
// Spinlock API
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
typedef struct {
|
typedef struct {
|
||||||
void (* interrupt_set)(bool);
|
void (* interrupt_set)(bool);
|
||||||
} osal_critical_t;
|
} osal_spinlock_t;
|
||||||
|
|
||||||
#define OSAL_CRITIAL_DEF(_name, _int_set) \
|
// For SMP, spinlock must be locked by hardware, not use interrupt
|
||||||
osal_critical_t _name = { .interrupt_set = _int_set }
|
#define OSAL_SPINLOCK_DEF(_name, _int_set) \
|
||||||
|
osal_spinlock_t _name = { .interrupt_set = _int_set }
|
||||||
|
|
||||||
TU_ATTR_ALWAYS_INLINE static inline void osal_critical_init(osal_critical_t *ctx) {
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_init(osal_spinlock_t *ctx) {
|
||||||
(void) ctx;
|
(void) ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
TU_ATTR_ALWAYS_INLINE static inline void osal_critical_enter(osal_critical_t *ctx, bool in_isr) {
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_lock(osal_spinlock_t *ctx, bool in_isr) {
|
||||||
if (!in_isr) {
|
if (!in_isr) {
|
||||||
ctx->interrupt_set(false);
|
ctx->interrupt_set(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TU_ATTR_ALWAYS_INLINE static inline void osal_critical_exit(osal_critical_t *ctx, bool in_isr) {
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_unlock(osal_spinlock_t *ctx, bool in_isr) {
|
||||||
if (!in_isr) {
|
if (!in_isr) {
|
||||||
ctx->interrupt_set(true);
|
ctx->interrupt_set(true);
|
||||||
}
|
}
|
||||||
|
@@ -44,22 +44,22 @@ TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// Critical API
|
// Spinlock API
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
typedef critical_section_t osal_critical_t;
|
typedef critical_section_t osal_spinlock_t; // pico implement critical section with spinlock
|
||||||
#define OSAL_CRITIAL_DEF(_name, _int_set) \
|
#define OSAL_SPINLOCK_DEF(_name, _int_set) \
|
||||||
osal_critical_t _name
|
osal_spinlock_t _name
|
||||||
|
|
||||||
TU_ATTR_ALWAYS_INLINE static inline void osal_critical_init(osal_critical_t *ctx) {
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_init(osal_spinlock_t *ctx) {
|
||||||
critical_section_init(ctx);
|
critical_section_init(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
TU_ATTR_ALWAYS_INLINE static inline void osal_critical_enter(osal_critical_t *ctx, bool in_isr) {
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_lock(osal_spinlock_t *ctx, bool in_isr) {
|
||||||
(void) in_isr;
|
(void) in_isr;
|
||||||
critical_section_enter_blocking(ctx);
|
critical_section_enter_blocking(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
TU_ATTR_ALWAYS_INLINE static inline void osal_critical_exit(osal_critical_t *ctx, bool in_isr) {
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_unlock(osal_spinlock_t *ctx, bool in_isr) {
|
||||||
(void) in_isr;
|
(void) in_isr;
|
||||||
critical_section_exit(ctx);
|
critical_section_exit(ctx);
|
||||||
}
|
}
|
||||||
|
@@ -42,6 +42,28 @@ TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) {
|
|||||||
rt_thread_mdelay(msec);
|
rt_thread_mdelay(msec);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------+
|
||||||
|
// Spinlock API
|
||||||
|
//--------------------------------------------------------------------+
|
||||||
|
typedef struct rt_spinlock osal_spinlock_t;
|
||||||
|
|
||||||
|
#define OSAL_SPINLOCK_DEF(_name, _int_set) \
|
||||||
|
osal_spinlock_t _name
|
||||||
|
|
||||||
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_init(osal_spinlock_t *ctx) {
|
||||||
|
rt_spin_lock_init(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_lock(osal_spinlock_t *ctx, bool in_isr) {
|
||||||
|
(void) in_isr;
|
||||||
|
rt_spin_lock(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_unlock(osal_spinlock_t *ctx, bool in_isr) {
|
||||||
|
(void) in_isr;
|
||||||
|
rt_spin_unlock(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// Semaphore API
|
// Semaphore API
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
@@ -56,6 +56,25 @@ TU_ATTR_ALWAYS_INLINE static inline uint16_t msec2wait(uint32_t msec) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------+
|
||||||
|
// Spinlock API, stub not implemented
|
||||||
|
//--------------------------------------------------------------------+
|
||||||
|
typedef uint8_t osal_spinlock_t;
|
||||||
|
#define OSAL_SPINLOCK_DEF(_name, _int_set) \
|
||||||
|
osal_spinlock_t _name
|
||||||
|
|
||||||
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_init(osal_spinlock_t *ctx) {
|
||||||
|
(void) ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_lock(osal_spinlock_t *ctx, bool in_isr) {
|
||||||
|
(void) ctx; (void) in_isr;
|
||||||
|
}
|
||||||
|
|
||||||
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_unlock(osal_spinlock_t *ctx, bool in_isr) {
|
||||||
|
(void) ctx; (void) in_isr;
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// Semaphore API
|
// Semaphore API
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
@@ -35,6 +35,31 @@ TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) {
|
|||||||
k_msleep(msec);
|
k_msleep(msec);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------+
|
||||||
|
// Spinlock API
|
||||||
|
//--------------------------------------------------------------------+
|
||||||
|
typedef struct {
|
||||||
|
struct k_spinlock lock;
|
||||||
|
k_spinlock_key_t key;
|
||||||
|
} osal_spinlock_t;
|
||||||
|
|
||||||
|
#define OSAL_SPINLOCK_DEF(_name, _int_set) \
|
||||||
|
osal_spinlock_t _name
|
||||||
|
|
||||||
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_init(osal_spinlock_t *ctx) {
|
||||||
|
(void) ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_lock(osal_spinlock_t *ctx, bool in_isr) {
|
||||||
|
(void) in_isr;
|
||||||
|
ctx->key = k_spin_lock(&ctx->lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
TU_ATTR_ALWAYS_INLINE static inline void osal_spin_unlock(osal_spinlock_t *ctx, bool in_isr) {
|
||||||
|
(void) in_isr;
|
||||||
|
k_spin_unlock(&ctx->lock, ctx->key);
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// Binary Semaphore API
|
// Binary Semaphore API
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
@@ -57,7 +57,7 @@ typedef struct {
|
|||||||
static xfer_ctl_t xfer_status[DWC2_EP_MAX][2];
|
static xfer_ctl_t xfer_status[DWC2_EP_MAX][2];
|
||||||
#define XFER_CTL_BASE(_ep, _dir) (&xfer_status[_ep][_dir])
|
#define XFER_CTL_BASE(_ep, _dir) (&xfer_status[_ep][_dir])
|
||||||
|
|
||||||
static OSAL_CRITIAL_DEF(_dcd_critical, usbd_int_set);
|
static OSAL_SPINLOCK_DEF(_dcd_spinlock, usbd_int_set);
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
// EP0 transfers are limited to 1 packet - larger sizes has to be split
|
// EP0 transfers are limited to 1 packet - larger sizes has to be split
|
||||||
@@ -394,7 +394,7 @@ bool dcd_init(uint8_t rhport, const tusb_rhport_init_t* rh_init) {
|
|||||||
dwc2_regs_t* dwc2 = DWC2_REG(rhport);
|
dwc2_regs_t* dwc2 = DWC2_REG(rhport);
|
||||||
|
|
||||||
tu_memclr(&_dcd_data, sizeof(_dcd_data));
|
tu_memclr(&_dcd_data, sizeof(_dcd_data));
|
||||||
osal_critical_init(&_dcd_critical);
|
osal_spin_init(&_dcd_spinlock);
|
||||||
|
|
||||||
// Core Initialization
|
// Core Initialization
|
||||||
const bool is_highspeed = dwc2_core_is_highspeed(dwc2, TUSB_ROLE_DEVICE);
|
const bool is_highspeed = dwc2_core_is_highspeed(dwc2, TUSB_ROLE_DEVICE);
|
||||||
@@ -539,7 +539,7 @@ void dcd_edpt_close_all(uint8_t rhport) {
|
|||||||
dwc2_regs_t* dwc2 = DWC2_REG(rhport);
|
dwc2_regs_t* dwc2 = DWC2_REG(rhport);
|
||||||
uint8_t const ep_count = _dwc2_controller[rhport].ep_count;
|
uint8_t const ep_count = _dwc2_controller[rhport].ep_count;
|
||||||
|
|
||||||
osal_critical_enter(&_dcd_critical, false);
|
osal_spin_lock(&_dcd_spinlock, false);
|
||||||
|
|
||||||
_dcd_data.allocated_epin_count = 0;
|
_dcd_data.allocated_epin_count = 0;
|
||||||
|
|
||||||
@@ -560,7 +560,7 @@ void dcd_edpt_close_all(uint8_t rhport) {
|
|||||||
dfifo_flush_rx(dwc2);
|
dfifo_flush_rx(dwc2);
|
||||||
dfifo_device_init(rhport); // re-init dfifo
|
dfifo_device_init(rhport); // re-init dfifo
|
||||||
|
|
||||||
osal_critical_exit(&_dcd_critical, false);
|
osal_spin_unlock(&_dcd_spinlock, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool dcd_edpt_iso_alloc(uint8_t rhport, uint8_t ep_addr, uint16_t largest_packet_size) {
|
bool dcd_edpt_iso_alloc(uint8_t rhport, uint8_t ep_addr, uint16_t largest_packet_size) {
|
||||||
@@ -581,7 +581,7 @@ bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, uint16_t to
|
|||||||
xfer_ctl_t* xfer = XFER_CTL_BASE(epnum, dir);
|
xfer_ctl_t* xfer = XFER_CTL_BASE(epnum, dir);
|
||||||
bool ret;
|
bool ret;
|
||||||
|
|
||||||
osal_critical_enter(&_dcd_critical, false);
|
osal_spin_lock(&_dcd_spinlock, false);
|
||||||
|
|
||||||
if (xfer->max_size == 0) {
|
if (xfer->max_size == 0) {
|
||||||
ret = false; // Endpoint is closed
|
ret = false; // Endpoint is closed
|
||||||
@@ -600,7 +600,7 @@ bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, uint16_t to
|
|||||||
ret = true;
|
ret = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
osal_critical_exit(&_dcd_critical, false);
|
osal_spin_unlock(&_dcd_spinlock, false);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -618,7 +618,7 @@ bool dcd_edpt_xfer_fifo(uint8_t rhport, uint8_t ep_addr, tu_fifo_t* ff, uint16_t
|
|||||||
xfer_ctl_t* xfer = XFER_CTL_BASE(epnum, dir);
|
xfer_ctl_t* xfer = XFER_CTL_BASE(epnum, dir);
|
||||||
bool ret;
|
bool ret;
|
||||||
|
|
||||||
osal_critical_enter(&_dcd_critical, false);
|
osal_spin_lock(&_dcd_spinlock, false);
|
||||||
|
|
||||||
if (xfer->max_size == 0) {
|
if (xfer->max_size == 0) {
|
||||||
ret = false; // Endpoint is closed
|
ret = false; // Endpoint is closed
|
||||||
@@ -633,7 +633,7 @@ bool dcd_edpt_xfer_fifo(uint8_t rhport, uint8_t ep_addr, tu_fifo_t* ff, uint16_t
|
|||||||
ret = true;
|
ret = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
osal_critical_exit(&_dcd_critical, false);
|
osal_spin_unlock(&_dcd_spinlock, false);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -1021,14 +1021,14 @@ void dcd_int_handler(uint8_t rhport) {
|
|||||||
if (gintsts & GINTSTS_USBRST) {
|
if (gintsts & GINTSTS_USBRST) {
|
||||||
// USBRST is start of reset.
|
// USBRST is start of reset.
|
||||||
#if TUP_MCU_MULTIPLE_CORE
|
#if TUP_MCU_MULTIPLE_CORE
|
||||||
osal_critical_enter(&_dcd_critical, true);
|
osal_spin_lock(&_dcd_spinlock, true);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
dwc2->gintsts = GINTSTS_USBRST;
|
dwc2->gintsts = GINTSTS_USBRST;
|
||||||
handle_bus_reset(rhport);
|
handle_bus_reset(rhport);
|
||||||
|
|
||||||
#if TUP_MCU_MULTIPLE_CORE
|
#if TUP_MCU_MULTIPLE_CORE
|
||||||
osal_critical_exit(&_dcd_critical, true);
|
osal_spin_unlock(&_dcd_spinlock, true);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user