-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.c
More file actions
38 lines (30 loc) · 815 Bytes
/
timer.c
File metadata and controls
38 lines (30 loc) · 815 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "timer.h"
#include "idt.h"
#include "pic.h"
#include "io.h"
#include <stdint.h>
#define PIT_CMD 0x43
#define PIT_CH0 0x40
#define PIT_FREQ 1193182
#define TARGET_HZ 100
static volatile uint32_t tick_count = 0;
void irq0_handler(void) {
tick_count++;
pic_send_eoi(0);
}
void timer_init(void) {
uint16_t divisor = PIT_FREQ / TARGET_HZ;
/* Channel 0, lobyte/hibyte, rate generator (mode 2) */
outb(PIT_CMD, 0x34);
outb(PIT_CH0, (uint8_t)(divisor & 0xFF));
outb(PIT_CH0, (uint8_t)((divisor >> 8) & 0xFF));
/* Install IRQ0 handler at IDT vector 32 */
extern void irq0_stub(void);
idt_set_gate(32, (uint32_t)irq0_stub, 0x08, 0x8E);
}
uint32_t timer_get_uptime(void) {
return tick_count / TARGET_HZ;
}
uint32_t timer_get_ticks(void) {
return tick_count;
}