Luna/kernel/src/thread/PIT.cpp
2022-09-21 17:56:53 +02:00

30 lines
701 B
C++

#define MODULE "pit"
#include "thread/PIT.h"
#include "io/IO.h"
#include "log/Log.h"
#define PIT_CHANNEL_0_PORT 0x40
volatile uint64_t PIT::ms_since_boot = 0;
static uint16_t divisor = 65535;
void PIT::initialize(uint64_t frequency)
{
divisor = base_frequency / frequency;
kdbgln("Configuring PIT to use divisor %d (will tick %lu times per second)", divisor, frequency);
if (divisor < 100) divisor = 100;
IO::outb(PIT_CHANNEL_0_PORT, (uint8_t)(divisor & 0xFF));
IO::delay();
IO::outb(PIT_CHANNEL_0_PORT, (uint8_t)((divisor & 0xFF00) >> 8));
}
uint64_t PIT::frequency()
{
return base_frequency / divisor;
}
void PIT::tick()
{
ms_since_boot += 1000 / frequency();
}