root / lab3 / keyboard.c @ 11
History | View | Annotate | Download (2.15 KB)
1 | 10 | up20180645 | #include "keyboard.h" |
---|---|---|---|
2 | 9 | up20180645 | |
3 | 10 | up20180645 | uint8_t scan_code=0; // make code or break code |
4 | 9 | up20180645 | int keyboard_id = KEYBOARD_IRQ; // KEYBOARD_IRQ is defined in interrupt.h in .minix-src folder |
5 | uint32_t cnt = 0; // counter of sys_inb calls |
||
6 | |||
7 | int (util_sys_inb)(int port, uint8_t *value) { //transform 8 bit into 32 bit |
||
8 | |||
9 | uint32_t new_val; //initializing 32 bit variable
|
||
10 | |||
11 | if(sys_inb(port,&new_val)!=0){ //verifies if there is an error |
||
12 | printf("Error in util_sys_inb\n");
|
||
13 | return 1; |
||
14 | } |
||
15 | *value=new_val & 0xFF; //dereferencing "value" |
||
16 | #ifdef LAB3
|
||
17 | cnt++; |
||
18 | #endif
|
||
19 | |||
20 | return 0; |
||
21 | } |
||
22 | |||
23 | void (kbc_ih)(void){ |
||
24 | 10 | up20180645 | uint8_t status_reg; |
25 | 9 | up20180645 | |
26 | 10 | up20180645 | if(util_sys_inb(STATUS_REG,&status_reg)!=0){ //checks if there is an error |
27 | 9 | up20180645 | return;
|
28 | } |
||
29 | 11 | up20180645 | if(((status_reg & STAT_REG_OBF)==0) ||((status_reg&(STAT_REG_PAR|STAT_REG_TIMEOUT))!=0)|| ((status_reg&STAT_REG_AUX)!=0)){ //checks if there is a parity or timeout error (mask -> 0xC0, bit 7 and 6 set), checks if output buffer is empty and checks if there is data coming from the mouse |
30 | 9 | up20180645 | return;
|
31 | } |
||
32 | 10 | up20180645 | util_sys_inb(OUTPUT_BUF,&scan_code); |
33 | 9 | up20180645 | |
34 | } |
||
35 | |||
36 | 11 | up20180645 | |
37 | 9 | up20180645 | int (kbc_subscribe_int)(uint8_t *bit_no) { //similar function to that of timer_subscribe_int |
38 | *bit_no = BIT(keyboard_id); |
||
39 | if(sys_irqsetpolicy(KEYBOARD_IRQ,(IRQ_REENABLE|IRQ_EXCLUSIVE),&keyboard_id)==1){ //operation to subscribe int |
||
40 | printf("Error subscribing int\n");
|
||
41 | return 1; |
||
42 | } |
||
43 | return 0; |
||
44 | } |
||
45 | |||
46 | int (kbc_unsubscribe_int)() { //similar function to that of timer_unsubscribe_int |
||
47 | if(sys_irqrmpolicy(&keyboard_id)==1){ |
||
48 | printf("Error unsubscribing int\n");
|
||
49 | return 1; |
||
50 | } |
||
51 | return 0; |
||
52 | } |
||
53 | 11 | up20180645 | |
54 | int (enable_interrupt)()
|
||
55 | { |
||
56 | uint8_t command; |
||
57 | |||
58 | if(sys_outb(STATUS_REG,READ_COMMAND)!=0){ |
||
59 | printf("Error writing read-command\n");
|
||
60 | return 1; |
||
61 | } |
||
62 | |||
63 | if(util_sys_inb(OUTPUT_BUF, &command)!=0){ |
||
64 | printf("Error reading output buffer\n");
|
||
65 | return 1; |
||
66 | } |
||
67 | |||
68 | command = command | INT_ENABLE; |
||
69 | |||
70 | if(sys_outb(STATUS_REG,OUTPUT_BUF)!=0){ |
||
71 | printf("Error writing output buffer\n");
|
||
72 | return 1; |
||
73 | } |
||
74 | |||
75 | if(sys_outb(OUTPUT_BUF,command)!=0){ |
||
76 | printf("Error writing new buffer status\n");
|
||
77 | return 1; |
||
78 | } |
||
79 | |||
80 | return 0; |
||
81 | } |