Project

General

Profile

Statistics
| Revision:

root / lab4 / test / lab3 / lab3.c @ 13

History | View | Annotate | Download (5.78 KB)

1 13 up20180614
#include <lcom/lcf.h>
2
#include <minix/sysutil.h>
3
4
#include <lcom/lab3.h>
5
6
#include <stdbool.h>
7
#include "i8254.h"
8
#include <stdint.h>
9
#include "i8042.h"
10
#include "keyboard.h"
11
//#include <timer.c>
12
13
14
/* #define IRQ_KB 1
15
#define DELAY_US 20000
16
#define EXIT_CODE 0x81
17
#define I_O_BUFFER 0x60 */
18
19
uint32_t cnt=0;
20
 uint32_t scancode;
21
int flag=0;
22
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
    return 1;
39
40
  // LCF clean up tasks
41
  // [must be the last statement before return]
42
  lcf_cleanup();
43
44
  return 0;
45
}
46
47
48
49
int(kbd_test_scan)() {
50
  /* To be completed by the students */
51
  int ipc_status;
52
  uint8_t r, bit_no = 0;
53
  message msg;
54
  int hook_id = bit_no;
55
/*   uint8_t scan_arr[2] = {0, 0};
56

57
  int flag = 0; */
58
59
  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
  //kbc_ih();
74
  printf("\n");
75
  while (scancode !=
76
         EXIT_CODE)
77
  { /* You may want to use a different condition */
78
    /* 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
    }
85
    if (is_ipc_notify(ipc_status)) { /* received notification */
86
      switch (_ENDPOINT_P(msg.m_source)) {
87
      case HARDWARE: /* hardware interrupt notification */
88
        if (msg.m_notify.interrupts & BIT(0)) { /* subscribed interrupt */
89
90
          kbc_ih();
91
          }
92
93
94
        // 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
    } else {
101
      // printf("3\n"); /* received a standard message, not a notification */
102
      /* no standard messages expected: do nothing */
103
    }
104
  }
105
  kbd_print_no_sysinb(cnt);
106
  if (sys_irqrmpolicy(&hook_id) != 0)
107
    return 1;
108
109
  return 0;
110
}
111
112
int(kbd_test_poll)() {
113
  /* To be completed by the students */
114
  uint32_t key=0;
115
  uint8_t scan_arr[2] = {0, 0};
116
  //int flag = 0;
117
  //uint32_t cnt=0;
118
119
  while(scancode != EXIT_CODE)
120
  {
121
    tickdelay(micros_to_ticks(DELAY_US));
122
    #ifdef LAB3
123
    cnt++;
124
    #endif
125
    sys_inb(IN_BUF, &scancode);
126
          if (scancode & O_BUFFER_FULL) {
127
            #ifdef LAB3
128
            cnt++;
129
             #endif
130
            sys_inb(I_O_BUFFER, &scancode);
131
          if (scancode != TWO_BYTES && flag == 0) {
132
            scan_arr[0] = scancode;
133
            kbd_print_scancode(!(scancode & PAR_ERR), 1, scan_arr);
134
          }
135
136
          else if (scancode == TWO_BYTES) {
137
            scan_arr[0] = scancode;
138
            flag = 1;
139
          }
140
141
          else if (flag == 1) {
142
            scan_arr[1] = scancode;
143
            kbd_print_scancode(!(scancode & PAR_ERR), 2, scan_arr);
144
            flag = 0;
145
          }
146
        }
147
  }
148
  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
  kbd_print_no_sysinb(cnt);
161
  return 0;
162
}
163
164
int(kbd_test_timed_scan)(uint8_t n) {
165
  /* To be completed by the students */
166
/*   uint8_t scan_arr[2] = {0, 0};
167
  int flag=0; */
168
   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
  int cont=0;//cnt=0;
174
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
                  kbc_ih();
202
                   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
215
 }
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
}