ADXL345 triple axis, digital accelerometer library.

Dependents:   IMURover IMUfilter_RPYExample xbee_base 12_TCPIP ... more

Committer:
aberk
Date:
Mon Sep 06 13:53:03 2010 +0000
Revision:
0:bd8f0f20f433
Version 1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aberk 0:bd8f0f20f433 1 /**
aberk 0:bd8f0f20f433 2 * @author Aaron Berk
aberk 0:bd8f0f20f433 3 *
aberk 0:bd8f0f20f433 4 * @section LICENSE
aberk 0:bd8f0f20f433 5 *
aberk 0:bd8f0f20f433 6 * Copyright (c) 2010 ARM Limited
aberk 0:bd8f0f20f433 7 *
aberk 0:bd8f0f20f433 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
aberk 0:bd8f0f20f433 9 * of this software and associated documentation files (the "Software"), to deal
aberk 0:bd8f0f20f433 10 * in the Software without restriction, including without limitation the rights
aberk 0:bd8f0f20f433 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
aberk 0:bd8f0f20f433 12 * copies of the Software, and to permit persons to whom the Software is
aberk 0:bd8f0f20f433 13 * furnished to do so, subject to the following conditions:
aberk 0:bd8f0f20f433 14 *
aberk 0:bd8f0f20f433 15 * The above copyright notice and this permission notice shall be included in
aberk 0:bd8f0f20f433 16 * all copies or substantial portions of the Software.
aberk 0:bd8f0f20f433 17 *
aberk 0:bd8f0f20f433 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
aberk 0:bd8f0f20f433 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
aberk 0:bd8f0f20f433 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
aberk 0:bd8f0f20f433 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
aberk 0:bd8f0f20f433 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
aberk 0:bd8f0f20f433 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
aberk 0:bd8f0f20f433 24 * THE SOFTWARE.
aberk 0:bd8f0f20f433 25 *
aberk 0:bd8f0f20f433 26 * @section DESCRIPTION
aberk 0:bd8f0f20f433 27 *
aberk 0:bd8f0f20f433 28 * ADXL345, triple axis, digital interface, accelerometer.
aberk 0:bd8f0f20f433 29 *
aberk 0:bd8f0f20f433 30 * Datasheet:
aberk 0:bd8f0f20f433 31 *
aberk 0:bd8f0f20f433 32 * http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf
aberk 0:bd8f0f20f433 33 */
aberk 0:bd8f0f20f433 34
aberk 0:bd8f0f20f433 35 #ifndef ADXL345_H
aberk 0:bd8f0f20f433 36 #define ADXL345_H
aberk 0:bd8f0f20f433 37
aberk 0:bd8f0f20f433 38 /**
aberk 0:bd8f0f20f433 39 * Includes
aberk 0:bd8f0f20f433 40 */
aberk 0:bd8f0f20f433 41 #include "mbed.h"
aberk 0:bd8f0f20f433 42
aberk 0:bd8f0f20f433 43 /**
aberk 0:bd8f0f20f433 44 * Defines
aberk 0:bd8f0f20f433 45 */
aberk 0:bd8f0f20f433 46 //Registers.
aberk 0:bd8f0f20f433 47 #define ADXL345_DEVID_REG 0x00
aberk 0:bd8f0f20f433 48 #define ADXL345_THRESH_TAP_REG 0x1D
aberk 0:bd8f0f20f433 49 #define ADXL345_OFSX_REG 0x1E
aberk 0:bd8f0f20f433 50 #define ADXL345_OFSY_REG 0x1F
aberk 0:bd8f0f20f433 51 #define ADXL345_OFSZ_REG 0x20
aberk 0:bd8f0f20f433 52 #define ADXL345_DUR_REG 0x21
aberk 0:bd8f0f20f433 53 #define ADXL345_LATENT_REG 0x22
aberk 0:bd8f0f20f433 54 #define ADXL345_WINDOW_REG 0x23
aberk 0:bd8f0f20f433 55 #define ADXL345_THRESH_ACT_REG 0x24
aberk 0:bd8f0f20f433 56 #define ADXL345_THRESH_INACT_REG 0x25
aberk 0:bd8f0f20f433 57 #define ADXL345_TIME_INACT_REG 0x26
aberk 0:bd8f0f20f433 58 #define ADXL345_ACT_INACT_CTL_REG 0x27
aberk 0:bd8f0f20f433 59 #define ADXL345_THRESH_FF_REG 0x28
aberk 0:bd8f0f20f433 60 #define ADXL345_TIME_FF_REG 0x29
aberk 0:bd8f0f20f433 61 #define ADXL345_TAP_AXES_REG 0x2A
aberk 0:bd8f0f20f433 62 #define ADXL345_ACT_TAP_STATUS_REG 0x2B
aberk 0:bd8f0f20f433 63 #define ADXL345_BW_RATE_REG 0x2C
aberk 0:bd8f0f20f433 64 #define ADXL345_POWER_CTL_REG 0x2D
aberk 0:bd8f0f20f433 65 #define ADXL345_INT_ENABLE_REG 0x2E
aberk 0:bd8f0f20f433 66 #define ADXL345_INT_MAP_REG 0x2F
aberk 0:bd8f0f20f433 67 #define ADXL345_INT_SOURCE_REG 0x30
aberk 0:bd8f0f20f433 68 #define ADXL345_DATA_FORMAT_REG 0x31
aberk 0:bd8f0f20f433 69 #define ADXL345_DATAX0_REG 0x32
aberk 0:bd8f0f20f433 70 #define ADXL345_DATAX1_REG 0x33
aberk 0:bd8f0f20f433 71 #define ADXL345_DATAY0_REG 0x34
aberk 0:bd8f0f20f433 72 #define ADXL345_DATAY1_REG 0x35
aberk 0:bd8f0f20f433 73 #define ADXL345_DATAZ0_REG 0x36
aberk 0:bd8f0f20f433 74 #define ADXL345_DATAZ1_REG 0x37
aberk 0:bd8f0f20f433 75 #define ADXL345_FIFO_CTL 0x38
aberk 0:bd8f0f20f433 76 #define ADXL345_FIFO_STATUS 0x39
aberk 0:bd8f0f20f433 77
aberk 0:bd8f0f20f433 78 //Data rate codes.
aberk 0:bd8f0f20f433 79 #define ADXL345_3200HZ 0x0F
aberk 0:bd8f0f20f433 80 #define ADXL345_1600HZ 0x0E
aberk 0:bd8f0f20f433 81 #define ADXL345_800HZ 0x0D
aberk 0:bd8f0f20f433 82 #define ADXL345_400HZ 0x0C
aberk 0:bd8f0f20f433 83 #define ADXL345_200HZ 0x0B
aberk 0:bd8f0f20f433 84 #define ADXL345_100HZ 0x0A
aberk 0:bd8f0f20f433 85 #define ADXL345_50HZ 0x09
aberk 0:bd8f0f20f433 86 #define ADXL345_25HZ 0x08
aberk 0:bd8f0f20f433 87 #define ADXL345_12HZ5 0x07
aberk 0:bd8f0f20f433 88 #define ADXL345_6HZ25 0x06
aberk 0:bd8f0f20f433 89
aberk 0:bd8f0f20f433 90 #define ADXL345_SPI_READ 0x80
aberk 0:bd8f0f20f433 91 #define ADXL345_SPI_WRITE 0x00
aberk 0:bd8f0f20f433 92 #define ADXL345_MULTI_BYTE 0x60
aberk 0:bd8f0f20f433 93
aberk 0:bd8f0f20f433 94 #define ADXL345_X 0x00
aberk 0:bd8f0f20f433 95 #define ADXL345_Y 0x01
aberk 0:bd8f0f20f433 96 #define ADXL345_Z 0x02
aberk 0:bd8f0f20f433 97
aberk 0:bd8f0f20f433 98 /**
aberk 0:bd8f0f20f433 99 * ADXL345 triple axis, digital interface, accelerometer.
aberk 0:bd8f0f20f433 100 */
aberk 0:bd8f0f20f433 101 class ADXL345 {
aberk 0:bd8f0f20f433 102
aberk 0:bd8f0f20f433 103 public:
aberk 0:bd8f0f20f433 104
aberk 0:bd8f0f20f433 105 /**
aberk 0:bd8f0f20f433 106 * Constructor.
aberk 0:bd8f0f20f433 107 *
aberk 0:bd8f0f20f433 108 * @param mosi mbed pin to use for MOSI line of SPI interface.
aberk 0:bd8f0f20f433 109 * @param miso mbed pin to use for MISO line of SPI interface.
aberk 0:bd8f0f20f433 110 * @param sck mbed pin to use for SCK line of SPI interface.
aberk 0:bd8f0f20f433 111 * @param cs mbed pin to use for not chip select line of SPI interface.
aberk 0:bd8f0f20f433 112 */
aberk 0:bd8f0f20f433 113 ADXL345(PinName mosi, PinName miso, PinName sck, PinName cs);
aberk 0:bd8f0f20f433 114
aberk 0:bd8f0f20f433 115 /**
aberk 0:bd8f0f20f433 116 * Read the device ID register on the device.
aberk 0:bd8f0f20f433 117 *
aberk 0:bd8f0f20f433 118 * @return The device ID code [0xE5]
aberk 0:bd8f0f20f433 119 */
aberk 0:bd8f0f20f433 120 int getDevId(void);
aberk 0:bd8f0f20f433 121
aberk 0:bd8f0f20f433 122 /**
aberk 0:bd8f0f20f433 123 * Read the tap threshold on the device.
aberk 0:bd8f0f20f433 124 *
aberk 0:bd8f0f20f433 125 * @return The tap threshold as an 8-bit number with a scale factor of
aberk 0:bd8f0f20f433 126 * 62.5mg/LSB.
aberk 0:bd8f0f20f433 127 */
aberk 0:bd8f0f20f433 128 int getTapThreshold(void);
aberk 0:bd8f0f20f433 129
aberk 0:bd8f0f20f433 130 /**
aberk 0:bd8f0f20f433 131 * Set the tap threshold.
aberk 0:bd8f0f20f433 132 *
aberk 0:bd8f0f20f433 133 * @param The tap threshold as an 8-bit number with a scale factor of
aberk 0:bd8f0f20f433 134 * 62.5mg/LSB.
aberk 0:bd8f0f20f433 135 */
aberk 0:bd8f0f20f433 136 void setTapThreshold(int threshold);
aberk 0:bd8f0f20f433 137
aberk 0:bd8f0f20f433 138 /**
aberk 0:bd8f0f20f433 139 * Get the current offset for a particular axis.
aberk 0:bd8f0f20f433 140 *
aberk 0:bd8f0f20f433 141 * @param axis 0x00 -> X-axis
aberk 0:bd8f0f20f433 142 * 0x01 -> Y-axis
aberk 0:bd8f0f20f433 143 * 0x02 -> Z-axis
aberk 0:bd8f0f20f433 144 * @return The current offset as an 8-bit 2's complement number with scale
aberk 0:bd8f0f20f433 145 * factor 15.6mg/LSB.
aberk 0:bd8f0f20f433 146 */
aberk 0:bd8f0f20f433 147 int getOffset(int axis);
aberk 0:bd8f0f20f433 148
aberk 0:bd8f0f20f433 149 /**
aberk 0:bd8f0f20f433 150 * Set the offset for a particular axis.
aberk 0:bd8f0f20f433 151 *
aberk 0:bd8f0f20f433 152 * @param axis 0x00 -> X-axis
aberk 0:bd8f0f20f433 153 * 0x01 -> Y-axis
aberk 0:bd8f0f20f433 154 * 0x02 -> Z-axis
aberk 0:bd8f0f20f433 155 * @param offset The offset as an 8-bit 2's complement number with scale
aberk 0:bd8f0f20f433 156 * factor 15.6mg/LSB.
aberk 0:bd8f0f20f433 157 */
aberk 0:bd8f0f20f433 158 void setOffset(int axis, char offset);
aberk 0:bd8f0f20f433 159
aberk 0:bd8f0f20f433 160 /**
aberk 0:bd8f0f20f433 161 * Get the tap duration required to trigger an event.
aberk 0:bd8f0f20f433 162 *
aberk 0:bd8f0f20f433 163 * @return The max time that an event must be above the tap threshold to
aberk 0:bd8f0f20f433 164 * qualify as a tap event, in microseconds.
aberk 0:bd8f0f20f433 165 */
aberk 0:bd8f0f20f433 166 int getTapDuration(void);
aberk 0:bd8f0f20f433 167
aberk 0:bd8f0f20f433 168 /**
aberk 0:bd8f0f20f433 169 * Set the tap duration required to trigger an event.
aberk 0:bd8f0f20f433 170 *
aberk 0:bd8f0f20f433 171 * @param duration_us The max time that an event must be above the tap
aberk 0:bd8f0f20f433 172 * threshold to qualify as a tap event, in microseconds.
aberk 0:bd8f0f20f433 173 * Time will be normalized by the scale factor which is
aberk 0:bd8f0f20f433 174 * 625us/LSB. A value of 0 disables the single/double
aberk 0:bd8f0f20f433 175 * tap functions.
aberk 0:bd8f0f20f433 176 */
aberk 0:bd8f0f20f433 177 void setTapDuration(int duration_us);
aberk 0:bd8f0f20f433 178
aberk 0:bd8f0f20f433 179 /**
aberk 0:bd8f0f20f433 180 * Get the tap latency between the detection of a tap and the time window.
aberk 0:bd8f0f20f433 181 *
aberk 0:bd8f0f20f433 182 * @return The wait time from the detection of a tap event to the start of
aberk 0:bd8f0f20f433 183 * the time window during which a possible second tap event can be
aberk 0:bd8f0f20f433 184 * detected in milliseconds.
aberk 0:bd8f0f20f433 185 */
aberk 0:bd8f0f20f433 186 float getTapLatency(void);
aberk 0:bd8f0f20f433 187
aberk 0:bd8f0f20f433 188 /**
aberk 0:bd8f0f20f433 189 * Set the tap latency between the detection of a tap and the time window.
aberk 0:bd8f0f20f433 190 *
aberk 0:bd8f0f20f433 191 * @param latency_ms The wait time from the detection of a tap event to the
aberk 0:bd8f0f20f433 192 * start of the time window during which a possible
aberk 0:bd8f0f20f433 193 * second tap event can be detected in milliseconds.
aberk 0:bd8f0f20f433 194 * A value of 0 disables the double tap function.
aberk 0:bd8f0f20f433 195 */
aberk 0:bd8f0f20f433 196 void setTapLatency(int latency_ms);
aberk 0:bd8f0f20f433 197
aberk 0:bd8f0f20f433 198 /**
aberk 0:bd8f0f20f433 199 * Get the time of window between tap latency and a double tap.
aberk 0:bd8f0f20f433 200 *
aberk 0:bd8f0f20f433 201 * @return The amount of time after the expiration of the latency time
aberk 0:bd8f0f20f433 202 * during which a second valid tap can begin, in milliseconds.
aberk 0:bd8f0f20f433 203 */
aberk 0:bd8f0f20f433 204 float getWindowTime(void);
aberk 0:bd8f0f20f433 205
aberk 0:bd8f0f20f433 206 /**
aberk 0:bd8f0f20f433 207 * Set the time of the window between tap latency and a double tap.
aberk 0:bd8f0f20f433 208 *
aberk 0:bd8f0f20f433 209 * @param window_ms The amount of time after the expiration of the latency
aberk 0:bd8f0f20f433 210 * time during which a second valid tap can begin,
aberk 0:bd8f0f20f433 211 * in milliseconds.
aberk 0:bd8f0f20f433 212 */
aberk 0:bd8f0f20f433 213 void setWindowTime(int window_ms);
aberk 0:bd8f0f20f433 214
aberk 0:bd8f0f20f433 215 /**
aberk 0:bd8f0f20f433 216 * Get the threshold value for detecting activity.
aberk 0:bd8f0f20f433 217 *
aberk 0:bd8f0f20f433 218 * @return The threshold value for detecting activity as an 8-bit number.
aberk 0:bd8f0f20f433 219 * Scale factor is 62.5mg/LSB.
aberk 0:bd8f0f20f433 220 */
aberk 0:bd8f0f20f433 221 int getActivityThreshold(void);
aberk 0:bd8f0f20f433 222
aberk 0:bd8f0f20f433 223 /**
aberk 0:bd8f0f20f433 224 * Set the threshold value for detecting activity.
aberk 0:bd8f0f20f433 225 *
aberk 0:bd8f0f20f433 226 * @param threshold The threshold value for detecting activity as an 8-bit
aberk 0:bd8f0f20f433 227 * number. Scale factor is 62.5mg/LSB. A value of 0 may
aberk 0:bd8f0f20f433 228 * result in undesirable behavior if the activity
aberk 0:bd8f0f20f433 229 * interrupt is enabled.
aberk 0:bd8f0f20f433 230 */
aberk 0:bd8f0f20f433 231 void setActivityThreshold(int threshold);
aberk 0:bd8f0f20f433 232
aberk 0:bd8f0f20f433 233 /**
aberk 0:bd8f0f20f433 234 * Get the threshold value for detecting inactivity.
aberk 0:bd8f0f20f433 235 *
aberk 0:bd8f0f20f433 236 * @return The threshold value for detecting inactivity as an 8-bit number.
aberk 0:bd8f0f20f433 237 * Scale factor is 62.5mg/LSB.
aberk 0:bd8f0f20f433 238 */
aberk 0:bd8f0f20f433 239 int getInactivityThreshold(void);
aberk 0:bd8f0f20f433 240
aberk 0:bd8f0f20f433 241 /**
aberk 0:bd8f0f20f433 242 * Set the threshold value for detecting inactivity.
aberk 0:bd8f0f20f433 243 *
aberk 0:bd8f0f20f433 244 * @param threshold The threshold value for detecting inactivity as an
aberk 0:bd8f0f20f433 245 * 8-bit number. Scale factor is 62.5mg/LSB.
aberk 0:bd8f0f20f433 246 */
aberk 0:bd8f0f20f433 247 void setInactivityThreshold(int threshold);
aberk 0:bd8f0f20f433 248
aberk 0:bd8f0f20f433 249 /**
aberk 0:bd8f0f20f433 250 * Get the time required for inactivity to be declared.
aberk 0:bd8f0f20f433 251 *
aberk 0:bd8f0f20f433 252 * @return The amount of time that acceleration must be less than the
aberk 0:bd8f0f20f433 253 * inactivity threshold for inactivity to be declared, in
aberk 0:bd8f0f20f433 254 * seconds.
aberk 0:bd8f0f20f433 255 */
aberk 0:bd8f0f20f433 256 int getTimeInactivity(void);
aberk 0:bd8f0f20f433 257
aberk 0:bd8f0f20f433 258 /**
aberk 0:bd8f0f20f433 259 * Set the time required for inactivity to be declared.
aberk 0:bd8f0f20f433 260 *
aberk 0:bd8f0f20f433 261 * @param inactivity The amount of time that acceleration must be less than
aberk 0:bd8f0f20f433 262 * the inactivity threshold for inactivity to be
aberk 0:bd8f0f20f433 263 * declared, in seconds. A value of 0 results in an
aberk 0:bd8f0f20f433 264 * interrupt when the output data is less than the
aberk 0:bd8f0f20f433 265 * threshold inactivity.
aberk 0:bd8f0f20f433 266 */
aberk 0:bd8f0f20f433 267 void setTimeInactivity(int timeInactivity);
aberk 0:bd8f0f20f433 268
aberk 0:bd8f0f20f433 269 /**
aberk 0:bd8f0f20f433 270 * Get the activity/inactivity control settings.
aberk 0:bd8f0f20f433 271 *
aberk 0:bd8f0f20f433 272 * D7 D6 D5 D4
aberk 0:bd8f0f20f433 273 * +-----------+--------------+--------------+--------------+
aberk 0:bd8f0f20f433 274 * | ACT ac/dc | ACT_X enable | ACT_Y enable | ACT_Z enable |
aberk 0:bd8f0f20f433 275 * +-----------+--------------+--------------+--------------+
aberk 0:bd8f0f20f433 276 *
aberk 0:bd8f0f20f433 277 * D3 D2 D1 D0
aberk 0:bd8f0f20f433 278 * +-------------+----------------+----------------+----------------+
aberk 0:bd8f0f20f433 279 * | INACT ac/dc | INACT_X enable | INACT_Y enable | INACT_Z enable |
aberk 0:bd8f0f20f433 280 * +-------------+----------------+----------------+----------------+
aberk 0:bd8f0f20f433 281 *
aberk 0:bd8f0f20f433 282 * See datasheet for details.
aberk 0:bd8f0f20f433 283 *
aberk 0:bd8f0f20f433 284 * @return The contents of the ACT_INACT_CTL register.
aberk 0:bd8f0f20f433 285 */
aberk 0:bd8f0f20f433 286 int getActivityInactivityControl(void);
aberk 0:bd8f0f20f433 287
aberk 0:bd8f0f20f433 288 /**
aberk 0:bd8f0f20f433 289 * Set the activity/inactivity control settings.
aberk 0:bd8f0f20f433 290 *
aberk 0:bd8f0f20f433 291 * D7 D6 D5 D4
aberk 0:bd8f0f20f433 292 * +-----------+--------------+--------------+--------------+
aberk 0:bd8f0f20f433 293 * | ACT ac/dc | ACT_X enable | ACT_Y enable | ACT_Z enable |
aberk 0:bd8f0f20f433 294 * +-----------+--------------+--------------+--------------+
aberk 0:bd8f0f20f433 295 *
aberk 0:bd8f0f20f433 296 * D3 D2 D1 D0
aberk 0:bd8f0f20f433 297 * +-------------+----------------+----------------+----------------+
aberk 0:bd8f0f20f433 298 * | INACT ac/dc | INACT_X enable | INACT_Y enable | INACT_Z enable |
aberk 0:bd8f0f20f433 299 * +-------------+----------------+----------------+----------------+
aberk 0:bd8f0f20f433 300 *
aberk 0:bd8f0f20f433 301 * See datasheet for details.
aberk 0:bd8f0f20f433 302 *
aberk 0:bd8f0f20f433 303 * @param settings The control byte to write to the ACT_INACT_CTL register.
aberk 0:bd8f0f20f433 304 */
aberk 0:bd8f0f20f433 305 void setActivityInactivityControl(int settings);
aberk 0:bd8f0f20f433 306
aberk 0:bd8f0f20f433 307 /**
aberk 0:bd8f0f20f433 308 * Get the threshold for free fall detection.
aberk 0:bd8f0f20f433 309 *
aberk 0:bd8f0f20f433 310 * @return The threshold value for free-fall detection, as an 8-bit number,
aberk 0:bd8f0f20f433 311 * with scale factor 62.5mg/LSB.
aberk 0:bd8f0f20f433 312 */
aberk 0:bd8f0f20f433 313 int getFreefallThreshold(void);
aberk 0:bd8f0f20f433 314
aberk 0:bd8f0f20f433 315 /**
aberk 0:bd8f0f20f433 316 * Set the threshold for free fall detection.
aberk 0:bd8f0f20f433 317 *
aberk 0:bd8f0f20f433 318 * @return The threshold value for free-fall detection, as an 8-bit number,
aberk 0:bd8f0f20f433 319 * with scale factor 62.5mg/LSB. A value of 0 may result in
aberk 0:bd8f0f20f433 320 * undesirable behavior if the free-fall interrupt is enabled.
aberk 0:bd8f0f20f433 321 * Values between 300 mg and 600 mg (0x05 to 0x09) are recommended.
aberk 0:bd8f0f20f433 322 */
aberk 0:bd8f0f20f433 323 void setFreefallThreshold(int threshold);
aberk 0:bd8f0f20f433 324
aberk 0:bd8f0f20f433 325 /**
aberk 0:bd8f0f20f433 326 * Get the time required to generate a free fall interrupt.
aberk 0:bd8f0f20f433 327 *
aberk 0:bd8f0f20f433 328 * @return The minimum time that the value of all axes must be less than
aberk 0:bd8f0f20f433 329 * the freefall threshold to generate a free-fall interrupt, in
aberk 0:bd8f0f20f433 330 * milliseconds.
aberk 0:bd8f0f20f433 331 */
aberk 0:bd8f0f20f433 332 int getFreefallTime(void);
aberk 0:bd8f0f20f433 333
aberk 0:bd8f0f20f433 334 /**
aberk 0:bd8f0f20f433 335 * Set the time required to generate a free fall interrupt.
aberk 0:bd8f0f20f433 336 *
aberk 0:bd8f0f20f433 337 * @return The minimum time that the value of all axes must be less than
aberk 0:bd8f0f20f433 338 * the freefall threshold to generate a free-fall interrupt, in
aberk 0:bd8f0f20f433 339 * milliseconds. A value of 0 may result in undesirable behavior
aberk 0:bd8f0f20f433 340 * if the free-fall interrupt is enabled. Values between 100 ms
aberk 0:bd8f0f20f433 341 * and 350 ms (0x14 to 0x46) are recommended.
aberk 0:bd8f0f20f433 342 */
aberk 0:bd8f0f20f433 343 void setFreefallTime(int freefallTime_ms);
aberk 0:bd8f0f20f433 344
aberk 0:bd8f0f20f433 345 /**
aberk 0:bd8f0f20f433 346 * Get the axis tap settings.
aberk 0:bd8f0f20f433 347 *
aberk 0:bd8f0f20f433 348 * D3 D2 D1 D0
aberk 0:bd8f0f20f433 349 * +----------+--------------+--------------+--------------+
aberk 0:bd8f0f20f433 350 * | Suppress | TAP_X enable | TAP_Y enable | TAP_Z enable |
aberk 0:bd8f0f20f433 351 * +----------+--------------+--------------+--------------+
aberk 0:bd8f0f20f433 352 *
aberk 0:bd8f0f20f433 353 * (D7-D4 are 0s).
aberk 0:bd8f0f20f433 354 *
aberk 0:bd8f0f20f433 355 * See datasheet for more details.
aberk 0:bd8f0f20f433 356 *
aberk 0:bd8f0f20f433 357 * @return The contents of the TAP_AXES register.
aberk 0:bd8f0f20f433 358 */
aberk 0:bd8f0f20f433 359 int getTapAxisControl(void);
aberk 0:bd8f0f20f433 360
aberk 0:bd8f0f20f433 361 /**
aberk 0:bd8f0f20f433 362 * Set the axis tap settings.
aberk 0:bd8f0f20f433 363 *
aberk 0:bd8f0f20f433 364 * D3 D2 D1 D0
aberk 0:bd8f0f20f433 365 * +----------+--------------+--------------+--------------+
aberk 0:bd8f0f20f433 366 * | Suppress | TAP_X enable | TAP_Y enable | TAP_Z enable |
aberk 0:bd8f0f20f433 367 * +----------+--------------+--------------+--------------+
aberk 0:bd8f0f20f433 368 *
aberk 0:bd8f0f20f433 369 * (D7-D4 are 0s).
aberk 0:bd8f0f20f433 370 *
aberk 0:bd8f0f20f433 371 * See datasheet for more details.
aberk 0:bd8f0f20f433 372 *
aberk 0:bd8f0f20f433 373 * @param The control byte to write to the TAP_AXES register.
aberk 0:bd8f0f20f433 374 */
aberk 0:bd8f0f20f433 375 void setTapAxisControl(int settings);
aberk 0:bd8f0f20f433 376
aberk 0:bd8f0f20f433 377 /**
aberk 0:bd8f0f20f433 378 * Get the source of a tap.
aberk 0:bd8f0f20f433 379 *
aberk 0:bd8f0f20f433 380 * @return The contents of the ACT_TAP_STATUS register.
aberk 0:bd8f0f20f433 381 */
aberk 0:bd8f0f20f433 382 int getTapSource(void);
aberk 0:bd8f0f20f433 383
aberk 0:bd8f0f20f433 384 /**
aberk 0:bd8f0f20f433 385 * Set the power mode.
aberk 0:bd8f0f20f433 386 *
aberk 0:bd8f0f20f433 387 * @param mode 0 -> Normal operation.
aberk 0:bd8f0f20f433 388 * 1 -> Reduced power operation.
aberk 0:bd8f0f20f433 389 */
aberk 0:bd8f0f20f433 390 void setPowerMode(char mode);
aberk 0:bd8f0f20f433 391
aberk 0:bd8f0f20f433 392 /**
aberk 0:bd8f0f20f433 393 * Set the data rate.
aberk 0:bd8f0f20f433 394 *
aberk 0:bd8f0f20f433 395 * @param rate The rate code (see #defines or datasheet).
aberk 0:bd8f0f20f433 396 */
aberk 0:bd8f0f20f433 397 void setDataRate(int rate);
aberk 0:bd8f0f20f433 398
aberk 0:bd8f0f20f433 399 /**
aberk 0:bd8f0f20f433 400 * Get the power control settings.
aberk 0:bd8f0f20f433 401 *
aberk 0:bd8f0f20f433 402 * See datasheet for details.
aberk 0:bd8f0f20f433 403 *
aberk 0:bd8f0f20f433 404 * @return The contents of the POWER_CTL register.
aberk 0:bd8f0f20f433 405 */
aberk 0:bd8f0f20f433 406 int getPowerControl(void);
aberk 0:bd8f0f20f433 407
aberk 0:bd8f0f20f433 408 /**
aberk 0:bd8f0f20f433 409 * Set the power control settings.
aberk 0:bd8f0f20f433 410 *
aberk 0:bd8f0f20f433 411 * See datasheet for details.
aberk 0:bd8f0f20f433 412 *
aberk 0:bd8f0f20f433 413 * @param The control byte to write to the POWER_CTL register.
aberk 0:bd8f0f20f433 414 */
aberk 0:bd8f0f20f433 415 void setPowerControl(int settings);
aberk 0:bd8f0f20f433 416
aberk 0:bd8f0f20f433 417 /**
aberk 0:bd8f0f20f433 418 * Get the interrupt enable settings.
aberk 0:bd8f0f20f433 419 *
aberk 0:bd8f0f20f433 420 * @return The contents of the INT_ENABLE register.
aberk 0:bd8f0f20f433 421 */
aberk 0:bd8f0f20f433 422 int getInterruptEnableControl(void);
aberk 0:bd8f0f20f433 423
aberk 0:bd8f0f20f433 424 /**
aberk 0:bd8f0f20f433 425 * Set the interrupt enable settings.
aberk 0:bd8f0f20f433 426 *
aberk 0:bd8f0f20f433 427 * @param settings The control byte to write to the INT_ENABLE register.
aberk 0:bd8f0f20f433 428 */
aberk 0:bd8f0f20f433 429 void setInterruptEnableControl(int settings);
aberk 0:bd8f0f20f433 430
aberk 0:bd8f0f20f433 431 /**
aberk 0:bd8f0f20f433 432 * Get the interrupt mapping settings.
aberk 0:bd8f0f20f433 433 *
aberk 0:bd8f0f20f433 434 * @return The contents of the INT_MAP register.
aberk 0:bd8f0f20f433 435 */
aberk 0:bd8f0f20f433 436 int getInterruptMappingControl(void);
aberk 0:bd8f0f20f433 437
aberk 0:bd8f0f20f433 438 /**
aberk 0:bd8f0f20f433 439 * Set the interrupt mapping settings.
aberk 0:bd8f0f20f433 440 *
aberk 0:bd8f0f20f433 441 * @param settings The control byte to write to the INT_MAP register.
aberk 0:bd8f0f20f433 442 */
aberk 0:bd8f0f20f433 443 void setInterruptMappingControl(int settings);
aberk 0:bd8f0f20f433 444
aberk 0:bd8f0f20f433 445 /**
aberk 0:bd8f0f20f433 446 * Get the interrupt source.
aberk 0:bd8f0f20f433 447 *
aberk 0:bd8f0f20f433 448 * @return The contents of the INT_SOURCE register.
aberk 0:bd8f0f20f433 449 */
aberk 0:bd8f0f20f433 450 int getInterruptSource(void);
aberk 0:bd8f0f20f433 451
aberk 0:bd8f0f20f433 452 /**
aberk 0:bd8f0f20f433 453 * Get the data format settings.
aberk 0:bd8f0f20f433 454 *
aberk 0:bd8f0f20f433 455 * @return The contents of the DATA_FORMAT register.
aberk 0:bd8f0f20f433 456 */
aberk 0:bd8f0f20f433 457 int getDataFormatControl(void);
aberk 0:bd8f0f20f433 458
aberk 0:bd8f0f20f433 459 /**
aberk 0:bd8f0f20f433 460 * Set the data format settings.
aberk 0:bd8f0f20f433 461 *
aberk 0:bd8f0f20f433 462 * @param settings The control byte to write to the DATA_FORMAT register.
aberk 0:bd8f0f20f433 463 */
aberk 0:bd8f0f20f433 464 void setDataFormatControl(int settings);
aberk 0:bd8f0f20f433 465
aberk 0:bd8f0f20f433 466 /**
aberk 0:bd8f0f20f433 467 * Get the output of all three axes.
aberk 0:bd8f0f20f433 468 *
aberk 0:bd8f0f20f433 469 * @param Pointer to a buffer to hold the accelerometer value for the
aberk 0:bd8f0f20f433 470 * x-axis, y-axis and z-axis [in that order].
aberk 0:bd8f0f20f433 471 */
aberk 0:bd8f0f20f433 472 void getOutput(int* readings);
aberk 0:bd8f0f20f433 473
aberk 0:bd8f0f20f433 474 /**
aberk 0:bd8f0f20f433 475 * Get the FIFO control settings.
aberk 0:bd8f0f20f433 476 *
aberk 0:bd8f0f20f433 477 * @return The contents of the FIFO_CTL register.
aberk 0:bd8f0f20f433 478 */
aberk 0:bd8f0f20f433 479 int getFifoControl(void);
aberk 0:bd8f0f20f433 480
aberk 0:bd8f0f20f433 481 /**
aberk 0:bd8f0f20f433 482 * Set the FIFO control settings.
aberk 0:bd8f0f20f433 483 *
aberk 0:bd8f0f20f433 484 * @param The control byte to write to the FIFO_CTL register.
aberk 0:bd8f0f20f433 485 */
aberk 0:bd8f0f20f433 486 void setFifoControl(int settings);
aberk 0:bd8f0f20f433 487
aberk 0:bd8f0f20f433 488 /**
aberk 0:bd8f0f20f433 489 * Get FIFO status.
aberk 0:bd8f0f20f433 490 *
aberk 0:bd8f0f20f433 491 * @return The contents of the FIFO_STATUS register.
aberk 0:bd8f0f20f433 492 */
aberk 0:bd8f0f20f433 493 int getFifoStatus(void);
aberk 0:bd8f0f20f433 494
aberk 0:bd8f0f20f433 495 private:
aberk 0:bd8f0f20f433 496
aberk 0:bd8f0f20f433 497 SPI spi_;
aberk 0:bd8f0f20f433 498 DigitalOut nCS_;
aberk 0:bd8f0f20f433 499
aberk 0:bd8f0f20f433 500 /**
aberk 0:bd8f0f20f433 501 * Read one byte from a register on the device.
aberk 0:bd8f0f20f433 502 *
aberk 0:bd8f0f20f433 503 * @param address Address of the register to read.
aberk 0:bd8f0f20f433 504 *
aberk 0:bd8f0f20f433 505 * @return The contents of the register address.
aberk 0:bd8f0f20f433 506 */
aberk 0:bd8f0f20f433 507 int oneByteRead(int address);
aberk 0:bd8f0f20f433 508
aberk 0:bd8f0f20f433 509 /**
aberk 0:bd8f0f20f433 510 * Write one byte to a register on the device.
aberk 0:bd8f0f20f433 511 *
aberk 0:bd8f0f20f433 512 * @param address Address of the register to write to.
aberk 0:bd8f0f20f433 513 * @param data The data to write into the register.
aberk 0:bd8f0f20f433 514 */
aberk 0:bd8f0f20f433 515 void oneByteWrite(int address, char data);
aberk 0:bd8f0f20f433 516
aberk 0:bd8f0f20f433 517 /**
aberk 0:bd8f0f20f433 518 * Read several consecutive bytes on the device.
aberk 0:bd8f0f20f433 519 *
aberk 0:bd8f0f20f433 520 * @param startAddress The address of the first register to read from.
aberk 0:bd8f0f20f433 521 * @param buffer Pointer to a buffer to store data read from the device.
aberk 0:bd8f0f20f433 522 * @param size The number of bytes to read.
aberk 0:bd8f0f20f433 523 */
aberk 0:bd8f0f20f433 524 void multiByteRead(int startAddress, char* buffer, int size);
aberk 0:bd8f0f20f433 525
aberk 0:bd8f0f20f433 526 /**
aberk 0:bd8f0f20f433 527 * Write several consecutive bytes on the device.
aberk 0:bd8f0f20f433 528 *
aberk 0:bd8f0f20f433 529 * @param startAddress The address of the first register to write to.
aberk 0:bd8f0f20f433 530 * @param buffer Pointer to a buffer which contains the data to write.
aberk 0:bd8f0f20f433 531 * @param size The number of bytes to write.
aberk 0:bd8f0f20f433 532 */
aberk 0:bd8f0f20f433 533 void multiByteWrite(int startAddress, char* buffer, int size);
aberk 0:bd8f0f20f433 534
aberk 0:bd8f0f20f433 535 };
aberk 0:bd8f0f20f433 536
aberk 0:bd8f0f20f433 537 #endif /* ADXL345_H */