root / lab4 / mouse.c @ 80
History | View | Annotate | Download (4.72 KB)
1 | 59 | up20180642 | #include <lcom/lcf.h> |
---|---|---|---|
2 | |||
3 | #include "mouse.h" |
||
4 | |||
5 | #include "errors.h" |
||
6 | #include "kbc_macros.h" |
||
7 | #include "mouse_macros.h" |
||
8 | #include "kbc.h" |
||
9 | |||
10 | int (subscribe_mouse_interrupt)(uint8_t interrupt_bit, int *interrupt_id) { |
||
11 | if (interrupt_id == NULL) return NULL_PTR; |
||
12 | *interrupt_id = interrupt_bit; |
||
13 | if (sys_irqsetpolicy(MOUSE_IRQ, IRQ_REENABLE | IRQ_EXCLUSIVE, interrupt_id)) return SBCR_ERROR; |
||
14 | return SUCCESS;
|
||
15 | } |
||
16 | |||
17 | int got_error_mouse_ih = 0; |
||
18 | 80 | up20180642 | int counter_mouse_ih = 0; |
19 | 59 | up20180642 | |
20 | void (mouse_ih)(void) { |
||
21 | uint8_t status = 0;
|
||
22 | got_error_mouse_ih = 0;
|
||
23 | 80 | up20180642 | if(counter_mouse_ih >= 3) counter_mouse_ih = 0; |
24 | 59 | up20180642 | |
25 | 68 | up20180642 | if ((got_error_mouse_ih = util_sys_inb(STATUS_REG, &status))) return; |
26 | 59 | up20180642 | |
27 | if (status & (TIME_OUT_REC | PARITY_ERROR)) {
|
||
28 | 68 | up20180642 | got_error_mouse_ih = OTHER_ERROR; |
29 | 59 | up20180642 | return;
|
30 | } |
||
31 | 76 | up20180655 | if (((status & AUX_MOUSE) == 0) || ((status & OUT_BUF_FUL) == 0)) { |
32 | got_error_mouse_ih = OTHER_ERROR; |
||
33 | return;
|
||
34 | } |
||
35 | 59 | up20180642 | |
36 | uint8_t byte = 0;
|
||
37 | |||
38 | 68 | up20180642 | if ((got_error_mouse_ih = util_sys_inb(OUTPUT_BUF, &byte))) return; |
39 | 59 | up20180642 | |
40 | /// This does not run if: I was expecting the first one but what I get is definitely not the first byte
|
||
41 | 80 | up20180642 | if((byte & FIRST_BYTE_ID) || counter_mouse_ih){
|
42 | packet_mouse_ih[counter_mouse_ih] = byte; |
||
43 | counter_mouse_ih++; |
||
44 | 59 | up20180642 | } |
45 | } |
||
46 | |||
47 | struct packet (mouse_parse_packet)(const uint8_t *packet_bytes){ |
||
48 | struct packet pp;
|
||
49 | pp.bytes[0] = packet_bytes[0]; |
||
50 | pp.bytes[1] = packet_bytes[1]; |
||
51 | pp.bytes[2] = packet_bytes[2]; |
||
52 | pp.rb = pp.bytes[0] & RIGHT_BUTTON;
|
||
53 | pp.mb = pp.bytes[0] & MIDDLE_BUTTON;
|
||
54 | pp.lb = pp.bytes[0] & LEFT_BUTTON;
|
||
55 | 71 | up20180655 | pp.delta_x = sign_extend_byte((packet_bytes[0] & MSB_X_DELTA) != 0, pp.bytes[1]); |
56 | pp.delta_y = sign_extend_byte((packet_bytes[0] & MSB_Y_DELTA) != 0, pp.bytes[2]); |
||
57 | 59 | up20180642 | pp.x_ov = pp.bytes[0] & X_OVERFLOW;
|
58 | pp.y_ov = pp.bytes[0] & Y_OVERFLOW;
|
||
59 | return pp;
|
||
60 | } |
||
61 | |||
62 | 80 | up20180642 | int mouse_poll(struct packet *pp, uint16_t period){ |
63 | 77 | up20180642 | int ret = 0; |
64 | 80 | up20180642 | |
65 | int counter = 0; |
||
66 | uint8_t packet[3];
|
||
67 | uint8_t byte; |
||
68 | while(counter < 3){ |
||
69 | if((ret = mouse_read_data(&byte, period))) return ret; |
||
70 | if((byte & FIRST_BYTE_ID) || counter){
|
||
71 | packet[counter] = byte; |
||
72 | counter++; |
||
73 | } |
||
74 | 77 | up20180642 | } |
75 | 80 | up20180642 | *pp = mouse_parse_packet(packet); |
76 | return SUCCESS;
|
||
77 | 59 | up20180642 | } |
78 | 70 | up20180655 | |
79 | 80 | up20180642 | int (mouse_set_data_report)(int on){ |
80 | if(on) return mouse_issue_cmd(ENABLE_DATA_REP); |
||
81 | else return mouse_issue_cmd( DIS_DATA_REP); |
||
82 | } |
||
83 | |||
84 | int (mouse_read_data)(uint8_t *data, uint16_t period) {
|
||
85 | 70 | up20180655 | int ret;
|
86 | if ((ret = mouse_issue_cmd(READ_DATA))) return ret; |
||
87 | 80 | up20180642 | if ((ret = mouse_poll_byte(data, period))) return ret; |
88 | 70 | up20180655 | return SUCCESS;
|
89 | } |
||
90 | |||
91 | int (mouse_issue_cmd)(uint32_t cmd) {
|
||
92 | int ret;
|
||
93 | uint8_t ack = 0;
|
||
94 | 78 | up20180655 | for (unsigned int i = 0; i < KBC_NUM_TRIES; i++) { |
95 | if ((ret = kbc_issue_cmd(MOUSE_WRITE_B))) return ret; |
||
96 | if ((ret = kbc_issue_arg(cmd))) return ret; |
||
97 | if ((ret = mouse_read_ack(&ack))) return ret; |
||
98 | |||
99 | if (ack == ACK_OK) return SUCCESS; |
||
100 | if (ack == ACK_ERROR) return INVALID_COMMAND; |
||
101 | tickdelay(micros_to_ticks(DELAY)); |
||
102 | } |
||
103 | return TIMEOUT_ERROR;
|
||
104 | 70 | up20180655 | } |
105 | |||
106 | int (mouse_read_byte)(uint8_t *byte) {
|
||
107 | int ret = 0; |
||
108 | uint8_t stat; |
||
109 | for(int i = 0; i < KBC_NUM_TRIES; ++i){ |
||
110 | if((ret = util_sys_inb(STATUS_REG, &stat))) return ret; |
||
111 | if((stat&OUT_BUF_FUL) && (stat&AUX_MOUSE)) {
|
||
112 | if(stat & (PARITY_ERROR | TIME_OUT_REC)) return OTHER_ERROR; |
||
113 | if((ret = util_sys_inb(OUTPUT_BUF, byte))) return ret; |
||
114 | else return SUCCESS; |
||
115 | } |
||
116 | tickdelay(micros_to_ticks(DELAY)); |
||
117 | } |
||
118 | return TIMEOUT_ERROR;
|
||
119 | } |
||
120 | 71 | up20180655 | |
121 | 77 | up20180642 | int (mouse_read_ack)(uint8_t *byte) {
|
122 | int ret = 0; |
||
123 | uint8_t stat; |
||
124 | //for(int i = 0; i < KBC_NUM_TRIES; ++i){
|
||
125 | if((ret = util_sys_inb(STATUS_REG, &stat))) return ret; |
||
126 | //if((stat&OUT_BUF_FUL) && (stat&AUX_MOUSE)) {
|
||
127 | if(stat & (PARITY_ERROR | TIME_OUT_REC)) return OTHER_ERROR; |
||
128 | if((ret = util_sys_inb(OUTPUT_BUF, byte))) return ret; |
||
129 | else return SUCCESS; |
||
130 | //}
|
||
131 | //tickdelay(micros_to_ticks(DELAY));
|
||
132 | //}
|
||
133 | //return TIMEOUT_ERROR;
|
||
134 | } |
||
135 | |||
136 | 80 | up20180642 | int (mouse_poll_byte)(uint8_t *byte, uint16_t period) {
|
137 | int ret = 0; |
||
138 | uint8_t stat; |
||
139 | while(true){ |
||
140 | if((ret = util_sys_inb(STATUS_REG, &stat))) return ret; |
||
141 | if((stat&OUT_BUF_FUL) && (stat&AUX_MOUSE)) {
|
||
142 | if(stat & (PARITY_ERROR | TIME_OUT_REC)) return OTHER_ERROR; |
||
143 | if((ret = util_sys_inb(OUTPUT_BUF, byte))) return ret; |
||
144 | else return SUCCESS; |
||
145 | } |
||
146 | tickdelay(micros_to_ticks(period*1000));
|
||
147 | } |
||
148 | } |
||
149 | |||
150 | 71 | up20180655 | int16_t (sign_extend_byte)(uint8_t sign_bit, uint8_t byte) { |
151 | return (int16_t)(((0xFF * sign_bit)<<8) | byte); |
||
152 | } |