Revision 5
Code corrected
lab2/lab2.c | ||
---|---|---|
52 | 52 |
|
53 | 53 |
int(timer_test_time_base)(uint8_t timer, uint32_t freq) { |
54 | 54 |
|
55 |
if(timer_set_frequency(timer, freq)==1){ //tests for error |
|
55 |
if(timer<0|timer>2){ //tests for error in timer selection |
|
56 |
printf("Error selecting timer\n"); |
|
57 |
return 1; |
|
58 |
} |
|
59 |
if(timer_set_frequency(timer, freq)==1){ //tests for error |
|
56 | 60 |
printf("Error testing time base\n"); |
57 | 61 |
return 1; |
58 | 62 |
} |
... | ... | |
64 | 68 |
uint8_t irq_set; |
65 | 69 |
message msg; |
66 | 70 |
|
71 |
if(timer_set_frequency(0, 60)==1){ //tests for error |
|
72 |
printf("Error setting frequency\n"); |
|
73 |
return 1; |
|
74 |
} |
|
75 |
|
|
67 | 76 |
if(timer_subscribe_int(&irq_set)==1){ |
68 | 77 |
printf("Error subscribing int\n"); |
69 | 78 |
return 1; |
70 | 79 |
} |
71 | 80 |
|
72 |
while(count/60.0<time){ //looping until counter reaches the same value as time
|
|
73 |
if ((r = driver_receive(ANY, &msg, &ipc_status))!=0){
|
|
81 |
while(count/sys_hz()<time){ //looping until counter reaches the same value as time
|
|
82 |
if ((r = driver_receive(ANY, &msg, &ipc_status))==1){
|
|
74 | 83 |
printf("driver_receive failed with: %d",r); |
75 | 84 |
continue; |
76 | 85 |
} |
... | ... | |
81 | 90 |
|
82 | 91 |
timer_int_handler(); |
83 | 92 |
|
84 |
if((count%60)==0){
|
|
93 |
if((count%sys_hz())==0){
|
|
85 | 94 |
timer_print_elapsed_time(); //prints message each second; |
86 | 95 |
} |
87 | 96 |
} |
... | ... | |
91 | 100 |
} |
92 | 101 |
} |
93 | 102 |
else { //received a standard message, not a notification |
94 |
//no standard messages expected: do nothing
|
|
103 |
//no standard messages expected: do nothing |
|
95 | 104 |
} |
96 | 105 |
} |
97 | 106 |
|
lab2/timer.c | ||
---|---|---|
9 | 9 |
int hook_id = TIMER0_IRQ; |
10 | 10 |
|
11 | 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 |
|
12 |
|
|
13 |
if(freq<19){ |
|
14 |
return 1; |
|
15 |
} |
|
16 |
|
|
17 |
//freq=TIMER_FREQ/div, where TIMER_FREQ is the frequency of the clock and div os the value loaded nittially in the timer |
|
13 | 18 |
uint16_t div = (TIMER_FREQ /freq); |
14 | 19 |
|
15 | 20 |
uint8_t msb, lsb; |
... | ... | |
19 | 24 |
|
20 | 25 |
uint8_t st; |
21 | 26 |
|
22 |
if(timer_get_conf(timer,&st)!=0){
|
|
27 |
if(timer_get_conf(timer,&st)==1){
|
|
23 | 28 |
printf("Error getting conf in frequency set\n"); |
24 | 29 |
return 1; |
25 | 30 |
} |
... | ... | |
30 | 35 |
else |
31 | 36 |
{ |
32 | 37 |
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
|
|
38 |
st= ((st & 0x0F)|TIMER_LSB_MSB|TIMER_SEL0); //The mask 0x0F is used so that the 4 LSBs are not changed
|
|
34 | 39 |
sys_outb(TIMER_CTRL,st); //Sends control word to program the timer |
35 | 40 |
sys_outb(TIMER_0,lsb); |
36 | 41 |
sys_outb(TIMER_0,msb); //Both these functions program the timer |
... | ... | |
74 | 79 |
|
75 | 80 |
int (timer_get_conf)(uint8_t timer, uint8_t *st) { |
76 | 81 |
|
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)
|
|
82 |
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 | 83 |
//TIMER_RB_COUNT (BIT 5) and TIMER_RB_SEL (bit timer+1) |
79 | 84 |
|
80 |
if (sys_outb(TIMER_CTRL, read_back)!=1) { //testing for an error
|
|
85 |
if (sys_outb(TIMER_CTRL, read_back)==0) { //testing for an error
|
|
81 | 86 |
switch (timer) { |
82 | 87 |
case 0: |
83 |
if (util_sys_inb(TIMER_0, st)!=0) { //tests if TIMER_0 was chosen
|
|
88 |
if (util_sys_inb(TIMER_0, st)==1) { //tests if TIMER_0 was chosen
|
|
84 | 89 |
printf("Error selecting Timer 0\n"); |
85 | 90 |
return 1; |
86 | 91 |
} |
87 | 92 |
break; |
88 | 93 |
case 1: |
89 |
if (util_sys_inb(TIMER_1, st)!=0) { //tests if TIMER_1 was chosen
|
|
94 |
if (util_sys_inb(TIMER_1, st)==1) { //tests if TIMER_1 was chosen
|
|
90 | 95 |
printf("Error selecting Timer 1\n"); |
91 | 96 |
return 1; |
92 | 97 |
} |
93 | 98 |
break; |
94 | 99 |
case 2: |
95 |
if (util_sys_inb(TIMER_2, st)!=0) { //tests if TIMER_2 was chosen
|
|
100 |
if (util_sys_inb(TIMER_2, st)==1) { //tests if TIMER_2 was chosen
|
|
96 | 101 |
printf("Error selecting Timer 2\n"); |
97 | 102 |
return 1; |
98 | 103 |
} |
... | ... | |
103 | 108 |
} |
104 | 109 |
return 0; |
105 | 110 |
} |
111 |
|
|
106 | 112 |
printf("Error in get config\n"); |
107 | 113 |
return 1; |
108 | 114 |
} |
109 | 115 |
|
110 | 116 |
int(timer_display_conf)(uint8_t timer, uint8_t st, enum timer_status_field field){ |
111 | 117 |
|
112 |
if(timer < 0 | timer > 2) //tests if the timer has a correct value |
|
113 |
return 1; |
|
118 |
if(timer<0|timer>2){ //tests for error in timer selection |
|
119 |
printf("Error selecting timer\n"); |
|
120 |
return 1; |
|
121 |
} |
|
114 | 122 |
|
115 | 123 |
union timer_status_field_val config; //create union (needed for the function at the end) |
116 | 124 |
uint8_t aux; |
117 | 125 |
aux=st; |
126 |
|
|
118 | 127 |
switch(field){ |
119 | 128 |
case tsf_all: |
120 | 129 |
config.byte=aux; |
121 | 130 |
break; |
131 |
|
|
122 | 132 |
case tsf_initial: //Checking init_mode (mask -> 0x30) |
123 | 133 |
if((aux & 0x30)==TIMER_LSB){ //if 0x10 |
124 | 134 |
config.in_mode=LSB_only; |
... | ... | |
135 | 145 |
return 1; |
136 | 146 |
} |
137 | 147 |
break; |
148 |
|
|
138 | 149 |
case tsf_mode: //Checking count_mode (mask -> 0x0E) |
139 | 150 |
if((aux & 0x0E)==0){ //if 0x00 |
140 | 151 |
config.count_mode=0; |
141 | 152 |
} |
142 |
else if((aux & 0x0E)==2){ |
|
153 |
else if((aux & 0x0E)==2){ //if 0x02
|
|
143 | 154 |
config.count_mode=1; |
144 | 155 |
} |
145 |
else if((aux & 0x0E)==4|(aux & 0x0E)==12){ |
|
156 |
else if((aux & 0x0E)==4|(aux & 0x0E)==12){ //if 0x04 or 0x0C
|
|
146 | 157 |
config.count_mode=2; |
147 | 158 |
} |
148 |
else if((aux & 0x0E)==6|(aux & 0x0E)==14){ |
|
159 |
else if((aux & 0x0E)==6|(aux & 0x0E)==14){ //if 0x06 or 0x0E
|
|
149 | 160 |
config.count_mode=3; |
150 | 161 |
} |
151 |
else if((aux & 0x0E)==8){ |
|
162 |
else if((aux & 0x0E)==8){ //if 0x08
|
|
152 | 163 |
config.count_mode=4; |
153 | 164 |
} |
154 |
else if((aux & 0x0E)==10){ |
|
165 |
else if((aux & 0x0E)==10){ //if 0x0A
|
|
155 | 166 |
config.count_mode=5; |
156 | 167 |
} |
157 | 168 |
else{ |
... | ... | |
159 | 170 |
return 1; |
160 | 171 |
} |
161 | 172 |
break; |
173 |
|
|
162 | 174 |
case tsf_base: //Checking bcd (mask -> 0x01) |
163 | 175 |
if((aux & 0x01)== TIMER_BCD){ |
164 |
config.bcd = true; //if the last bit is 1, it's bcd |
|
176 |
config.bcd = true; //if the last bit is 1, it's bcd
|
|
165 | 177 |
} |
166 | 178 |
else if((aux & 0x01)==TIMER_BIN){ |
167 | 179 |
config.bcd = false; //if the last bit is 0, it's binary |
... | ... | |
171 | 183 |
return 1; |
172 | 184 |
} |
173 | 185 |
break; |
186 |
|
|
174 | 187 |
default: |
175 | 188 |
return 1; |
176 | 189 |
|
177 | 190 |
} |
178 | 191 |
|
179 |
if(timer_print_config(timer, field, config)!=0){
|
|
192 |
if(timer_print_config(timer, field, config)==1){ //function that prints the result
|
|
180 | 193 |
printf("Error printing\n"); |
181 |
return 1; //function that prints the result
|
|
194 |
return 1;
|
|
182 | 195 |
} |
183 |
|
|
184 | 196 |
|
185 | 197 |
return 0; |
186 |
|
|
187 | 198 |
} |
lab2/utils.c | ||
---|---|---|
17 | 17 |
|
18 | 18 |
int (util_sys_inb)(int port, uint8_t *value) { //transform 8 bit into 32 bit |
19 | 19 |
|
20 |
uint32_t new_val; //initializing 32 bit variable |
|
20 |
uint32_t new_val; //initializing 32 bit variable
|
|
21 | 21 |
|
22 |
if(sys_inb(port,&new_val)!=0){ //verifies if there is an error |
|
22 |
if(sys_inb(port,&new_val)!=0){ //verifies if there is an error
|
|
23 | 23 |
printf("Error in util_sys_inb\n"); |
24 | 24 |
return 1; |
25 | 25 |
} |
26 |
*value=new_val & 0xFF; // dereferencing "value"
|
|
26 |
*value=new_val & 0xFF; //dereferencing "value"
|
|
27 | 27 |
|
28 | 28 |
return 0; |
29 | 29 |
} |
Also available in: Unified diff