diff --git a/examples/obsolete/device/device_freertos/.cproject b/examples/obsolete/device/device_freertos/.cproject index 2183c323..e3c134ea 100644 --- a/examples/obsolete/device/device_freertos/.cproject +++ b/examples/obsolete/device/device_freertos/.cproject @@ -89,7 +89,7 @@ - + @@ -186,7 +186,7 @@ - + @@ -291,7 +291,7 @@ - + @@ -387,7 +387,7 @@ - + @@ -487,7 +487,7 @@ - + @@ -583,7 +583,7 @@ - + diff --git a/hw/bsp/board.h b/hw/bsp/board.h index 2c566bb1..827f22d7 100644 --- a/hw/bsp/board.h +++ b/hw/bsp/board.h @@ -123,6 +123,8 @@ #define BOARD_TICKS_HZ 1000 +#define board_tick2ms(tck) ( ( ((uint64_t)(tck)) * 1000) / BOARD_TICKS_HZ ) + //--------------------------------------------------------------------+ // Board Common API //--------------------------------------------------------------------+ diff --git a/hw/bsp/ea4357/board_ea4357.c b/hw/bsp/ea4357/board_ea4357.c index 963e7629..549eb70f 100644 --- a/hw/bsp/ea4357/board_ea4357.c +++ b/hw/bsp/ea4357/board_ea4357.c @@ -79,7 +79,7 @@ void SysTick_Handler (void) uint32_t tusb_hal_millis(void) { - return (system_ticks*1000) / BOARD_TICKS_HZ; + return board_tick2ms(system_ticks); } #endif diff --git a/hw/bsp/pca10056/board_pca10056.c b/hw/bsp/pca10056/board_pca10056.c index 98071429..e1668ebb 100644 --- a/hw/bsp/pca10056/board_pca10056.c +++ b/hw/bsp/pca10056/board_pca10056.c @@ -63,7 +63,7 @@ uint32_t tusb_hal_millis(void) //#define tick2ms(tck) ( ( ((uint64_t)(tck)) * 1000) / configTICK_RATE_HZ ) //return tick2ms( app_timer_cnt_get() ); - return (system_ticks*1000) / BOARD_TICKS_HZ; + return board_tick2ms(system_ticks); } /*------------------------------------------------------------------*/ diff --git a/tinyusb/common/timeout_timer.h b/tinyusb/common/timeout_timer.h index f504cc91..aafc8cce 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 856ae795..516a30a7 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 00000000..68bafa3e --- /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 f2fe668a..b0a99ad3 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 0b73de4e..5325fd29 100644 --- a/tinyusb/osal/osal_none.h +++ b/tinyusb/osal/osal_none.h @@ -50,11 +50,6 @@ extern "C" { #endif -//--------------------------------------------------------------------+ -// TICK API -//--------------------------------------------------------------------+ -#define osal_millis tusb_hal_millis - //--------------------------------------------------------------------+ // TASK API // NOTES: Each blocking OSAL_NONE services such as semaphore wait, @@ -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 0025bf84..f09ba0f7 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 diff --git a/vendor/freertos/FreeRTOSConfig.h b/vendor/freertos/FreeRTOSConfig.h index 09b78988..fa1707f9 100644 --- a/vendor/freertos/FreeRTOSConfig.h +++ b/vendor/freertos/FreeRTOSConfig.h @@ -1,12 +1,12 @@ #ifndef __FREERTOS_CONFIG__H #define __FREERTOS_CONFIG__H -#include "common/assertion.h" - //--------------------------------------------------------------------+ // See http://www.freertos.org/a00110.html. //--------------------------------------------------------------------+ #if TUSB_CFG_MCU == MCU_LPC43XX + // TODO remove + #include "lpc43xx_cgu.h" #define configCPU_CLOCK_HZ CGU_GetPCLKFrequency(CGU_PERIPHERAL_M4CORE) #else #define configCPU_CLOCK_HZ SystemCoreClock @@ -72,7 +72,25 @@ #define INCLUDE_xTimerPendFunctionCall 0 /* Define to trap errors during development. */ -#define configASSERT( x ) if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); hal_debugger_breakpoint(); } + +// Halt CPU (breakpoint) when hitting error, only apply for Cortex M3, M4, M7 +#if defined(__ARM_ARCH_7M__) || defined (__ARM_ARCH_7EM__) + +static inline void configASSERT_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"); +} + +#else +#define configASSERT_breakpoint() +#endif + + +#define configASSERT( x ) if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); configASSERT_breakpoint(); } /* FreeRTOS hooks to NVIC vectors */ #define xPortPendSVHandler PendSV_Handler diff --git a/vendor/freertos/freertos_hook.c b/vendor/freertos/freertos_hook.c index 0800ca0d..6672f72b 100644 --- a/vendor/freertos/freertos_hook.c +++ b/vendor/freertos/freertos_hook.c @@ -41,7 +41,7 @@ //--------------------------------------------------------------------+ #include "FreeRTOS.h" #include "task.h" -#include "common/common.h" +#include "common/tusb_common.h" //#include "board.h" //--------------------------------------------------------------------+ @@ -58,7 +58,7 @@ void vApplicationMallocFailedHook(void) { taskDISABLE_INTERRUPTS(); - ASSERT_FAILED_MSG(VOID_RETURN, "freeRTOS run out of heap memory"); + TU_ASSERT(false, ); } void vApplicationIdleHook(void) @@ -72,7 +72,7 @@ void vApplicationStackOverflowHook(xTaskHandle pxTask, signed char *pcTaskName) (void) pxTask; taskDISABLE_INTERRUPTS(); - ASSERT_FAILED_MSG(VOID_RETURN, pcTaskName); + TU_ASSERT(false, ); } // executes from within an ISR