Skip to content

Commit 11142b5

Browse files
authored
Merge pull request #551 from adafruit/fix-micros
micro() use DWT cyclecount if enabled else use rtos tick
2 parents 6c66cd1 + 9a96003 commit 11142b5

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,13 @@ void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
239239
if (diff > 1)
240240
{
241241
vTaskStepTick(diff - 1);
242+
243+
// If dwt cycle count is enable, adjust it as welll
244+
if ( (CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA_Msk) && (DWT->CTRL & DWT_CTRL_CYCCNTENA_Msk) )
245+
{
246+
DWT->CYCCNT += (((diff-1) * 1000000) / configTICK_RATE_HZ) * 64;
247+
}
248+
242249
switch_req = xTaskIncrementTick();
243250
}
244251
else if (diff == 1)

0 commit comments

Comments
 (0)