Interface to access to Avago ADNS-9500 laser mouse sensors.
Fork of ADNS9500 by
adns9500.cpp@10:bbf9ff378632, 2012-07-11 (annotated)
- Committer:
- aplatanado
- Date:
- Wed Jul 11 15:56:05 2012 +0000
- Revision:
- 10:bbf9ff378632
- Parent:
- 9:8b1e889e94fe
- Child:
- 11:07a39997a421
spi_frequency was not setted in constructor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aplatanado |
0:782f2061a8f5 | 1 | /* |
aplatanado |
1:fa3052be61b5 | 2 | * adns9500.hpp - Interface to access to Avago ADNS-9500 laser mouse sensors |
aplatanado |
0:782f2061a8f5 | 3 | * |
aplatanado |
0:782f2061a8f5 | 4 | * Copyright 2012 Jesus Torres <jmtorres@ull.es> |
aplatanado |
0:782f2061a8f5 | 5 | * |
aplatanado |
0:782f2061a8f5 | 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
aplatanado |
0:782f2061a8f5 | 7 | * you may not use this file except in compliance with the License. |
aplatanado |
0:782f2061a8f5 | 8 | * You may obtain a copy of the License at |
aplatanado |
0:782f2061a8f5 | 9 | * |
aplatanado |
0:782f2061a8f5 | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
aplatanado |
0:782f2061a8f5 | 11 | * |
aplatanado |
0:782f2061a8f5 | 12 | * Unless required by applicable law or agreed to in writing, software |
aplatanado |
0:782f2061a8f5 | 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
aplatanado |
0:782f2061a8f5 | 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
aplatanado |
0:782f2061a8f5 | 15 | * See the License for the specific language governing permissions and |
aplatanado |
0:782f2061a8f5 | 16 | * limitations under the License. |
aplatanado |
0:782f2061a8f5 | 17 | */ |
aplatanado |
0:782f2061a8f5 | 18 | |
aplatanado | 2:ee0c13ef1320 | 19 | #include <cstdlib> |
aplatanado |
0:782f2061a8f5 | 20 | #include <fstream> |
aplatanado |
0:782f2061a8f5 | 21 | #include <math.h> |
aplatanado |
0:782f2061a8f5 | 22 | #include <mbed.h> |
aplatanado |
0:782f2061a8f5 | 23 | #include <string> |
aplatanado |
0:782f2061a8f5 | 24 | |
aplatanado |
1:fa3052be61b5 | 25 | #include <adns9500.hpp> |
aplatanado |
0:782f2061a8f5 | 26 | |
aplatanado |
0:782f2061a8f5 | 27 | #define WAIT_TSRAD() wait_us(100) |
aplatanado | 2:ee0c13ef1320 | 28 | #define WAIT_TSRR() wait_us(20) |
aplatanado | 2:ee0c13ef1320 | 29 | #define WAIT_TSRW() wait_us(20) |
aplatanado | 2:ee0c13ef1320 | 30 | #define WAIT_TSWR() wait_us(120) |
aplatanado | 2:ee0c13ef1320 | 31 | #define WAIT_TSWW() wait_us(120) |
aplatanado |
0:782f2061a8f5 | 32 | #define WAIT_TBEXIT() wait_us(1) // 500ns |
aplatanado |
0:782f2061a8f5 | 33 | #define WAIT_TNCSSCLK() wait_us(1) // 120ns |
aplatanado |
0:782f2061a8f5 | 34 | #define WAIT_TSCLKNCS() wait_us(20) |
aplatanado |
0:782f2061a8f5 | 35 | #define WAIT_TLOAD() wait_us(15) |
aplatanado |
0:782f2061a8f5 | 36 | |
aplatanado | 2:ee0c13ef1320 | 37 | #define LONG_WAIT_MS(x) \ |
aplatanado | 2:ee0c13ef1320 | 38 | WAIT_TSCLKNCS(); ncs_.write(1); wait_ms(x); ncs_.write(0); WAIT_TNCSSCLK() |
aplatanado | 2:ee0c13ef1320 | 39 | #define LONG_WAIT_US(x) \ |
aplatanado | 2:ee0c13ef1320 | 40 | WAIT_TSCLKNCS(); ncs_.write(1); wait_us(x); ncs_.write(0); WAIT_TNCSSCLK() |
aplatanado | 2:ee0c13ef1320 | 41 | |
aplatanado |
0:782f2061a8f5 | 42 | #define DEFAULT_MAX_FPS 1958 |
aplatanado | 2:ee0c13ef1320 | 43 | #define DEFAULT_MAX_FRAME_PERIOD ((int)ceil(1e6 / DEFAULT_MAX_FPS)) // in us |
aplatanado |
0:782f2061a8f5 | 44 | #define DEFAULT_X_CPI 1620 |
aplatanado |
0:782f2061a8f5 | 45 | #define DEFAULT_Y_CPI 1620 |
aplatanado |
0:782f2061a8f5 | 46 | #define CPI_CHANGE_UNIT 90 |
aplatanado |
0:782f2061a8f5 | 47 | |
aplatanado | 2:ee0c13ef1320 | 48 | #define SPI_BITS_PER_FRAME 8 |
aplatanado | 2:ee0c13ef1320 | 49 | #define SPI_MODE 3 |
aplatanado | 2:ee0c13ef1320 | 50 | #define SPI_WRITE_MODE 0x80 |
aplatanado | 2:ee0c13ef1320 | 51 | |
aplatanado |
0:782f2061a8f5 | 52 | #define SET_BIT(word, mask) (word | mask) |
aplatanado |
0:782f2061a8f5 | 53 | #define CLEAR_BIT(word, mask) (word & (~mask)) |
aplatanado |
0:782f2061a8f5 | 54 | |
aplatanado |
0:782f2061a8f5 | 55 | namespace adns9500 { |
aplatanado |
0:782f2061a8f5 | 56 | |
aplatanado |
0:782f2061a8f5 | 57 | ADNS9500::ADNS9500(PinName mosi, PinName miso, PinName sclk, PinName ncs, |
aplatanado |
0:782f2061a8f5 | 58 | int spi_frequency, PinName motion) |
aplatanado |
0:782f2061a8f5 | 59 | : spi_(mosi, miso, sclk), |
aplatanado |
0:782f2061a8f5 | 60 | motion_(motion), |
aplatanado |
0:782f2061a8f5 | 61 | ncs_(ncs), |
aplatanado |
0:782f2061a8f5 | 62 | enabled_(false), |
aplatanado |
0:782f2061a8f5 | 63 | xCpi_(DEFAULT_X_CPI), yCpi_(DEFAULT_Y_CPI) |
aplatanado |
0:782f2061a8f5 | 64 | { |
aplatanado | 2:ee0c13ef1320 | 65 | spi_.format(SPI_BITS_PER_FRAME, SPI_MODE); |
aplatanado | 10:bbf9ff378632 | 66 | spi_.frequency(spi_frequency); |
aplatanado | 2:ee0c13ef1320 | 67 | |
aplatanado | 2:ee0c13ef1320 | 68 | motion_.mode(PullUp); |
aplatanado |
0:782f2061a8f5 | 69 | motion_.fall(this, &ADNS9500::motionTrigger); |
aplatanado |
0:782f2061a8f5 | 70 | } |
aplatanado |
0:782f2061a8f5 | 71 | |
aplatanado |
0:782f2061a8f5 | 72 | ADNS9500::~ADNS9500() |
aplatanado |
0:782f2061a8f5 | 73 | { |
aplatanado |
0:782f2061a8f5 | 74 | shutdown(); |
aplatanado |
0:782f2061a8f5 | 75 | } |
aplatanado |
0:782f2061a8f5 | 76 | |
aplatanado |
0:782f2061a8f5 | 77 | void ADNS9500::reset(const char* firmware) |
aplatanado |
0:782f2061a8f5 | 78 | { |
aplatanado |
0:782f2061a8f5 | 79 | // SPI port reset |
aplatanado |
0:782f2061a8f5 | 80 | ncs_.write(1); |
aplatanado |
0:782f2061a8f5 | 81 | WAIT_TNCSSCLK(); |
aplatanado |
0:782f2061a8f5 | 82 | ncs_.write(0); |
aplatanado |
0:782f2061a8f5 | 83 | WAIT_TNCSSCLK(); |
aplatanado |
0:782f2061a8f5 | 84 | |
aplatanado | 9:8b1e889e94fe | 85 | // send 0x5a to POWER_UP_RESET and wait for at least 50ms |
aplatanado | 2:ee0c13ef1320 | 86 | spiSend(POWER_UP_RESET, 0x5a); |
aplatanado | 2:ee0c13ef1320 | 87 | LONG_WAIT_MS(50); |
aplatanado |
0:782f2061a8f5 | 88 | |
aplatanado |
0:782f2061a8f5 | 89 | // clear observation register. Only required to deassert shutdown mode. |
aplatanado | 2:ee0c13ef1320 | 90 | spiSend(OBSERVATION, 0x00); |
aplatanado | 2:ee0c13ef1320 | 91 | LONG_WAIT_US(DEFAULT_MAX_FRAME_PERIOD); |
aplatanado | 8:97e5df54b8bb | 92 | |
aplatanado |
0:782f2061a8f5 | 93 | // check observation register bits [5:0] |
aplatanado | 2:ee0c13ef1320 | 94 | int observation = spiReceive(OBSERVATION); |
aplatanado | 9:8b1e889e94fe | 95 | if (! ADNS9500_IF_OBSERVATION_TEST(observation)) { |
aplatanado | 9:8b1e889e94fe | 96 | WAIT_TSCLKNCS(); |
aplatanado | 9:8b1e889e94fe | 97 | ncs_.write(1); |
aplatanado | 9:8b1e889e94fe | 98 | |
aplatanado |
0:782f2061a8f5 | 99 | error("ADNS9500::reset : observation register test failed: 0x%x\n", observation); |
aplatanado | 9:8b1e889e94fe | 100 | } |
aplatanado |
0:782f2061a8f5 | 101 | |
aplatanado |
0:782f2061a8f5 | 102 | // read motion data |
aplatanado | 2:ee0c13ef1320 | 103 | WAIT_TSRR(); |
aplatanado | 2:ee0c13ef1320 | 104 | spiReceive(MOTION); |
aplatanado | 2:ee0c13ef1320 | 105 | WAIT_TSRR(); |
aplatanado | 2:ee0c13ef1320 | 106 | spiReceive(DELTA_X_L); |
aplatanado | 2:ee0c13ef1320 | 107 | WAIT_TSRR(); |
aplatanado | 2:ee0c13ef1320 | 108 | spiReceive(DELTA_X_H); |
aplatanado | 2:ee0c13ef1320 | 109 | WAIT_TSRR(); |
aplatanado | 2:ee0c13ef1320 | 110 | spiReceive(DELTA_Y_L); |
aplatanado | 2:ee0c13ef1320 | 111 | WAIT_TSRR(); |
aplatanado | 2:ee0c13ef1320 | 112 | spiReceive(DELTA_Y_H); |
aplatanado |
0:782f2061a8f5 | 113 | |
aplatanado | 2:ee0c13ef1320 | 114 | // read product and revision id to test the connection |
aplatanado | 2:ee0c13ef1320 | 115 | WAIT_TSRR(); |
aplatanado |
0:782f2061a8f5 | 116 | spi_.write(PRODUCT_ID); |
aplatanado |
0:782f2061a8f5 | 117 | WAIT_TSRAD(); |
aplatanado | 2:ee0c13ef1320 | 118 | int product_id = spi_.write(0x00); |
aplatanado | 2:ee0c13ef1320 | 119 | WAIT_TSRR(); |
aplatanado | 2:ee0c13ef1320 | 120 | spi_.write(REVISION_ID); |
aplatanado |
0:782f2061a8f5 | 121 | WAIT_TSRAD(); |
aplatanado |
0:782f2061a8f5 | 122 | int revision_id = spi_.write(0x00); |
aplatanado | 2:ee0c13ef1320 | 123 | |
aplatanado |
0:782f2061a8f5 | 124 | WAIT_TSCLKNCS(); |
aplatanado |
0:782f2061a8f5 | 125 | ncs_.write(1); |
aplatanado |
0:782f2061a8f5 | 126 | |
aplatanado |
0:782f2061a8f5 | 127 | if (product_id != 0x33) { |
aplatanado |
0:782f2061a8f5 | 128 | error("ADNS9500::reset : bad product ID: 0x%x\n", product_id); |
aplatanado |
0:782f2061a8f5 | 129 | } |
aplatanado |
0:782f2061a8f5 | 130 | |
aplatanado |
0:782f2061a8f5 | 131 | if (revision_id != 0x03) { |
aplatanado |
0:782f2061a8f5 | 132 | error("ADNS9500::reset : bad revision ID: 0x%x\n", revision_id); |
aplatanado |
0:782f2061a8f5 | 133 | } |
aplatanado |
0:782f2061a8f5 | 134 | |
aplatanado |
0:782f2061a8f5 | 135 | enabled_ = true; |
aplatanado |
0:782f2061a8f5 | 136 | |
aplatanado |
0:782f2061a8f5 | 137 | if (firmware) { |
aplatanado |
0:782f2061a8f5 | 138 | sromDownload(firmware); |
aplatanado |
0:782f2061a8f5 | 139 | enableLaser(); |
aplatanado |
0:782f2061a8f5 | 140 | } |
aplatanado |
0:782f2061a8f5 | 141 | } |
aplatanado |
0:782f2061a8f5 | 142 | |
aplatanado |
0:782f2061a8f5 | 143 | void ADNS9500::shutdown() |
aplatanado |
0:782f2061a8f5 | 144 | { |
aplatanado |
0:782f2061a8f5 | 145 | if (! enabled_) |
aplatanado |
0:782f2061a8f5 | 146 | error("ADNS9500::shutdown : the sensor is not enabled\n"); |
aplatanado |
0:782f2061a8f5 | 147 | |
aplatanado |
0:782f2061a8f5 | 148 | ncs_.write(0); |
aplatanado |
0:782f2061a8f5 | 149 | WAIT_TNCSSCLK(); |
aplatanado |
0:782f2061a8f5 | 150 | |
aplatanado | 9:8b1e889e94fe | 151 | // send 0x5a to POWER_UP_RESET |
aplatanado | 2:ee0c13ef1320 | 152 | spiSend(POWER_UP_RESET, 0x5a); |
aplatanado | 2:ee0c13ef1320 | 153 | |
aplatanado |
0:782f2061a8f5 | 154 | WAIT_TSCLKNCS(); |
aplatanado |
0:782f2061a8f5 | 155 | ncs_.write(1); |
aplatanado |
0:782f2061a8f5 | 156 | |
aplatanado |
0:782f2061a8f5 | 157 | enabled_ = false; |
aplatanado |
0:782f2061a8f5 | 158 | } |
aplatanado |
0:782f2061a8f5 | 159 | |
aplatanado |
0:782f2061a8f5 | 160 | int ADNS9500::read(Register lregister) |
aplatanado |
0:782f2061a8f5 | 161 | { |
aplatanado |
0:782f2061a8f5 | 162 | if (! enabled_) |
aplatanado |
0:782f2061a8f5 | 163 | error("ADNS9500::read : the sensor is not enabled\n"); |
aplatanado |
0:782f2061a8f5 | 164 | |
aplatanado |
0:782f2061a8f5 | 165 | ncs_.write(0); |
aplatanado |
0:782f2061a8f5 | 166 | WAIT_TNCSSCLK(); |
aplatanado |
0:782f2061a8f5 | 167 | |
aplatanado |
0:782f2061a8f5 | 168 | // send the command to read the register |
aplatanado | 2:ee0c13ef1320 | 169 | int value = spiReceive(lregister); |
aplatanado | 2:ee0c13ef1320 | 170 | |
aplatanado |
0:782f2061a8f5 | 171 | WAIT_TSCLKNCS(); |
aplatanado |
0:782f2061a8f5 | 172 | ncs_.write(1); |
aplatanado | 2:ee0c13ef1320 | 173 | |
aplatanado |
0:782f2061a8f5 | 174 | return value; |
aplatanado |
0:782f2061a8f5 | 175 | } |
aplatanado |
0:782f2061a8f5 | 176 | |
aplatanado |
0:782f2061a8f5 | 177 | int ADNS9500::read(Register uregister, Register lregister) |
aplatanado |
0:782f2061a8f5 | 178 | { |
aplatanado |
0:782f2061a8f5 | 179 | if (! enabled_) |
aplatanado |
0:782f2061a8f5 | 180 | error("ADNS9500::read : the sensor is not enabled\n"); |
aplatanado |
0:782f2061a8f5 | 181 | |
aplatanado |
0:782f2061a8f5 | 182 | ncs_.write(0); |
aplatanado |
0:782f2061a8f5 | 183 | WAIT_TNCSSCLK(); |
aplatanado |
0:782f2061a8f5 | 184 | |
aplatanado |
0:782f2061a8f5 | 185 | // send the command to read the registers |
aplatanado | 2:ee0c13ef1320 | 186 | int lvalue = spiReceive(lregister); |
aplatanado | 2:ee0c13ef1320 | 187 | WAIT_TSRR(); |
aplatanado | 2:ee0c13ef1320 | 188 | int uvalue = spiReceive(uregister); |
aplatanado | 2:ee0c13ef1320 | 189 | |
aplatanado |
0:782f2061a8f5 | 190 | WAIT_TSCLKNCS(); |
aplatanado |
0:782f2061a8f5 | 191 | ncs_.write(1); |
aplatanado |
0:782f2061a8f5 | 192 | |
aplatanado |
0:782f2061a8f5 | 193 | return ADNS9500_UINT16(uvalue, lvalue); |
aplatanado |
0:782f2061a8f5 | 194 | } |
aplatanado |
0:782f2061a8f5 | 195 | |
aplatanado |
0:782f2061a8f5 | 196 | int ADNS9500::sromDownload(const char* filename) |
aplatanado |
0:782f2061a8f5 | 197 | { |
aplatanado |
0:782f2061a8f5 | 198 | if (! enabled_) |
aplatanado |
0:782f2061a8f5 | 199 | error("ADNS9500::sromDownload : the sensor is not enabled\n"); |
aplatanado |
0:782f2061a8f5 | 200 | |
aplatanado |
0:782f2061a8f5 | 201 | ncs_.write(0); |
aplatanado |
0:782f2061a8f5 | 202 | WAIT_TNCSSCLK(); |
aplatanado |
0:782f2061a8f5 | 203 | |
aplatanado |
0:782f2061a8f5 | 204 | // SROM download |
aplatanado | 2:ee0c13ef1320 | 205 | spiSend(CONFIGURATION_IV, ADNS9500_CONFIGURATION_IV_SROM_SIZE); |
aplatanado | 2:ee0c13ef1320 | 206 | WAIT_TSWW(); |
aplatanado | 2:ee0c13ef1320 | 207 | spiSend(SROM_ENABLE, 0x1d); |
aplatanado | 2:ee0c13ef1320 | 208 | LONG_WAIT_US(DEFAULT_MAX_FRAME_PERIOD); |
aplatanado | 2:ee0c13ef1320 | 209 | |
aplatanado | 2:ee0c13ef1320 | 210 | spiSend(SROM_ENABLE, 0x18); |
aplatanado | 2:ee0c13ef1320 | 211 | WAIT_TSWW(); |
aplatanado | 2:ee0c13ef1320 | 212 | spi_.write(SET_BIT(SROM_LOAD_BURST, SPI_WRITE_MODE)); |
aplatanado | 2:ee0c13ef1320 | 213 | |
aplatanado | 2:ee0c13ef1320 | 214 | // we expect a line per byte in hex without 0x prefix |
aplatanado | 2:ee0c13ef1320 | 215 | char buffer[4]; |
aplatanado |
0:782f2061a8f5 | 216 | ifstream ifs(filename, ifstream::in); |
aplatanado | 2:ee0c13ef1320 | 217 | while(ifs.getline(buffer, sizeof(buffer)).good()) { |
aplatanado |
0:782f2061a8f5 | 218 | WAIT_TLOAD(); |
aplatanado | 2:ee0c13ef1320 | 219 | int byte = strtol(buffer, NULL, 16); |
aplatanado | 2:ee0c13ef1320 | 220 | spi_.write(byte); |
aplatanado |
0:782f2061a8f5 | 221 | } |
aplatanado |
0:782f2061a8f5 | 222 | WAIT_TSCLKNCS(); |
aplatanado |
0:782f2061a8f5 | 223 | ncs_.write(1); |
aplatanado |
0:782f2061a8f5 | 224 | WAIT_TBEXIT(); |
aplatanado |
0:782f2061a8f5 | 225 | |
aplatanado |
0:782f2061a8f5 | 226 | if (! ifs.eof()) |
aplatanado |
0:782f2061a8f5 | 227 | error("ADNS9500::sromDownload : error reading from file: %s\n", filename); |
aplatanado |
0:782f2061a8f5 | 228 | |
aplatanado |
0:782f2061a8f5 | 229 | // test if SROM was downloaded successfully |
aplatanado |
0:782f2061a8f5 | 230 | wait_us(160); |
aplatanado |
0:782f2061a8f5 | 231 | ncs_.write(0); |
aplatanado |
0:782f2061a8f5 | 232 | WAIT_TNCSSCLK(); |
aplatanado | 2:ee0c13ef1320 | 233 | |
aplatanado | 2:ee0c13ef1320 | 234 | int srom_id = spiReceive(SROM_ID); |
aplatanado | 2:ee0c13ef1320 | 235 | |
aplatanado |
0:782f2061a8f5 | 236 | WAIT_TSCLKNCS(); |
aplatanado |
0:782f2061a8f5 | 237 | ncs_.write(1); |
aplatanado |
0:782f2061a8f5 | 238 | |
aplatanado |
0:782f2061a8f5 | 239 | if (! srom_id) |
aplatanado |
0:782f2061a8f5 | 240 | error("ADNS9500::sromDownload : the firmware was not successful downloaded\n"); |
aplatanado |
0:782f2061a8f5 | 241 | |
aplatanado |
0:782f2061a8f5 | 242 | // test laser fault condition |
aplatanado |
0:782f2061a8f5 | 243 | ncs_.write(0); |
aplatanado |
0:782f2061a8f5 | 244 | WAIT_TNCSSCLK(); |
aplatanado | 2:ee0c13ef1320 | 245 | |
aplatanado | 2:ee0c13ef1320 | 246 | int motion = spiReceive(MOTION); |
aplatanado | 2:ee0c13ef1320 | 247 | |
aplatanado |
0:782f2061a8f5 | 248 | WAIT_TSCLKNCS(); |
aplatanado |
0:782f2061a8f5 | 249 | ncs_.write(1); |
aplatanado |
0:782f2061a8f5 | 250 | |
aplatanado |
0:782f2061a8f5 | 251 | if (ADNS9500_IF_LASER_FAULT(motion)) |
aplatanado |
0:782f2061a8f5 | 252 | error("ADNS9500::sromDownload : laser fault condition detected\n"); |
aplatanado |
0:782f2061a8f5 | 253 | |
aplatanado |
0:782f2061a8f5 | 254 | // return the SROM CRC value |
aplatanado |
0:782f2061a8f5 | 255 | ncs_.write(0); |
aplatanado |
0:782f2061a8f5 | 256 | WAIT_TNCSSCLK(); |
aplatanado |
0:782f2061a8f5 | 257 | |
aplatanado | 2:ee0c13ef1320 | 258 | spiSend(SROM_ENABLE, 0x15); |
aplatanado | 2:ee0c13ef1320 | 259 | LONG_WAIT_MS(10); |
aplatanado | 2:ee0c13ef1320 | 260 | |
aplatanado | 2:ee0c13ef1320 | 261 | int lcrc = spiReceive(DATA_OUT_LOWER); |
aplatanado | 2:ee0c13ef1320 | 262 | WAIT_TSRR(); |
aplatanado | 2:ee0c13ef1320 | 263 | int ucrc = spiReceive(DATA_OUT_UPPER); |
aplatanado |
0:782f2061a8f5 | 264 | |
aplatanado |
0:782f2061a8f5 | 265 | WAIT_TSCLKNCS(); |
aplatanado |
0:782f2061a8f5 | 266 | ncs_.write(1); |
aplatanado |
0:782f2061a8f5 | 267 | |
aplatanado |
0:782f2061a8f5 | 268 | return ADNS9500_UINT16(ucrc, lcrc); |
aplatanado |
0:782f2061a8f5 | 269 | } |
aplatanado |
0:782f2061a8f5 | 270 | |
aplatanado |
0:782f2061a8f5 | 271 | void ADNS9500::enableLaser(bool enable) |
aplatanado |
0:782f2061a8f5 | 272 | { |
aplatanado |
0:782f2061a8f5 | 273 | if (! enabled_) |
aplatanado |
0:782f2061a8f5 | 274 | error("ADNS9500::enableLaser : the sensor is not enabled\n"); |
aplatanado | 2:ee0c13ef1320 | 275 | |
aplatanado |
0:782f2061a8f5 | 276 | ncs_.write(0); |
aplatanado |
0:782f2061a8f5 | 277 | WAIT_TNCSSCLK(); |
aplatanado |
0:782f2061a8f5 | 278 | |
aplatanado |
0:782f2061a8f5 | 279 | if (enable) { |
aplatanado |
0:782f2061a8f5 | 280 | int laser_ctrl0 = CLEAR_BIT(0x00, ADNS9500_LASER_CTRL0_FORCE_DISABLED); |
aplatanado | 2:ee0c13ef1320 | 281 | spiSend(LASER_CTRL0, laser_ctrl0); |
aplatanado |
0:782f2061a8f5 | 282 | } |
aplatanado |
0:782f2061a8f5 | 283 | else { |
aplatanado |
0:782f2061a8f5 | 284 | int laser_ctrl0 = SET_BIT(0x00, ADNS9500_LASER_CTRL0_FORCE_DISABLED); |
aplatanado | 2:ee0c13ef1320 | 285 | spiSend(LASER_CTRL0, laser_ctrl0); |
aplatanado |
0:782f2061a8f5 | 286 | } |
aplatanado |
0:782f2061a8f5 | 287 | |
aplatanado |
0:782f2061a8f5 | 288 | WAIT_TSCLKNCS(); |
aplatanado |
0:782f2061a8f5 | 289 | ncs_.write(1); |
aplatanado |
0:782f2061a8f5 | 290 | } |
aplatanado |
0:782f2061a8f5 | 291 | |
aplatanado |
0:782f2061a8f5 | 292 | bool ADNS9500::getMotionDelta(int& dx, int& dy) |
aplatanado |
0:782f2061a8f5 | 293 | { |
aplatanado |
0:782f2061a8f5 | 294 | if (! enabled_) |
aplatanado |
0:782f2061a8f5 | 295 | error("ADNS9500::getMotionDelta : the sensor is not enabled\n"); |
aplatanado |
0:782f2061a8f5 | 296 | |
aplatanado |
0:782f2061a8f5 | 297 | ncs_.write(0); |
aplatanado |
0:782f2061a8f5 | 298 | WAIT_TNCSSCLK(); |
aplatanado | 2:ee0c13ef1320 | 299 | |
aplatanado | 2:ee0c13ef1320 | 300 | dx = 0; |
aplatanado | 2:ee0c13ef1320 | 301 | dy = 0; |
aplatanado | 2:ee0c13ef1320 | 302 | |
aplatanado | 2:ee0c13ef1320 | 303 | int motion = spiReceive(MOTION); |
aplatanado |
0:782f2061a8f5 | 304 | if (ADNS9500_IF_MOTION(motion)) { |
aplatanado | 2:ee0c13ef1320 | 305 | WAIT_TSRR(); |
aplatanado | 2:ee0c13ef1320 | 306 | int dxl = spiReceive(DELTA_X_L); |
aplatanado | 2:ee0c13ef1320 | 307 | WAIT_TSRR(); |
aplatanado | 2:ee0c13ef1320 | 308 | dx += ADNS9500_INT16(spiReceive(DELTA_X_H), dxl); |
aplatanado |
0:782f2061a8f5 | 309 | |
aplatanado | 2:ee0c13ef1320 | 310 | WAIT_TSRR(); |
aplatanado | 2:ee0c13ef1320 | 311 | int dyl = spiReceive(DELTA_Y_L); |
aplatanado | 2:ee0c13ef1320 | 312 | WAIT_TSRR(); |
aplatanado | 2:ee0c13ef1320 | 313 | dy += ADNS9500_INT16(spiReceive(DELTA_Y_H), dyl); |
aplatanado |
0:782f2061a8f5 | 314 | } |
aplatanado |
0:782f2061a8f5 | 315 | |
aplatanado |
0:782f2061a8f5 | 316 | WAIT_TSCLKNCS(); |
aplatanado |
0:782f2061a8f5 | 317 | ncs_.write(1); |
aplatanado | 2:ee0c13ef1320 | 318 | |
aplatanado |
0:782f2061a8f5 | 319 | return ADNS9500_IF_MOTION(motion); |
aplatanado |
0:782f2061a8f5 | 320 | } |
aplatanado |
0:782f2061a8f5 | 321 | |
aplatanado |
0:782f2061a8f5 | 322 | bool ADNS9500::getMotionDeltaMM(float& dx, float& dy) |
aplatanado |
0:782f2061a8f5 | 323 | { |
aplatanado |
0:782f2061a8f5 | 324 | int rawDx, rawDy; |
aplatanado |
0:782f2061a8f5 | 325 | |
aplatanado |
0:782f2061a8f5 | 326 | bool motion = getMotionDelta(rawDx, rawDy); |
aplatanado |
0:782f2061a8f5 | 327 | dx = (float)rawDx / xCpi_ * 25.4; |
aplatanado |
0:782f2061a8f5 | 328 | dy = (float)rawDy / yCpi_ * 25.4; |
aplatanado |
0:782f2061a8f5 | 329 | |
aplatanado |
0:782f2061a8f5 | 330 | return motion; |
aplatanado |
0:782f2061a8f5 | 331 | } |
aplatanado |
0:782f2061a8f5 | 332 | |
aplatanado |
0:782f2061a8f5 | 333 | bool ADNS9500::getMotionData(MotionData& data) |
aplatanado |
0:782f2061a8f5 | 334 | { |
aplatanado |
0:782f2061a8f5 | 335 | if (! enabled_) |
aplatanado |
0:782f2061a8f5 | 336 | error("ADNS9500::getMotionData : the sensor is not enabled\n"); |
aplatanado |
0:782f2061a8f5 | 337 | |
aplatanado |
0:782f2061a8f5 | 338 | ncs_.write(0); |
aplatanado |
0:782f2061a8f5 | 339 | WAIT_TNCSSCLK(); |
aplatanado |
0:782f2061a8f5 | 340 | |
aplatanado |
0:782f2061a8f5 | 341 | // activate motion burst mode |
aplatanado |
0:782f2061a8f5 | 342 | spi_.write(0x50); |
aplatanado | 2:ee0c13ef1320 | 343 | WAIT_TSRAD(); // see the chronogram |
aplatanado |
0:782f2061a8f5 | 344 | |
aplatanado |
0:782f2061a8f5 | 345 | // read motion burst data |
aplatanado |
0:782f2061a8f5 | 346 | data.motion = spi_.write(0x00); |
aplatanado |
0:782f2061a8f5 | 347 | data.observation = spi_.write(0x00); |
aplatanado |
0:782f2061a8f5 | 348 | |
aplatanado |
0:782f2061a8f5 | 349 | int ldx = spi_.write(0x00); |
aplatanado | 2:ee0c13ef1320 | 350 | data.dx = ADNS9500_INT16(spi_.write(0x00), ldx); |
aplatanado |
0:782f2061a8f5 | 351 | |
aplatanado |
0:782f2061a8f5 | 352 | int ldy = spi_.write(0x00); |
aplatanado | 2:ee0c13ef1320 | 353 | data.dy = ADNS9500_INT16(spi_.write(0x00), ldy); |
aplatanado |
0:782f2061a8f5 | 354 | |
aplatanado | 2:ee0c13ef1320 | 355 | data.surfaceQuality = spi_.write(0x00) * 4; |
aplatanado | 2:ee0c13ef1320 | 356 | data.averagePixel = spi_.write(0x00) / 1.76; |
aplatanado |
0:782f2061a8f5 | 357 | data.maximumPixel = spi_.write(0x00); |
aplatanado |
0:782f2061a8f5 | 358 | data.minimumPixel = spi_.write(0x00); |
aplatanado |
0:782f2061a8f5 | 359 | |
aplatanado |
0:782f2061a8f5 | 360 | int ushutter = spi_.write(0x00); |
aplatanado |
0:782f2061a8f5 | 361 | data.shutter = ADNS9500_UINT16(ushutter, spi_.write(0x00)); |
aplatanado |
0:782f2061a8f5 | 362 | |
aplatanado |
0:782f2061a8f5 | 363 | int uframe_period = spi_.write(0x00); |
aplatanado |
0:782f2061a8f5 | 364 | data.framePeriod = ADNS9500_UINT16(uframe_period, spi_.write(0x00)); |
aplatanado |
0:782f2061a8f5 | 365 | |
aplatanado |
0:782f2061a8f5 | 366 | WAIT_TSCLKNCS(); |
aplatanado |
0:782f2061a8f5 | 367 | ncs_.write(1); |
aplatanado |
0:782f2061a8f5 | 368 | WAIT_TBEXIT(); |
aplatanado | 2:ee0c13ef1320 | 369 | |
aplatanado | 2:ee0c13ef1320 | 370 | data.dxMM = (float)data.dx / xCpi_ * 25.4; |
aplatanado | 2:ee0c13ef1320 | 371 | data.dyMM = (float)data.dy / yCpi_ * 25.4; |
aplatanado | 2:ee0c13ef1320 | 372 | |
aplatanado | 2:ee0c13ef1320 | 373 | // write a value to Motion register to clear motion bit |
aplatanado | 2:ee0c13ef1320 | 374 | ncs_.write(0); |
aplatanado | 2:ee0c13ef1320 | 375 | WAIT_TNCSSCLK(); |
aplatanado |
0:782f2061a8f5 | 376 | |
aplatanado | 2:ee0c13ef1320 | 377 | spiSend(MOTION, 0x00); |
aplatanado | 2:ee0c13ef1320 | 378 | |
aplatanado | 2:ee0c13ef1320 | 379 | WAIT_TSCLKNCS(); |
aplatanado | 2:ee0c13ef1320 | 380 | ncs_.write(1); |
aplatanado | 2:ee0c13ef1320 | 381 | |
aplatanado |
0:782f2061a8f5 | 382 | return ADNS9500_IF_MOTION(data.motion); |
aplatanado |
0:782f2061a8f5 | 383 | } |
aplatanado |
0:782f2061a8f5 | 384 | |
aplatanado |
0:782f2061a8f5 | 385 | void ADNS9500::setResolution(Resolution xy_resolution) |
aplatanado |
0:782f2061a8f5 | 386 | { |
aplatanado |
0:782f2061a8f5 | 387 | if (! enabled_) |
aplatanado |
0:782f2061a8f5 | 388 | error("ADNS9500::setResolution : the sensor is not enabled\n"); |
aplatanado |
0:782f2061a8f5 | 389 | |
aplatanado |
0:782f2061a8f5 | 390 | ncs_.write(0); |
aplatanado |
0:782f2061a8f5 | 391 | WAIT_TNCSSCLK(); |
aplatanado |
0:782f2061a8f5 | 392 | |
aplatanado |
0:782f2061a8f5 | 393 | // enable XY axes CPI in sync mode |
aplatanado | 2:ee0c13ef1320 | 394 | int rpt_mod = spiReceive(CONFIGURATION_II); |
aplatanado | 2:ee0c13ef1320 | 395 | rpt_mod = CLEAR_BIT(rpt_mod, ADNS9500_CONFIGURATION_II_RPT_MOD); |
aplatanado | 2:ee0c13ef1320 | 396 | WAIT_TSRW(); |
aplatanado | 2:ee0c13ef1320 | 397 | spiSend(CONFIGURATION_II, rpt_mod); |
aplatanado |
0:782f2061a8f5 | 398 | |
aplatanado |
0:782f2061a8f5 | 399 | // set resolution for X-axis and Y-axis |
aplatanado | 2:ee0c13ef1320 | 400 | WAIT_TSWW(); |
aplatanado | 2:ee0c13ef1320 | 401 | spiSend(CONFIGURATION_I, xy_resolution); |
aplatanado |
0:782f2061a8f5 | 402 | |
aplatanado |
0:782f2061a8f5 | 403 | WAIT_TSCLKNCS(); |
aplatanado |
0:782f2061a8f5 | 404 | ncs_.write(1); |
aplatanado |
0:782f2061a8f5 | 405 | |
aplatanado |
0:782f2061a8f5 | 406 | xCpi_ = xy_resolution * CPI_CHANGE_UNIT; |
aplatanado |
0:782f2061a8f5 | 407 | yCpi_ = xy_resolution * CPI_CHANGE_UNIT; |
aplatanado |
0:782f2061a8f5 | 408 | } |
aplatanado |
0:782f2061a8f5 | 409 | |
aplatanado |
0:782f2061a8f5 | 410 | void ADNS9500::setResolution(Resolution x_resolution, Resolution y_resolution) |
aplatanado |
0:782f2061a8f5 | 411 | { |
aplatanado |
0:782f2061a8f5 | 412 | if (! enabled_) |
aplatanado |
0:782f2061a8f5 | 413 | error("ADNS9500::setResolution : the sensor is not enabled\n"); |
aplatanado |
0:782f2061a8f5 | 414 | |
aplatanado |
0:782f2061a8f5 | 415 | ncs_.write(0); |
aplatanado |
0:782f2061a8f5 | 416 | WAIT_TNCSSCLK(); |
aplatanado |
0:782f2061a8f5 | 417 | |
aplatanado |
0:782f2061a8f5 | 418 | // disable XY axes CPI in sync mode |
aplatanado | 2:ee0c13ef1320 | 419 | int rpt_mod = spiReceive(CONFIGURATION_II); |
aplatanado | 2:ee0c13ef1320 | 420 | rpt_mod = SET_BIT(rpt_mod, ADNS9500_CONFIGURATION_II_RPT_MOD); |
aplatanado | 2:ee0c13ef1320 | 421 | WAIT_TSRW(); |
aplatanado | 2:ee0c13ef1320 | 422 | spiSend(CONFIGURATION_II, rpt_mod); |
aplatanado |
0:782f2061a8f5 | 423 | |
aplatanado |
0:782f2061a8f5 | 424 | // set resolution for X-axis |
aplatanado | 2:ee0c13ef1320 | 425 | WAIT_TSWW(); |
aplatanado | 2:ee0c13ef1320 | 426 | spiSend(CONFIGURATION_I, x_resolution); |
aplatanado |
0:782f2061a8f5 | 427 | |
aplatanado |
0:782f2061a8f5 | 428 | // set resolution for Y-axis |
aplatanado | 2:ee0c13ef1320 | 429 | WAIT_TSWW(); |
aplatanado | 2:ee0c13ef1320 | 430 | spiSend(CONFIGURATION_V, y_resolution); |
aplatanado |
0:782f2061a8f5 | 431 | |
aplatanado |
0:782f2061a8f5 | 432 | WAIT_TSCLKNCS(); |
aplatanado |
0:782f2061a8f5 | 433 | ncs_.write(1); |
aplatanado |
0:782f2061a8f5 | 434 | |
aplatanado |
0:782f2061a8f5 | 435 | xCpi_ = x_resolution * CPI_CHANGE_UNIT; |
aplatanado |
0:782f2061a8f5 | 436 | yCpi_ = y_resolution * CPI_CHANGE_UNIT; |
aplatanado |
0:782f2061a8f5 | 437 | } |
aplatanado |
0:782f2061a8f5 | 438 | |
aplatanado | 3:898ed1944119 | 439 | void ADNS9500::captureFrame(uint8_t* pixels) |
aplatanado |
0:782f2061a8f5 | 440 | { |
aplatanado |
0:782f2061a8f5 | 441 | if (! enabled_) |
aplatanado |
0:782f2061a8f5 | 442 | error("ADNS9500::captureFrame : the sensor is not enabled\n"); |
aplatanado |
0:782f2061a8f5 | 443 | |
aplatanado |
0:782f2061a8f5 | 444 | ncs_.write(0); |
aplatanado |
0:782f2061a8f5 | 445 | WAIT_TNCSSCLK(); |
aplatanado |
0:782f2061a8f5 | 446 | |
aplatanado | 2:ee0c13ef1320 | 447 | spiSend(FRAME_CAPTURE, 0x93); |
aplatanado | 2:ee0c13ef1320 | 448 | WAIT_TSWW(); |
aplatanado | 2:ee0c13ef1320 | 449 | spiSend(FRAME_CAPTURE, 0xc5); |
aplatanado | 2:ee0c13ef1320 | 450 | LONG_WAIT_US(2*DEFAULT_MAX_FRAME_PERIOD); |
aplatanado | 2:ee0c13ef1320 | 451 | |
aplatanado |
0:782f2061a8f5 | 452 | // check for first pixel reading motion bit |
aplatanado | 3:898ed1944119 | 453 | int motion = spiReceive(MOTION); |
aplatanado | 3:898ed1944119 | 454 | WAIT_TSRR(); |
aplatanado | 3:898ed1944119 | 455 | while(! ADNS9500_IF_FRAME_FIRST_PIXEL(motion)) { |
aplatanado | 2:ee0c13ef1320 | 456 | int motion = spiReceive(MOTION); |
aplatanado | 2:ee0c13ef1320 | 457 | WAIT_TSRR(); |
aplatanado |
0:782f2061a8f5 | 458 | } |
aplatanado | 2:ee0c13ef1320 | 459 | |
aplatanado |
0:782f2061a8f5 | 460 | // read pixel values |
aplatanado |
0:782f2061a8f5 | 461 | spi_.write(PIXEL_BURST); |
aplatanado |
0:782f2061a8f5 | 462 | WAIT_TSRAD(); |
aplatanado | 3:898ed1944119 | 463 | for (uint8_t* p = pixels; p != pixels + NUMBER_OF_PIXELS_PER_FRAME; ++p) { |
aplatanado | 3:898ed1944119 | 464 | *p = spi_.write(0x00); |
aplatanado |
0:782f2061a8f5 | 465 | WAIT_TLOAD(); |
aplatanado |
0:782f2061a8f5 | 466 | } |
aplatanado |
0:782f2061a8f5 | 467 | |
aplatanado | 3:898ed1944119 | 468 | // burst exit |
aplatanado |
0:782f2061a8f5 | 469 | ncs_.write(1); |
aplatanado |
0:782f2061a8f5 | 470 | WAIT_TBEXIT(); |
aplatanado |
0:782f2061a8f5 | 471 | } |
aplatanado | 2:ee0c13ef1320 | 472 | |
aplatanado | 2:ee0c13ef1320 | 473 | void ADNS9500::spiSend(Register address, int value) |
aplatanado | 2:ee0c13ef1320 | 474 | { |
aplatanado | 2:ee0c13ef1320 | 475 | spi_.write(SET_BIT(address, SPI_WRITE_MODE)); |
aplatanado | 2:ee0c13ef1320 | 476 | spi_.write(value); |
aplatanado | 2:ee0c13ef1320 | 477 | } |
aplatanado | 2:ee0c13ef1320 | 478 | |
aplatanado | 2:ee0c13ef1320 | 479 | int ADNS9500::spiReceive(Register address) |
aplatanado | 2:ee0c13ef1320 | 480 | { |
aplatanado | 2:ee0c13ef1320 | 481 | spi_.write(CLEAR_BIT(address, SPI_WRITE_MODE)); |
aplatanado | 2:ee0c13ef1320 | 482 | WAIT_TSRAD(); |
aplatanado | 2:ee0c13ef1320 | 483 | return spi_.write(0x00); |
aplatanado | 2:ee0c13ef1320 | 484 | } |
aplatanado |
0:782f2061a8f5 | 485 | } |