timer.c 745 B

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