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

@@ -89,7 +89,7 @@ tusb_error_t tusbh_cdc_rndis_get_mac_addr(uint8_t dev_addr, uint8_t mac_address[
// To enable the TASK_ASSERT style (quick return on false condition) in a real RTOS, a task must act as a wrapper
// and is used mainly to call subtasks. Within a subtask return statement can be called freely, the task with
// forever loop cannot have any return at all.
OSAL_TASK_FUNCTION(cdch_rndis_task) (void* p_task_para)
OSAL_TASK_FUNCTION(cdch_rndis_task) (void* param;)
{
OSAL_TASK_LOOP_BEGIN

View File

@@ -54,8 +54,7 @@
TUSB_CFG_ATTR_USBRAM STATIC_VAR msch_interface_t msch_data[TUSB_CFG_HOST_DEVICE_MAX];
//------------- Initalization Data -------------//
OSAL_SEM_DEF(msch_semaphore);
static osal_semaphore_handle_t msch_sem_hdl;
static osal_semaphore_t msch_sem_hdl;
// buffer used to read scsi information when mounted, largest response data currently is inquiry
TUSB_CFG_ATTR_USBRAM ATTR_ALIGNED(4) STATIC_VAR uint8_t msch_buffer[sizeof(scsi_inquiry_data_t)];
@@ -291,7 +290,7 @@ tusb_error_t tuh_msc_write10(uint8_t dev_addr, uint8_t lun, void const * p_buffe
void msch_init(void)
{
memclr_(msch_data, sizeof(msch_interface_t)*TUSB_CFG_HOST_DEVICE_MAX);
msch_sem_hdl = osal_semaphore_create( OSAL_SEM_REF(msch_semaphore) );
msch_sem_hdl = osal_semaphore_create(1, 0);
}
tusb_error_t msch_open_subtask(uint8_t dev_addr, tusb_descriptor_interface_t const *p_interface_desc, uint16_t *p_length)

View File

@@ -147,10 +147,9 @@ STATIC_ASSERT(sizeof(usbd_task_event_t) <= 12, "size is not correct");
#define TUSB_CFG_OS_TASK_PRIO 0
#endif
OSAL_SEM_DEF(usbd_control_xfer_semaphore_def);
static osal_queue_t usbd_queue_hdl;
/*static*/ osal_semaphore_handle_t usbd_control_xfer_sem_hdl; // TODO may need to change to static with wrapper function
/*static*/ osal_semaphore_t usbd_control_xfer_sem_hdl; // TODO may need to change to static with wrapper function
//--------------------------------------------------------------------+
// IMPLEMENTATION
@@ -166,7 +165,7 @@ tusb_error_t usbd_init (void)
usbd_queue_hdl = osal_queue_create(USBD_TASK_QUEUE_DEPTH, sizeof(usbd_task_event_t));
ASSERT_PTR(usbd_queue_hdl, TUSB_ERROR_OSAL_QUEUE_FAILED);
usbd_control_xfer_sem_hdl = osal_semaphore_create( OSAL_SEM_REF(usbd_control_xfer_semaphore_def) );
usbd_control_xfer_sem_hdl = osal_semaphore_create(1, 0);
ASSERT_PTR(usbd_queue_hdl, TUSB_ERROR_OSAL_SEMAPHORE_FAILED);
osal_task_t usbd_hdl;

View File

@@ -99,7 +99,7 @@ bool tusbd_is_configured(uint8_t coreid) ATTR_WARN_UNUSED_RESULT;
//--------------------------------------------------------------------+
#ifdef _TINY_USB_SOURCE_FILE_
extern osal_semaphore_handle_t usbd_control_xfer_sem_hdl;
extern osal_semaphore_t usbd_control_xfer_sem_hdl;
tusb_error_t usbd_init(void);
void usbd_task( void* param);

View File

@@ -42,6 +42,10 @@
#define _TINY_USB_SOURCE_FILE_
#ifndef TUSB_CFG_OS_TASK_PRIO
#define TUSB_CFG_OS_TASK_PRIO 0
#endif
//--------------------------------------------------------------------+
// INCLUDE
//--------------------------------------------------------------------+
@@ -153,10 +157,10 @@ tusb_error_t usbh_init(void)
{
usbh_device_info_t * const p_device = &usbh_devices[i];
p_device->control.sem_hdl = osal_semaphore_create( OSAL_SEM_REF(p_device->control.semaphore) );
p_device->control.sem_hdl = osal_semaphore_create(1, 0);
ASSERT_PTR(p_device->control.sem_hdl, TUSB_ERROR_OSAL_SEMAPHORE_FAILED);
p_device->control.mutex_hdl = osal_mutex_create ( OSAL_MUTEX_REF(p_device->control.mutex) );
p_device->control.mutex_hdl = osal_mutex_create();
ASSERT_PTR(p_device->control.mutex_hdl, TUSB_ERROR_OSAL_MUTEX_FAILED);
}

View File

@@ -98,7 +98,7 @@ ATTR_WEAK void tuh_device_mount_failed_cb(tusb_error_t error, tusb_descriptor
#ifdef _TINY_USB_SOURCE_FILE_
OSAL_TASK_FUNCTION (usbh_enumeration_task, p_task_para);
void usbh_enumeration_task(void* param);
tusb_error_t usbh_init(void);
tusb_error_t usbh_control_xfer_subtask(uint8_t dev_addr, uint8_t bmRequestType, uint8_t bRequest,

View File

@@ -91,11 +91,8 @@ typedef struct {
// uint8_t xferred_bytes; TODO not yet necessary
tusb_control_request_t request;
OSAL_SEM_DEF(semaphore); // TODO move to semaphore pool ?
osal_semaphore_handle_t sem_hdl; // used to synchronize with HCD when control xfer complete
OSAL_MUTEX_DEF(mutex); // TODO move to mutex pool ?
osal_mutex_handle_t mutex_hdl; // used to exclusively occupy control pipe
osal_semaphore_t sem_hdl; // used to synchronize with HCD when control xfer complete
osal_mutex_t mutex_hdl; // used to exclusively occupy control pipe
} control;
} usbh_device_info_t;

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