clean up osal semaphore/queue/mutex

This commit is contained in:
hathach
2018-03-01 11:17:11 +07:00
parent 30124b9b02
commit a789fad4b7
24 changed files with 225 additions and 249 deletions

View File

@@ -151,9 +151,6 @@ void osal_task_delay(uint32_t msec);
typedef volatile uint8_t osal_semaphore_t;
typedef osal_semaphore_t * osal_semaphore_handle_t;
#define OSAL_SEM_DEF(name) osal_semaphore_t name
#define OSAL_SEM_REF(name) &name
osal_semaphore_handle_t osal_semaphore_create(osal_semaphore_t * p_sem);
void osal_semaphore_wait(osal_semaphore_handle_t sem_hdl, uint32_t msec, tusb_error_t *p_error);
tusb_error_t osal_semaphore_post(osal_semaphore_handle_t sem_hdl);

View File

@@ -77,29 +77,29 @@ static inline void osal_task_delay(uint32_t msec)
//--------------------------------------------------------------------+
// Semaphore API
//--------------------------------------------------------------------+
#define OSAL_SEM_DEF(name)
#define OSAL_SEM_REF(name)
typedef xSemaphoreHandle osal_semaphore_handle_t;
typedef xSemaphoreHandle osal_semaphore_t;
// create FreeRTOS binary semaphore with zero as init value TODO: omit semaphore take from vSemaphoreCreateBinary API, should double checks this
#define osal_semaphore_create(x) xSemaphoreCreateBinary()
//#define osal_semaphore_create(x) xSemaphoreCreateBinary()
static inline osal_semaphore_t osal_semaphore_create(uint32_t max, uint32_t init)
{
return xSemaphoreCreateCounting(max, init);
}
// TODO add timeout (with instant return from ISR option) for semaphore post & queue send
static inline tusb_error_t osal_semaphore_post(osal_semaphore_handle_t const sem_hdl) ATTR_ALWAYS_INLINE;
static inline tusb_error_t osal_semaphore_post(osal_semaphore_handle_t const sem_hdl)
static inline tusb_error_t osal_semaphore_post(osal_semaphore_t sem_hdl)
{
return (xSemaphoreGive(sem_hdl) == pdTRUE) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_SEMAPHORE_FAILED;
}
static inline void osal_semaphore_wait(osal_semaphore_handle_t const sem_hdl, uint32_t msec, tusb_error_t *p_error) ATTR_ALWAYS_INLINE;
static inline void osal_semaphore_wait(osal_semaphore_handle_t const 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 *p_error)
{
uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : osal_tick_from_msec(msec);
(*p_error) = ( xSemaphoreTake(sem_hdl, ticks) == pdPASS ) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT;
}
static inline void osal_semaphore_reset(osal_semaphore_handle_t const sem_hdl) ATTR_ALWAYS_INLINE;
static inline void osal_semaphore_reset(osal_semaphore_handle_t const sem_hdl)
static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl)
{
(void) xSemaphoreTake(sem_hdl, 0);
}
@@ -107,27 +107,22 @@ static inline void osal_semaphore_reset(osal_semaphore_handle_t const sem_hdl)
//--------------------------------------------------------------------+
// MUTEX API (priority inheritance)
//--------------------------------------------------------------------+
#define OSAL_MUTEX_DEF OSAL_SEM_DEF
#define OSAL_MUTEX_REF OSAL_SEM_REF
typedef xSemaphoreHandle osal_mutex_handle_t;
typedef xSemaphoreHandle osal_mutex_t;
#define osal_mutex_create(x) xSemaphoreCreateMutex()
static inline tusb_error_t osal_mutex_release(osal_mutex_handle_t const mutex_hdl) ATTR_ALWAYS_INLINE;
static inline tusb_error_t osal_mutex_release(osal_mutex_handle_t const mutex_hdl)
static inline tusb_error_t osal_mutex_release(osal_mutex_t mutex_hdl)
{
return (xSemaphoreGive(mutex_hdl) == pdPASS) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_MUTEX_FAILED;
return osal_semaphore_post(mutex_hdl);
}
static inline void osal_mutex_wait(osal_mutex_handle_t const mutex_hdl, uint32_t msec, tusb_error_t *p_error) ATTR_ALWAYS_INLINE;
static inline void osal_mutex_wait(osal_mutex_handle_t const mutex_hdl, uint32_t msec, tusb_error_t *p_error)
static inline void osal_mutex_wait(osal_mutex_t mutex_hdl, uint32_t msec, tusb_error_t *p_error)
{
uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : osal_tick_from_msec(msec);
(*p_error) = ( xSemaphoreTake(mutex_hdl, ticks) == pdPASS ) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT;
osal_semaphore_wait(mutex_hdl, msec, p_error);
}
static inline void osal_mutex_reset(osal_mutex_handle_t const mutex_hdl) ATTR_ALWAYS_INLINE;
static inline void osal_mutex_reset(osal_mutex_handle_t const mutex_hdl)
// TOOD remove
static inline void osal_mutex_reset(osal_mutex_t mutex_hdl)
{
xSemaphoreGive(mutex_hdl);
}
@@ -137,7 +132,6 @@ static inline void osal_mutex_reset(osal_mutex_handle_t const mutex_hdl)
//--------------------------------------------------------------------+
typedef xQueueHandle osal_queue_t;
static inline osal_queue_t osal_queue_create(uint32_t depth, uint32_t item_size)
{
return xQueueCreate(depth, item_size);

View File

@@ -150,43 +150,49 @@ static inline bool osal_task_create(osal_func_t code, const char* name, uint32_t
//--------------------------------------------------------------------+
// Semaphore API
//--------------------------------------------------------------------+
typedef volatile uint8_t osal_semaphore_t;
typedef osal_semaphore_t * osal_semaphore_handle_t;
#define OSAL_SEM_DEF(name) osal_semaphore_t name
#define OSAL_SEM_REF(name) &name
static inline osal_semaphore_handle_t osal_semaphore_create(osal_semaphore_t * p_sem) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
static inline osal_semaphore_handle_t osal_semaphore_create(osal_semaphore_t * p_sem)
typedef struct
{
(*p_sem) = 0; // TODO consider to have initial count parameter
return (osal_semaphore_handle_t) p_sem;
volatile uint16_t count;
uint16_t max_count;
}osal_semaphore_data_t;
typedef osal_semaphore_data_t* osal_semaphore_t;
static inline osal_semaphore_t osal_semaphore_create(uint32_t max_count, uint32_t init)
{
osal_semaphore_data_t* sem_data = (osal_semaphore_data_t*) tu_malloc( sizeof(osal_semaphore_data_t));
VERIFY(sem_data, NULL);
sem_data->count = init;
sem_data->max_count = max_count;
return sem_data;
}
static inline tusb_error_t osal_semaphore_post(osal_semaphore_handle_t sem_hdl) ATTR_ALWAYS_INLINE;
static inline tusb_error_t osal_semaphore_post(osal_semaphore_handle_t sem_hdl)
static inline tusb_error_t osal_semaphore_post(osal_semaphore_t sem_hdl)
{
(*sem_hdl)++;
if (sem_hdl->count < sem_hdl->max_count ) sem_hdl->count++;
return TUSB_ERROR_NONE;
}
static inline void osal_semaphore_reset(osal_semaphore_handle_t sem_hdl) ATTR_ALWAYS_INLINE;
static inline void osal_semaphore_reset(osal_semaphore_handle_t sem_hdl)
static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl)
{
(*sem_hdl) = 0;
sem_hdl->count = 0;
}
#define osal_semaphore_wait(sem_hdl, msec, p_error) \
do {\
timeout = osal_tick_get();\
state = __LINE__; case __LINE__:\
if( *(sem_hdl) == 0 ) {\
if( sem_hdl->count == 0 ) {\
if ( ( ((uint32_t) (msec)) != OSAL_TIMEOUT_WAIT_FOREVER) && (timeout + osal_tick_from_msec(msec) <= osal_tick_get()) ) /* time out */ \
*(p_error) = TUSB_ERROR_OSAL_TIMEOUT;\
else\
return TUSB_ERROR_OSAL_WAITING;\
} else{\
(*(sem_hdl))--; /*TODO mutex hal_interrupt_disable consideration*/\
if (sem_hdl->count) sem_hdl->count--; /*TODO mutex hal_interrupt_disable consideration*/\
*(p_error) = TUSB_ERROR_NONE;\
}\
}while(0)
@@ -194,31 +200,22 @@ static inline void osal_semaphore_reset(osal_semaphore_handle_t sem_hdl)
//--------------------------------------------------------------------+
// MUTEX API (priority inheritance)
//--------------------------------------------------------------------+
typedef osal_semaphore_t osal_mutex_t;
typedef osal_semaphore_handle_t osal_mutex_handle_t;
typedef osal_semaphore_t osal_mutex_t;
#define OSAL_MUTEX_DEF(name) osal_mutex_t name
#define OSAL_MUTEX_REF(name) &name
static inline osal_mutex_handle_t osal_mutex_create(osal_mutex_t * p_mutex) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
static inline osal_mutex_handle_t osal_mutex_create(osal_mutex_t * p_mutex)
static inline osal_mutex_t osal_mutex_create(void)
{
(*p_mutex) = 1;
return (osal_mutex_handle_t) p_mutex;
return osal_semaphore_create(1, 0);
}
static inline tusb_error_t osal_mutex_release(osal_mutex_handle_t mutex_hdl) ATTR_ALWAYS_INLINE;
static inline tusb_error_t osal_mutex_release(osal_mutex_handle_t mutex_hdl)
static inline tusb_error_t osal_mutex_release(osal_mutex_t mutex_hdl)
{
(*mutex_hdl) = 1; // mutex is a binary semaphore
return TUSB_ERROR_NONE;
return osal_semaphore_post(mutex_hdl);
}
static inline void osal_mutex_reset(osal_mutex_handle_t mutex_hdl) ATTR_ALWAYS_INLINE;
static inline void osal_mutex_reset(osal_mutex_handle_t mutex_hdl)
// TOOD remove
static inline void osal_mutex_reset(osal_mutex_t mutex_hdl)
{
(*mutex_hdl) = 1;
osal_semaphore_reset(mutex_hdl);
}
#define osal_mutex_wait osal_semaphore_wait