root / lab2 / timer.c @ 4
History | View | Annotate | Download (6 KB)
1 |
#include <lcom/lcf.h> |
---|---|
2 |
#include <lcom/timer.h> |
3 |
|
4 |
#include <stdint.h> |
5 |
|
6 |
#include "i8254.h" |
7 |
|
8 |
unsigned int count=0; //Initializing counter |
9 |
int hook_id = TIMER0_IRQ;
|
10 |
|
11 |
int (timer_set_frequency)(uint8_t timer, uint32_t freq) {
|
12 |
// freq=TIMER_FREQ/div, where TIMER_FREQ is the frequency of the clock and div os the value loaded nittially in the timer
|
13 |
uint16_t div = (TIMER_FREQ /freq); |
14 |
|
15 |
uint8_t msb, lsb; |
16 |
|
17 |
util_get_LSB(div,&lsb); //getting LSB
|
18 |
util_get_MSB(div,&msb); //getting MSB
|
19 |
|
20 |
uint8_t st; |
21 |
|
22 |
if(timer_get_conf(timer,&st)!=0){ |
23 |
printf("Error getting conf in frequency set\n");
|
24 |
return 1; |
25 |
} |
26 |
if(timer<0|timer>2){ |
27 |
printf("Error selecting timer in frequency set\n");
|
28 |
return 1; |
29 |
} |
30 |
else
|
31 |
{ |
32 |
if(timer==0){ |
33 |
st= ((st & 0x0F)|TIMER_LSB_MSB|TIMER_SEL0); //The mask 0x0f is used so that the 4 LSBs are not changed |
34 |
sys_outb(TIMER_CTRL,st); //Sends control word to program the timer
|
35 |
sys_outb(TIMER_0,lsb); |
36 |
sys_outb(TIMER_0,msb); //Both these functions program the timer
|
37 |
} |
38 |
else if(timer==1){ |
39 |
st= ((st & 0x0F)|TIMER_LSB_MSB|TIMER_SEL1);
|
40 |
sys_outb(TIMER_CTRL,st); |
41 |
sys_outb(TIMER_1,lsb); |
42 |
sys_outb(TIMER_1,msb); |
43 |
} |
44 |
else if(timer==2){ |
45 |
st= ((st & 0x0F)|TIMER_LSB_MSB|TIMER_SEL2);
|
46 |
sys_outb(TIMER_CTRL,st); |
47 |
sys_outb(TIMER_2,lsb); |
48 |
sys_outb(TIMER_2,msb); |
49 |
} |
50 |
} |
51 |
return 0; |
52 |
} |
53 |
|
54 |
int (timer_subscribe_int)(uint8_t *bit_no) {
|
55 |
*bit_no = BIT(hook_id); |
56 |
if(sys_irqsetpolicy(TIMER0_IRQ,IRQ_REENABLE,&hook_id)==1){ //operation to subscribe int |
57 |
printf("Error subscribing int\n");
|
58 |
return 1; |
59 |
} |
60 |
return 0; |
61 |
} |
62 |
|
63 |
int (timer_unsubscribe_int)() {
|
64 |
if(sys_irqrmpolicy(&hook_id)==1){ |
65 |
printf("Error unsubscribing int\n");
|
66 |
return 1; |
67 |
} |
68 |
return 0; |
69 |
} |
70 |
|
71 |
void (timer_int_handler)() {
|
72 |
count++; |
73 |
} |
74 |
|
75 |
int (timer_get_conf)(uint8_t timer, uint8_t *st) {
|
76 |
|
77 |
uint8_t read_back = TIMER_RB_CMD | TIMER_RB_COUNT_ | TIMER_RB_SEL(timer); //forming the read-back command (TIMER_RB_CMD (bit 7 and 6)
|
78 |
//TIMER_RB_COUNT (BIT 5) and TIMER_RB_SEL (bit timer+1)
|
79 |
|
80 |
if (sys_outb(TIMER_CTRL, read_back)!=1) { //testing for an error |
81 |
switch (timer) {
|
82 |
case 0: |
83 |
if (util_sys_inb(TIMER_0, st)!=0) { //tests if TIMER_0 was chosen |
84 |
printf("Error selecting Timer 0\n");
|
85 |
return 1; |
86 |
} |
87 |
break;
|
88 |
case 1: |
89 |
if (util_sys_inb(TIMER_1, st)!=0) { //tests if TIMER_1 was chosen |
90 |
printf("Error selecting Timer 1\n");
|
91 |
return 1; |
92 |
} |
93 |
break;
|
94 |
case 2: |
95 |
if (util_sys_inb(TIMER_2, st)!=0) { //tests if TIMER_2 was chosen |
96 |
printf("Error selecting Timer 2\n");
|
97 |
return 1; |
98 |
} |
99 |
break;
|
100 |
default:
|
101 |
printf("No clock selected\n"); //happens if timer<0 or timer>2 |
102 |
return 1; |
103 |
} |
104 |
return 0; |
105 |
} |
106 |
printf("Error in get config\n");
|
107 |
return 1; |
108 |
} |
109 |
|
110 |
int(timer_display_conf)(uint8_t timer, uint8_t st, enum timer_status_field field){ |
111 |
|
112 |
if(timer < 0 | timer > 2) //tests if the timer has a correct value |
113 |
return 1; |
114 |
|
115 |
union timer_status_field_val config; //create union (needed for the function at the end) |
116 |
uint8_t aux; |
117 |
aux=st; |
118 |
switch(field){
|
119 |
case tsf_all:
|
120 |
config.byte=aux; |
121 |
break;
|
122 |
case tsf_initial: //Checking init_mode (mask -> 0x30) |
123 |
if((aux & 0x30)==TIMER_LSB){ //if 0x10 |
124 |
config.in_mode=LSB_only; |
125 |
} |
126 |
else if ((aux & 0x30)==TIMER_MSB){ //if 0x20 |
127 |
config.in_mode=MSB_only; |
128 |
} |
129 |
else if((aux & 0x30)==TIMER_LSB_MSB){ //if 0x30 |
130 |
config.in_mode=MSB_after_LSB; |
131 |
} |
132 |
else{
|
133 |
config.in_mode=INVAL_val; |
134 |
printf("Invalid Initialization Mode\n");
|
135 |
return 1; |
136 |
} |
137 |
break;
|
138 |
case tsf_mode: //Checking count_mode (mask -> 0x0E) |
139 |
if((aux & 0x0E)==0){ //if 0x00 |
140 |
config.count_mode=0;
|
141 |
} |
142 |
else if((aux & 0x0E)==2){ |
143 |
config.count_mode=1;
|
144 |
} |
145 |
else if((aux & 0x0E)==4|(aux & 0x0E)==12){ |
146 |
config.count_mode=2;
|
147 |
} |
148 |
else if((aux & 0x0E)==6|(aux & 0x0E)==14){ |
149 |
config.count_mode=3;
|
150 |
} |
151 |
else if((aux & 0x0E)==8){ |
152 |
config.count_mode=4;
|
153 |
} |
154 |
else if((aux & 0x0E)==10){ |
155 |
config.count_mode=5;
|
156 |
} |
157 |
else{
|
158 |
printf("Error in checking counting mode\n");
|
159 |
return 1; |
160 |
} |
161 |
break;
|
162 |
case tsf_base: //Checking bcd (mask -> 0x01) |
163 |
if((aux & 0x01)== TIMER_BCD){ |
164 |
config.bcd = true; //if the last bit is 1, it's bcd |
165 |
} |
166 |
else if((aux & 0x01)==TIMER_BIN){ |
167 |
config.bcd = false; //if the last bit is 0, it's binary |
168 |
} |
169 |
else{
|
170 |
printf("Error in checking base\n");
|
171 |
return 1; |
172 |
} |
173 |
break;
|
174 |
default:
|
175 |
return 1; |
176 |
|
177 |
} |
178 |
|
179 |
if(timer_print_config(timer, field, config)!=0){ |
180 |
printf("Error printing\n");
|
181 |
return 1; //function that prints the result |
182 |
} |
183 |
|
184 |
|
185 |
return 0; |
186 |
|
187 |
} |