root / proj / libs / peripherals / src / mouse.c @ 280
History | View | Annotate | Download (5.88 KB)
1 |
#include <lcom/lcf.h> |
---|---|
2 |
|
3 |
#include "mouse.h" |
4 |
|
5 |
#include "errors.h" |
6 |
#include "kbc.h" |
7 |
|
8 |
/* 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 |
#define RESET 0xFF /* @brief Reset mouse */ |
23 |
#define RESEND 0xFE /* @brief Resend command */ |
24 |
#define DEFAULT 0xF6 /* @brief Set default values */ |
25 |
#define DIS_DATA_REP 0xF5 /* @brief Disable Data Reporting */ |
26 |
#define ENABLE_DATA_REP 0xF4 /* @brief Enable Data Reporting */ |
27 |
#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 |
#define READ_DATA 0xEB /* @brief Sends data packet request */ |
30 |
#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 |
|
36 |
/* Mouse Controller Responses */
|
37 |
#define ACK_OK 0xFA /* @brief Operation sucessful */ |
38 |
#define ACK_INVALID 0xFE /* @brief Invalid Byte, first occurence */ |
39 |
#define ACK_ERROR 0xFC /* @brief Invalid Byte on resend */ |
40 |
|
41 |
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 |
int got_error_mouse_ih = 0; |
49 |
int counter_mouse_ih = 0; |
50 |
|
51 |
void (mouse_ih)(void) { |
52 |
uint8_t status = 0;
|
53 |
got_error_mouse_ih = 0;
|
54 |
if(counter_mouse_ih >= 3) counter_mouse_ih = 0; |
55 |
|
56 |
if ((got_error_mouse_ih = util_sys_inb(STATUS_REG, &status))) return; |
57 |
|
58 |
if (status & (TIME_OUT_REC | PARITY_ERROR)) {
|
59 |
got_error_mouse_ih = OTHER_ERROR; |
60 |
return;
|
61 |
} |
62 |
if (((status & AUX_MOUSE) == 0) || ((status & OUT_BUF_FUL) == 0)) { |
63 |
got_error_mouse_ih = READ_ERROR; |
64 |
return;
|
65 |
} |
66 |
|
67 |
uint8_t byte = 0;
|
68 |
|
69 |
if ((got_error_mouse_ih = util_sys_inb(OUTPUT_BUF, &byte))) return; |
70 |
|
71 |
/// This does not run if: I was expecting the first one but what I get is definitely not the first byte
|
72 |
if((byte & FIRST_BYTE_ID) || counter_mouse_ih){
|
73 |
packet_mouse_ih[counter_mouse_ih++] = byte; |
74 |
} |
75 |
} |
76 |
|
77 |
void (mouse_parse_packet)(const uint8_t *packet_bytes, struct packet *pp){ |
78 |
pp->bytes[0] = packet_bytes[0]; |
79 |
pp->bytes[1] = packet_bytes[1]; |
80 |
pp->bytes[2] = packet_bytes[2]; |
81 |
pp->rb = pp->bytes[0] & RIGHT_BUTTON;
|
82 |
pp->mb = pp->bytes[0] & MIDDLE_BUTTON;
|
83 |
pp->lb = pp->bytes[0] & LEFT_BUTTON;
|
84 |
pp->delta_x = sign_extend_byte((packet_bytes[0] & MSB_X_DELTA) != 0, pp->bytes[1]); |
85 |
pp->delta_y = sign_extend_byte((packet_bytes[0] & MSB_Y_DELTA) != 0, pp->bytes[2]); |
86 |
pp->x_ov = pp->bytes[0] & X_OVERFLOW;
|
87 |
pp->y_ov = pp->bytes[0] & Y_OVERFLOW;
|
88 |
} |
89 |
|
90 |
int mouse_poll(struct packet *pp, uint16_t period){ |
91 |
int ret = 0; |
92 |
|
93 |
uint8_t packet[3];
|
94 |
uint8_t byte; |
95 |
if ((ret = mouse_issue_cmd(READ_DATA))) return ret; |
96 |
for(unsigned i = 0; i < 3; ++i){ |
97 |
if((ret = mouse_poll_byte(&byte, period))) return ret; |
98 |
packet[i] = byte; |
99 |
} |
100 |
mouse_parse_packet(packet, pp); |
101 |
return SUCCESS;
|
102 |
} |
103 |
|
104 |
int (mouse_set_data_report)(int on){ |
105 |
if(on) return mouse_issue_cmd(ENABLE_DATA_REP); |
106 |
else return mouse_issue_cmd( DIS_DATA_REP); |
107 |
} |
108 |
|
109 |
int (mouse_read_data)(uint8_t *data, uint16_t period) {
|
110 |
int ret;
|
111 |
if ((ret = mouse_issue_cmd(READ_DATA))) return ret; |
112 |
if ((ret = mouse_poll_byte(data, period))) return ret; |
113 |
return SUCCESS;
|
114 |
} |
115 |
|
116 |
int (mouse_issue_cmd)(uint32_t cmd) {
|
117 |
int ret;
|
118 |
uint8_t ack = 0;
|
119 |
for (unsigned int i = 0; i < KBC_NUM_TRIES; i++) { |
120 |
if ((ret = kbc_issue_cmd(MOUSE_WRITE_B))) return ret; |
121 |
if ((ret = kbc_issue_arg(cmd))) return ret; |
122 |
if ((ret = mouse_read_byte(&ack))) return ret; |
123 |
|
124 |
if (ack == ACK_OK) return SUCCESS; |
125 |
if (ack == ACK_ERROR) return INVALID_COMMAND; |
126 |
tickdelay(micros_to_ticks(DELAY)); |
127 |
} |
128 |
return TIMEOUT_ERROR;
|
129 |
} |
130 |
|
131 |
int (mouse_read_byte)(uint8_t *byte) {
|
132 |
int ret = 0; |
133 |
uint8_t stat; |
134 |
for(int i = 0; i < KBC_NUM_TRIES; ++i){ |
135 |
if((ret = util_sys_inb(STATUS_REG, &stat))) return ret; |
136 |
if((stat&OUT_BUF_FUL) && (stat&AUX_MOUSE)) {
|
137 |
if(stat & (PARITY_ERROR | TIME_OUT_REC)) return OTHER_ERROR; |
138 |
if((ret = util_sys_inb(OUTPUT_BUF, byte))) return ret; |
139 |
else return SUCCESS; |
140 |
} |
141 |
tickdelay(micros_to_ticks(DELAY)); |
142 |
} |
143 |
return TIMEOUT_ERROR;
|
144 |
} |
145 |
|
146 |
int (mouse_poll_byte)(uint8_t *byte, uint16_t period) {
|
147 |
int ret = 0; |
148 |
uint8_t stat; |
149 |
while(true){ |
150 |
if((ret = util_sys_inb(STATUS_REG, &stat))) return ret; |
151 |
if((stat&OUT_BUF_FUL) && (stat&AUX_MOUSE)) {
|
152 |
if(stat & (PARITY_ERROR | TIME_OUT_REC)) return OTHER_ERROR; |
153 |
if((ret = util_sys_inb(OUTPUT_BUF, byte))) return ret; |
154 |
else return SUCCESS; |
155 |
} |
156 |
tickdelay(micros_to_ticks(DELAY)); |
157 |
} |
158 |
} |
159 |
|
160 |
int16_t (sign_extend_byte)(uint8_t sign_bit, uint8_t byte) { |
161 |
return (int16_t)(((0xFF * sign_bit)<<8) | byte); |
162 |
} |