| 
									
										
										
										
											2013-05-06 12:50:19 +07:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*!
 | 
					
						
							|  |  |  |     @file     osal_none.h | 
					
						
							|  |  |  |     @author   hathach (tinyusb.org) | 
					
						
							| 
									
										
										
										
											2013-01-22 17:41:06 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-06 12:50:19 +07:00
										 |  |  |     @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. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							| 
									
										
										
										
											2013-01-22 17:41:06 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** \file
 | 
					
						
							|  |  |  |  *  \brief TBD | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  \note TBD | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \ingroup TBD
 | 
					
						
							|  |  |  |  *  \defgroup TBD | 
					
						
							|  |  |  |  *  \brief TBD | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  @{ | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef _TUSB_OSAL_NONE_H_
 | 
					
						
							|  |  |  | #define _TUSB_OSAL_NONE_H_
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-23 15:35:27 +07:00
										 |  |  | #include "osal_common.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-22 17:41:06 +07:00
										 |  |  | #ifdef __cplusplus
 | 
					
						
							|  |  |  |  extern "C" { | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-01 12:00:34 +07:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // TICK API
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2013-02-27 20:13:49 +07:00
										 |  |  | extern volatile uint32_t osal_tick_current; | 
					
						
							|  |  |  | static inline void osal_tick_tock(void) ATTR_ALWAYS_INLINE; | 
					
						
							|  |  |  | static inline void osal_tick_tock(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   osal_tick_current++; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-07 03:40:24 +07:00
										 |  |  | static inline volatile uint32_t osal_tick_get(void) ATTR_ALWAYS_INLINE; | 
					
						
							|  |  |  | static inline volatile uint32_t osal_tick_get(void) | 
					
						
							| 
									
										
										
										
											2013-02-27 20:13:49 +07:00
										 |  |  | { | 
					
						
							|  |  |  |   return osal_tick_current; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-01-30 11:35:37 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // TASK API
 | 
					
						
							|  |  |  | // NOTES: Each blocking OSAL_NONE services such as semaphore wait,
 | 
					
						
							|  |  |  | // queue receive embedded return statement, therefore local variable
 | 
					
						
							|  |  |  | // retain value before/after such services needed to declare as static
 | 
					
						
							|  |  |  | // OSAL_TASK_LOOP
 | 
					
						
							|  |  |  | // {
 | 
					
						
							|  |  |  | //   OSAL_TASK_LOOP_BEGIN
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //   task body statements
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //   OSAL_TASK_LOOP_ENG
 | 
					
						
							|  |  |  | // }
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2013-04-25 16:41:00 +07:00
										 |  |  | #define OSAL_TASK_DEF(variable, name, code, stack_depth, prio)
 | 
					
						
							| 
									
										
										
										
											2013-02-26 17:28:05 +07:00
										 |  |  | #define osal_task_create(x) TUSB_ERROR_NONE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-24 17:53:43 +07:00
										 |  |  | #define OSAL_TASK_FUNCTION(task_func) \
 | 
					
						
							|  |  |  |   tusb_error_t task_func | 
					
						
							| 
									
										
										
										
											2013-06-27 03:20:14 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-24 17:53:43 +07:00
										 |  |  | #define TASK_RESTART \
 | 
					
						
							|  |  |  |   state = 0 | 
					
						
							| 
									
										
										
										
											2013-01-30 12:55:44 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-02 15:36:20 +07:00
										 |  |  | #define OSAL_TASK_LOOP_BEGIN \
 | 
					
						
							| 
									
										
										
										
											2013-04-25 11:55:24 +07:00
										 |  |  |   ATTR_UNUSED static uint32_t timeout = 0;\ | 
					
						
							| 
									
										
										
										
											2013-01-30 11:35:37 +07:00
										 |  |  |   static uint16_t state = 0;\ | 
					
						
							| 
									
										
										
										
											2013-02-02 15:36:20 +07:00
										 |  |  |   switch(state) {\ | 
					
						
							|  |  |  |     case 0:\ | 
					
						
							| 
									
										
										
										
											2013-01-30 11:35:37 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define OSAL_TASK_LOOP_END \
 | 
					
						
							|  |  |  |   default:\ | 
					
						
							| 
									
										
										
										
											2013-02-26 17:28:05 +07:00
										 |  |  |     TASK_RESTART;\ | 
					
						
							|  |  |  |   }\ | 
					
						
							|  |  |  |   return TUSB_ERROR_NONE; | 
					
						
							| 
									
										
										
										
											2013-02-02 15:36:20 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 16:41:00 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define osal_task_delay(msec) \
 | 
					
						
							|  |  |  |   do {\ | 
					
						
							|  |  |  |     timeout = osal_tick_get();\ | 
					
						
							|  |  |  |     state = __LINE__; case __LINE__:\ | 
					
						
							| 
									
										
										
										
											2013-04-27 00:24:15 +07:00
										 |  |  |       if ( timeout + osal_tick_from_msec(msec) > osal_tick_get() ) /* time out */ \ | 
					
						
							| 
									
										
										
										
											2013-04-25 16:41:00 +07:00
										 |  |  |         return TUSB_ERROR_OSAL_WAITING;\ | 
					
						
							|  |  |  |   }while(0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-27 16:32:54 +07:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // SUBTASK (a sub function that uses OS blocking services & called by a task
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2013-02-26 17:28:05 +07:00
										 |  |  | //------------- Sub Task -------------//
 | 
					
						
							| 
									
										
										
										
											2013-04-24 17:53:43 +07:00
										 |  |  | #define OSAL_SUBTASK_INVOKED_AND_WAIT(subtask, status) \
 | 
					
						
							| 
									
										
										
										
											2013-02-26 17:28:05 +07:00
										 |  |  |   do {\ | 
					
						
							|  |  |  |     state = __LINE__; case __LINE__:\ | 
					
						
							|  |  |  |     {\ | 
					
						
							| 
									
										
										
										
											2013-04-24 17:53:43 +07:00
										 |  |  |       status = subtask; /* invoke sub task */\ | 
					
						
							| 
									
										
										
										
											2013-02-26 17:28:05 +07:00
										 |  |  |       if (TUSB_ERROR_OSAL_WAITING == status) /* sub task not finished -> continue waiting */\ | 
					
						
							|  |  |  |         return TUSB_ERROR_OSAL_WAITING;\ | 
					
						
							|  |  |  |     }\ | 
					
						
							| 
									
										
										
										
											2013-03-12 12:06:40 +07:00
										 |  |  |   }while(0) | 
					
						
							| 
									
										
										
										
											2013-02-01 12:00:34 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-24 12:20:56 +07:00
										 |  |  | #define OSAL_SUBTASK_BEGIN  OSAL_TASK_LOOP_BEGIN
 | 
					
						
							|  |  |  | #define OSAL_SUBTASK_END    OSAL_TASK_LOOP_END
 | 
					
						
							| 
									
										
										
										
											2013-02-26 17:28:05 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-24 17:53:43 +07:00
										 |  |  | //------------- Sub Task Assert -------------//
 | 
					
						
							| 
									
										
										
										
											2013-06-27 16:32:54 +07:00
										 |  |  | #define SUBTASK_EXIT(error)  \
 | 
					
						
							|  |  |  |     TASK_RESTART; return error | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-24 17:53:43 +07:00
										 |  |  | #define _SUBTASK_ASSERT_ERROR_HANDLER(error, func_call) \
 | 
					
						
							| 
									
										
										
										
											2013-02-26 17:28:05 +07:00
										 |  |  |   func_call; TASK_RESTART; return error | 
					
						
							| 
									
										
										
										
											2013-02-06 12:03:01 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-24 12:20:56 +07:00
										 |  |  | #define SUBTASK_ASSERT_STATUS(sts) \
 | 
					
						
							| 
									
										
										
										
											2013-04-24 17:53:43 +07:00
										 |  |  |     ASSERT_DEFINE_WITH_HANDLER(_SUBTASK_ASSERT_ERROR_HANDLER, , tusb_error_t status = (tusb_error_t)(sts),\ | 
					
						
							| 
									
										
										
										
											2013-02-26 17:28:05 +07:00
										 |  |  |                                TUSB_ERROR_NONE == status, status, "%s", TUSB_ErrorStr[status]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-24 12:20:56 +07:00
										 |  |  | #define SUBTASK_ASSERT_STATUS_WITH_HANDLER(sts, func_call) \
 | 
					
						
							| 
									
										
										
										
											2013-04-24 17:53:43 +07:00
										 |  |  |     ASSERT_DEFINE_WITH_HANDLER(_SUBTASK_ASSERT_ERROR_HANDLER, func_call, tusb_error_t status = (tusb_error_t)(sts),\ | 
					
						
							| 
									
										
										
										
											2013-02-26 17:28:05 +07:00
										 |  |  |                                TUSB_ERROR_NONE == status, status, "%s", TUSB_ErrorStr[status]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-27 03:20:14 +07:00
										 |  |  | // TODO allow to specify error return
 | 
					
						
							| 
									
										
										
										
											2013-04-24 12:20:56 +07:00
										 |  |  | #define SUBTASK_ASSERT(condition)  \
 | 
					
						
							| 
									
										
										
										
											2013-04-24 17:53:43 +07:00
										 |  |  |     ASSERT_DEFINE_WITH_HANDLER(_SUBTASK_ASSERT_ERROR_HANDLER, , , \ | 
					
						
							| 
									
										
										
										
											2013-02-26 17:28:05 +07:00
										 |  |  |                                (condition), TUSB_ERROR_OSAL_TASK_FAILED, "%s", "evaluated to false") | 
					
						
							| 
									
										
										
										
											2013-05-09 12:49:52 +07:00
										 |  |  | // TODO remove assert with handler by catching error in enum main task
 | 
					
						
							| 
									
										
										
										
											2013-04-24 12:20:56 +07:00
										 |  |  | #define SUBTASK_ASSERT_WITH_HANDLER(condition, func_call) \
 | 
					
						
							| 
									
										
										
										
											2013-04-24 17:53:43 +07:00
										 |  |  |     ASSERT_DEFINE_WITH_HANDLER(_SUBTASK_ASSERT_ERROR_HANDLER, func_call, ,\ | 
					
						
							| 
									
										
										
										
											2013-02-26 17:28:05 +07:00
										 |  |  |                                condition, TUSB_ERROR_OSAL_TASK_FAILED, "%s", "evaluated to false") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-30 11:35:37 +07:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // Semaphore API
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2013-01-30 23:29:27 +07:00
										 |  |  | typedef volatile uint8_t osal_semaphore_t; | 
					
						
							| 
									
										
										
										
											2013-01-30 11:35:37 +07:00
										 |  |  | typedef osal_semaphore_t * osal_semaphore_handle_t; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-04 16:07:42 +07:00
										 |  |  | #define OSAL_SEM_DEF(name)\
 | 
					
						
							|  |  |  |   osal_semaphore_t name | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define OSAL_SEM_REF(name)\
 | 
					
						
							|  |  |  |   &name | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-27 16:19:22 +07:00
										 |  |  | 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) | 
					
						
							| 
									
										
										
										
											2013-01-30 11:35:37 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-06-27 16:19:22 +07:00
										 |  |  |   (*p_sem) = 0; // TODO consider to have initial count parameter
 | 
					
						
							| 
									
										
										
										
											2013-01-30 17:53:37 +07:00
										 |  |  |   return (osal_semaphore_handle_t) p_sem; | 
					
						
							| 
									
										
										
										
											2013-01-30 11:35:37 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-27 16:19:22 +07:00
										 |  |  | 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) | 
					
						
							| 
									
										
										
										
											2013-01-30 11:35:37 +07:00
										 |  |  | { | 
					
						
							|  |  |  |   (*sem_hdl)++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return TUSB_ERROR_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-27 16:19:22 +07:00
										 |  |  | 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) | 
					
						
							| 
									
										
										
										
											2013-03-11 12:00:25 +07:00
										 |  |  | { | 
					
						
							|  |  |  |   (*sem_hdl) = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-01 10:33:43 +07:00
										 |  |  | #define osal_semaphore_wait(sem_hdl, msec, p_error) \
 | 
					
						
							| 
									
										
										
										
											2013-01-30 11:35:37 +07:00
										 |  |  |   do {\ | 
					
						
							| 
									
										
										
										
											2013-02-01 12:00:34 +07:00
										 |  |  |     timeout = osal_tick_get();\ | 
					
						
							| 
									
										
										
										
											2013-01-30 11:35:37 +07:00
										 |  |  |     state = __LINE__; case __LINE__:\ | 
					
						
							| 
									
										
										
										
											2013-02-01 12:00:34 +07:00
										 |  |  |     if( *(sem_hdl) == 0 ) {\ | 
					
						
							| 
									
										
										
										
											2013-02-06 12:03:01 +07:00
										 |  |  |       if ( (msec != OSAL_TIMEOUT_WAIT_FOREVER) && (timeout + osal_tick_from_msec(msec) < osal_tick_get()) ) /* time out */ \ | 
					
						
							| 
									
										
										
										
											2013-02-01 12:00:34 +07:00
										 |  |  |         *(p_error) = TUSB_ERROR_OSAL_TIMEOUT;\ | 
					
						
							|  |  |  |       else\ | 
					
						
							| 
									
										
										
										
											2013-02-26 17:28:05 +07:00
										 |  |  |         return TUSB_ERROR_OSAL_WAITING;\ | 
					
						
							| 
									
										
										
										
											2013-02-04 20:54:52 +07:00
										 |  |  |     } else{\ | 
					
						
							| 
									
										
										
										
											2013-02-01 12:00:34 +07:00
										 |  |  |       (*(sem_hdl))--; /*TODO mutex hal_interrupt_disable consideration*/\ | 
					
						
							| 
									
										
										
										
											2013-02-04 20:54:52 +07:00
										 |  |  |       *(p_error) = TUSB_ERROR_NONE;\ | 
					
						
							|  |  |  |     }\ | 
					
						
							| 
									
										
										
										
											2013-01-30 11:35:37 +07:00
										 |  |  |   }while(0) | 
					
						
							| 
									
										
										
										
											2013-01-23 14:57:12 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-22 17:41:06 +07:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2013-06-27 16:19:22 +07:00
										 |  |  | // MUTEX API (priority inheritance)
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | typedef osal_semaphore_t        osal_mutex_t; | 
					
						
							|  |  |  | typedef osal_semaphore_handle_t 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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   (*p_mutex) = 1; | 
					
						
							|  |  |  |   return (osal_mutex_handle_t) p_mutex; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   (*mutex_hdl) = 1; // mutex is a binary semaphore
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return TUSB_ERROR_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   (*mutex_hdl) = 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define osal_mutex_wait osal_semaphore_wait
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2013-01-22 17:41:06 +07:00
										 |  |  | // QUEUE API
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2013-01-26 01:37:15 +07:00
										 |  |  | typedef struct{ | 
					
						
							| 
									
										
										
										
											2013-04-10 01:13:31 +07:00
										 |  |  |            void *  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
 | 
					
						
							| 
									
										
										
										
											2013-01-26 01:37:15 +07:00
										 |  |  | } osal_queue_t; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-30 11:35:37 +07:00
										 |  |  | typedef osal_queue_t * osal_queue_handle_t; | 
					
						
							| 
									
										
										
										
											2013-01-29 11:06:30 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-30 17:53:37 +07:00
										 |  |  | // use to declare a queue, within the scope of tinyusb, should only use primitive type only
 | 
					
						
							| 
									
										
										
										
											2013-02-04 16:07:42 +07:00
										 |  |  | #define OSAL_QUEUE_DEF(name, queue_depth, type)\
 | 
					
						
							| 
									
										
										
										
											2013-05-21 17:57:21 +07:00
										 |  |  |   type name##_buffer[queue_depth];\ | 
					
						
							| 
									
										
										
										
											2013-01-30 17:53:37 +07:00
										 |  |  |   osal_queue_t name = {\ | 
					
						
							| 
									
										
										
										
											2013-04-10 01:13:31 +07:00
										 |  |  |       .buffer    = name##_buffer,\ | 
					
						
							|  |  |  |       .depth     = queue_depth,\ | 
					
						
							|  |  |  |       .item_size = sizeof(type)\ | 
					
						
							| 
									
										
										
										
											2013-01-30 17:53:37 +07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 11:55:24 +07:00
										 |  |  | static inline osal_queue_handle_t osal_queue_create(osal_queue_t * const p_queue) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE; | 
					
						
							| 
									
										
										
										
											2013-01-30 17:53:37 +07:00
										 |  |  | static inline osal_queue_handle_t osal_queue_create(osal_queue_t * const p_queue) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   p_queue->count = p_queue->wr_idx = p_queue->rd_idx = 0; | 
					
						
							|  |  |  |   return (osal_queue_handle_t) p_queue; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-21 17:57:21 +07:00
										 |  |  | // TODO move to osal_none.c
 | 
					
						
							| 
									
										
										
										
											2013-01-30 23:29:27 +07:00
										 |  |  | // when queue is full, it will overwrite the oldest data in the queue
 | 
					
						
							| 
									
										
										
										
											2013-04-27 00:24:15 +07:00
										 |  |  | static inline tusb_error_t osal_queue_send(osal_queue_handle_t const queue_hdl, void const * data) ATTR_ALWAYS_INLINE; | 
					
						
							|  |  |  | static inline tusb_error_t osal_queue_send(osal_queue_handle_t const queue_hdl, void const * data) | 
					
						
							| 
									
										
										
										
											2013-01-30 17:53:37 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-01-30 23:29:27 +07:00
										 |  |  |   //TODO mutex lock hal_interrupt_disable
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-10 01:13:31 +07:00
										 |  |  |   memcpy( queue_hdl->buffer + (queue_hdl->wr_idx * queue_hdl->item_size), | 
					
						
							|  |  |  |           data, | 
					
						
							|  |  |  |           queue_hdl->item_size); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-30 17:53:37 +07:00
										 |  |  |   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++; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-30 23:29:27 +07:00
										 |  |  |   //TODO mutex unlock hal_interrupt_enable
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-30 17:53:37 +07:00
										 |  |  |   return TUSB_ERROR_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-11 12:00:25 +07:00
										 |  |  | 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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   queue_hdl->count = queue_hdl->rd_idx = queue_hdl->wr_idx = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-01 12:39:39 +07:00
										 |  |  | #define osal_queue_receive(queue_hdl, p_data, msec, p_error) \
 | 
					
						
							| 
									
										
										
										
											2013-01-30 23:29:27 +07:00
										 |  |  |   do {\ | 
					
						
							| 
									
										
										
										
											2013-02-01 12:39:39 +07:00
										 |  |  |     timeout = osal_tick_get();\ | 
					
						
							| 
									
										
										
										
											2013-01-30 23:29:27 +07:00
										 |  |  |     state = __LINE__; case __LINE__:\ | 
					
						
							| 
									
										
										
										
											2013-02-01 12:39:39 +07:00
										 |  |  |     if( queue_hdl-> count == 0 ) {\ | 
					
						
							| 
									
										
										
										
											2013-02-06 12:03:01 +07:00
										 |  |  |       if ( (msec != OSAL_TIMEOUT_WAIT_FOREVER) && ( timeout + osal_tick_from_msec(msec) < osal_tick_get() )) /* time out */ \ | 
					
						
							| 
									
										
										
										
											2013-02-01 12:39:39 +07:00
										 |  |  |         *(p_error) = TUSB_ERROR_OSAL_TIMEOUT;\ | 
					
						
							|  |  |  |       else\ | 
					
						
							| 
									
										
										
										
											2013-02-26 17:28:05 +07:00
										 |  |  |         return TUSB_ERROR_OSAL_WAITING;\ | 
					
						
							| 
									
										
										
										
											2013-02-01 12:39:39 +07:00
										 |  |  |     } else{\ | 
					
						
							| 
									
										
										
										
											2013-01-30 23:29:27 +07:00
										 |  |  |       /*TODO mutex lock hal_interrupt_disable */\ | 
					
						
							| 
									
										
										
										
											2013-04-10 01:13:31 +07:00
										 |  |  |       memcpy(p_data, queue_hdl->buffer + (queue_hdl->rd_idx * queue_hdl->item_size), queue_hdl->item_size);\ | 
					
						
							| 
									
										
										
										
											2013-01-30 23:29:27 +07:00
										 |  |  |       queue_hdl->rd_idx = (queue_hdl->rd_idx + 1) % queue_hdl->depth;\ | 
					
						
							|  |  |  |       queue_hdl->count--;\ | 
					
						
							|  |  |  |       /*TODO mutex unlock hal_interrupt_enable */\ | 
					
						
							| 
									
										
										
										
											2013-02-04 20:54:52 +07:00
										 |  |  |       *(p_error) = TUSB_ERROR_NONE;\ | 
					
						
							| 
									
										
										
										
											2013-01-30 23:29:27 +07:00
										 |  |  |     }\ | 
					
						
							|  |  |  |   }while(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-30 11:35:37 +07:00
										 |  |  | // queue_send, queue_receive
 | 
					
						
							| 
									
										
										
										
											2013-01-22 17:41:06 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef __cplusplus
 | 
					
						
							|  |  |  |  } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif /* _TUSB_OSAL_NONE_H_ */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @} */ |