os/timer.c

33 lines
576 B
C
Raw Normal View History

2024-05-21 18:41:16 +03:00
#include "timer.h"
#include "io.h"
#include "sys.h"
2024-05-23 20:59:18 +03:00
#include "interrupts.h"
2024-05-21 18:41:16 +03:00
volatile uint32_t tick = 0;
2024-05-23 20:59:18 +03:00
void pit_handler(Stack *registers)
2024-05-21 18:41:16 +03:00
{
2024-05-23 20:59:18 +03:00
++tick;
2024-05-22 00:15:05 +03:00
}
2024-05-21 18:41:16 +03:00
void delay(uint32_t milliseconds)
{
uint32_t end = tick + milliseconds;
while (tick < end)
2024-05-23 20:59:18 +03:00
;
2024-05-21 18:41:16 +03:00
}
void pit_set_frequency(uint32_t frequency)
{
uint32_t divisor = PIT_FREQUENCY / frequency;
outb(PIT_COMMAND, 0x36);
outb(PIT_CHANNEL_0, divisor & 0xFF);
2024-05-23 20:59:18 +03:00
outb(PIT_CHANNEL_0, divisor >> 8);
2024-05-21 18:41:16 +03:00
}
void timer_install(void)
{
2024-05-23 20:59:18 +03:00
irq_install_handler(0, pit_handler);
2024-05-21 18:41:16 +03:00
pit_set_frequency(1000);
2024-05-23 20:59:18 +03:00
}