implement delay_ns with DWT

This commit is contained in:
hathach committed 2017-03-11 00:49:36 +07:00
1 parent 25f4551693
commit 3b0088f6e9
3 files changed
+46 -1

No files matched your search

+14
View File
@@ -40,6 +40,20 @@ void delay( uint32_t ms )
vTaskDelay(ms2tick(ms));
}
void dwt_enable(void)
{
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; /* Global Enable for DWT */
DWT->CYCCNT = 0; /* Reset the counter */
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; /* Enable cycle counter */
}
void dwt_disable(void)
{
DWT->CTRL &= ~DWT_CTRL_CYCCNTENA_Msk;
CoreDebug->DEMCR &= ~CoreDebug_DEMCR_TRCENA_Msk;
}
#ifdef __cplusplus
}
#endif
+29 -1
View File
@@ -56,7 +56,7 @@ extern uint32_t micros( void ) ;
*
* \param dwMs the number of milliseconds to pause (uint32_t)
*/
extern void delay( uint32_t dwMs ) ;
extern void delay( uint32_t dwMs );
/**
* \brief Pauses the program for the amount of time (in microseconds) specified as parameter.
@@ -74,6 +74,34 @@ static __inline__ void delayMicroseconds( uint32_t usec )
nrf_delay_us(usec);
}
void dwt_enable(void);
void dwt_disable(void);
/**
* Delay nano seconds, delay_ns() make use of DWT of debug core.
* dwt_enable() must be called previously.
* Note: nrf52 run at 64MHz
* - 1000 ns ~ 64 cycles
* - 500 ns ~ 32 cycles
* - 250 ns ~ 16 cycles
* - 125 ns ~ 8 cycles
*/
// Enable pragma when this bug is fixed https://bugs.launchpad.net/gcc-arm-embedded/+bug/1534360
// #pragma GCC push_options
// #pragma GCC optimize ("Ofast")
#define DELAY_CYCLE_CORRECTION 8 // ~125 ns overhead for delay_ns()
#define delay_ns(ns) \
do {\
register uint32_t _endtime = DWT->CYCCNT;\
_endtime += ((F_CPU/1000000)*ns)/1000 - DELAY_CYCLE_CORRECTION;\
while (DWT->CYCCNT < _endtime) {}\
} while(0)
// #pragma GCC pop_options
#ifdef __cplusplus
}
#endif
+3
View File
@@ -40,6 +40,7 @@
#if CFG_DEBUG
#define CFG_DEBUG_NFFS 0
#if CFG_DEBUG_NFFS
static void print_buf(const uint8_t* address, uint32_t count)
{
for(uint32_t i=0; i<count; i++) cprintf("%02X ", address[i] );
@@ -66,6 +67,8 @@ static void print_write_after(uint32_t address, uint32_t count)
}
#endif
#endif
static SemaphoreHandle_t _evt_sem = NULL;
static volatile uint32_t _op_result;