diff options
Diffstat (limited to 'drivers/sensors/pmw3360.c')
-rw-r--r-- | drivers/sensors/pmw3360.c | 237 |
1 files changed, 237 insertions, 0 deletions
diff --git a/drivers/sensors/pmw3360.c b/drivers/sensors/pmw3360.c new file mode 100644 index 000000000..91ee87b95 --- /dev/null +++ b/drivers/sensors/pmw3360.c | |||
@@ -0,0 +1,237 @@ | |||
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 | |||
25 | bool _inBurst = false; | ||
26 | |||
27 | #ifndef PMW_CPI | ||
28 | # define PMW_CPI 1600 | ||
29 | #endif | ||
30 | #ifndef PMW_CLOCK_SPEED | ||
31 | # define PMW_CLOCK_SPEED 70000000 | ||
32 | #endif | ||
33 | #ifndef SPI_MODE | ||
34 | # define SPI_MODE 3 | ||
35 | #endif | ||
36 | #ifndef SPI_DIVISOR | ||
37 | # define SPI_DIVISOR (F_CPU / PMW_CLOCK_SPEED) | ||
38 | #endif | ||
39 | #ifndef ROTATIONAL_TRANSFORM_ANGLE | ||
40 | # define ROTATIONAL_TRANSFORM_ANGLE 0x00 | ||
41 | #endif | ||
42 | #ifndef PMW_CS_PIN | ||
43 | # define PMW_CS_PIN SPI_SS_PIN | ||
44 | #endif | ||
45 | |||
46 | void 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')); } | ||
47 | |||
48 | bool spi_start_adv(void) { | ||
49 | bool status = spi_start(PMW_CS_PIN, false, SPI_MODE, SPI_DIVISOR); | ||
50 | wait_us(1); | ||
51 | return status; | ||
52 | } | ||
53 | |||
54 | void spi_stop_adv(void) { | ||
55 | wait_us(1); | ||
56 | spi_stop(); | ||
57 | } | ||
58 | |||
59 | spi_status_t spi_write_adv(uint8_t reg_addr, uint8_t data) { | ||
60 | if (reg_addr != REG_Motion_Burst) { | ||
61 | _inBurst = false; | ||
62 | } | ||
63 | |||
64 | spi_start_adv(); | ||
65 | // send address of the register, with MSBit = 1 to indicate it's a write | ||
66 | spi_status_t status = spi_write(reg_addr | 0x80); | ||
67 | status = spi_write(data); | ||
68 | |||
69 | // tSCLK-NCS for write operation | ||
70 | wait_us(20); | ||
71 | |||
72 | // tSWW/tSWR (=120us) minus tSCLK-NCS. Could be shortened, but is looks like a safe lower bound | ||
73 | wait_us(100); | ||
74 | spi_stop(); | ||
75 | return status; | ||
76 | } | ||
77 | |||
78 | uint8_t spi_read_adv(uint8_t reg_addr) { | ||
79 | spi_start_adv(); | ||
80 | // send adress of the register, with MSBit = 0 to indicate it's a read | ||
81 | spi_write(reg_addr & 0x7f); | ||
82 | |||
83 | uint8_t data = spi_read(); | ||
84 | |||
85 | // tSCLK-NCS for read operation is 120ns | ||
86 | wait_us(1); | ||
87 | |||
88 | // tSRW/tSRR (=20us) minus tSCLK-NCS | ||
89 | wait_us(19); | ||
90 | |||
91 | spi_stop(); | ||
92 | return data; | ||
93 | } | ||
94 | |||
95 | void pmw_set_cpi(uint16_t cpi) { | ||
96 | uint8_t cpival = constrain((cpi / 100) - 1, 0, 0x77); // limits to 0--119 | ||
97 | |||
98 | spi_start_adv(); | ||
99 | spi_write_adv(REG_Config1, cpival); | ||
100 | spi_stop(); | ||
101 | } | ||
102 | |||
103 | uint16_t pmw_get_cpi(void) { | ||
104 | uint8_t cpival = spi_read_adv(REG_Config1); | ||
105 | return (uint16_t)(cpival & 0xFF) * 100; | ||
106 | } | ||
107 | |||
108 | bool pmw_spi_init(void) { | ||
109 | setPinOutput(PMW_CS_PIN); | ||
110 | |||
111 | spi_init(); | ||
112 | _inBurst = false; | ||
113 | |||
114 | spi_stop(); | ||
115 | spi_start_adv(); | ||
116 | spi_stop(); | ||
117 | |||
118 | spi_write_adv(REG_Shutdown, 0xb6); // Shutdown first | ||
119 | wait_ms(300); | ||
120 | |||
121 | spi_start_adv(); | ||
122 | wait_us(40); | ||
123 | spi_stop_adv(); | ||
124 | wait_us(40); | ||
125 | |||
126 | spi_write_adv(REG_Power_Up_Reset, 0x5a); | ||
127 | wait_ms(50); | ||
128 | |||
129 | spi_read_adv(REG_Motion); | ||
130 | spi_read_adv(REG_Delta_X_L); | ||
131 | spi_read_adv(REG_Delta_X_H); | ||
132 | spi_read_adv(REG_Delta_Y_L); | ||
133 | spi_read_adv(REG_Delta_Y_H); | ||
134 | |||
135 | pmw_upload_firmware(); | ||
136 | |||
137 | spi_stop_adv(); | ||
138 | |||
139 | wait_ms(10); | ||
140 | pmw_set_cpi(PMW_CPI); | ||
141 | |||
142 | wait_ms(1); | ||
143 | |||
144 | spi_write_adv(REG_Config2, 0x00); | ||
145 | |||
146 | spi_write_adv(REG_Angle_Tune, constrain(ROTATIONAL_TRANSFORM_ANGLE, -30, 30)); | ||
147 | |||
148 | bool init_success = pmw_check_signature(); | ||
149 | |||
150 | writePinLow(PMW_CS_PIN); | ||
151 | |||
152 | return init_success; | ||
153 | } | ||
154 | |||
155 | void pmw_upload_firmware(void) { | ||
156 | spi_write_adv(REG_SROM_Enable, 0x1d); | ||
157 | |||
158 | wait_ms(10); | ||
159 | |||
160 | spi_write_adv(REG_SROM_Enable, 0x18); | ||
161 | |||
162 | spi_start_adv(); | ||
163 | spi_write(REG_SROM_Load_Burst | 0x80); | ||
164 | wait_us(15); | ||
165 | |||
166 | unsigned char c; | ||
167 | for (int i = 0; i < firmware_length; i++) { | ||
168 | c = (unsigned char)pgm_read_byte(firmware_data + i); | ||
169 | spi_write(c); | ||
170 | wait_us(15); | ||
171 | } | ||
172 | wait_us(200); | ||
173 | |||
174 | spi_read_adv(REG_SROM_ID); | ||
175 | |||
176 | spi_write_adv(REG_Config2, 0x00); | ||
177 | |||
178 | spi_stop(); | ||
179 | wait_ms(10); | ||
180 | } | ||
181 | |||
182 | bool pmw_check_signature(void) { | ||
183 | uint8_t pid = spi_read_adv(REG_Product_ID); | ||
184 | uint8_t iv_pid = spi_read_adv(REG_Inverse_Product_ID); | ||
185 | uint8_t SROM_ver = spi_read_adv(REG_SROM_ID); | ||
186 | return (pid == 0x42 && iv_pid == 0xBD && SROM_ver == 0x04); // signature for SROM 0x04 | ||
187 | } | ||
188 | |||
189 | report_pmw_t pmw_read_burst(void) { | ||
190 | if (!_inBurst) { | ||
191 | dprintf("burst on"); | ||
192 | spi_write_adv(REG_Motion_Burst, 0x00); | ||
193 | _inBurst = true; | ||
194 | } | ||
195 | |||
196 | spi_start_adv(); | ||
197 | spi_write(REG_Motion_Burst); | ||
198 | wait_us(35); // waits for tSRAD | ||
199 | |||
200 | report_pmw_t data; | ||
201 | data.motion = 0; | ||
202 | data.dx = 0; | ||
203 | data.mdx = 0; | ||
204 | data.dy = 0; | ||
205 | data.mdx = 0; | ||
206 | |||
207 | data.motion = spi_read(); | ||
208 | spi_write(0x00); // skip Observation | ||
209 | data.dx = spi_read(); | ||
210 | data.mdx = spi_read(); | ||
211 | data.dy = spi_read(); | ||
212 | data.mdy = spi_read(); | ||
213 | |||
214 | spi_stop(); | ||
215 | |||
216 | print_byte(data.motion); | ||
217 | print_byte(data.dx); | ||
218 | print_byte(data.mdx); | ||
219 | print_byte(data.dy); | ||
220 | print_byte(data.mdy); | ||
221 | dprintf("\n"); | ||
222 | |||
223 | data.isMotion = (data.motion & 0x80) != 0; | ||
224 | data.isOnSurface = (data.motion & 0x08) == 0; | ||
225 | data.dx |= (data.mdx << 8); | ||
226 | data.dx = data.dx * -1; | ||
227 | data.dy |= (data.mdy << 8); | ||
228 | data.dy = data.dy * -1; | ||
229 | |||
230 | spi_stop(); | ||
231 | |||
232 | if (data.motion & 0b111) { // panic recovery, sometimes burst mode works weird. | ||
233 | _inBurst = false; | ||
234 | } | ||
235 | |||
236 | return data; | ||
237 | } | ||