mirror of
https://github.com/hathach/tinyusb.git
synced 2025-03-17 04:21:23 +00:00
add osal_mutex
This commit is contained in:
parent
b2f18744fe
commit
4683dc1e68
@ -81,20 +81,20 @@ typedef void (*osal_task_func_t)( void * );
|
||||
* osal_queue_t osal_queue_create(osal_queue_def_t* qdef)
|
||||
* osal_queue_receive (osal_queue_t const queue_hdl, void *p_data, uint32_t msec, tusb_error_t *p_error)
|
||||
* bool osal_queue_send(osal_queue_t const queue_hdl, void const * data, bool in_isr)
|
||||
* osal_queue_flush() TODO remove
|
||||
* osal_queue_reset()
|
||||
*
|
||||
* Semaphore
|
||||
* osal_semaphore_def_t, osal_semaphore_t
|
||||
* osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef)
|
||||
* bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
|
||||
* void osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec, tusb_error_t *p_error)
|
||||
* void osal_semaphore_reset_isr(osal_semaphore_t const sem_hdl)
|
||||
* void osal_semaphore_reset(osal_semaphore_t const sem_hdl)
|
||||
*
|
||||
* Mutex
|
||||
* osal_mutex_t
|
||||
* osal_mutex_create()
|
||||
* bool osal_mutex_release(osal_mutex_t mutex_hdl)
|
||||
* void osal_mutex_wait(osal_mutex_t mutex_hdl, uint32_t msec, tusb_error_t *p_error)
|
||||
* osal_mutex_create(osal_mutex_def_t* mdef)
|
||||
* bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
|
||||
* void osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec, tusb_error_t *p_error)
|
||||
*/
|
||||
|
||||
#if CFG_TUSB_OS == OPT_OS_FREERTOS
|
||||
|
@ -114,10 +114,10 @@ static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef)
|
||||
return xQueueCreateStatic(qdef->depth, qdef->item_sz, (uint8_t*) qdef->buf, &qdef->sq);
|
||||
}
|
||||
|
||||
static inline void osal_queue_receive (osal_queue_t const queue_hdl, void *p_data, uint32_t msec, tusb_error_t *p_error)
|
||||
static inline void osal_queue_receive (osal_queue_t const queue_hdl, void *p_data, uint32_t msec, tusb_error_t *err)
|
||||
{
|
||||
uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : pdMS_TO_TICKS(msec);
|
||||
(*p_error) = ( xQueueReceive(queue_hdl, p_data, ticks) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT);
|
||||
(*err) = ( xQueueReceive(queue_hdl, p_data, ticks) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT);
|
||||
}
|
||||
|
||||
static inline bool osal_queue_send(osal_queue_t const queue_hdl, void const * data, bool in_isr)
|
||||
@ -125,10 +125,9 @@ static inline bool osal_queue_send(osal_queue_t const queue_hdl, void const * da
|
||||
return in_isr ? xQueueSendToBackFromISR(queue_hdl, data, NULL) : xQueueSendToBack(queue_hdl, data, OSAL_TIMEOUT_WAIT_FOREVER);
|
||||
}
|
||||
|
||||
static inline void osal_queue_flush(osal_queue_t const queue_hdl)
|
||||
static inline void osal_queue_reset(osal_queue_t const queue_hdl)
|
||||
{
|
||||
// TODO move to thread context
|
||||
// xQueueReset(queue_hdl);
|
||||
xQueueReset(queue_hdl);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
@ -147,33 +146,33 @@ static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
|
||||
return in_isr ? xSemaphoreGiveFromISR(sem_hdl, NULL) : xSemaphoreGive(sem_hdl);
|
||||
}
|
||||
|
||||
static inline void osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec, tusb_error_t *p_error)
|
||||
static inline void osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec, tusb_error_t *err)
|
||||
{
|
||||
uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : pdMS_TO_TICKS(msec);
|
||||
(*p_error) = (xSemaphoreTake(sem_hdl, ticks) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT);
|
||||
(*err) = (xSemaphoreTake(sem_hdl, ticks) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT);
|
||||
}
|
||||
|
||||
static inline void osal_semaphore_reset_isr(osal_semaphore_t const sem_hdl)
|
||||
static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl)
|
||||
{
|
||||
xSemaphoreTakeFromISR(sem_hdl, NULL);
|
||||
xQueueReset(sem_hdl);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MUTEX API (priority inheritance)
|
||||
//--------------------------------------------------------------------+
|
||||
typedef StaticSemaphore_t osal_mutex_def_t;
|
||||
typedef SemaphoreHandle_t osal_mutex_t;
|
||||
|
||||
#define osal_mutex_create(x) xSemaphoreCreateMutex()
|
||||
|
||||
static inline bool osal_mutex_release(osal_mutex_t mutex_hdl)
|
||||
static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
|
||||
{
|
||||
return xSemaphoreGive(mutex_hdl);
|
||||
return xSemaphoreCreateMutexStatic(mdef);
|
||||
}
|
||||
|
||||
static inline void osal_mutex_wait(osal_mutex_t mutex_hdl, uint32_t msec, tusb_error_t *p_error)
|
||||
#define osal_mutex_lock osal_semaphore_wait
|
||||
|
||||
static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
|
||||
{
|
||||
uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : pdMS_TO_TICKS(msec);
|
||||
(*p_error) = (xSemaphoreTake(mutex_hdl, ticks) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT);
|
||||
return xSemaphoreGive(mutex_hdl);
|
||||
}
|
||||
|
||||
|
||||
|
@ -78,12 +78,12 @@ static inline bool osal_task_create(osal_task_def_t* taskdef)
|
||||
#define TASK_RESTART \
|
||||
_state = 0
|
||||
|
||||
#define osal_task_delay(msec) \
|
||||
do { \
|
||||
_timeout = tusb_hal_millis(); \
|
||||
_state = __LINE__; case __LINE__: \
|
||||
if ( _timeout + msec > tusb_hal_millis() ) \
|
||||
return TUSB_ERROR_OSAL_WAITING; \
|
||||
#define osal_task_delay(_msec) \
|
||||
do { \
|
||||
_timeout = tusb_hal_millis(); \
|
||||
_state = __LINE__; case __LINE__: \
|
||||
if ( _timeout + (_msec) > tusb_hal_millis() ) \
|
||||
return TUSB_ERROR_OSAL_WAITING; \
|
||||
}while(0)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
@ -139,26 +139,26 @@ static inline bool osal_queue_send(osal_queue_t const queue_hdl, void const * da
|
||||
return tu_fifo_write( (tu_fifo_t*) queue_hdl, data);
|
||||
}
|
||||
|
||||
static inline void osal_queue_flush(osal_queue_t const queue_hdl)
|
||||
static inline void osal_queue_reset(osal_queue_t const queue_hdl)
|
||||
{
|
||||
queue_hdl->count = queue_hdl->rd_idx = queue_hdl->wr_idx = 0;
|
||||
}
|
||||
|
||||
#define osal_queue_receive(queue_hdl, p_data, msec, p_error) \
|
||||
do { \
|
||||
_timeout = tusb_hal_millis(); \
|
||||
_state = __LINE__; case __LINE__: \
|
||||
if( queue_hdl->count == 0 ) { \
|
||||
if ( (msec != OSAL_TIMEOUT_WAIT_FOREVER) && ( _timeout + msec <= tusb_hal_millis()) ) \
|
||||
*(p_error) = TUSB_ERROR_OSAL_TIMEOUT; \
|
||||
else \
|
||||
return TUSB_ERROR_OSAL_WAITING; \
|
||||
} else{ \
|
||||
/*tusb_hal_int_disable_all();*/ \
|
||||
tu_fifo_read(queue_hdl, p_data); \
|
||||
/*tusb_hal_int_enable_all();*/ \
|
||||
*(p_error) = TUSB_ERROR_NONE; \
|
||||
} \
|
||||
#define osal_queue_receive(_q_hdl, p_data, _msec, _err) \
|
||||
do { \
|
||||
_timeout = tusb_hal_millis(); \
|
||||
_state = __LINE__; case __LINE__: \
|
||||
if( (_q_hdl)->count == 0 ) { \
|
||||
if ( ((_msec) != OSAL_TIMEOUT_WAIT_FOREVER) && ( _timeout + (_msec) <= tusb_hal_millis()) ) \
|
||||
*(_err) = TUSB_ERROR_OSAL_TIMEOUT; \
|
||||
else \
|
||||
return TUSB_ERROR_OSAL_WAITING; \
|
||||
} else{ \
|
||||
/* Enter critical ? */ \
|
||||
tu_fifo_read(queue_hdl, p_data); \
|
||||
/* Exit critical ? */ \
|
||||
*(_err) = TUSB_ERROR_NONE; \
|
||||
} \
|
||||
}while(0)
|
||||
|
||||
|
||||
@ -168,66 +168,73 @@ static inline void osal_queue_flush(osal_queue_t const queue_hdl)
|
||||
typedef struct
|
||||
{
|
||||
volatile uint16_t count;
|
||||
uint16_t max_count;
|
||||
}osal_semaphore_def_t;
|
||||
|
||||
typedef osal_semaphore_def_t* osal_semaphore_t;
|
||||
|
||||
|
||||
static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef)
|
||||
{
|
||||
semdef->count = 0;
|
||||
semdef->max_count = 1;
|
||||
semdef->count = 0;
|
||||
return semdef;
|
||||
}
|
||||
|
||||
static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
|
||||
static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
|
||||
{
|
||||
(void) in_isr;
|
||||
if (sem_hdl->count < sem_hdl->max_count ) sem_hdl->count++;
|
||||
sem_hdl->count++;
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline void osal_semaphore_reset_isr(osal_semaphore_t sem_hdl)
|
||||
static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl)
|
||||
{
|
||||
sem_hdl->count = 0;
|
||||
}
|
||||
|
||||
#define osal_semaphore_wait(sem_hdl, msec, p_error) \
|
||||
do { \
|
||||
_timeout = tusb_hal_millis(); \
|
||||
_state = __LINE__; case __LINE__: \
|
||||
if( sem_hdl->count == 0 ) { \
|
||||
if ( (msec != OSAL_TIMEOUT_WAIT_FOREVER) && (_timeout + msec <= tusb_hal_millis()) ) \
|
||||
*(p_error) = TUSB_ERROR_OSAL_TIMEOUT; \
|
||||
else \
|
||||
return TUSB_ERROR_OSAL_WAITING; \
|
||||
} else{ \
|
||||
/*tusb_hal_int_disable_all();*/ \
|
||||
sem_hdl->count--; \
|
||||
/*tusb_hal_int_enable_all();*/ \
|
||||
*(p_error) = TUSB_ERROR_NONE; \
|
||||
} \
|
||||
#define osal_semaphore_wait(_sem_hdl, _msec, _err) \
|
||||
do { \
|
||||
_timeout = tusb_hal_millis(); \
|
||||
_state = __LINE__; case __LINE__: \
|
||||
if( (_sem_hdl)->count == 0 ) { \
|
||||
if ( ((_msec) != OSAL_TIMEOUT_WAIT_FOREVER) && (_timeout + (_msec) <= tusb_hal_millis()) ) \
|
||||
*(_err) = TUSB_ERROR_OSAL_TIMEOUT; \
|
||||
else \
|
||||
return TUSB_ERROR_OSAL_WAITING; \
|
||||
} else{ \
|
||||
/* Enter critical ? */ \
|
||||
(_sem_hdl)->count--; \
|
||||
/* Exit critical ? */ \
|
||||
*(_err) = TUSB_ERROR_NONE; \
|
||||
} \
|
||||
}while(0)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MUTEX API (priority inheritance)
|
||||
// MUTEX API
|
||||
// Within tinyusb, mutex is never used in ISR context
|
||||
//--------------------------------------------------------------------+
|
||||
#if 0
|
||||
typedef osal_semaphore_def_t osal_mutex_def_t;
|
||||
typedef osal_semaphore_t osal_mutex_t;
|
||||
|
||||
static inline osal_mutex_t osal_mutex_create(void)
|
||||
static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
|
||||
{
|
||||
return osal_semaphore_create(1, 0);
|
||||
mdef->count = 1;
|
||||
return mdef;
|
||||
}
|
||||
|
||||
static inline bool osal_mutex_release(osal_mutex_t mutex_hdl)
|
||||
{
|
||||
return osal_semaphore_post(mutex_hdl);
|
||||
}
|
||||
#define osal_mutex_unlock(_mutex_hdl) osal_semaphore_post(_mutex_hdl, false)
|
||||
#define osal_mutex_lock osal_semaphore_wait
|
||||
|
||||
#define osal_mutex_wait osal_semaphore_wait
|
||||
#endif
|
||||
// check if mutex is available for non-thread/substask usage in some cases
|
||||
static inline bool osal_mutex_lock_notask(osal_mutex_t mutex_hdl)
|
||||
{
|
||||
if (mutex_hdl->count)
|
||||
{
|
||||
mutex_hdl->count--;
|
||||
return true;
|
||||
}else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
Loading…
x
Reference in New Issue
Block a user