添加时钟中断处理
							parent
							
								
									bfed67715d
								
							
						
					
					
						commit
						1f093dd193
					
				| @ -0,0 +1,29 @@ | ||||
| 
 | ||||
| use riscv::register::time; | ||||
| use crate::config::CLOCK_FREQ; | ||||
| use crate::sbi::set_timer; | ||||
| 
 | ||||
| const TICKS_PER_SEC: usize = 100; | ||||
| const MICRO_PER_SEC: usize = 1_000_000; | ||||
| const MSEC_PER_SEC: usize = 1_000; | ||||
| 
 | ||||
| //
 | ||||
| pub fn get_time() -> usize { | ||||
|     time::read() | ||||
| } | ||||
| 
 | ||||
| // 读取mtime的值, 然后使用 (当前每秒的频率 / TICKS_PER_SEC 100) = 得到 10ms的数值的增量, 相加得到下一个下10ms 后mtime应该属于的值
 | ||||
| // 并设置mtimecmp, 这样在10ms后就会发出一个S特权的时钟中断
 | ||||
| pub fn set_next_trigger() { | ||||
|     set_timer(get_time() + CLOCK_FREQ / TICKS_PER_SEC); | ||||
| } | ||||
| 
 | ||||
| // 以微秒为单位, 返回当前计数器经过的微秒
 | ||||
| // 当前 (计时器的值的总数 / 频率) = 计时器中经过了多少秒,    (计时器的值的总数 / 频率) * 1_000_000 得到微秒(1秒有1_000_000微秒)
 | ||||
| pub fn get_time_us() -> usize { | ||||
|     time::read() / (CLOCK_FREQ / MICRO_PER_SEC) | ||||
| } | ||||
| 
 | ||||
| pub fn get_time_ms() -> usize { | ||||
|     time::read() / (CLOCK_FREQ / MSEC_PER_SEC) | ||||
| } | ||||
					Loading…
					
					
				
		Reference in New Issue