root / lab4 / mouse.c @ 16
History | View | Annotate | Download (5.29 KB)
1 | 16 | up20180645 | #include "mouse.h" |
---|---|---|---|
2 | #include <lcom/lcf.h> |
||
3 | 15 | up20180645 | |
4 | uint8_t byte; |
||
5 | 16 | up20180645 | int mouse_id=2; //KBD_AUX_IRQ is defined in interrupt.h in .minix-src folder |
6 | 15 | up20180645 | |
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 (mouse_ih)(void){ |
||
24 | 16 | up20180645 | //kbc_read_out_buf(&byte);
|
25 | 15 | up20180645 | uint8_t status_reg; |
26 | |||
27 | 16 | up20180645 | for(int i=0; i<10;i++){ |
28 | if(util_sys_inb(STATUS_REG,&status_reg)!=0){ //checks if there is an error |
||
29 | printf("Error reading status register in the mouse interruption\n");
|
||
30 | return;
|
||
31 | } |
||
32 | if((status_reg & OUTPUT_BUF)!=0){ |
||
33 | if((status_reg & (STAT_REG_PAR|STAT_REG_TIMEOUT))!=0){ //checks if there is a parity or timeout error (mask -> 0xC0, bit 7 and 6 set), checks if output buffer is empty |
||
34 | printf("Parity/Timeout error or output buffer is empty or data coming from the mouse\n");
|
||
35 | return;
|
||
36 | } |
||
37 | |||
38 | if(util_sys_inb(OUTPUT_BUF,&byte)!=0){//checks if there is an error |
||
39 | printf("Error reading output buffer in the mouse interruption\n");
|
||
40 | return;
|
||
41 | } |
||
42 | |||
43 | } |
||
44 | tickdelay(micros_to_ticks(DELAY_US)); |
||
45 | 15 | up20180645 | } |
46 | 16 | up20180645 | return;
|
47 | 15 | up20180645 | |
48 | } |
||
49 | |||
50 | int (mouse_subscribe_int)(uint8_t *bit_no) { //similar function to that of timer_subscribe_int |
||
51 | 16 | up20180645 | *bit_no = BIT(mouse_id); |
52 | if(sys_irqsetpolicy(12,(IRQ_REENABLE|IRQ_EXCLUSIVE),&mouse_id)==1){ //operation to subscribe int |
||
53 | 15 | up20180645 | printf("Error subscribing int\n");
|
54 | return 1; |
||
55 | } |
||
56 | return 0; |
||
57 | } |
||
58 | |||
59 | int (mouse_unsubscribe_int)() { //similar function to that of timer_unsubscribe_int |
||
60 | 16 | up20180645 | if(sys_irqrmpolicy(&mouse_id)==1){ |
61 | 15 | up20180645 | printf("Error unsubscribing int\n");
|
62 | return 1; |
||
63 | } |
||
64 | return 0; |
||
65 | } |
||
66 | |||
67 | int parse_packet(struct packet *pp){ |
||
68 | if((pp->bytes[0]&0x80) == 0x80){ |
||
69 | pp->y_ov=true;
|
||
70 | |||
71 | } |
||
72 | else{
|
||
73 | pp->y_ov=false;
|
||
74 | } |
||
75 | |||
76 | if((pp->bytes[0]&0x40)==0x40){ |
||
77 | pp->x_ov=true;
|
||
78 | |||
79 | } |
||
80 | else{
|
||
81 | pp->x_ov=false;
|
||
82 | } |
||
83 | |||
84 | if((pp->bytes[0]&0x02)==0x02){ |
||
85 | pp->rb=true;
|
||
86 | |||
87 | } |
||
88 | else{
|
||
89 | pp->rb=false;
|
||
90 | } |
||
91 | |||
92 | if((pp->bytes[0]&0x04)==0x04){ |
||
93 | pp->mb=true;
|
||
94 | } |
||
95 | else{
|
||
96 | pp->mb=false;
|
||
97 | } |
||
98 | |||
99 | if((pp->bytes[0]&0x01)==0x01){ |
||
100 | pp->lb=true;
|
||
101 | } |
||
102 | else{
|
||
103 | pp->lb=false;
|
||
104 | } |
||
105 | |||
106 | 16 | up20180645 | if((pp->bytes[0] &0x10)==0x10){ |
107 | pp->delta_x = pp->bytes[1] -256; |
||
108 | } |
||
109 | else{
|
||
110 | pp->delta_x = pp->bytes[1];
|
||
111 | } |
||
112 | 15 | up20180645 | |
113 | 16 | up20180645 | if((pp->bytes[0]&0x20)==0x20){ |
114 | pp->delta_y = pp->bytes[2] -256; |
||
115 | 15 | up20180645 | } |
116 | else{
|
||
117 | 16 | up20180645 | pp->delta_y = pp->bytes[2];
|
118 | 15 | up20180645 | } |
119 | |||
120 | 16 | up20180645 | return 0; |
121 | } |
||
122 | 15 | up20180645 | |
123 | 16 | up20180645 | /*int kbc_write(int port, uint8_t cmd) {
|
124 | uint8_t status;
|
||
125 | int retry = 0;
|
||
126 | |||
127 | while (retry < 10) {
|
||
128 | if(util_sys_inb(STATUS_REG, &status)!=0){
|
||
129 | printf("Error in kbc_write");
|
||
130 | return 1;
|
||
131 | }
|
||
132 | |||
133 | //... Checking if IN_BUF is OK to write to
|
||
134 | if ((status & 0x02)!=0) {
|
||
135 | //.... Writing the command
|
||
136 | if (sys_outb(port, cmd)!=0) {
|
||
137 | printf("Error");
|
||
138 | return 1;
|
||
139 | }
|
||
140 | return 0;
|
||
141 | }
|
||
142 | |||
143 | //tickdelay(micros_to_ticks(DELAY_US)); // IF NOT EMPTY wait for IN_BUF to be empty
|
||
144 | retry++;
|
||
145 | }
|
||
146 | |||
147 | return 1;
|
||
148 | }
|
||
149 | |||
150 | int read_out_buf(uint8_t *content) {
|
||
151 | uint8_t status;
|
||
152 | uint8_t content8; // 8-bit content
|
||
153 | int retry = 0;
|
||
154 | |||
155 | while (retry < 4) {
|
||
156 | util_sys_inb(STATUS_REG, &status);
|
||
157 | |||
158 | //... Checking if there is any error and if the byte came from AUX
|
||
159 | if (((status_reg & STAT_REG_OBF)==0) ||((status_reg&(STAT_REG_PAR|STAT_REG_TIMEOUT))!=0)||((status_reg&STAT_REG_AUX)==0)) { // Parity or Timeout error, invalid data
|
||
160 | return 1;
|
||
161 | }
|
||
162 | |||
163 | //... Checking if OUT_BUF is FULL to read
|
||
164 | if (status & STAT_REG_OBF) {
|
||
165 | //... Reading the Output Buffer
|
||
166 | if (UTIL_sys_inb(OUTPUT_BUF, &content8)) {
|
||
167 | return 1;
|
||
168 | }
|
||
169 | // The information in address content now contains content32 in 8-bit
|
||
170 | *content = (uint8_t)content8;
|
||
171 | return 0;
|
||
172 | }
|
||
173 | |||
174 | tickdelay(micros_to_ticks(DELAY_US)); // IF NOT EMPTY wait for IN_BUF to be empty
|
||
175 | retry++;
|
||
176 | }
|
||
177 | |||
178 | return 1;
|
||
179 | }
|
||
180 | |||
181 | |||
182 | int kbc_write_mouse_command(uint8_t cmd) {
|
||
183 | uint8_t response;
|
||
184 | |||
185 | if(kbc_write(KBC_CMD_REG, 0xD4)!=0){
|
||
186 | printf("Error");
|
||
187 | return 1;
|
||
188 | 15 | up20180645 | }
|
189 | 16 | up20180645 | if(kbc_write(INPUT_BUF, cmd)!=0){
|
190 | printf("Error");
|
||
191 | return 1;
|
||
192 | 15 | up20180645 | }
|
193 | 16 | up20180645 | tickdelay(micros_to_ticks(DELAY_US));
|
194 | if(kbc_read_out_buf(&response)!=0){
|
||
195 | printf("Error");
|
||
196 | return 1;
|
||
197 | }
|
||
198 | |||
199 | // Checking if invalid byte
|
||
200 | if (response != 0xFA) {
|
||
201 | // repeat process if invalid
|
||
202 | kbc_write(KBC_CTRL_REG, 0xD4);
|
||
203 | kbc_write(INPUT_BUF, cmd);
|
||
204 | tickdelay(micros_to_ticks(DELAY_US));
|
||
205 | kbc_read_out_buf(&response);
|
||
206 | // Checking if second consecutive invalid byte
|
||
207 | if (response == 0xFC){
|
||
208 | return 1;
|
||
209 | }
|
||
210 | }
|
||
211 | |||
212 | return 0;
|
||
213 | }*/
|
||
214 | |||
215 | int write_cmd_mouse(uint8_t cmd)
|
||
216 | { |
||
217 | uint8_t status_reg, ack; |
||
218 | |||
219 | do
|
||
220 | { |
||
221 | if (util_sys_inb(STATUS_REG, &status_reg) != 0){ |
||
222 | return 1; |
||
223 | } |
||
224 | |||
225 | if ((status_reg & STAT_REG_IBF) == 0){ |
||
226 | if (sys_outb(KBC_CMD_REG, 0xD4) != 0){ //0xD4 -> code to write byte to mouse |
||
227 | return 1; |
||
228 | } |
||
229 | } |
||
230 | else
|
||
231 | continue;
|
||
232 | |||
233 | if (util_sys_inb(STATUS_REG, &status_reg) != 0){ |
||
234 | return 1; |
||
235 | } |
||
236 | |||
237 | if ((status_reg & STAT_REG_IBF) == 0){ |
||
238 | if (sys_outb(OUTPUT_BUF, cmd) != 0){ |
||
239 | return 1; |
||
240 | } |
||
241 | } |
||
242 | else
|
||
243 | continue;
|
||
244 | |||
245 | tickdelay(micros_to_ticks(DELAY_US)); |
||
246 | |||
247 | if (util_sys_inb(INPUT_BUF, &ack) != 0){ |
||
248 | return 1; |
||
249 | } |
||
250 | |||
251 | } while (ack != 0xFA); // 0xFA -> mask that checks if everything is ok |
||
252 | |||
253 | return 0; |
||
254 | } |