Project

General

Profile

Statistics
| Revision:

root / lab3 / lab3.c @ 6

History | View | Annotate | Download (4.06 KB)

1 4 up20180614
#include <lcom/lcf.h>
2
#include <minix/sysutil.h>
3
4
#include <lcom/lab3.h>
5
6
#include <stdbool.h>
7
#include <stdint.h>
8
//#include <timer.c>
9
10
#define IRQ_KB 1
11
#define DELAY_US 20000
12
#define EXIT_CODE 0x81
13
#define I_O_BUFFER 0x60
14
15
static uint32_t scancode;
16
17
int main(int argc, char *argv[]) {
18
  // sets the language of LCF messages (can be either EN-US or PT-PT)
19
  lcf_set_language("EN-US");
20
21
  // enables to log function invocations that are being "wrapped" by LCF
22
  // [comment this out if you don't want/need it]
23
  lcf_trace_calls("/home/lcom/labs/lab3/trace.txt");
24
25
  // enables to save the output of printf function calls on a file
26
  // [comment this out if you don't want/need it]
27
  lcf_log_output("/home/lcom/labs/lab3/output.txt");
28
29
  // handles control over to LCF
30
  // [LCF handles command line arguments and invokes the right function]
31
  if (lcf_start(argc, argv))
32 6 up20180614
    return 1;
33 4 up20180614
34
  // LCF clean up tasks
35
  // [must be the last statement before return]
36
  lcf_cleanup();
37
38
  return 0;
39
}
40
41 5 up20180614
void(kbc_ih)() {}
42 4 up20180614
43
int(kbd_test_scan)() {
44
  /* To be completed by the students */
45
  int ipc_status;
46 5 up20180614
  uint8_t r, bit_no = 0;
47 4 up20180614
  message msg;
48 5 up20180614
  int hook_id = bit_no;
49
  uint8_t scan_arr[2] = {0, 0};
50
  scan_arr[0] = scancode & 0xFF;
51
  scan_arr[1] = scancode & 0xFF00;
52 6 up20180614
  int flag = 0;
53 4 up20180614
54 5 up20180614
  printf("%d\n",
55
         sys_irqsetpolicy(IRQ_KB, IRQ_REENABLE | IRQ_EXCLUSIVE, &hook_id));
56
  /* sys_outb(0x64, 0x20);
57
  sys_inb(0x64, &scancode);
58
  printf("%x\n", scancode);
59
  if (scancode & 0x10) {
60
    scancode = (scancode & 0xEF);
61
  }
62

63
  printf("%x\n", scancode);
64
  sys_outb(0x64, 0x60);
65
  sys_outb(0x64, scancode); */
66
67
  printf("%x\n", scancode);
68
  kbc_ih();
69
  printf("\n");
70 6 up20180614
  while (scancode !=
71 5 up20180614
         EXIT_CODE) { /* 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
      printf("driver_receive failed with: %d", r);
77
      continue;
78 4 up20180614
    }
79
    if (is_ipc_notify(ipc_status)) { /* received notification */
80 5 up20180614
      switch (_ENDPOINT_P(msg.m_source)) {
81
      case HARDWARE: /* hardware interrupt notification */
82
        if (msg.m_notify.interrupts & BIT(0)) { /* subscribed interrupt */
83
84
          sys_inb(0x64, &scancode);
85
          if (scancode & 0x01) {
86
            sys_inb(0x60, &scancode);
87 6 up20180614
          if (scancode != 0xe0 && flag == 0) {
88
            // tickdelay(micros_to_ticks(DELAY_US));
89 5 up20180614
            scan_arr[0] = scancode;
90
            kbd_print_scancode(!(scancode & 0x80), 1, scan_arr);
91
          }
92
93 6 up20180614
          else if (scancode == 0xe0) {
94
            scan_arr[0] = scancode;
95
            flag = 1;
96 5 up20180614
          }
97
98 6 up20180614
          else if (flag == 1) {
99 5 up20180614
            scan_arr[1] = scancode;
100
            kbd_print_scancode(!(scancode & 0x80), 2, scan_arr);
101 6 up20180614
            flag = 0;
102 5 up20180614
          }
103 6 up20180614
          }
104 5 up20180614
105 4 up20180614
        }
106 5 up20180614
        // printf("msg:%x\n",msg.m_notify.interrupts);
107
        break;
108
      default:
109
        // printf("2\n");
110
        break; /* no other notifications expected: do nothing */
111
      }
112 4 up20180614
    } else {
113 5 up20180614
      // printf("3\n"); /* received a standard message, not a notification */
114
      /* no standard messages expected: do nothing */
115 4 up20180614
    }
116 5 up20180614
  }
117
  if (sys_irqrmpolicy(&hook_id) != 0)
118
    return 1;
119 4 up20180614
120
  return 0;
121
}
122
123
int(kbd_test_poll)() {
124
  /* To be completed by the students */
125 6 up20180614
  uint8_t scan_arr[2] = {0, 0};
126
  int flag = 0;
127
  while(scancode != EXIT_CODE)
128
  {
129
    tickdelay(micros_to_ticks(DELAY_US));
130
    sys_inb(0x64, &scancode);
131
          if (scancode & 0x01) {
132
            sys_inb(0x60, &scancode);
133
          if (scancode != 0xe0 && flag == 0) {
134
            scan_arr[0] = scancode;
135
            kbd_print_scancode(!(scancode & 0x80), 1, scan_arr);
136
          }
137 4 up20180614
138 6 up20180614
          else if (scancode == 0xe0) {
139
            scan_arr[0] = scancode;
140
            flag = 1;
141
          }
142
143
          else if (flag == 1) {
144
            scan_arr[1] = scancode;
145
            kbd_print_scancode(!(scancode & 0x80), 2, scan_arr);
146
            flag = 0;
147
          }
148
        }
149
150
  }
151
152 4 up20180614
  return 1;
153
}
154
155
int(kbd_test_timed_scan)(uint8_t n) {
156
  /* To be completed by the students */
157
  printf("%s is not yet implemented!\n", __func__);
158
159
  return 1;
160
}