From ccd2fe3ca3f46b58455695e3bdf91a4fe41e3cff Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 5 Aug 2020 16:46:16 +0700 Subject: [PATCH 1/2] micro() use DWT cyclecount if enabled else use rtos tick DWT must be previously enabled by user sketch with dwt_enable() --- cores/nRF5/delay.c | 5 ----- cores/nRF5/delay.h | 14 +++++++++++++- .../portable/CMSIS/nrf52/port_cmsis_systick.c | 6 ++++++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/cores/nRF5/delay.c b/cores/nRF5/delay.c index f96d5058..73c68e1d 100644 --- a/cores/nRF5/delay.c +++ b/cores/nRF5/delay.c @@ -30,11 +30,6 @@ uint32_t millis( void ) return tick2ms(xTaskGetTickCount()); } -uint32_t micros( void ) -{ - return tick2us(xTaskGetTickCount()); -} - void delay( uint32_t ms ) { uint32_t ticks = ms2tick(ms); diff --git a/cores/nRF5/delay.h b/cores/nRF5/delay.h index f25809f7..404334ba 100644 --- a/cores/nRF5/delay.h +++ b/cores/nRF5/delay.h @@ -26,6 +26,13 @@ extern "C" { #include #include "nrfx.h" #include "variant.h" +#include "rtos.h" + +static inline bool dwt_enabled(void) __attribute__((always_inline)); +static inline bool dwt_enabled(void) +{ + return (CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA_Msk) && (DWT->CTRL & DWT_CTRL_CYCCNTENA_Msk); +} /** * \brief Returns the number of milliseconds since the Arduino board began running the current program. @@ -46,7 +53,12 @@ extern uint32_t millis( void ) ; * * \note There are 1,000 microseconds in a millisecond and 1,000,000 microseconds in a second. */ -extern uint32_t micros( void ) ; +static inline uint32_t micros( void ) __attribute__((always_inline)); +static inline uint32_t micros( void ) +{ + // Use DWT cycle count if it is enabled, otherwise use rtos tick + return dwt_enabled() ? (DWT->CYCCNT / 64) : tick2us(xTaskGetTickCount()); +} /** * \brief Pauses the program for the amount of time (in miliseconds) specified as parameter. diff --git a/cores/nRF5/freertos/portable/CMSIS/nrf52/port_cmsis_systick.c b/cores/nRF5/freertos/portable/CMSIS/nrf52/port_cmsis_systick.c index 21dec1fc..8f60b2c3 100644 --- a/cores/nRF5/freertos/portable/CMSIS/nrf52/port_cmsis_systick.c +++ b/cores/nRF5/freertos/portable/CMSIS/nrf52/port_cmsis_systick.c @@ -230,6 +230,12 @@ void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime ) if (diff > 0) { vTaskStepTick(diff); + + // If dwt cycle count is enable, adjust it as welll + if ( (CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA_Msk) && (DWT->CTRL & DWT_CTRL_CYCCNTENA_Msk) ) + { + DWT->CYCCNT += ((diff * 1000000) / configTICK_RATE_HZ) * 64; + } } } } From 9a960037bb56b33f5d7c99baca795622c61c0c7a Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 20 Aug 2020 23:15:40 +0700 Subject: [PATCH 2/2] correct micro ticks --- cores/nRF5/freertos/portable/CMSIS/nrf52/port_cmsis_systick.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/nRF5/freertos/portable/CMSIS/nrf52/port_cmsis_systick.c b/cores/nRF5/freertos/portable/CMSIS/nrf52/port_cmsis_systick.c index 44f90ac1..44ab0155 100644 --- a/cores/nRF5/freertos/portable/CMSIS/nrf52/port_cmsis_systick.c +++ b/cores/nRF5/freertos/portable/CMSIS/nrf52/port_cmsis_systick.c @@ -243,7 +243,7 @@ void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime ) // If dwt cycle count is enable, adjust it as welll if ( (CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA_Msk) && (DWT->CTRL & DWT_CTRL_CYCCNTENA_Msk) ) { - DWT->CYCCNT += ((diff * 1000000) / configTICK_RATE_HZ) * 64; + DWT->CYCCNT += (((diff-1) * 1000000) / configTICK_RATE_HZ) * 64; } switch_req = xTaskIncrementTick();