Skip to content

Commit 65399bc

Browse files
committed
alif/mphalport: Use DWT->CYCCNT for CPU tick if SysTick/LPTIMER is used.
Using CYCCNT gives much higher resolution, at 400MHz. Signed-off-by: Damien George <damien@micropython.org>
1 parent b3806c5 commit 65399bc

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

ports/alif/mphalport.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,19 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
116116
}
117117

118118
mp_uint_t mp_hal_ticks_cpu(void) {
119+
#if MICROPY_HW_SYSTEM_TICK_USE_SYSTICK || MICROPY_HW_SYSTEM_TICK_USE_LPTIMER
120+
// SysTick and LPTIMER run relatively slowly, so use cycle counter for CPU ticks.
121+
if (!(DWT->CTRL & DWT_CTRL_CYCCNTENA_Msk)) {
122+
// Enable CYCCNT.
123+
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
124+
DWT->CYCCNT = 0;
125+
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
126+
}
127+
return DWT->CYCCNT;
128+
#else
129+
// UTIMER runs at CPU frequency so use it directly as CPU ticks.
119130
return system_tick_get_u32();
131+
#endif
120132
}
121133

122134
mp_uint_t mp_hal_ticks_us(void) {

0 commit comments

Comments
 (0)