forked from uiaict/2023-ikt218-osdev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pit.c
91 lines (48 loc) · 1.47 KB
/
pit.c
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <pit.h>
#include <system.h>
#include <terminal.h>
#include <isr.h>
uint32_t tick = 0;
static void timer_callback(registers_t regs)
{
tick++;
//terminal_writestring("Tick: ");
// terminal_write_dec(tick);
// terminal_writestring("\n");
}
void init_pit()
{
register_interrupt_handler(IRQ0, &timer_callback);
uint32_t divisor = PIT_DEFAULT_DIVISOR ;
// Send the command byte.
outb(PIT_CMD_PORT, 0x36);
// Divisor has to be sent byte-wise, so split here into upper/lower bytes.
// Send the frequency divisor.
outb(PIT_CHANNEL0_PORT, divisor & 0xFF);
outb(PIT_CHANNEL0_PORT, divisor>>8);
}
uint32_t get_current_tick(){
return tick;
}
void sleep_busy(uint32_t milliseconds){
uint32_t start_tick = get_current_tick();
uint32_t ticks_to_wait = milliseconds * TICKS_PER_MS;
uint32_t elapsed_ticks =0;
while (elapsed_ticks < ticks_to_wait){
asm("nop");
elapsed_ticks++;
// terminal_writestring("sleeping \n");
}
// terminal_writestring("I am awake. yayy");
// terminal_initialize();
}
void sleep_interrupt(uint32_t milliseconds){
uint32_t current_tick = get_current_tick();
uint32_t ticks_to_wait= milliseconds * TICKS_PER_MS;
uint32_t end_ticks= current_tick+ticks_to_wait ;
while (current_tick<end_ticks ){
asm volatile("sti");
asm volatile("hlt");
current_tick = get_current_tick();
}
}