| 
									
										
										
										
											2013-01-22 17:41:06 +07:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * osal_none.h | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  Created on: Jan 19, 2013 | 
					
						
							|  |  |  |  *      Author: hathach | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Software License Agreement (BSD License) | 
					
						
							| 
									
										
										
										
											2013-01-27 13:50:40 +07:00
										 |  |  |  * Copyright (c) 2013, hathach (tinyusb.net) | 
					
						
							| 
									
										
										
										
											2013-01-22 17:41:06 +07:00
										 |  |  |  * 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. The name of the author may not be used to endorse or promote products | 
					
						
							|  |  |  |  *    derived from this software without specific prior written permission. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 tiny usb stack. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \file
 | 
					
						
							|  |  |  |  *  \brief TBD | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  \note TBD | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \ingroup TBD
 | 
					
						
							|  |  |  |  *  \defgroup TBD | 
					
						
							|  |  |  |  *  \brief TBD | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  @{ | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef _TUSB_OSAL_NONE_H_
 | 
					
						
							|  |  |  | #define _TUSB_OSAL_NONE_H_
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef __cplusplus
 | 
					
						
							|  |  |  |  extern "C" { | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-30 11:35:37 +07:00
										 |  |  | #include "osal_common.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-01 12:00:34 +07:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // TICK API
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | void osal_tick_tock(void); | 
					
						
							|  |  |  | uint32_t osal_tick_get(void); | 
					
						
							| 
									
										
										
										
											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-01-31 13:11:50 +07:00
										 |  |  | //#define osal_task_create(code, name, stack_depth, parameters, prio)
 | 
					
						
							| 
									
										
										
										
											2013-01-30 12:55:44 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-02 15:36:20 +07:00
										 |  |  | #define OSAL_TASK_LOOP_BEGIN \
 | 
					
						
							| 
									
										
										
										
											2013-02-01 12:00:34 +07:00
										 |  |  |   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-02 15:36:20 +07:00
										 |  |  |     state = 0;\ | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-06 12:03:01 +07:00
										 |  |  | #define TASK_ASSERT_ERROR_HANDLER(error, func_call) \
 | 
					
						
							| 
									
										
										
										
											2013-02-06 13:15:12 +07:00
										 |  |  |   func_call; state = 0; return | 
					
						
							| 
									
										
										
										
											2013-02-01 12:00:34 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-06 12:03:01 +07:00
										 |  |  | #define TASK_ASSERT_STATUS_HANDLER(sts, func_call) \
 | 
					
						
							|  |  |  |     ASSERT_DEFINE_WITH_HANDLER(TASK_ASSERT_ERROR_HANDLER, func_call, tusb_error_t status = (tusb_error_t)(sts),\ | 
					
						
							|  |  |  |                                TUSB_ERROR_NONE == status, status, "%s", TUSB_ErrorStr[status]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define TASK_ASSERT(condition)  ASSERT_DEFINE_WITH_HANDLER(TASK_ASSERT_ERROR_HANDLER, , , (condition), (void) 0, "%s", "evaluated to false")
 | 
					
						
							| 
									
										
										
										
											2013-02-02 15:36:20 +07:00
										 |  |  | #define TASK_ASSERT_STATUS(sts) \
 | 
					
						
							| 
									
										
										
										
											2013-02-06 12:03:01 +07:00
										 |  |  |     ASSERT_DEFINE_WITH_HANDLER(TASK_ASSERT_ERROR_HANDLER, , tusb_error_t status = (tusb_error_t)(sts),\ | 
					
						
							| 
									
										
										
										
											2013-02-02 15:36:20 +07:00
										 |  |  |                   TUSB_ERROR_NONE == status, status, "%s", TUSB_ErrorStr[status]) | 
					
						
							| 
									
										
										
										
											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-01-30 17:53:37 +07:00
										 |  |  | static inline osal_semaphore_handle_t osal_semaphore_create(osal_semaphore_t * const p_sem) ATTR_ALWAYS_INLINE; | 
					
						
							|  |  |  | static inline osal_semaphore_handle_t osal_semaphore_create(osal_semaphore_t * const p_sem) | 
					
						
							| 
									
										
										
										
											2013-01-30 11:35:37 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-01-30 17:53:37 +07:00
										 |  |  |   (*p_sem) = 0; | 
					
						
							|  |  |  |   return (osal_semaphore_handle_t) p_sem; | 
					
						
							| 
									
										
										
										
											2013-01-30 11:35:37 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   (*sem_hdl)++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return TUSB_ERROR_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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\ | 
					
						
							|  |  |  |         return;\ | 
					
						
							| 
									
										
										
										
											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
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // QUEUE API
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2013-01-26 01:37:15 +07:00
										 |  |  | typedef struct{ | 
					
						
							| 
									
										
										
										
											2013-01-30 17:53:37 +07:00
										 |  |  |            uint32_t * const buffer     ; ///< buffer pointer
 | 
					
						
							| 
									
										
										
										
											2013-02-04 17:13:26 +07:00
										 |  |  |            uint8_t const depth        ; ///< buffer size
 | 
					
						
							|  |  |  |   volatile uint8_t count          ; ///< bytes in fifo
 | 
					
						
							|  |  |  |   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-01-30 17:53:37 +07:00
										 |  |  |   uint32_t name##_buffer[queue_depth];\ | 
					
						
							|  |  |  |   osal_queue_t name = {\ | 
					
						
							|  |  |  |       .buffer = name##_buffer,\ | 
					
						
							|  |  |  |       .depth   = queue_depth\ | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline osal_queue_handle_t osal_queue_create(osal_queue_t * const p_queue) ATTR_ALWAYS_INLINE; | 
					
						
							|  |  |  | 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-01-30 23:29:27 +07:00
										 |  |  | // when queue is full, it will overwrite the oldest data in the queue
 | 
					
						
							| 
									
										
										
										
											2013-01-30 17:53:37 +07:00
										 |  |  | static inline tusb_error_t osal_queue_send(osal_queue_handle_t const queue_hdl, uint32_t data) ATTR_ALWAYS_INLINE; | 
					
						
							|  |  |  | static inline tusb_error_t osal_queue_send(osal_queue_handle_t const queue_hdl, uint32_t data) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-01-30 23:29:27 +07:00
										 |  |  |   //TODO mutex lock hal_interrupt_disable
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-30 17:53:37 +07:00
										 |  |  |   queue_hdl->buffer[queue_hdl->wr_idx] = data; | 
					
						
							|  |  |  |   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-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\ | 
					
						
							|  |  |  |         return;\ | 
					
						
							|  |  |  |     } else{\ | 
					
						
							| 
									
										
										
										
											2013-01-30 23:29:27 +07:00
										 |  |  |       /*TODO mutex lock hal_interrupt_disable */\ | 
					
						
							|  |  |  |       *p_data = queue_hdl->buffer[queue_hdl->rd_idx];\ | 
					
						
							|  |  |  |       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_ */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @} */ |