cfenollosa_os-tutorial/20-interrupts-timer/cpu/timer.c

32 lines
745 B
C
Raw Normal View History

2015-03-17 19:47:43 +00:00
#include "timer.h"
#include "../drivers/screen.h"
2015-03-19 19:07:50 +00:00
#include "../kernel/util.h"
2015-03-17 19:47:43 +00:00
#include "isr.h"
u32 tick = 0;
static void timer_callback(registers_t regs) {
tick++;
kprint("Tick: ");
2015-03-19 19:07:50 +00:00
2015-03-20 15:42:19 +00:00
char tick_ascii[256];
2015-03-19 19:07:50 +00:00
int_to_ascii(tick, tick_ascii);
kprint(tick_ascii);
2015-03-17 19:47:43 +00:00
kprint("\n");
}
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);
}