root / lab3 / lab3.c @ 6
History | View | Annotate | Download (4.06 KB)
1 |
#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 |
return 1; |
33 |
|
34 |
// LCF clean up tasks
|
35 |
// [must be the last statement before return]
|
36 |
lcf_cleanup(); |
37 |
|
38 |
return 0; |
39 |
} |
40 |
|
41 |
void(kbc_ih)() {}
|
42 |
|
43 |
int(kbd_test_scan)() {
|
44 |
/* To be completed by the students */
|
45 |
int ipc_status;
|
46 |
uint8_t r, bit_no = 0;
|
47 |
message msg; |
48 |
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 |
int flag = 0; |
53 |
|
54 |
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 |
while (scancode !=
|
71 |
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 |
} |
79 |
if (is_ipc_notify(ipc_status)) { /* received notification */ |
80 |
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 |
if (scancode != 0xe0 && flag == 0) { |
88 |
// tickdelay(micros_to_ticks(DELAY_US));
|
89 |
scan_arr[0] = scancode;
|
90 |
kbd_print_scancode(!(scancode & 0x80), 1, scan_arr); |
91 |
} |
92 |
|
93 |
else if (scancode == 0xe0) { |
94 |
scan_arr[0] = scancode;
|
95 |
flag = 1;
|
96 |
} |
97 |
|
98 |
else if (flag == 1) { |
99 |
scan_arr[1] = scancode;
|
100 |
kbd_print_scancode(!(scancode & 0x80), 2, scan_arr); |
101 |
flag = 0;
|
102 |
} |
103 |
} |
104 |
|
105 |
} |
106 |
// 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 |
} else {
|
113 |
// printf("3\n"); /* received a standard message, not a notification */
|
114 |
/* no standard messages expected: do nothing */
|
115 |
} |
116 |
} |
117 |
if (sys_irqrmpolicy(&hook_id) != 0) |
118 |
return 1; |
119 |
|
120 |
return 0; |
121 |
} |
122 |
|
123 |
int(kbd_test_poll)() {
|
124 |
/* To be completed by the students */
|
125 |
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 |
|
138 |
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 |
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 |
} |