From 5e8d70a184f01121ed9849452dc81ebe9600b86f Mon Sep 17 00:00:00 2001
From: hathach <thachha@live.com>
Date: Mon, 4 Feb 2013 18:05:22 +0700
Subject: [PATCH] complete define osal_freeRTOS for semaphore and queue

---
 tinyusb/common/errors.h      |  1 +
 tinyusb/host/usbh.h          |  3 ++-
 tinyusb/osal/osal_freeRTOS.h | 28 +++++++++++++++++++++++++++-
 3 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/tinyusb/common/errors.h b/tinyusb/common/errors.h
index 9839f42f4..2d8a80e22 100644
--- a/tinyusb/common/errors.h
+++ b/tinyusb/common/errors.h
@@ -69,6 +69,7 @@
     ENTRY(TUSB_ERROR_OSAL_TIMEOUT)\
     ENTRY(TUSB_ERROR_OSAL_TASK_FAILED)\
     ENTRY(TUSB_ERROR_OSAL_QUEUE_FAILED)\
+    ENTRY(TUSB_ERROR_OSAL_SEMAPHORE_FAILED)\
     ENTRY(TUSB_ERROR_FAILED)\
 
 
diff --git a/tinyusb/host/usbh.h b/tinyusb/host/usbh.h
index 9aa447d58..fcc43395f 100644
--- a/tinyusb/host/usbh.h
+++ b/tinyusb/host/usbh.h
@@ -130,12 +130,13 @@ void         tusbh_device_mounted_cb (tusb_error_t const error, tusb_handle_devi
 tusb_error_t tusbh_configuration_set     (tusb_handle_device_t const device_hdl, uint8_t const configure_number) ATTR_WARN_UNUSED_RESULT;
 tusbh_device_status_t tusbh_device_status_get (tusb_handle_device_t const device_hdl) ATTR_WARN_UNUSED_RESULT;
 
+#if TUSB_CFG_OS == TUSB_OS_NONE // TODO move later
 static inline void tusb_tick_tock(void) ATTR_ALWAYS_INLINE;
 static inline void tusb_tick_tock(void)
 {
   osal_tick_tock();
 }
-
+#endif
 
 //--------------------------------------------------------------------+
 // CLASS-USBD API
diff --git a/tinyusb/osal/osal_freeRTOS.h b/tinyusb/osal/osal_freeRTOS.h
index 4d89a1acc..b1f16adf0 100644
--- a/tinyusb/osal/osal_freeRTOS.h
+++ b/tinyusb/osal/osal_freeRTOS.h
@@ -85,10 +85,23 @@
 #define OSAL_SEM_DEF(name)
 typedef xSemaphoreHandle osal_semaphore_handle_t;
 
-// create FreeRTOS binary semaphore with zero as init value
+// create FreeRTOS binary semaphore with zero as init value TODO: omit semaphore take from vSemaphoreCreateBinary API, should double checks this
 #define osal_semaphore_create(x) \
   xQueueGenericCreate( ( unsigned portBASE_TYPE ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE )
 
+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)
+{
+  portBASE_TYPE taskWaken;
+  return (xSemaphoreGiveFromISR(sem_hdl, &taskWaken) == pdTRUE) ? 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) = ( xSemaphoreTake(sem_hdl, osal_tick_from_msec(msec)) == pdPASS ) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT;
+}
+
 //--------------------------------------------------------------------+
 // QUEUE API
 //--------------------------------------------------------------------+
@@ -106,6 +119,19 @@ typedef xQueueHandle osal_queue_handle_t;
 #define osal_queue_create(p_queue) \
   xQueueCreate((p_queue)->depth, sizeof(uint32_t))
 
+static inline void osal_queue_receive (osal_queue_handle_t const queue_hdl, uint32_t *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, uint32_t *p_data, uint32_t msec, tusb_error_t *p_error)
+{
+  (*p_error) = ( xQueueReceive(queue_hdl, p_data, osal_tick_from_msec(msec)) == pdPASS ) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT;
+}
+
+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)
+{
+  portBASE_TYPE taskWaken;
+  return ( xQueueSendFromISR(queue_hdl, &data, &taskWaken) == pdTRUE ) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_QUEUE_FAILED;
+}
+
 #ifdef __cplusplus
  }
 #endif