Project

General

Profile

Statistics
| Revision:

root / lab2 / lab2.c @ 5

History | View | Annotate | Download (3.19 KB)

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

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

    
7
extern unsigned int count;
8

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

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

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

    
21
  // handles control over to LCF
22
  // [LCF handles command line arguments and invokes the right function]
23
  if (lcf_start(argc, argv))
24
    return 1;
25

    
26
  // LCF clean up tasks
27
  // [must be the last statement before return]
28
  lcf_cleanup();
29

    
30
  return 0;
31
}
32

    
33

    
34
int(timer_test_read_config)(uint8_t timer, enum timer_status_field field) {
35

    
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;
51
}
52

    
53
int(timer_test_time_base)(uint8_t timer, uint32_t freq) {
54

    
55
  if(timer<0|timer>2){                                  //tests for error in timer selection
56
    printf("Error selecting timer\n");
57
    return 1;
58
  }
59
  if(timer_set_frequency(timer, freq)==1){              //tests for error
60
    printf("Error testing time base\n");
61
    return 1;
62
  }
63
  return 0;
64
}
65

    
66
int(timer_test_int)(uint8_t time) {
67
  int r, ipc_status;
68
  uint8_t irq_set;
69
  message msg;
70

    
71
  if(timer_set_frequency(0, 60)==1){                    //tests for error
72
    printf("Error setting frequency\n");
73
    return 1;
74
  }
75

    
76
  if(timer_subscribe_int(&irq_set)==1){
77
    printf("Error subscribing int\n");
78
    return 1;
79
  }
80

    
81
  while(count/sys_hz()<time){                                     //looping until counter reaches the same value as time
82
    if ((r = driver_receive(ANY, &msg, &ipc_status))==1){ 
83
      printf("driver_receive failed with: %d",r);
84
      continue;
85
    }
86
    if (is_ipc_notify(ipc_status)){                             //received notification
87
      switch (_ENDPOINT_P(msg.m_source)){
88
        case HARDWARE:                                          //hardware interrupt notification                                
89
                if (msg.m_notify.interrupts &irq_set){          // subscribed interrupt
90
                  
91
                  timer_int_handler();
92

    
93
                  if((count%sys_hz())==0){
94
                    timer_print_elapsed_time();                 //prints message each second;
95
                  }
96
                }
97
               break;
98
        default:
99
              break;                                            //no other notifications expected: do nothing        
100
        }
101
    } 
102
    else {                                                      //received a standard message, not a notification
103
      //no standard messages expected: do nothing
104
    }
105
  }
106
  
107
  if(timer_unsubscribe_int()==1){
108
    printf("Error unsubscribing int\n");
109
    return 1;
110
  }
111
  return 0;
112
}