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