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
|
// Copyright 2022 Pablo Martinez (@elpekenin)
// Copyright 2022 Daniel Kao (dkao)
// Copyright 2022 Stefan Kerkmann (KarlK90)
// Copyright 2022 Ulrich Spörlein (@uqs)
// Copyright 2021 Alabastard (@Alabastard-64)
// Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <[email protected]>
// Copyright 2019 Sunjun Kim
// Copyright 2020 Ploopy Corporation
// SPDX-License-Identifier: GPL-2.0-or-later
#include "pointing_device_internal.h"
#include "pmw33xx_common.h"
#include "string.h"
#include "wait.h"
#include "spi_master.h"
#include "progmem.h"
extern const uint8_t pmw33xx_firmware_signature[2] PROGMEM;
static const pin_t cs_pins_left[] = PMW33XX_CS_PINS;
static const pin_t cs_pins_right[] = PMW33XX_CS_PINS_RIGHT;
static bool in_burst_left[ARRAY_SIZE(cs_pins_left)] = {0};
static bool in_burst_right[ARRAY_SIZE(cs_pins_right)] = {0};
bool __attribute__((cold)) pmw33xx_upload_firmware(uint8_t sensor);
bool __attribute__((cold)) pmw33xx_check_signature(uint8_t sensor);
const pointing_device_driver_t pmw33xx_pointing_device_driver = {
.init = pmw33xx_init_wrapper,
.get_report = pmw33xx_get_report,
.set_cpi = pmw33xx_set_cpi_wrapper,
.get_cpi = pmw33xx_get_cpi_wrapper,
};
uint16_t __attribute__((weak)) pmw33xx_srom_get_length(void) {
return 0;
}
uint8_t __attribute__((weak)) pmw33xx_srom_get_byte(uint16_t position) {
return 0;
}
void pmw33xx_set_cpi_all_sensors(uint16_t cpi) {
for (uint8_t sensor = 0; sensor < pmw33xx_number_of_sensors; sensor++) {
pmw33xx_set_cpi(sensor, cpi);
}
}
bool pmw33xx_spi_start(uint8_t sensor) {
if (!spi_start(cs_pins[sensor], false, 3, PMW33XX_SPI_DIVISOR)) {
spi_stop();
return false;
}
// tNCS-SCLK, 10ns
wait_us(1);
return true;
}
bool pmw33xx_write(uint8_t sensor, uint8_t reg_addr, uint8_t data) {
if (!pmw33xx_spi_start(sensor)) {
return false;
}
if (reg_addr != REG_Motion_Burst) {
in_burst[sensor] = false;
}
// send address of the register, with MSBit = 1 to indicate it's a write
uint8_t command[2] = {reg_addr | 0x80, data};
if (spi_transmit(command, sizeof(command)) != SPI_STATUS_SUCCESS) {
return false;
}
// tSCLK-NCS for write operation is 35us
wait_us(35);
spi_stop();
// tSWW/tSWR (=18us) minus tSCLK-NCS. Could be shortened, but it looks like
// a safe lower bound
wait_us(145);
return true;
}
uint8_t pmw33xx_read(uint8_t sensor, uint8_t reg_addr) {
if (!pmw33xx_spi_start(sensor)) {
return 0;
}
// send adress of the register, with MSBit = 0 to indicate it's a read
spi_write(reg_addr & 0x7f);
// tSRAD (=160us)
wait_us(160);
uint8_t data = spi_read();
// tSCLK-NCS, 120ns
wait_us(1);
spi_stop();
// tSRW/tSRR (=20us) mins tSCLK-NCS
wait_us(19);
return data;
}
__attribute__((weak)) bool pmw33xx_check_signature(uint8_t sensor) {
uint8_t signature_dump[2] = {
pmw33xx_read(sensor, REG_Product_ID),
pmw33xx_read(sensor, REG_Inverse_Product_ID),
};
return memcmp(pmw33xx_firmware_signature, signature_dump, sizeof(signature_dump)) == 0;
}
bool pmw33xx_upload_firmware(uint8_t sensor) {
// Datasheet claims we need to disable REST mode first, but during startup
// it's already disabled and we're not turning it on ...
// pmw33xx_write(REG_Config2, 0x00); // disable REST mode
if (!pmw33xx_write(sensor, REG_SROM_Enable, 0x1d)) {
return false;
}
wait_ms(10);
pmw33xx_write(sensor, REG_SROM_Enable, 0x18);
if (!pmw33xx_spi_start(sensor)) {
return false;
}
spi_write(REG_SROM_Load_Burst | 0x80);
wait_us(15);
for (size_t i = 0; i < pmw33xx_srom_get_length(); i++) {
spi_write(pmw33xx_srom_get_byte(i));
wait_us(15);
}
spi_stop();
wait_us(200);
pmw33xx_read(sensor, REG_SROM_ID);
pmw33xx_write(sensor, REG_Config2, 0x00);
return true;
}
bool pmw33xx_init(uint8_t sensor) {
if (sensor >= pmw33xx_number_of_sensors) {
return false;
}
spi_init();
// power up, need to first drive NCS high then low. the datasheet does not
// say for how long, 40us works well in practice.
if (!pmw33xx_spi_start(sensor)) {
return false;
}
wait_us(40);
spi_stop();
wait_us(40);
if (!pmw33xx_write(sensor, REG_Power_Up_Reset, 0x5a)) {
return false;
}
wait_ms(50);
// read registers and discard
pmw33xx_read(sensor, REG_Motion);
pmw33xx_read(sensor, REG_Delta_X_L);
pmw33xx_read(sensor, REG_Delta_X_H);
pmw33xx_read(sensor, REG_Delta_Y_L);
pmw33xx_read(sensor, REG_Delta_Y_H);
if (pmw33xx_srom_get_length() != 0) {
if (!pmw33xx_upload_firmware(sensor)) {
pd_dprintf("PMW33XX (%d): firmware upload failed!\n", sensor);
return false;
}
} else {
pd_dprintf("PMW33XX (%d): firmware upload skipped.\n", sensor);
}
spi_stop();
wait_ms(10);
pmw33xx_set_cpi(sensor, PMW33XX_CPI);
wait_ms(1);
pmw33xx_write(sensor, REG_Config2, 0x00);
pmw33xx_write(sensor, REG_Angle_Tune, CONSTRAIN(ROTATIONAL_TRANSFORM_ANGLE, -127, 127));
pmw33xx_write(sensor, REG_Lift_Config, PMW33XX_LIFTOFF_DISTANCE);
if (!pmw33xx_check_signature(sensor)) {
pd_dprintf("PMW33XX (%d): firmware signature verification failed!\n", sensor);
return false;
}
return true;
}
pmw33xx_report_t pmw33xx_read_burst(uint8_t sensor) {
pmw33xx_report_t report = {0};
if (sensor >= pmw33xx_number_of_sensors) {
return report;
}
if (!in_burst[sensor]) {
pd_dprintf("PMW33XX (%d): burst\n", sensor);
if (!pmw33xx_write(sensor, REG_Motion_Burst, 0x00)) {
return report;
}
in_burst[sensor] = true;
}
if (!pmw33xx_spi_start(sensor)) {
return report;
}
spi_write(REG_Motion_Burst);
wait_us(35); // waits for tSRAD_MOTBR
spi_receive((uint8_t *)&report, sizeof(report));
// panic recovery, sometimes burst mode works weird.
if (report.motion.w & 0b111) {
in_burst[sensor] = false;
}
spi_stop();
pd_dprintf("PMW33XX (%d): motion: 0x%x dx: %i dy: %i\n", sensor, report.motion.w, report.delta_x, report.delta_y);
report.delta_x *= -1;
report.delta_y *= -1;
return report;
}
bool pmw33xx_init_wrapper(void) {
return pmw33xx_init(0);
}
void pmw33xx_set_cpi_wrapper(uint16_t cpi) {
pmw33xx_set_cpi(0, cpi);
}
uint16_t pmw33xx_get_cpi_wrapper(void) {
return pmw33xx_get_cpi(0);
}
report_mouse_t pmw33xx_get_report(report_mouse_t mouse_report) {
pmw33xx_report_t report = pmw33xx_read_burst(0);
static bool in_motion = false;
if (report.motion.b.is_lifted) {
return mouse_report;
}
if (!report.motion.b.is_motion) {
in_motion = false;
return mouse_report;
}
if (!in_motion) {
in_motion = true;
pd_dprintf("PWM3360 (0): starting motion\n");
}
mouse_report.x = CONSTRAIN_HID_XY(report.delta_x);
mouse_report.y = CONSTRAIN_HID_XY(report.delta_y);
return mouse_report;
}
|