mirror of
https://github.com/LearningOS/rust-based-os-comp2022.git
synced 2026-02-09 13:15:35 +08:00
24 lines
519 B
Rust
24 lines
519 B
Rust
//! RISC-V timer-related functionality
|
|
|
|
use crate::config::CLOCK_FREQ;
|
|
use crate::sbi::set_timer;
|
|
use riscv::register::time;
|
|
|
|
const TICKS_PER_SEC: usize = 100;
|
|
const MICRO_PER_SEC: usize = 1_000_000;
|
|
|
|
/// read the `mtime` register
|
|
pub fn get_time() -> usize {
|
|
time::read()
|
|
}
|
|
|
|
/// get current time in microseconds
|
|
pub fn get_time_us() -> usize {
|
|
time::read() / (CLOCK_FREQ / MICRO_PER_SEC)
|
|
}
|
|
|
|
/// set the next timer interrupt
|
|
pub fn set_next_trigger() {
|
|
set_timer(get_time() + CLOCK_FREQ / TICKS_PER_SEC);
|
|
}
|