Project

General

Profile

Revision 4

All functions running, review code

View differences:

lab2/lab2.c
4 4
#include <stdbool.h>
5 5
#include <stdint.h>
6 6

  
7
extern unsigned int count;
7 8

  
8 9
int main(int argc, char *argv[]) {
9 10
  // sets the language of LCF messages (can be either EN-US or PT-PT)
......
29 30
  return 0;
30 31
}
31 32

  
33

  
32 34
int(timer_test_read_config)(uint8_t timer, enum timer_status_field field) {
33
  /* To be implemented by the students */
34
  printf("%s is not yet implemented!\n", __func__);
35 35

  
36
  return 1;
36
  uint8_t st;
37

  
38
  if(timer<0|timer>2){                      //tests for error in timer selection
39
    printf("Error selecting timer\n");
40
    return 1;
41
  }
42
  else if(timer_get_conf(timer, &st)==1){
43
    printf("Error getting configuration\n");
44
    return 1;
45
  }
46
  else if(timer_display_conf(timer, st, field)==1){
47
    printf("Error getting display configuration\n");
48
    return 1;
49
  }
50
  return 0;
37 51
}
38 52

  
39 53
int(timer_test_time_base)(uint8_t timer, uint32_t freq) {
40
  /* To be implemented by the students */
41
  printf("%s is not yet implemented!\n", __func__);
42 54

  
43
  return 1;
55
  if(timer_set_frequency(timer, freq)==1){                      //tests for error
56
    printf("Error testing time base\n");
57
    return 1;
58
  }
59
  return 0;
44 60
}
45 61

  
46 62
int(timer_test_int)(uint8_t time) {
47
  /* To be implemented by the students */
48
  printf("%s is not yet implemented!\n", __func__);
63
  int r, ipc_status;
64
  uint8_t irq_set;
65
  message msg;
49 66

  
50
  return 1;
67
  if(timer_subscribe_int(&irq_set)==1){
68
    printf("Error subscribing int\n");
69
    return 1;
70
  }
71

  
72
  while(count/60.0<time){                                     //looping until counter reaches the same value as time
73
    if ((r = driver_receive(ANY, &msg, &ipc_status))!=0){ 
74
      printf("driver_receive failed with: %d",r);
75
      continue;
76
    }
77
    if (is_ipc_notify(ipc_status)){                             //received notification
78
      switch (_ENDPOINT_P(msg.m_source)){
79
        case HARDWARE:                                          //hardware interrupt notification				
80
                if (msg.m_notify.interrupts &irq_set){          // subscribed interrupt
81
                  
82
                  timer_int_handler();
83

  
84
                  if((count%60)==0){
85
                    timer_print_elapsed_time();                 //prints message each second;
86
                  }
87
                }
88
               break;
89
        default:
90
              break;                                            //no other notifications expected: do nothing	
91
        }
92
    } 
93
    else {                                                      //received a standard message, not a notification
94
                                                                //no standard messages expected: do nothing
95
    }
96
  }
97
  
98
  if(timer_unsubscribe_int()==1){
99
    printf("Error unsubscribing int\n");
100
    return 1;
101
  }
102
  return 0;
51 103
}
lab2/timer.c
5 5

  
6 6
#include "i8254.h"
7 7

  
8
unsigned int count=0; //Initializing counter
9
int hook_id = TIMER0_IRQ;
10

  
8 11
int (timer_set_frequency)(uint8_t timer, uint32_t freq) {
9
  /* To be implemented by the students */
10
  printf("%s is not yet implemented!\n", __func__);
12
  // freq=TIMER_FREQ/div, where TIMER_FREQ is the frequency of the clock and div os the value loaded nittially in the timer
13
    uint16_t div = (TIMER_FREQ /freq);
11 14

  
12
  return 1;
15
    uint8_t msb, lsb;
16

  
17
    util_get_LSB(div,&lsb);         //getting LSB
18
    util_get_MSB(div,&msb);         //getting MSB
19

  
20
    uint8_t st;
21

  
22
    if(timer_get_conf(timer,&st)!=0){
23
        printf("Error getting conf in frequency set\n");
24
        return 1;
25
    }
26
    if(timer<0|timer>2){
27
        printf("Error selecting timer in frequency set\n");
28
        return 1;
29
    }
30
    else
31
    {
32
        if(timer==0){
33
            st= ((st & 0x0F)|TIMER_LSB_MSB|TIMER_SEL0);               //The mask 0x0f is used so that the 4 LSBs are not changed
34
            sys_outb(TIMER_CTRL,st);                                //Sends control word to program the timer
35
            sys_outb(TIMER_0,lsb);
36
            sys_outb(TIMER_0,msb);                                  //Both these functions program the timer
37
        }
38
        else if(timer==1){
39
            st= ((st & 0x0F)|TIMER_LSB_MSB|TIMER_SEL1); 
40
            sys_outb(TIMER_CTRL,st);                   
41
            sys_outb(TIMER_1,lsb);
42
            sys_outb(TIMER_1,msb);
43
        }
44
        else if(timer==2){
45
            st= ((st & 0x0F)|TIMER_LSB_MSB|TIMER_SEL2); 
46
            sys_outb(TIMER_CTRL,st);                   
47
            sys_outb(TIMER_2,lsb);
48
            sys_outb(TIMER_2,msb);
49
        }
50
    }
51
  return 0;
13 52
}
14 53

  
15 54
int (timer_subscribe_int)(uint8_t *bit_no) {
16
    /* To be implemented by the students */
17
  printf("%s is not yet implemented!\n", __func__);
18

  
19
  return 1;
55
    *bit_no = BIT(hook_id);
56
    if(sys_irqsetpolicy(TIMER0_IRQ,IRQ_REENABLE,&hook_id)==1){      //operation to subscribe int
57
        printf("Error subscribing int\n");
58
        return 1;
59
    }
60
  return 0;
20 61
}
21 62

  
22 63
int (timer_unsubscribe_int)() {
23
  /* To be implemented by the students */
24
  printf("%s is not yet implemented!\n", __func__);
25

  
26
  return 1;
64
  if(sys_irqrmpolicy(&hook_id)==1){
65
      printf("Error unsubscribing int\n");
66
      return 1;
67
  }
68
  return 0;
27 69
}
28 70

  
29 71
void (timer_int_handler)() {
30
  /* To be implemented by the students */
31
  printf("%s is not yet implemented!\n", __func__);
72
  count++;
32 73
}
33 74

  
34 75
int (timer_get_conf)(uint8_t timer, uint8_t *st) {
35
  /* To be implemented by the students */
36
  printf("%s is not yet implemented!\n", __func__);
37 76

  
38
  return 1;
77
    uint8_t read_back = TIMER_RB_CMD | TIMER_RB_COUNT_ | TIMER_RB_SEL(timer); //forming the read-back command (TIMER_RB_CMD (bit 7 and 6)
78
                                                                                //TIMER_RB_COUNT (BIT 5) and TIMER_RB_SEL (bit timer+1)
79

  
80
    if (sys_outb(TIMER_CTRL, read_back)!=1) {                            //testing for an error
81
        switch (timer) {
82
            case 0:
83
                if (util_sys_inb(TIMER_0, st)!=0) {               //tests if TIMER_0 was chosen
84
                    printf("Error selecting Timer 0\n");
85
                    return 1;
86
                }
87
                break;
88
            case 1:
89
                if (util_sys_inb(TIMER_1, st)!=0) {               //tests if TIMER_1 was chosen
90
                    printf("Error selecting Timer 1\n");
91
                    return 1;
92
                }
93
                break;
94
            case 2:
95
                if (util_sys_inb(TIMER_2, st)!=0) {               //tests if TIMER_2 was chosen
96
                    printf("Error selecting Timer 2\n");
97
                    return 1;
98
                }
99
                break;
100
            default:
101
                printf("No clock selected\n");                                  //happens if timer<0 or timer>2
102
                return 1;
103
        }
104
        return 0;
105
    }
106
    printf("Error in get config\n");
107
    return 1;
39 108
}
40 109

  
41
int (timer_display_conf)(uint8_t timer, uint8_t st,
42
                        enum timer_status_field field) {
43
  /* To be implemented by the students */
44
  printf("%s is not yet implemented!\n", __func__);
110
int(timer_display_conf)(uint8_t timer, uint8_t st, enum timer_status_field field){
45 111

  
46
  return 1;
112
    if(timer < 0 | timer > 2)                           //tests if the timer has a correct value
113
        return  1;
114

  
115
    union timer_status_field_val config;                //create union (needed for the function at the end)
116
    uint8_t aux;
117
    aux=st;
118
    switch(field){
119
        case tsf_all:
120
            config.byte=aux;
121
            break;
122
        case tsf_initial:                               //Checking init_mode (mask -> 0x30)
123
            if((aux & 0x30)==TIMER_LSB){                //if 0x10
124
                config.in_mode=LSB_only;
125
            }
126
            else if ((aux & 0x30)==TIMER_MSB){          //if 0x20
127
                config.in_mode=MSB_only;
128
            }
129
            else if((aux & 0x30)==TIMER_LSB_MSB){       //if 0x30
130
                config.in_mode=MSB_after_LSB;
131
            }
132
            else{
133
                config.in_mode=INVAL_val;
134
                printf("Invalid Initialization Mode\n");
135
                return 1;
136
            }
137
            break;
138
        case tsf_mode:                                  //Checking count_mode (mask -> 0x0E)
139
            if((aux & 0x0E)==0){                        //if 0x00
140
                config.count_mode=0;
141
            }
142
            else if((aux & 0x0E)==2){
143
                config.count_mode=1;
144
            }
145
            else if((aux & 0x0E)==4|(aux & 0x0E)==12){
146
                config.count_mode=2;
147
            }
148
            else if((aux & 0x0E)==6|(aux & 0x0E)==14){
149
                config.count_mode=3;
150
            }
151
            else if((aux & 0x0E)==8){
152
                config.count_mode=4;
153
            }
154
            else if((aux & 0x0E)==10){
155
                config.count_mode=5;
156
            }
157
            else{
158
                printf("Error in checking counting mode\n");
159
                return 1;
160
            }
161
            break;
162
        case tsf_base:                                  //Checking bcd (mask -> 0x01)
163
            if((aux & 0x01)== TIMER_BCD){
164
                config.bcd = true;                    //if the last bit is 1, it's bcd
165
            }
166
            else if((aux & 0x01)==TIMER_BIN){
167
                config.bcd = false;                     //if the last bit is 0, it's binary
168
            }
169
            else{
170
                printf("Error in checking base\n");
171
                return 1;
172
            }
173
            break;
174
        default:
175
            return 1;
176

  
177
    }
178

  
179
    if(timer_print_config(timer, field, config)!=0){
180
        printf("Error printing\n");
181
        return 1;           //function that prints the result
182
    }
183
        
184

  
185
    return 0;
186

  
47 187
}
lab2/utils.c
3 3
#include <stdint.h>
4 4

  
5 5
int(util_get_LSB)(uint16_t val, uint8_t *lsb) {
6
  /* To be implemented by the students */
7
  printf("%s is not yet implemented!\n", __func__);
6
  *lsb = (uint8_t) val;             //truncation of 16 bit to 8 bit (maintains the LSB)
8 7

  
9
  return 1;
8
  return 0;
10 9
}
11 10

  
12 11
int(util_get_MSB)(uint16_t val, uint8_t *msb) {
13
  /* To be implemented by the students */
14
  printf("%s is not yet implemented!\n", __func__);
12
  val= val>>8;                      //8 bit shift, gets rid of LSB
13
  *msb=(uint8_t)val;                //truncation of 16 bit to 8 bit (this time we have the MSB)
15 14

  
16
  return 1;
15
  return 0;
17 16
}
18 17

  
19
int (util_sys_inb)(int port, uint8_t *value) {
20
  /* To be implemented by the students */
21
  printf("%s is not yet implemented!\n", __func__);
18
int (util_sys_inb)(int port, uint8_t *value) { //transform 8 bit into 32 bit
22 19

  
23
  return 1;
20
  uint32_t new_val;             //initializing 32 bit variable
21

  
22
  if(sys_inb(port,&new_val)!=0){   //verifies if there is an error
23
      printf("Error in util_sys_inb\n");
24
      return 1;
25
  }
26
  *value=new_val & 0xFF;        // dereferencing "value"
27

  
28
  return 0;
24 29
}

Also available in: Unified diff