diff options
| author | hathach <[email protected]> | 2018-03-01 11:42:13 +0700 |
|---|---|---|
| committer | hathach <[email protected]> | 2018-03-01 11:42:13 +0700 |
| commit | 40935fc01ca33b6123c1626f930faf9b48e0b2b0 (patch) | |
| tree | be38b953e522cca805a51f6962d4916b49f51d80 /tinyusb/osal | |
| parent | 329fdc026c4536b218438577c90491bc0b69fff8 (diff) | |
more osal clean up
Diffstat (limited to 'tinyusb/osal')
| -rw-r--r-- | tinyusb/osal/osal_freeRTOS.h | 81 | ||||
| -rw-r--r-- | tinyusb/osal/osal_none.h | 106 |
2 files changed, 97 insertions, 90 deletions
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 @@ -80,22 +80,59 @@ static inline void osal_task_delay(uint32_t msec) } //--------------------------------------------------------------------+ +// QUEUE API +//--------------------------------------------------------------------+ +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); +} + +static inline void osal_queue_receive (osal_queue_t const queue_hdl, void *p_data, uint32_t msec, tusb_error_t *p_error) +{ + uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : osal_tick_from_msec(msec); + + portBASE_TYPE result = (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) ? xQueueReceiveFromISR(queue_hdl, p_data, NULL) : xQueueReceive(queue_hdl, p_data, ticks); + (*p_error) = ( result == pdPASS ) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT; +} + +static inline bool osal_queue_send(osal_queue_t const queue_hdl, void const * data) +{ + if (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) + { + return xQueueSendToFrontFromISR(queue_hdl, data, NULL); + }else + { + return xQueueSendToFront(queue_hdl, data, 0); + } +} + +static inline void osal_queue_flush(osal_queue_t const queue_hdl) +{ + xQueueReset(queue_hdl); +} + +//--------------------------------------------------------------------+ // 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) +static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl) { - return (xSemaphoreGive(sem_hdl) == pdTRUE) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_SEMAPHORE_FAILED; + 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) @@ -116,7 +153,7 @@ typedef xSemaphoreHandle osal_mutex_t; #define osal_mutex_create(x) xSemaphoreCreateMutex() -static inline tusb_error_t osal_mutex_release(osal_mutex_t mutex_hdl) +static inline bool osal_mutex_release(osal_mutex_t mutex_hdl) { return osal_semaphore_post(mutex_hdl); } @@ -132,39 +169,7 @@ static inline void osal_mutex_reset(osal_mutex_t mutex_hdl) xSemaphoreGive(mutex_hdl); } -//--------------------------------------------------------------------+ -// QUEUE API -//--------------------------------------------------------------------+ -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); -} - -static inline void osal_queue_receive (osal_queue_t const queue_hdl, void *p_data, uint32_t msec, tusb_error_t *p_error) -{ - uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : osal_tick_from_msec(msec); - - portBASE_TYPE result = (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) ? xQueueReceiveFromISR(queue_hdl, p_data, NULL) : xQueueReceive(queue_hdl, p_data, ticks); - (*p_error) = ( result == pdPASS ) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT; -} - -static inline bool osal_queue_send(osal_queue_t const queue_hdl, void const * data) -{ - if (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) - { - return xQueueSendToFrontFromISR(queue_hdl, data, NULL); - }else - { - return xQueueSendToFront(queue_hdl, data, 0); - } -} -static inline void osal_queue_flush(osal_queue_t const queue_hdl) -{ - xQueueReset(queue_hdl); -} #ifdef __cplusplus } 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 @@ -151,6 +151,57 @@ static inline osal_task_t osal_task_create(osal_func_t code, const char* name, u condition, TUSB_ERROR_OSAL_TASK_FAILED, "%s", "evaluated to false")
//--------------------------------------------------------------------+
+// QUEUE API
+//--------------------------------------------------------------------+
+typedef fifo_t* osal_queue_t;
+
+static inline osal_queue_t osal_queue_create(uint32_t depth, uint32_t item_size)
+{
+ fifo_t* ff = (fifo_t* ) tu_malloc( sizeof(fifo_t) );
+ uint8_t* buf = (uint8_t*) tu_malloc( depth*item_size );
+
+ VERIFY( ff && buf, NULL);
+
+ *ff = (fifo_t) {
+ .buffer = buf, .depth = depth, .item_size = item_size,
+ .count = 0, .wr_idx =0, .rd_idx = 0, .overwritable = false
+ };
+
+ return (osal_queue_t) ff;
+}
+
+
+static inline bool osal_queue_send(osal_queue_t const queue_hdl, void const * data)
+{
+ return fifo_write( (fifo_t*) queue_hdl, data);
+}
+
+static inline void osal_queue_flush(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 = osal_tick_get();\
+ state = __LINE__; case __LINE__:\
+ if( queue_hdl->count == 0 ) {\
+ if ( (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{\
+ /*TODO mutex lock hal_interrupt_disable */\
+ memcpy(p_data, queue_hdl->buffer + (queue_hdl->rd_idx * queue_hdl->item_size), queue_hdl->item_size);\
+ queue_hdl->rd_idx = (queue_hdl->rd_idx + 1) % queue_hdl->depth;\
+ queue_hdl->count--;\
+ /*TODO mutex unlock hal_interrupt_enable */\
+ *(p_error) = TUSB_ERROR_NONE;\
+ }\
+ }while(0)
+
+
+//--------------------------------------------------------------------+
// Semaphore API
//--------------------------------------------------------------------+
typedef struct
@@ -173,11 +224,10 @@ static inline osal_semaphore_t osal_semaphore_create(uint32_t max_count, uint32_ return sem_data;
}
-static inline tusb_error_t osal_semaphore_post(osal_semaphore_t sem_hdl)
+static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl)
{
if (sem_hdl->count < sem_hdl->max_count ) sem_hdl->count++;
-
- return TUSB_ERROR_NONE;
+ return true;
}
static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl)
@@ -210,7 +260,7 @@ 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)
+static inline bool osal_mutex_release(osal_mutex_t mutex_hdl)
{
return osal_semaphore_post(mutex_hdl);
}
@@ -223,55 +273,7 @@ static inline void osal_mutex_reset(osal_mutex_t mutex_hdl) #define osal_mutex_wait osal_semaphore_wait
-//--------------------------------------------------------------------+
-// QUEUE API
-//--------------------------------------------------------------------+
-typedef fifo_t* osal_queue_t;
-
-static inline osal_queue_t osal_queue_create(uint32_t depth, uint32_t item_size)
-{
- fifo_t* ff = (fifo_t* ) tu_malloc( sizeof(fifo_t) );
- uint8_t* buf = (uint8_t*) tu_malloc( depth*item_size );
-
- VERIFY( ff && buf, NULL);
-
- *ff = (fifo_t) {
- .buffer = buf, .depth = depth, .item_size = item_size,
- .count = 0, .wr_idx =0, .rd_idx = 0, .overwritable = false
- };
-
- return (osal_queue_t) ff;
-}
-
-
-static inline bool osal_queue_send(osal_queue_t const queue_hdl, void const * data)
-{
- return fifo_write( (fifo_t*) queue_hdl, data);
-}
-
-static inline void osal_queue_flush(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 = osal_tick_get();\
- state = __LINE__; case __LINE__:\
- if( queue_hdl->count == 0 ) {\
- if ( (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{\
- /*TODO mutex lock hal_interrupt_disable */\
- memcpy(p_data, queue_hdl->buffer + (queue_hdl->rd_idx * queue_hdl->item_size), queue_hdl->item_size);\
- queue_hdl->rd_idx = (queue_hdl->rd_idx + 1) % queue_hdl->depth;\
- queue_hdl->count--;\
- /*TODO mutex unlock hal_interrupt_enable */\
- *(p_error) = TUSB_ERROR_NONE;\
- }\
- }while(0)
#ifdef __cplusplus
}
|
