Skip to content

Commit b05c4ef

Browse files
Initial Release Firmware
I think we're ready to really get moving. Version 1.0 firmware release.
1 parent 8c88907 commit b05c4ef

File tree

14 files changed

+2433
-516
lines changed

14 files changed

+2433
-516
lines changed

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
A USB to serial mouse adapter!
33
<img align="center" alt="ASSEMBLED_ADAPTER" width="800px" src="https://raw.githubusercontent.com/LimeProgramming/USB-serial-mouse-adapter/main/images/banner_s.webp"/>
44

5-
## Important
6-
I'm writing this readme before the newest firmware version comes out.
7-
Wait until this message goes away before considering this readme valid.
8-
95
## About
106
This project came about when I got my hands on an old 486 based PC. Having never really dipped my toes into retro computing, I didn't have an old serial mouse on hand. A quick eBay searched revealed that the decent ones cost a nice chunk of change that I didn't feel they were worth. Another search on Vogons revealed that some mad lads out there made active PS/2 to serial adapters using various micro controllers.
117
Out of curiosity I examined how these worked and thought "Why can't I do this with a Pi Pico and USB mouse?" which kicked off this project.

binary/usb-2-232.uf2

40.5 KB
Binary file not shown.

firmware/CMakeLists.txt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ set(TARGET "usb-2-232")
55
set(CMAKE_C_STANDARD 11)
66
set(CMAKE_CXX_STANDARD 17)
77

8+
if(NOT CMAKE_BUILD_TYPE)
9+
set(CMAKE_BUILD_TYPE Release)
10+
endif()
11+
12+
#set( CMAKE_CXX_FLAGS "-Wall -Wextra")
13+
#set( CMAKE_C_FLAGS "-Wall -Wextra")
14+
set( CMAKE_C_FLAGS_RELEASE "-O2" )
15+
set( CMAKE_CXX_FLAGS_RELEASE "-O2" )
16+
817
# initalize pico_sdk from installed location
918
# (note this can come from environment, CMake cache etc)
1019
set(PICO_SDK_PATH "C:/Pico/pico-sdk-1.2.0")
@@ -19,7 +28,7 @@ pico_sdk_init()
1928

2029
# Add executable. Default name is the project name, version 0.1
2130

22-
add_executable(usb-2-232 usb-2-232.c include/utils.c)
31+
add_executable(usb-2-232 usb-2-232.c include/utils.c include/serial.c include/hid_app.c)
2332

2433
# Make sure TinyUSB can find tusb_config.h
2534
target_include_directories(usb-2-232 PUBLIC
@@ -30,14 +39,14 @@ link_directories(include/)
3039

3140
# In addition to pico_stdlib required for common PicoSDK functionality, add dependency on tinyusb_host
3241
# for TinyUSB device support and tinyusb_board for the additional board support library used by the example
33-
target_link_libraries(usb-2-232 pico_stdlib tinyusb_host tinyusb_board)
42+
target_link_libraries(usb-2-232 pico_stdlib hardware_flash hardware_sync tinyusb_host tinyusb_board)
3443

3544
pico_set_program_name(${TARGET} "usb-2-232")
3645
pico_set_program_description(${TARGET} "Pico usb to serial mouse adaptor.")
3746
pico_set_program_version(${TARGET} "0.1")
3847

39-
pico_enable_stdio_uart(usb-2-232 1)
4048
pico_enable_stdio_usb(usb-2-232 0)
49+
pico_enable_stdio_uart(usb-2-232 1)
4150

4251
pico_add_extra_outputs(usb-2-232)
4352

firmware/default_config.h

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#include <stdbool.h>
2+
3+
/*---------------------------------------*/
4+
// DEBUG //
5+
/*---------------------------------------*/
6+
7+
// All this really does is give printf's out via UART1
8+
#define DEBUG false // Debug Flag for things
9+
10+
11+
/*---------------------------------------*/
12+
// GPIO PINS //
13+
/*---------------------------------------*/
14+
15+
// DIP Switch Pins
16+
#define DIPSW_THREEBTN 9 // Dip Switch 1 | LOGITECH
17+
#define DIPSW_WHEEL 10 // Dip Switch 2 | MS Wheel
18+
#define DIPSW_75XYSPEED 11 // Dip Switch 3 | 75% speed | Dip 3 + 4 depressed will set mouse speed to 25%
19+
#define DIPSW_50XYSPEED 12 // Dip Switch 4 | 50% speed | Dip 3 + 4 depressed will set mouse speed to 25%
20+
#define DIPSW_7N2 13 // Dip Switch 5 | 7N2 format
21+
#define DIPSW_2400 14 // Dip Switch 6 | Reserved
22+
23+
// LEDS
24+
#define LED_PWR 2
25+
#define LED_ALERT 3
26+
27+
//UART stuff
28+
#define UART_ID uart1 // Use UART1, keep UART0 for DEBUG printing
29+
#define UART_TX_PIN 4 // UART1 TX --> MAX232 pin 11
30+
#define UART_RX_PIN 5 // UART1 RX --> MAX232 pin 12
31+
#define UART_CTS_PIN 6 // CTS --> MAX232 pin 9
32+
#define UART_RTS_PIN 7 // RTS --> MAX232 pin 10
33+
34+
35+
#define RESET_FLASH 17 // Button for resetting the stored settings back to default, defined below.
36+
37+
38+
/*---------------------------------------*/
39+
// Settings Defaults //
40+
/*---------------------------------------*/
41+
// These are used until they are changed via software for dipswitches
42+
43+
// global limit | Range 1 -> 200
44+
static uint8_t default_xytravel_percentage = 100;
45+
46+
// seperate x and y limits. | Range 1 -> 200
47+
static uint8_t default_xtravel_percentage = 100;
48+
static uint8_t default_ytravel_percentage = 100;
49+
50+
// TWOBTN = 0 | THREEBTN = 1 | WHEELBTN = 2
51+
static int8_t default_mousetype = 0;
52+
53+
// Double Stop Bit
54+
// 7N1 = false | 7N2 = true
55+
static bool default_doublestopbit = false;
56+
57+
// Baud Rate
58+
/* ---------- Acceptible baud rates ----------
59+
1200 | 2400 | 4800 | 9600
60+
Recommended value is 1200
61+
62+
Any values outside of the ones above, are rejected
63+
and the default value of 1200 is used.
64+
65+
Note:
66+
As far as I can see from docs, serial mice ran at
67+
1200 Baud, so any value higher then that may result in issues.
68+
Max value is 9600 because of hardware limits of the
69+
max232
70+
71+
------------------------------------------ */
72+
73+
static uint16_t default_baudrate = 1200;
74+
75+
// Swap left and right mouse buttons
76+
// Could be handy for left handed people.
77+
static bool default_swap_left_right = false;
78+
79+
// Uses the forward and back buttons of a mouse as alturnative left and right mouse buttons.
80+
// The forward and back butons are on the side of the mouse and usually act as the page forward and backwards in file and web browsers.
81+
// Forward button -> left click | Back Button -> right click
82+
static bool default_use_forward_backward = false;
83+
84+
// Changes above to
85+
// Forward button -> right click | Back Button -> left click
86+
static bool default_swap_forward_backward = false;
87+
88+
// Invert X axis (Left and Right movement)
89+
static bool default_invert_x = false;
90+
91+
// Invert Y axis (Up and Down movement)
92+
static bool default_invert_y = false;
93+
94+
95+
/*---------------------------------------*/
96+
// Advanced Settings //
97+
/*---------------------------------------*/
98+
99+
100+
/* ---------------------------------------------------------- */
101+
/* Numbers for Serial Speed and Delay times
102+
- Thanks to Aviancer for the numbers | https://github.com/Aviancer/amouse
103+
104+
1200 baud (bits/s) is 133.333333333... bytes/s
105+
44.44.. updates per second with 3 bytes.
106+
33.25.. updates per second with 4 bytes.
107+
~0.0075 seconds per byte, target time calculated for 4 bytes.
108+
109+
SERIALDELAY = (((7500 / (BAUD_RATE / 1200)) * NumberOfBytes) + TXWIGGLEROOM )
110+
111+
SerialDelay is the time between when serial packets are sent, based on the math above */
112+
/* ---------------------------------------------------------- */
113+
114+
// This is for adding a bit of padding to the serial delays, shouldn't be needed but some controllers are awkward.
115+
static uint32_t TXWIGGLEROOM = 0;

firmware/include/ctypes.h

Lines changed: 140 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,21 @@
33
#ifndef CTYPES_H_
44
#define CTYPES_H_1
55

6+
#include "pico.h"
7+
8+
9+
/* -------------------- Some Basic emums -------------------- */
10+
611
// Mouse tracking speed modifier
712
enum LED_STATE {
813
LED_OFF = 0,
914
LED_ON = 1,
1015
LED_UNKNOWN_DEVICE = 2,
1116
LED_UNKNOWN_DEVICE_1 = 3,
12-
LED_SERIAL_CONNECTED = 4,
13-
LED_SERIAL_CONNECTED_1 = 5,
14-
LED_SERIAL_DISCONNECTED = 6,
15-
LED_SERIAL_DISCONNECTED_1 = 7,
16-
};
17-
18-
// Mouse tracking speed modifier
19-
enum MO_SPEED {
20-
SPEED100 = 0, // 100% speed
21-
SPEED75 = 1, // 75% speed
22-
SPEED50 = 2, // 50% speed
23-
SPEED25 = 3 // 50% speed
17+
LED_SERIAL_CONNECTED = 4,
18+
LED_SERIAL_CONNECTED_1 = 5,
19+
LED_SERIAL_DISCONNECTED = 6,
20+
LED_SERIAL_DISCONNECTED_1 = 7,
2421
};
2522

2623
// Mouse Type enum
@@ -37,30 +34,155 @@ enum PC_INIT_STATES {
3734
CTS_TOGGLED = 2 // CTS was low, now high -> do ident.
3835
};
3936

40-
typedef struct { // Mouse report information
37+
/* -------------------- Mouse Packet Structure -------------------- */
38+
/* This is the hold the mouse state for the previous cycle.
39+
Each cycle is defined by the Baud rate. */
40+
41+
typedef struct {
42+
// State of left, middle and right buttons
4143
bool left, middle, right;
44+
45+
// mouse location delta
4246
int16_t x, y, wheel;
47+
48+
// Is update required this cycle?
4349
bool update;
50+
4451
} MOUSE_PKT;
4552

46-
typedef struct { // Mouse Settings and information
47-
uint8_t speed; // Mouse Tracking Speed
48-
uint8_t type; // Mouse Type
49-
uint8_t pc_state; // CTS state tracker | taken from Aviancer's code since it was more straightforward than what I had already
50-
MOUSE_PKT mpkt; // Current Mouse Packet
53+
/* -------------------- Raw Mouse Packet Structure -------------------- */
54+
/* This is the hold the mouse state for the previous cycle.
55+
Each cycle is defined by the Baud rate. */
56+
typedef struct {
57+
58+
// The Button Flip Flop | Left Click + Middle Click + Right Click
59+
bool btnFlipFlop[3];
60+
61+
// FlipFlop Cycle Update Flag | Left Click + Middle Click + Right Click
62+
bool btnUpdated[3];
63+
64+
// FlipFlop toggle flag | Left Click + Middle Click + Right Click
65+
bool btnToggle[3];
66+
67+
// Mouse location delta | X + Y + Wheel
68+
int16_t x, y, wheel;
69+
70+
// Is update required this cycle?
71+
//bool update;
72+
73+
} MOUSE_RPKT;
74+
75+
/* -------------------- Persistent Data Structure -------------------- */
76+
/* This is to hold the mouse data that is to survive hard reboots */
77+
78+
typedef struct {
79+
// 255 = first time execution | false = ran before.
80+
uint8_t firstrun;
81+
82+
// global limit | Range 1 -> 200
83+
uint8_t xytravel_percentage;
84+
85+
// seperate x and y limits. | Range 1 -> 200
86+
uint8_t xtravel_percentage;
87+
uint8_t ytravel_percentage;
88+
89+
// TWOBTN = 0 | THREEBTN = 1 | WHEELBTN = 2
90+
uint8_t mousetype;
91+
92+
// Double Stop Bit
93+
// 7N1 = 0 | 7N2 = 1
94+
bool doublestopbit;
95+
96+
// Baud rate
97+
// 1200 | 2400 | 4800 | 9600
98+
uint16_t baudrate;
99+
100+
// Swap the left and right buttons
101+
bool swap_left_right;
102+
103+
// use forward and backward as ALT left and right buttons
104+
bool use_forward_backward;
105+
106+
// Swap forward and backwards
107+
bool swap_forward_backward;
108+
109+
// Invert X and Y movement
110+
bool invert_x;
111+
bool invert_y;
112+
113+
// Firmware Versioning
114+
uint8_t FW_V_MAJOR;
115+
uint8_t FW_V_MINOR;
116+
uint8_t FW_V_REVISION;
117+
118+
// Previous DipSwitch Button State
119+
bool ST_DIPSW_THREEBTN; // DIP 1
120+
bool ST_DIPSW_WHEEL; // DIP 2
121+
bool ST_DIPSW_75XYSPEED; // DIP 3
122+
bool ST_DIPSW_50XYSPEED; // DIP 4
123+
bool ST_DIPSW_7N2; // DIP 5
124+
bool ST_DIPSW_2400; // DIP 6
125+
126+
} PERSISTENT_MOUSE_DATA;
127+
128+
extern PERSISTENT_MOUSE_DATA pmData;
129+
130+
/* -------------------- Mouse Settings -------------------- */
131+
// Mouse Settings and information
132+
133+
typedef struct {
134+
135+
// CTS state tracker | taken from Aviancer's code since it was more straightforward than what I had already
136+
uint8_t pc_state;
137+
138+
// serial state tracker. 0 = Mouse mode | 1 = terminal mode.
139+
uint8_t serial_state;
140+
141+
// Current Processed Mouse Packet.
142+
MOUSE_PKT mpkt;
143+
144+
// Raw Mouse data.
145+
MOUSE_RPKT rmpkt;
146+
147+
// Persisten Mouse data, survives reboots.
148+
PERSISTENT_MOUSE_DATA persistent;
149+
150+
// One Byte Delay Time, calculated on startup.
151+
int32_t serialdelay_1B;
152+
153+
// Three Byte Delay Time, calculated on startup.
154+
int32_t serialdelay_3B;
155+
156+
// Four Byte Delay Time, calculated on startup.
157+
int32_t serialdelay_4B;
158+
159+
// M3Z | Ident info serial mouse.
160+
uint8_t intro_pkts[3];
161+
162+
// Is mouse connected flag
163+
bool mouse_conn;
164+
51165
} MOUSE_DATA;
52166

167+
// Extern value, declared again in usb-2-232.c, can be used everywhere ctypes is included.
168+
extern MOUSE_DATA mouse_data;
169+
170+
171+
/* -------------------- Make the file work stuff -------------------- */
172+
53173
#if CTYPES_C_
54174

55175
enum MO_SPEED MO_SPEED;
56176
enum MO_TYPE MO_TYPE;
57177
enum PC_INIT_STATES PC_INIT_STATES;
178+
//PersistentData persistentData;
58179

59180
#else
60181

61182
extern enum MO_SPEED MO_SPEED;
62183
extern enum MO_TYPE MO_TYPE;
63184
extern enum PC_INIT_STATES PC_INIT_STATES;
185+
//extern PersistentData persistentData;
64186

65187
#endif
66188

0 commit comments

Comments
 (0)