-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path3360_Mouse_pico.ino
513 lines (402 loc) · 12.4 KB
/
3360_Mouse_pico.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
* Copyright (c) 2024, wareya
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <SPI.h>
#include <mbed.h>
REDIRECT_STDOUT_TO(Serial);
#include <PluggableUSBHID.h>
#include "srom_3360_0x05.h"
#include "relmouse_16.h"
USBMouse16 mouse(false);
#define REG_PRODUCT_ID (0x00)
#define REG_REVISION_ID (0x01)
#define REG_MOTION (0x02)
#define REG_DELTA_X_L (0x03)
#define REG_DELTA_X_H (0x04)
#define REG_DELTA_Y_L (0x05)
#define REG_DELTA_Y_H (0x06)
#define REG_SQUAL (0x07)
#define REG_RAW_DATA_SUM (0x08)
#define REG_MAX_RAW_DATA (0x09)
#define REG_MIN_RAW_DATA (0x0A)
#define REG_SHUTTER_LOWER (0x0B)
#define REG_SHUTTER_UPPER (0x0C)
#define REG_CONTROL (0x0D)
#define REG_CONFIG1 (0x0F)
#define REG_CONFIG2 (0x10)
#define REG_ANGLE_TUNE (0x11)
#define REG_FRAME_CAPTURE (0x12)
#define REG_SROM_ENABLE (0x13)
#define REG_RUN_DOWNSHIFT (0x14)
#define REG_REST1_RATE_LOWER (0x15)
#define REG_REST1_RATE_UPPER (0x16)
#define REG_REST1_DOWNSHIFT (0x17)
#define REG_REST2_RATE_LOWER (0x18)
#define REG_REST2_RATE_UPPER (0x19)
#define REG_REST2_DOWNSHIFT (0x1A)
#define REG_REST3_RATE_LOWER (0x1B)
#define REG_REST3_RATE_UPPER (0x1C)
#define REG_OBSERVATION (0x24)
#define REG_DATA_OUT_LOWER (0x25)
#define REG_DATA_OUT_UPPER (0x26)
#define REG_RAW_DATA_DUMP (0x29)
#define REG_SROM_ID (0x2A)
#define REG_MIN_SQ_RUN (0x2B)
#define REG_RAW_DATA_THRESHOLD (0x2C)
#define REG_CONFIG5 (0x2F)
#define REG_POWER_UP_RESET (0x3A)
#define REG_SHUTDOWN (0x3B)
#define REG_INVERSE_PRODUCT_ID (0x3F)
#define REG_LIFTCUTOFF_TUNE3 (0x41)
#define REG_ANGLE_SNAP (0x42)
#define REG_LIFTCUTOFF_TUNE1 (0x4A)
#define REG_MOTION_BURST (0x50)
#define REG_LIFTCUTOFF_TUNE_TIMEOUT (0x58)
#define REG_LIFTCUTOFF_TUNE_MIN_LENGTH (0x5A)
#define REG_SROM_LOAD_BURST (0x62)
#define REG_LIFT_CONFIG (0x63)
#define REG_RAW_DATA_BURST (0x64)
#define REG_LIFTOFF_TUNE2 (0x65)
#define CONFIG2_REST_ENABLED (0x30)
#define CONFIG2_REPORT_MODE (0x04)
#define BUTTONS_ON 0
#define BUTTONS_OFF 1
#define BUTTON_M1 2
#define BUTTON_M2 3
#define BUTTON_M3 4
#define BUTTON_M4 6
#define BUTTON_M5 7
#define BUTTON_DPI 5
#define ENCODER_A 8
#define ENCODER_B 10
#define ENCODER_COM 9
#define PIN_NCS 17
#define PIN_MOSI 19
#define PIN_MISO 16
SPISettings spisettings(2000000, MSBFIRST, SPI_MODE3);
MbedSPI spi(PIN_MISO, PIN_MOSI, 18);
void setup_buttons()
{
pinMode(BUTTONS_OFF, INPUT);
pinMode(BUTTONS_ON, INPUT);
pinMode(BUTTON_M1, INPUT_PULLUP);
pinMode(BUTTON_M2, INPUT_PULLUP);
pinMode(BUTTON_M3, INPUT_PULLUP);
pinMode(BUTTON_M4, INPUT_PULLUP);
pinMode(BUTTON_M5, INPUT_PULLUP);
pinMode(BUTTON_DPI, INPUT_PULLUP);
pinMode(ENCODER_A, INPUT_PULLUP);
pinMode(ENCODER_B, INPUT_PULLUP);
pinMode(ENCODER_COM, OUTPUT);
digitalWrite(ENCODER_COM, LOW);
}
bool mouse_inited;
void setup()
{
Serial1.begin(1000000);
delay(3000);
// SCK, MISO, MOSI, SS
SPI.begin();
pinMode(PIN_NCS, OUTPUT);
digitalWrite(PIN_NCS, HIGH);
delayMicroseconds(1);
digitalWrite(PIN_NCS, LOW);
delayMicroseconds(1);
digitalWrite(PIN_NCS, HIGH);
delayMicroseconds(1);
pmw3360_boot();
delay(10);
pmw3360_config();
setup_buttons();
}
uint32_t pins_state = 0;
uint8_t buttons = 0;
uint8_t which_m3 = 0;
// 8ms latch time
uint8_t buttons_latch_max = 8;
uint8_t buttons_latch[5] = {0, 0, 0, 0, 0};
volatile uint32_t * pad_control = (uint32_t *)0x4001c000;
volatile uint32_t * gpio_oe_set = (uint32_t *)0xd0000024;
volatile uint32_t * gpio_oe_clr = (uint32_t *)0xd0000028;
volatile uint32_t * gpio_in = (uint32_t *)0xd0000004;
void update_buttons()
{
/*
// disable pullup/pulldown
pad_control[BUTTONS_OFF + 1] &= 0xFFFFFFF2;
pad_control[BUTTONS_ON + 1] &= 0xFFFFFFF2;
*/
// change BUTTONS_OFF to high impedance and BUTTONS_ON to output
// using pinMode for this is way, WAY too slow, like 50us per call (?!?!?!)
*gpio_oe_clr = 1 << BUTTONS_OFF;
*gpio_oe_set = 1 << BUTTONS_ON;
// read ON pins
digitalWrite(BUTTONS_ON, LOW);
delayMicroseconds(1);
uint32_t pins_on = ~*gpio_in;
// change BUTTONS_OFF to high impedance and BUTTONS_ON to output
*gpio_oe_set = 1 << BUTTONS_OFF;
*gpio_oe_clr = 1 << BUTTONS_ON;
// read OFF pins
digitalWrite(BUTTONS_OFF, LOW);
delayMicroseconds(1);
uint32_t pins_off = ~*gpio_in;
// update pin state
// treat middle state (on-off matching) as no-update (use previous state)
uint32_t pins_update = pins_on ^ pins_off;
pins_state = (pins_state & (~pins_update)) | (pins_on & pins_update);
// update button state
uint8_t next_buttons =
((!!(pins_state & (1 << BUTTON_M1))))
| ((!!(pins_state & (1 << BUTTON_M2))) << 1)
| ((!!(pins_state & (1 << BUTTON_M3))) << 2)
| ((!!(pins_state & (1 << BUTTON_DPI))) << 3)
| ((!!(pins_state & (1 << BUTTON_M4))) << 4)
| ((!!(pins_state & (1 << BUTTON_M5))) << 5);
// handle latch
uint8_t ok_mask =
((buttons_latch[0] == 0))
| ((buttons_latch[1] == 0) << 1)
| ((buttons_latch[2] == 0) << 2)
| ((buttons_latch[3] == 0) << 3)
| ((buttons_latch[4] == 0) << 4)
| ((buttons_latch[5] == 0) << 5);
//printf("%u, %u, %u, %u\n", ~pins_on, ~pins_off, pins_state, ok_mask);
next_buttons = (next_buttons & ok_mask) | (buttons & ~ok_mask);
// update latch timings
for (int i = 0; i < 6; i++)
{
if (((next_buttons ^ buttons) >> i) & 1)
buttons_latch[i] = buttons_latch_max;
else if (buttons_latch[i])
buttons_latch[i] -= 1;
}
if ((next_buttons & (1 << 2)) != (buttons & (1 << 2)))
which_m3 = 0;
if ((next_buttons & (1 << 3)) != (buttons & (1 << 3)))
which_m3 = 1;
buttons = next_buttons;
}
uint8_t wheel_state_a = 0;
uint8_t wheel_state_b = 0;
uint8_t wheel_state_output = 0;
int8_t wheel_progress = 0;
void update_wheel()
{
uint8_t wheel_new_a = digitalRead(ENCODER_A);
uint8_t wheel_new_b = digitalRead(ENCODER_B);
if (wheel_new_a != wheel_state_a || wheel_new_b != wheel_state_b)
{
if (wheel_new_a == wheel_new_b && wheel_state_output != wheel_new_a)
{
// when scrolling up, B changes first. when scrolling down, A changes first
if (wheel_new_b == wheel_state_b)
wheel_progress += 1;
// the wheel can glitch and jump straight from 00 to 11 (or vice versa)
// so we need to check that only one state has changed since the last test
else if(wheel_new_a == wheel_state_a)
wheel_progress -= 1;
wheel_state_output = wheel_new_a;
}
wheel_state_a = wheel_new_a;
wheel_state_b = wheel_new_b;
}
}
int n = 0;
int usb_hid_poll_interval = 1;
struct MotionBurstData {
uint8_t motion;
uint8_t observation;
int16_t x;
int16_t y;
uint8_t squal;
uint8_t raw_sum;
uint8_t raw_max;
uint8_t raw_min;
uint16_t shutter;
};
MotionBurstData spi_read_motion_burst(bool do_update_wheel);
int dpi = 12; // in hundreds; 1200 dpi
int lod = 2;
void check_config_inputs()
{
if ((buttons & 1) &&
(buttons & 2) &&
(buttons & 8) &&
(buttons & 16))
{
int olddpi = dpi;
if (wheel_progress < 0)
dpi += dpi < 16 ? 1 : dpi < 32 ? 2 : dpi < 64 ? 4 : 8;
else if (wheel_progress > 0)
dpi -= dpi <= 16 ? 1 : dpi <= 32 ? 2 : dpi <= 64 ? 4 : 8;
if (dpi > 120)
dpi = 120;
else if (dpi < 1)
dpi = 1;
if (olddpi != dpi)
{
spi_write(REG_CONFIG1, dpi - 1);
printf("new DPI: %d\n", dpi * 100);
}
wheel_progress = 0;
}
}
void loop()
{
// these execute nearly instantly
// we want to call update_wheel roughly every 250ms to avoid skipping
update_wheel();
delayMicroseconds(250);
update_wheel();
int16_t x = 0;
int16_t y = 0;
// takes ~400us to execute; wheel is updated again around 250ms into the call
MotionBurstData data = spi_read_motion_burst(true);
if (mouse_inited && data.motion)
{
x = data.x;
y = data.y;
}
update_wheel();
//uint32_t a = micros();
update_buttons();
//uint32_t b = micros();
//Serial.println(b - a);
check_config_inputs();
int8_t wheel = wheel_progress;
wheel_progress = 0;
//uint32_t a = micros();
// this will return around 1ms after the last time it returned
// which should be around 250us from now
uint8_t new_buttons = buttons;
if (which_m3)
new_buttons = (new_buttons & 3) | ((new_buttons >> 1) & ~3);
else
new_buttons = (new_buttons & 7) | ((new_buttons >> 1) & ~7);
mouse.update(x, y, new_buttons, wheel);
//uint32_t b = micros();
//Serial.println(b - a);
}
void pmw3360_boot()
{
spi_write(REG_POWER_UP_RESET, 0x5A);
delay(50);
spi_read(0x02);
spi_read(0x03);
spi_read(0x04);
spi_read(0x05);
spi_read(0x06);
srom_upload();
}
void pmw3360_config()
{
spi_write(REG_CONFIG1, dpi - 1);
}
void spi_begin()
{
SPI.beginTransaction(spisettings);
}
void spi_end()
{
SPI.endTransaction();
}
void spi_write(byte addr, byte data)
{
spi_begin();
digitalWrite(PIN_NCS, LOW);
delayMicroseconds(1); // 120 nanoseconds; t_NCS-SCLK
SPI.transfer(addr | 0x80);
SPI.transfer(data);
delayMicroseconds(35); // t_SCLK-NCS(write)
digitalWrite(PIN_NCS, HIGH);
spi_end();
delayMicroseconds(180); // max(t_SWW, t_SWR)
}
byte spi_read(byte addr)
{
spi_begin();
digitalWrite(PIN_NCS, LOW);
delayMicroseconds(1); // 120 nanoseconds; t_NCS-SCLK
SPI.transfer(addr & 0x7F);
delayMicroseconds(160); // t_SRAD
byte ret = SPI.transfer(0);
delayMicroseconds(1); // 120 nanoseconds; t_SCLK-NCS(read)
digitalWrite(PIN_NCS, HIGH);
spi_end();
delayMicroseconds(20); // max(t_SRW, t_SRR)
return ret;
}
MotionBurstData spi_read_motion_burst(bool do_update_wheel)
{
MotionBurstData ret = {0};
// this takes around 260us to execute; update scroll wheel again after calling it
spi_write(REG_MOTION_BURST, 0x00);
if (do_update_wheel)
update_wheel();
spi_begin();
digitalWrite(PIN_NCS, LOW);
delayMicroseconds(1); // 120 nanoseconds; t_NCS-SCLK
SPI.transfer(REG_MOTION_BURST);
delayMicroseconds(35); // t_SRAD_MOTBR
ret.motion = SPI.transfer(0);
ret.observation = SPI.transfer(0);
ret.x = SPI.transfer(0);
ret.x |= ((uint16_t)SPI.transfer(0)) << 8;
ret.y = SPI.transfer(0);
ret.y |= ((uint16_t)SPI.transfer(0)) << 8;
ret.squal = SPI.transfer(0);
// don't care about the rest; terminate
digitalWrite(PIN_NCS, HIGH);
delayMicroseconds(1); // t_BEXIT = 500ns; wait 1000ns (1us)
/*
ret.raw_sum = SPI.transfer(0);
ret.raw_max = SPI.transfer(0);
ret.raw_min = SPI.transfer(0);
ret.shutter = SPI.transfer(0);
ret.shutter |= ((uint16_t)SPI.transfer(0)) << 8;
*/
spi_end();
return ret;
}
void srom_upload()
{
spi_write(REG_CONFIG2, 0x00);
spi_write(REG_SROM_ENABLE, 0x1D);
delay(10);
spi_write(REG_SROM_ENABLE, 0x18);
spi_begin();
digitalWrite(PIN_NCS, LOW);
delayMicroseconds(1);
SPI.transfer(REG_SROM_LOAD_BURST | 0x80);
delayMicroseconds(15);
for(size_t i = 0; i < SROM_LENGTH; i += 1)
{
SPI.transfer(srom[i]);
delayMicroseconds(15);
}
digitalWrite(PIN_NCS, HIGH);
delayMicroseconds(1);
spi_end();
delayMicroseconds(200);
byte id = spi_read(REG_SROM_ID);
mouse_inited = (id != 0xFF);
printf("\n");
printf("srom id: ");
printf("%d", id);
printf("\n");
spi_write(REG_CONFIG2, 0x00);
}