rtc.c (893B)
1 /* 2 * core/rtc.c 3 * 4 * Copyright (C) 2009 stateless 5 */ 6 7 #include <rtc.h> 8 #include <common.h> 9 #include <x86.h> 10 #include <idt.h> 11 #include <tss.h> 12 #include <tty.h> 13 14 volatile uint32_t systimer = 0; 15 16 static uint32_t curr_freq = 100; 17 18 void 19 rtc_callback(__attribute__ ((unused)) struct trapframe_t *regs) 20 { 21 ++systimer; 22 if (systimer % 500) 23 blink(); 24 schedule(); 25 } 26 27 void 28 init_rtc(uint32_t freq) 29 { 30 register_isr_handler(32, rtc_callback); 31 32 curr_freq = freq; 33 uint32_t divisor = 1193182 / freq; 34 /* channel: 0, access mode: lo/hi byte, operating mode: square wave, binary mode */ 35 outb(0x43, 0x36); 36 outb(0x40, (divisor & 0xff)); 37 outb(0x40, ((divisor >> 8) & 0xff)); 38 } 39 40 void 41 sleep(uint32_t seconds) 42 { 43 while (seconds--) { 44 uint32_t now = systimer; 45 while (systimer < now + curr_freq) 46 ; 47 } 48 } 49 50 void 51 msleep(uint32_t msec) 52 { 53 uint32_t s = systimer; 54 while (systimer < s + msec) 55 ; 56 return; 57 } 58