Files
Espresso/drivers/pit.c

44 lines
845 B
C
Raw Permalink Normal View History

2025-06-17 15:50:07 -05:00
#include <types.h>
2025-06-13 18:03:39 -05:00
#include <port_io.h>
2025-06-17 15:50:07 -05:00
#include <stdio.h>
2025-06-13 18:03:39 -05:00
2025-06-17 15:50:07 -05:00
#include <drivers/pit.h>
2025-06-13 18:03:39 -05:00
2025-06-17 15:50:07 -05:00
#define PIT_CHANNEL0 0x40
#define PIT_COMMAND 0x43
#define PIT_IRQ_LINE 0
#define PIT_FREQUENCY_BASE 1193182
2025-06-13 18:03:39 -05:00
2025-06-17 15:50:07 -05:00
volatile uint64_t pit_ticks = 0;
bool pit_initialized = false;
void pit_init(void)
2025-06-13 18:03:39 -05:00
{
2025-06-17 15:50:07 -05:00
uint16_t divisor = (uint16_t)1193;
/* Send command byte */
outb(PIT_COMMAND, 0x36); /* Channel 0, low/high byte, mode 3 (square wave), binary */
2025-06-13 18:03:39 -05:00
2025-06-17 15:50:07 -05:00
/* Send divisor low and high byte */
outb(PIT_CHANNEL0, (uint8_t)(divisor & 0xFF)); /* Low byte */
outb(PIT_CHANNEL0, (uint8_t)((divisor >> 8) & 0xFF)); /* High byte */
pit_initialized = true;
}
2025-06-13 18:03:39 -05:00
2025-06-17 15:50:07 -05:00
void pit_handler(void)
{
pit_ticks++;
2025-06-13 18:03:39 -05:00
}
2025-06-17 15:50:07 -05:00
void pit_sleep(uint64_t ms)
2025-06-13 18:03:39 -05:00
{
2025-06-17 15:50:07 -05:00
uint64_t target = pit_ticks + ms;
while (pit_ticks < target)
{
asm volatile ("hlt");
}
}