-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.c
More file actions
97 lines (86 loc) · 1.97 KB
/
timer.c
File metadata and controls
97 lines (86 loc) · 1.97 KB
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
92
93
94
95
96
97
/*NOTES
* the pit channel 2 gate is controlled by io port 0x61 bit 0
*channel 0 - connected to irq0
*channel 1 - unusable
*channel 2 - pc speaker but can be used for other purposes
*we need to decide how we are going to program the poit usign port 0x61
* if a frequency of 100hz is desired the divisor needed is 11913182/100 = 11931- split this value into a high and low byte
*/
#include "timer.h"
volatile int pit_read_flag = 0;
int timer_ticks = 0;
void init_timer()
{
unsigned long flags;
cli_and_save(flags);
enable_irq(TIMER_IRQ);
/*asm volatile (
"movl $0x10000, %%eax;"
"cmpl $18, %%ebx;")*/
restore_flags(flags);
}
void handle_tick(void)
{
//"r" (irq0_frequency), "r" (irq0_ms), "r" (system_timer_fractions), "r" (system_timer_ms)
//int system_timer_fractions = 0;
//int system_timer_ms = 0;
//int irq0_fractions = 0;
//int irq0_ms = 0;
//int irq0_frequency = 0;
//int pit_reload_value = 0;
timer_ticks++;
//if (timer_ticks % 18 == 0)
//printf("tick %d\n",timer_ticks );
pit_read_flag = 0;
/*asm volatile(
"leave ;"
"iret ;"
:
:
:"memory");
*/
/*asm volatile(
"PUSHL %%eax ;"
"PUSHL %%ebx ;"
"MOVL $0, %%eax ;"
"MOVL $0, %%ebx ;"
"ADDL $0, %%eax ;"
"ADDL $0, %%ebx ;"
"MOVB $0x20, %%al;"
"OUTB %%al, $0x20;"
"POPL %%ebx;"
"POPL %%eax;"
"IRET;"
:
:
:"%eax" , "%ebx"
);*/
}
int32_t timer_set(int hz)
{
if (hz < 0)
{
return -1 ;
}
int divisor = MAX_FREQ_PIT / hz;
outb(0x36, PIT_CMDREG);
outb(divisor & 0xFF, PIT_CHANNEL0);
outb(divisor>>8, PIT_CHANNEL0);
return 0;
}
void speaker_set(int hz)
{
int divisor = MAX_FREQ_PIT/ hz;
outb(0xb6, PIT_CMDREG);
outb((unsigned char) (divisor), PIT_CHANNEL2);
outb((unsigned char) (divisor>>8), PIT_CHANNEL2);
}
int32_t pit_read()
{
unsigned long flags;
cli_and_save(flags);
pit_read_flag = 1;
restore_flags(flags);
while (pit_read_flag != 0 ) {}//keep spinning
return 0;
}