timer.c 658 B

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