summaryrefslogtreecommitdiff
path: root/tinyusb/osal
diff options
context:
space:
mode:
authorhathach <[email protected]>2018-02-28 16:45:54 +0700
committerhathach <[email protected]>2018-02-28 16:45:54 +0700
commit30124b9b02e067c967aca642823f3ed4cb7f618d (patch)
tree5823eb3dfd498e85420d26b3924057bf272c42b4 /tinyusb/osal
parentc961bb47feee422746f5aa81e48b00a50a4cbf0e (diff)
refactor osal queue API
Diffstat (limited to 'tinyusb/osal')
-rw-r--r--tinyusb/osal/osal.h7
-rw-r--r--tinyusb/osal/osal_cmsis_rtx.h214
-rw-r--r--tinyusb/osal/osal_freeRTOS.h40
-rw-r--r--tinyusb/osal/osal_none.c81
-rw-r--r--tinyusb/osal/osal_none.h45
5 files changed, 42 insertions, 345 deletions
diff --git a/tinyusb/osal/osal.h b/tinyusb/osal/osal.h
index 4f2fdad22..a3c9b65bd 100644
--- a/tinyusb/osal/osal.h
+++ b/tinyusb/osal/osal.h
@@ -58,11 +58,16 @@
#ifndef _TEST_
+/*------------- Task -------------*/
typedef void (*osal_func_t)(void *param);
typedef void* osal_task_t;
static inline bool osal_task_create(osal_func_t code, const char* name, uint32_t stack_size, void* param, uint32_t prio, osal_task_t* task_hdl);
+/*------------- Queue -------------*/
+
+
+
#if TUSB_CFG_OS == TUSB_OS_NONE
#include "osal_none.h"
@@ -180,8 +185,6 @@ typedef struct{
typedef osal_queue_t * osal_queue_handle_t;
-#define OSAL_QUEUE_DEF(name, queue_depth, type) osal_queue_t name
-#define OSAL_QUEUE_REF(name) (&name)
osal_queue_handle_t osal_queue_create (osal_queue_t *p_queue);
void osal_queue_receive (osal_queue_handle_t const queue_hdl, void *p_data, uint32_t msec, tusb_error_t *p_error);
diff --git a/tinyusb/osal/osal_cmsis_rtx.h b/tinyusb/osal/osal_cmsis_rtx.h
deleted file mode 100644
index 62a1ec4ff..000000000
--- a/tinyusb/osal/osal_cmsis_rtx.h
+++ /dev/null
@@ -1,214 +0,0 @@
-/**************************************************************************/
-/*!
- @file osal_cmsis_rtx.h
- @author hathach (tinyusb.org)
-
- @section LICENSE
-
- Software License Agreement (BSD License)
-
- Copyright (c) 2013, hathach (tinyusb.org)
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are met:
- 1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
- 3. Neither the name of the copyright holders nor the
- names of its contributors may be used to endorse or promote products
- derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
- This file is part of the tinyusb stack.
-*/
-/**************************************************************************/
-
-/** \ingroup group_osal
- * @{
- * \defgroup Group_CMSISRTX CMSIS-RTOS RTX
- * @{ */
-
-#ifndef _TUSB_OSAL_CMSIS_RTX_H_
-#define _TUSB_OSAL_CMSIS_RTX_H_
-
-#include "osal_common.h"
-#include "cmsis_os.h"
-
-#ifdef __cplusplus
- extern "C" {
-#endif
-
-//--------------------------------------------------------------------+
-// TICK API
-//--------------------------------------------------------------------+
-#define osal_tick_get osKernelSysTick
-
-//--------------------------------------------------------------------+
-// TASK API
-//--------------------------------------------------------------------+
-#define OSAL_TASK_FUNCTION(task_func, p_para) void task_func(void const * p_para)
-
-typedef osThreadDef_t osal_task_t;
-
-#define OSAL_TASK_DEF(task_code, task_stack_depth, task_prio) \
- osThreadDef(task_code, (osPriority) task_prio, 1, task_stack_depth*4) // stack depth is in bytes
-
-#define OSAL_TASK_REF(task_name) osThread(task_name)
-
-static inline tusb_error_t osal_task_create(const osal_task_t *task) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
-static inline tusb_error_t osal_task_create(const osal_task_t *task)
-{
- return ( osThreadCreate(task, NULL) != NULL ) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TASK_CREATE_FAILED;
-}
-
-#define osal_task_delay(msec) osDelay(msec)
-
-//--------------------------------------------------------------------+
-// Semaphore API
-//--------------------------------------------------------------------+
-typedef struct {
- uint32_t sem_cb[2];
-}osal_semaphore_t;
-
-typedef osSemaphoreId 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)
-{
- return osSemaphoreCreate( &(osSemaphoreDef_t) { p_sem }, 0 );
-}
-
-// 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)
-{
- return (osSemaphoreRelease(sem_hdl) == osOK) ? 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)
-{
- (*p_error) = ( osSemaphoreWait(sem_hdl, msec) > 0 ) ? 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)
-{
- osSemaphoreWait(sem_hdl, 0); // instant return without putting caller to suspend
-}
-
-//--------------------------------------------------------------------+
-// MUTEX API (priority inheritance)
-//--------------------------------------------------------------------+
-typedef struct {
- uint32_t mutex_cb[3];
-}osal_mutex_t;
-
-typedef osMutexId osal_mutex_handle_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)
-{
- return osMutexCreate( &(osMutexDef_t) {p_mutex} );
-}
-
-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)
-{
- return (osMutexRelease(mutex_hdl) == osOK) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_MUTEX_FAILED;
-}
-
-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)
-{
- (*p_error) = ( osMutexWait(mutex_hdl, msec) == osOK ) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT;
-}
-
-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)
-{
- osMutexRelease(mutex_hdl);
-}
-
-//--------------------------------------------------------------------+
-// QUEUE API (for RTX: osMailQId is osMessageQDef_t.pool)
-//--------------------------------------------------------------------+
-typedef osMailQDef_t osal_queue_t;
-typedef osal_queue_t * osal_queue_handle_t;
-
-#define OSAL_QUEUE_DEF(name, queue_depth, type)\
- osMailQDef(name, queue_depth, type);
-
-#define OSAL_QUEUE_REF(name) ((osal_queue_t*) osMailQ(name))
-
-static inline osal_queue_handle_t osal_queue_create(osal_queue_t * const p_queue) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
-static inline osal_queue_handle_t osal_queue_create(osal_queue_t * const p_queue)
-{
- return (NULL != osMailCreate( (osMailQDef_t const *) p_queue, NULL)) ? p_queue : NULL;
-}
-
-static inline void osal_queue_receive (osal_queue_handle_t const queue_hdl, void *p_data, uint32_t msec, tusb_error_t *p_error) ATTR_ALWAYS_INLINE;
-static inline void osal_queue_receive (osal_queue_handle_t const queue_hdl, void *p_data, uint32_t msec, tusb_error_t *p_error)
-{
- osEvent evt = osMailGet(queue_hdl->pool, msec);
-
- if (evt.status != osEventMail)
- {
- (*p_error) = TUSB_ERROR_OSAL_TIMEOUT;
- }else
- {
- memcpy(p_data, evt.value.p, queue_hdl->item_sz);
- osMailFree(queue_hdl->pool, evt.value.p);
- (*p_error) = TUSB_ERROR_NONE;
- }
-}
-
-static inline tusb_error_t osal_queue_send(osal_queue_handle_t const queue_hdl, void const * data) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
-static inline tusb_error_t osal_queue_send(osal_queue_handle_t const queue_hdl, void const * data)
-{
- void *p_buf = osMailAlloc(queue_hdl->pool, 0); // instantly return in case of sending within ISR (mostly used)
- if (p_buf == NULL) return TUSB_ERROR_OSAL_QUEUE_FAILED;
-
- memcpy(p_buf, data, queue_hdl->item_sz);
- osMailPut(queue_hdl->pool, p_buf);
- return TUSB_ERROR_NONE;
-}
-
-static inline void osal_queue_flush(osal_queue_handle_t const queue_hdl) ATTR_ALWAYS_INLINE;
-static inline void osal_queue_flush(osal_queue_handle_t const queue_hdl)
-{
- osEvent evt;
-
- while( (evt = osMailGet(queue_hdl->pool, 0) ).status == osEventMail )
- {
- osMailFree(queue_hdl->pool, evt.value.p);
- }
-}
-
-#ifdef __cplusplus
- }
-#endif
-
-#endif /* _TUSB_OSAL_CMSIS_RTX_H_ */
-
-/** @} */
-/** @} */
diff --git a/tinyusb/osal/osal_freeRTOS.h b/tinyusb/osal/osal_freeRTOS.h
index d8d42c033..379f42bb4 100644
--- a/tinyusb/osal/osal_freeRTOS.h
+++ b/tinyusb/osal/osal_freeRTOS.h
@@ -135,38 +135,34 @@ static inline void osal_mutex_reset(osal_mutex_handle_t const mutex_hdl)
//--------------------------------------------------------------------+
// QUEUE API
//--------------------------------------------------------------------+
-typedef struct{
- uint8_t const depth; ///< buffer size
- uint8_t const item_size; ///< size of each item
-} osal_queue_t;
+typedef xQueueHandle osal_queue_t;
-typedef xQueueHandle osal_queue_handle_t;
-#define OSAL_QUEUE_DEF(name, queue_depth, type)\
- osal_queue_t name = {\
- .depth = queue_depth,\
- .item_size = sizeof(type)\
- }
-
-#define OSAL_QUEUE_REF(name) (&name)
-
-#define osal_queue_create(p_queue) xQueueCreate((p_queue)->depth, (p_queue)->item_size)
+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_handle_t const queue_hdl, void *p_data, uint32_t msec, tusb_error_t *p_error) ATTR_ALWAYS_INLINE;
-static inline void osal_queue_receive (osal_queue_handle_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 *p_error)
{
uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : osal_tick_from_msec(msec);
- (*p_error) = ( xQueueReceive(queue_hdl, p_data, ticks) == pdPASS ) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT;
+
+ 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 tusb_error_t osal_queue_send(osal_queue_handle_t const queue_hdl, void const * data) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
-static inline tusb_error_t osal_queue_send(osal_queue_handle_t const queue_hdl, void const * data)
+static inline bool osal_queue_send(osal_queue_t const queue_hdl, void const * data)
{
- return ( xQueueSend(queue_hdl, data, 0) == pdTRUE ) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_QUEUE_FAILED;
+ 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_handle_t const queue_hdl) ATTR_ALWAYS_INLINE;
-static inline void osal_queue_flush(osal_queue_handle_t const queue_hdl)
+static inline void osal_queue_flush(osal_queue_t const queue_hdl)
{
xQueueReset(queue_hdl);
}
diff --git a/tinyusb/osal/osal_none.c b/tinyusb/osal/osal_none.c
deleted file mode 100644
index 263a90822..000000000
--- a/tinyusb/osal/osal_none.c
+++ /dev/null
@@ -1,81 +0,0 @@
-/**************************************************************************/
-/*!
- @file osal_none.c
- @author hathach (tinyusb.org)
-
- @section LICENSE
-
- Software License Agreement (BSD License)
-
- Copyright (c) 2013, hathach (tinyusb.org)
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are met:
- 1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
- 3. Neither the name of the copyright holders nor the
- names of its contributors may be used to endorse or promote products
- derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND
- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
- This file is part of the tinyusb stack.
-*/
-/**************************************************************************/
-
-#include "tusb_option.h"
-#include "osal.h"
-
-#if TUSB_CFG_OS == TUSB_OS_NONE
-
-#define _TINY_USB_SOURCE_FILE_
-
-//--------------------------------------------------------------------+
-// INCLUDE
-//--------------------------------------------------------------------+
-#include "osal_none.h"
-
-//--------------------------------------------------------------------+
-// INTERNAL OBJECT & FUNCTION DECLARATION
-//--------------------------------------------------------------------+
-
-//--------------------------------------------------------------------+
-// QUEUE API
-//--------------------------------------------------------------------+
-// when queue is full, it will overwrite the oldest data in the queue
-tusb_error_t osal_queue_send(osal_queue_handle_t const queue_hdl, void const * data)
-{
- //TODO mutex lock hal_interrupt_disable
- memcpy( queue_hdl->buffer + (queue_hdl->wr_idx * queue_hdl->item_size),
- data,
- queue_hdl->item_size);
-
- queue_hdl->wr_idx = (queue_hdl->wr_idx + 1) % queue_hdl->depth;
-
- if (queue_hdl->depth == queue_hdl->count) // queue is full, 1st rd is overwritten
- {
- queue_hdl->rd_idx = queue_hdl->wr_idx; // keep full state
- }else
- {
- queue_hdl->count++;
- }
-
- //TODO mutex unlock hal_interrupt_enable
-
- return TUSB_ERROR_NONE;
-}
-
-#endif
diff --git a/tinyusb/osal/osal_none.h b/tinyusb/osal/osal_none.h
index d7302d2cf..1a833b07d 100644
--- a/tinyusb/osal/osal_none.h
+++ b/tinyusb/osal/osal_none.h
@@ -44,6 +44,7 @@
#define _TUSB_OSAL_NONE_H_
#include "osal_common.h"
+#include "common/fifo.h"
#ifdef __cplusplus
extern "C" {
@@ -225,38 +226,30 @@ static inline void osal_mutex_reset(osal_mutex_handle_t mutex_hdl)
//--------------------------------------------------------------------+
// QUEUE API
//--------------------------------------------------------------------+
-typedef struct{
- uint8_t* const buffer ; ///< buffer pointer
- uint8_t const depth ; ///< max items
- uint8_t const item_size ; ///< size of each item
- volatile uint8_t count ; ///< number of items in queue
- volatile uint8_t wr_idx ; ///< write pointer
- volatile uint8_t rd_idx ; ///< read pointer
-} osal_queue_t;
+typedef fifo_t* osal_queue_t;
-typedef osal_queue_t * osal_queue_handle_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);
-// use to declare a queue, within the scope of tinyusb, should only use primitive type only
-#define OSAL_QUEUE_DEF(name, queue_depth, type)\
- STATIC_ASSERT(queue_depth < 256, "OSAL Queue only support up to 255 depth");\
- type name##_buffer[queue_depth];\
- osal_queue_t name = {\
- .buffer = (uint8_t*) name##_buffer,\
- .depth = queue_depth,\
- .item_size = sizeof(type)\
- }
+ *ff = (fifo_t) {
+ .buffer = buf, .depth = depth, .item_size = item_size,
+ .count = 0, .wr_idx =0, .rd_idx = 0, .overwritable = false
+ };
-#define OSAL_QUEUE_REF(name) (&name)
+ return (osal_queue_t) ff;
+}
-static inline osal_queue_handle_t osal_queue_create(osal_queue_t * const p_queue) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
-static inline osal_queue_handle_t osal_queue_create(osal_queue_t * const p_queue)
+
+static inline bool osal_queue_send(osal_queue_t const queue_hdl, void const * data)
{
- p_queue->count = p_queue->wr_idx = p_queue->rd_idx = 0;
- return (osal_queue_handle_t) p_queue;
+ return fifo_write( (fifo_t*) queue_hdl, data);
}
-tusb_error_t osal_queue_send(osal_queue_handle_t const queue_hdl, void const * data);
-static inline void osal_queue_flush(osal_queue_handle_t const queue_hdl) ATTR_ALWAYS_INLINE;
-static inline void osal_queue_flush(osal_queue_handle_t const queue_hdl)
+
+static inline void osal_queue_flush(osal_queue_t const queue_hdl)
{
queue_hdl->count = queue_hdl->rd_idx = queue_hdl->wr_idx = 0;
}