Added a math library, with clamp and POW. added printf support for printing doubles added a few helper functions in PIT for calcaulting the time in seconds of a clock cycle based on divisor
65 lines
1.6 KiB
C
65 lines
1.6 KiB
C
#include <math.h>
|
|
|
|
#include <kernel/x86/pit.h>
|
|
#include <kernel/x86/io.h>
|
|
|
|
struct pit_state pit;
|
|
|
|
uint32_t normalize_divisor(uint32_t divisor)
|
|
{
|
|
if (divisor == 0)
|
|
return 65536;
|
|
return clamp_u32(divisor, 0, 65535);
|
|
}
|
|
|
|
double get_time_from_divisor(uint32_t divisor)
|
|
{
|
|
uint32_t _divisor = normalize_divisor(divisor);
|
|
|
|
double clock_rate = 3579545.0 / 3.0; // This is for a bit more precision in the hz rate
|
|
|
|
return (_divisor / (clock_rate)) * 1000;
|
|
}
|
|
|
|
/**
|
|
* Im worried about an overflow
|
|
*
|
|
*
|
|
* What i want to do is
|
|
*
|
|
* Every time we get the Timer intterupt, i want to increment a variable, just the amount of times its fired
|
|
* and then we can use the time rate above to get total time elapsed, which we can use for sleep and stuff
|
|
*
|
|
* Im worried about overflows, but i can add something that will automatically flip it over? or do uints already roll over? they already rollover!!! WOOOO!!!
|
|
* just need to be aware that only a maximum of 52ms * 32int max (64 int max) can be tracked before it rolls over!
|
|
*/
|
|
|
|
|
|
uint16_t read_pit_count(uint8_t channel) // Only in lobyte/hibyte mode
|
|
|
|
{
|
|
uint16_t count = 0;
|
|
|
|
__asm__ volatile ("cli");
|
|
|
|
|
|
outb(PIT_CMD_REG, (channel << 6)); // xx000000 where x is the channel
|
|
|
|
count = inb(channel);
|
|
count |= inb(channel) << 8;
|
|
|
|
return count; // TODO make sure to enable interrupts after this
|
|
}
|
|
|
|
void set_pit_count(uint8_t channel, uint16_t count) // Only in lobyte/hibyte mode
|
|
{
|
|
__asm__ volatile ("cli");
|
|
|
|
outb(channel, count & 0xFF); // low byte
|
|
outb(channel, (count & 0xFF00) >> 8); // high byte
|
|
|
|
// TODO: make sure to set interrupts
|
|
}
|
|
|
|
|