Project

General

Profile

Statistics
| Revision:

root / proj / src / mouse.c @ 257

History | View | Annotate | Download (5.91 KB)

1
#include <lcom/lcf.h>
2

    
3
#include "mouse.h"
4

    
5
#include "errors.h"
6
#include "kbc_macros.h"
7
#include "kbc.h"
8

    
9
/* Mouse Data Packet */
10
// Byte 0 - Button States
11
#define LEFT_BUTTON     BIT(0) /* @brief Left button click event*/
12
#define RIGHT_BUTTON    BIT(1) /* @brief Right button click event */
13
#define MIDDLE_BUTTON   BIT(2) /* @brief Middle button click event */
14
#define FIRST_BYTE_ID   BIT(3) /* @brief Identifier of first byte of packet CAREFUL: Not 100% accurate */
15
#define MSB_X_DELTA     BIT(4) /* @brief Most significant bit of X delta */
16
#define MSB_Y_DELTA     BIT(5) /* @brief Most significant bit of Y delta */
17
#define X_OVERFLOW      BIT(6) /* @brief X delta overflowed */
18
#define Y_OVERFLOW      BIT(7) /* @brief Y delta overflowed */
19
// Byte 1 - X delta
20
// Byte 2 - Y delta
21

    
22
/* Mouse Commands */
23
#define RESET           0xFF /* @brief Reset mouse */
24
#define RESEND          0xFE /* @brief Resend command */
25
#define DEFAULT         0xF6 /* @brief Set default values */
26
#define DIS_DATA_REP    0xF5 /* @brief Disable Data Reporting */
27
#define ENABLE_DATA_REP 0xF4 /* @brief Enable Data Reporting */
28
#define SET_SAMPLE_RT   0xF3 /* @brief Sets state sampling rate */
29
#define SET_REMOTE_MD   0xF0 /* @brief Sets Mouse on Remote Mode, data on request */
30
#define READ_DATA       0xEB /* @brief Sends data packet request */
31
#define SET_STREAM_MD   0xEA /* @brief Sets mouse on Stream Mode, data on events */
32
#define STATUS_REQUEST  0xE9 /* @brief Get mouse configuration */
33
#define SET_RESOLUTION  0xE8 /* @brief Sets resolution for mouse movement */
34
#define SCALING_ACC_MD  0xE7 /* @brief Sets scaling on acceleration mode */
35
#define SCALING_LIN_MD  0xE6 /* @brief Sets scaling on linear mode */
36

    
37
/* Mouse Controller Responses */
38
#define ACK_OK      0xFA /* @brief Operation sucessful */
39
#define ACK_INVALID 0xFE /* @brief Invalid Byte, first occurence */
40
#define ACK_ERROR   0xFC /* @brief Invalid Byte on resend */
41

    
42
int (subscribe_mouse_interrupt)(uint8_t interrupt_bit, int *interrupt_id) {
43
    if (interrupt_id == NULL) return NULL_PTR;
44
    *interrupt_id = interrupt_bit;
45
    if (sys_irqsetpolicy(MOUSE_IRQ, IRQ_REENABLE | IRQ_EXCLUSIVE, interrupt_id)) return SBCR_ERROR;
46
    return SUCCESS;
47
}
48

    
49
int got_error_mouse_ih = 0;
50
int counter_mouse_ih = 0;
51

    
52
void (mouse_ih)(void) {
53
    uint8_t status = 0;
54
    got_error_mouse_ih = 0;
55
    if(counter_mouse_ih >= 3) counter_mouse_ih = 0;
56

    
57
    if ((got_error_mouse_ih = util_sys_inb(STATUS_REG, &status))) return;
58

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

    
68
    uint8_t byte = 0;
69

    
70
    if ((got_error_mouse_ih = util_sys_inb(OUTPUT_BUF, &byte))) return;
71

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

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

    
91
int mouse_poll(struct packet *pp, uint16_t period){
92
    int ret = 0;
93

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

    
105
int (mouse_set_data_report)(int on){
106
    if(on) return mouse_issue_cmd(ENABLE_DATA_REP);
107
    else   return mouse_issue_cmd(   DIS_DATA_REP);
108
}
109

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

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

    
125
        if (ack == ACK_OK) return SUCCESS;
126
        if (ack == ACK_ERROR) return INVALID_COMMAND;
127
        tickdelay(micros_to_ticks(DELAY));
128
    }
129
    return TIMEOUT_ERROR;
130
}
131

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

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

    
161
int16_t (sign_extend_byte)(uint8_t sign_bit, uint8_t byte) {
162
    return (int16_t)(((0xFF * sign_bit)<<8) | byte);
163
}