root / lab2 / timer.c @ 16
History | View | Annotate | Download (3.33 KB)
1 | 4 | up20180655 | #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 | 10 | up20180655 | 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 | 11 | up20180655 | 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 | 10 | up20180655 | } |
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 | 11 | up20180655 | if (sys_outb(TIMER_CTRL, write_cmd)) return 1; |
26 | 10 | up20180655 | |
27 | 11 | up20180655 | // counter_init = clock/freq
|
28 | 15 | up20180655 | uint16_t counter_init = (uint16_t)(TIMER_FREQ / freq); |
29 | 10 | up20180655 | |
30 | 11 | up20180655 | int timer_port = 0; |
31 | switch(timer) {
|
||
32 | 12 | up20180655 | case 0: timer_port = TIMER_0; break; |
33 | case 1: timer_port = TIMER_1; break; |
||
34 | case 2: timer_port = TIMER_2; break; |
||
35 | 15 | up20180655 | default: return 1; break; |
36 | 11 | up20180655 | } |
37 | 15 | up20180655 | uint8_t lsb = 0, msb = 0; |
38 | if (util_get_LSB(counter_init, &lsb)) return 1; |
||
39 | if (util_get_MSB(counter_init, &msb)) return 1; |
||
40 | 11 | up20180655 | |
41 | 15 | up20180655 | if (sys_outb(timer_port, lsb)) return 1; |
42 | if (sys_outb(timer_port, msb)) return 1; |
||
43 | 11 | up20180655 | |
44 | 15 | up20180655 | //if (sys_outw(timer_port, counter_init)) return 1;
|
45 | 12 | up20180655 | |
46 | 15 | up20180655 | return 0; |
47 | 4 | up20180655 | } |
48 | |||
49 | int (timer_subscribe_int)(uint8_t *bit_no) {
|
||
50 | /* To be implemented by the students */
|
||
51 | 5 | up20180642 | printf("%s is not yet implemented!\n", __func__);
|
52 | 4 | up20180655 | |
53 | 5 | up20180642 | return 1; |
54 | 4 | up20180655 | } |
55 | |||
56 | int (timer_unsubscribe_int)() {
|
||
57 | 5 | up20180642 | /* To be implemented by the students */
|
58 | printf("%s is not yet implemented!\n", __func__);
|
||
59 | 4 | up20180655 | |
60 | 5 | up20180642 | return 1; |
61 | 4 | up20180655 | } |
62 | |||
63 | void (timer_int_handler)() {
|
||
64 | 5 | up20180642 | /* To be implemented by the students */
|
65 | printf("%s is not yet implemented!\n", __func__);
|
||
66 | 4 | up20180655 | } |
67 | |||
68 | int (timer_get_conf)(uint8_t timer, uint8_t *st) {
|
||
69 | 9 | up20180642 | // Write read-back command to TIMER_CTRL
|
70 | 5 | up20180642 | u32_t cmd = TIMER_RB_CMD | TIMER_RB_COUNT_ | TIMER_RB_SEL(timer); |
71 | 10 | up20180655 | if(sys_outb(TIMER_CTRL, cmd)) return 1; |
72 | 4 | up20180655 | |
73 | 5 | up20180642 | int read_port;
|
74 | 7 | up20180655 | switch(timer) {
|
75 | 5 | up20180642 | case 0: read_port = TIMER_0; break; |
76 | case 1: read_port = TIMER_1; break; |
||
77 | case 2: read_port = TIMER_2; break; |
||
78 | 6 | up20180642 | default: return 1; break; |
79 | 5 | up20180642 | } |
80 | if(util_sys_inb(read_port, st)) return 1; |
||
81 | return 0; |
||
82 | 4 | up20180655 | } |
83 | |||
84 | 6 | up20180642 | int (timer_display_conf)(uint8_t timer, uint8_t st, enum timer_status_field field) { |
85 | union timer_status_field_val conf;
|
||
86 | uint8_t in_mode; |
||
87 | 16 | up20180642 | switch(field){
|
88 | case tsf_all:
|
||
89 | conf.byte = st; |
||
90 | break;
|
||
91 | case tsf_initial:
|
||
92 | in_mode = (st & TIMER_INMODE_MASK) >> TIMER_INMODE_POS; |
||
93 | switch(in_mode){
|
||
94 | case 0: conf.in_mode = INVAL_val ; break; //000 |
||
95 | case 1: conf.in_mode = LSB_only ; break; //001 |
||
96 | case 2: conf.in_mode = MSB_only ; break; //010 |
||
97 | case 3: conf.in_mode = MSB_after_LSB; break; //011 |
||
98 | default: return 1; break; |
||
99 | } |
||
100 | break;
|
||
101 | case tsf_mode:
|
||
102 | conf.count_mode = (st & TIMER_MODE_MASK)>>TIMER_MODE_POS; |
||
103 | if(conf.count_mode == 0x6 || conf.count_mode == 0x7) |
||
104 | conf.count_mode &= TIMER_MODE_RED2; |
||
105 | break;
|
||
106 | case tsf_base:
|
||
107 | conf.bcd = st & TIMER_BCD; |
||
108 | break;
|
||
109 | default: return 1; break; |
||
110 | 5 | up20180642 | } |
111 | 16 | up20180642 | if(timer_print_config(timer, field, conf)) return 1; |
112 | 6 | up20180642 | return 0; |
113 | } |