Project

General

Profile

Statistics
| Revision:

root / lab2 / lab2.c @ 4

History | View | Annotate | Download (2.97 KB)

1 3 up20180645
#include <lcom/lcf.h>
2
#include <lcom/lab2.h>
3
4
#include <stdbool.h>
5
#include <stdint.h>
6
7 4 up20180645
extern unsigned int count;
8 3 up20180645
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 4 up20180645
34 3 up20180645
int(timer_test_read_config)(uint8_t timer, enum timer_status_field field) {
35
36 4 up20180645
  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 3 up20180645
}
52
53
int(timer_test_time_base)(uint8_t timer, uint32_t freq) {
54
55 4 up20180645
  if(timer_set_frequency(timer, freq)==1){                      //tests for error
56
    printf("Error testing time base\n");
57
    return 1;
58
  }
59
  return 0;
60 3 up20180645
}
61
62
int(timer_test_int)(uint8_t time) {
63 4 up20180645
  int r, ipc_status;
64
  uint8_t irq_set;
65
  message msg;
66 3 up20180645
67 4 up20180645
  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;
103 3 up20180645
}