this is just implement uart using nrf51 with adxl345 spi

Dependents:   nRF51_ADXL345_UART_SPI

Committer:
asyrofi
Date:
Thu Dec 07 06:30:16 2017 +0000
Revision:
0:cd40b5e776b0
siplah;

Who changed what in which revision?

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