kernel: Rework the timer subsystem to count in microseconds
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-05-07 21:51:36 +02:00
parent 9bab4c62a1
commit 916a73ca95
Signed by: apio
GPG Key ID: B8A7D06E42258954
3 changed files with 15 additions and 19 deletions

View File

@ -58,7 +58,7 @@ namespace Timer
{
void tick()
{
timer_ticks++;
timer_ticks += ARCH_TIMER_RESOLUTION;
}
usize raw_ticks()
@ -68,20 +68,17 @@ namespace Timer
usize ticks()
{
return ticks_ms() / 1000;
return ticks_us() / US_PER_SECOND;
}
usize ticks_ms()
{
return timer_ticks / ARCH_TIMER_FREQ;
return timer_ticks / 1000;
}
usize ticks_us() // We want a bit of precision; if there are 10 ticks/ms, do not return the truncated ms value *
// 1000, but ticks * 100 (1000/10), which is more precise
usize ticks_us()
{
if constexpr (ARCH_TIMER_FREQ > 1000) return timer_ticks / (ARCH_TIMER_FREQ / 1000);
else
return timer_ticks * (1000 / ARCH_TIMER_FREQ);
return timer_ticks;
}
usize ticks_ns()
@ -91,22 +88,22 @@ namespace Timer
usize boot()
{
return boot_timestamp;
return boot_timestamp / US_PER_SECOND;
}
usize boot_ms()
{
return boot_timestamp * MS_PER_SECOND;
return boot_timestamp / 1000;
}
usize boot_us()
{
return boot_timestamp * US_PER_SECOND;
return boot_timestamp;
}
usize boot_ns()
{
return boot_timestamp * NS_PER_SECOND;
return boot_timestamp * 1000;
}
usize clock()
@ -131,14 +128,12 @@ namespace Timer
void init()
{
boot_timestamp = bootloader_time_to_unix(bootboot.datetime);
boot_timestamp = bootloader_time_to_unix(bootboot.datetime) * US_PER_SECOND;
arch_init();
}
}
static_assert(IsPowerOfTwo<usize, ARCH_TIMER_FREQ>, "ARCH_TIMER_FREQ must be a power of two");
bool should_invoke_scheduler()
{
return (timer_ticks % ARCH_TIMER_FREQ) == 0;
return (timer_ticks % 1000) == 0;
}

View File

@ -7,8 +7,8 @@ const u64 base_frequency = 1193182;
void Timer::arch_init()
{
constexpr u16 divisor = (u16)(base_frequency / (ARCH_TIMER_FREQ * 1000));
static_assert(divisor >= 100, "ARCH_TIMER_FREQ is too high");
constexpr u16 divisor = (u16)(base_frequency / ((1000 / ARCH_TIMER_RESOLUTION) * 1000));
static_assert(divisor >= 100, "ARCH_TIMER_RESOLUTION is too low");
IO::outb(PIT_CHANNEL_0, (u8)(divisor & 0xFF));
IO::outb(0x80, 0); // short delay
IO::outb(PIT_CHANNEL_0, (u8)((divisor & 0xFF00) >> 8));

View File

@ -1,4 +1,5 @@
#pragma once
#include <luna/Types.h>
const usize ARCH_TIMER_FREQ = 4;
// Every timer tick is equivalent to 250 microseconds.
const usize ARCH_TIMER_RESOLUTION = 250;