diff --git a/ports/rp2/mphalport.c b/ports/rp2/mphalport.c index 3fcc011b6..3f5015162 100644 --- a/ports/rp2/mphalport.c +++ b/ports/rp2/mphalport.c @@ -125,6 +125,17 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { return did_write ? ret : 0; } +#if PICO_RISCV +__attribute__((naked)) mp_uint_t mp_hal_ticks_cpu(void) { + __asm volatile ( + "li a0, 4\n" // mask value to uninhibit mcycle counter + "csrw mcountinhibit, a0\n" // uninhibit mcycle counter + "csrr a0, mcycle\n" // get mcycle counter + "ret\n" + ); +} +#endif + void mp_hal_delay_us(mp_uint_t us) { // Avoid calling sleep_us() and invoking the alarm pool by splitting long // sleeps into an optional longer sleep and a shorter busy-wait diff --git a/ports/rp2/mphalport.h b/ports/rp2/mphalport.h index 1d260c8bd..da865fb7e 100644 --- a/ports/rp2/mphalport.h +++ b/ports/rp2/mphalport.h @@ -96,11 +96,15 @@ static inline mp_uint_t mp_hal_ticks_ms(void) { return to_ms_since_boot(get_absolute_time()); } +#if PICO_ARM static inline mp_uint_t mp_hal_ticks_cpu(void) { // ticks_cpu() is defined as using the highest-resolution timing source // in the system. This is usually a CPU clock, but doesn't have to be. return time_us_32(); } +#elif PICO_RISCV +mp_uint_t mp_hal_ticks_cpu(void); +#endif static inline mp_uint_t mp_hal_get_cpu_freq(void) { return clock_get_hz(clk_sys);