Project

General

Profile

Revision 11

FINAL RE

View differences:

lab3/timer.c
1
#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
  util_get_MSB(tudo,&msb);
20
  util_get_LSB(tudo,&lsb);
21
  
22
  //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
  sys_irqsetpolicy(IRQ_TIMER,IRQ_REENABLE,&hook_id);
62

  
63
  return 1;
64
}
65

  
66
int (timer_unsubscribe_int)() {
67
  /* To be implemented by the students */
68
// printf("%s is not yet implemented!\n", __func__);
69

  
70
sys_irqrmpolicy(&hook_id);
71

  
72
  return 1;
73
}
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
    if((st&BIT(0))==1)
154
    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
}
165 0

  
lab3/lab2.c
1
#include <lcom/lcf.h>
2
#include <lcom/lab2.h>
3

  
4
#include <stdbool.h>
5
#include <stdint.h>
6

  
7
#include "i8254.h"
8
//#include "timer.c"
9
extern uint32_t cont;
10

  
11

  
12
int main(int argc, char *argv[]) {
13
  
14
  // sets the language of LCF messages (can be either EN-US or PT-PT)
15
  lcf_set_language("EN-US");
16

  
17
  // enables to log function invocations that are being "wrapped" by LCF
18
  // [comment this out if you don't want/need it]
19
  lcf_trace_calls("/home/lcom/labs/lab2/trace.txt");
20

  
21
  // enables to save the output of printf function calls on a file
22
  // [comment this out if you don't want/need it]
23
  lcf_log_output("/home/lcom/labs/lab2/output.txt");
24

  
25
  // handles control over to LCF
26
  // [LCF handles command line arguments and invokes the right function]
27
  if (lcf_start(argc, argv))
28
    return 1;
29

  
30
  // LCF clean up tasks
31
  // [must be the last statement before return]
32
  lcf_cleanup();
33

  
34
  return 0;
35
}
36

  
37
int(timer_test_read_config)(uint8_t timer, enum timer_status_field field) {
38
  /* To be implemented by the students */
39
  uint8_t st;
40
  if(timer_get_conf(timer,&st)==1) return 1;
41
  if(timer_display_conf(timer,st,field)==1) return 1;
42

  
43
  return 0;
44
 // printf("%s is not yet implemented!\n", __func__);
45

  
46
}
47

  
48
int(timer_test_time_base)(uint8_t timer, uint32_t freq) {
49
  /* To be implemented by the students */
50
  if(freq<MIN_FREQ || freq>TIMER_FREQ)
51
  {
52
    printf("OUT OF RANGE FREQUENCY\n");
53
    return 1;
54
  }
55
  return(timer_set_frequency(timer,freq));
56
  //printf("%s is not yet implemented!\n", __func__);
57

  
58
}
59

  
60
int(timer_test_int)(uint8_t time) {
61
  /* To be implemented by the students */
62

  
63
  if(time<0) return 1;
64

  
65
  int ipc_status;
66
  message msg;
67
  uint8_t r;
68
  uint8_t bit_no = 0;
69
  timer_subscribe_int(&bit_no);
70
  while( (cont/DEFAULT_FREQ)<time )
71
 { /* You may want to use a different condition */
72
     /* Get a request message. */
73
     // printf("1\n");
74
     //printf("cont:%d, time:%d\n\n",cont/60,time);
75
     if ( (r = driver_receive(ANY, &msg, &ipc_status)) != 0 ) 
76
     { 
77
        printf("driver_receive failed with: %d", r);
78
       continue;
79
    }
80
    if (is_ipc_notify(ipc_status)) { /* received notification */
81
        switch (_ENDPOINT_P(msg.m_source)) {
82
            case HARDWARE: /* hardware interrupt notification */				
83
                if (msg.m_notify.interrupts  & BIT(0)) { /* subscribed interrupt */
84
                   timer_int_handler();
85
                   if(cont%DEFAULT_FREQ==0)
86
                   timer_print_elapsed_time();
87
                   //printf("cont:%d\n",cont);
88
               }
89
                //printf("msg:%x\n",msg.m_notify.interrupts);
90
                break;
91
            default:
92
                //printf("2\n");
93
                break; /* no other notifications expected: do nothing */	
94
        }
95
    } else {
96
     // printf("3\n"); /* received a standard message, not a notification */
97
        /* no standard messages expected: do nothing */
98
    }
99
 }
100
 if(timer_unsubscribe_int()!=0) return 1;
101

  
102

  
103
  //printf("%s is not yet implemented!\n", __func__);
104

  
105
  return 0;
106
}
107
//dois primeiros bits sao 1, (TIMER_RB_CNTRL|BIT(5))
108
//SYS_OUTB para controlo->0x43, Config timer 0, dizer que queremos escrever para o controlo, e depois ler a parte corresponder
109
//g9
110 0

  

Also available in: Unified diff