MMA8452

Dependents:   ECE2035_SUM17_Project

Committer:
kennyainny
Date:
Fri Jul 14 21:42:37 2017 +0000
Revision:
0:79012a2fa4d1
no changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kennyainny 0:79012a2fa4d1 1 #pragma once
kennyainny 0:79012a2fa4d1 2
kennyainny 0:79012a2fa4d1 3 // Authors: Ashley Mills, Nicholas Herriot
kennyainny 0:79012a2fa4d1 4 /* Copyright (c) 2013 Vodafone, MIT License
kennyainny 0:79012a2fa4d1 5 *
kennyainny 0:79012a2fa4d1 6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
kennyainny 0:79012a2fa4d1 7 * and associated documentation files (the "Software"), to deal in the Software without restriction,
kennyainny 0:79012a2fa4d1 8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
kennyainny 0:79012a2fa4d1 9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
kennyainny 0:79012a2fa4d1 10 * furnished to do so, subject to the following conditions:
kennyainny 0:79012a2fa4d1 11 *
kennyainny 0:79012a2fa4d1 12 * The above copyright notice and this permission notice shall be included in all copies or
kennyainny 0:79012a2fa4d1 13 * substantial portions of the Software.
kennyainny 0:79012a2fa4d1 14 *
kennyainny 0:79012a2fa4d1 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
kennyainny 0:79012a2fa4d1 16 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
kennyainny 0:79012a2fa4d1 17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
kennyainny 0:79012a2fa4d1 18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
kennyainny 0:79012a2fa4d1 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
kennyainny 0:79012a2fa4d1 20 */
kennyainny 0:79012a2fa4d1 21
kennyainny 0:79012a2fa4d1 22 // the SparkFun breakout board defaults to 1, set to 0 if SA0 jumper on the bottom of the board is set
kennyainny 0:79012a2fa4d1 23 // see the Table 10. I2C Device Address Sequence in Freescale MMA8452Q pdf
kennyainny 0:79012a2fa4d1 24
kennyainny 0:79012a2fa4d1 25 #include "mbed.h"
kennyainny 0:79012a2fa4d1 26
kennyainny 0:79012a2fa4d1 27 #define MMA8452_DEBUG 1
kennyainny 0:79012a2fa4d1 28
kennyainny 0:79012a2fa4d1 29 // More info on MCU Master address can be found on section 5.10.1 of http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=MMA8452Q
kennyainny 0:79012a2fa4d1 30 #define SA0 1
kennyainny 0:79012a2fa4d1 31 #if SA0
kennyainny 0:79012a2fa4d1 32 #define MMA8452_ADDRESS 0x3A // 0x1D<<1 // SA0 is high, 0x1C if low -
kennyainny 0:79012a2fa4d1 33 #else
kennyainny 0:79012a2fa4d1 34 #define MMA8452_ADDRESS 0x38 // 0x1C<<1
kennyainny 0:79012a2fa4d1 35 #endif
kennyainny 0:79012a2fa4d1 36
kennyainny 0:79012a2fa4d1 37 // Register descriptions found in section 6 of pdf
kennyainny 0:79012a2fa4d1 38 #define MMA8452_STATUS 0x00 // Type 'read' : Status of the data registers
kennyainny 0:79012a2fa4d1 39 #define MMA8452_OUT_X_MSB 0x01 // Type 'read' : x axis - MSB of 2 byte sample
kennyainny 0:79012a2fa4d1 40 #define MMA8452_OUT_X_LSB 0x02 // Type 'read' : x axis - LSB of 2 byte sample
kennyainny 0:79012a2fa4d1 41 #define MMA8452_OUT_Y_MSB 0x03 // Type 'read' : y axis - MSB of 2 byte sample
kennyainny 0:79012a2fa4d1 42 #define MMA8452_OUT_Y_LSB 0x04 // Type 'read' : y axis - LSB of 2 byte sample
kennyainny 0:79012a2fa4d1 43 #define MMA8452_OUT_Z_MSB 0x05 // Type 'read' : z axis - MSB of 2 byte sample
kennyainny 0:79012a2fa4d1 44 #define MMA8452_OUT_Z_LSB 0x06 // Type 'read' : z axis - LSB of 2 byte sample
kennyainny 0:79012a2fa4d1 45
kennyainny 0:79012a2fa4d1 46 // register definitions
kennyainny 0:79012a2fa4d1 47 #define MMA8452_XYZ_DATA_CFG 0x0E
kennyainny 0:79012a2fa4d1 48
kennyainny 0:79012a2fa4d1 49 #define MMA8452_SYSMOD 0x0B // Type 'read' : This tells you if device is active, sleep or standy 0x00=STANDBY 0x01=WAKE 0x02=SLEEP
kennyainny 0:79012a2fa4d1 50 #define MMA8452_WHO_AM_I 0x0D // Type 'read' : This should return the device id of 0x2A
kennyainny 0:79012a2fa4d1 51
kennyainny 0:79012a2fa4d1 52 #define MMA8452_PL_STATUS 0x10 // Type 'read' : This shows portrait landscape mode orientation
kennyainny 0:79012a2fa4d1 53 #define MMA8452_PL_CFG 0x11 // Type 'read/write' : This allows portrait landscape configuration
kennyainny 0:79012a2fa4d1 54 #define MMA8452_PL_COUNT 0x12 // Type 'read' : This is the portraint landscape debounce counter
kennyainny 0:79012a2fa4d1 55 #define MMA8452_PL_BF_ZCOMP 0x13 // Type 'read' :
kennyainny 0:79012a2fa4d1 56 #define MMA8452_PL_THS_REG 0x14 // Type 'read' :
kennyainny 0:79012a2fa4d1 57
kennyainny 0:79012a2fa4d1 58 #define MMA8452_FF_MT_CFG 0X15 // Type 'read/write' : Freefaul motion functional block configuration
kennyainny 0:79012a2fa4d1 59 #define MMA8452_FF_MT_SRC 0X16 // Type 'read' : Freefaul motion event source register
kennyainny 0:79012a2fa4d1 60 #define MMA8452_FF_MT_THS 0X17 // Type 'read' : Freefaul motion threshold register
kennyainny 0:79012a2fa4d1 61 #define MMA8452_FF_COUNT 0X18 // Type 'read' : Freefaul motion debouce counter
kennyainny 0:79012a2fa4d1 62
kennyainny 0:79012a2fa4d1 63 #define MMA8452_ASLP_COUNT 0x29 // Type 'read/write' : Counter settings for auto sleep
kennyainny 0:79012a2fa4d1 64 #define MMA8452_CTRL_REG_1 0x2A // Type 'read/write' :
kennyainny 0:79012a2fa4d1 65 #define MMA8452_CTRL_REG_2 0x2B // Type 'read/write' :
kennyainny 0:79012a2fa4d1 66 #define MMA8452_CTRL_REG_3 0x2C // Type 'read/write' :
kennyainny 0:79012a2fa4d1 67 #define MMA8452_CTRL_REG_4 0x2D // Type 'read/write' :
kennyainny 0:79012a2fa4d1 68 #define MMA8452_CTRL_REG_5 0x2E // Type 'read/write' :
kennyainny 0:79012a2fa4d1 69
kennyainny 0:79012a2fa4d1 70 // Defined in table 13 of the Freescale PDF
kennyainny 0:79012a2fa4d1 71 /// xxx these all need to have better names
kennyainny 0:79012a2fa4d1 72 #define STANDBY 0x00 // State value returned after a SYSMOD request, it can be in state STANDBY, WAKE or SLEEP
kennyainny 0:79012a2fa4d1 73 #define WAKE 0x01 // State value returned after a SYSMOD request, it can be in state STANDBY, WAKE or SLEEP
kennyainny 0:79012a2fa4d1 74 #define SLEEP 0x02 // State value returned after a SYSMOD request, it can be in state STANDBY, WAKE or SLEEP
kennyainny 0:79012a2fa4d1 75 #define ACTIVE 0x01 // Stage value returned and set in Control Register 1, it can be STANDBY=00, or ACTIVE=01
kennyainny 0:79012a2fa4d1 76
kennyainny 0:79012a2fa4d1 77 #define TILT_STATUS 0x03 // Tilt Status (Read only)
kennyainny 0:79012a2fa4d1 78 #define SRST_STATUS 0x04 // Sample Rate Status Register (Read only)
kennyainny 0:79012a2fa4d1 79 #define SPCNT_STATUS 0x05 // Sleep Count Register (Read/Write)
kennyainny 0:79012a2fa4d1 80 #define INTSU_STATUS 0x06 // Interrupt Setup Register
kennyainny 0:79012a2fa4d1 81 #define MODE_STATUS 0x07 // Mode Register (Read/Write)
kennyainny 0:79012a2fa4d1 82 #define SR_STATUS 0x08 // Auto-Wake and Active Mode Portrait/Landscape Samples per Seconds Register (Read/Write)
kennyainny 0:79012a2fa4d1 83 #define PDET_STATUS 0x09 // Tap/Pulse Detection Register (Read/Write)
kennyainny 0:79012a2fa4d1 84 #define PD_STATUS 0xA // Tap/Pulse Debounce Count Register (Read/Write)
kennyainny 0:79012a2fa4d1 85
kennyainny 0:79012a2fa4d1 86 // masks for enabling/disabling standby
kennyainny 0:79012a2fa4d1 87 #define MMA8452_ACTIVE_MASK 0x01
kennyainny 0:79012a2fa4d1 88 #define MMA8452_STANDBY_MASK 0xFE
kennyainny 0:79012a2fa4d1 89
kennyainny 0:79012a2fa4d1 90 // mask for dynamic range reading and writing
kennyainny 0:79012a2fa4d1 91 #define MMA8452_DYNAMIC_RANGE_MASK 0xFC
kennyainny 0:79012a2fa4d1 92
kennyainny 0:79012a2fa4d1 93 // mask and shift for data rate reading and writing
kennyainny 0:79012a2fa4d1 94 #define MMA8452_DATA_RATE_MASK 0xC7
kennyainny 0:79012a2fa4d1 95 #define MMA8452_DATA_RATE_MASK_SHIFT 0x03
kennyainny 0:79012a2fa4d1 96
kennyainny 0:79012a2fa4d1 97 // mask and shift for general reading and writing
kennyainny 0:79012a2fa4d1 98 #define MMA8452_WRITE_MASK 0xFE
kennyainny 0:79012a2fa4d1 99 #define MMA8452_READ_MASK 0x01
kennyainny 0:79012a2fa4d1 100
kennyainny 0:79012a2fa4d1 101 // mask and shift for bit depth reading and writing
kennyainny 0:79012a2fa4d1 102 #define MMA8452_BIT_DEPTH_MASK 0xFD
kennyainny 0:79012a2fa4d1 103 #define MMA8452_BIT_DEPTH_MASK_SHIFT 0x01
kennyainny 0:79012a2fa4d1 104
kennyainny 0:79012a2fa4d1 105 // status masks and shifts
kennyainny 0:79012a2fa4d1 106 #define MMA8452_STATUS_ZYXDR_MASK 0x08
kennyainny 0:79012a2fa4d1 107 #define MMA8452_STATUS_ZDR_MASK 0x04
kennyainny 0:79012a2fa4d1 108 #define MMA8452_STATUS_YDR_MASK 0x02
kennyainny 0:79012a2fa4d1 109 #define MMA8452_STATUS_XDR_MASK 0x01
kennyainny 0:79012a2fa4d1 110
kennyainny 0:79012a2fa4d1 111 /**
kennyainny 0:79012a2fa4d1 112 * Wrapper for the MMA8452 I2C driven accelerometer.
kennyainny 0:79012a2fa4d1 113 */
kennyainny 0:79012a2fa4d1 114 class MMA8452 {
kennyainny 0:79012a2fa4d1 115
kennyainny 0:79012a2fa4d1 116 public:
kennyainny 0:79012a2fa4d1 117
kennyainny 0:79012a2fa4d1 118 enum DynamicRange {
kennyainny 0:79012a2fa4d1 119 DYNAMIC_RANGE_2G=0x00,
kennyainny 0:79012a2fa4d1 120 DYNAMIC_RANGE_4G,
kennyainny 0:79012a2fa4d1 121 DYNAMIC_RANGE_8G,
kennyainny 0:79012a2fa4d1 122 DYNAMIC_RANGE_UNKNOWN
kennyainny 0:79012a2fa4d1 123 };
kennyainny 0:79012a2fa4d1 124
kennyainny 0:79012a2fa4d1 125 enum BitDepth {
kennyainny 0:79012a2fa4d1 126 BIT_DEPTH_12=0x00,
kennyainny 0:79012a2fa4d1 127 BIT_DEPTH_8, // 1 sets fast read mode, hence the inversion
kennyainny 0:79012a2fa4d1 128 BIT_DEPTH_UNKNOWN
kennyainny 0:79012a2fa4d1 129 };
kennyainny 0:79012a2fa4d1 130
kennyainny 0:79012a2fa4d1 131 enum DataRateHz {
kennyainny 0:79012a2fa4d1 132 RATE_800=0x00,
kennyainny 0:79012a2fa4d1 133 RATE_400,
kennyainny 0:79012a2fa4d1 134 RATE_200,
kennyainny 0:79012a2fa4d1 135 RATE_100,
kennyainny 0:79012a2fa4d1 136 RATE_50,
kennyainny 0:79012a2fa4d1 137 RATE_12_5,
kennyainny 0:79012a2fa4d1 138 RATE_6_25,
kennyainny 0:79012a2fa4d1 139 RATE_1_563,
kennyainny 0:79012a2fa4d1 140 RATE_UNKNOWN
kennyainny 0:79012a2fa4d1 141 };
kennyainny 0:79012a2fa4d1 142
kennyainny 0:79012a2fa4d1 143 /**
kennyainny 0:79012a2fa4d1 144 * Create an accelerometer object connected to the specified I2C pins.
kennyainny 0:79012a2fa4d1 145 *
kennyainny 0:79012a2fa4d1 146 * @param sda I2C data port
kennyainny 0:79012a2fa4d1 147 * @param scl I2C clock port
kennyainny 0:79012a2fa4d1 148 * @param frequency
kennyainny 0:79012a2fa4d1 149 *
kennyainny 0:79012a2fa4d1 150 */
kennyainny 0:79012a2fa4d1 151 MMA8452(PinName sda, PinName scl, int frequency);
kennyainny 0:79012a2fa4d1 152
kennyainny 0:79012a2fa4d1 153 /// Destructor
kennyainny 0:79012a2fa4d1 154 ~MMA8452();
kennyainny 0:79012a2fa4d1 155
kennyainny 0:79012a2fa4d1 156 /**
kennyainny 0:79012a2fa4d1 157 * Puts the MMA8452 in active mode.
kennyainny 0:79012a2fa4d1 158 * @return 0 on success, 1 on failure.
kennyainny 0:79012a2fa4d1 159 */
kennyainny 0:79012a2fa4d1 160 int activate();
kennyainny 0:79012a2fa4d1 161
kennyainny 0:79012a2fa4d1 162 /**
kennyainny 0:79012a2fa4d1 163 * Puts the MMA8452 in standby.
kennyainny 0:79012a2fa4d1 164 * @return 0 on success, 1 on failure.
kennyainny 0:79012a2fa4d1 165 */
kennyainny 0:79012a2fa4d1 166 int standby();
kennyainny 0:79012a2fa4d1 167
kennyainny 0:79012a2fa4d1 168 /**
kennyainny 0:79012a2fa4d1 169 * Read the device ID from the accelerometer (should be 0x2a)
kennyainny 0:79012a2fa4d1 170 *
kennyainny 0:79012a2fa4d1 171 * @param dst pointer to store the ID
kennyainny 0:79012a2fa4d1 172 * @return 0 on success, 1 on failure.
kennyainny 0:79012a2fa4d1 173 */
kennyainny 0:79012a2fa4d1 174 int getDeviceID(char* dst);
kennyainny 0:79012a2fa4d1 175
kennyainny 0:79012a2fa4d1 176 /**
kennyainny 0:79012a2fa4d1 177 * Read the MMA8452 status register.
kennyainny 0:79012a2fa4d1 178 *
kennyainny 0:79012a2fa4d1 179 * @param dst pointer to store the register value.
kennyainny 0:79012a2fa4d1 180 * @ return 0 on success, 1 on failure.
kennyainny 0:79012a2fa4d1 181 */
kennyainny 0:79012a2fa4d1 182 int getStatus(char* dst);
kennyainny 0:79012a2fa4d1 183
kennyainny 0:79012a2fa4d1 184 /**
kennyainny 0:79012a2fa4d1 185 * Read the raw x, y, an z registers of the MMA8452 in one operation.
kennyainny 0:79012a2fa4d1 186 * All three registers are read sequentially and stored in the provided buffer.
kennyainny 0:79012a2fa4d1 187 * The stored values are signed 2's complement left-aligned 12 or 8 bit integers.
kennyainny 0:79012a2fa4d1 188 *
kennyainny 0:79012a2fa4d1 189 * @param dst The destination buffer. Note that this needs to be 3 bytes for
kennyainny 0:79012a2fa4d1 190 * BIT_DEPTH_8 and 6 bytes for BIT_DEPTH_12. It is upto the caller to ensure this.
kennyainny 0:79012a2fa4d1 191 * @return 0 for success, and 1 for failure
kennyainny 0:79012a2fa4d1 192 * @sa setBitDepth
kennyainny 0:79012a2fa4d1 193 */
kennyainny 0:79012a2fa4d1 194 int readXYZRaw(char *dst);
kennyainny 0:79012a2fa4d1 195
kennyainny 0:79012a2fa4d1 196 /// Read the raw x register into the provided buffer. @sa readXYZRaw
kennyainny 0:79012a2fa4d1 197 int readXRaw(char *dst);
kennyainny 0:79012a2fa4d1 198 /// Read the raw y register into the provided buffer. @sa readXYZRaw
kennyainny 0:79012a2fa4d1 199 int readYRaw(char *dst);
kennyainny 0:79012a2fa4d1 200 /// Read the raw z register into the provided buffer. @sa readXYZRaw
kennyainny 0:79012a2fa4d1 201 int readZRaw(char *dst);
kennyainny 0:79012a2fa4d1 202
kennyainny 0:79012a2fa4d1 203 /**
kennyainny 0:79012a2fa4d1 204 * Read the x, y, and z signed counts of the MMA8452 axes.
kennyainny 0:79012a2fa4d1 205 *
kennyainny 0:79012a2fa4d1 206 * Count resolution is either 8 bits or 12 bits, and the range is either +-2G, +-4G, or +-8G
kennyainny 0:79012a2fa4d1 207 * depending on settings. The number of counts per G are 1024, 512, 256 for 2,4, and 8 G
kennyainny 0:79012a2fa4d1 208 * respectively at 12 bit resolution and 64, 32, 16 for 2, 4, and 8 G respectively at
kennyainny 0:79012a2fa4d1 209 * 8 bit resolution.
kennyainny 0:79012a2fa4d1 210 *
kennyainny 0:79012a2fa4d1 211 * This function queries the MMA8452 and returns the signed counts for each axes.
kennyainny 0:79012a2fa4d1 212 *
kennyainny 0:79012a2fa4d1 213 * @param x Pointer to integer to store x count
kennyainny 0:79012a2fa4d1 214 * @param y Pointer to integer to store y count
kennyainny 0:79012a2fa4d1 215 * @param z Pointer to integer to store z count
kennyainny 0:79012a2fa4d1 216 * @return 0 on success, 1 on failure.
kennyainny 0:79012a2fa4d1 217 */
kennyainny 0:79012a2fa4d1 218 int readXYZCounts(int *x, int *y, int *z);
kennyainny 0:79012a2fa4d1 219
kennyainny 0:79012a2fa4d1 220 /// Read the x axes signed count. @sa readXYZCounts
kennyainny 0:79012a2fa4d1 221 int readXCount(int *x);
kennyainny 0:79012a2fa4d1 222 /// Read the y axes signed count. @sa readXYZCounts
kennyainny 0:79012a2fa4d1 223 int readYCount(int *y);
kennyainny 0:79012a2fa4d1 224 /// Read the z axes signed count. @sa readXYZCounts
kennyainny 0:79012a2fa4d1 225 int readZCount(int *z);
kennyainny 0:79012a2fa4d1 226
kennyainny 0:79012a2fa4d1 227 /**
kennyainny 0:79012a2fa4d1 228 * Read the x, y, and z accelerations measured in G.
kennyainny 0:79012a2fa4d1 229 *
kennyainny 0:79012a2fa4d1 230 * The measurement resolution is controlled via setBitDepth which can
kennyainny 0:79012a2fa4d1 231 * be 8 or 12, and by setDynamicRange, which can be +-2G, +-4G, or +-8G.
kennyainny 0:79012a2fa4d1 232 *
kennyainny 0:79012a2fa4d1 233 * @param x A pointer to the double to store the x acceleration in.
kennyainny 0:79012a2fa4d1 234 * @param y A pointer to the double to store the y acceleration in.
kennyainny 0:79012a2fa4d1 235 * @param z A pointer to the double to store the z acceleration in.
kennyainny 0:79012a2fa4d1 236 *
kennyainny 0:79012a2fa4d1 237 * @return 0 on success, 1 on failure.
kennyainny 0:79012a2fa4d1 238 */
kennyainny 0:79012a2fa4d1 239 int readXYZGravity(double *x, double *y, double *z);
kennyainny 0:79012a2fa4d1 240
kennyainny 0:79012a2fa4d1 241 /// Read the x gravity in G into the provided double pointer. @sa readXYZGravity
kennyainny 0:79012a2fa4d1 242 int readXGravity(double *x);
kennyainny 0:79012a2fa4d1 243 /// Read the y gravity in G into the provided double pointer. @sa readXYZGravity
kennyainny 0:79012a2fa4d1 244 int readYGravity(double *y);
kennyainny 0:79012a2fa4d1 245 /// Read the z gravity in G into the provided double pointer. @sa readXYZGravity
kennyainny 0:79012a2fa4d1 246 int readZGravity(double *z);
kennyainny 0:79012a2fa4d1 247
kennyainny 0:79012a2fa4d1 248 /// Returns 1 if data has been internally sampled (is available) for all axes since last read, 0 otherwise.
kennyainny 0:79012a2fa4d1 249 int isXYZReady();
kennyainny 0:79012a2fa4d1 250 /// Returns 1 if data has been internally sampled (is available) for the x-axis since last read, 0 otherwise.
kennyainny 0:79012a2fa4d1 251 int isXReady();
kennyainny 0:79012a2fa4d1 252 /// Returns 1 if data has been internally sampled (is available) for the y-axis since last read, 0 otherwise.
kennyainny 0:79012a2fa4d1 253 int isYReady();
kennyainny 0:79012a2fa4d1 254 /// Returns 1 if data has been internally sampled (is available) for the z-axis since last read, 0 otherwise.
kennyainny 0:79012a2fa4d1 255 int isZReady();
kennyainny 0:79012a2fa4d1 256
kennyainny 0:79012a2fa4d1 257 /**
kennyainny 0:79012a2fa4d1 258 * Reads a single byte from the specified MMA8452 register.
kennyainny 0:79012a2fa4d1 259 *
kennyainny 0:79012a2fa4d1 260 * @param addr The internal register address.
kennyainny 0:79012a2fa4d1 261 * @param dst The destination buffer address.
kennyainny 0:79012a2fa4d1 262 * @return 1 on success, 0 on failure.
kennyainny 0:79012a2fa4d1 263 */
kennyainny 0:79012a2fa4d1 264 int readRegister(char addr, char *dst);
kennyainny 0:79012a2fa4d1 265
kennyainny 0:79012a2fa4d1 266 /**
kennyainny 0:79012a2fa4d1 267 * Reads n bytes from the specified MMA8452 register.
kennyainny 0:79012a2fa4d1 268 *
kennyainny 0:79012a2fa4d1 269 * @param addr The internal register address.
kennyainny 0:79012a2fa4d1 270 * @param dst The destination buffer address.
kennyainny 0:79012a2fa4d1 271 * @param nbytes The number of bytes to read.
kennyainny 0:79012a2fa4d1 272 * @return 1 on success, 0 on failure.
kennyainny 0:79012a2fa4d1 273 */
kennyainny 0:79012a2fa4d1 274 int readRegister(char addr, char *dst, int nbytes);
kennyainny 0:79012a2fa4d1 275
kennyainny 0:79012a2fa4d1 276 /**
kennyainny 0:79012a2fa4d1 277 * Write to the specified MMA8452 register.
kennyainny 0:79012a2fa4d1 278 *
kennyainny 0:79012a2fa4d1 279 * @param addr The internal register address
kennyainny 0:79012a2fa4d1 280 * @param data Data byte to write
kennyainny 0:79012a2fa4d1 281 */
kennyainny 0:79012a2fa4d1 282 int writeRegister(char addr, char data);
kennyainny 0:79012a2fa4d1 283
kennyainny 0:79012a2fa4d1 284 /**
kennyainny 0:79012a2fa4d1 285 * Write a data buffer to the specified MMA8452 register.
kennyainny 0:79012a2fa4d1 286 *
kennyainny 0:79012a2fa4d1 287 * @param addr The internal register address
kennyainny 0:79012a2fa4d1 288 * @param data Pointer to data buffer to write
kennyainny 0:79012a2fa4d1 289 * @param nbytes The length of the data buffer to write
kennyainny 0:79012a2fa4d1 290 */
kennyainny 0:79012a2fa4d1 291 int writeRegister(char addr, char *data, int nbytes);
kennyainny 0:79012a2fa4d1 292
kennyainny 0:79012a2fa4d1 293 int setDynamicRange(DynamicRange range, int toggleActivation=1);
kennyainny 0:79012a2fa4d1 294 int setBitDepth(BitDepth depth, int toggleActivation=1);
kennyainny 0:79012a2fa4d1 295 int setDataRate(DataRateHz dataRate, int toggleActivation=1);
kennyainny 0:79012a2fa4d1 296
kennyainny 0:79012a2fa4d1 297 DynamicRange getDynamicRange();
kennyainny 0:79012a2fa4d1 298 DataRateHz getDataRate();
kennyainny 0:79012a2fa4d1 299 BitDepth getBitDepth();
kennyainny 0:79012a2fa4d1 300
kennyainny 0:79012a2fa4d1 301 #ifdef MMA8452_DEBUG
kennyainny 0:79012a2fa4d1 302 void debugRegister(char reg);
kennyainny 0:79012a2fa4d1 303 #endif
kennyainny 0:79012a2fa4d1 304
kennyainny 0:79012a2fa4d1 305 private:
kennyainny 0:79012a2fa4d1 306 /**
kennyainny 0:79012a2fa4d1 307 * Reads the specified register, applies the mask with logical AND, logical ORs the value
kennyainny 0:79012a2fa4d1 308 * and writes back the result to the register. If toggleActivation is set to true then the
kennyainny 0:79012a2fa4d1 309 * device is put in standby before the operation, and activated at the end.
kennyainny 0:79012a2fa4d1 310 * Setting it to false is useful for setting options on a device that you want to keep in
kennyainny 0:79012a2fa4d1 311 * standby.
kennyainny 0:79012a2fa4d1 312 */
kennyainny 0:79012a2fa4d1 313 int maskAndApplyRegister(char reg, char mask, char value, int toggleActivation);
kennyainny 0:79012a2fa4d1 314
kennyainny 0:79012a2fa4d1 315 /// Reads the specified register, applies the mask with logical AND, and writes the result back.
kennyainny 0:79012a2fa4d1 316 int logicalANDRegister(char addr, char mask);
kennyainny 0:79012a2fa4d1 317 /// Reads the specified register, applies the mask with logical OR, and writes the result back.
kennyainny 0:79012a2fa4d1 318 int logicalORRegister(char addr, char mask);
kennyainny 0:79012a2fa4d1 319 /// Reads the specified register, applies the mask with logical XOR, and writes the result back.
kennyainny 0:79012a2fa4d1 320 int logicalXORRegister(char addr, char mask);
kennyainny 0:79012a2fa4d1 321
kennyainny 0:79012a2fa4d1 322 /// Converts the 12-bit two's complement number in buf to a signed integer. Returns the integer.
kennyainny 0:79012a2fa4d1 323 int twelveBitToSigned(char *buf);
kennyainny 0:79012a2fa4d1 324 /// Converts the 8-bit two's complement number in buf to a signed integer. Returns the integer.
kennyainny 0:79012a2fa4d1 325 int eightBitToSigned(char *buf);
kennyainny 0:79012a2fa4d1 326
kennyainny 0:79012a2fa4d1 327 /// Converts a count to a gravity using the supplied countsPerG. Returns the gravity.
kennyainny 0:79012a2fa4d1 328 double convertCountToGravity(int count, int countsPerG);
kennyainny 0:79012a2fa4d1 329
kennyainny 0:79012a2fa4d1 330 /// Reads the register at addr, applies the mask with logical AND, and returns the result.
kennyainny 0:79012a2fa4d1 331 char getMaskedRegister(int addr, char mask);
kennyainny 0:79012a2fa4d1 332
kennyainny 0:79012a2fa4d1 333 /// Get the counts per G for the current settings of bit depth and dynamic range.
kennyainny 0:79012a2fa4d1 334 int getCountsPerG();
kennyainny 0:79012a2fa4d1 335
kennyainny 0:79012a2fa4d1 336 I2C _i2c;
kennyainny 0:79012a2fa4d1 337 int _frequency;
kennyainny 0:79012a2fa4d1 338 int _readAddress;
kennyainny 0:79012a2fa4d1 339 int _writeAddress;
kennyainny 0:79012a2fa4d1 340
kennyainny 0:79012a2fa4d1 341 BitDepth _bitDepth;
kennyainny 0:79012a2fa4d1 342 DynamicRange _dynamicRange;
kennyainny 0:79012a2fa4d1 343 };