summaryrefslogtreecommitdiff
path: root/tinyusb
diff options
context:
space:
mode:
Diffstat (limited to 'tinyusb')
-rw-r--r--tinyusb/common/timeout_timer.h5
-rw-r--r--tinyusb/common/tusb_verify.h8
-rw-r--r--tinyusb/osal/osal.c54
-rw-r--r--tinyusb/osal/osal_freeRTOS.h51
-rw-r--r--tinyusb/osal/osal_none.h17
-rw-r--r--tinyusb/tusb_hal.h1
6 files changed, 84 insertions, 52 deletions
diff --git a/tinyusb/common/timeout_timer.h b/tinyusb/common/timeout_timer.h
index f504cc91f..aafc8cce2 100644
--- a/tinyusb/common/timeout_timer.h
+++ b/tinyusb/common/timeout_timer.h
@@ -45,6 +45,7 @@
#define _TUSB_TIMEOUT_TTIMER_H_
#include "tusb_compiler.h"
+#include "tusb_hal.h"
#ifdef __cplusplus
extern "C" {
@@ -58,12 +59,12 @@ typedef struct {
static inline void timeout_set(timeout_timer_t* tt, uint32_t msec)
{
tt->interval = msec;
- tt->start = osal_millis();
+ tt->start = tusb_hal_millis();
}
static inline bool timeout_expired(timeout_timer_t* tt)
{
- return ( osal_millis() - tt->start ) >= tt->interval;
+ return ( tusb_hal_millis() - tt->start ) >= tt->interval;
}
#ifdef __cplusplus
diff --git a/tinyusb/common/tusb_verify.h b/tinyusb/common/tusb_verify.h
index 856ae7958..516a30a75 100644
--- a/tinyusb/common/tusb_verify.h
+++ b/tinyusb/common/tusb_verify.h
@@ -71,13 +71,13 @@
// Halt CPU (breakpoint) when hitting error, only apply for Cortex M3, M4, M7
#if defined(__ARM_ARCH_7M__) || defined (__ARM_ARCH_7EM__)
-// Cortex M CoreDebug->DHCSR
-#define ARM_CM_DHCSR (*((volatile uint32_t*) 0xE000EDF0UL))
-
static inline void verify_breakpoint(void)
{
+ // Cortex M CoreDebug->DHCSR
+ volatile uint32_t* ARM_CM_DHCSR = ((volatile uint32_t*) 0xE000EDF0UL);
+
// Only halt mcu if debugger is attached
- if ( ARM_CM_DHCSR & 1UL ) __asm("BKPT #0\n");
+ if ( (*ARM_CM_DHCSR) & 1UL ) __asm("BKPT #0\n");
}
#else
diff --git a/tinyusb/osal/osal.c b/tinyusb/osal/osal.c
new file mode 100644
index 000000000..68bafa3ea
--- /dev/null
+++ b/tinyusb/osal/osal.c
@@ -0,0 +1,54 @@
+/**************************************************************************/
+/*!
+ @file osal.c
+ @author hathach
+
+ @section LICENSE
+
+ Software License Agreement (BSD License)
+
+ Copyright (c) 2018, 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"
+
+
+//--------------------------------------------------------------------+
+// TICK API
+//--------------------------------------------------------------------+
+#if TUSB_CFG_OS == TUSB_OS_FREERTOS
+
+uint32_t tusb_hal_millis(void)
+{
+ return ( ( ((uint64_t) xTaskGetTickCount()) * 1000) / configTICK_RATE_HZ );
+}
+
+#endif
+
diff --git a/tinyusb/osal/osal_freeRTOS.h b/tinyusb/osal/osal_freeRTOS.h
index f2fe668ae..b0a99ad38 100644
--- a/tinyusb/osal/osal_freeRTOS.h
+++ b/tinyusb/osal/osal_freeRTOS.h
@@ -56,10 +56,15 @@
extern "C" {
#endif
-//--------------------------------------------------------------------+
-// TICK API
-//--------------------------------------------------------------------+
-#define osal_millis xTaskGetTickCount
+#if 0
+// Helper to determine if we are in ISR to use ISR API (only cover ARM Cortex)
+// Note: Actually we don't need this since any event signal (queue send, semaphore post)
+// is done in ISR, other event receive (queue receive, semaphore wait ) in in thread
+static inline bool in_isr(void)
+{
+ return (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk);
+}
+#endif
//--------------------------------------------------------------------+
// TASK API
@@ -92,20 +97,12 @@ static inline osal_queue_t osal_queue_create(uint32_t depth, uint32_t item_size)
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 : pdMS_TO_TICKS(msec);
-
- 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;
+ (*p_error) = ( xQueueReceive(queue_hdl, p_data, ticks) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT);
}
static inline bool osal_queue_send(osal_queue_t const queue_hdl, void const * data)
{
- if (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk)
- {
- return xQueueSendToFrontFromISR(queue_hdl, data, NULL);
- }else
- {
- return xQueueSendToFront(queue_hdl, data, 0);
- }
+ return xQueueSendToFrontFromISR(queue_hdl, data, NULL);
}
static inline void osal_queue_flush(osal_queue_t const queue_hdl)
@@ -126,30 +123,13 @@ static inline osal_semaphore_t osal_semaphore_create(uint32_t max, uint32_t init
// TODO add timeout (with instant return from ISR option) for semaphore post & queue send
static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl)
{
- if (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk)
- {
- return xSemaphoreGiveFromISR(sem_hdl, NULL);
- }else
- {
- return xSemaphoreGive(sem_hdl);
- }
+ return xSemaphoreGiveFromISR(sem_hdl, NULL);
}
static inline void osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec, tusb_error_t *p_error)
{
uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : pdMS_TO_TICKS(msec);
-
- BaseType_t result;
-
- if (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk)
- {
- result = xSemaphoreTakeFromISR(sem_hdl, NULL);
- }else
- {
- result = xSemaphoreTake(sem_hdl, ticks);
- }
-
- (*p_error) = result ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT;
+ (*p_error) = (xSemaphoreTake(sem_hdl, ticks) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT);
}
static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl)
@@ -167,12 +147,13 @@ typedef xSemaphoreHandle osal_mutex_t;
static inline bool osal_mutex_release(osal_mutex_t mutex_hdl)
{
- return osal_semaphore_post(mutex_hdl);
+ return xSemaphoreGive(mutex_hdl);
}
static inline void osal_mutex_wait(osal_mutex_t mutex_hdl, uint32_t msec, tusb_error_t *p_error)
{
- osal_semaphore_wait(mutex_hdl, msec, p_error);
+ uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : pdMS_TO_TICKS(msec);
+ (*p_error) = (xSemaphoreTake(mutex_hdl, ticks) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT);
}
// TOOD remove
diff --git a/tinyusb/osal/osal_none.h b/tinyusb/osal/osal_none.h
index 0b73de4e9..5325fd29e 100644
--- a/tinyusb/osal/osal_none.h
+++ b/tinyusb/osal/osal_none.h
@@ -51,11 +51,6 @@
#endif
//--------------------------------------------------------------------+
-// TICK API
-//--------------------------------------------------------------------+
-#define osal_millis tusb_hal_millis
-
-//--------------------------------------------------------------------+
// TASK API
// NOTES: Each blocking OSAL_NONE services such as semaphore wait,
// queue receive embedded return statement, therefore local variable
@@ -97,9 +92,9 @@ static inline osal_task_t osal_task_create(osal_func_t code, const char* name, u
#define osal_task_delay(msec) \
do {\
- _timeout = osal_millis();\
+ _timeout = tusb_hal_millis();\
_state = __LINE__; case __LINE__:\
- if ( _timeout + msec > osal_millis() ) \
+ if ( _timeout + msec > tusb_hal_millis() ) \
return TUSB_ERROR_OSAL_WAITING;\
}while(0)
@@ -164,10 +159,10 @@ static inline void osal_queue_flush(osal_queue_t const queue_hdl)
#define osal_queue_receive(queue_hdl, p_data, msec, p_error) \
do {\
- _timeout = osal_millis();\
+ _timeout = tusb_hal_millis();\
_state = __LINE__; case __LINE__:\
if( queue_hdl->count == 0 ) {\
- if ( (msec != OSAL_TIMEOUT_WAIT_FOREVER) && ( _timeout + msec <= osal_millis()) ) /* time out */ \
+ if ( (msec != OSAL_TIMEOUT_WAIT_FOREVER) && ( _timeout + msec <= tusb_hal_millis()) ) /* time out */ \
*(p_error) = TUSB_ERROR_OSAL_TIMEOUT;\
else\
return TUSB_ERROR_OSAL_WAITING;\
@@ -218,10 +213,10 @@ static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl)
#define osal_semaphore_wait(sem_hdl, msec, p_error) \
do {\
- _timeout = osal_millis();\
+ _timeout = tusb_hal_millis();\
_state = __LINE__; case __LINE__:\
if( sem_hdl->count == 0 ) {\
- if ( (msec != OSAL_TIMEOUT_WAIT_FOREVER) && (_timeout + msec <= osal_millis()) ) /* time out */ \
+ if ( (msec != OSAL_TIMEOUT_WAIT_FOREVER) && (_timeout + msec <= tusb_hal_millis()) ) /* time out */ \
*(p_error) = TUSB_ERROR_OSAL_TIMEOUT;\
else\
return TUSB_ERROR_OSAL_WAITING;\
diff --git a/tinyusb/tusb_hal.h b/tinyusb/tusb_hal.h
index 0025bf844..f09ba0f7d 100644
--- a/tinyusb/tusb_hal.h
+++ b/tinyusb/tusb_hal.h
@@ -75,6 +75,7 @@ void tusb_hal_int_enable(uint8_t rhport);
*/
void tusb_hal_int_disable(uint8_t rhport);
+// Only required to implement if using No RTOS (osal_none)
uint32_t tusb_hal_millis(void);
#ifdef __cplusplus