samd/mphalport: Use CYCCNT for SAMD51's mp_hal_ticks_cpu().

And use mp_hal_ticks_us() for SAM21's mp_hal_ticks_cpu().  The SAMD21 has
no CYCCNT register, and the SysTick register has only a 1 ms span (== 48000
count range).
This commit is contained in:
robert-hh 2022-06-10 08:23:09 +02:00 committed by Damien George
parent a9304af8fa
commit fd7b57dd22
2 changed files with 18 additions and 4 deletions

View File

@ -71,12 +71,22 @@ static inline mp_uint_t mp_hal_ticks_us(void) {
#endif
}
// ticks_cpu is limited to a 1 ms period, since the CPU SysTick counter
// is used for the 1 ms SysTick_Handler interrupt.
static inline mp_uint_t mp_hal_ticks_cpu(void) {
return (system_time_t)SysTick->VAL;
#if defined (MCU_SAMD21)
#define mp_hal_ticks_cpu mp_hal_ticks_us
#elif defined (MCU_SAMD51)
static inline void mp_hal_ticks_cpu_enable(void) {
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
DWT->CYCCNT = 0;
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
}
static inline mp_uint_t mp_hal_ticks_cpu(void) {
return (system_time_t)DWT->CYCCNT;
}
#endif
static inline uint64_t mp_hal_time_ns(void) {
return mp_hal_ticks_ms_64() * 1000000;
}

View File

@ -36,6 +36,7 @@
#include "samd_soc.h"
#include "sam.h"
#include "tusb.h"
#include "mphalport.h"
static void usb_init(void) {
// Init USB clock
@ -110,4 +111,7 @@ void samd_init(void) {
SysTick_Config(get_cpu_freq() / 1000);
init_us_counter();
usb_init();
#if defined (MCU_SAMD51)
mp_hal_ticks_cpu_enable();
#endif
}