Skip to content

Commit bf79bfa

Browse files
committed
rp2/mphalport: Implement mp_hal_ticks_cpu for RISCV using mcycle.
Signed-off-by: Damien George <damien@micropython.org>
1 parent aab8fca commit bf79bfa

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

ports/rp2/mphalport.c

Lines changed: 11 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,15 @@ static inline mp_uint_t mp_hal_ticks_ms(void) {
9292
return to_ms_since_boot(get_absolute_time());
9393
}
9494

95+
#if PICO_ARM
9596
static inline mp_uint_t mp_hal_ticks_cpu(void) {
9697
// ticks_cpu() is defined as using the highest-resolution timing source
9798
// in the system. This is usually a CPU clock, but doesn't have to be.
9899
return time_us_32();
99100
}
101+
#elif PICO_RISCV
102+
mp_uint_t mp_hal_ticks_cpu(void);
103+
#endif
100104

101105
static inline mp_uint_t mp_hal_get_cpu_freq(void) {
102106
return clock_get_hz(clk_sys);

0 commit comments

Comments
 (0)