Skip to content

Commit ea2eed1

Browse files
committed
rp2/mphalport: Implement mp_hal_ticks_cpu for RISCV using mcycle.
Signed-off-by: Damien George <[email protected]>
1 parent 957cea2 commit ea2eed1

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

ports/rp2/mphalport.c

+11
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
125125
return did_write ? ret : 0;
126126
}
127127

128+
#if PICO_RISCV
129+
__attribute__((naked)) mp_uint_t mp_hal_ticks_cpu(void) {
130+
__asm volatile (
131+
"li a0, 4\n" // mask value to uninhibit mcycle counter
132+
"csrw mcountinhibit, a0\n" // uninhibit mcycle counter
133+
"csrr a0, mcycle\n" // get mcycle counter
134+
"ret\n"
135+
);
136+
}
137+
#endif
138+
128139
void mp_hal_delay_us(mp_uint_t us) {
129140
// Avoid calling sleep_us() and invoking the alarm pool by splitting long
130141
// sleeps into an optional longer sleep and a shorter busy-wait

ports/rp2/mphalport.h

+4
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,15 @@ static inline mp_uint_t mp_hal_ticks_ms(void) {
9696
return to_ms_since_boot(get_absolute_time());
9797
}
9898

99+
#if PICO_ARM
99100
static inline mp_uint_t mp_hal_ticks_cpu(void) {
100101
// ticks_cpu() is defined as using the highest-resolution timing source
101102
// in the system. This is usually a CPU clock, but doesn't have to be.
102103
return time_us_32();
103104
}
105+
#elif PICO_RISCV
106+
mp_uint_t mp_hal_ticks_cpu(void);
107+
#endif
104108

105109
static inline mp_uint_t mp_hal_get_cpu_freq(void) {
106110
return clock_get_hz(clk_sys);

0 commit comments

Comments
 (0)