1
0

timer.c 558 B

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