Skip to content

Commit ccd2fe3

Browse files
committed
micro() use DWT cyclecount if enabled else use rtos tick
DWT must be previously enabled by user sketch with dwt_enable()
1 parent 782ab61 commit ccd2fe3

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

cores/nRF5/delay.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ uint32_t millis( void )
3030
return tick2ms(xTaskGetTickCount());
3131
}
3232

33-
uint32_t micros( void )
34-
{
35-
return tick2us(xTaskGetTickCount());
36-
}
37-
3833
void delay( uint32_t ms )
3934
{
4035
uint32_t ticks = ms2tick(ms);

cores/nRF5/delay.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ extern "C" {
2626
#include <stdint.h>
2727
#include "nrfx.h"
2828
#include "variant.h"
29+
#include "rtos.h"
30+
31+
static inline bool dwt_enabled(void) __attribute__((always_inline));
32+
static inline bool dwt_enabled(void)
33+
{
34+
return (CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA_Msk) && (DWT->CTRL & DWT_CTRL_CYCCNTENA_Msk);
35+
}
2936

3037
/**
3138
* \brief Returns the number of milliseconds since the Arduino board began running the current program.
@@ -46,7 +53,12 @@ extern uint32_t millis( void ) ;
4653
*
4754
* \note There are 1,000 microseconds in a millisecond and 1,000,000 microseconds in a second.
4855
*/
49-
extern uint32_t micros( void ) ;
56+
static inline uint32_t micros( void ) __attribute__((always_inline));
57+
static inline uint32_t micros( void )
58+
{
59+
// Use DWT cycle count if it is enabled, otherwise use rtos tick
60+
return dwt_enabled() ? (DWT->CYCCNT / 64) : tick2us(xTaskGetTickCount());
61+
}
5062

5163
/**
5264
* \brief Pauses the program for the amount of time (in miliseconds) specified as parameter.

cores/nRF5/freertos/portable/CMSIS/nrf52/port_cmsis_systick.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@ void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
230230
if (diff > 0)
231231
{
232232
vTaskStepTick(diff);
233+
234+
// If dwt cycle count is enable, adjust it as welll
235+
if ( (CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA_Msk) && (DWT->CTRL & DWT_CTRL_CYCCNTENA_Msk) )
236+
{
237+
DWT->CYCCNT += ((diff * 1000000) / configTICK_RATE_HZ) * 64;
238+
}
233239
}
234240
}
235241
}

0 commit comments

Comments
 (0)