root / lab3 / lab3.c @ 10
History | View | Annotate | Download (5.78 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 | 8 | up20180614 | #include "i8254.h" |
8 | 4 | up20180614 | #include <stdint.h> |
9 | 9 | up20180614 | #include "i8042.h" |
10 | #include "keyboard.h" |
||
11 | 4 | up20180614 | //#include <timer.c>
|
12 | |||
13 | 7 | up20180614 | |
14 | 9 | up20180614 | /* #define IRQ_KB 1
|
15 | 4 | up20180614 | #define DELAY_US 20000
|
16 | #define EXIT_CODE 0x81
|
||
17 | 9 | up20180614 | #define I_O_BUFFER 0x60 */
|
18 | 4 | up20180614 | |
19 | 9 | up20180614 | uint32_t cnt=0;
|
20 | uint32_t scancode; |
||
21 | int flag=0; |
||
22 | 4 | up20180614 | |
23 | int main(int argc, char *argv[]) { |
||
24 | // sets the language of LCF messages (can be either EN-US or PT-PT)
|
||
25 | lcf_set_language("EN-US");
|
||
26 | |||
27 | // enables to log function invocations that are being "wrapped" by LCF
|
||
28 | // [comment this out if you don't want/need it]
|
||
29 | lcf_trace_calls("/home/lcom/labs/lab3/trace.txt");
|
||
30 | |||
31 | // enables to save the output of printf function calls on a file
|
||
32 | // [comment this out if you don't want/need it]
|
||
33 | lcf_log_output("/home/lcom/labs/lab3/output.txt");
|
||
34 | |||
35 | // handles control over to LCF
|
||
36 | // [LCF handles command line arguments and invokes the right function]
|
||
37 | if (lcf_start(argc, argv))
|
||
38 | 6 | up20180614 | return 1; |
39 | 4 | up20180614 | |
40 | // LCF clean up tasks
|
||
41 | // [must be the last statement before return]
|
||
42 | lcf_cleanup(); |
||
43 | |||
44 | return 0; |
||
45 | } |
||
46 | |||
47 | |||
48 | 9 | up20180614 | |
49 | 4 | up20180614 | int(kbd_test_scan)() {
|
50 | /* To be completed by the students */
|
||
51 | int ipc_status;
|
||
52 | 5 | up20180614 | uint8_t r, bit_no = 0;
|
53 | 4 | up20180614 | message msg; |
54 | 5 | up20180614 | int hook_id = bit_no;
|
55 | 9 | up20180614 | /* uint8_t scan_arr[2] = {0, 0};
|
56 | 8 | up20180614 | |
57 | 9 | up20180614 | int flag = 0; */
|
58 | 4 | up20180614 | |
59 | 5 | up20180614 | printf("%d\n",
|
60 | sys_irqsetpolicy(IRQ_KB, IRQ_REENABLE | IRQ_EXCLUSIVE, &hook_id)); |
||
61 | /* sys_outb(0x64, 0x20);
|
||
62 | sys_inb(0x64, &scancode);
|
||
63 | printf("%x\n", scancode);
|
||
64 | if (scancode & 0x10) {
|
||
65 | scancode = (scancode & 0xEF);
|
||
66 | }
|
||
67 | |||
68 | printf("%x\n", scancode);
|
||
69 | sys_outb(0x64, 0x60);
|
||
70 | sys_outb(0x64, scancode); */
|
||
71 | |||
72 | printf("%x\n", scancode);
|
||
73 | 7 | up20180614 | //kbc_ih();
|
74 | 5 | up20180614 | printf("\n");
|
75 | 6 | up20180614 | while (scancode !=
|
76 | 8 | up20180614 | EXIT_CODE) |
77 | { /* You may want to use a different condition */
|
||
78 | 5 | up20180614 | /* Get a request message. */
|
79 | // printf("1\n");
|
||
80 | // printf("cont:%d, time:%d\n\n",cont/60,time);
|
||
81 | if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) { |
||
82 | printf("driver_receive failed with: %d", r);
|
||
83 | continue;
|
||
84 | 4 | up20180614 | } |
85 | if (is_ipc_notify(ipc_status)) { /* received notification */ |
||
86 | 5 | up20180614 | switch (_ENDPOINT_P(msg.m_source)) {
|
87 | case HARDWARE: /* hardware interrupt notification */ |
||
88 | if (msg.m_notify.interrupts & BIT(0)) { /* subscribed interrupt */ |
||
89 | |||
90 | 9 | up20180614 | kbc_ih(); |
91 | 5 | up20180614 | } |
92 | |||
93 | 9 | up20180614 | |
94 | 5 | up20180614 | // printf("msg:%x\n",msg.m_notify.interrupts);
|
95 | break;
|
||
96 | default:
|
||
97 | // printf("2\n");
|
||
98 | break; /* no other notifications expected: do nothing */ |
||
99 | } |
||
100 | 4 | up20180614 | } else {
|
101 | 5 | up20180614 | // printf("3\n"); /* received a standard message, not a notification */
|
102 | /* no standard messages expected: do nothing */
|
||
103 | 4 | up20180614 | } |
104 | 5 | up20180614 | } |
105 | 7 | up20180614 | kbd_print_no_sysinb(cnt); |
106 | 5 | up20180614 | if (sys_irqrmpolicy(&hook_id) != 0) |
107 | return 1; |
||
108 | 4 | up20180614 | |
109 | return 0; |
||
110 | } |
||
111 | |||
112 | int(kbd_test_poll)() {
|
||
113 | /* To be completed by the students */
|
||
114 | 8 | up20180614 | uint32_t key=0;
|
115 | 6 | up20180614 | uint8_t scan_arr[2] = {0, 0}; |
116 | 9 | up20180614 | //int flag = 0;
|
117 | //uint32_t cnt=0;
|
||
118 | |||
119 | 6 | up20180614 | while(scancode != EXIT_CODE)
|
120 | { |
||
121 | tickdelay(micros_to_ticks(DELAY_US)); |
||
122 | 7 | up20180614 | #ifdef LAB3
|
123 | cnt++; |
||
124 | #endif
|
||
125 | 9 | up20180614 | sys_inb(IN_BUF, &scancode); |
126 | if (scancode & O_BUFFER_FULL) {
|
||
127 | 7 | up20180614 | #ifdef LAB3
|
128 | cnt++; |
||
129 | #endif
|
||
130 | 9 | up20180614 | sys_inb(I_O_BUFFER, &scancode); |
131 | if (scancode != TWO_BYTES && flag == 0) { |
||
132 | 6 | up20180614 | scan_arr[0] = scancode;
|
133 | 9 | up20180614 | kbd_print_scancode(!(scancode & PAR_ERR), 1, scan_arr);
|
134 | 6 | up20180614 | } |
135 | 4 | up20180614 | |
136 | 9 | up20180614 | else if (scancode == TWO_BYTES) { |
137 | 6 | up20180614 | scan_arr[0] = scancode;
|
138 | flag = 1;
|
||
139 | } |
||
140 | |||
141 | else if (flag == 1) { |
||
142 | scan_arr[1] = scancode;
|
||
143 | 9 | up20180614 | kbd_print_scancode(!(scancode & PAR_ERR), 2, scan_arr);
|
144 | 6 | up20180614 | flag = 0;
|
145 | } |
||
146 | } |
||
147 | } |
||
148 | 9 | up20180614 | sys_outb(IN_BUF, READ_CMD); |
149 | sys_outb(IN_BUF, I_O_BUFFER); |
||
150 | |||
151 | #ifdef LAB3
|
||
152 | cnt++; |
||
153 | #endif
|
||
154 | |||
155 | sys_inb(I_O_BUFFER,&key); |
||
156 | |||
157 | sys_outb(IN_BUF,I_O_BUFFER); //enable interrupts again
|
||
158 | sys_outb(I_O_BUFFER, key | BIT(0));
|
||
159 | |||
160 | 7 | up20180614 | kbd_print_no_sysinb(cnt); |
161 | 8 | up20180614 | return 0; |
162 | 4 | up20180614 | } |
163 | |||
164 | int(kbd_test_timed_scan)(uint8_t n) {
|
||
165 | /* To be completed by the students */
|
||
166 | 9 | up20180614 | /* uint8_t scan_arr[2] = {0, 0};
|
167 | int flag=0; */
|
||
168 | 8 | up20180614 | int ipc_status;
|
169 | message msg; |
||
170 | uint8_t r; |
||
171 | uint8_t bit_no_timer = 0, bit_no_kbd = 1; |
||
172 | int hook_id_kbd=bit_no_kbd, hook_id_timer=bit_no_timer;
|
||
173 | 9 | up20180614 | int cont=0;//cnt=0; |
174 | 8 | up20180614 | |
175 | sys_irqsetpolicy(IRQ_TIMER,IRQ_REENABLE,&hook_id_timer); |
||
176 | sys_irqsetpolicy(IRQ_KB, IRQ_REENABLE | IRQ_EXCLUSIVE, &hook_id_kbd); |
||
177 | kbc_ih(); |
||
178 | while( (cont/DEFAULT_FREQ)<n && scancode !=EXIT_CODE) //se não atingir o tempo de espera nem a tecla for ESC |
||
179 | { |
||
180 | /* You may want to use a different condition */
|
||
181 | /* Get a request message. */
|
||
182 | // printf("1\n");
|
||
183 | //printf("cont:%d, time:%d\n\n",cont/60,time);
|
||
184 | if ( (r = driver_receive(ANY, &msg, &ipc_status)) != 0 ) |
||
185 | { |
||
186 | printf("driver_receive failed with: %d", r);
|
||
187 | continue;
|
||
188 | } |
||
189 | if (is_ipc_notify(ipc_status)) {
|
||
190 | switch (_ENDPOINT_P(msg.m_source)) {
|
||
191 | case HARDWARE:
|
||
192 | if (msg.m_notify.interrupts & BIT(bit_no_timer)) {
|
||
193 | cont++; |
||
194 | if(cont%DEFAULT_FREQ==0) |
||
195 | timer_print_elapsed_time(); |
||
196 | //printf("cont:%d\n",cont);
|
||
197 | } |
||
198 | if (msg.m_notify.interrupts & BIT(bit_no_kbd))
|
||
199 | { |
||
200 | |||
201 | 9 | up20180614 | kbc_ih(); |
202 | 8 | up20180614 | cont=0;
|
203 | } |
||
204 | //printf("msg:%x\n",msg.m_notify.interrupts);
|
||
205 | break;
|
||
206 | default:
|
||
207 | //printf("2\n");
|
||
208 | break; /* no other notifications expected: do nothing */ |
||
209 | } |
||
210 | } |
||
211 | // printf("3\n"); /* received a standard message, not a notification */
|
||
212 | /* no standard messages expected: do nothing */
|
||
213 | |||
214 | 9 | up20180614 | |
215 | 8 | up20180614 | } |
216 | kbd_print_no_sysinb(cnt); |
||
217 | |||
218 | if(sys_irqrmpolicy(&hook_id_timer)!=0) return 1; |
||
219 | if(sys_irqrmpolicy(&hook_id_kbd)!=0) return 1; |
||
220 | |||
221 | |||
222 | return 0; |
||
223 | |||
224 | |||
225 | //printf("%s is not yet implemented!\n", __func__);
|
||
226 | |||
227 | |||
228 | 4 | up20180614 | } |