Skip to content

Commit abbe883

Browse files
iabdalkaderdpgeorge
authored andcommitted
qemu/mcu/riscv: Implement ticks using the RDTIME control register.
Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
1 parent e1f063a commit abbe883

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

ports/qemu/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ LDFLAGS += -mabi=$(RV32_ABI) -march=$(RV32_ARCH) -Wl,-EL
110110
SRC_C += \
111111
mcu/rv32/interrupts.c \
112112
mcu/rv32/startup.c \
113+
mcu/riscv/ticks.c \
113114

114115
SRC_BOARD_O += mcu/rv32/entrypoint.o
115116

@@ -143,6 +144,7 @@ LDFLAGS += -mabi=$(RV64_ABI) -march=$(RV64_ARCH) -Wl,-EL -mcmodel=medany
143144
SRC_C += \
144145
mcu/rv64/interrupts.c \
145146
mcu/rv64/startup.c \
147+
mcu/riscv/ticks.c \
146148

147149
SRC_BOARD_O += mcu/rv64/entrypoint.o
148150

ports/qemu/mcu/riscv/ticks.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2025 Ibrahim Abdelkader <iabdalkader@openmv.io>
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdint.h>
28+
29+
// RISC-V timebase frequency for QEMU
30+
#ifndef TIMEBASE_FREQ_HZ
31+
#define TIMEBASE_FREQ_HZ 10000000u
32+
#endif
33+
34+
static uint64_t ticks_get_ticks(void) {
35+
#if __riscv_xlen < 64
36+
uint32_t ticks_lo = 0;
37+
uint32_t ticks_hi = 0;
38+
uint32_t rollover = 0;
39+
do {
40+
__asm volatile (
41+
"rdtimeh %0 \n"
42+
"rdtime %1 \n"
43+
"rdtimeh %2 \n"
44+
: "=r" (ticks_hi), "=r" (ticks_lo), "=r" (rollover)
45+
:
46+
:
47+
);
48+
} while (ticks_hi != rollover);
49+
return ((uint64_t)(ticks_hi) << 32ULL) | (uint64_t)(ticks_lo);
50+
#else
51+
uint64_t ticks = 0;
52+
__asm volatile (
53+
"rdtime %0 \n"
54+
: "=r" (ticks)
55+
:
56+
:
57+
);
58+
return ticks;
59+
#endif
60+
}
61+
62+
uintptr_t ticks_ms(void) {
63+
return (uintptr_t)(ticks_get_ticks() / (TIMEBASE_FREQ_HZ / 1000));
64+
}
65+
66+
uintptr_t ticks_us(void) {
67+
return (uintptr_t)(ticks_get_ticks() / (TIMEBASE_FREQ_HZ / 1000000));
68+
}

0 commit comments

Comments
 (0)