root / lab2 / timer.c @ 1
History | View | Annotate | Download (3.16 KB)
1 | 1 | up20180614 | #include <lcom/lcf.h> |
---|---|---|---|
2 | #include <lcom/timer.h> |
||
3 | |||
4 | #include <stdint.h> |
||
5 | |||
6 | #include "i8254.h" |
||
7 | |||
8 | uint32_t cont=0;
|
||
9 | int hook_id;
|
||
10 | |||
11 | |||
12 | int (timer_set_frequency)(uint8_t timer, uint32_t freq) {
|
||
13 | /* To be implemented by the students */
|
||
14 | uint8_t conf; |
||
15 | timer_get_conf(timer,&conf); |
||
16 | uint32_t tudo; |
||
17 | uint8_t msb,lsb; |
||
18 | tudo=(TIMER_FREQ/freq); |
||
19 | msb=(tudo&0xFF00)>>8; |
||
20 | lsb=tudo&0xFF;
|
||
21 | //printf("%x\n%x\n%x\n%d\n",tudo,msb,lsb,timer);
|
||
22 | |||
23 | |||
24 | if(timer==0) |
||
25 | { |
||
26 | conf=(conf&0xF);
|
||
27 | conf = (conf | TIMER_SEL0 | TIMER_LSB_MSB); |
||
28 | if(sys_outb(TIMER_CTRL, conf)!=0) return 1; |
||
29 | if(sys_outb(TIMER_0, lsb)!=0) return 1; |
||
30 | if(sys_outb(TIMER_0, msb)!=0) return 1; |
||
31 | } |
||
32 | |||
33 | if(timer==1) |
||
34 | { |
||
35 | conf=(conf&0xF);
|
||
36 | conf = (conf | TIMER_SEL1 | TIMER_LSB_MSB); |
||
37 | if(sys_outb(TIMER_CTRL, conf)!=0) return 1; |
||
38 | if(sys_outb(TIMER_1, lsb)!=0) return 1; |
||
39 | if(sys_outb(TIMER_1, msb)!=0) return 1; |
||
40 | } |
||
41 | |||
42 | if(timer==2) |
||
43 | { |
||
44 | conf=(conf&0xF);
|
||
45 | conf = (conf | TIMER_SEL2 | TIMER_LSB_MSB); |
||
46 | if(sys_outb(TIMER_CTRL, conf)!=0) return 1; |
||
47 | if(sys_outb(TIMER_2, lsb)!=0) return 1; |
||
48 | if(sys_outb(TIMER_2, msb)!=0) return 1; |
||
49 | } |
||
50 | return 0; |
||
51 | |||
52 | } |
||
53 | |||
54 | int (timer_subscribe_int)(uint8_t *bit_no) {
|
||
55 | /* To be implemented by the students */
|
||
56 | //printf("%s is not yet implemented!\n", __func__);
|
||
57 | /* int policy;
|
||
58 | sys_irqrmpolicy(&policy); */
|
||
59 | hook_id=*bit_no; |
||
60 | sys_irqsetpolicy(IRQ_TIMER,IRQ_REENABLE,&hook_id); |
||
61 | |||
62 | return 1; |
||
63 | } |
||
64 | |||
65 | int (timer_unsubscribe_int)() {
|
||
66 | /* To be implemented by the students */
|
||
67 | // printf("%s is not yet implemented!\n", __func__);
|
||
68 | |||
69 | sys_irqrmpolicy(&hook_id); |
||
70 | |||
71 | return 1; |
||
72 | } |
||
73 | |||
74 | void (timer_int_handler)() {
|
||
75 | /* To be implemented by the students */
|
||
76 | //printf("%s is not yet implemented!\n", __func__);
|
||
77 | cont=cont+1;
|
||
78 | } |
||
79 | |||
80 | int (timer_get_conf)(uint8_t timer, uint8_t *st) {
|
||
81 | /* To be implemented by the students */
|
||
82 | |||
83 | uint8_t rbc = (TIMER_RB_CMD|TIMER_RB_SEL(4)|TIMER_RB_SEL(timer));
|
||
84 | if(sys_outb(TIMER_CTRL,rbc)!=0) return 1; |
||
85 | |||
86 | |||
87 | if(timer==0) |
||
88 | return (util_sys_inb(TIMER_0,st));
|
||
89 | if(timer==1) |
||
90 | return (util_sys_inb(TIMER_1,st));
|
||
91 | if(timer==2) |
||
92 | return (util_sys_inb(TIMER_2,st));
|
||
93 | |||
94 | return 1; |
||
95 | |||
96 | //printf("%s is not yet implemented!\n", __func__);
|
||
97 | |||
98 | |||
99 | } |
||
100 | |||
101 | int (timer_display_conf)(uint8_t timer, uint8_t st,
|
||
102 | enum timer_status_field field) {
|
||
103 | /* To be implemented by the students */
|
||
104 | union timer_status_field_val val;
|
||
105 | |||
106 | |||
107 | if(field==tsf_all)
|
||
108 | { |
||
109 | val.byte=st; |
||
110 | |||
111 | } |
||
112 | if(field==tsf_initial)
|
||
113 | { |
||
114 | //printf("%d\n",tsf_initial);
|
||
115 | |||
116 | if(((st&0x30)>>4)==0) |
||
117 | val.in_mode=INVAL_val; |
||
118 | |||
119 | if(((st&0x30)>>4)==1) |
||
120 | val.in_mode=LSB_only; |
||
121 | |||
122 | if(((st&0x30)>>4)==2) |
||
123 | val.in_mode=MSB_only; |
||
124 | |||
125 | if(((st&0x30)>>4)==3) |
||
126 | val.in_mode=MSB_after_LSB; |
||
127 | } |
||
128 | |||
129 | |||
130 | if(field==tsf_mode)
|
||
131 | { |
||
132 | //printf("%x\n",st);
|
||
133 | val.count_mode=((st&0xE)>>1); |
||
134 | |||
135 | /* printf("%x\n",val.count_mode);
|
||
136 | val.count_mode=(val.count_mode>>1);
|
||
137 | printf("%x\n",val.count_mode); */
|
||
138 | //val.count_mode=(st&0x1C)/4;
|
||
139 | |||
140 | if (val.count_mode==6) |
||
141 | val.count_mode=2;
|
||
142 | |||
143 | if (val.count_mode==7) |
||
144 | val.count_mode=3;
|
||
145 | |||
146 | |||
147 | } |
||
148 | |||
149 | if(field==tsf_base ) //BCD |
||
150 | { |
||
151 | //printf("%d\n",tsf_base);
|
||
152 | if((st&0x1)==1) |
||
153 | val.bcd=true;
|
||
154 | |||
155 | else val.bcd=false; |
||
156 | } |
||
157 | |||
158 | |||
159 | return( timer_print_config(timer, field, val));
|
||
160 | //printf("%s is not yet implemented!\n", __func__);
|
||
161 | |||
162 | //return 0;
|
||
163 | } |