From 6bf8000e6322da88f91f7eb53f510c2ba72743e5 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sun, 27 Jul 2014 16:23:23 +0200 Subject: [PATCH 1/6] added serial mouse driver for Microsoft and Mousesystems mice * Both drivers use the interface defined in serial_mouse.h * They should work with any UART implementation (hardware/software UART) * The Microsoft driver is currently untested. The Mousesystems driver is confirmed to work. --- protocol/serial_mouse.h | 26 +++++ protocol/serial_mouse_microsoft.c | 125 ++++++++++++++++++++++++ protocol/serial_mouse_mousesystems.c | 140 +++++++++++++++++++++++++++ 3 files changed, 291 insertions(+) create mode 100644 protocol/serial_mouse.h create mode 100644 protocol/serial_mouse_microsoft.c create mode 100644 protocol/serial_mouse_mousesystems.c diff --git a/protocol/serial_mouse.h b/protocol/serial_mouse.h new file mode 100644 index 0000000..c3c19d7 --- /dev/null +++ b/protocol/serial_mouse.h @@ -0,0 +1,26 @@ +/* +Copyright 2011 Jun Wako + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#ifndef SERIAL_MOUSE_H +#define SERIAL_MOUSE_H + +#include + +uint8_t serial_mouse_init(void); +void serial_mouse_task(void); + +#endif diff --git a/protocol/serial_mouse_microsoft.c b/protocol/serial_mouse_microsoft.c new file mode 100644 index 0000000..6b3f806 --- /dev/null +++ b/protocol/serial_mouse_microsoft.c @@ -0,0 +1,125 @@ +/* +Copyright 2011,2013 Jun Wako + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#include +#include +#include + +#include "serial.h" +#include "serial_mouse.h" +#include "report.h" +#include "host.h" +#include "timer.h" +#include "print.h" +#include "debug.h" + +static void print_usb_data(const report_mouse_t *report); + +uint8_t serial_mouse_init(void) +{ + serial_init(); + return 0; +} + +void serial_mouse_task(void) +{ + /* 3 byte ring buffer */ + static uint8_t buffer[3]; + static int buffer_cur = 0; + + static report_mouse_t report = {}; + + int16_t rcv; + + rcv = serial_recv2(); + if (rcv < 0) + /* no new data */ + return; + + if (debug_mouse) + xprintf("serial_mouse: byte: %04X\n", rcv); + + /* + * If bit 6 is one, this signals the beginning + * of a 3 byte sequence/packet. + */ + if (rcv & (1 << 6)) + buffer_cur = 0; + + buffer[buffer_cur] = (uint8_t)rcv; + + if (buffer_cur == 0 && buffer[buffer_cur] == 0x20) { + /* + * Logitech extension: This must be a follow-up on + * the last 3-byte packet signaling a middle button click + */ + report.buttons |= MOUSE_BTN3; + report.x = report.y = 0; + + print_usb_data(&report); + host_mouse_send(&report); + return; + } + + buffer_cur++; + + if (buffer_cur < 3) + return; + buffer_cur = 0; + + /* + * parse 3 byte packet. + * NOTE: We only get a complete packet + * if the mouse moved or the button states + * change. + */ + report.buttons = 0; + if (buffer[0] & (1 << 5)) + report.buttons |= MOUSE_BTN1; + if (buffer[0] & (1 << 4)) + report.buttons |= MOUSE_BTN2; + + report.x = (buffer[0] << 6) | buffer[1]; + report.y = ((buffer[0] << 4) & 0xC0) | buffer[2]; + + /* USB HID uses values from -127 to 127 only */ + report.x = report.x < -127 ? -127 : report.x; + report.y = report.y < -127 ? -127 : report.y; + +#if 0 + if (!report.buttons && !report.x && !report.y) { + /* + * Microsoft extension: Middle mouse button pressed + * FIXME: I don't know how exactly this extension works. + */ + report.buttons |= MOUSE_BTN3; + } +#endif + + print_usb_data(&report); + host_mouse_send(&report); +} + +static void print_usb_data(const report_mouse_t *report) +{ + if (!debug_mouse) + return; + + xprintf("serial_mouse usb: [%02X|%d %d %d %d]\n", + report->buttons, report->x, report->y, + report->v, report->h); +} diff --git a/protocol/serial_mouse_mousesystems.c b/protocol/serial_mouse_mousesystems.c new file mode 100644 index 0000000..ec708c9 --- /dev/null +++ b/protocol/serial_mouse_mousesystems.c @@ -0,0 +1,140 @@ +/* +Copyright 2011,2013 Jun Wako + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#include +#include +#include + +#include "serial.h" +#include "serial_mouse.h" +#include "report.h" +#include "host.h" +#include "timer.h" +#include "print.h" +#include "debug.h" + +#define SERIAL_MOUSE_CENTER_SCROLL + +static void print_usb_data(const report_mouse_t *report); + +uint8_t serial_mouse_init(void) +{ + serial_init(); + return 0; +} + +void serial_mouse_task(void) +{ + /* 5 byte ring buffer */ + static uint8_t buffer[5]; + static int buffer_cur = 0; + + int16_t rcv; + + report_mouse_t report = {0, 0, 0, 0, 0}; + + rcv = serial_recv2(); + if (rcv < 0) + /* no new data */ + return; + + if (debug_mouse) + xprintf("serial_mouse: byte: %04X\n", rcv); + + /* + * Synchronization: mouse(4) says that all + * bytes but the first one in the packet have + * bit 7 == 0, but this is untrue. + * Therefore we discard all bytes up to the + * first one with the characteristic bit pattern. + */ + if (buffer_cur == 0 && (rcv >> 3) != 0x10) + return; + + buffer[buffer_cur++] = (uint8_t)rcv; + + if (buffer_cur < 5) + return; + buffer_cur = 0; + +#ifdef SERIAL_MOUSE_CENTER_SCROLL + if ((buffer[0] & 0x7) == 0x5 && (buffer[1] || buffer[2])) { + report.h = (int8_t)buffer[1]; + /* USB HID uses only values from -127 to 127 */ + report.h = report.h < -127 ? -127 : report.h; + report.v = (int8_t)buffer[2]; + report.v = report.v < -127 ? -127 : report.v; + + print_usb_data(&report); + host_mouse_send(&report); + + if (buffer[3] || buffer[4]) { + report.h = (int8_t)buffer[3]; + report.h = report.h < -127 ? -127 : report.h; + report.v = (int8_t)buffer[4]; + report.v = report.v < -127 ? -127 : report.v; + + print_usb_data(&report); + host_mouse_send(&report); + } + + return; + } +#endif + + /* + * parse 5 byte packet. + * NOTE: We only get a complete packet + * if the mouse moved or the button states + * change. + */ + if (!(buffer[0] & (1 << 2))) + report.buttons |= MOUSE_BTN1; + if (!(buffer[0] & (1 << 1))) + report.buttons |= MOUSE_BTN3; + if (!(buffer[0] & (1 << 0))) + report.buttons |= MOUSE_BTN2; + + report.x = (int8_t)buffer[1]; + /* USB HID uses only values from -127 to 127 */ + report.x = report.x < -127 ? -127 : report.x; + report.y = -(int8_t)buffer[2]; + report.y = report.y < -127 ? -127 : report.y; + + print_usb_data(&report); + host_mouse_send(&report); + + if (buffer[3] || buffer[4]) { + report.x = (int8_t)buffer[3]; + report.x = report.x < -127 ? -127 : report.x; + report.y = -(int8_t)buffer[4]; + report.y = report.y < -127 ? -127 : report.y; + + print_usb_data(&report); + host_mouse_send(&report); + } +} + +static void print_usb_data(const report_mouse_t *report) +{ + if (!debug_mouse) + return; + + xprintf("serial_mouse usb: [%02X|%d %d %d %d]\n", + report->buttons, report->x, report->y, + report->v, report->h); +} From 388fe60c67dbc4232a979d3de3b3b161a67871e2 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sun, 27 Jul 2014 16:26:38 +0200 Subject: [PATCH 2/6] fixed PS/2 keyboard converter config When using the PS/2 interrupt implementation, the DATA pin should be PD0 as the documentation (README.md) states. --- converter/ps2_usb/config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/converter/ps2_usb/config.h b/converter/ps2_usb/config.h index c9bab1b..889f0c3 100644 --- a/converter/ps2_usb/config.h +++ b/converter/ps2_usb/config.h @@ -69,7 +69,7 @@ along with this program. If not, see . #define PS2_DATA_PORT PORTD #define PS2_DATA_PIN PIND #define PS2_DATA_DDR DDRD -#define PS2_DATA_BIT 2 +#define PS2_DATA_BIT 0 #define PS2_INT_INIT() do { \ EICRA |= ((1< Date: Sun, 27 Jul 2014 17:07:26 +0200 Subject: [PATCH 3/6] integrated serial mouse drivers as a feature into the firmware architecture * can be enabled by defining Makefile macro SERIAL_MOUSE_MICROSOFT_ENABLE or SERIAL_MOUSE_MOUSESYSTEMS_ENABLE. * Serial implementation can be chosen via SERIAL_MOUSE_USE_SOFT and SERIAL_MOUSE_USE_UART macros * UART configuration still has to be done in config.h: I added working clauses for both mouse protocols to ps2_usb's config.h --- converter/ps2_usb/Makefile | 13 ++++++++++ converter/ps2_usb/config.h | 38 ++++++++++++++++++++++++++++ protocol.mk | 20 +++++++++++++++ protocol/lufa/lufa.c | 12 +++++++++ protocol/serial_mouse_mousesystems.c | 2 +- 5 files changed, 84 insertions(+), 1 deletion(-) diff --git a/converter/ps2_usb/Makefile b/converter/ps2_usb/Makefile index 1dd23c1..f20039c 100644 --- a/converter/ps2_usb/Makefile +++ b/converter/ps2_usb/Makefile @@ -91,6 +91,19 @@ PS2_USE_USART = yes # uses hardware USART engine for PS/2 signal receive(recomen #PS2_USE_INT = yes # uses external interrupt for falling edge of PS/2 clock pin #PS2_USE_BUSYWAIT = yes # uses primitive reference code +# Serial Mouse Options +# You can choose a mouse protocol and the implementation of +# the underlying serial connection. +# +#SERIAL_MOUSE_MICROSOFT_ENABLE = yes # Enable support for Microsoft-compatible mice +#SERIAL_MOUSE_MOUSESYSTEMS_ENABLE = yes # Enable support for Mousesystems-compatible mice +#SERIAL_MOUSE_USE_UART = yes # use hardware UART for serial connection +#SERIAL_MOUSE_USE_SOFT = yes # use software serial implementation + +# Optional serial mouse driver features +# Support scrolling while holding the middle mouse button +# (currently only supported for Mousesystems mice): +#OPT_DEFS += -DSERIAL_MOUSE_CENTER_SCROLL # Optimize size but this may cause error "relocation truncated to fit" #EXTRALDFLAGS = -Wl,--relax diff --git a/converter/ps2_usb/config.h b/converter/ps2_usb/config.h index 889f0c3..5b64400 100644 --- a/converter/ps2_usb/config.h +++ b/converter/ps2_usb/config.h @@ -170,4 +170,42 @@ along with this program. If not, see . #endif #endif +#ifdef SERIAL_MOUSE_MICROSOFT + /* + * Serial(USART) configuration (for Microsoft serial mice) + * asynchronous, positive logic, 1200baud, bit order: LSB first + * 1-start bit, 7-data bit, no parity, 1-stop bit + */ + #define SERIAL_UART_BAUD 1200 + #define SERIAL_UART_DATA UDR1 + #define SERIAL_UART_UBRR ((F_CPU/(16UL*SERIAL_UART_BAUD))-1) + #define SERIAL_UART_RXD_VECT USART1_RX_vect + #define SERIAL_UART_TXD_READY (UCSR1A&(1<>8); /* baud rate */ \ + UCSR1B |= (1<>8); /* baud rate */ \ + UCSR1B |= (1<. #include "print.h" #include "debug.h" -#define SERIAL_MOUSE_CENTER_SCROLL +//#define SERIAL_MOUSE_CENTER_SCROLL static void print_usb_data(const report_mouse_t *report); From 0bfba7acc4e05e66c8ab448286fc51bc94d03a57 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sun, 27 Jul 2014 17:18:14 +0200 Subject: [PATCH 4/6] factored out serial_mouse_init() into serial_mouse.h --- protocol/serial_mouse.h | 9 ++++++++- protocol/serial_mouse_microsoft.c | 6 ------ protocol/serial_mouse_mousesystems.c | 6 ------ 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/protocol/serial_mouse.h b/protocol/serial_mouse.h index c3c19d7..2ccd3d9 100644 --- a/protocol/serial_mouse.h +++ b/protocol/serial_mouse.h @@ -20,7 +20,14 @@ along with this program. If not, see . #include -uint8_t serial_mouse_init(void); +#include "serial.h" + +static inline uint8_t serial_mouse_init(void) +{ + serial_init(); + return 0; +} + void serial_mouse_task(void); #endif diff --git a/protocol/serial_mouse_microsoft.c b/protocol/serial_mouse_microsoft.c index 6b3f806..f83036a 100644 --- a/protocol/serial_mouse_microsoft.c +++ b/protocol/serial_mouse_microsoft.c @@ -29,12 +29,6 @@ along with this program. If not, see . static void print_usb_data(const report_mouse_t *report); -uint8_t serial_mouse_init(void) -{ - serial_init(); - return 0; -} - void serial_mouse_task(void) { /* 3 byte ring buffer */ diff --git a/protocol/serial_mouse_mousesystems.c b/protocol/serial_mouse_mousesystems.c index 68b2b5b..36c6738 100644 --- a/protocol/serial_mouse_mousesystems.c +++ b/protocol/serial_mouse_mousesystems.c @@ -31,12 +31,6 @@ along with this program. If not, see . static void print_usb_data(const report_mouse_t *report); -uint8_t serial_mouse_init(void) -{ - serial_init(); - return 0; -} - void serial_mouse_task(void) { /* 5 byte ring buffer */ From eb902844946f0bda7da76cdb1e9aafae4881b63c Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sun, 27 Jul 2014 17:26:44 +0200 Subject: [PATCH 5/6] serial_mouse: simplified clipping of X/Y/V/H changes below -127 using a MAX macro --- protocol/serial_mouse_microsoft.c | 9 +++++++-- protocol/serial_mouse_mousesystems.c | 29 +++++++++++++--------------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/protocol/serial_mouse_microsoft.c b/protocol/serial_mouse_microsoft.c index f83036a..54fedae 100644 --- a/protocol/serial_mouse_microsoft.c +++ b/protocol/serial_mouse_microsoft.c @@ -27,6 +27,11 @@ along with this program. If not, see . #include "print.h" #include "debug.h" +#ifdef MAX +#undef MAX +#endif +#define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) + static void print_usb_data(const report_mouse_t *report); void serial_mouse_task(void) @@ -91,8 +96,8 @@ void serial_mouse_task(void) report.y = ((buffer[0] << 4) & 0xC0) | buffer[2]; /* USB HID uses values from -127 to 127 only */ - report.x = report.x < -127 ? -127 : report.x; - report.y = report.y < -127 ? -127 : report.y; + report.x = MAX(report.x, -127); + report.y = MAX(report.y, -127); #if 0 if (!report.buttons && !report.x && !report.y) { diff --git a/protocol/serial_mouse_mousesystems.c b/protocol/serial_mouse_mousesystems.c index 36c6738..c4ddbb8 100644 --- a/protocol/serial_mouse_mousesystems.c +++ b/protocol/serial_mouse_mousesystems.c @@ -27,6 +27,11 @@ along with this program. If not, see . #include "print.h" #include "debug.h" +#ifdef MAX +#undef MAX +#endif +#define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) + //#define SERIAL_MOUSE_CENTER_SCROLL static void print_usb_data(const report_mouse_t *report); @@ -67,20 +72,16 @@ void serial_mouse_task(void) #ifdef SERIAL_MOUSE_CENTER_SCROLL if ((buffer[0] & 0x7) == 0x5 && (buffer[1] || buffer[2])) { - report.h = (int8_t)buffer[1]; /* USB HID uses only values from -127 to 127 */ - report.h = report.h < -127 ? -127 : report.h; - report.v = (int8_t)buffer[2]; - report.v = report.v < -127 ? -127 : report.v; + report.h = MAX((int8_t)buffer[1], -127); + report.v = MAX((int8_t)buffer[2], -127); print_usb_data(&report); host_mouse_send(&report); if (buffer[3] || buffer[4]) { - report.h = (int8_t)buffer[3]; - report.h = report.h < -127 ? -127 : report.h; - report.v = (int8_t)buffer[4]; - report.v = report.v < -127 ? -127 : report.v; + report.h = MAX((int8_t)buffer[3], -127); + report.v = MAX((int8_t)buffer[4], -127); print_usb_data(&report); host_mouse_send(&report); @@ -103,20 +104,16 @@ void serial_mouse_task(void) if (!(buffer[0] & (1 << 0))) report.buttons |= MOUSE_BTN2; - report.x = (int8_t)buffer[1]; /* USB HID uses only values from -127 to 127 */ - report.x = report.x < -127 ? -127 : report.x; - report.y = -(int8_t)buffer[2]; - report.y = report.y < -127 ? -127 : report.y; + report.x = MAX((int8_t)buffer[1], -127); + report.y = MAX(-(int8_t)buffer[2], -127); print_usb_data(&report); host_mouse_send(&report); if (buffer[3] || buffer[4]) { - report.x = (int8_t)buffer[3]; - report.x = report.x < -127 ? -127 : report.x; - report.y = -(int8_t)buffer[4]; - report.y = report.y < -127 ? -127 : report.y; + report.x = MAX((int8_t)buffer[3], -127); + report.y = MAX(-(int8_t)buffer[4], -127); print_usb_data(&report); host_mouse_send(&report); From 03fd4a6ff0fcccf8d7683b7fac3d9e6ae4fb635e Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sun, 27 Jul 2014 17:58:41 +0200 Subject: [PATCH 6/6] updated copyrights: serial_mouse.h, serial_mouse_microsoft.c and serial_mouse_mousesystems.c are new --- protocol/serial_mouse.h | 2 +- protocol/serial_mouse_microsoft.c | 2 +- protocol/serial_mouse_mousesystems.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/protocol/serial_mouse.h b/protocol/serial_mouse.h index 2ccd3d9..226314f 100644 --- a/protocol/serial_mouse.h +++ b/protocol/serial_mouse.h @@ -1,5 +1,5 @@ /* -Copyright 2011 Jun Wako +Copyright 2014 Robin Haberkorn This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/protocol/serial_mouse_microsoft.c b/protocol/serial_mouse_microsoft.c index 54fedae..ab74b7c 100644 --- a/protocol/serial_mouse_microsoft.c +++ b/protocol/serial_mouse_microsoft.c @@ -1,5 +1,5 @@ /* -Copyright 2011,2013 Jun Wako +Copyright 2014 Robin Haberkorn This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/protocol/serial_mouse_mousesystems.c b/protocol/serial_mouse_mousesystems.c index c4ddbb8..cfe8996 100644 --- a/protocol/serial_mouse_mousesystems.c +++ b/protocol/serial_mouse_mousesystems.c @@ -1,5 +1,5 @@ /* -Copyright 2011,2013 Jun Wako +Copyright 2014 Robin Haberkorn This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by