My crappy terminal OS

timer.c 1.1KB

    #include "timer.h" #include "isr.h" #include "ports.h" #include "../libc/function.h" u32 tick = 0; u32 cl_freq = 0; volatile u8 done = 0; u32 countdown = 0; static void timer_callback(registers_t regs) { tick++; if (!done && countdown) countdown--; if (countdown == 0) done = 1; UNUSED(regs); } void init_timer(u32 freq) { cl_freq = 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); } void delay(u32 ms) { if (cl_freq == 0) return; //Timer isn't initialized, so don't delay done = 0; countdown = ms * cl_freq / 1000; while (!done) { //TODO: Lower CPU clock speed to save some power __asm__("nop"); __asm__("nop"); __asm__("nop"); __asm__("nop"); __asm__("nop"); __asm__("nop"); __asm__("nop"); __asm__("nop"); __asm__("nop"); __asm__("nop"); } return; }