aboutsummaryrefslogtreecommitdiff
path: root/drivers/sensors/adns5050.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/sensors/adns5050.c')
-rw-r--r--drivers/sensors/adns5050.c193
1 files changed, 193 insertions, 0 deletions
diff --git a/drivers/sensors/adns5050.c b/drivers/sensors/adns5050.c
new file mode 100644
index 000000000..e7273977d
--- /dev/null
+++ b/drivers/sensors/adns5050.c
@@ -0,0 +1,193 @@
1/* Copyright 2021 Colin Lam (Ploopy Corporation)
2 * Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
3 * Copyright 2019 Sunjun Kim
4 * Copyright 2019 Hiroyuki Okada
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20
21#include "adns5050.h"
22#include "wait.h"
23#include "debug.h"
24#include "print.h"
25#include "gpio.h"
26
27#ifndef OPTIC_ROTATED
28# define OPTIC_ROTATED false
29#endif
30
31// Definitions for the ADNS serial line.
32#ifndef ADNS_SCLK_PIN
33# define ADNS_SCLK_PIN B7
34#endif
35
36#ifndef ADNS_SDIO_PIN
37# define ADNS_SDIO_PIN C6
38#endif
39
40#ifndef ADNS_CS_PIN
41# define ADNS_CS_PIN B4
42#endif
43
44#ifdef CONSOLE_ENABLE
45void 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')); }
46#endif
47
48// Initialize the ADNS serial pins.
49void adns_init(void) {
50 setPinOutput(ADNS_SCLK_PIN);
51 setPinOutput(ADNS_SDIO_PIN);
52 setPinOutput(ADNS_CS_PIN);
53}
54
55// Perform a synchronization with the ADNS.
56// Just as with the serial protocol, this is used by the slave to send a
57// synchronization signal to the master.
58void adns_sync(void) {
59 writePinLow(ADNS_CS_PIN);
60 wait_us(1);
61 writePinHigh(ADNS_CS_PIN);
62}
63
64void adns_cs_select(void) {
65 writePinLow(ADNS_CS_PIN);
66}
67
68void adns_cs_deselect(void) {
69 writePinHigh(ADNS_CS_PIN);
70}
71
72uint8_t adns_serial_read(void) {
73 setPinInput(ADNS_SDIO_PIN);
74 uint8_t byte = 0;
75
76 for (uint8_t i = 0; i < 8; ++i) {
77 writePinLow(ADNS_SCLK_PIN);
78 wait_us(1);
79
80 byte = (byte << 1) | readPin(ADNS_SDIO_PIN);
81
82 writePinHigh(ADNS_SCLK_PIN);
83 wait_us(1);
84 }
85
86 return byte;
87}
88
89void adns_serial_write(uint8_t data) {
90 setPinOutput(ADNS_SDIO_PIN);
91
92 for (int8_t b = 7; b >= 0; b--) {
93 writePinLow(ADNS_SCLK_PIN);
94
95 if (data & (1 << b))
96 writePinHigh(ADNS_SDIO_PIN);
97 else
98 writePinLow(ADNS_SDIO_PIN);
99
100 wait_us(2);
101
102 writePinHigh(ADNS_SCLK_PIN);
103 }
104
105 // tSWR. See page 15 of the ADNS spec sheet.
106 // Technically, this is only necessary if the next operation is an SDIO
107 // read. This is not guaranteed to be the case, but we're being lazy.
108 wait_us(4);
109
110 // Note that tSWW is never necessary. All write operations require at
111 // least 32us, which exceeds tSWW, so there's never a need to wait for it.
112}
113
114// Read a byte of data from a register on the ADNS.
115// Don't forget to use the register map (as defined in the header file).
116uint8_t adns_read_reg(uint8_t reg_addr) {
117 adns_cs_select();
118
119 adns_serial_write(reg_addr);
120
121 // We don't need a minimum tSRAD here. That's because a 4ms wait time is
122 // already included in adns_serial_write(), so we're good.
123 // See page 10 and 15 of the ADNS spec sheet.
124 //wait_us(4);
125
126 uint8_t byte = adns_serial_read();
127
128 // tSRW & tSRR. See page 15 of the ADNS spec sheet.
129 // Technically, this is only necessary if the next operation is an SDIO
130 // read or write. This is not guaranteed to be the case.
131 // Honestly, this wait could probably be removed.
132 wait_us(1);
133
134 adns_cs_deselect();
135
136 return byte;
137}
138
139void adns_write_reg(uint8_t reg_addr, uint8_t data) {
140 adns_cs_select();
141 adns_serial_write( 0b10000000 | reg_addr );
142 adns_serial_write(data);
143 adns_cs_deselect();
144}
145
146report_adns_t adns_read_burst(void) {
147 adns_cs_select();
148
149 report_adns_t data;
150 data.dx = 0;
151 data.dy = 0;
152
153 adns_serial_write(REG_MOTION_BURST);
154
155 // We don't need a minimum tSRAD here. That's because a 4ms wait time is
156 // already included in adns_serial_write(), so we're good.
157 // See page 10 and 15 of the ADNS spec sheet.
158 //wait_us(4);
159
160 uint8_t x = adns_serial_read();
161 uint8_t y = adns_serial_read();
162
163 // Burst mode returns a bunch of other shit that we don't really need.
164 // Setting CS to high ends burst mode early.
165 adns_cs_deselect();
166
167 data.dx = convert_twoscomp(x);
168 data.dy = convert_twoscomp(y);
169
170 return data;
171}
172
173// Convert a two's complement byte from an unsigned data type into a signed
174// data type.
175int8_t convert_twoscomp(uint8_t data) {
176 if ((data & 0x80) == 0x80)
177 return -128 + (data & 0x7F);
178 else
179 return data;
180}
181
182// Don't forget to use the definitions for CPI in the header file.
183void adns_set_cpi(uint8_t cpi) {
184 adns_write_reg(REG_MOUSE_CONTROL2, cpi);
185}
186
187bool adns_check_signature(void) {
188 uint8_t pid = adns_read_reg(REG_PRODUCT_ID);
189 uint8_t rid = adns_read_reg(REG_REVISION_ID);
190 uint8_t pid2 = adns_read_reg(REG_PRODUCT_ID2);
191
192 return (pid == 0x12 && rid == 0x01 && pid2 == 0x26);
193}