44mod allocator;
55mod frame;
66mod pci;
7+ use core:: hint:: spin_loop;
78use core:: panic:: PanicInfo ;
9+ use core:: time:: Duration ;
810
911use frame:: frame_alloc;
12+ use polyhal:: irq:: IRQ ;
1013use polyhal:: mem:: { get_fdt, get_mem_areas} ;
1114use polyhal:: percpu:: get_local_thread_pointer;
15+ use polyhal:: timer:: { self , current_time} ;
1216use polyhal:: {
1317 common:: PageAlloc ,
1418 instruction:: { ebreak, shutdown} ,
@@ -31,6 +35,9 @@ impl PageAlloc for PageAllocImpl {
3135 }
3236}
3337
38+ #[ percpu]
39+ static mut TIMER_TICKS : usize = 0 ;
40+
3441/// kernel interrupt
3542#[ polyhal:: arch_interrupt]
3643fn kernel_interrupt ( ctx : & mut TrapFrame , trap_type : TrapType ) {
@@ -51,7 +58,10 @@ fn kernel_interrupt(ctx: &mut TrapFrame, trap_type: TrapType) {
5158 log:: info!( "illegal instruction" ) ;
5259 }
5360 Timer => {
54- log:: info!( "Timer" ) ;
61+ TIMER_TICKS . with_mut ( |ticks| * ticks += 1 ) ;
62+ let next = current_time ( ) + Duration :: from_millis ( 100 ) ;
63+ timer:: set_next_timer ( next) ;
64+ log:: info!( "Timer: {:#x?} next: {:#x?}" , TIMER_TICKS . read( ) , next) ;
5565 }
5666 _ => {
5767 log:: warn!( "unsuspended trap type: {:?}" , trap_type) ;
@@ -87,8 +97,18 @@ fn main(hartid: usize) {
8797
8898 // Test BreakPoint Interrupt
8999 ebreak ( ) ;
90-
91100 crate :: pci:: init ( ) ;
101+
102+ log:: info!( "Waiting for TIMER_TICKS greater than 5" ) ;
103+ IRQ :: int_enable ( ) ;
104+ let target = current_time ( ) + Duration :: from_millis ( 1 ) ;
105+ timer:: set_next_timer ( target) ;
106+ log:: info!( "current: {:#x?} target: {:#x?}" , current_time( ) , target) ;
107+
108+ while * TIMER_TICKS <= 5 {
109+ spin_loop ( ) ;
110+ }
111+
92112 log:: info!( "Run END. Shutdown successfully." ) ;
93113 shutdown ( ) ;
94114}
0 commit comments