From 30124b9b02e067c967aca642823f3ed4cb7f618d Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 28 Feb 2018 16:45:54 +0700 Subject: [PATCH] refactor osal queue API --- tinyusb/common/common.h | 6 + tinyusb/common/fifo.c | 8 +- tinyusb/common/fifo.h | 20 ++-- tinyusb/device/usbd.c | 9 +- tinyusb/host/usbh.c | 5 +- tinyusb/osal/osal.h | 7 +- tinyusb/osal/osal_cmsis_rtx.h | 214 ---------------------------------- tinyusb/osal/osal_freeRTOS.h | 40 +++---- tinyusb/osal/osal_none.c | 81 ------------- tinyusb/osal/osal_none.h | 51 ++++---- tinyusb/tusb_option.h | 9 ++ 11 files changed, 81 insertions(+), 369 deletions(-) delete mode 100644 tinyusb/osal/osal_cmsis_rtx.h delete mode 100644 tinyusb/osal/osal_none.c diff --git a/tinyusb/common/common.h b/tinyusb/common/common.h index 6481b33ad..a624b941f 100644 --- a/tinyusb/common/common.h +++ b/tinyusb/common/common.h @@ -109,6 +109,12 @@ #define memclr_(buffer, size) memset((buffer), 0, (size)) +#define memclr(buffer, size) memset(buffer, 0, size) +#define varclr(_var) memclr(_var, sizeof(*(_var))) +#define arrclr(_arr) memclr(_arr, sizeof(_arr)) + +#define arrcount(_arr) ( sizeof(_arr) / sizeof(_arr[0]) ) + static inline uint8_t const * descriptor_next(uint8_t const p_desc[]) ATTR_ALWAYS_INLINE ATTR_PURE; static inline uint8_t const * descriptor_next(uint8_t const p_desc[]) { diff --git a/tinyusb/common/fifo.c b/tinyusb/common/fifo.c index dd7a66298..8db3f04ac 100644 --- a/tinyusb/common/fifo.c +++ b/tinyusb/common/fifo.c @@ -83,7 +83,7 @@ static inline bool fifo_initalized(fifo_t* f) bool fifo_read(fifo_t* f, void * p_buffer) { if( !fifo_initalized(f) ) return false; - if( fifo_is_empty(f) ) return false; + if( fifo_empty(f) ) return false; mutex_lock_if_needed(f); @@ -117,7 +117,7 @@ bool fifo_read(fifo_t* f, void * p_buffer) uint16_t fifo_read_n (fifo_t* f, void * p_buffer, uint16_t count) { if( !fifo_initalized(f) ) return false; - if( fifo_is_empty(f) ) return false; + if( fifo_empty(f) ) return false; /* Limit up to fifo's count */ count = min16_of(count, f->count); @@ -192,7 +192,7 @@ bool fifo_peek_at(fifo_t* f, uint16_t position, void * p_buffer) bool fifo_write(fifo_t* f, void const * p_data) { if ( !fifo_initalized(f) ) return false; - if ( fifo_is_full(f) && !f->overwritable ) return false; + if ( fifo_full(f) && !f->overwritable ) return false; mutex_lock_if_needed(f); @@ -202,7 +202,7 @@ bool fifo_write(fifo_t* f, void const * p_data) f->wr_idx = (f->wr_idx + 1) % f->depth; - if (fifo_is_full(f)) + if (fifo_full(f)) { f->rd_idx = f->wr_idx; // keep the full state (rd == wr && len = size) } diff --git a/tinyusb/common/fifo.h b/tinyusb/common/fifo.h index f069c54af..e162b5d5e 100644 --- a/tinyusb/common/fifo.h +++ b/tinyusb/common/fifo.h @@ -78,13 +78,15 @@ */ typedef struct { - uint8_t* const buffer ; ///< buffer pointer - uint16_t const depth ; ///< max items - uint16_t const item_size ; ///< size of each item - volatile uint16_t count ; ///< number of items in queue - volatile uint16_t wr_idx ; ///< write pointer - volatile uint16_t rd_idx ; ///< read pointer - bool overwritable ; + uint8_t* buffer ; ///< buffer pointer + uint16_t depth ; ///< max items + uint16_t item_size ; ///< size of each item + + volatile uint16_t count ; ///< number of items in queue + volatile uint16_t wr_idx ; ///< write pointer + volatile uint16_t rd_idx ; ///< read pointer + + bool overwritable ; #if CFG_FIFO_MUTEX fifo_mutex_t * const mutex; @@ -118,12 +120,12 @@ static inline bool fifo_peek(fifo_t* f, void * p_buffer) return fifo_peek_at(f, 0, p_buffer); } -static inline bool fifo_is_empty(fifo_t* f) +static inline bool fifo_empty(fifo_t* f) { return (f->count == 0); } -static inline bool fifo_is_full(fifo_t* f) +static inline bool fifo_full(fifo_t* f) { return (f->count == f->depth); } diff --git a/tinyusb/device/usbd.c b/tinyusb/device/usbd.c index 23b178513..c4552c3fc 100644 --- a/tinyusb/device/usbd.c +++ b/tinyusb/device/usbd.c @@ -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_QUEUE_DEF(usbd_queue_def, USBD_TASK_QUEUE_DEPTH, usbd_task_event_t); OSAL_SEM_DEF(usbd_control_xfer_semaphore_def); -static osal_queue_handle_t usbd_queue_hdl; +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 //--------------------------------------------------------------------+ @@ -164,7 +163,7 @@ tusb_error_t usbd_init (void) ASSERT_STATUS ( dcd_init() ); //------------- Task init -------------// - usbd_queue_hdl = osal_queue_create( OSAL_QUEUE_REF(usbd_queue_def) ); + 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) ); @@ -437,7 +436,7 @@ void usbd_setup_received_isr(uint8_t coreid, tusb_control_request_t * p_request) }; task_event.setup_received = (*p_request); - ASSERT( TUSB_ERROR_NONE == osal_queue_send(usbd_queue_hdl, &task_event), VOID_RETURN); + VERIFY( osal_queue_send(usbd_queue_hdl, &task_event), ); } void usbd_xfer_isr(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xferred_bytes) @@ -457,7 +456,7 @@ void usbd_xfer_isr(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xfer task_event.xfer_done.xferred_byte = xferred_bytes; task_event.xfer_done.edpt_hdl = edpt_hdl; - ASSERT( TUSB_ERROR_NONE == osal_queue_send(usbd_queue_hdl, &task_event), VOID_RETURN); + VERIFY( osal_queue_send(usbd_queue_hdl, &task_event), ); } } diff --git a/tinyusb/host/usbh.c b/tinyusb/host/usbh.c index 37d772bdb..8d19f5f15 100644 --- a/tinyusb/host/usbh.c +++ b/tinyusb/host/usbh.c @@ -109,9 +109,8 @@ TUSB_CFG_ATTR_USBRAM usbh_device_info_t usbh_devices[TUSB_CFG_HOST_DEVICE_MAX+1] //------------- Enumeration Task Data -------------/ enum { ENUM_QUEUE_DEPTH = 16 }; -OSAL_QUEUE_DEF(enum_queue_def, ENUM_QUEUE_DEPTH, uint32_t); -STATIC_VAR osal_queue_handle_t enum_queue_hdl; +STATIC_VAR osal_queue_t enum_queue_hdl; TUSB_CFG_ATTR_USBRAM ATTR_ALIGNED(4) STATIC_VAR uint8_t enum_data_buffer[TUSB_CFG_HOST_ENUM_BUFFER_SIZE]; //------------- Reporter Task Data -------------// @@ -144,7 +143,7 @@ tusb_error_t usbh_init(void) ASSERT_STATUS( hcd_init() ); //------------- Enumeration & Reporter Task init -------------// - enum_queue_hdl = osal_queue_create( OSAL_QUEUE_REF(enum_queue_def) ); + enum_queue_hdl = osal_queue_create( ENUM_QUEUE_DEPTH, sizeof(uint32_t) ); ASSERT_PTR(enum_queue_hdl, TUSB_ERROR_OSAL_QUEUE_FAILED); osal_task_create(usbh_enumeration_task, "usbh", 200, NULL, TUSB_CFG_OS_TASK_PRIO, NULL); 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)\ - } +static inline osal_queue_t osal_queue_create(uint32_t depth, uint32_t item_size) +{ + return xQueueCreate(depth, item_size); +} -#define OSAL_QUEUE_REF(name) (&name) - -#define osal_queue_create(p_queue) xQueueCreate((p_queue)->depth, (p_queue)->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; - -// 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)\ - } - -#define OSAL_QUEUE_REF(name) (&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) +static inline osal_queue_t osal_queue_create(uint32_t depth, uint32_t item_size) { - p_queue->count = p_queue->wr_idx = p_queue->rd_idx = 0; - return (osal_queue_handle_t) p_queue; + 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; } -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 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; } diff --git a/tinyusb/tusb_option.h b/tinyusb/tusb_option.h index b1fd46ef0..19b369fce 100644 --- a/tinyusb/tusb_option.h +++ b/tinyusb/tusb_option.h @@ -131,6 +131,15 @@ // #warning TUSB_CFG_CONFIGURATION_MAX is not defined, default value is 1 //#endif +#ifndef tu_malloc +#include +#define tu_malloc malloc +#endif + +#ifndef tu_free +#define tu_free free +#endif + //--------------------------------------------------------------------+ // HOST OPTIONS //--------------------------------------------------------------------+