Project

General

Profile

Statistics
| Revision:

root / lab2 / timer.c @ 7

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