timer.c 555 B

1234567891011121314151617181920212223
  1. #include "timer.h"
  2. #include "isr.h"
  3. u32 tick = 0;
  4. static void timer_callback(registers_t regs) {
  5. tick++;
  6. }
  7. void init_timer(u32 freq) {
  8. /* Install the function we just wrote */
  9. register_interrupt_handler(IRQ0, timer_callback);
  10. /* Get the PIT value: hardware clock at 1193180 Hz */
  11. u32 divisor = 1193180 / freq;
  12. u8 low = (u8)(divisor & 0xFF);
  13. u8 high = (u8)( (divisor >> 8) & 0xFF);
  14. /* Send the command */
  15. port_byte_out(0x43, 0x36); /* Command port */
  16. port_byte_out(0x40, low);
  17. port_byte_out(0x40, high);
  18. }