aboutsummaryrefslogtreecommitdiff
path: root/keyboards/ploopyco/pmw3360.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/ploopyco/pmw3360.c')
-rw-r--r--keyboards/ploopyco/pmw3360.c218
1 files changed, 0 insertions, 218 deletions
diff --git a/keyboards/ploopyco/pmw3360.c b/keyboards/ploopyco/pmw3360.c
deleted file mode 100644
index 5f9f72a9e..000000000
--- a/keyboards/ploopyco/pmw3360.c
+++ /dev/null
@@ -1,218 +0,0 @@
1/* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
2 * Copyright 2019 Sunjun Kim
3 * Copyright 2020 Ploopy Corporation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "wait.h"
20#include "debug.h"
21#include "print.h"
22#include "pmw3360.h"
23#include "pmw3360_firmware.h"
24
25bool _inBurst = false;
26
27#ifndef PMW_CPI
28# define PMW_CPI 1600
29#endif
30#ifndef SPI_DIVISOR
31# define SPI_DIVISOR 2
32#endif
33#ifndef ROTATIONAL_TRANSFORM_ANGLE
34# define ROTATIONAL_TRANSFORM_ANGLE 0x00
35#endif
36
37void print_byte(uint8_t byte) { dprintf("%c%c%c%c%c%c%c%c|", (byte & 0x80 ? '1' : '0'), (byte & 0x40 ? '1' : '0'), (byte & 0x20 ? '1' : '0'), (byte & 0x10 ? '1' : '0'), (byte & 0x08 ? '1' : '0'), (byte & 0x04 ? '1' : '0'), (byte & 0x02 ? '1' : '0'), (byte & 0x01 ? '1' : '0')); }
38
39
40bool spi_start_adv(void) {
41 bool status = spi_start(SPI_SS_PIN, false, 3, SPI_DIVISOR);
42 wait_us(1);
43 return status;
44}
45
46void spi_stop_adv(void) {
47 wait_us(1);
48 spi_stop();
49}
50
51spi_status_t spi_write_adv(uint8_t reg_addr, uint8_t data) {
52 if (reg_addr != REG_Motion_Burst) {
53 _inBurst = false;
54 }
55
56 spi_start_adv();
57 // send address of the register, with MSBit = 1 to indicate it's a write
58 spi_status_t status = spi_write(reg_addr | 0x80);
59 status = spi_write(data);
60
61 // tSCLK-NCS for write operation
62 wait_us(20);
63
64 // tSWW/tSWR (=120us) minus tSCLK-NCS. Could be shortened, but is looks like a safe lower bound
65 wait_us(100);
66 spi_stop();
67 return status;
68}
69
70uint8_t spi_read_adv(uint8_t reg_addr) {
71 spi_start_adv();
72 // send adress of the register, with MSBit = 0 to indicate it's a read
73 spi_write(reg_addr & 0x7f);
74
75 uint8_t data = spi_read();
76
77 // tSCLK-NCS for read operation is 120ns
78 wait_us(1);
79
80 // tSRW/tSRR (=20us) minus tSCLK-NCS
81 wait_us(19);
82
83 spi_stop();
84 return data;
85}
86
87void pmw_set_cpi(uint16_t cpi) {
88 int cpival = constrain((cpi / 100) - 1, 0, 0x77); // limits to 0--119
89
90 spi_start_adv();
91 spi_write_adv(REG_Config1, cpival);
92 spi_stop();
93}
94
95bool pmw_spi_init(void) {
96 spi_init();
97 _inBurst = false;
98
99 spi_stop();
100 spi_start_adv();
101 spi_stop();
102
103 spi_write_adv(REG_Shutdown, 0xb6); // Shutdown first
104 wait_ms(300);
105
106 spi_start_adv();
107 wait_us(40);
108 spi_stop_adv();
109 wait_us(40);
110
111 spi_write_adv(REG_Power_Up_Reset, 0x5a);
112 wait_ms(50);
113
114 spi_read_adv(REG_Motion);
115 spi_read_adv(REG_Delta_X_L);
116 spi_read_adv(REG_Delta_X_H);
117 spi_read_adv(REG_Delta_Y_L);
118 spi_read_adv(REG_Delta_Y_H);
119
120 pmw_upload_firmware();
121
122 spi_stop_adv();
123
124 wait_ms(10);
125 pmw_set_cpi(PMW_CPI);
126
127 wait_ms(1);
128
129 return pmw_check_signature();
130}
131
132void pmw_upload_firmware(void) {
133 spi_write_adv(REG_Config2, 0x00);
134
135 spi_write_adv(REG_Angle_Tune, constrain(ROTATIONAL_TRANSFORM_ANGLE, -30, 30));
136
137 spi_write_adv(REG_SROM_Enable, 0x1d);
138
139 wait_ms(10);
140
141 spi_write_adv(REG_SROM_Enable, 0x18);
142
143 spi_start_adv();
144 spi_write(REG_SROM_Load_Burst | 0x80);
145 wait_us(15);
146
147 unsigned char c;
148 for (int i = 0; i < firmware_length; i++) {
149 c = (unsigned char)pgm_read_byte(firmware_data + i);
150 spi_write(c);
151 wait_us(15);
152 }
153 wait_us(200);
154
155 spi_read_adv(REG_SROM_ID);
156
157 spi_write_adv(REG_Config2, 0x00);
158
159 spi_stop();
160 wait_ms(10);
161}
162
163bool pmw_check_signature(void) {
164 uint8_t pid = spi_read_adv(REG_Product_ID);
165 uint8_t iv_pid = spi_read_adv(REG_Inverse_Product_ID);
166 uint8_t SROM_ver = spi_read_adv(REG_SROM_ID);
167 return (pid == 0x42 && iv_pid == 0xBD && SROM_ver == 0x04); // signature for SROM 0x04
168}
169
170report_pmw_t pmw_read_burst(void) {
171 if (!_inBurst) {
172 dprintf("burst on");
173 spi_write_adv(REG_Motion_Burst, 0x00);
174 _inBurst = true;
175 }
176
177 spi_start_adv();
178 spi_write(REG_Motion_Burst);
179 wait_us(35); // waits for tSRAD
180
181 report_pmw_t data;
182 data.motion = 0;
183 data.dx = 0;
184 data.mdx = 0;
185 data.dy = 0;
186 data.mdx = 0;
187
188 data.motion = spi_read();
189 spi_write(0x00); // skip Observation
190 data.dx = spi_read();
191 data.mdx = spi_read();
192 data.dy = spi_read();
193 data.mdy = spi_read();
194
195 spi_stop();
196
197 print_byte(data.motion);
198 print_byte(data.dx);
199 print_byte(data.mdx);
200 print_byte(data.dy);
201 print_byte(data.mdy);
202 dprintf("\n");
203
204 data.isMotion = (data.motion & 0x80) != 0;
205 data.isOnSurface = (data.motion & 0x08) == 0;
206 data.dx |= (data.mdx << 8);
207 data.dx = data.dx * -1;
208 data.dy |= (data.mdy << 8);
209 data.dy = data.dy * -1;
210
211 spi_stop();
212
213 if (data.motion & 0b111) { // panic recovery, sometimes burst mode works weird.
214 _inBurst = false;
215 }
216
217 return data;
218}