cfenollosa_os-tutorial/21-shell/cpu/timer.c

27 lines
622 B
C
Raw Normal View History

2015-03-20 18:20:44 +00:00
#include "timer.h"
#include "isr.h"
2015-03-23 15:08:34 +00:00
#include "ports.h"
2015-03-23 19:03:41 +00:00
#include "../libc/function.h"
2015-03-20 18:20:44 +00:00
u32 tick = 0;
2015-03-23 19:03:41 +00:00
static void timer_callback(registers_t regs) {
2015-03-20 18:20:44 +00:00
tick++;
2015-03-23 19:03:41 +00:00
UNUSED(regs);
2015-03-20 18:20:44 +00:00
}
void init_timer(u32 freq) {
/* Install the function we just wrote */
register_interrupt_handler(IRQ0, timer_callback);
/* Get the PIT value: hardware clock at 1193180 Hz */
u32 divisor = 1193180 / freq;
u8 low = (u8)(divisor & 0xFF);
u8 high = (u8)( (divisor >> 8) & 0xFF);
/* Send the command */
port_byte_out(0x43, 0x36); /* Command port */
port_byte_out(0x40, low);
port_byte_out(0x40, high);
}