timer.c 644 B

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