Project

General

Profile

Statistics
| Revision:

root / proj / libs / kbc / src / mouse.c @ 322

History | View | Annotate | Download (6.12 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
uint8_t packet_mouse_ih[3];
50
int counter_mouse_ih = 0;
51
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

    
55
void (mouse_ih)(void) {
56
    uint8_t status = 0;
57
    got_error_mouse_ih = 0;
58
    if(counter_mouse_ih >= 3) counter_mouse_ih = 0;
59

    
60
    if ((got_error_mouse_ih = util_sys_inb(STATUS_REG, &status))) return;
61

    
62
    if (status & (TIME_OUT_REC | PARITY_ERROR)) {
63
        got_error_mouse_ih = OTHER_ERROR;
64
        return;
65
    }
66
    if (((status & AUX_MOUSE) == 0) || ((status & OUT_BUF_FUL) == 0)) {
67
        got_error_mouse_ih = READ_ERROR;
68
        return;
69
    }
70

    
71
    uint8_t byte = 0;
72

    
73
    if ((got_error_mouse_ih = util_sys_inb(OUTPUT_BUF, &byte))) return;
74

    
75
    /// This does not run if: I was expecting the first one but what I get is definitely not the first byte
76
    if((byte & FIRST_BYTE_ID)  || counter_mouse_ih){
77
        packet_mouse_ih[counter_mouse_ih++] = byte;
78
    }
79
}
80

    
81
void (mouse_parse_packet)(const uint8_t *packet_bytes, struct packet *pp){
82
    pp->bytes[0] = packet_bytes[0];
83
    pp->bytes[1] = packet_bytes[1];
84
    pp->bytes[2] = packet_bytes[2];
85
    pp->rb       = pp->bytes[0] & RIGHT_BUTTON;
86
    pp->mb       = pp->bytes[0] & MIDDLE_BUTTON;
87
    pp->lb       = pp->bytes[0] & LEFT_BUTTON;
88
    pp->delta_x  = sign_extend_byte((packet_bytes[0] & MSB_X_DELTA) != 0, pp->bytes[1]);
89
    pp->delta_y  = sign_extend_byte((packet_bytes[0] & MSB_Y_DELTA) != 0, pp->bytes[2]);
90
    pp->x_ov     = pp->bytes[0] & X_OVERFLOW;
91
    pp->y_ov     = pp->bytes[0] & Y_OVERFLOW;
92
}
93

    
94
int mouse_poll(struct packet *pp, uint16_t period){
95
    int ret = 0;
96

    
97
    uint8_t packet[3];
98
    uint8_t byte;
99
    if ((ret = mouse_issue_cmd(READ_DATA))) return ret;
100
    for(unsigned i = 0; i < 3; ++i){
101
        if((ret = mouse_poll_byte(&byte, period))) return ret;
102
        packet[i] = byte;
103
    }
104
    mouse_parse_packet(packet, pp);
105
    return SUCCESS;
106
}
107

    
108
int (mouse_set_data_report)(int on){
109
    if(on) return mouse_issue_cmd(ENABLE_DATA_REP);
110
    else   return mouse_issue_cmd(   DIS_DATA_REP);
111
}
112

    
113
int (mouse_read_data)(uint8_t *data, uint16_t period) {
114
    int ret;
115
    if ((ret = mouse_issue_cmd(READ_DATA))) return ret;
116
    if ((ret = mouse_poll_byte(data, period))) return ret;
117
    return SUCCESS;
118
}
119

    
120
int (mouse_issue_cmd)(uint32_t cmd) {
121
    int ret;
122
    uint8_t ack = 0;
123
    for (unsigned int i = 0; i < KBC_NUM_TRIES; i++) {
124
        if ((ret = kbc_issue_cmd(MOUSE_WRITE_B))) return ret;
125
        if ((ret = kbc_issue_arg(cmd))) return ret;
126
        if ((ret = mouse_read_byte(&ack))) return ret;
127

    
128
        if (ack == ACK_OK) return SUCCESS;
129
        if (ack == ACK_ERROR) return INVALID_COMMAND;
130
        tickdelay(micros_to_ticks(DELAY));
131
    }
132
    return TIMEOUT_ERROR;
133
}
134

    
135
int (mouse_read_byte)(uint8_t *byte) {
136
    int ret = 0;
137
    uint8_t stat;
138
    for(int i = 0; i < KBC_NUM_TRIES; ++i){
139
        if((ret = util_sys_inb(STATUS_REG, &stat))) return ret;
140
        if((stat&OUT_BUF_FUL) && (stat&AUX_MOUSE)) {
141
            if(stat & (PARITY_ERROR | TIME_OUT_REC)) return OTHER_ERROR;
142
            if((ret = util_sys_inb(OUTPUT_BUF, byte))) return ret;
143
            else return SUCCESS;
144
        }
145
        tickdelay(micros_to_ticks(DELAY));
146
    }
147
    return TIMEOUT_ERROR;
148
}
149

    
150
int (mouse_poll_byte)(uint8_t *byte, uint16_t period) {
151
    int ret = 0;
152
    uint8_t stat;
153
    while(true){
154
        if((ret = util_sys_inb(STATUS_REG, &stat))) return ret;
155
        if((stat&OUT_BUF_FUL) && (stat&AUX_MOUSE)) {
156
            if(stat & (PARITY_ERROR | TIME_OUT_REC)) return OTHER_ERROR;
157
            if((ret = util_sys_inb(OUTPUT_BUF, byte))) return ret;
158
            else return SUCCESS;
159
        }
160
        tickdelay(micros_to_ticks(DELAY));
161
    }
162
}
163

    
164
int16_t (sign_extend_byte)(uint8_t sign_bit, uint8_t byte) {
165
    return (int16_t)(((0xFF * sign_bit)<<8) | byte);
166
}