From 40935fc01ca33b6123c1626f930faf9b48e0b2b0 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 1 Mar 2018 11:42:13 +0700 Subject: [PATCH] more osal clean up --- demos/device/src/cdc_device_app.c | 2 +- demos/host/src/cdc_serial_host_app.c | 2 +- tinyusb/class/msc_host.c | 2 +- tinyusb/device/usbd.c | 2 +- tinyusb/host/usbh.c | 2 +- tinyusb/osal/osal_freeRTOS.h | 111 ++++++++++---------- tinyusb/osal/osal_none.h | 148 ++++++++++++++------------- 7 files changed, 138 insertions(+), 131 deletions(-) diff --git a/demos/device/src/cdc_device_app.c b/demos/device/src/cdc_device_app.c index f07f703ad..427b9a159 100644 --- a/demos/device/src/cdc_device_app.c +++ b/demos/device/src/cdc_device_app.c @@ -88,7 +88,7 @@ void tusbd_cdc_xfer_cb(uint8_t coreid, tusb_event_t event, cdc_pipeid_t pipe_id, { fifo_write(&fifo_serial, serial_rx_buffer+i); } - (void) osal_semaphore_post(sem_hdl); // notify main task + osal_semaphore_post(sem_hdl); // notify main task break; case TUSB_EVENT_XFER_ERROR: diff --git a/demos/host/src/cdc_serial_host_app.c b/demos/host/src/cdc_serial_host_app.c index dd7d1373c..4c31eb442 100644 --- a/demos/host/src/cdc_serial_host_app.c +++ b/demos/host/src/cdc_serial_host_app.c @@ -86,7 +86,7 @@ void tuh_cdc_xfer_isr(uint8_t dev_addr, tusb_event_t event, cdc_pipeid_t pipe_id { case TUSB_EVENT_XFER_COMPLETE: received_bytes = xferred_bytes; - (void) osal_semaphore_post(sem_hdl); // notify main task + osal_semaphore_post(sem_hdl); // notify main task break; case TUSB_EVENT_XFER_ERROR: diff --git a/tinyusb/class/msc_host.c b/tinyusb/class/msc_host.c index f4859c1eb..00186ddd9 100644 --- a/tinyusb/class/msc_host.c +++ b/tinyusb/class/msc_host.c @@ -407,7 +407,7 @@ void msch_isr(pipe_handle_t pipe_hdl, tusb_event_t event, uint32_t xferred_bytes tuh_msc_isr(pipe_hdl.dev_addr, event, xferred_bytes); }else { // still initializing under open subtask - ASSERT( TUSB_ERROR_NONE == osal_semaphore_post(msch_sem_hdl), VOID_RETURN ); + osal_semaphore_post(msch_sem_hdl); } } } diff --git a/tinyusb/device/usbd.c b/tinyusb/device/usbd.c index 46437e36f..bbe8290e1 100644 --- a/tinyusb/device/usbd.c +++ b/tinyusb/device/usbd.c @@ -441,7 +441,7 @@ void usbd_xfer_isr(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xfer { if (edpt_hdl.class_code == 0 ) // Control Transfer { - ASSERT( TUSB_ERROR_NONE == osal_semaphore_post( usbd_control_xfer_sem_hdl ), VOID_RETURN); + osal_semaphore_post( usbd_control_xfer_sem_hdl ); }else { usbd_task_event_t task_event = diff --git a/tinyusb/host/usbh.c b/tinyusb/host/usbh.c index 8ab25a19a..e1fb320bd 100644 --- a/tinyusb/host/usbh.c +++ b/tinyusb/host/usbh.c @@ -253,7 +253,7 @@ void usbh_xfer_isr(pipe_handle_t pipe_hdl, uint8_t class_code, tusb_event_t even { usbh_devices[ pipe_hdl.dev_addr ].control.pipe_status = event; // usbh_devices[ pipe_hdl.dev_addr ].control.xferred_bytes = xferred_bytes; not yet neccessary - ASSERT( TUSB_ERROR_NONE == osal_semaphore_post( usbh_devices[ pipe_hdl.dev_addr ].control.sem_hdl ), VOID_RETURN); + osal_semaphore_post( usbh_devices[ pipe_hdl.dev_addr ].control.sem_hdl ); }else if (usbh_class_drivers[class_index].isr) { usbh_class_drivers[class_index].isr(pipe_hdl, event, xferred_bytes); diff --git a/tinyusb/osal/osal_freeRTOS.h b/tinyusb/osal/osal_freeRTOS.h index 0e043ded6..da68a5059 100644 --- a/tinyusb/osal/osal_freeRTOS.h +++ b/tinyusb/osal/osal_freeRTOS.h @@ -79,59 +79,6 @@ static inline void osal_task_delay(uint32_t msec) vTaskDelay( (TUSB_CFG_TICKS_HZ * msec) / 1000 ); } -//--------------------------------------------------------------------+ -// Semaphore API -//--------------------------------------------------------------------+ -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() - -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_t sem_hdl) -{ - return (xSemaphoreGive(sem_hdl) == pdTRUE) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_SEMAPHORE_FAILED; -} - -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_t const sem_hdl) -{ - (void) xSemaphoreTake(sem_hdl, 0); -} - -//--------------------------------------------------------------------+ -// MUTEX API (priority inheritance) -//--------------------------------------------------------------------+ -typedef xSemaphoreHandle osal_mutex_t; - -#define osal_mutex_create(x) xSemaphoreCreateMutex() - -static inline tusb_error_t osal_mutex_release(osal_mutex_t mutex_hdl) -{ - return osal_semaphore_post(mutex_hdl); -} - -static inline void osal_mutex_wait(osal_mutex_t mutex_hdl, uint32_t msec, tusb_error_t *p_error) -{ - osal_semaphore_wait(mutex_hdl, msec, p_error); -} - -// TOOD remove -static inline void osal_mutex_reset(osal_mutex_t mutex_hdl) -{ - xSemaphoreGive(mutex_hdl); -} - //--------------------------------------------------------------------+ // QUEUE API //--------------------------------------------------------------------+ @@ -166,6 +113,64 @@ static inline void osal_queue_flush(osal_queue_t const queue_hdl) xQueueReset(queue_hdl); } +//--------------------------------------------------------------------+ +// Semaphore API +//--------------------------------------------------------------------+ +typedef xSemaphoreHandle osal_semaphore_t; + +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 bool osal_semaphore_post(osal_semaphore_t sem_hdl) +{ + if (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) + { + return xSemaphoreGive(sem_hdl); + }else + { + return xSemaphoreGiveFromISR(sem_hdl, NULL); + } +} + +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_t const sem_hdl) +{ + (void) xSemaphoreTake(sem_hdl, 0); +} + +//--------------------------------------------------------------------+ +// MUTEX API (priority inheritance) +//--------------------------------------------------------------------+ +typedef xSemaphoreHandle osal_mutex_t; + +#define osal_mutex_create(x) xSemaphoreCreateMutex() + +static inline bool osal_mutex_release(osal_mutex_t mutex_hdl) +{ + return osal_semaphore_post(mutex_hdl); +} + +static inline void osal_mutex_wait(osal_mutex_t mutex_hdl, uint32_t msec, tusb_error_t *p_error) +{ + osal_semaphore_wait(mutex_hdl, msec, p_error); +} + +// TOOD remove +static inline void osal_mutex_reset(osal_mutex_t mutex_hdl) +{ + xSemaphoreGive(mutex_hdl); +} + + + #ifdef __cplusplus } #endif diff --git a/tinyusb/osal/osal_none.h b/tinyusb/osal/osal_none.h index bf0a82d8d..469722ec7 100644 --- a/tinyusb/osal/osal_none.h +++ b/tinyusb/osal/osal_none.h @@ -150,79 +150,6 @@ static inline osal_task_t osal_task_create(osal_func_t code, const char* name, u ASSERT_DEFINE_WITH_HANDLER(_SUBTASK_ASSERT_ERROR_HANDLER, func_call, ,\ condition, TUSB_ERROR_OSAL_TASK_FAILED, "%s", "evaluated to false") -//--------------------------------------------------------------------+ -// Semaphore API -//--------------------------------------------------------------------+ -typedef struct -{ - 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_t 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_t sem_hdl) -{ - 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->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{\ - if (sem_hdl->count) sem_hdl->count--; /*TODO mutex hal_interrupt_disable consideration*/\ - *(p_error) = TUSB_ERROR_NONE;\ - }\ - }while(0) - -//--------------------------------------------------------------------+ -// MUTEX API (priority inheritance) -//--------------------------------------------------------------------+ -typedef osal_semaphore_t osal_mutex_t; - -static inline osal_mutex_t osal_mutex_create(void) -{ - return osal_semaphore_create(1, 0); -} - -static inline tusb_error_t osal_mutex_release(osal_mutex_t mutex_hdl) -{ - return osal_semaphore_post(mutex_hdl); -} - -// TOOD remove -static inline void osal_mutex_reset(osal_mutex_t mutex_hdl) -{ - osal_semaphore_reset(mutex_hdl); -} - -#define osal_mutex_wait osal_semaphore_wait - //--------------------------------------------------------------------+ // QUEUE API //--------------------------------------------------------------------+ @@ -273,6 +200,81 @@ static inline void osal_queue_flush(osal_queue_t const queue_hdl) }\ }while(0) + +//--------------------------------------------------------------------+ +// Semaphore API +//--------------------------------------------------------------------+ +typedef struct +{ + 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 bool osal_semaphore_post(osal_semaphore_t sem_hdl) +{ + if (sem_hdl->count < sem_hdl->max_count ) sem_hdl->count++; + return true; +} + +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 = osal_tick_get();\ + state = __LINE__; case __LINE__:\ + 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{\ + if (sem_hdl->count) sem_hdl->count--; /*TODO mutex hal_interrupt_disable consideration*/\ + *(p_error) = TUSB_ERROR_NONE;\ + }\ + }while(0) + +//--------------------------------------------------------------------+ +// MUTEX API (priority inheritance) +//--------------------------------------------------------------------+ +typedef osal_semaphore_t osal_mutex_t; + +static inline osal_mutex_t osal_mutex_create(void) +{ + return osal_semaphore_create(1, 0); +} + +static inline bool osal_mutex_release(osal_mutex_t mutex_hdl) +{ + return osal_semaphore_post(mutex_hdl); +} + +// TOOD remove +static inline void osal_mutex_reset(osal_mutex_t mutex_hdl) +{ + osal_semaphore_reset(mutex_hdl); +} + +#define osal_mutex_wait osal_semaphore_wait + + + #ifdef __cplusplus } #endif