LumaOS  v0.1.116
An experimental x86-64 OS
Loading...
Searching...
No Matches
pit.c
Go to the documentation of this file.
1
9
10#include <arch/x86_64/pit.h>
11
12#include "arch/x86_64/io.h"
13#include "arch/x86_64/irq.h"
14#include "arch/x86_64/pic.h"
15
16volatile uint64_t pit_ticks = 0;
17volatile uint16_t pit_frequency = 0;
18
23static void timer_irq(struct interrupt_frame *frame)
24{
25 (void)frame;
26 pit_ticks++;
27}
28
29void pit_init(uint16_t frequency)
30{
31 if (frequency == 0) return;
32
33 uint16_t divisor = 1193182 / frequency;
34
35 outb(0x43, 0x36); // PIT command byte
36 outb(0x40, divisor & 0xFF); // Send the divisor low byte
37 outb(0x40, divisor >> 8); // Send the divisor high byte
38
39 pit_frequency = frequency;
40
41 irq_register(0, timer_irq); // Register the handler with the irq handler
43}
44
45void pit_sleep_ms(uint32_t ms)
46{
47 uint64_t target = pit_ticks + (ms * pit_frequency / 1000);
48
49 while (pit_ticks < target)
50 asm volatile ("hlt");
51}
Provides utilities for IO operations with the hardware.
void outb(uint16_t port, uint8_t value)
Outputs a byte to the specified port.
Definition io.h:22
void irq_register(uint8_t irq, irq_handler_fn fn)
Registers a handler function for the specified IRQ number.
Definition irq.c:18
IRQ Handler and utilities.
void pic_clear_mask(uint8_t irq)
Unmasks (Enables) a line.
Definition pic.c:79
Handles the legacy PIC for IRQ interrupts.
static void timer_irq(struct interrupt_frame *frame)
Handles the timer.
Definition pit.c:23
void pit_init(uint16_t frequency)
Initialize the PIT timer.
Definition pit.c:29
void pit_sleep_ms(uint32_t ms)
Sleep for a certain time using the PIT.
Definition pit.c:45
Handles timer interrupts from the PIT (Programmable Interrupt Timer).
The frame holding the registers when an interrupt happens.
Definition idt.h:16