root / proj / libs / kbc / src / mouse.c @ 335
History | View | Annotate | Download (6.2 KB)
1 | 150 | up20180655 | #include <lcom/lcf.h> |
---|---|---|---|
2 | |||
3 | #include "mouse.h" |
||
4 | |||
5 | #include "errors.h" |
||
6 | #include "kbc.h" |
||
7 | |||
8 | 255 | up20180642 | /* Mouse Data Packet */
|
9 | // Byte 0 - Button States
|
||
10 | #define LEFT_BUTTON BIT(0) /* @brief Left button click event*/ |
||
11 | #define RIGHT_BUTTON BIT(1) /* @brief Right button click event */ |
||
12 | #define MIDDLE_BUTTON BIT(2) /* @brief Middle button click event */ |
||
13 | #define FIRST_BYTE_ID BIT(3) /* @brief Identifier of first byte of packet CAREFUL: Not 100% accurate */ |
||
14 | #define MSB_X_DELTA BIT(4) /* @brief Most significant bit of X delta */ |
||
15 | #define MSB_Y_DELTA BIT(5) /* @brief Most significant bit of Y delta */ |
||
16 | #define X_OVERFLOW BIT(6) /* @brief X delta overflowed */ |
||
17 | #define Y_OVERFLOW BIT(7) /* @brief Y delta overflowed */ |
||
18 | // Byte 1 - X delta
|
||
19 | // Byte 2 - Y delta
|
||
20 | |||
21 | /* Mouse Commands */
|
||
22 | 324 | up20180642 | #define RESET 0xFF /* @brief Reset mouse */ |
23 | #define RESEND 0xFE /* @brief Resend command */ |
||
24 | #define DEFAULT 0xF6 /* @brief Set default values */ |
||
25 | 255 | up20180642 | #define DIS_DATA_REP 0xF5 /* @brief Disable Data Reporting */ |
26 | #define ENABLE_DATA_REP 0xF4 /* @brief Enable Data Reporting */ |
||
27 | 324 | up20180642 | #define SET_SAMPLE_RT 0xF3 /* @brief Sets state sampling rate */ |
28 | #define SET_REMOTE_MD 0xF0 /* @brief Sets Mouse on Remote Mode, data on request */ |
||
29 | 255 | up20180642 | #define READ_DATA 0xEB /* @brief Sends data packet request */ |
30 | 324 | up20180642 | #define SET_STREAM_MD 0xEA /* @brief Sets mouse on Stream Mode, data on events */ |
31 | #define STATUS_REQUEST 0xE9 /* @brief Get mouse configuration */ |
||
32 | #define SET_RESOLUTION 0xE8 /* @brief Sets resolution for mouse movement */ |
||
33 | #define SCALING_ACC_MD 0xE7 /* @brief Sets scaling on acceleration mode */ |
||
34 | #define SCALING_LIN_MD 0xE6 /* @brief Sets scaling on linear mode */ |
||
35 | 255 | up20180642 | |
36 | /* Mouse Controller Responses */
|
||
37 | #define ACK_OK 0xFA /* @brief Operation sucessful */ |
||
38 | 324 | up20180642 | #define ACK_INVALID 0xFE /* @brief Invalid Byte, first occurence */ |
39 | 255 | up20180642 | #define ACK_ERROR 0xFC /* @brief Invalid Byte on resend */ |
40 | |||
41 | 150 | up20180655 | int (subscribe_mouse_interrupt)(uint8_t interrupt_bit, int *interrupt_id) { |
42 | if (interrupt_id == NULL) return NULL_PTR; |
||
43 | *interrupt_id = interrupt_bit; |
||
44 | if (sys_irqsetpolicy(MOUSE_IRQ, IRQ_REENABLE | IRQ_EXCLUSIVE, interrupt_id)) return SBCR_ERROR; |
||
45 | return SUCCESS;
|
||
46 | } |
||
47 | |||
48 | 323 | up20180642 | static int got_error_mouse_ih = 0; |
49 | static uint8_t packet_mouse_ih[3]; |
||
50 | static int counter_mouse_ih = 0; |
||
51 | 322 | up20180642 | int (mouse_get_got_error_mouse_ih)(void){return got_error_mouse_ih; } |
52 | const uint8_t* (mouse_get_packet_mouse_ih)(void){return packet_mouse_ih; } |
||
53 | int (mouse_get_counter_mouse_ih)(void){return counter_mouse_ih; } |
||
54 | 323 | up20180642 | void (mouse_set_counter_mouse_ih)(int n){ counter_mouse_ih = n; } |
55 | 150 | up20180655 | |
56 | void (mouse_ih)(void) { |
||
57 | uint8_t status = 0;
|
||
58 | got_error_mouse_ih = 0;
|
||
59 | if(counter_mouse_ih >= 3) counter_mouse_ih = 0; |
||
60 | |||
61 | if ((got_error_mouse_ih = util_sys_inb(STATUS_REG, &status))) return; |
||
62 | |||
63 | if (status & (TIME_OUT_REC | PARITY_ERROR)) {
|
||
64 | got_error_mouse_ih = OTHER_ERROR; |
||
65 | return;
|
||
66 | } |
||
67 | if (((status & AUX_MOUSE) == 0) || ((status & OUT_BUF_FUL) == 0)) { |
||
68 | got_error_mouse_ih = READ_ERROR; |
||
69 | return;
|
||
70 | } |
||
71 | |||
72 | uint8_t byte = 0;
|
||
73 | |||
74 | if ((got_error_mouse_ih = util_sys_inb(OUTPUT_BUF, &byte))) return; |
||
75 | |||
76 | /// This does not run if: I was expecting the first one but what I get is definitely not the first byte
|
||
77 | if((byte & FIRST_BYTE_ID) || counter_mouse_ih){
|
||
78 | packet_mouse_ih[counter_mouse_ih++] = byte; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | 250 | up20180655 | void (mouse_parse_packet)(const uint8_t *packet_bytes, struct packet *pp){ |
83 | pp->bytes[0] = packet_bytes[0]; |
||
84 | pp->bytes[1] = packet_bytes[1]; |
||
85 | pp->bytes[2] = packet_bytes[2]; |
||
86 | pp->rb = pp->bytes[0] & RIGHT_BUTTON;
|
||
87 | pp->mb = pp->bytes[0] & MIDDLE_BUTTON;
|
||
88 | pp->lb = pp->bytes[0] & LEFT_BUTTON;
|
||
89 | pp->delta_x = sign_extend_byte((packet_bytes[0] & MSB_X_DELTA) != 0, pp->bytes[1]); |
||
90 | pp->delta_y = sign_extend_byte((packet_bytes[0] & MSB_Y_DELTA) != 0, pp->bytes[2]); |
||
91 | pp->x_ov = pp->bytes[0] & X_OVERFLOW;
|
||
92 | pp->y_ov = pp->bytes[0] & Y_OVERFLOW;
|
||
93 | 150 | up20180655 | } |
94 | |||
95 | int mouse_poll(struct packet *pp, uint16_t period){ |
||
96 | int ret = 0; |
||
97 | |||
98 | uint8_t packet[3];
|
||
99 | uint8_t byte; |
||
100 | if ((ret = mouse_issue_cmd(READ_DATA))) return ret; |
||
101 | for(unsigned i = 0; i < 3; ++i){ |
||
102 | if((ret = mouse_poll_byte(&byte, period))) return ret; |
||
103 | packet[i] = byte; |
||
104 | } |
||
105 | 250 | up20180655 | mouse_parse_packet(packet, pp); |
106 | 150 | up20180655 | return SUCCESS;
|
107 | } |
||
108 | |||
109 | int (mouse_set_data_report)(int on){ |
||
110 | if(on) return mouse_issue_cmd(ENABLE_DATA_REP); |
||
111 | else return mouse_issue_cmd( DIS_DATA_REP); |
||
112 | } |
||
113 | |||
114 | int (mouse_read_data)(uint8_t *data, uint16_t period) {
|
||
115 | int ret;
|
||
116 | if ((ret = mouse_issue_cmd(READ_DATA))) return ret; |
||
117 | if ((ret = mouse_poll_byte(data, period))) return ret; |
||
118 | return SUCCESS;
|
||
119 | } |
||
120 | |||
121 | 323 | up20180642 | int (mouse_issue_cmd)(uint8_t cmd) {
|
122 | 150 | up20180655 | int ret;
|
123 | uint8_t ack = 0;
|
||
124 | for (unsigned int i = 0; i < KBC_NUM_TRIES; i++) { |
||
125 | if ((ret = kbc_issue_cmd(MOUSE_WRITE_B))) return ret; |
||
126 | if ((ret = kbc_issue_arg(cmd))) return ret; |
||
127 | if ((ret = mouse_read_byte(&ack))) return ret; |
||
128 | |||
129 | if (ack == ACK_OK) return SUCCESS; |
||
130 | if (ack == ACK_ERROR) return INVALID_COMMAND; |
||
131 | tickdelay(micros_to_ticks(DELAY)); |
||
132 | } |
||
133 | return TIMEOUT_ERROR;
|
||
134 | } |
||
135 | |||
136 | int (mouse_read_byte)(uint8_t *byte) {
|
||
137 | int ret = 0; |
||
138 | uint8_t stat; |
||
139 | for(int i = 0; i < KBC_NUM_TRIES; ++i){ |
||
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(DELAY)); |
||
147 | } |
||
148 | return TIMEOUT_ERROR;
|
||
149 | } |
||
150 | |||
151 | int (mouse_poll_byte)(uint8_t *byte, uint16_t period) {
|
||
152 | int ret = 0; |
||
153 | uint8_t stat; |
||
154 | while(true){ |
||
155 | if((ret = util_sys_inb(STATUS_REG, &stat))) return ret; |
||
156 | if((stat&OUT_BUF_FUL) && (stat&AUX_MOUSE)) {
|
||
157 | if(stat & (PARITY_ERROR | TIME_OUT_REC)) return OTHER_ERROR; |
||
158 | if((ret = util_sys_inb(OUTPUT_BUF, byte))) return ret; |
||
159 | else return SUCCESS; |
||
160 | } |
||
161 | 328 | up20180642 | tickdelay(micros_to_ticks(period)); |
162 | 150 | up20180655 | } |
163 | } |
||
164 | |||
165 | int16_t (sign_extend_byte)(uint8_t sign_bit, uint8_t byte) { |
||
166 | return (int16_t)(((0xFF * sign_bit)<<8) | byte); |
||
167 | } |