root / lab2 / src / lab2.c @ 196
History | View | Annotate | Download (2.5 KB)
1 | 196 | up20180642 | #include <lcom/lcf.h> |
---|---|---|---|
2 | #include <lcom/lab2.h> |
||
3 | |||
4 | #include <stdbool.h> |
||
5 | #include <stdint.h> |
||
6 | |||
7 | #include "timer.h" |
||
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 | int(timer_test_read_config)(uint8_t timer, enum timer_status_field field){ |
||
34 | uint8_t state = 0;
|
||
35 | if(timer_get_conf(timer, &state)) return 1; |
||
36 | if(timer_display_conf(timer, state, field)) return 1; |
||
37 | return 0; |
||
38 | } |
||
39 | |||
40 | int(timer_test_time_base)(uint8_t timer, uint32_t freq) {
|
||
41 | if (timer_set_frequency(timer, freq)) return 1; |
||
42 | return 0; |
||
43 | } |
||
44 | |||
45 | int(timer_test_int)(uint8_t time) {
|
||
46 | const int frequency = 60; // Frequency asummed at 60Hz |
||
47 | int ipc_status, r;
|
||
48 | message msg; |
||
49 | uint8_t timer_id = 0;
|
||
50 | no_interrupts = 0;
|
||
51 | if (timer_subscribe_int(&timer_id)) return 1; |
||
52 | int irq_set = BIT(timer_id);
|
||
53 | while (time) {
|
||
54 | /* Get a request message. */
|
||
55 | if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) { |
||
56 | printf("driver_receive failed with %d", r);
|
||
57 | continue;
|
||
58 | } |
||
59 | if (is_ipc_notify(ipc_status)) { /* received notification */ |
||
60 | switch (_ENDPOINT_P(msg.m_source)) {
|
||
61 | case HARDWARE: /* hardware interrupt notification */ |
||
62 | if (msg.m_notify.interrupts & irq_set) { /* subscribed interrupt */ |
||
63 | timer_int_handler(); |
||
64 | if (!(no_interrupts % frequency)) { /* second elapsed */ |
||
65 | timer_print_elapsed_time(); |
||
66 | time--; |
||
67 | } |
||
68 | } |
||
69 | break;
|
||
70 | default:
|
||
71 | break; /* no other notifications expected: do nothing */ |
||
72 | } |
||
73 | } else { /* received standart message, not a notification */ |
||
74 | /* no standart message expected: do nothing */
|
||
75 | } |
||
76 | } |
||
77 | if (timer_unsubscribe_int()) return 1; |
||
78 | return 0; |
||
79 | } |