root / lab2 / timer.c @ 13
History | View | Annotate | Download (2.89 KB)
1 |
#include <lcom/lcf.h> |
---|---|
2 |
#include <lcom/timer.h> |
3 |
|
4 |
#include <stdint.h> |
5 |
|
6 |
#include "i8254.h" |
7 |
|
8 |
int (timer_set_frequency)(uint8_t timer, uint32_t freq) {
|
9 |
uint8_t status = 0;
|
10 |
if (timer_get_conf(timer, &status)) return 1; |
11 |
//Make command
|
12 |
uint8_t write_cmd = 0;
|
13 |
//Select timer
|
14 |
switch(timer) {
|
15 |
case 0: write_cmd |= TIMER_SEL0; break; |
16 |
case 1: write_cmd |= TIMER_SEL1; break; |
17 |
case 2: write_cmd |= TIMER_SEL2; break; |
18 |
default: return 1; break; |
19 |
} |
20 |
//Change both LSB and MSB
|
21 |
write_cmd |= TIMER_LSB_MSB; |
22 |
//Keep 4 least significant bits
|
23 |
write_cmd |= (status & (TIMER_MODE_MASK | TIMER_BCD)); |
24 |
//Write cmd
|
25 |
if (sys_outb(TIMER_CTRL, write_cmd)) return 1; |
26 |
|
27 |
// counter_init = clock/freq
|
28 |
// uint16_t counter_init = (uint16_t)(TIMER_FREQ / freq);
|
29 |
|
30 |
int timer_port = 0; |
31 |
switch(timer) {
|
32 |
case 0: timer_port = TIMER_0; break; |
33 |
case 1: timer_port = TIMER_1; break; |
34 |
case 2: timer_port = TIMER_2; break; |
35 |
default: return 1; break; |
36 |
} |
37 |
|
38 |
|
39 |
|
40 |
return 1; |
41 |
} |
42 |
|
43 |
int (timer_subscribe_int)(uint8_t *bit_no) {
|
44 |
/* To be implemented by the students */
|
45 |
printf("%s is not yet implemented!\n", __func__);
|
46 |
|
47 |
return 1; |
48 |
} |
49 |
|
50 |
int (timer_unsubscribe_int)() {
|
51 |
/* To be implemented by the students */
|
52 |
printf("%s is not yet implemented!\n", __func__);
|
53 |
|
54 |
return 1; |
55 |
} |
56 |
|
57 |
void (timer_int_handler)() {
|
58 |
/* To be implemented by the students */
|
59 |
printf("%s is not yet implemented!\n", __func__);
|
60 |
} |
61 |
|
62 |
int (timer_get_conf)(uint8_t timer, uint8_t *st) {
|
63 |
// Write read-back command to TIMER_CTRL
|
64 |
u32_t cmd = TIMER_RB_CMD | TIMER_RB_COUNT_ | TIMER_RB_SEL(timer); |
65 |
if(sys_outb(TIMER_CTRL, cmd)) return 1; |
66 |
|
67 |
int read_port;
|
68 |
switch(timer) {
|
69 |
case 0: read_port = TIMER_0; break; |
70 |
case 1: read_port = TIMER_1; break; |
71 |
case 2: read_port = TIMER_2; break; |
72 |
default: return 1; break; |
73 |
} |
74 |
if(util_sys_inb(read_port, st)) return 1; |
75 |
return 0; |
76 |
} |
77 |
|
78 |
int (timer_display_conf)(uint8_t timer, uint8_t st, enum timer_status_field field) { |
79 |
union timer_status_field_val conf;
|
80 |
uint8_t in_mode; |
81 |
if(field == tsf_all || field == tsf_initial){
|
82 |
in_mode = (st & TIMER_INMODE_MASK) >> TIMER_INMODE_POS; |
83 |
switch(in_mode){
|
84 |
case 0: conf.in_mode = INVAL_val ; break; //000 |
85 |
case 1: conf.in_mode = LSB_only ; break; //001 |
86 |
case 2: conf.in_mode = MSB_only ; break; //010 |
87 |
case 3: conf.in_mode = MSB_after_LSB; break; //011 |
88 |
default: return 1; break; |
89 |
} |
90 |
if(timer_print_config(timer, field, conf)) return 1; |
91 |
} |
92 |
if(field == tsf_mode){
|
93 |
conf.count_mode = (st & TIMER_MODE_MASK)>>TIMER_MODE_POS; |
94 |
if(timer_print_config(timer, field, conf)) return 1; |
95 |
} |
96 |
if(field == tsf_base){
|
97 |
conf.bcd = st & TIMER_BCD; |
98 |
if(timer_print_config(timer, field, conf)) return 1; |
99 |
} |
100 |
return 0; |
101 |
} |