From e4fee9944ebad6415ffb9e3b16eae45e453172b0 Mon Sep 17 00:00:00 2001 From: Yifan Wu Date: Tue, 29 Dec 2020 21:49:51 +0800 Subject: [PATCH] Update sys_time unit to ms && Update clock freq of K210. --- os/src/config.rs | 4 ++-- os/src/syscall/process.rs | 4 ++-- os/src/timer.rs | 11 ++++++++--- user/src/bin/03sleep.rs | 2 +- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/os/src/config.rs b/os/src/config.rs index 07c30246..20aba22e 100644 --- a/os/src/config.rs +++ b/os/src/config.rs @@ -5,7 +5,7 @@ pub const APP_BASE_ADDRESS: usize = 0x80100000; pub const APP_SIZE_LIMIT: usize = 0x20000; #[cfg(feature = "board_k210")] -pub const CPU_FREQ: usize = 10000000; +pub const CLOCK_FREQ: usize = 403000000 / 62; #[cfg(feature = "board_qemu")] -pub const CPU_FREQ: usize = 12500000; \ No newline at end of file +pub const CLOCK_FREQ: usize = 12500000; \ No newline at end of file diff --git a/os/src/syscall/process.rs b/os/src/syscall/process.rs index e0bdebae..93efe8ca 100644 --- a/os/src/syscall/process.rs +++ b/os/src/syscall/process.rs @@ -2,7 +2,7 @@ use crate::task::{ suspend_current_and_run_next, exit_current_and_run_next, }; -use crate::timer::get_time; +use crate::timer::get_time_ms; pub fn sys_exit(xstate: i32) -> ! { println!("[kernel] Application exited with code {}", xstate); @@ -16,5 +16,5 @@ pub fn sys_yield() -> isize { } pub fn sys_get_time() -> isize { - get_time() as isize + get_time_ms() as isize } \ No newline at end of file diff --git a/os/src/timer.rs b/os/src/timer.rs index 7522e708..ad226e64 100644 --- a/os/src/timer.rs +++ b/os/src/timer.rs @@ -1,13 +1,18 @@ use riscv::register::time; use crate::sbi::set_timer; -use crate::config::CPU_FREQ; +use crate::config::CLOCK_FREQ; const TICKS_PER_SEC: usize = 100; +const MSEC_PER_SEC: usize = 1000; pub fn get_time() -> usize { time::read() } +pub fn get_time_ms() -> usize { + time::read() / (CLOCK_FREQ / MSEC_PER_SEC) +} + pub fn set_next_trigger() { - set_timer(get_time() + CPU_FREQ / TICKS_PER_SEC); -} \ No newline at end of file + set_timer(get_time() + CLOCK_FREQ / TICKS_PER_SEC); +} diff --git a/user/src/bin/03sleep.rs b/user/src/bin/03sleep.rs index f3d9f31b..ada77e91 100644 --- a/user/src/bin/03sleep.rs +++ b/user/src/bin/03sleep.rs @@ -9,7 +9,7 @@ use user_lib::{sys_get_time, sys_yield}; #[no_mangle] fn main() -> i32 { let current_timer = sys_get_time(); - let wait_for = current_timer + 10000000; + let wait_for = current_timer + 3000; while sys_get_time() < wait_for { sys_yield(); }