Skip to main content

tg_clock/
lib.rs

1#![no_std]
2
3// use riscv::register::{sie, sstatus};
4use tg_rcore_tutorial_sbi::set_timer;
5
6pub fn init() {
7    unsafe {
8        // 开启 sstatus 的 SIE (Supervisor Interrupt Enable)
9        core::arch::asm!("csrs sstatus, {}", in(reg) 1 << 1);
10        // 开启 sie 的 STIE (Supervisor Timer Interrupt Enable)
11        core::arch::asm!("csrs sie, {}", in(reg) 1 << 5);
12    }
13    // 设置第一次时钟中断:当前时间 + 100000 个时钟周期
14    clock_set_next_event();
15}
16
17pub fn clock_set_next_event() {
18    // 100000 cycles later (approx. 10ms on QEMU)
19    set_timer((get_time() + 100000) as u64);
20}
21
22// 获取当前时间(mtime 寄存器)
23pub fn get_time() -> usize {
24    let mut time: usize;
25    unsafe {
26        core::arch::asm!("csrr {}, time", out(reg) time);
27    }
28    time
29}
30