feat(pit): add pit functions

This commit is contained in:
Malo Lecomte 2021-07-17 11:56:34 +02:00
parent 26bdf84db4
commit 416b4337c7
3 changed files with 63 additions and 0 deletions

@ -36,6 +36,7 @@ OBJS = \
isr.o \ isr.o \
pic/pic.o \ pic/pic.o \
pic/keyboard.o \ pic/keyboard.o \
pic/pit.o \
utils/ring_buffer.o utils/ring_buffer.o

24
k/pic/pit.c Normal file

@ -0,0 +1,24 @@
#include "io.h"
#include "pit.h"
static uint64_t counter = 0;
void pit_handler(void)
{
counter++;
}
void init_pit(void)
{
// write to control register
outb(PIT_CONTROL_REG, PIT_SET_COUNTER_0 | PIT_MODE_2 | PIT_RW_LSB_MSB);
// write frequency
outb(PIT_COUNTER_0, DIVIDER_LO);
outb(PIT_COUNTER_0, DIVIDER_HI);
}
uint64_t gettick(void)
{
return counter * 10;
}

38
k/pic/pit.h Normal file

@ -0,0 +1,38 @@
#ifndef PIT_H
#define PIT_H
#define BCD_SET 0x1
#define PIT_MODE_0 (0x0 << 1)
#define PIT_MODE_1 (0x1 << 1)
#define PIT_MODE_2 (0x2 << 1)
#define PIT_MODE_3 (0x3 << 1)
#define PIT_MODE_4 (0x4 << 1)
#define PIT_MODE_5 (0x5 << 1)
#define PIT_RW_SPECIAL (0x0 << 4)
#define PIT_RW_LSB (0x1 << 4)
#define PIT_RW_MSB (0x2 << 4)
#define PIT_RW_LSB_MSB (0x3 << 4)
#define PIT_SET_COUNTER_0 (0x0 << 6)
#define PIT_SET_COUNTER_1 (0x1 << 6)
#define PIT_SET_COUNTER_2 (0x2 << 6)
// port definition
#define PIT_COUNTER_0 0x40
#define PIT_COUNTER_1 0x41
#define PIT_COUNTER_2 0x42
#define PIT_CONTROL_REG 0x43
// to get 100Hz
#define DIVIDER_LO (11931 & 0xff)
#define DIVIDER_HI (11931 >> 8)
#include <stdint.h>
void pit_handler(void);
void init_pit(void);
uint64_t gettick(void);
#endif