forked from cfenollosa/os-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.c
More file actions
26 lines (21 loc) · 658 Bytes
/
timer.c
File metadata and controls
26 lines (21 loc) · 658 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
#include "timer.h"
#include "isr.h"
#include "ports.h"
#include "../libc/function.h"
uint32_t tick = 0;
static void timer_callback(registers_t *regs) {
tick++;
UNUSED(regs);
}
void init_timer(uint32_t freq) {
/* Install the function we just wrote */
register_interrupt_handler(IRQ0, timer_callback);
/* Get the PIT value: hardware clock at 1193180 Hz */
uint32_t divisor = 1193180 / freq;
uint8_t low = (uint8_t)(divisor & 0xFF);
uint8_t high = (uint8_t)( (divisor >> 8) & 0xFF);
/* Send the command */
port_byte_out(0x43, 0x36); /* Command port */
port_byte_out(0x40, low);
port_byte_out(0x40, high);
}