root / lab2 / timer.c @ 21
History | View | Annotate | Download (3.44 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 | 18 | up20180655 | |
10 | // Frequencies out this range are not supported (by limitation of hardware)
|
||
11 | if (freq > TIMER_FREQ || freq < TIMER_MIN_FREQ) return 1; |
||
12 | |||
13 | 10 | up20180655 | uint8_t status = 0;
|
14 | if (timer_get_conf(timer, &status)) return 1; |
||
15 | //Make command
|
||
16 | uint8_t write_cmd = 0;
|
||
17 | //Select timer
|
||
18 | switch(timer) {
|
||
19 | 11 | up20180655 | case 0: write_cmd |= TIMER_SEL0; break; |
20 | case 1: write_cmd |= TIMER_SEL1; break; |
||
21 | case 2: write_cmd |= TIMER_SEL2; break; |
||
22 | default: return 1; break; |
||
23 | 10 | up20180655 | } |
24 | //Change both LSB and MSB
|
||
25 | write_cmd |= TIMER_LSB_MSB; |
||
26 | //Keep 4 least significant bits
|
||
27 | write_cmd |= (status & (TIMER_MODE_MASK | TIMER_BCD)); |
||
28 | //Write cmd
|
||
29 | 11 | up20180655 | if (sys_outb(TIMER_CTRL, write_cmd)) return 1; |
30 | 10 | up20180655 | |
31 | 11 | up20180655 | // counter_init = clock/freq
|
32 | 15 | up20180655 | uint16_t counter_init = (uint16_t)(TIMER_FREQ / freq); |
33 | 10 | up20180655 | |
34 | 11 | up20180655 | int timer_port = 0; |
35 | switch(timer) {
|
||
36 | 12 | up20180655 | case 0: timer_port = TIMER_0; break; |
37 | case 1: timer_port = TIMER_1; break; |
||
38 | case 2: timer_port = TIMER_2; break; |
||
39 | 15 | up20180655 | default: return 1; break; |
40 | 11 | up20180655 | } |
41 | 15 | up20180655 | uint8_t lsb = 0, msb = 0; |
42 | if (util_get_LSB(counter_init, &lsb)) return 1; |
||
43 | if (util_get_MSB(counter_init, &msb)) return 1; |
||
44 | 11 | up20180655 | |
45 | 15 | up20180655 | if (sys_outb(timer_port, lsb)) return 1; |
46 | if (sys_outb(timer_port, msb)) return 1; |
||
47 | 11 | up20180655 | |
48 | 15 | up20180655 | return 0; |
49 | 4 | up20180655 | } |
50 | |||
51 | int (timer_subscribe_int)(uint8_t *bit_no) {
|
||
52 | 19 | up20180642 | int hook_id = 2; |
53 | if(sys_irqsetpolicy(TIMER0_IRQ, IRQ_REENABLE, &hook_id)) return 1; |
||
54 | *bit_no = hook_id; |
||
55 | return 0; |
||
56 | 4 | up20180655 | } |
57 | |||
58 | 19 | up20180642 | int (timer_unsubscribe_int)(uint8_t *bit_no) {
|
59 | int hook_id = *bit_no;
|
||
60 | if(sys_irqrmpolicy(&hook_id)) return 1; |
||
61 | *bit_no = hook_id; |
||
62 | return 0; |
||
63 | 4 | up20180655 | } |
64 | |||
65 | void (timer_int_handler)() {
|
||
66 | 20 | up20180655 | int no_interrupts = 0; //should be the global |
67 | |||
68 | no_interrupts++; |
||
69 | 4 | up20180655 | } |
70 | |||
71 | int (timer_get_conf)(uint8_t timer, uint8_t *st) {
|
||
72 | 9 | up20180642 | // Write read-back command to TIMER_CTRL
|
73 | 5 | up20180642 | u32_t cmd = TIMER_RB_CMD | TIMER_RB_COUNT_ | TIMER_RB_SEL(timer); |
74 | 10 | up20180655 | if(sys_outb(TIMER_CTRL, cmd)) return 1; |
75 | 4 | up20180655 | |
76 | 5 | up20180642 | int read_port;
|
77 | 7 | up20180655 | switch(timer) {
|
78 | 5 | up20180642 | case 0: read_port = TIMER_0; break; |
79 | case 1: read_port = TIMER_1; break; |
||
80 | case 2: read_port = TIMER_2; break; |
||
81 | 6 | up20180642 | default: return 1; break; |
82 | 5 | up20180642 | } |
83 | if(util_sys_inb(read_port, st)) return 1; |
||
84 | return 0; |
||
85 | 4 | up20180655 | } |
86 | |||
87 | 6 | up20180642 | int (timer_display_conf)(uint8_t timer, uint8_t st, enum timer_status_field field) { |
88 | union timer_status_field_val conf;
|
||
89 | uint8_t in_mode; |
||
90 | 16 | up20180642 | switch(field){
|
91 | case tsf_all:
|
||
92 | conf.byte = st; |
||
93 | break;
|
||
94 | case tsf_initial:
|
||
95 | in_mode = (st & TIMER_INMODE_MASK) >> TIMER_INMODE_POS; |
||
96 | switch(in_mode){
|
||
97 | case 0: conf.in_mode = INVAL_val ; break; //000 |
||
98 | case 1: conf.in_mode = LSB_only ; break; //001 |
||
99 | case 2: conf.in_mode = MSB_only ; break; //010 |
||
100 | case 3: conf.in_mode = MSB_after_LSB; break; //011 |
||
101 | default: return 1; break; |
||
102 | } |
||
103 | break;
|
||
104 | case tsf_mode:
|
||
105 | conf.count_mode = (st & TIMER_MODE_MASK)>>TIMER_MODE_POS; |
||
106 | 17 | up20180642 | if(conf.count_mode == TIMER_MODE_2ALT || conf.count_mode == TIMER_MODE_3ALT)
|
107 | 16 | up20180642 | conf.count_mode &= TIMER_MODE_RED2; |
108 | break;
|
||
109 | case tsf_base:
|
||
110 | conf.bcd = st & TIMER_BCD; |
||
111 | break;
|
||
112 | default: return 1; break; |
||
113 | 5 | up20180642 | } |
114 | 16 | up20180642 | if(timer_print_config(timer, field, conf)) return 1; |
115 | 6 | up20180642 | return 0; |
116 | } |