mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /**
elessair 0:f269e3021894 2 * @author Aaron Berk
elessair 0:f269e3021894 3 *
elessair 0:f269e3021894 4 * @section LICENSE
elessair 0:f269e3021894 5 *
elessair 0:f269e3021894 6 * Copyright (c) 2010 ARM Limited
elessair 0:f269e3021894 7 *
elessair 0:f269e3021894 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
elessair 0:f269e3021894 9 * of this software and associated documentation files (the "Software"), to deal
elessair 0:f269e3021894 10 * in the Software without restriction, including without limitation the rights
elessair 0:f269e3021894 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
elessair 0:f269e3021894 12 * copies of the Software, and to permit persons to whom the Software is
elessair 0:f269e3021894 13 * furnished to do so, subject to the following conditions:
elessair 0:f269e3021894 14 *
elessair 0:f269e3021894 15 * The above copyright notice and this permission notice shall be included in
elessair 0:f269e3021894 16 * all copies or substantial portions of the Software.
elessair 0:f269e3021894 17 *
elessair 0:f269e3021894 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
elessair 0:f269e3021894 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
elessair 0:f269e3021894 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
elessair 0:f269e3021894 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
elessair 0:f269e3021894 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
elessair 0:f269e3021894 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
elessair 0:f269e3021894 24 * THE SOFTWARE.
elessair 0:f269e3021894 25 *
elessair 0:f269e3021894 26 * @section DESCRIPTION
elessair 0:f269e3021894 27 *
elessair 0:f269e3021894 28 * ADXL345, triple axis, digital interface, accelerometer.
elessair 0:f269e3021894 29 *
elessair 0:f269e3021894 30 * Datasheet:
elessair 0:f269e3021894 31 *
elessair 0:f269e3021894 32 * http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf
elessair 0:f269e3021894 33 */
elessair 0:f269e3021894 34
elessair 0:f269e3021894 35 /**
elessair 0:f269e3021894 36 * Includes
elessair 0:f269e3021894 37 */
elessair 0:f269e3021894 38 #include "ADXL345.h"
elessair 0:f269e3021894 39
elessair 0:f269e3021894 40 ADXL345::ADXL345(PinName mosi,
elessair 0:f269e3021894 41 PinName miso,
elessair 0:f269e3021894 42 PinName sck,
elessair 0:f269e3021894 43 PinName cs) : spi_(mosi, miso, sck), nCS_(cs) {
elessair 0:f269e3021894 44
elessair 0:f269e3021894 45 //2MHz, allowing us to use the fastest data rates.
elessair 0:f269e3021894 46 spi_.frequency(2000000);
elessair 0:f269e3021894 47 spi_.format(8,3);
elessair 0:f269e3021894 48
elessair 0:f269e3021894 49 nCS_ = 1;
elessair 0:f269e3021894 50
elessair 0:f269e3021894 51 wait_us(500);
elessair 0:f269e3021894 52
elessair 0:f269e3021894 53 }
elessair 0:f269e3021894 54
elessair 0:f269e3021894 55 int ADXL345::getDevId(void) {
elessair 0:f269e3021894 56
elessair 0:f269e3021894 57 return oneByteRead(ADXL345_DEVID_REG);
elessair 0:f269e3021894 58
elessair 0:f269e3021894 59 }
elessair 0:f269e3021894 60
elessair 0:f269e3021894 61 int ADXL345::getTapThreshold(void) {
elessair 0:f269e3021894 62
elessair 0:f269e3021894 63 return oneByteRead(ADXL345_THRESH_TAP_REG);
elessair 0:f269e3021894 64
elessair 0:f269e3021894 65 }
elessair 0:f269e3021894 66
elessair 0:f269e3021894 67 void ADXL345::setTapThreshold(int threshold) {
elessair 0:f269e3021894 68
elessair 0:f269e3021894 69 oneByteWrite(ADXL345_THRESH_TAP_REG, threshold);
elessair 0:f269e3021894 70
elessair 0:f269e3021894 71 }
elessair 0:f269e3021894 72
elessair 0:f269e3021894 73 int ADXL345::getOffset(int axis) {
elessair 0:f269e3021894 74
elessair 0:f269e3021894 75 int address = 0;
elessair 0:f269e3021894 76
elessair 0:f269e3021894 77 if (axis == ADXL345_X) {
elessair 0:f269e3021894 78 address = ADXL345_OFSX_REG;
elessair 0:f269e3021894 79 } else if (axis == ADXL345_Y) {
elessair 0:f269e3021894 80 address = ADXL345_OFSY_REG;
elessair 0:f269e3021894 81 } else if (axis == ADXL345_Z) {
elessair 0:f269e3021894 82 address = ADXL345_OFSZ_REG;
elessair 0:f269e3021894 83 }
elessair 0:f269e3021894 84
elessair 0:f269e3021894 85 return oneByteRead(address);
elessair 0:f269e3021894 86
elessair 0:f269e3021894 87 }
elessair 0:f269e3021894 88
elessair 0:f269e3021894 89 void ADXL345::setOffset(int axis, char offset) {
elessair 0:f269e3021894 90
elessair 0:f269e3021894 91 int address = 0;
elessair 0:f269e3021894 92
elessair 0:f269e3021894 93 if (axis == ADXL345_X) {
elessair 0:f269e3021894 94 address = ADXL345_OFSX_REG;
elessair 0:f269e3021894 95 } else if (axis == ADXL345_Y) {
elessair 0:f269e3021894 96 address = ADXL345_OFSY_REG;
elessair 0:f269e3021894 97 } else if (axis == ADXL345_Z) {
elessair 0:f269e3021894 98 address = ADXL345_OFSZ_REG;
elessair 0:f269e3021894 99 }
elessair 0:f269e3021894 100
elessair 0:f269e3021894 101 return oneByteWrite(address, offset);
elessair 0:f269e3021894 102
elessair 0:f269e3021894 103 }
elessair 0:f269e3021894 104
elessair 0:f269e3021894 105 int ADXL345::getTapDuration(void) {
elessair 0:f269e3021894 106
elessair 0:f269e3021894 107 return oneByteRead(ADXL345_DUR_REG)*625;
elessair 0:f269e3021894 108
elessair 0:f269e3021894 109 }
elessair 0:f269e3021894 110
elessair 0:f269e3021894 111 void ADXL345::setTapDuration(int duration_us) {
elessair 0:f269e3021894 112
elessair 0:f269e3021894 113 int tapDuration = duration_us / 625;
elessair 0:f269e3021894 114
elessair 0:f269e3021894 115 oneByteWrite(ADXL345_DUR_REG, tapDuration);
elessair 0:f269e3021894 116
elessair 0:f269e3021894 117 }
elessair 0:f269e3021894 118
elessair 0:f269e3021894 119 float ADXL345::getTapLatency(void) {
elessair 0:f269e3021894 120
elessair 0:f269e3021894 121 return oneByteRead(ADXL345_LATENT_REG)*1.25;
elessair 0:f269e3021894 122
elessair 0:f269e3021894 123 }
elessair 0:f269e3021894 124
elessair 0:f269e3021894 125 void ADXL345::setTapLatency(int latency_ms) {
elessair 0:f269e3021894 126
elessair 0:f269e3021894 127 int tapLatency = latency_ms / 1.25;
elessair 0:f269e3021894 128
elessair 0:f269e3021894 129 oneByteWrite(ADXL345_LATENT_REG, tapLatency);
elessair 0:f269e3021894 130
elessair 0:f269e3021894 131 }
elessair 0:f269e3021894 132
elessair 0:f269e3021894 133 float ADXL345::getWindowTime(void) {
elessair 0:f269e3021894 134
elessair 0:f269e3021894 135 return oneByteRead(ADXL345_WINDOW_REG)*1.25;
elessair 0:f269e3021894 136
elessair 0:f269e3021894 137 }
elessair 0:f269e3021894 138
elessair 0:f269e3021894 139 void ADXL345::setWindowTime(int window_ms) {
elessair 0:f269e3021894 140
elessair 0:f269e3021894 141 int windowTime = window_ms / 1.25;
elessair 0:f269e3021894 142
elessair 0:f269e3021894 143 oneByteWrite(ADXL345_WINDOW_REG, windowTime);
elessair 0:f269e3021894 144
elessair 0:f269e3021894 145 }
elessair 0:f269e3021894 146
elessair 0:f269e3021894 147 int ADXL345::getActivityThreshold(void) {
elessair 0:f269e3021894 148
elessair 0:f269e3021894 149 return oneByteRead(ADXL345_THRESH_ACT_REG);
elessair 0:f269e3021894 150
elessair 0:f269e3021894 151 }
elessair 0:f269e3021894 152
elessair 0:f269e3021894 153 void ADXL345::setActivityThreshold(int threshold) {
elessair 0:f269e3021894 154
elessair 0:f269e3021894 155 oneByteWrite(ADXL345_THRESH_ACT_REG, threshold);
elessair 0:f269e3021894 156
elessair 0:f269e3021894 157 }
elessair 0:f269e3021894 158
elessair 0:f269e3021894 159 int ADXL345::getInactivityThreshold(void) {
elessair 0:f269e3021894 160
elessair 0:f269e3021894 161 return oneByteRead(ADXL345_THRESH_INACT_REG);
elessair 0:f269e3021894 162
elessair 0:f269e3021894 163 }
elessair 0:f269e3021894 164
elessair 0:f269e3021894 165 void ADXL345::setInactivityThreshold(int threshold) {
elessair 0:f269e3021894 166
elessair 0:f269e3021894 167 return oneByteWrite(ADXL345_THRESH_INACT_REG, threshold);
elessair 0:f269e3021894 168
elessair 0:f269e3021894 169 }
elessair 0:f269e3021894 170
elessair 0:f269e3021894 171 int ADXL345::getTimeInactivity(void) {
elessair 0:f269e3021894 172
elessair 0:f269e3021894 173 return oneByteRead(ADXL345_TIME_INACT_REG);
elessair 0:f269e3021894 174
elessair 0:f269e3021894 175 }
elessair 0:f269e3021894 176
elessair 0:f269e3021894 177 void ADXL345::setTimeInactivity(int timeInactivity) {
elessair 0:f269e3021894 178
elessair 0:f269e3021894 179 oneByteWrite(ADXL345_TIME_INACT_REG, timeInactivity);
elessair 0:f269e3021894 180
elessair 0:f269e3021894 181 }
elessair 0:f269e3021894 182
elessair 0:f269e3021894 183 int ADXL345::getActivityInactivityControl(void) {
elessair 0:f269e3021894 184
elessair 0:f269e3021894 185 return oneByteRead(ADXL345_ACT_INACT_CTL_REG);
elessair 0:f269e3021894 186
elessair 0:f269e3021894 187 }
elessair 0:f269e3021894 188
elessair 0:f269e3021894 189 void ADXL345::setActivityInactivityControl(int settings) {
elessair 0:f269e3021894 190
elessair 0:f269e3021894 191 oneByteWrite(ADXL345_ACT_INACT_CTL_REG, settings);
elessair 0:f269e3021894 192
elessair 0:f269e3021894 193 }
elessair 0:f269e3021894 194
elessair 0:f269e3021894 195 int ADXL345::getFreefallThreshold(void) {
elessair 0:f269e3021894 196
elessair 0:f269e3021894 197 return oneByteRead(ADXL345_THRESH_FF_REG);
elessair 0:f269e3021894 198
elessair 0:f269e3021894 199 }
elessair 0:f269e3021894 200
elessair 0:f269e3021894 201 void ADXL345::setFreefallThreshold(int threshold) {
elessair 0:f269e3021894 202
elessair 0:f269e3021894 203 oneByteWrite(ADXL345_THRESH_FF_REG, threshold);
elessair 0:f269e3021894 204
elessair 0:f269e3021894 205 }
elessair 0:f269e3021894 206
elessair 0:f269e3021894 207 int ADXL345::getFreefallTime(void) {
elessair 0:f269e3021894 208
elessair 0:f269e3021894 209 return oneByteRead(ADXL345_TIME_FF_REG)*5;
elessair 0:f269e3021894 210
elessair 0:f269e3021894 211 }
elessair 0:f269e3021894 212
elessair 0:f269e3021894 213 void ADXL345::setFreefallTime(int freefallTime_ms) {
elessair 0:f269e3021894 214
elessair 0:f269e3021894 215 int freefallTime = freefallTime_ms / 5;
elessair 0:f269e3021894 216
elessair 0:f269e3021894 217 oneByteWrite(ADXL345_TIME_FF_REG, freefallTime);
elessair 0:f269e3021894 218
elessair 0:f269e3021894 219 }
elessair 0:f269e3021894 220
elessair 0:f269e3021894 221 int ADXL345::getTapAxisControl(void) {
elessair 0:f269e3021894 222
elessair 0:f269e3021894 223 return oneByteRead(ADXL345_TAP_AXES_REG);
elessair 0:f269e3021894 224
elessair 0:f269e3021894 225 }
elessair 0:f269e3021894 226
elessair 0:f269e3021894 227 void ADXL345::setTapAxisControl(int settings) {
elessair 0:f269e3021894 228
elessair 0:f269e3021894 229 oneByteWrite(ADXL345_TAP_AXES_REG, settings);
elessair 0:f269e3021894 230
elessair 0:f269e3021894 231 }
elessair 0:f269e3021894 232
elessair 0:f269e3021894 233 int ADXL345::getTapSource(void) {
elessair 0:f269e3021894 234
elessair 0:f269e3021894 235 return oneByteRead(ADXL345_ACT_TAP_STATUS_REG);
elessair 0:f269e3021894 236
elessair 0:f269e3021894 237 }
elessair 0:f269e3021894 238
elessair 0:f269e3021894 239 void ADXL345::setPowerMode(char mode) {
elessair 0:f269e3021894 240
elessair 0:f269e3021894 241 //Get the current register contents, so we don't clobber the rate value.
elessair 0:f269e3021894 242 char registerContents = oneByteRead(ADXL345_BW_RATE_REG);
elessair 0:f269e3021894 243
elessair 0:f269e3021894 244 registerContents = (mode << 4) | registerContents;
elessair 0:f269e3021894 245
elessair 0:f269e3021894 246 oneByteWrite(ADXL345_BW_RATE_REG, registerContents);
elessair 0:f269e3021894 247
elessair 0:f269e3021894 248 }
elessair 0:f269e3021894 249
elessair 0:f269e3021894 250 int ADXL345::getPowerControl(void) {
elessair 0:f269e3021894 251
elessair 0:f269e3021894 252 return oneByteRead(ADXL345_POWER_CTL_REG);
elessair 0:f269e3021894 253
elessair 0:f269e3021894 254 }
elessair 0:f269e3021894 255
elessair 0:f269e3021894 256 void ADXL345::setPowerControl(int settings) {
elessair 0:f269e3021894 257
elessair 0:f269e3021894 258 oneByteWrite(ADXL345_POWER_CTL_REG, settings);
elessair 0:f269e3021894 259
elessair 0:f269e3021894 260 }
elessair 0:f269e3021894 261
elessair 0:f269e3021894 262 int ADXL345::getInterruptEnableControl(void) {
elessair 0:f269e3021894 263
elessair 0:f269e3021894 264 return oneByteRead(ADXL345_INT_ENABLE_REG);
elessair 0:f269e3021894 265
elessair 0:f269e3021894 266 }
elessair 0:f269e3021894 267
elessair 0:f269e3021894 268 void ADXL345::setInterruptEnableControl(int settings) {
elessair 0:f269e3021894 269
elessair 0:f269e3021894 270 oneByteWrite(ADXL345_INT_ENABLE_REG, settings);
elessair 0:f269e3021894 271
elessair 0:f269e3021894 272 }
elessair 0:f269e3021894 273
elessair 0:f269e3021894 274 int ADXL345::getInterruptMappingControl(void) {
elessair 0:f269e3021894 275
elessair 0:f269e3021894 276 return oneByteRead(ADXL345_INT_MAP_REG);
elessair 0:f269e3021894 277
elessair 0:f269e3021894 278 }
elessair 0:f269e3021894 279
elessair 0:f269e3021894 280 void ADXL345::setInterruptMappingControl(int settings) {
elessair 0:f269e3021894 281
elessair 0:f269e3021894 282 oneByteWrite(ADXL345_INT_MAP_REG, settings);
elessair 0:f269e3021894 283
elessair 0:f269e3021894 284 }
elessair 0:f269e3021894 285
elessair 0:f269e3021894 286 int ADXL345::getInterruptSource(void){
elessair 0:f269e3021894 287
elessair 0:f269e3021894 288 return oneByteRead(ADXL345_INT_SOURCE_REG);
elessair 0:f269e3021894 289
elessair 0:f269e3021894 290 }
elessair 0:f269e3021894 291
elessair 0:f269e3021894 292 int ADXL345::getDataFormatControl(void){
elessair 0:f269e3021894 293
elessair 0:f269e3021894 294 return oneByteRead(ADXL345_DATA_FORMAT_REG);
elessair 0:f269e3021894 295
elessair 0:f269e3021894 296 }
elessair 0:f269e3021894 297
elessair 0:f269e3021894 298 void ADXL345::setDataFormatControl(int settings){
elessair 0:f269e3021894 299
elessair 0:f269e3021894 300 oneByteWrite(ADXL345_DATA_FORMAT_REG, settings);
elessair 0:f269e3021894 301
elessair 0:f269e3021894 302 }
elessair 0:f269e3021894 303
elessair 0:f269e3021894 304 void ADXL345::setDataRate(int rate) {
elessair 0:f269e3021894 305
elessair 0:f269e3021894 306 //Get the current register contents, so we don't clobber the power bit.
elessair 0:f269e3021894 307 char registerContents = oneByteRead(ADXL345_BW_RATE_REG);
elessair 0:f269e3021894 308
elessair 0:f269e3021894 309 registerContents &= 0x10;
elessair 0:f269e3021894 310 registerContents |= rate;
elessair 0:f269e3021894 311
elessair 0:f269e3021894 312 oneByteWrite(ADXL345_BW_RATE_REG, registerContents);
elessair 0:f269e3021894 313
elessair 0:f269e3021894 314 }
elessair 0:f269e3021894 315
elessair 0:f269e3021894 316 void ADXL345::getOutput(int* readings){
elessair 0:f269e3021894 317
elessair 0:f269e3021894 318 char buffer[6];
elessair 0:f269e3021894 319
elessair 0:f269e3021894 320 multiByteRead(ADXL345_DATAX0_REG, buffer, 6);
elessair 0:f269e3021894 321
elessair 0:f269e3021894 322 readings[0] = (int)buffer[1] << 8 | (int)buffer[0];
elessair 0:f269e3021894 323 readings[1] = (int)buffer[3] << 8 | (int)buffer[2];
elessair 0:f269e3021894 324 readings[2] = (int)buffer[5] << 8 | (int)buffer[4];
elessair 0:f269e3021894 325
elessair 0:f269e3021894 326 }
elessair 0:f269e3021894 327
elessair 0:f269e3021894 328 int ADXL345::getFifoControl(void){
elessair 0:f269e3021894 329
elessair 0:f269e3021894 330 return oneByteRead(ADXL345_FIFO_CTL);
elessair 0:f269e3021894 331
elessair 0:f269e3021894 332 }
elessair 0:f269e3021894 333
elessair 0:f269e3021894 334 void ADXL345::setFifoControl(int settings){
elessair 0:f269e3021894 335
elessair 0:f269e3021894 336 oneByteWrite(ADXL345_FIFO_STATUS, settings);
elessair 0:f269e3021894 337
elessair 0:f269e3021894 338 }
elessair 0:f269e3021894 339
elessair 0:f269e3021894 340 int ADXL345::getFifoStatus(void){
elessair 0:f269e3021894 341
elessair 0:f269e3021894 342 return oneByteRead(ADXL345_FIFO_STATUS);
elessair 0:f269e3021894 343
elessair 0:f269e3021894 344 }
elessair 0:f269e3021894 345
elessair 0:f269e3021894 346 int ADXL345::oneByteRead(int address) {
elessair 0:f269e3021894 347
elessair 0:f269e3021894 348 int tx = (ADXL345_SPI_READ | (address & 0x3F));
elessair 0:f269e3021894 349 int rx = 0;
elessair 0:f269e3021894 350
elessair 0:f269e3021894 351 nCS_ = 0;
elessair 0:f269e3021894 352 //Send address to read from.
elessair 0:f269e3021894 353 spi_.write(tx);
elessair 0:f269e3021894 354 //Read back contents of address.
elessair 0:f269e3021894 355 rx = spi_.write(0x00);
elessair 0:f269e3021894 356 nCS_ = 1;
elessair 0:f269e3021894 357
elessair 0:f269e3021894 358 return rx;
elessair 0:f269e3021894 359
elessair 0:f269e3021894 360 }
elessair 0:f269e3021894 361
elessair 0:f269e3021894 362 void ADXL345::oneByteWrite(int address, char data) {
elessair 0:f269e3021894 363
elessair 0:f269e3021894 364 int tx = (ADXL345_SPI_WRITE | (address & 0x3F));
elessair 0:f269e3021894 365
elessair 0:f269e3021894 366 nCS_ = 0;
elessair 0:f269e3021894 367 //Send address to write to.
elessair 0:f269e3021894 368 spi_.write(tx);
elessair 0:f269e3021894 369 //Send data to be written.
elessair 0:f269e3021894 370 spi_.write(data);
elessair 0:f269e3021894 371 nCS_ = 1;
elessair 0:f269e3021894 372
elessair 0:f269e3021894 373 }
elessair 0:f269e3021894 374
elessair 0:f269e3021894 375 void ADXL345::multiByteRead(int startAddress, char* buffer, int size) {
elessair 0:f269e3021894 376
elessair 0:f269e3021894 377 int tx = (ADXL345_SPI_READ | ADXL345_MULTI_BYTE | (startAddress & 0x3F));
elessair 0:f269e3021894 378
elessair 0:f269e3021894 379 nCS_ = 0;
elessair 0:f269e3021894 380 //Send address to start reading from.
elessair 0:f269e3021894 381 spi_.write(tx);
elessair 0:f269e3021894 382
elessair 0:f269e3021894 383 for (int i = 0; i < size; i++) {
elessair 0:f269e3021894 384 buffer[i] = spi_.write(0x00);
elessair 0:f269e3021894 385 }
elessair 0:f269e3021894 386
elessair 0:f269e3021894 387 nCS_ = 1;
elessair 0:f269e3021894 388
elessair 0:f269e3021894 389 }
elessair 0:f269e3021894 390
elessair 0:f269e3021894 391 void ADXL345::multiByteWrite(int startAddress, char* buffer, int size) {
elessair 0:f269e3021894 392
elessair 0:f269e3021894 393 int tx = (ADXL345_SPI_WRITE | ADXL345_MULTI_BYTE | (startAddress & 0x3F));
elessair 0:f269e3021894 394
elessair 0:f269e3021894 395 nCS_ = 0;
elessair 0:f269e3021894 396 //Send address to start reading from.
elessair 0:f269e3021894 397 spi_.write(tx);
elessair 0:f269e3021894 398
elessair 0:f269e3021894 399 for (int i = 0; i < size; i++) {
elessair 0:f269e3021894 400 buffer[i] = spi_.write(0x00);
elessair 0:f269e3021894 401 }
elessair 0:f269e3021894 402
elessair 0:f269e3021894 403 nCS_ = 1;
elessair 0:f269e3021894 404
elessair 0:f269e3021894 405 }