From 416b4337c784511c03aef9c3d6667f55cbf77869 Mon Sep 17 00:00:00 2001 From: Malo Lecomte Date: Sat, 17 Jul 2021 11:56:34 +0200 Subject: [PATCH] feat(pit): add pit functions --- k/Makefile | 1 + k/pic/pit.c | 24 ++++++++++++++++++++++++ k/pic/pit.h | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 k/pic/pit.c create mode 100644 k/pic/pit.h diff --git a/k/Makefile b/k/Makefile index e6346e0..ba94c04 100644 --- a/k/Makefile +++ b/k/Makefile @@ -36,6 +36,7 @@ OBJS = \ isr.o \ pic/pic.o \ pic/keyboard.o \ + pic/pit.o \ utils/ring_buffer.o diff --git a/k/pic/pit.c b/k/pic/pit.c new file mode 100644 index 0000000..2d72bad --- /dev/null +++ b/k/pic/pit.c @@ -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; +} diff --git a/k/pic/pit.h b/k/pic/pit.h new file mode 100644 index 0000000..2b2c3e8 --- /dev/null +++ b/k/pic/pit.h @@ -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 + +void pit_handler(void); +void init_pit(void); +uint64_t gettick(void); + +#endif