add osal_mutex

This commit is contained in:
hathach 2018-11-02 15:45:27 +07:00
parent b2f18744fe
commit 4683dc1e68
3 changed files with 81 additions and 75 deletions

View File

@ -81,20 +81,20 @@ typedef void (*osal_task_func_t)( void * );
* osal_queue_t osal_queue_create(osal_queue_def_t* qdef) * 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) * 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) * 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 * Semaphore
* osal_semaphore_def_t, osal_semaphore_t * osal_semaphore_def_t, osal_semaphore_t
* 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_post(osal_semaphore_t sem_hdl, bool in_isr) * 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_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 * Mutex
* osal_mutex_t * osal_mutex_t
* osal_mutex_create() * osal_mutex_create(osal_mutex_def_t* mdef)
* bool osal_mutex_release(osal_mutex_t mutex_hdl) * bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
* void osal_mutex_wait(osal_mutex_t mutex_hdl, uint32_t msec, tusb_error_t *p_error) * void osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec, tusb_error_t *p_error)
*/ */
#if CFG_TUSB_OS == OPT_OS_FREERTOS #if CFG_TUSB_OS == OPT_OS_FREERTOS

View File

@ -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); 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); 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) 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); 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); 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); 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) // MUTEX API (priority inheritance)
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
typedef StaticSemaphore_t osal_mutex_def_t;
typedef SemaphoreHandle_t osal_mutex_t; typedef SemaphoreHandle_t osal_mutex_t;
#define osal_mutex_create(x) xSemaphoreCreateMutex() static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
static inline bool osal_mutex_release(osal_mutex_t mutex_hdl)
{ {
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); return xSemaphoreGive(mutex_hdl);
(*p_error) = (xSemaphoreTake(mutex_hdl, ticks) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT);
} }

View File

@ -78,11 +78,11 @@ static inline bool osal_task_create(osal_task_def_t* taskdef)
#define TASK_RESTART \ #define TASK_RESTART \
_state = 0 _state = 0
#define osal_task_delay(msec) \ #define osal_task_delay(_msec) \
do { \ do { \
_timeout = tusb_hal_millis(); \ _timeout = tusb_hal_millis(); \
_state = __LINE__; case __LINE__: \ _state = __LINE__; case __LINE__: \
if ( _timeout + msec > tusb_hal_millis() ) \ if ( _timeout + (_msec) > tusb_hal_millis() ) \
return TUSB_ERROR_OSAL_WAITING; \ return TUSB_ERROR_OSAL_WAITING; \
}while(0) }while(0)
@ -139,25 +139,25 @@ 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); 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; queue_hdl->count = queue_hdl->rd_idx = queue_hdl->wr_idx = 0;
} }
#define osal_queue_receive(queue_hdl, p_data, msec, p_error) \ #define osal_queue_receive(_q_hdl, p_data, _msec, _err) \
do { \ do { \
_timeout = tusb_hal_millis(); \ _timeout = tusb_hal_millis(); \
_state = __LINE__; case __LINE__: \ _state = __LINE__; case __LINE__: \
if( queue_hdl->count == 0 ) { \ if( (_q_hdl)->count == 0 ) { \
if ( (msec != OSAL_TIMEOUT_WAIT_FOREVER) && ( _timeout + msec <= tusb_hal_millis()) ) \ if ( ((_msec) != OSAL_TIMEOUT_WAIT_FOREVER) && ( _timeout + (_msec) <= tusb_hal_millis()) ) \
*(p_error) = TUSB_ERROR_OSAL_TIMEOUT; \ *(_err) = TUSB_ERROR_OSAL_TIMEOUT; \
else \ else \
return TUSB_ERROR_OSAL_WAITING; \ return TUSB_ERROR_OSAL_WAITING; \
} else{ \ } else{ \
/*tusb_hal_int_disable_all();*/ \ /* Enter critical ? */ \
tu_fifo_read(queue_hdl, p_data); \ tu_fifo_read(queue_hdl, p_data); \
/*tusb_hal_int_enable_all();*/ \ /* Exit critical ? */ \
*(p_error) = TUSB_ERROR_NONE; \ *(_err) = TUSB_ERROR_NONE; \
} \ } \
}while(0) }while(0)
@ -168,66 +168,73 @@ static inline void osal_queue_flush(osal_queue_t const queue_hdl)
typedef struct typedef struct
{ {
volatile uint16_t count; volatile uint16_t count;
uint16_t max_count;
}osal_semaphore_def_t; }osal_semaphore_def_t;
typedef osal_semaphore_def_t* osal_semaphore_t; typedef osal_semaphore_def_t* osal_semaphore_t;
static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef)
{ {
semdef->count = 0; semdef->count = 0;
semdef->max_count = 1;
return semdef; 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; (void) in_isr;
if (sem_hdl->count < sem_hdl->max_count ) sem_hdl->count++; sem_hdl->count++;
return true; 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; sem_hdl->count = 0;
} }
#define osal_semaphore_wait(sem_hdl, msec, p_error) \ #define osal_semaphore_wait(_sem_hdl, _msec, _err) \
do { \ do { \
_timeout = tusb_hal_millis(); \ _timeout = tusb_hal_millis(); \
_state = __LINE__; case __LINE__: \ _state = __LINE__; case __LINE__: \
if( sem_hdl->count == 0 ) { \ if( (_sem_hdl)->count == 0 ) { \
if ( (msec != OSAL_TIMEOUT_WAIT_FOREVER) && (_timeout + msec <= tusb_hal_millis()) ) \ if ( ((_msec) != OSAL_TIMEOUT_WAIT_FOREVER) && (_timeout + (_msec) <= tusb_hal_millis()) ) \
*(p_error) = TUSB_ERROR_OSAL_TIMEOUT; \ *(_err) = TUSB_ERROR_OSAL_TIMEOUT; \
else \ else \
return TUSB_ERROR_OSAL_WAITING; \ return TUSB_ERROR_OSAL_WAITING; \
} else{ \ } else{ \
/*tusb_hal_int_disable_all();*/ \ /* Enter critical ? */ \
sem_hdl->count--; \ (_sem_hdl)->count--; \
/*tusb_hal_int_enable_all();*/ \ /* Exit critical ? */ \
*(p_error) = TUSB_ERROR_NONE; \ *(_err) = TUSB_ERROR_NONE; \
} \ } \
}while(0) }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; 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) #define osal_mutex_unlock(_mutex_hdl) osal_semaphore_post(_mutex_hdl, false)
{ #define osal_mutex_lock osal_semaphore_wait
return osal_semaphore_post(mutex_hdl);
}
#define osal_mutex_wait osal_semaphore_wait // check if mutex is available for non-thread/substask usage in some cases
#endif 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 #ifdef __cplusplus