Lib for MPU9150/9250 IMU sensors

Dependents:   ezSBC_MPU9250

Committer:
JojoS
Date:
Mon Apr 24 16:21:35 2017 +0000
Revision:
0:ab00d9bcd00d
modified for mbed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JojoS 0:ab00d9bcd00d 1 // I2Cdev library collection - MPU9150 I2C device class
JojoS 0:ab00d9bcd00d 2 // Based on InvenSense MPU-9150 register map document rev. 2.0, 5/19/2011 (RM-MPU-6000A-00)
JojoS 0:ab00d9bcd00d 3 // 8/24/2011 by Jeff Rowberg <jeff@rowberg.net>
JojoS 0:ab00d9bcd00d 4 // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
JojoS 0:ab00d9bcd00d 5 //
JojoS 0:ab00d9bcd00d 6 // Changelog:
JojoS 0:ab00d9bcd00d 7 // ... - ongoing debug release
JojoS 0:ab00d9bcd00d 8
JojoS 0:ab00d9bcd00d 9 // NOTE: THIS IS ONLY A PARIAL RELEASE. THIS DEVICE CLASS IS CURRENTLY UNDERGOING ACTIVE
JojoS 0:ab00d9bcd00d 10 // DEVELOPMENT AND IS STILL MISSING SOME IMPORTANT FEATURES. PLEASE KEEP THIS IN MIND IF
JojoS 0:ab00d9bcd00d 11 // YOU DECIDE TO USE THIS PARTICULAR CODE FOR ANYTHING.
JojoS 0:ab00d9bcd00d 12
JojoS 0:ab00d9bcd00d 13 /* ============================================
JojoS 0:ab00d9bcd00d 14 I2Cdev device library code is placed under the MIT license
JojoS 0:ab00d9bcd00d 15 Copyright (c) 2012 Jeff Rowberg
JojoS 0:ab00d9bcd00d 16
JojoS 0:ab00d9bcd00d 17 Permission is hereby granted, free of charge, to any person obtaining a copy
JojoS 0:ab00d9bcd00d 18 of this software and associated documentation files (the "Software"), to deal
JojoS 0:ab00d9bcd00d 19 in the Software without restriction, including without limitation the rights
JojoS 0:ab00d9bcd00d 20 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
JojoS 0:ab00d9bcd00d 21 copies of the Software, and to permit persons to whom the Software is
JojoS 0:ab00d9bcd00d 22 furnished to do so, subject to the following conditions:
JojoS 0:ab00d9bcd00d 23
JojoS 0:ab00d9bcd00d 24 The above copyright notice and this permission notice shall be included in
JojoS 0:ab00d9bcd00d 25 all copies or substantial portions of the Software.
JojoS 0:ab00d9bcd00d 26
JojoS 0:ab00d9bcd00d 27 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
JojoS 0:ab00d9bcd00d 28 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
JojoS 0:ab00d9bcd00d 29 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
JojoS 0:ab00d9bcd00d 30 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
JojoS 0:ab00d9bcd00d 31 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
JojoS 0:ab00d9bcd00d 32 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
JojoS 0:ab00d9bcd00d 33 THE SOFTWARE.
JojoS 0:ab00d9bcd00d 34 ===============================================
JojoS 0:ab00d9bcd00d 35 */
JojoS 0:ab00d9bcd00d 36
JojoS 0:ab00d9bcd00d 37 #include "MPU9150.h"
JojoS 0:ab00d9bcd00d 38
JojoS 0:ab00d9bcd00d 39 /** Default constructor, uses default I2C address.
JojoS 0:ab00d9bcd00d 40 * @see MPU9150_DEFAULT_ADDRESS
JojoS 0:ab00d9bcd00d 41 */
JojoS 0:ab00d9bcd00d 42 MPU9150::MPU9150(I2Cdev &dev) : i2Cdev(dev) {
JojoS 0:ab00d9bcd00d 43 devAddr = MPU9150_DEFAULT_ADDRESS;
JojoS 0:ab00d9bcd00d 44 }
JojoS 0:ab00d9bcd00d 45
JojoS 0:ab00d9bcd00d 46 /** Specific address constructor.
JojoS 0:ab00d9bcd00d 47 * @param address I2C address
JojoS 0:ab00d9bcd00d 48 * @see MPU9150_DEFAULT_ADDRESS
JojoS 0:ab00d9bcd00d 49 * @see MPU9150_ADDRESS_AD0_LOW
JojoS 0:ab00d9bcd00d 50 * @see MPU9150_ADDRESS_AD0_HIGH
JojoS 0:ab00d9bcd00d 51 */
JojoS 0:ab00d9bcd00d 52 MPU9150::MPU9150(I2Cdev &dev, uint8_t address) : i2Cdev(dev) {
JojoS 0:ab00d9bcd00d 53 devAddr = address;
JojoS 0:ab00d9bcd00d 54 }
JojoS 0:ab00d9bcd00d 55
JojoS 0:ab00d9bcd00d 56 /** Power on and prepare for general usage.
JojoS 0:ab00d9bcd00d 57 * This will activate the device and take it out of sleep mode (which must be done
JojoS 0:ab00d9bcd00d 58 * after start-up). This function also sets both the accelerometer and the gyroscope
JojoS 0:ab00d9bcd00d 59 * to their most sensitive settings, namely +/- 2g and +/- 250 degrees/sec, and sets
JojoS 0:ab00d9bcd00d 60 * the clock source to use the X Gyro for reference, which is slightly better than
JojoS 0:ab00d9bcd00d 61 * the default internal clock source.
JojoS 0:ab00d9bcd00d 62 */
JojoS 0:ab00d9bcd00d 63 void MPU9150::initialize() {
JojoS 0:ab00d9bcd00d 64 setClockSource(MPU9150_CLOCK_PLL_XGYRO);
JojoS 0:ab00d9bcd00d 65 setFullScaleGyroRange(MPU9150_GYRO_FS_250);
JojoS 0:ab00d9bcd00d 66 setFullScaleAccelRange(MPU9150_ACCEL_FS_2);
JojoS 0:ab00d9bcd00d 67 setSleepEnabled(false); // thanks to Jack Elston for pointing this one out!
JojoS 0:ab00d9bcd00d 68 }
JojoS 0:ab00d9bcd00d 69
JojoS 0:ab00d9bcd00d 70 /** Verify the I2C connection.
JojoS 0:ab00d9bcd00d 71 * Make sure the device is connected and responds as expected.
JojoS 0:ab00d9bcd00d 72 * @return True if connection is valid, false otherwise
JojoS 0:ab00d9bcd00d 73 */
JojoS 0:ab00d9bcd00d 74 bool MPU9150::testConnection() {
JojoS 0:ab00d9bcd00d 75 return getDeviceID() == 0x34;
JojoS 0:ab00d9bcd00d 76 }
JojoS 0:ab00d9bcd00d 77
JojoS 0:ab00d9bcd00d 78 // AUX_VDDIO register (InvenSense demo code calls this RA_*G_OFFS_TC)
JojoS 0:ab00d9bcd00d 79
JojoS 0:ab00d9bcd00d 80 /** Get the auxiliary I2C supply voltage level.
JojoS 0:ab00d9bcd00d 81 * When set to 1, the auxiliary I2C bus high logic level is VDD. When cleared to
JojoS 0:ab00d9bcd00d 82 * 0, the auxiliary I2C bus high logic level is VLOGIC. This does not apply to
JojoS 0:ab00d9bcd00d 83 * the MPU-6000, which does not have a VLOGIC pin.
JojoS 0:ab00d9bcd00d 84 * @return I2C supply voltage level (0=VLOGIC, 1=VDD)
JojoS 0:ab00d9bcd00d 85 */
JojoS 0:ab00d9bcd00d 86 uint8_t MPU9150::getAuxVDDIOLevel() {
JojoS 0:ab00d9bcd00d 87 i2Cdev.readBit(devAddr, MPU9150_RA_YG_OFFS_TC, MPU9150_TC_PWR_MODE_BIT, buffer);
JojoS 0:ab00d9bcd00d 88 return buffer[0];
JojoS 0:ab00d9bcd00d 89 }
JojoS 0:ab00d9bcd00d 90 /** Set the auxiliary I2C supply voltage level.
JojoS 0:ab00d9bcd00d 91 * When set to 1, the auxiliary I2C bus high logic level is VDD. When cleared to
JojoS 0:ab00d9bcd00d 92 * 0, the auxiliary I2C bus high logic level is VLOGIC. This does not apply to
JojoS 0:ab00d9bcd00d 93 * the MPU-6000, which does not have a VLOGIC pin.
JojoS 0:ab00d9bcd00d 94 * @param level I2C supply voltage level (0=VLOGIC, 1=VDD)
JojoS 0:ab00d9bcd00d 95 */
JojoS 0:ab00d9bcd00d 96 void MPU9150::setAuxVDDIOLevel(uint8_t level) {
JojoS 0:ab00d9bcd00d 97 i2Cdev.writeBit(devAddr, MPU9150_RA_YG_OFFS_TC, MPU9150_TC_PWR_MODE_BIT, level);
JojoS 0:ab00d9bcd00d 98 }
JojoS 0:ab00d9bcd00d 99
JojoS 0:ab00d9bcd00d 100 // SMPLRT_DIV register
JojoS 0:ab00d9bcd00d 101
JojoS 0:ab00d9bcd00d 102 /** Get gyroscope output rate divider.
JojoS 0:ab00d9bcd00d 103 * The sensor register output, FIFO output, DMP sampling, Motion detection, Zero
JojoS 0:ab00d9bcd00d 104 * Motion detection, and Free Fall detection are all based on the Sample Rate.
JojoS 0:ab00d9bcd00d 105 * The Sample Rate is generated by dividing the gyroscope output rate by
JojoS 0:ab00d9bcd00d 106 * SMPLRT_DIV:
JojoS 0:ab00d9bcd00d 107 *
JojoS 0:ab00d9bcd00d 108 * Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV)
JojoS 0:ab00d9bcd00d 109 *
JojoS 0:ab00d9bcd00d 110 * where Gyroscope Output Rate = 8kHz when the DLPF is disabled (DLPF_CFG = 0 or
JojoS 0:ab00d9bcd00d 111 * 7), and 1kHz when the DLPF is enabled (see Register 26).
JojoS 0:ab00d9bcd00d 112 *
JojoS 0:ab00d9bcd00d 113 * Note: The accelerometer output rate is 1kHz. This means that for a Sample
JojoS 0:ab00d9bcd00d 114 * Rate greater than 1kHz, the same accelerometer sample may be output to the
JojoS 0:ab00d9bcd00d 115 * FIFO, DMP, and sensor registers more than once.
JojoS 0:ab00d9bcd00d 116 *
JojoS 0:ab00d9bcd00d 117 * For a diagram of the gyroscope and accelerometer signal paths, see Section 8
JojoS 0:ab00d9bcd00d 118 * of the MPU-6000/MPU-9150 Product Specification document.
JojoS 0:ab00d9bcd00d 119 *
JojoS 0:ab00d9bcd00d 120 * @return Current sample rate
JojoS 0:ab00d9bcd00d 121 * @see MPU9150_RA_SMPLRT_DIV
JojoS 0:ab00d9bcd00d 122 */
JojoS 0:ab00d9bcd00d 123 uint8_t MPU9150::getRate() {
JojoS 0:ab00d9bcd00d 124 i2Cdev.readByte(devAddr, MPU9150_RA_SMPLRT_DIV, buffer);
JojoS 0:ab00d9bcd00d 125 return buffer[0];
JojoS 0:ab00d9bcd00d 126 }
JojoS 0:ab00d9bcd00d 127 /** Set gyroscope sample rate divider.
JojoS 0:ab00d9bcd00d 128 * @param rate New sample rate divider
JojoS 0:ab00d9bcd00d 129 * @see getRate()
JojoS 0:ab00d9bcd00d 130 * @see MPU9150_RA_SMPLRT_DIV
JojoS 0:ab00d9bcd00d 131 */
JojoS 0:ab00d9bcd00d 132 void MPU9150::setRate(uint8_t rate) {
JojoS 0:ab00d9bcd00d 133 i2Cdev.writeByte(devAddr, MPU9150_RA_SMPLRT_DIV, rate);
JojoS 0:ab00d9bcd00d 134 }
JojoS 0:ab00d9bcd00d 135
JojoS 0:ab00d9bcd00d 136 // CONFIG register
JojoS 0:ab00d9bcd00d 137
JojoS 0:ab00d9bcd00d 138 /** Get external FSYNC configuration.
JojoS 0:ab00d9bcd00d 139 * Configures the external Frame Synchronization (FSYNC) pin sampling. An
JojoS 0:ab00d9bcd00d 140 * external signal connected to the FSYNC pin can be sampled by configuring
JojoS 0:ab00d9bcd00d 141 * EXT_SYNC_SET. Signal changes to the FSYNC pin are latched so that short
JojoS 0:ab00d9bcd00d 142 * strobes may be captured. The latched FSYNC signal will be sampled at the
JojoS 0:ab00d9bcd00d 143 * Sampling Rate, as defined in register 25. After sampling, the latch will
JojoS 0:ab00d9bcd00d 144 * reset to the current FSYNC signal state.
JojoS 0:ab00d9bcd00d 145 *
JojoS 0:ab00d9bcd00d 146 * The sampled value will be reported in place of the least significant bit in
JojoS 0:ab00d9bcd00d 147 * a sensor data register determined by the value of EXT_SYNC_SET according to
JojoS 0:ab00d9bcd00d 148 * the following table.
JojoS 0:ab00d9bcd00d 149 *
JojoS 0:ab00d9bcd00d 150 * <pre>
JojoS 0:ab00d9bcd00d 151 * EXT_SYNC_SET | FSYNC Bit Location
JojoS 0:ab00d9bcd00d 152 * -------------+-------------------
JojoS 0:ab00d9bcd00d 153 * 0 | Input disabled
JojoS 0:ab00d9bcd00d 154 * 1 | TEMP_OUT_L[0]
JojoS 0:ab00d9bcd00d 155 * 2 | GYRO_XOUT_L[0]
JojoS 0:ab00d9bcd00d 156 * 3 | GYRO_YOUT_L[0]
JojoS 0:ab00d9bcd00d 157 * 4 | GYRO_ZOUT_L[0]
JojoS 0:ab00d9bcd00d 158 * 5 | ACCEL_XOUT_L[0]
JojoS 0:ab00d9bcd00d 159 * 6 | ACCEL_YOUT_L[0]
JojoS 0:ab00d9bcd00d 160 * 7 | ACCEL_ZOUT_L[0]
JojoS 0:ab00d9bcd00d 161 * </pre>
JojoS 0:ab00d9bcd00d 162 *
JojoS 0:ab00d9bcd00d 163 * @return FSYNC configuration value
JojoS 0:ab00d9bcd00d 164 */
JojoS 0:ab00d9bcd00d 165 uint8_t MPU9150::getExternalFrameSync() {
JojoS 0:ab00d9bcd00d 166 i2Cdev.readBits(devAddr, MPU9150_RA_CONFIG, MPU9150_CFG_EXT_SYNC_SET_BIT, MPU9150_CFG_EXT_SYNC_SET_LENGTH, buffer);
JojoS 0:ab00d9bcd00d 167 return buffer[0];
JojoS 0:ab00d9bcd00d 168 }
JojoS 0:ab00d9bcd00d 169 /** Set external FSYNC configuration.
JojoS 0:ab00d9bcd00d 170 * @see getExternalFrameSync()
JojoS 0:ab00d9bcd00d 171 * @see MPU9150_RA_CONFIG
JojoS 0:ab00d9bcd00d 172 * @param sync New FSYNC configuration value
JojoS 0:ab00d9bcd00d 173 */
JojoS 0:ab00d9bcd00d 174 void MPU9150::setExternalFrameSync(uint8_t sync) {
JojoS 0:ab00d9bcd00d 175 i2Cdev.writeBits(devAddr, MPU9150_RA_CONFIG, MPU9150_CFG_EXT_SYNC_SET_BIT, MPU9150_CFG_EXT_SYNC_SET_LENGTH, sync);
JojoS 0:ab00d9bcd00d 176 }
JojoS 0:ab00d9bcd00d 177 /** Get digital low-pass filter configuration.
JojoS 0:ab00d9bcd00d 178 * The DLPF_CFG parameter sets the digital low pass filter configuration. It
JojoS 0:ab00d9bcd00d 179 * also determines the internal sampling rate used by the device as shown in
JojoS 0:ab00d9bcd00d 180 * the table below.
JojoS 0:ab00d9bcd00d 181 *
JojoS 0:ab00d9bcd00d 182 * Note: The accelerometer output rate is 1kHz. This means that for a Sample
JojoS 0:ab00d9bcd00d 183 * Rate greater than 1kHz, the same accelerometer sample may be output to the
JojoS 0:ab00d9bcd00d 184 * FIFO, DMP, and sensor registers more than once.
JojoS 0:ab00d9bcd00d 185 *
JojoS 0:ab00d9bcd00d 186 * <pre>
JojoS 0:ab00d9bcd00d 187 * | ACCELEROMETER | GYROSCOPE
JojoS 0:ab00d9bcd00d 188 * DLPF_CFG | Bandwidth | Delay | Bandwidth | Delay | Sample Rate
JojoS 0:ab00d9bcd00d 189 * ---------+-----------+--------+-----------+--------+-------------
JojoS 0:ab00d9bcd00d 190 * 0 | 260Hz | 0ms | 256Hz | 0.98ms | 8kHz
JojoS 0:ab00d9bcd00d 191 * 1 | 184Hz | 2.0ms | 188Hz | 1.9ms | 1kHz
JojoS 0:ab00d9bcd00d 192 * 2 | 94Hz | 3.0ms | 98Hz | 2.8ms | 1kHz
JojoS 0:ab00d9bcd00d 193 * 3 | 44Hz | 4.9ms | 42Hz | 4.8ms | 1kHz
JojoS 0:ab00d9bcd00d 194 * 4 | 21Hz | 8.5ms | 20Hz | 8.3ms | 1kHz
JojoS 0:ab00d9bcd00d 195 * 5 | 10Hz | 13.8ms | 10Hz | 13.4ms | 1kHz
JojoS 0:ab00d9bcd00d 196 * 6 | 5Hz | 19.0ms | 5Hz | 18.6ms | 1kHz
JojoS 0:ab00d9bcd00d 197 * 7 | -- Reserved -- | -- Reserved -- | Reserved
JojoS 0:ab00d9bcd00d 198 * </pre>
JojoS 0:ab00d9bcd00d 199 *
JojoS 0:ab00d9bcd00d 200 * @return DLFP configuration
JojoS 0:ab00d9bcd00d 201 * @see MPU9150_RA_CONFIG
JojoS 0:ab00d9bcd00d 202 * @see MPU9150_CFG_DLPF_CFG_BIT
JojoS 0:ab00d9bcd00d 203 * @see MPU9150_CFG_DLPF_CFG_LENGTH
JojoS 0:ab00d9bcd00d 204 */
JojoS 0:ab00d9bcd00d 205 uint8_t MPU9150::getDLPFMode() {
JojoS 0:ab00d9bcd00d 206 i2Cdev.readBits(devAddr, MPU9150_RA_CONFIG, MPU9150_CFG_DLPF_CFG_BIT, MPU9150_CFG_DLPF_CFG_LENGTH, buffer);
JojoS 0:ab00d9bcd00d 207 return buffer[0];
JojoS 0:ab00d9bcd00d 208 }
JojoS 0:ab00d9bcd00d 209 /** Set digital low-pass filter configuration.
JojoS 0:ab00d9bcd00d 210 * @param mode New DLFP configuration setting
JojoS 0:ab00d9bcd00d 211 * @see getDLPFBandwidth()
JojoS 0:ab00d9bcd00d 212 * @see MPU9150_DLPF_BW_256
JojoS 0:ab00d9bcd00d 213 * @see MPU9150_RA_CONFIG
JojoS 0:ab00d9bcd00d 214 * @see MPU9150_CFG_DLPF_CFG_BIT
JojoS 0:ab00d9bcd00d 215 * @see MPU9150_CFG_DLPF_CFG_LENGTH
JojoS 0:ab00d9bcd00d 216 */
JojoS 0:ab00d9bcd00d 217 void MPU9150::setDLPFMode(uint8_t mode) {
JojoS 0:ab00d9bcd00d 218 i2Cdev.writeBits(devAddr, MPU9150_RA_CONFIG, MPU9150_CFG_DLPF_CFG_BIT, MPU9150_CFG_DLPF_CFG_LENGTH, mode);
JojoS 0:ab00d9bcd00d 219 }
JojoS 0:ab00d9bcd00d 220
JojoS 0:ab00d9bcd00d 221 // GYRO_CONFIG register
JojoS 0:ab00d9bcd00d 222
JojoS 0:ab00d9bcd00d 223 /** Get full-scale gyroscope range.
JojoS 0:ab00d9bcd00d 224 * The FS_SEL parameter allows setting the full-scale range of the gyro sensors,
JojoS 0:ab00d9bcd00d 225 * as described in the table below.
JojoS 0:ab00d9bcd00d 226 *
JojoS 0:ab00d9bcd00d 227 * <pre>
JojoS 0:ab00d9bcd00d 228 * 0 = +/- 250 degrees/sec
JojoS 0:ab00d9bcd00d 229 * 1 = +/- 500 degrees/sec
JojoS 0:ab00d9bcd00d 230 * 2 = +/- 1000 degrees/sec
JojoS 0:ab00d9bcd00d 231 * 3 = +/- 2000 degrees/sec
JojoS 0:ab00d9bcd00d 232 * </pre>
JojoS 0:ab00d9bcd00d 233 *
JojoS 0:ab00d9bcd00d 234 * @return Current full-scale gyroscope range setting
JojoS 0:ab00d9bcd00d 235 * @see MPU9150_GYRO_FS_250
JojoS 0:ab00d9bcd00d 236 * @see MPU9150_RA_GYRO_CONFIG
JojoS 0:ab00d9bcd00d 237 * @see MPU9150_GCONFIG_FS_SEL_BIT
JojoS 0:ab00d9bcd00d 238 * @see MPU9150_GCONFIG_FS_SEL_LENGTH
JojoS 0:ab00d9bcd00d 239 */
JojoS 0:ab00d9bcd00d 240 uint8_t MPU9150::getFullScaleGyroRange() {
JojoS 0:ab00d9bcd00d 241 i2Cdev.readBits(devAddr, MPU9150_RA_GYRO_CONFIG, MPU9150_GCONFIG_FS_SEL_BIT, MPU9150_GCONFIG_FS_SEL_LENGTH, buffer);
JojoS 0:ab00d9bcd00d 242 return buffer[0];
JojoS 0:ab00d9bcd00d 243 }
JojoS 0:ab00d9bcd00d 244 /** Set full-scale gyroscope range.
JojoS 0:ab00d9bcd00d 245 * @param range New full-scale gyroscope range value
JojoS 0:ab00d9bcd00d 246 * @see getFullScaleRange()
JojoS 0:ab00d9bcd00d 247 * @see MPU9150_GYRO_FS_250
JojoS 0:ab00d9bcd00d 248 * @see MPU9150_RA_GYRO_CONFIG
JojoS 0:ab00d9bcd00d 249 * @see MPU9150_GCONFIG_FS_SEL_BIT
JojoS 0:ab00d9bcd00d 250 * @see MPU9150_GCONFIG_FS_SEL_LENGTH
JojoS 0:ab00d9bcd00d 251 */
JojoS 0:ab00d9bcd00d 252 void MPU9150::setFullScaleGyroRange(uint8_t range) {
JojoS 0:ab00d9bcd00d 253 i2Cdev.writeBits(devAddr, MPU9150_RA_GYRO_CONFIG, MPU9150_GCONFIG_FS_SEL_BIT, MPU9150_GCONFIG_FS_SEL_LENGTH, range);
JojoS 0:ab00d9bcd00d 254 }
JojoS 0:ab00d9bcd00d 255
JojoS 0:ab00d9bcd00d 256 // ACCEL_CONFIG register
JojoS 0:ab00d9bcd00d 257
JojoS 0:ab00d9bcd00d 258 /** Get self-test enabled setting for accelerometer X axis.
JojoS 0:ab00d9bcd00d 259 * @return Self-test enabled value
JojoS 0:ab00d9bcd00d 260 * @see MPU9150_RA_ACCEL_CONFIG
JojoS 0:ab00d9bcd00d 261 */
JojoS 0:ab00d9bcd00d 262 bool MPU9150::getAccelXSelfTest() {
JojoS 0:ab00d9bcd00d 263 i2Cdev.readBit(devAddr, MPU9150_RA_ACCEL_CONFIG, MPU9150_ACONFIG_XA_ST_BIT, buffer);
JojoS 0:ab00d9bcd00d 264 return buffer[0];
JojoS 0:ab00d9bcd00d 265 }
JojoS 0:ab00d9bcd00d 266 /** Get self-test enabled setting for accelerometer X axis.
JojoS 0:ab00d9bcd00d 267 * @param enabled Self-test enabled value
JojoS 0:ab00d9bcd00d 268 * @see MPU9150_RA_ACCEL_CONFIG
JojoS 0:ab00d9bcd00d 269 */
JojoS 0:ab00d9bcd00d 270 void MPU9150::setAccelXSelfTest(bool enabled) {
JojoS 0:ab00d9bcd00d 271 i2Cdev.writeBit(devAddr, MPU9150_RA_ACCEL_CONFIG, MPU9150_ACONFIG_XA_ST_BIT, enabled);
JojoS 0:ab00d9bcd00d 272 }
JojoS 0:ab00d9bcd00d 273 /** Get self-test enabled value for accelerometer Y axis.
JojoS 0:ab00d9bcd00d 274 * @return Self-test enabled value
JojoS 0:ab00d9bcd00d 275 * @see MPU9150_RA_ACCEL_CONFIG
JojoS 0:ab00d9bcd00d 276 */
JojoS 0:ab00d9bcd00d 277 bool MPU9150::getAccelYSelfTest() {
JojoS 0:ab00d9bcd00d 278 i2Cdev.readBit(devAddr, MPU9150_RA_ACCEL_CONFIG, MPU9150_ACONFIG_YA_ST_BIT, buffer);
JojoS 0:ab00d9bcd00d 279 return buffer[0];
JojoS 0:ab00d9bcd00d 280 }
JojoS 0:ab00d9bcd00d 281 /** Get self-test enabled value for accelerometer Y axis.
JojoS 0:ab00d9bcd00d 282 * @param enabled Self-test enabled value
JojoS 0:ab00d9bcd00d 283 * @see MPU9150_RA_ACCEL_CONFIG
JojoS 0:ab00d9bcd00d 284 */
JojoS 0:ab00d9bcd00d 285 void MPU9150::setAccelYSelfTest(bool enabled) {
JojoS 0:ab00d9bcd00d 286 i2Cdev.writeBit(devAddr, MPU9150_RA_ACCEL_CONFIG, MPU9150_ACONFIG_YA_ST_BIT, enabled);
JojoS 0:ab00d9bcd00d 287 }
JojoS 0:ab00d9bcd00d 288 /** Get self-test enabled value for accelerometer Z axis.
JojoS 0:ab00d9bcd00d 289 * @return Self-test enabled value
JojoS 0:ab00d9bcd00d 290 * @see MPU9150_RA_ACCEL_CONFIG
JojoS 0:ab00d9bcd00d 291 */
JojoS 0:ab00d9bcd00d 292 bool MPU9150::getAccelZSelfTest() {
JojoS 0:ab00d9bcd00d 293 i2Cdev.readBit(devAddr, MPU9150_RA_ACCEL_CONFIG, MPU9150_ACONFIG_ZA_ST_BIT, buffer);
JojoS 0:ab00d9bcd00d 294 return buffer[0];
JojoS 0:ab00d9bcd00d 295 }
JojoS 0:ab00d9bcd00d 296 /** Set self-test enabled value for accelerometer Z axis.
JojoS 0:ab00d9bcd00d 297 * @param enabled Self-test enabled value
JojoS 0:ab00d9bcd00d 298 * @see MPU9150_RA_ACCEL_CONFIG
JojoS 0:ab00d9bcd00d 299 */
JojoS 0:ab00d9bcd00d 300 void MPU9150::setAccelZSelfTest(bool enabled) {
JojoS 0:ab00d9bcd00d 301 i2Cdev.writeBit(devAddr, MPU9150_RA_ACCEL_CONFIG, MPU9150_ACONFIG_ZA_ST_BIT, enabled);
JojoS 0:ab00d9bcd00d 302 }
JojoS 0:ab00d9bcd00d 303 /** Get full-scale accelerometer range.
JojoS 0:ab00d9bcd00d 304 * The FS_SEL parameter allows setting the full-scale range of the accelerometer
JojoS 0:ab00d9bcd00d 305 * sensors, as described in the table below.
JojoS 0:ab00d9bcd00d 306 *
JojoS 0:ab00d9bcd00d 307 * <pre>
JojoS 0:ab00d9bcd00d 308 * 0 = +/- 2g
JojoS 0:ab00d9bcd00d 309 * 1 = +/- 4g
JojoS 0:ab00d9bcd00d 310 * 2 = +/- 8g
JojoS 0:ab00d9bcd00d 311 * 3 = +/- 16g
JojoS 0:ab00d9bcd00d 312 * </pre>
JojoS 0:ab00d9bcd00d 313 *
JojoS 0:ab00d9bcd00d 314 * @return Current full-scale accelerometer range setting
JojoS 0:ab00d9bcd00d 315 * @see MPU9150_ACCEL_FS_2
JojoS 0:ab00d9bcd00d 316 * @see MPU9150_RA_ACCEL_CONFIG
JojoS 0:ab00d9bcd00d 317 * @see MPU9150_ACONFIG_AFS_SEL_BIT
JojoS 0:ab00d9bcd00d 318 * @see MPU9150_ACONFIG_AFS_SEL_LENGTH
JojoS 0:ab00d9bcd00d 319 */
JojoS 0:ab00d9bcd00d 320 uint8_t MPU9150::getFullScaleAccelRange() {
JojoS 0:ab00d9bcd00d 321 i2Cdev.readBits(devAddr, MPU9150_RA_ACCEL_CONFIG, MPU9150_ACONFIG_AFS_SEL_BIT, MPU9150_ACONFIG_AFS_SEL_LENGTH, buffer);
JojoS 0:ab00d9bcd00d 322 return buffer[0];
JojoS 0:ab00d9bcd00d 323 }
JojoS 0:ab00d9bcd00d 324 /** Set full-scale accelerometer range.
JojoS 0:ab00d9bcd00d 325 * @param range New full-scale accelerometer range setting
JojoS 0:ab00d9bcd00d 326 * @see getFullScaleAccelRange()
JojoS 0:ab00d9bcd00d 327 */
JojoS 0:ab00d9bcd00d 328 void MPU9150::setFullScaleAccelRange(uint8_t range) {
JojoS 0:ab00d9bcd00d 329 i2Cdev.writeBits(devAddr, MPU9150_RA_ACCEL_CONFIG, MPU9150_ACONFIG_AFS_SEL_BIT, MPU9150_ACONFIG_AFS_SEL_LENGTH, range);
JojoS 0:ab00d9bcd00d 330 }
JojoS 0:ab00d9bcd00d 331 /** Get the high-pass filter configuration.
JojoS 0:ab00d9bcd00d 332 * The DHPF is a filter module in the path leading to motion detectors (Free
JojoS 0:ab00d9bcd00d 333 * Fall, Motion threshold, and Zero Motion). The high pass filter output is not
JojoS 0:ab00d9bcd00d 334 * available to the data registers (see Figure in Section 8 of the MPU-6000/
JojoS 0:ab00d9bcd00d 335 * MPU-9150 Product Specification document).
JojoS 0:ab00d9bcd00d 336 *
JojoS 0:ab00d9bcd00d 337 * The high pass filter has three modes:
JojoS 0:ab00d9bcd00d 338 *
JojoS 0:ab00d9bcd00d 339 * <pre>
JojoS 0:ab00d9bcd00d 340 * Reset: The filter output settles to zero within one sample. This
JojoS 0:ab00d9bcd00d 341 * effectively disables the high pass filter. This mode may be toggled
JojoS 0:ab00d9bcd00d 342 * to quickly settle the filter.
JojoS 0:ab00d9bcd00d 343 *
JojoS 0:ab00d9bcd00d 344 * On: The high pass filter will pass signals above the cut off frequency.
JojoS 0:ab00d9bcd00d 345 *
JojoS 0:ab00d9bcd00d 346 * Hold: When triggered, the filter holds the present sample. The filter
JojoS 0:ab00d9bcd00d 347 * output will be the difference between the input sample and the held
JojoS 0:ab00d9bcd00d 348 * sample.
JojoS 0:ab00d9bcd00d 349 * </pre>
JojoS 0:ab00d9bcd00d 350 *
JojoS 0:ab00d9bcd00d 351 * <pre>
JojoS 0:ab00d9bcd00d 352 * ACCEL_HPF | Filter Mode | Cut-off Frequency
JojoS 0:ab00d9bcd00d 353 * ----------+-------------+------------------
JojoS 0:ab00d9bcd00d 354 * 0 | Reset | None
JojoS 0:ab00d9bcd00d 355 * 1 | On | 5Hz
JojoS 0:ab00d9bcd00d 356 * 2 | On | 2.5Hz
JojoS 0:ab00d9bcd00d 357 * 3 | On | 1.25Hz
JojoS 0:ab00d9bcd00d 358 * 4 | On | 0.63Hz
JojoS 0:ab00d9bcd00d 359 * 7 | Hold | None
JojoS 0:ab00d9bcd00d 360 * </pre>
JojoS 0:ab00d9bcd00d 361 *
JojoS 0:ab00d9bcd00d 362 * @return Current high-pass filter configuration
JojoS 0:ab00d9bcd00d 363 * @see MPU9150_DHPF_RESET
JojoS 0:ab00d9bcd00d 364 * @see MPU9150_RA_ACCEL_CONFIG
JojoS 0:ab00d9bcd00d 365 */
JojoS 0:ab00d9bcd00d 366 uint8_t MPU9150::getDHPFMode() {
JojoS 0:ab00d9bcd00d 367 i2Cdev.readBits(devAddr, MPU9150_RA_ACCEL_CONFIG, MPU9150_ACONFIG_ACCEL_HPF_BIT, MPU9150_ACONFIG_ACCEL_HPF_LENGTH, buffer);
JojoS 0:ab00d9bcd00d 368 return buffer[0];
JojoS 0:ab00d9bcd00d 369 }
JojoS 0:ab00d9bcd00d 370 /** Set the high-pass filter configuration.
JojoS 0:ab00d9bcd00d 371 * @param bandwidth New high-pass filter configuration
JojoS 0:ab00d9bcd00d 372 * @see setDHPFMode()
JojoS 0:ab00d9bcd00d 373 * @see MPU9150_DHPF_RESET
JojoS 0:ab00d9bcd00d 374 * @see MPU9150_RA_ACCEL_CONFIG
JojoS 0:ab00d9bcd00d 375 */
JojoS 0:ab00d9bcd00d 376 void MPU9150::setDHPFMode(uint8_t bandwidth) {
JojoS 0:ab00d9bcd00d 377 i2Cdev.writeBits(devAddr, MPU9150_RA_ACCEL_CONFIG, MPU9150_ACONFIG_ACCEL_HPF_BIT, MPU9150_ACONFIG_ACCEL_HPF_LENGTH, bandwidth);
JojoS 0:ab00d9bcd00d 378 }
JojoS 0:ab00d9bcd00d 379
JojoS 0:ab00d9bcd00d 380 // FF_THR register
JojoS 0:ab00d9bcd00d 381
JojoS 0:ab00d9bcd00d 382 /** Get free-fall event acceleration threshold.
JojoS 0:ab00d9bcd00d 383 * This register configures the detection threshold for Free Fall event
JojoS 0:ab00d9bcd00d 384 * detection. The unit of FF_THR is 1LSB = 2mg. Free Fall is detected when the
JojoS 0:ab00d9bcd00d 385 * absolute value of the accelerometer measurements for the three axes are each
JojoS 0:ab00d9bcd00d 386 * less than the detection threshold. This condition increments the Free Fall
JojoS 0:ab00d9bcd00d 387 * duration counter (Register 30). The Free Fall interrupt is triggered when the
JojoS 0:ab00d9bcd00d 388 * Free Fall duration counter reaches the time specified in FF_DUR.
JojoS 0:ab00d9bcd00d 389 *
JojoS 0:ab00d9bcd00d 390 * For more details on the Free Fall detection interrupt, see Section 8.2 of the
JojoS 0:ab00d9bcd00d 391 * MPU-6000/MPU-9150 Product Specification document as well as Registers 56 and
JojoS 0:ab00d9bcd00d 392 * 58 of this document.
JojoS 0:ab00d9bcd00d 393 *
JojoS 0:ab00d9bcd00d 394 * @return Current free-fall acceleration threshold value (LSB = 2mg)
JojoS 0:ab00d9bcd00d 395 * @see MPU9150_RA_FF_THR
JojoS 0:ab00d9bcd00d 396 */
JojoS 0:ab00d9bcd00d 397 uint8_t MPU9150::getFreefallDetectionThreshold() {
JojoS 0:ab00d9bcd00d 398 i2Cdev.readByte(devAddr, MPU9150_RA_FF_THR, buffer);
JojoS 0:ab00d9bcd00d 399 return buffer[0];
JojoS 0:ab00d9bcd00d 400 }
JojoS 0:ab00d9bcd00d 401 /** Get free-fall event acceleration threshold.
JojoS 0:ab00d9bcd00d 402 * @param threshold New free-fall acceleration threshold value (LSB = 2mg)
JojoS 0:ab00d9bcd00d 403 * @see getFreefallDetectionThreshold()
JojoS 0:ab00d9bcd00d 404 * @see MPU9150_RA_FF_THR
JojoS 0:ab00d9bcd00d 405 */
JojoS 0:ab00d9bcd00d 406 void MPU9150::setFreefallDetectionThreshold(uint8_t threshold) {
JojoS 0:ab00d9bcd00d 407 i2Cdev.writeByte(devAddr, MPU9150_RA_FF_THR, threshold);
JojoS 0:ab00d9bcd00d 408 }
JojoS 0:ab00d9bcd00d 409
JojoS 0:ab00d9bcd00d 410 // FF_DUR register
JojoS 0:ab00d9bcd00d 411
JojoS 0:ab00d9bcd00d 412 /** Get free-fall event duration threshold.
JojoS 0:ab00d9bcd00d 413 * This register configures the duration counter threshold for Free Fall event
JojoS 0:ab00d9bcd00d 414 * detection. The duration counter ticks at 1kHz, therefore FF_DUR has a unit
JojoS 0:ab00d9bcd00d 415 * of 1 LSB = 1 ms.
JojoS 0:ab00d9bcd00d 416 *
JojoS 0:ab00d9bcd00d 417 * The Free Fall duration counter increments while the absolute value of the
JojoS 0:ab00d9bcd00d 418 * accelerometer measurements are each less than the detection threshold
JojoS 0:ab00d9bcd00d 419 * (Register 29). The Free Fall interrupt is triggered when the Free Fall
JojoS 0:ab00d9bcd00d 420 * duration counter reaches the time specified in this register.
JojoS 0:ab00d9bcd00d 421 *
JojoS 0:ab00d9bcd00d 422 * For more details on the Free Fall detection interrupt, see Section 8.2 of
JojoS 0:ab00d9bcd00d 423 * the MPU-6000/MPU-9150 Product Specification document as well as Registers 56
JojoS 0:ab00d9bcd00d 424 * and 58 of this document.
JojoS 0:ab00d9bcd00d 425 *
JojoS 0:ab00d9bcd00d 426 * @return Current free-fall duration threshold value (LSB = 1ms)
JojoS 0:ab00d9bcd00d 427 * @see MPU9150_RA_FF_DUR
JojoS 0:ab00d9bcd00d 428 */
JojoS 0:ab00d9bcd00d 429 uint8_t MPU9150::getFreefallDetectionDuration() {
JojoS 0:ab00d9bcd00d 430 i2Cdev.readByte(devAddr, MPU9150_RA_FF_DUR, buffer);
JojoS 0:ab00d9bcd00d 431 return buffer[0];
JojoS 0:ab00d9bcd00d 432 }
JojoS 0:ab00d9bcd00d 433 /** Get free-fall event duration threshold.
JojoS 0:ab00d9bcd00d 434 * @param duration New free-fall duration threshold value (LSB = 1ms)
JojoS 0:ab00d9bcd00d 435 * @see getFreefallDetectionDuration()
JojoS 0:ab00d9bcd00d 436 * @see MPU9150_RA_FF_DUR
JojoS 0:ab00d9bcd00d 437 */
JojoS 0:ab00d9bcd00d 438 void MPU9150::setFreefallDetectionDuration(uint8_t duration) {
JojoS 0:ab00d9bcd00d 439 i2Cdev.writeByte(devAddr, MPU9150_RA_FF_DUR, duration);
JojoS 0:ab00d9bcd00d 440 }
JojoS 0:ab00d9bcd00d 441
JojoS 0:ab00d9bcd00d 442 // MOT_THR register
JojoS 0:ab00d9bcd00d 443
JojoS 0:ab00d9bcd00d 444 /** Get motion detection event acceleration threshold.
JojoS 0:ab00d9bcd00d 445 * This register configures the detection threshold for Motion interrupt
JojoS 0:ab00d9bcd00d 446 * generation. The unit of MOT_THR is 1LSB = 2mg. Motion is detected when the
JojoS 0:ab00d9bcd00d 447 * absolute value of any of the accelerometer measurements exceeds this Motion
JojoS 0:ab00d9bcd00d 448 * detection threshold. This condition increments the Motion detection duration
JojoS 0:ab00d9bcd00d 449 * counter (Register 32). The Motion detection interrupt is triggered when the
JojoS 0:ab00d9bcd00d 450 * Motion Detection counter reaches the time count specified in MOT_DUR
JojoS 0:ab00d9bcd00d 451 * (Register 32).
JojoS 0:ab00d9bcd00d 452 *
JojoS 0:ab00d9bcd00d 453 * The Motion interrupt will indicate the axis and polarity of detected motion
JojoS 0:ab00d9bcd00d 454 * in MOT_DETECT_STATUS (Register 97).
JojoS 0:ab00d9bcd00d 455 *
JojoS 0:ab00d9bcd00d 456 * For more details on the Motion detection interrupt, see Section 8.3 of the
JojoS 0:ab00d9bcd00d 457 * MPU-6000/MPU-9150 Product Specification document as well as Registers 56 and
JojoS 0:ab00d9bcd00d 458 * 58 of this document.
JojoS 0:ab00d9bcd00d 459 *
JojoS 0:ab00d9bcd00d 460 * @return Current motion detection acceleration threshold value (LSB = 2mg)
JojoS 0:ab00d9bcd00d 461 * @see MPU9150_RA_MOT_THR
JojoS 0:ab00d9bcd00d 462 */
JojoS 0:ab00d9bcd00d 463 uint8_t MPU9150::getMotionDetectionThreshold() {
JojoS 0:ab00d9bcd00d 464 i2Cdev.readByte(devAddr, MPU9150_RA_MOT_THR, buffer);
JojoS 0:ab00d9bcd00d 465 return buffer[0];
JojoS 0:ab00d9bcd00d 466 }
JojoS 0:ab00d9bcd00d 467 /** Set free-fall event acceleration threshold.
JojoS 0:ab00d9bcd00d 468 * @param threshold New motion detection acceleration threshold value (LSB = 2mg)
JojoS 0:ab00d9bcd00d 469 * @see getMotionDetectionThreshold()
JojoS 0:ab00d9bcd00d 470 * @see MPU9150_RA_MOT_THR
JojoS 0:ab00d9bcd00d 471 */
JojoS 0:ab00d9bcd00d 472 void MPU9150::setMotionDetectionThreshold(uint8_t threshold) {
JojoS 0:ab00d9bcd00d 473 i2Cdev.writeByte(devAddr, MPU9150_RA_MOT_THR, threshold);
JojoS 0:ab00d9bcd00d 474 }
JojoS 0:ab00d9bcd00d 475
JojoS 0:ab00d9bcd00d 476 // MOT_DUR register
JojoS 0:ab00d9bcd00d 477
JojoS 0:ab00d9bcd00d 478 /** Get motion detection event duration threshold.
JojoS 0:ab00d9bcd00d 479 * This register configures the duration counter threshold for Motion interrupt
JojoS 0:ab00d9bcd00d 480 * generation. The duration counter ticks at 1 kHz, therefore MOT_DUR has a unit
JojoS 0:ab00d9bcd00d 481 * of 1LSB = 1ms. The Motion detection duration counter increments when the
JojoS 0:ab00d9bcd00d 482 * absolute value of any of the accelerometer measurements exceeds the Motion
JojoS 0:ab00d9bcd00d 483 * detection threshold (Register 31). The Motion detection interrupt is
JojoS 0:ab00d9bcd00d 484 * triggered when the Motion detection counter reaches the time count specified
JojoS 0:ab00d9bcd00d 485 * in this register.
JojoS 0:ab00d9bcd00d 486 *
JojoS 0:ab00d9bcd00d 487 * For more details on the Motion detection interrupt, see Section 8.3 of the
JojoS 0:ab00d9bcd00d 488 * MPU-6000/MPU-9150 Product Specification document.
JojoS 0:ab00d9bcd00d 489 *
JojoS 0:ab00d9bcd00d 490 * @return Current motion detection duration threshold value (LSB = 1ms)
JojoS 0:ab00d9bcd00d 491 * @see MPU9150_RA_MOT_DUR
JojoS 0:ab00d9bcd00d 492 */
JojoS 0:ab00d9bcd00d 493 uint8_t MPU9150::getMotionDetectionDuration() {
JojoS 0:ab00d9bcd00d 494 i2Cdev.readByte(devAddr, MPU9150_RA_MOT_DUR, buffer);
JojoS 0:ab00d9bcd00d 495 return buffer[0];
JojoS 0:ab00d9bcd00d 496 }
JojoS 0:ab00d9bcd00d 497 /** Set motion detection event duration threshold.
JojoS 0:ab00d9bcd00d 498 * @param duration New motion detection duration threshold value (LSB = 1ms)
JojoS 0:ab00d9bcd00d 499 * @see getMotionDetectionDuration()
JojoS 0:ab00d9bcd00d 500 * @see MPU9150_RA_MOT_DUR
JojoS 0:ab00d9bcd00d 501 */
JojoS 0:ab00d9bcd00d 502 void MPU9150::setMotionDetectionDuration(uint8_t duration) {
JojoS 0:ab00d9bcd00d 503 i2Cdev.writeByte(devAddr, MPU9150_RA_MOT_DUR, duration);
JojoS 0:ab00d9bcd00d 504 }
JojoS 0:ab00d9bcd00d 505
JojoS 0:ab00d9bcd00d 506 // ZRMOT_THR register
JojoS 0:ab00d9bcd00d 507
JojoS 0:ab00d9bcd00d 508 /** Get zero motion detection event acceleration threshold.
JojoS 0:ab00d9bcd00d 509 * This register configures the detection threshold for Zero Motion interrupt
JojoS 0:ab00d9bcd00d 510 * generation. The unit of ZRMOT_THR is 1LSB = 2mg. Zero Motion is detected when
JojoS 0:ab00d9bcd00d 511 * the absolute value of the accelerometer measurements for the 3 axes are each
JojoS 0:ab00d9bcd00d 512 * less than the detection threshold. This condition increments the Zero Motion
JojoS 0:ab00d9bcd00d 513 * duration counter (Register 34). The Zero Motion interrupt is triggered when
JojoS 0:ab00d9bcd00d 514 * the Zero Motion duration counter reaches the time count specified in
JojoS 0:ab00d9bcd00d 515 * ZRMOT_DUR (Register 34).
JojoS 0:ab00d9bcd00d 516 *
JojoS 0:ab00d9bcd00d 517 * Unlike Free Fall or Motion detection, Zero Motion detection triggers an
JojoS 0:ab00d9bcd00d 518 * interrupt both when Zero Motion is first detected and when Zero Motion is no
JojoS 0:ab00d9bcd00d 519 * longer detected.
JojoS 0:ab00d9bcd00d 520 *
JojoS 0:ab00d9bcd00d 521 * When a zero motion event is detected, a Zero Motion Status will be indicated
JojoS 0:ab00d9bcd00d 522 * in the MOT_DETECT_STATUS register (Register 97). When a motion-to-zero-motion
JojoS 0:ab00d9bcd00d 523 * condition is detected, the status bit is set to 1. When a zero-motion-to-
JojoS 0:ab00d9bcd00d 524 * motion condition is detected, the status bit is set to 0.
JojoS 0:ab00d9bcd00d 525 *
JojoS 0:ab00d9bcd00d 526 * For more details on the Zero Motion detection interrupt, see Section 8.4 of
JojoS 0:ab00d9bcd00d 527 * the MPU-6000/MPU-9150 Product Specification document as well as Registers 56
JojoS 0:ab00d9bcd00d 528 * and 58 of this document.
JojoS 0:ab00d9bcd00d 529 *
JojoS 0:ab00d9bcd00d 530 * @return Current zero motion detection acceleration threshold value (LSB = 2mg)
JojoS 0:ab00d9bcd00d 531 * @see MPU9150_RA_ZRMOT_THR
JojoS 0:ab00d9bcd00d 532 */
JojoS 0:ab00d9bcd00d 533 uint8_t MPU9150::getZeroMotionDetectionThreshold() {
JojoS 0:ab00d9bcd00d 534 i2Cdev.readByte(devAddr, MPU9150_RA_ZRMOT_THR, buffer);
JojoS 0:ab00d9bcd00d 535 return buffer[0];
JojoS 0:ab00d9bcd00d 536 }
JojoS 0:ab00d9bcd00d 537 /** Set zero motion detection event acceleration threshold.
JojoS 0:ab00d9bcd00d 538 * @param threshold New zero motion detection acceleration threshold value (LSB = 2mg)
JojoS 0:ab00d9bcd00d 539 * @see getZeroMotionDetectionThreshold()
JojoS 0:ab00d9bcd00d 540 * @see MPU9150_RA_ZRMOT_THR
JojoS 0:ab00d9bcd00d 541 */
JojoS 0:ab00d9bcd00d 542 void MPU9150::setZeroMotionDetectionThreshold(uint8_t threshold) {
JojoS 0:ab00d9bcd00d 543 i2Cdev.writeByte(devAddr, MPU9150_RA_ZRMOT_THR, threshold);
JojoS 0:ab00d9bcd00d 544 }
JojoS 0:ab00d9bcd00d 545
JojoS 0:ab00d9bcd00d 546 // ZRMOT_DUR register
JojoS 0:ab00d9bcd00d 547
JojoS 0:ab00d9bcd00d 548 /** Get zero motion detection event duration threshold.
JojoS 0:ab00d9bcd00d 549 * This register configures the duration counter threshold for Zero Motion
JojoS 0:ab00d9bcd00d 550 * interrupt generation. The duration counter ticks at 16 Hz, therefore
JojoS 0:ab00d9bcd00d 551 * ZRMOT_DUR has a unit of 1 LSB = 64 ms. The Zero Motion duration counter
JojoS 0:ab00d9bcd00d 552 * increments while the absolute value of the accelerometer measurements are
JojoS 0:ab00d9bcd00d 553 * each less than the detection threshold (Register 33). The Zero Motion
JojoS 0:ab00d9bcd00d 554 * interrupt is triggered when the Zero Motion duration counter reaches the time
JojoS 0:ab00d9bcd00d 555 * count specified in this register.
JojoS 0:ab00d9bcd00d 556 *
JojoS 0:ab00d9bcd00d 557 * For more details on the Zero Motion detection interrupt, see Section 8.4 of
JojoS 0:ab00d9bcd00d 558 * the MPU-6000/MPU-9150 Product Specification document, as well as Registers 56
JojoS 0:ab00d9bcd00d 559 * and 58 of this document.
JojoS 0:ab00d9bcd00d 560 *
JojoS 0:ab00d9bcd00d 561 * @return Current zero motion detection duration threshold value (LSB = 64ms)
JojoS 0:ab00d9bcd00d 562 * @see MPU9150_RA_ZRMOT_DUR
JojoS 0:ab00d9bcd00d 563 */
JojoS 0:ab00d9bcd00d 564 uint8_t MPU9150::getZeroMotionDetectionDuration() {
JojoS 0:ab00d9bcd00d 565 i2Cdev.readByte(devAddr, MPU9150_RA_ZRMOT_DUR, buffer);
JojoS 0:ab00d9bcd00d 566 return buffer[0];
JojoS 0:ab00d9bcd00d 567 }
JojoS 0:ab00d9bcd00d 568 /** Set zero motion detection event duration threshold.
JojoS 0:ab00d9bcd00d 569 * @param duration New zero motion detection duration threshold value (LSB = 1ms)
JojoS 0:ab00d9bcd00d 570 * @see getZeroMotionDetectionDuration()
JojoS 0:ab00d9bcd00d 571 * @see MPU9150_RA_ZRMOT_DUR
JojoS 0:ab00d9bcd00d 572 */
JojoS 0:ab00d9bcd00d 573 void MPU9150::setZeroMotionDetectionDuration(uint8_t duration) {
JojoS 0:ab00d9bcd00d 574 i2Cdev.writeByte(devAddr, MPU9150_RA_ZRMOT_DUR, duration);
JojoS 0:ab00d9bcd00d 575 }
JojoS 0:ab00d9bcd00d 576
JojoS 0:ab00d9bcd00d 577 // FIFO_EN register
JojoS 0:ab00d9bcd00d 578
JojoS 0:ab00d9bcd00d 579 /** Get temperature FIFO enabled value.
JojoS 0:ab00d9bcd00d 580 * When set to 1, this bit enables TEMP_OUT_H and TEMP_OUT_L (Registers 65 and
JojoS 0:ab00d9bcd00d 581 * 66) to be written into the FIFO buffer.
JojoS 0:ab00d9bcd00d 582 * @return Current temperature FIFO enabled value
JojoS 0:ab00d9bcd00d 583 * @see MPU9150_RA_FIFO_EN
JojoS 0:ab00d9bcd00d 584 */
JojoS 0:ab00d9bcd00d 585 bool MPU9150::getTempFIFOEnabled() {
JojoS 0:ab00d9bcd00d 586 i2Cdev.readBit(devAddr, MPU9150_RA_FIFO_EN, MPU9150_TEMP_FIFO_EN_BIT, buffer);
JojoS 0:ab00d9bcd00d 587 return buffer[0];
JojoS 0:ab00d9bcd00d 588 }
JojoS 0:ab00d9bcd00d 589 /** Set temperature FIFO enabled value.
JojoS 0:ab00d9bcd00d 590 * @param enabled New temperature FIFO enabled value
JojoS 0:ab00d9bcd00d 591 * @see getTempFIFOEnabled()
JojoS 0:ab00d9bcd00d 592 * @see MPU9150_RA_FIFO_EN
JojoS 0:ab00d9bcd00d 593 */
JojoS 0:ab00d9bcd00d 594 void MPU9150::setTempFIFOEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 595 i2Cdev.writeBit(devAddr, MPU9150_RA_FIFO_EN, MPU9150_TEMP_FIFO_EN_BIT, enabled);
JojoS 0:ab00d9bcd00d 596 }
JojoS 0:ab00d9bcd00d 597 /** Get gyroscope X-axis FIFO enabled value.
JojoS 0:ab00d9bcd00d 598 * When set to 1, this bit enables GYRO_XOUT_H and GYRO_XOUT_L (Registers 67 and
JojoS 0:ab00d9bcd00d 599 * 68) to be written into the FIFO buffer.
JojoS 0:ab00d9bcd00d 600 * @return Current gyroscope X-axis FIFO enabled value
JojoS 0:ab00d9bcd00d 601 * @see MPU9150_RA_FIFO_EN
JojoS 0:ab00d9bcd00d 602 */
JojoS 0:ab00d9bcd00d 603 bool MPU9150::getXGyroFIFOEnabled() {
JojoS 0:ab00d9bcd00d 604 i2Cdev.readBit(devAddr, MPU9150_RA_FIFO_EN, MPU9150_XG_FIFO_EN_BIT, buffer);
JojoS 0:ab00d9bcd00d 605 return buffer[0];
JojoS 0:ab00d9bcd00d 606 }
JojoS 0:ab00d9bcd00d 607 /** Set gyroscope X-axis FIFO enabled value.
JojoS 0:ab00d9bcd00d 608 * @param enabled New gyroscope X-axis FIFO enabled value
JojoS 0:ab00d9bcd00d 609 * @see getXGyroFIFOEnabled()
JojoS 0:ab00d9bcd00d 610 * @see MPU9150_RA_FIFO_EN
JojoS 0:ab00d9bcd00d 611 */
JojoS 0:ab00d9bcd00d 612 void MPU9150::setXGyroFIFOEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 613 i2Cdev.writeBit(devAddr, MPU9150_RA_FIFO_EN, MPU9150_XG_FIFO_EN_BIT, enabled);
JojoS 0:ab00d9bcd00d 614 }
JojoS 0:ab00d9bcd00d 615 /** Get gyroscope Y-axis FIFO enabled value.
JojoS 0:ab00d9bcd00d 616 * When set to 1, this bit enables GYRO_YOUT_H and GYRO_YOUT_L (Registers 69 and
JojoS 0:ab00d9bcd00d 617 * 70) to be written into the FIFO buffer.
JojoS 0:ab00d9bcd00d 618 * @return Current gyroscope Y-axis FIFO enabled value
JojoS 0:ab00d9bcd00d 619 * @see MPU9150_RA_FIFO_EN
JojoS 0:ab00d9bcd00d 620 */
JojoS 0:ab00d9bcd00d 621 bool MPU9150::getYGyroFIFOEnabled() {
JojoS 0:ab00d9bcd00d 622 i2Cdev.readBit(devAddr, MPU9150_RA_FIFO_EN, MPU9150_YG_FIFO_EN_BIT, buffer);
JojoS 0:ab00d9bcd00d 623 return buffer[0];
JojoS 0:ab00d9bcd00d 624 }
JojoS 0:ab00d9bcd00d 625 /** Set gyroscope Y-axis FIFO enabled value.
JojoS 0:ab00d9bcd00d 626 * @param enabled New gyroscope Y-axis FIFO enabled value
JojoS 0:ab00d9bcd00d 627 * @see getYGyroFIFOEnabled()
JojoS 0:ab00d9bcd00d 628 * @see MPU9150_RA_FIFO_EN
JojoS 0:ab00d9bcd00d 629 */
JojoS 0:ab00d9bcd00d 630 void MPU9150::setYGyroFIFOEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 631 i2Cdev.writeBit(devAddr, MPU9150_RA_FIFO_EN, MPU9150_YG_FIFO_EN_BIT, enabled);
JojoS 0:ab00d9bcd00d 632 }
JojoS 0:ab00d9bcd00d 633 /** Get gyroscope Z-axis FIFO enabled value.
JojoS 0:ab00d9bcd00d 634 * When set to 1, this bit enables GYRO_ZOUT_H and GYRO_ZOUT_L (Registers 71 and
JojoS 0:ab00d9bcd00d 635 * 72) to be written into the FIFO buffer.
JojoS 0:ab00d9bcd00d 636 * @return Current gyroscope Z-axis FIFO enabled value
JojoS 0:ab00d9bcd00d 637 * @see MPU9150_RA_FIFO_EN
JojoS 0:ab00d9bcd00d 638 */
JojoS 0:ab00d9bcd00d 639 bool MPU9150::getZGyroFIFOEnabled() {
JojoS 0:ab00d9bcd00d 640 i2Cdev.readBit(devAddr, MPU9150_RA_FIFO_EN, MPU9150_ZG_FIFO_EN_BIT, buffer);
JojoS 0:ab00d9bcd00d 641 return buffer[0];
JojoS 0:ab00d9bcd00d 642 }
JojoS 0:ab00d9bcd00d 643 /** Set gyroscope Z-axis FIFO enabled value.
JojoS 0:ab00d9bcd00d 644 * @param enabled New gyroscope Z-axis FIFO enabled value
JojoS 0:ab00d9bcd00d 645 * @see getZGyroFIFOEnabled()
JojoS 0:ab00d9bcd00d 646 * @see MPU9150_RA_FIFO_EN
JojoS 0:ab00d9bcd00d 647 */
JojoS 0:ab00d9bcd00d 648 void MPU9150::setZGyroFIFOEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 649 i2Cdev.writeBit(devAddr, MPU9150_RA_FIFO_EN, MPU9150_ZG_FIFO_EN_BIT, enabled);
JojoS 0:ab00d9bcd00d 650 }
JojoS 0:ab00d9bcd00d 651 /** Get accelerometer FIFO enabled value.
JojoS 0:ab00d9bcd00d 652 * When set to 1, this bit enables ACCEL_XOUT_H, ACCEL_XOUT_L, ACCEL_YOUT_H,
JojoS 0:ab00d9bcd00d 653 * ACCEL_YOUT_L, ACCEL_ZOUT_H, and ACCEL_ZOUT_L (Registers 59 to 64) to be
JojoS 0:ab00d9bcd00d 654 * written into the FIFO buffer.
JojoS 0:ab00d9bcd00d 655 * @return Current accelerometer FIFO enabled value
JojoS 0:ab00d9bcd00d 656 * @see MPU9150_RA_FIFO_EN
JojoS 0:ab00d9bcd00d 657 */
JojoS 0:ab00d9bcd00d 658 bool MPU9150::getAccelFIFOEnabled() {
JojoS 0:ab00d9bcd00d 659 i2Cdev.readBit(devAddr, MPU9150_RA_FIFO_EN, MPU9150_ACCEL_FIFO_EN_BIT, buffer);
JojoS 0:ab00d9bcd00d 660 return buffer[0];
JojoS 0:ab00d9bcd00d 661 }
JojoS 0:ab00d9bcd00d 662 /** Set accelerometer FIFO enabled value.
JojoS 0:ab00d9bcd00d 663 * @param enabled New accelerometer FIFO enabled value
JojoS 0:ab00d9bcd00d 664 * @see getAccelFIFOEnabled()
JojoS 0:ab00d9bcd00d 665 * @see MPU9150_RA_FIFO_EN
JojoS 0:ab00d9bcd00d 666 */
JojoS 0:ab00d9bcd00d 667 void MPU9150::setAccelFIFOEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 668 i2Cdev.writeBit(devAddr, MPU9150_RA_FIFO_EN, MPU9150_ACCEL_FIFO_EN_BIT, enabled);
JojoS 0:ab00d9bcd00d 669 }
JojoS 0:ab00d9bcd00d 670 /** Get Slave 2 FIFO enabled value.
JojoS 0:ab00d9bcd00d 671 * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96)
JojoS 0:ab00d9bcd00d 672 * associated with Slave 2 to be written into the FIFO buffer.
JojoS 0:ab00d9bcd00d 673 * @return Current Slave 2 FIFO enabled value
JojoS 0:ab00d9bcd00d 674 * @see MPU9150_RA_FIFO_EN
JojoS 0:ab00d9bcd00d 675 */
JojoS 0:ab00d9bcd00d 676 bool MPU9150::getSlave2FIFOEnabled() {
JojoS 0:ab00d9bcd00d 677 i2Cdev.readBit(devAddr, MPU9150_RA_FIFO_EN, MPU9150_SLV2_FIFO_EN_BIT, buffer);
JojoS 0:ab00d9bcd00d 678 return buffer[0];
JojoS 0:ab00d9bcd00d 679 }
JojoS 0:ab00d9bcd00d 680 /** Set Slave 2 FIFO enabled value.
JojoS 0:ab00d9bcd00d 681 * @param enabled New Slave 2 FIFO enabled value
JojoS 0:ab00d9bcd00d 682 * @see getSlave2FIFOEnabled()
JojoS 0:ab00d9bcd00d 683 * @see MPU9150_RA_FIFO_EN
JojoS 0:ab00d9bcd00d 684 */
JojoS 0:ab00d9bcd00d 685 void MPU9150::setSlave2FIFOEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 686 i2Cdev.writeBit(devAddr, MPU9150_RA_FIFO_EN, MPU9150_SLV2_FIFO_EN_BIT, enabled);
JojoS 0:ab00d9bcd00d 687 }
JojoS 0:ab00d9bcd00d 688 /** Get Slave 1 FIFO enabled value.
JojoS 0:ab00d9bcd00d 689 * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96)
JojoS 0:ab00d9bcd00d 690 * associated with Slave 1 to be written into the FIFO buffer.
JojoS 0:ab00d9bcd00d 691 * @return Current Slave 1 FIFO enabled value
JojoS 0:ab00d9bcd00d 692 * @see MPU9150_RA_FIFO_EN
JojoS 0:ab00d9bcd00d 693 */
JojoS 0:ab00d9bcd00d 694 bool MPU9150::getSlave1FIFOEnabled() {
JojoS 0:ab00d9bcd00d 695 i2Cdev.readBit(devAddr, MPU9150_RA_FIFO_EN, MPU9150_SLV1_FIFO_EN_BIT, buffer);
JojoS 0:ab00d9bcd00d 696 return buffer[0];
JojoS 0:ab00d9bcd00d 697 }
JojoS 0:ab00d9bcd00d 698 /** Set Slave 1 FIFO enabled value.
JojoS 0:ab00d9bcd00d 699 * @param enabled New Slave 1 FIFO enabled value
JojoS 0:ab00d9bcd00d 700 * @see getSlave1FIFOEnabled()
JojoS 0:ab00d9bcd00d 701 * @see MPU9150_RA_FIFO_EN
JojoS 0:ab00d9bcd00d 702 */
JojoS 0:ab00d9bcd00d 703 void MPU9150::setSlave1FIFOEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 704 i2Cdev.writeBit(devAddr, MPU9150_RA_FIFO_EN, MPU9150_SLV1_FIFO_EN_BIT, enabled);
JojoS 0:ab00d9bcd00d 705 }
JojoS 0:ab00d9bcd00d 706 /** Get Slave 0 FIFO enabled value.
JojoS 0:ab00d9bcd00d 707 * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96)
JojoS 0:ab00d9bcd00d 708 * associated with Slave 0 to be written into the FIFO buffer.
JojoS 0:ab00d9bcd00d 709 * @return Current Slave 0 FIFO enabled value
JojoS 0:ab00d9bcd00d 710 * @see MPU9150_RA_FIFO_EN
JojoS 0:ab00d9bcd00d 711 */
JojoS 0:ab00d9bcd00d 712 bool MPU9150::getSlave0FIFOEnabled() {
JojoS 0:ab00d9bcd00d 713 i2Cdev.readBit(devAddr, MPU9150_RA_FIFO_EN, MPU9150_SLV0_FIFO_EN_BIT, buffer);
JojoS 0:ab00d9bcd00d 714 return buffer[0];
JojoS 0:ab00d9bcd00d 715 }
JojoS 0:ab00d9bcd00d 716 /** Set Slave 0 FIFO enabled value.
JojoS 0:ab00d9bcd00d 717 * @param enabled New Slave 0 FIFO enabled value
JojoS 0:ab00d9bcd00d 718 * @see getSlave0FIFOEnabled()
JojoS 0:ab00d9bcd00d 719 * @see MPU9150_RA_FIFO_EN
JojoS 0:ab00d9bcd00d 720 */
JojoS 0:ab00d9bcd00d 721 void MPU9150::setSlave0FIFOEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 722 i2Cdev.writeBit(devAddr, MPU9150_RA_FIFO_EN, MPU9150_SLV0_FIFO_EN_BIT, enabled);
JojoS 0:ab00d9bcd00d 723 }
JojoS 0:ab00d9bcd00d 724
JojoS 0:ab00d9bcd00d 725 // I2C_MST_CTRL register
JojoS 0:ab00d9bcd00d 726
JojoS 0:ab00d9bcd00d 727 /** Get multi-master enabled value.
JojoS 0:ab00d9bcd00d 728 * Multi-master capability allows multiple I2C masters to operate on the same
JojoS 0:ab00d9bcd00d 729 * bus. In circuits where multi-master capability is required, set MULT_MST_EN
JojoS 0:ab00d9bcd00d 730 * to 1. This will increase current drawn by approximately 30uA.
JojoS 0:ab00d9bcd00d 731 *
JojoS 0:ab00d9bcd00d 732 * In circuits where multi-master capability is required, the state of the I2C
JojoS 0:ab00d9bcd00d 733 * bus must always be monitored by each separate I2C Master. Before an I2C
JojoS 0:ab00d9bcd00d 734 * Master can assume arbitration of the bus, it must first confirm that no other
JojoS 0:ab00d9bcd00d 735 * I2C Master has arbitration of the bus. When MULT_MST_EN is set to 1, the
JojoS 0:ab00d9bcd00d 736 * MPU-60X0's bus arbitration detection logic is turned on, enabling it to
JojoS 0:ab00d9bcd00d 737 * detect when the bus is available.
JojoS 0:ab00d9bcd00d 738 *
JojoS 0:ab00d9bcd00d 739 * @return Current multi-master enabled value
JojoS 0:ab00d9bcd00d 740 * @see MPU9150_RA_I2C_MST_CTRL
JojoS 0:ab00d9bcd00d 741 */
JojoS 0:ab00d9bcd00d 742 bool MPU9150::getMultiMasterEnabled() {
JojoS 0:ab00d9bcd00d 743 i2Cdev.readBit(devAddr, MPU9150_RA_I2C_MST_CTRL, MPU9150_MULT_MST_EN_BIT, buffer);
JojoS 0:ab00d9bcd00d 744 return buffer[0];
JojoS 0:ab00d9bcd00d 745 }
JojoS 0:ab00d9bcd00d 746 /** Set multi-master enabled value.
JojoS 0:ab00d9bcd00d 747 * @param enabled New multi-master enabled value
JojoS 0:ab00d9bcd00d 748 * @see getMultiMasterEnabled()
JojoS 0:ab00d9bcd00d 749 * @see MPU9150_RA_I2C_MST_CTRL
JojoS 0:ab00d9bcd00d 750 */
JojoS 0:ab00d9bcd00d 751 void MPU9150::setMultiMasterEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 752 i2Cdev.writeBit(devAddr, MPU9150_RA_I2C_MST_CTRL, MPU9150_MULT_MST_EN_BIT, enabled);
JojoS 0:ab00d9bcd00d 753 }
JojoS 0:ab00d9bcd00d 754 /** Get wait-for-external-sensor-data enabled value.
JojoS 0:ab00d9bcd00d 755 * When the WAIT_FOR_ES bit is set to 1, the Data Ready interrupt will be
JojoS 0:ab00d9bcd00d 756 * delayed until External Sensor data from the Slave Devices are loaded into the
JojoS 0:ab00d9bcd00d 757 * EXT_SENS_DATA registers. This is used to ensure that both the internal sensor
JojoS 0:ab00d9bcd00d 758 * data (i.e. from gyro and accel) and external sensor data have been loaded to
JojoS 0:ab00d9bcd00d 759 * their respective data registers (i.e. the data is synced) when the Data Ready
JojoS 0:ab00d9bcd00d 760 * interrupt is triggered.
JojoS 0:ab00d9bcd00d 761 *
JojoS 0:ab00d9bcd00d 762 * @return Current wait-for-external-sensor-data enabled value
JojoS 0:ab00d9bcd00d 763 * @see MPU9150_RA_I2C_MST_CTRL
JojoS 0:ab00d9bcd00d 764 */
JojoS 0:ab00d9bcd00d 765 bool MPU9150::getWaitForExternalSensorEnabled() {
JojoS 0:ab00d9bcd00d 766 i2Cdev.readBit(devAddr, MPU9150_RA_I2C_MST_CTRL, MPU9150_WAIT_FOR_ES_BIT, buffer);
JojoS 0:ab00d9bcd00d 767 return buffer[0];
JojoS 0:ab00d9bcd00d 768 }
JojoS 0:ab00d9bcd00d 769 /** Set wait-for-external-sensor-data enabled value.
JojoS 0:ab00d9bcd00d 770 * @param enabled New wait-for-external-sensor-data enabled value
JojoS 0:ab00d9bcd00d 771 * @see getWaitForExternalSensorEnabled()
JojoS 0:ab00d9bcd00d 772 * @see MPU9150_RA_I2C_MST_CTRL
JojoS 0:ab00d9bcd00d 773 */
JojoS 0:ab00d9bcd00d 774 void MPU9150::setWaitForExternalSensorEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 775 i2Cdev.writeBit(devAddr, MPU9150_RA_I2C_MST_CTRL, MPU9150_WAIT_FOR_ES_BIT, enabled);
JojoS 0:ab00d9bcd00d 776 }
JojoS 0:ab00d9bcd00d 777 /** Get Slave 3 FIFO enabled value.
JojoS 0:ab00d9bcd00d 778 * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96)
JojoS 0:ab00d9bcd00d 779 * associated with Slave 3 to be written into the FIFO buffer.
JojoS 0:ab00d9bcd00d 780 * @return Current Slave 3 FIFO enabled value
JojoS 0:ab00d9bcd00d 781 * @see MPU9150_RA_MST_CTRL
JojoS 0:ab00d9bcd00d 782 */
JojoS 0:ab00d9bcd00d 783 bool MPU9150::getSlave3FIFOEnabled() {
JojoS 0:ab00d9bcd00d 784 i2Cdev.readBit(devAddr, MPU9150_RA_I2C_MST_CTRL, MPU9150_SLV_3_FIFO_EN_BIT, buffer);
JojoS 0:ab00d9bcd00d 785 return buffer[0];
JojoS 0:ab00d9bcd00d 786 }
JojoS 0:ab00d9bcd00d 787 /** Set Slave 3 FIFO enabled value.
JojoS 0:ab00d9bcd00d 788 * @param enabled New Slave 3 FIFO enabled value
JojoS 0:ab00d9bcd00d 789 * @see getSlave3FIFOEnabled()
JojoS 0:ab00d9bcd00d 790 * @see MPU9150_RA_MST_CTRL
JojoS 0:ab00d9bcd00d 791 */
JojoS 0:ab00d9bcd00d 792 void MPU9150::setSlave3FIFOEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 793 i2Cdev.writeBit(devAddr, MPU9150_RA_I2C_MST_CTRL, MPU9150_SLV_3_FIFO_EN_BIT, enabled);
JojoS 0:ab00d9bcd00d 794 }
JojoS 0:ab00d9bcd00d 795 /** Get slave read/write transition enabled value.
JojoS 0:ab00d9bcd00d 796 * The I2C_MST_P_NSR bit configures the I2C Master's transition from one slave
JojoS 0:ab00d9bcd00d 797 * read to the next slave read. If the bit equals 0, there will be a restart
JojoS 0:ab00d9bcd00d 798 * between reads. If the bit equals 1, there will be a stop followed by a start
JojoS 0:ab00d9bcd00d 799 * of the following read. When a write transaction follows a read transaction,
JojoS 0:ab00d9bcd00d 800 * the stop followed by a start of the successive write will be always used.
JojoS 0:ab00d9bcd00d 801 *
JojoS 0:ab00d9bcd00d 802 * @return Current slave read/write transition enabled value
JojoS 0:ab00d9bcd00d 803 * @see MPU9150_RA_I2C_MST_CTRL
JojoS 0:ab00d9bcd00d 804 */
JojoS 0:ab00d9bcd00d 805 bool MPU9150::getSlaveReadWriteTransitionEnabled() {
JojoS 0:ab00d9bcd00d 806 i2Cdev.readBit(devAddr, MPU9150_RA_I2C_MST_CTRL, MPU9150_I2C_MST_P_NSR_BIT, buffer);
JojoS 0:ab00d9bcd00d 807 return buffer[0];
JojoS 0:ab00d9bcd00d 808 }
JojoS 0:ab00d9bcd00d 809 /** Set slave read/write transition enabled value.
JojoS 0:ab00d9bcd00d 810 * @param enabled New slave read/write transition enabled value
JojoS 0:ab00d9bcd00d 811 * @see getSlaveReadWriteTransitionEnabled()
JojoS 0:ab00d9bcd00d 812 * @see MPU9150_RA_I2C_MST_CTRL
JojoS 0:ab00d9bcd00d 813 */
JojoS 0:ab00d9bcd00d 814 void MPU9150::setSlaveReadWriteTransitionEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 815 i2Cdev.writeBit(devAddr, MPU9150_RA_I2C_MST_CTRL, MPU9150_I2C_MST_P_NSR_BIT, enabled);
JojoS 0:ab00d9bcd00d 816 }
JojoS 0:ab00d9bcd00d 817 /** Get I2C master clock speed.
JojoS 0:ab00d9bcd00d 818 * I2C_MST_CLK is a 4 bit unsigned value which configures a divider on the
JojoS 0:ab00d9bcd00d 819 * MPU-60X0 internal 8MHz clock. It sets the I2C master clock speed according to
JojoS 0:ab00d9bcd00d 820 * the following table:
JojoS 0:ab00d9bcd00d 821 *
JojoS 0:ab00d9bcd00d 822 * <pre>
JojoS 0:ab00d9bcd00d 823 * I2C_MST_CLK | I2C Master Clock Speed | 8MHz Clock Divider
JojoS 0:ab00d9bcd00d 824 * ------------+------------------------+-------------------
JojoS 0:ab00d9bcd00d 825 * 0 | 348kHz | 23
JojoS 0:ab00d9bcd00d 826 * 1 | 333kHz | 24
JojoS 0:ab00d9bcd00d 827 * 2 | 320kHz | 25
JojoS 0:ab00d9bcd00d 828 * 3 | 308kHz | 26
JojoS 0:ab00d9bcd00d 829 * 4 | 296kHz | 27
JojoS 0:ab00d9bcd00d 830 * 5 | 286kHz | 28
JojoS 0:ab00d9bcd00d 831 * 6 | 276kHz | 29
JojoS 0:ab00d9bcd00d 832 * 7 | 267kHz | 30
JojoS 0:ab00d9bcd00d 833 * 8 | 258kHz | 31
JojoS 0:ab00d9bcd00d 834 * 9 | 500kHz | 16
JojoS 0:ab00d9bcd00d 835 * 10 | 471kHz | 17
JojoS 0:ab00d9bcd00d 836 * 11 | 444kHz | 18
JojoS 0:ab00d9bcd00d 837 * 12 | 421kHz | 19
JojoS 0:ab00d9bcd00d 838 * 13 | 400kHz | 20
JojoS 0:ab00d9bcd00d 839 * 14 | 381kHz | 21
JojoS 0:ab00d9bcd00d 840 * 15 | 364kHz | 22
JojoS 0:ab00d9bcd00d 841 * </pre>
JojoS 0:ab00d9bcd00d 842 *
JojoS 0:ab00d9bcd00d 843 * @return Current I2C master clock speed
JojoS 0:ab00d9bcd00d 844 * @see MPU9150_RA_I2C_MST_CTRL
JojoS 0:ab00d9bcd00d 845 */
JojoS 0:ab00d9bcd00d 846 uint8_t MPU9150::getMasterClockSpeed() {
JojoS 0:ab00d9bcd00d 847 i2Cdev.readBits(devAddr, MPU9150_RA_I2C_MST_CTRL, MPU9150_I2C_MST_CLK_BIT, MPU9150_I2C_MST_CLK_LENGTH, buffer);
JojoS 0:ab00d9bcd00d 848 return buffer[0];
JojoS 0:ab00d9bcd00d 849 }
JojoS 0:ab00d9bcd00d 850 /** Set I2C master clock speed.
JojoS 0:ab00d9bcd00d 851 * @reparam speed Current I2C master clock speed
JojoS 0:ab00d9bcd00d 852 * @see MPU9150_RA_I2C_MST_CTRL
JojoS 0:ab00d9bcd00d 853 */
JojoS 0:ab00d9bcd00d 854 void MPU9150::setMasterClockSpeed(uint8_t speed) {
JojoS 0:ab00d9bcd00d 855 i2Cdev.writeBits(devAddr, MPU9150_RA_I2C_MST_CTRL, MPU9150_I2C_MST_CLK_BIT, MPU9150_I2C_MST_CLK_LENGTH, speed);
JojoS 0:ab00d9bcd00d 856 }
JojoS 0:ab00d9bcd00d 857
JojoS 0:ab00d9bcd00d 858 // I2C_SLV* registers (Slave 0-3)
JojoS 0:ab00d9bcd00d 859
JojoS 0:ab00d9bcd00d 860 /** Get the I2C address of the specified slave (0-3).
JojoS 0:ab00d9bcd00d 861 * Note that Bit 7 (MSB) controls read/write mode. If Bit 7 is set, it's a read
JojoS 0:ab00d9bcd00d 862 * operation, and if it is cleared, then it's a write operation. The remaining
JojoS 0:ab00d9bcd00d 863 * bits (6-0) are the 7-bit device address of the slave device.
JojoS 0:ab00d9bcd00d 864 *
JojoS 0:ab00d9bcd00d 865 * In read mode, the result of the read is placed in the lowest available
JojoS 0:ab00d9bcd00d 866 * EXT_SENS_DATA register. For further information regarding the allocation of
JojoS 0:ab00d9bcd00d 867 * read results, please refer to the EXT_SENS_DATA register description
JojoS 0:ab00d9bcd00d 868 * (Registers 73 - 96).
JojoS 0:ab00d9bcd00d 869 *
JojoS 0:ab00d9bcd00d 870 * The MPU-9150 supports a total of five slaves, but Slave 4 has unique
JojoS 0:ab00d9bcd00d 871 * characteristics, and so it has its own functions (getSlave4* and setSlave4*).
JojoS 0:ab00d9bcd00d 872 *
JojoS 0:ab00d9bcd00d 873 * I2C data transactions are performed at the Sample Rate, as defined in
JojoS 0:ab00d9bcd00d 874 * Register 25. The user is responsible for ensuring that I2C data transactions
JojoS 0:ab00d9bcd00d 875 * to and from each enabled Slave can be completed within a single period of the
JojoS 0:ab00d9bcd00d 876 * Sample Rate.
JojoS 0:ab00d9bcd00d 877 *
JojoS 0:ab00d9bcd00d 878 * The I2C slave access rate can be reduced relative to the Sample Rate. This
JojoS 0:ab00d9bcd00d 879 * reduced access rate is determined by I2C_MST_DLY (Register 52). Whether a
JojoS 0:ab00d9bcd00d 880 * slave's access rate is reduced relative to the Sample Rate is determined by
JojoS 0:ab00d9bcd00d 881 * I2C_MST_DELAY_CTRL (Register 103).
JojoS 0:ab00d9bcd00d 882 *
JojoS 0:ab00d9bcd00d 883 * The processing order for the slaves is fixed. The sequence followed for
JojoS 0:ab00d9bcd00d 884 * processing the slaves is Slave 0, Slave 1, Slave 2, Slave 3 and Slave 4. If a
JojoS 0:ab00d9bcd00d 885 * particular Slave is disabled it will be skipped.
JojoS 0:ab00d9bcd00d 886 *
JojoS 0:ab00d9bcd00d 887 * Each slave can either be accessed at the sample rate or at a reduced sample
JojoS 0:ab00d9bcd00d 888 * rate. In a case where some slaves are accessed at the Sample Rate and some
JojoS 0:ab00d9bcd00d 889 * slaves are accessed at the reduced rate, the sequence of accessing the slaves
JojoS 0:ab00d9bcd00d 890 * (Slave 0 to Slave 4) is still followed. However, the reduced rate slaves will
JojoS 0:ab00d9bcd00d 891 * be skipped if their access rate dictates that they should not be accessed
JojoS 0:ab00d9bcd00d 892 * during that particular cycle. For further information regarding the reduced
JojoS 0:ab00d9bcd00d 893 * access rate, please refer to Register 52. Whether a slave is accessed at the
JojoS 0:ab00d9bcd00d 894 * Sample Rate or at the reduced rate is determined by the Delay Enable bits in
JojoS 0:ab00d9bcd00d 895 * Register 103.
JojoS 0:ab00d9bcd00d 896 *
JojoS 0:ab00d9bcd00d 897 * @param num Slave number (0-3)
JojoS 0:ab00d9bcd00d 898 * @return Current address for specified slave
JojoS 0:ab00d9bcd00d 899 * @see MPU9150_RA_I2C_SLV0_ADDR
JojoS 0:ab00d9bcd00d 900 */
JojoS 0:ab00d9bcd00d 901 uint8_t MPU9150::getSlaveAddress(uint8_t num) {
JojoS 0:ab00d9bcd00d 902 if (num > 3) return 0;
JojoS 0:ab00d9bcd00d 903 i2Cdev.readByte(devAddr, MPU9150_RA_I2C_SLV0_ADDR + num*3, buffer);
JojoS 0:ab00d9bcd00d 904 return buffer[0];
JojoS 0:ab00d9bcd00d 905 }
JojoS 0:ab00d9bcd00d 906 /** Set the I2C address of the specified slave (0-3).
JojoS 0:ab00d9bcd00d 907 * @param num Slave number (0-3)
JojoS 0:ab00d9bcd00d 908 * @param address New address for specified slave
JojoS 0:ab00d9bcd00d 909 * @see getSlaveAddress()
JojoS 0:ab00d9bcd00d 910 * @see MPU9150_RA_I2C_SLV0_ADDR
JojoS 0:ab00d9bcd00d 911 */
JojoS 0:ab00d9bcd00d 912 void MPU9150::setSlaveAddress(uint8_t num, uint8_t address) {
JojoS 0:ab00d9bcd00d 913 if (num > 3) return;
JojoS 0:ab00d9bcd00d 914 i2Cdev.writeByte(devAddr, MPU9150_RA_I2C_SLV0_ADDR + num*3, address);
JojoS 0:ab00d9bcd00d 915 }
JojoS 0:ab00d9bcd00d 916 /** Get the active internal register for the specified slave (0-3).
JojoS 0:ab00d9bcd00d 917 * Read/write operations for this slave will be done to whatever internal
JojoS 0:ab00d9bcd00d 918 * register address is stored in this MPU register.
JojoS 0:ab00d9bcd00d 919 *
JojoS 0:ab00d9bcd00d 920 * The MPU-9150 supports a total of five slaves, but Slave 4 has unique
JojoS 0:ab00d9bcd00d 921 * characteristics, and so it has its own functions.
JojoS 0:ab00d9bcd00d 922 *
JojoS 0:ab00d9bcd00d 923 * @param num Slave number (0-3)
JojoS 0:ab00d9bcd00d 924 * @return Current active register for specified slave
JojoS 0:ab00d9bcd00d 925 * @see MPU9150_RA_I2C_SLV0_REG
JojoS 0:ab00d9bcd00d 926 */
JojoS 0:ab00d9bcd00d 927 uint8_t MPU9150::getSlaveRegister(uint8_t num) {
JojoS 0:ab00d9bcd00d 928 if (num > 3) return 0;
JojoS 0:ab00d9bcd00d 929 i2Cdev.readByte(devAddr, MPU9150_RA_I2C_SLV0_REG + num*3, buffer);
JojoS 0:ab00d9bcd00d 930 return buffer[0];
JojoS 0:ab00d9bcd00d 931 }
JojoS 0:ab00d9bcd00d 932 /** Set the active internal register for the specified slave (0-3).
JojoS 0:ab00d9bcd00d 933 * @param num Slave number (0-3)
JojoS 0:ab00d9bcd00d 934 * @param reg New active register for specified slave
JojoS 0:ab00d9bcd00d 935 * @see getSlaveRegister()
JojoS 0:ab00d9bcd00d 936 * @see MPU9150_RA_I2C_SLV0_REG
JojoS 0:ab00d9bcd00d 937 */
JojoS 0:ab00d9bcd00d 938 void MPU9150::setSlaveRegister(uint8_t num, uint8_t reg) {
JojoS 0:ab00d9bcd00d 939 if (num > 3) return;
JojoS 0:ab00d9bcd00d 940 i2Cdev.writeByte(devAddr, MPU9150_RA_I2C_SLV0_REG + num*3, reg);
JojoS 0:ab00d9bcd00d 941 }
JojoS 0:ab00d9bcd00d 942 /** Get the enabled value for the specified slave (0-3).
JojoS 0:ab00d9bcd00d 943 * When set to 1, this bit enables Slave 0 for data transfer operations. When
JojoS 0:ab00d9bcd00d 944 * cleared to 0, this bit disables Slave 0 from data transfer operations.
JojoS 0:ab00d9bcd00d 945 * @param num Slave number (0-3)
JojoS 0:ab00d9bcd00d 946 * @return Current enabled value for specified slave
JojoS 0:ab00d9bcd00d 947 * @see MPU9150_RA_I2C_SLV0_CTRL
JojoS 0:ab00d9bcd00d 948 */
JojoS 0:ab00d9bcd00d 949 bool MPU9150::getSlaveEnabled(uint8_t num) {
JojoS 0:ab00d9bcd00d 950 if (num > 3) return 0;
JojoS 0:ab00d9bcd00d 951 i2Cdev.readBit(devAddr, MPU9150_RA_I2C_SLV0_CTRL + num*3, MPU9150_I2C_SLV_EN_BIT, buffer);
JojoS 0:ab00d9bcd00d 952 return buffer[0];
JojoS 0:ab00d9bcd00d 953 }
JojoS 0:ab00d9bcd00d 954 /** Set the enabled value for the specified slave (0-3).
JojoS 0:ab00d9bcd00d 955 * @param num Slave number (0-3)
JojoS 0:ab00d9bcd00d 956 * @param enabled New enabled value for specified slave
JojoS 0:ab00d9bcd00d 957 * @see getSlaveEnabled()
JojoS 0:ab00d9bcd00d 958 * @see MPU9150_RA_I2C_SLV0_CTRL
JojoS 0:ab00d9bcd00d 959 */
JojoS 0:ab00d9bcd00d 960 void MPU9150::setSlaveEnabled(uint8_t num, bool enabled) {
JojoS 0:ab00d9bcd00d 961 if (num > 3) return;
JojoS 0:ab00d9bcd00d 962 i2Cdev.writeBit(devAddr, MPU9150_RA_I2C_SLV0_CTRL + num*3, MPU9150_I2C_SLV_EN_BIT, enabled);
JojoS 0:ab00d9bcd00d 963 }
JojoS 0:ab00d9bcd00d 964 /** Get word pair byte-swapping enabled for the specified slave (0-3).
JojoS 0:ab00d9bcd00d 965 * When set to 1, this bit enables byte swapping. When byte swapping is enabled,
JojoS 0:ab00d9bcd00d 966 * the high and low bytes of a word pair are swapped. Please refer to
JojoS 0:ab00d9bcd00d 967 * I2C_SLV0_GRP for the pairing convention of the word pairs. When cleared to 0,
JojoS 0:ab00d9bcd00d 968 * bytes transferred to and from Slave 0 will be written to EXT_SENS_DATA
JojoS 0:ab00d9bcd00d 969 * registers in the order they were transferred.
JojoS 0:ab00d9bcd00d 970 *
JojoS 0:ab00d9bcd00d 971 * @param num Slave number (0-3)
JojoS 0:ab00d9bcd00d 972 * @return Current word pair byte-swapping enabled value for specified slave
JojoS 0:ab00d9bcd00d 973 * @see MPU9150_RA_I2C_SLV0_CTRL
JojoS 0:ab00d9bcd00d 974 */
JojoS 0:ab00d9bcd00d 975 bool MPU9150::getSlaveWordByteSwap(uint8_t num) {
JojoS 0:ab00d9bcd00d 976 if (num > 3) return 0;
JojoS 0:ab00d9bcd00d 977 i2Cdev.readBit(devAddr, MPU9150_RA_I2C_SLV0_CTRL + num*3, MPU9150_I2C_SLV_BYTE_SW_BIT, buffer);
JojoS 0:ab00d9bcd00d 978 return buffer[0];
JojoS 0:ab00d9bcd00d 979 }
JojoS 0:ab00d9bcd00d 980 /** Set word pair byte-swapping enabled for the specified slave (0-3).
JojoS 0:ab00d9bcd00d 981 * @param num Slave number (0-3)
JojoS 0:ab00d9bcd00d 982 * @param enabled New word pair byte-swapping enabled value for specified slave
JojoS 0:ab00d9bcd00d 983 * @see getSlaveWordByteSwap()
JojoS 0:ab00d9bcd00d 984 * @see MPU9150_RA_I2C_SLV0_CTRL
JojoS 0:ab00d9bcd00d 985 */
JojoS 0:ab00d9bcd00d 986 void MPU9150::setSlaveWordByteSwap(uint8_t num, bool enabled) {
JojoS 0:ab00d9bcd00d 987 if (num > 3) return;
JojoS 0:ab00d9bcd00d 988 i2Cdev.writeBit(devAddr, MPU9150_RA_I2C_SLV0_CTRL + num*3, MPU9150_I2C_SLV_BYTE_SW_BIT, enabled);
JojoS 0:ab00d9bcd00d 989 }
JojoS 0:ab00d9bcd00d 990 /** Get write mode for the specified slave (0-3).
JojoS 0:ab00d9bcd00d 991 * When set to 1, the transaction will read or write data only. When cleared to
JojoS 0:ab00d9bcd00d 992 * 0, the transaction will write a register address prior to reading or writing
JojoS 0:ab00d9bcd00d 993 * data. This should equal 0 when specifying the register address within the
JojoS 0:ab00d9bcd00d 994 * Slave device to/from which the ensuing data transaction will take place.
JojoS 0:ab00d9bcd00d 995 *
JojoS 0:ab00d9bcd00d 996 * @param num Slave number (0-3)
JojoS 0:ab00d9bcd00d 997 * @return Current write mode for specified slave (0 = register address + data, 1 = data only)
JojoS 0:ab00d9bcd00d 998 * @see MPU9150_RA_I2C_SLV0_CTRL
JojoS 0:ab00d9bcd00d 999 */
JojoS 0:ab00d9bcd00d 1000 bool MPU9150::getSlaveWriteMode(uint8_t num) {
JojoS 0:ab00d9bcd00d 1001 if (num > 3) return 0;
JojoS 0:ab00d9bcd00d 1002 i2Cdev.readBit(devAddr, MPU9150_RA_I2C_SLV0_CTRL + num*3, MPU9150_I2C_SLV_REG_DIS_BIT, buffer);
JojoS 0:ab00d9bcd00d 1003 return buffer[0];
JojoS 0:ab00d9bcd00d 1004 }
JojoS 0:ab00d9bcd00d 1005 /** Set write mode for the specified slave (0-3).
JojoS 0:ab00d9bcd00d 1006 * @param num Slave number (0-3)
JojoS 0:ab00d9bcd00d 1007 * @param mode New write mode for specified slave (0 = register address + data, 1 = data only)
JojoS 0:ab00d9bcd00d 1008 * @see getSlaveWriteMode()
JojoS 0:ab00d9bcd00d 1009 * @see MPU9150_RA_I2C_SLV0_CTRL
JojoS 0:ab00d9bcd00d 1010 */
JojoS 0:ab00d9bcd00d 1011 void MPU9150::setSlaveWriteMode(uint8_t num, bool mode) {
JojoS 0:ab00d9bcd00d 1012 if (num > 3) return;
JojoS 0:ab00d9bcd00d 1013 i2Cdev.writeBit(devAddr, MPU9150_RA_I2C_SLV0_CTRL + num*3, MPU9150_I2C_SLV_REG_DIS_BIT, mode);
JojoS 0:ab00d9bcd00d 1014 }
JojoS 0:ab00d9bcd00d 1015 /** Get word pair grouping order offset for the specified slave (0-3).
JojoS 0:ab00d9bcd00d 1016 * This sets specifies the grouping order of word pairs received from registers.
JojoS 0:ab00d9bcd00d 1017 * When cleared to 0, bytes from register addresses 0 and 1, 2 and 3, etc (even,
JojoS 0:ab00d9bcd00d 1018 * then odd register addresses) are paired to form a word. When set to 1, bytes
JojoS 0:ab00d9bcd00d 1019 * from register addresses are paired 1 and 2, 3 and 4, etc. (odd, then even
JojoS 0:ab00d9bcd00d 1020 * register addresses) are paired to form a word.
JojoS 0:ab00d9bcd00d 1021 *
JojoS 0:ab00d9bcd00d 1022 * @param num Slave number (0-3)
JojoS 0:ab00d9bcd00d 1023 * @return Current word pair grouping order offset for specified slave
JojoS 0:ab00d9bcd00d 1024 * @see MPU9150_RA_I2C_SLV0_CTRL
JojoS 0:ab00d9bcd00d 1025 */
JojoS 0:ab00d9bcd00d 1026 bool MPU9150::getSlaveWordGroupOffset(uint8_t num) {
JojoS 0:ab00d9bcd00d 1027 if (num > 3) return 0;
JojoS 0:ab00d9bcd00d 1028 i2Cdev.readBit(devAddr, MPU9150_RA_I2C_SLV0_CTRL + num*3, MPU9150_I2C_SLV_GRP_BIT, buffer);
JojoS 0:ab00d9bcd00d 1029 return buffer[0];
JojoS 0:ab00d9bcd00d 1030 }
JojoS 0:ab00d9bcd00d 1031 /** Set word pair grouping order offset for the specified slave (0-3).
JojoS 0:ab00d9bcd00d 1032 * @param num Slave number (0-3)
JojoS 0:ab00d9bcd00d 1033 * @param enabled New word pair grouping order offset for specified slave
JojoS 0:ab00d9bcd00d 1034 * @see getSlaveWordGroupOffset()
JojoS 0:ab00d9bcd00d 1035 * @see MPU9150_RA_I2C_SLV0_CTRL
JojoS 0:ab00d9bcd00d 1036 */
JojoS 0:ab00d9bcd00d 1037 void MPU9150::setSlaveWordGroupOffset(uint8_t num, bool enabled) {
JojoS 0:ab00d9bcd00d 1038 if (num > 3) return;
JojoS 0:ab00d9bcd00d 1039 i2Cdev.writeBit(devAddr, MPU9150_RA_I2C_SLV0_CTRL + num*3, MPU9150_I2C_SLV_GRP_BIT, enabled);
JojoS 0:ab00d9bcd00d 1040 }
JojoS 0:ab00d9bcd00d 1041 /** Get number of bytes to read for the specified slave (0-3).
JojoS 0:ab00d9bcd00d 1042 * Specifies the number of bytes transferred to and from Slave 0. Clearing this
JojoS 0:ab00d9bcd00d 1043 * bit to 0 is equivalent to disabling the register by writing 0 to I2C_SLV0_EN.
JojoS 0:ab00d9bcd00d 1044 * @param num Slave number (0-3)
JojoS 0:ab00d9bcd00d 1045 * @return Number of bytes to read for specified slave
JojoS 0:ab00d9bcd00d 1046 * @see MPU9150_RA_I2C_SLV0_CTRL
JojoS 0:ab00d9bcd00d 1047 */
JojoS 0:ab00d9bcd00d 1048 uint8_t MPU9150::getSlaveDataLength(uint8_t num) {
JojoS 0:ab00d9bcd00d 1049 if (num > 3) return 0;
JojoS 0:ab00d9bcd00d 1050 i2Cdev.readBits(devAddr, MPU9150_RA_I2C_SLV0_CTRL + num*3, MPU9150_I2C_SLV_LEN_BIT, MPU9150_I2C_SLV_LEN_LENGTH, buffer);
JojoS 0:ab00d9bcd00d 1051 return buffer[0];
JojoS 0:ab00d9bcd00d 1052 }
JojoS 0:ab00d9bcd00d 1053 /** Set number of bytes to read for the specified slave (0-3).
JojoS 0:ab00d9bcd00d 1054 * @param num Slave number (0-3)
JojoS 0:ab00d9bcd00d 1055 * @param length Number of bytes to read for specified slave
JojoS 0:ab00d9bcd00d 1056 * @see getSlaveDataLength()
JojoS 0:ab00d9bcd00d 1057 * @see MPU9150_RA_I2C_SLV0_CTRL
JojoS 0:ab00d9bcd00d 1058 */
JojoS 0:ab00d9bcd00d 1059 void MPU9150::setSlaveDataLength(uint8_t num, uint8_t length) {
JojoS 0:ab00d9bcd00d 1060 if (num > 3) return;
JojoS 0:ab00d9bcd00d 1061 i2Cdev.writeBits(devAddr, MPU9150_RA_I2C_SLV0_CTRL + num*3, MPU9150_I2C_SLV_LEN_BIT, MPU9150_I2C_SLV_LEN_LENGTH, length);
JojoS 0:ab00d9bcd00d 1062 }
JojoS 0:ab00d9bcd00d 1063
JojoS 0:ab00d9bcd00d 1064 // I2C_SLV* registers (Slave 4)
JojoS 0:ab00d9bcd00d 1065
JojoS 0:ab00d9bcd00d 1066 /** Get the I2C address of Slave 4.
JojoS 0:ab00d9bcd00d 1067 * Note that Bit 7 (MSB) controls read/write mode. If Bit 7 is set, it's a read
JojoS 0:ab00d9bcd00d 1068 * operation, and if it is cleared, then it's a write operation. The remaining
JojoS 0:ab00d9bcd00d 1069 * bits (6-0) are the 7-bit device address of the slave device.
JojoS 0:ab00d9bcd00d 1070 *
JojoS 0:ab00d9bcd00d 1071 * @return Current address for Slave 4
JojoS 0:ab00d9bcd00d 1072 * @see getSlaveAddress()
JojoS 0:ab00d9bcd00d 1073 * @see MPU9150_RA_I2C_SLV4_ADDR
JojoS 0:ab00d9bcd00d 1074 */
JojoS 0:ab00d9bcd00d 1075 uint8_t MPU9150::getSlave4Address() {
JojoS 0:ab00d9bcd00d 1076 i2Cdev.readByte(devAddr, MPU9150_RA_I2C_SLV4_ADDR, buffer);
JojoS 0:ab00d9bcd00d 1077 return buffer[0];
JojoS 0:ab00d9bcd00d 1078 }
JojoS 0:ab00d9bcd00d 1079 /** Set the I2C address of Slave 4.
JojoS 0:ab00d9bcd00d 1080 * @param address New address for Slave 4
JojoS 0:ab00d9bcd00d 1081 * @see getSlave4Address()
JojoS 0:ab00d9bcd00d 1082 * @see MPU9150_RA_I2C_SLV4_ADDR
JojoS 0:ab00d9bcd00d 1083 */
JojoS 0:ab00d9bcd00d 1084 void MPU9150::setSlave4Address(uint8_t address) {
JojoS 0:ab00d9bcd00d 1085 i2Cdev.writeByte(devAddr, MPU9150_RA_I2C_SLV4_ADDR, address);
JojoS 0:ab00d9bcd00d 1086 }
JojoS 0:ab00d9bcd00d 1087 /** Get the active internal register for the Slave 4.
JojoS 0:ab00d9bcd00d 1088 * Read/write operations for this slave will be done to whatever internal
JojoS 0:ab00d9bcd00d 1089 * register address is stored in this MPU register.
JojoS 0:ab00d9bcd00d 1090 *
JojoS 0:ab00d9bcd00d 1091 * @return Current active register for Slave 4
JojoS 0:ab00d9bcd00d 1092 * @see MPU9150_RA_I2C_SLV4_REG
JojoS 0:ab00d9bcd00d 1093 */
JojoS 0:ab00d9bcd00d 1094 uint8_t MPU9150::getSlave4Register() {
JojoS 0:ab00d9bcd00d 1095 i2Cdev.readByte(devAddr, MPU9150_RA_I2C_SLV4_REG, buffer);
JojoS 0:ab00d9bcd00d 1096 return buffer[0];
JojoS 0:ab00d9bcd00d 1097 }
JojoS 0:ab00d9bcd00d 1098 /** Set the active internal register for Slave 4.
JojoS 0:ab00d9bcd00d 1099 * @param reg New active register for Slave 4
JojoS 0:ab00d9bcd00d 1100 * @see getSlave4Register()
JojoS 0:ab00d9bcd00d 1101 * @see MPU9150_RA_I2C_SLV4_REG
JojoS 0:ab00d9bcd00d 1102 */
JojoS 0:ab00d9bcd00d 1103 void MPU9150::setSlave4Register(uint8_t reg) {
JojoS 0:ab00d9bcd00d 1104 i2Cdev.writeByte(devAddr, MPU9150_RA_I2C_SLV4_REG, reg);
JojoS 0:ab00d9bcd00d 1105 }
JojoS 0:ab00d9bcd00d 1106 /** Set new byte to write to Slave 4.
JojoS 0:ab00d9bcd00d 1107 * This register stores the data to be written into the Slave 4. If I2C_SLV4_RW
JojoS 0:ab00d9bcd00d 1108 * is set 1 (set to read), this register has no effect.
JojoS 0:ab00d9bcd00d 1109 * @param data New byte to write to Slave 4
JojoS 0:ab00d9bcd00d 1110 * @see MPU9150_RA_I2C_SLV4_DO
JojoS 0:ab00d9bcd00d 1111 */
JojoS 0:ab00d9bcd00d 1112 void MPU9150::setSlave4OutputByte(uint8_t data) {
JojoS 0:ab00d9bcd00d 1113 i2Cdev.writeByte(devAddr, MPU9150_RA_I2C_SLV4_DO, data);
JojoS 0:ab00d9bcd00d 1114 }
JojoS 0:ab00d9bcd00d 1115 /** Get the enabled value for the Slave 4.
JojoS 0:ab00d9bcd00d 1116 * When set to 1, this bit enables Slave 4 for data transfer operations. When
JojoS 0:ab00d9bcd00d 1117 * cleared to 0, this bit disables Slave 4 from data transfer operations.
JojoS 0:ab00d9bcd00d 1118 * @return Current enabled value for Slave 4
JojoS 0:ab00d9bcd00d 1119 * @see MPU9150_RA_I2C_SLV4_CTRL
JojoS 0:ab00d9bcd00d 1120 */
JojoS 0:ab00d9bcd00d 1121 bool MPU9150::getSlave4Enabled() {
JojoS 0:ab00d9bcd00d 1122 i2Cdev.readBit(devAddr, MPU9150_RA_I2C_SLV4_CTRL, MPU9150_I2C_SLV4_EN_BIT, buffer);
JojoS 0:ab00d9bcd00d 1123 return buffer[0];
JojoS 0:ab00d9bcd00d 1124 }
JojoS 0:ab00d9bcd00d 1125 /** Set the enabled value for Slave 4.
JojoS 0:ab00d9bcd00d 1126 * @param enabled New enabled value for Slave 4
JojoS 0:ab00d9bcd00d 1127 * @see getSlave4Enabled()
JojoS 0:ab00d9bcd00d 1128 * @see MPU9150_RA_I2C_SLV4_CTRL
JojoS 0:ab00d9bcd00d 1129 */
JojoS 0:ab00d9bcd00d 1130 void MPU9150::setSlave4Enabled(bool enabled) {
JojoS 0:ab00d9bcd00d 1131 i2Cdev.writeBit(devAddr, MPU9150_RA_I2C_SLV4_CTRL, MPU9150_I2C_SLV4_EN_BIT, enabled);
JojoS 0:ab00d9bcd00d 1132 }
JojoS 0:ab00d9bcd00d 1133 /** Get the enabled value for Slave 4 transaction interrupts.
JojoS 0:ab00d9bcd00d 1134 * When set to 1, this bit enables the generation of an interrupt signal upon
JojoS 0:ab00d9bcd00d 1135 * completion of a Slave 4 transaction. When cleared to 0, this bit disables the
JojoS 0:ab00d9bcd00d 1136 * generation of an interrupt signal upon completion of a Slave 4 transaction.
JojoS 0:ab00d9bcd00d 1137 * The interrupt status can be observed in Register 54.
JojoS 0:ab00d9bcd00d 1138 *
JojoS 0:ab00d9bcd00d 1139 * @return Current enabled value for Slave 4 transaction interrupts.
JojoS 0:ab00d9bcd00d 1140 * @see MPU9150_RA_I2C_SLV4_CTRL
JojoS 0:ab00d9bcd00d 1141 */
JojoS 0:ab00d9bcd00d 1142 bool MPU9150::getSlave4InterruptEnabled() {
JojoS 0:ab00d9bcd00d 1143 i2Cdev.readBit(devAddr, MPU9150_RA_I2C_SLV4_CTRL, MPU9150_I2C_SLV4_INT_EN_BIT, buffer);
JojoS 0:ab00d9bcd00d 1144 return buffer[0];
JojoS 0:ab00d9bcd00d 1145 }
JojoS 0:ab00d9bcd00d 1146 /** Set the enabled value for Slave 4 transaction interrupts.
JojoS 0:ab00d9bcd00d 1147 * @param enabled New enabled value for Slave 4 transaction interrupts.
JojoS 0:ab00d9bcd00d 1148 * @see getSlave4InterruptEnabled()
JojoS 0:ab00d9bcd00d 1149 * @see MPU9150_RA_I2C_SLV4_CTRL
JojoS 0:ab00d9bcd00d 1150 */
JojoS 0:ab00d9bcd00d 1151 void MPU9150::setSlave4InterruptEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 1152 i2Cdev.writeBit(devAddr, MPU9150_RA_I2C_SLV4_CTRL, MPU9150_I2C_SLV4_INT_EN_BIT, enabled);
JojoS 0:ab00d9bcd00d 1153 }
JojoS 0:ab00d9bcd00d 1154 /** Get write mode for Slave 4.
JojoS 0:ab00d9bcd00d 1155 * When set to 1, the transaction will read or write data only. When cleared to
JojoS 0:ab00d9bcd00d 1156 * 0, the transaction will write a register address prior to reading or writing
JojoS 0:ab00d9bcd00d 1157 * data. This should equal 0 when specifying the register address within the
JojoS 0:ab00d9bcd00d 1158 * Slave device to/from which the ensuing data transaction will take place.
JojoS 0:ab00d9bcd00d 1159 *
JojoS 0:ab00d9bcd00d 1160 * @return Current write mode for Slave 4 (0 = register address + data, 1 = data only)
JojoS 0:ab00d9bcd00d 1161 * @see MPU9150_RA_I2C_SLV4_CTRL
JojoS 0:ab00d9bcd00d 1162 */
JojoS 0:ab00d9bcd00d 1163 bool MPU9150::getSlave4WriteMode() {
JojoS 0:ab00d9bcd00d 1164 i2Cdev.readBit(devAddr, MPU9150_RA_I2C_SLV4_CTRL, MPU9150_I2C_SLV4_REG_DIS_BIT, buffer);
JojoS 0:ab00d9bcd00d 1165 return buffer[0];
JojoS 0:ab00d9bcd00d 1166 }
JojoS 0:ab00d9bcd00d 1167 /** Set write mode for the Slave 4.
JojoS 0:ab00d9bcd00d 1168 * @param mode New write mode for Slave 4 (0 = register address + data, 1 = data only)
JojoS 0:ab00d9bcd00d 1169 * @see getSlave4WriteMode()
JojoS 0:ab00d9bcd00d 1170 * @see MPU9150_RA_I2C_SLV4_CTRL
JojoS 0:ab00d9bcd00d 1171 */
JojoS 0:ab00d9bcd00d 1172 void MPU9150::setSlave4WriteMode(bool mode) {
JojoS 0:ab00d9bcd00d 1173 i2Cdev.writeBit(devAddr, MPU9150_RA_I2C_SLV4_CTRL, MPU9150_I2C_SLV4_REG_DIS_BIT, mode);
JojoS 0:ab00d9bcd00d 1174 }
JojoS 0:ab00d9bcd00d 1175 /** Get Slave 4 master delay value.
JojoS 0:ab00d9bcd00d 1176 * This configures the reduced access rate of I2C slaves relative to the Sample
JojoS 0:ab00d9bcd00d 1177 * Rate. When a slave's access rate is decreased relative to the Sample Rate,
JojoS 0:ab00d9bcd00d 1178 * the slave is accessed every:
JojoS 0:ab00d9bcd00d 1179 *
JojoS 0:ab00d9bcd00d 1180 * 1 / (1 + I2C_MST_DLY) samples
JojoS 0:ab00d9bcd00d 1181 *
JojoS 0:ab00d9bcd00d 1182 * This base Sample Rate in turn is determined by SMPLRT_DIV (register 25) and
JojoS 0:ab00d9bcd00d 1183 * DLPF_CFG (register 26). Whether a slave's access rate is reduced relative to
JojoS 0:ab00d9bcd00d 1184 * the Sample Rate is determined by I2C_MST_DELAY_CTRL (register 103). For
JojoS 0:ab00d9bcd00d 1185 * further information regarding the Sample Rate, please refer to register 25.
JojoS 0:ab00d9bcd00d 1186 *
JojoS 0:ab00d9bcd00d 1187 * @return Current Slave 4 master delay value
JojoS 0:ab00d9bcd00d 1188 * @see MPU9150_RA_I2C_SLV4_CTRL
JojoS 0:ab00d9bcd00d 1189 */
JojoS 0:ab00d9bcd00d 1190 uint8_t MPU9150::getSlave4MasterDelay() {
JojoS 0:ab00d9bcd00d 1191 i2Cdev.readBits(devAddr, MPU9150_RA_I2C_SLV4_CTRL, MPU9150_I2C_SLV4_MST_DLY_BIT, MPU9150_I2C_SLV4_MST_DLY_LENGTH, buffer);
JojoS 0:ab00d9bcd00d 1192 return buffer[0];
JojoS 0:ab00d9bcd00d 1193 }
JojoS 0:ab00d9bcd00d 1194 /** Set Slave 4 master delay value.
JojoS 0:ab00d9bcd00d 1195 * @param delay New Slave 4 master delay value
JojoS 0:ab00d9bcd00d 1196 * @see getSlave4MasterDelay()
JojoS 0:ab00d9bcd00d 1197 * @see MPU9150_RA_I2C_SLV4_CTRL
JojoS 0:ab00d9bcd00d 1198 */
JojoS 0:ab00d9bcd00d 1199 void MPU9150::setSlave4MasterDelay(uint8_t delay) {
JojoS 0:ab00d9bcd00d 1200 i2Cdev.writeBits(devAddr, MPU9150_RA_I2C_SLV4_CTRL, MPU9150_I2C_SLV4_MST_DLY_BIT, MPU9150_I2C_SLV4_MST_DLY_LENGTH, delay);
JojoS 0:ab00d9bcd00d 1201 }
JojoS 0:ab00d9bcd00d 1202 /** Get last available byte read from Slave 4.
JojoS 0:ab00d9bcd00d 1203 * This register stores the data read from Slave 4. This field is populated
JojoS 0:ab00d9bcd00d 1204 * after a read transaction.
JojoS 0:ab00d9bcd00d 1205 * @return Last available byte read from to Slave 4
JojoS 0:ab00d9bcd00d 1206 * @see MPU9150_RA_I2C_SLV4_DI
JojoS 0:ab00d9bcd00d 1207 */
JojoS 0:ab00d9bcd00d 1208 uint8_t MPU9150::getSlate4InputByte() {
JojoS 0:ab00d9bcd00d 1209 i2Cdev.readByte(devAddr, MPU9150_RA_I2C_SLV4_DI, buffer);
JojoS 0:ab00d9bcd00d 1210 return buffer[0];
JojoS 0:ab00d9bcd00d 1211 }
JojoS 0:ab00d9bcd00d 1212
JojoS 0:ab00d9bcd00d 1213 // I2C_MST_STATUS register
JojoS 0:ab00d9bcd00d 1214
JojoS 0:ab00d9bcd00d 1215 /** Get FSYNC interrupt status.
JojoS 0:ab00d9bcd00d 1216 * This bit reflects the status of the FSYNC interrupt from an external device
JojoS 0:ab00d9bcd00d 1217 * into the MPU-60X0. This is used as a way to pass an external interrupt
JojoS 0:ab00d9bcd00d 1218 * through the MPU-60X0 to the host application processor. When set to 1, this
JojoS 0:ab00d9bcd00d 1219 * bit will cause an interrupt if FSYNC_INT_EN is asserted in INT_PIN_CFG
JojoS 0:ab00d9bcd00d 1220 * (Register 55).
JojoS 0:ab00d9bcd00d 1221 * @return FSYNC interrupt status
JojoS 0:ab00d9bcd00d 1222 * @see MPU9150_RA_I2C_MST_STATUS
JojoS 0:ab00d9bcd00d 1223 */
JojoS 0:ab00d9bcd00d 1224 bool MPU9150::getPassthroughStatus() {
JojoS 0:ab00d9bcd00d 1225 i2Cdev.readBit(devAddr, MPU9150_RA_I2C_MST_STATUS, MPU9150_MST_PASS_THROUGH_BIT, buffer);
JojoS 0:ab00d9bcd00d 1226 return buffer[0];
JojoS 0:ab00d9bcd00d 1227 }
JojoS 0:ab00d9bcd00d 1228 /** Get Slave 4 transaction done status.
JojoS 0:ab00d9bcd00d 1229 * Automatically sets to 1 when a Slave 4 transaction has completed. This
JojoS 0:ab00d9bcd00d 1230 * triggers an interrupt if the I2C_MST_INT_EN bit in the INT_ENABLE register
JojoS 0:ab00d9bcd00d 1231 * (Register 56) is asserted and if the SLV_4_DONE_INT bit is asserted in the
JojoS 0:ab00d9bcd00d 1232 * I2C_SLV4_CTRL register (Register 52).
JojoS 0:ab00d9bcd00d 1233 * @return Slave 4 transaction done status
JojoS 0:ab00d9bcd00d 1234 * @see MPU9150_RA_I2C_MST_STATUS
JojoS 0:ab00d9bcd00d 1235 */
JojoS 0:ab00d9bcd00d 1236 bool MPU9150::getSlave4IsDone() {
JojoS 0:ab00d9bcd00d 1237 i2Cdev.readBit(devAddr, MPU9150_RA_I2C_MST_STATUS, MPU9150_MST_I2C_SLV4_DONE_BIT, buffer);
JojoS 0:ab00d9bcd00d 1238 return buffer[0];
JojoS 0:ab00d9bcd00d 1239 }
JojoS 0:ab00d9bcd00d 1240 /** Get master arbitration lost status.
JojoS 0:ab00d9bcd00d 1241 * This bit automatically sets to 1 when the I2C Master has lost arbitration of
JojoS 0:ab00d9bcd00d 1242 * the auxiliary I2C bus (an error condition). This triggers an interrupt if the
JojoS 0:ab00d9bcd00d 1243 * I2C_MST_INT_EN bit in the INT_ENABLE register (Register 56) is asserted.
JojoS 0:ab00d9bcd00d 1244 * @return Master arbitration lost status
JojoS 0:ab00d9bcd00d 1245 * @see MPU9150_RA_I2C_MST_STATUS
JojoS 0:ab00d9bcd00d 1246 */
JojoS 0:ab00d9bcd00d 1247 bool MPU9150::getLostArbitration() {
JojoS 0:ab00d9bcd00d 1248 i2Cdev.readBit(devAddr, MPU9150_RA_I2C_MST_STATUS, MPU9150_MST_I2C_LOST_ARB_BIT, buffer);
JojoS 0:ab00d9bcd00d 1249 return buffer[0];
JojoS 0:ab00d9bcd00d 1250 }
JojoS 0:ab00d9bcd00d 1251 /** Get Slave 4 NACK status.
JojoS 0:ab00d9bcd00d 1252 * This bit automatically sets to 1 when the I2C Master receives a NACK in a
JojoS 0:ab00d9bcd00d 1253 * transaction with Slave 4. This triggers an interrupt if the I2C_MST_INT_EN
JojoS 0:ab00d9bcd00d 1254 * bit in the INT_ENABLE register (Register 56) is asserted.
JojoS 0:ab00d9bcd00d 1255 * @return Slave 4 NACK interrupt status
JojoS 0:ab00d9bcd00d 1256 * @see MPU9150_RA_I2C_MST_STATUS
JojoS 0:ab00d9bcd00d 1257 */
JojoS 0:ab00d9bcd00d 1258 bool MPU9150::getSlave4Nack() {
JojoS 0:ab00d9bcd00d 1259 i2Cdev.readBit(devAddr, MPU9150_RA_I2C_MST_STATUS, MPU9150_MST_I2C_SLV4_NACK_BIT, buffer);
JojoS 0:ab00d9bcd00d 1260 return buffer[0];
JojoS 0:ab00d9bcd00d 1261 }
JojoS 0:ab00d9bcd00d 1262 /** Get Slave 3 NACK status.
JojoS 0:ab00d9bcd00d 1263 * This bit automatically sets to 1 when the I2C Master receives a NACK in a
JojoS 0:ab00d9bcd00d 1264 * transaction with Slave 3. This triggers an interrupt if the I2C_MST_INT_EN
JojoS 0:ab00d9bcd00d 1265 * bit in the INT_ENABLE register (Register 56) is asserted.
JojoS 0:ab00d9bcd00d 1266 * @return Slave 3 NACK interrupt status
JojoS 0:ab00d9bcd00d 1267 * @see MPU9150_RA_I2C_MST_STATUS
JojoS 0:ab00d9bcd00d 1268 */
JojoS 0:ab00d9bcd00d 1269 bool MPU9150::getSlave3Nack() {
JojoS 0:ab00d9bcd00d 1270 i2Cdev.readBit(devAddr, MPU9150_RA_I2C_MST_STATUS, MPU9150_MST_I2C_SLV3_NACK_BIT, buffer);
JojoS 0:ab00d9bcd00d 1271 return buffer[0];
JojoS 0:ab00d9bcd00d 1272 }
JojoS 0:ab00d9bcd00d 1273 /** Get Slave 2 NACK status.
JojoS 0:ab00d9bcd00d 1274 * This bit automatically sets to 1 when the I2C Master receives a NACK in a
JojoS 0:ab00d9bcd00d 1275 * transaction with Slave 2. This triggers an interrupt if the I2C_MST_INT_EN
JojoS 0:ab00d9bcd00d 1276 * bit in the INT_ENABLE register (Register 56) is asserted.
JojoS 0:ab00d9bcd00d 1277 * @return Slave 2 NACK interrupt status
JojoS 0:ab00d9bcd00d 1278 * @see MPU9150_RA_I2C_MST_STATUS
JojoS 0:ab00d9bcd00d 1279 */
JojoS 0:ab00d9bcd00d 1280 bool MPU9150::getSlave2Nack() {
JojoS 0:ab00d9bcd00d 1281 i2Cdev.readBit(devAddr, MPU9150_RA_I2C_MST_STATUS, MPU9150_MST_I2C_SLV2_NACK_BIT, buffer);
JojoS 0:ab00d9bcd00d 1282 return buffer[0];
JojoS 0:ab00d9bcd00d 1283 }
JojoS 0:ab00d9bcd00d 1284 /** Get Slave 1 NACK status.
JojoS 0:ab00d9bcd00d 1285 * This bit automatically sets to 1 when the I2C Master receives a NACK in a
JojoS 0:ab00d9bcd00d 1286 * transaction with Slave 1. This triggers an interrupt if the I2C_MST_INT_EN
JojoS 0:ab00d9bcd00d 1287 * bit in the INT_ENABLE register (Register 56) is asserted.
JojoS 0:ab00d9bcd00d 1288 * @return Slave 1 NACK interrupt status
JojoS 0:ab00d9bcd00d 1289 * @see MPU9150_RA_I2C_MST_STATUS
JojoS 0:ab00d9bcd00d 1290 */
JojoS 0:ab00d9bcd00d 1291 bool MPU9150::getSlave1Nack() {
JojoS 0:ab00d9bcd00d 1292 i2Cdev.readBit(devAddr, MPU9150_RA_I2C_MST_STATUS, MPU9150_MST_I2C_SLV1_NACK_BIT, buffer);
JojoS 0:ab00d9bcd00d 1293 return buffer[0];
JojoS 0:ab00d9bcd00d 1294 }
JojoS 0:ab00d9bcd00d 1295 /** Get Slave 0 NACK status.
JojoS 0:ab00d9bcd00d 1296 * This bit automatically sets to 1 when the I2C Master receives a NACK in a
JojoS 0:ab00d9bcd00d 1297 * transaction with Slave 0. This triggers an interrupt if the I2C_MST_INT_EN
JojoS 0:ab00d9bcd00d 1298 * bit in the INT_ENABLE register (Register 56) is asserted.
JojoS 0:ab00d9bcd00d 1299 * @return Slave 0 NACK interrupt status
JojoS 0:ab00d9bcd00d 1300 * @see MPU9150_RA_I2C_MST_STATUS
JojoS 0:ab00d9bcd00d 1301 */
JojoS 0:ab00d9bcd00d 1302 bool MPU9150::getSlave0Nack() {
JojoS 0:ab00d9bcd00d 1303 i2Cdev.readBit(devAddr, MPU9150_RA_I2C_MST_STATUS, MPU9150_MST_I2C_SLV0_NACK_BIT, buffer);
JojoS 0:ab00d9bcd00d 1304 return buffer[0];
JojoS 0:ab00d9bcd00d 1305 }
JojoS 0:ab00d9bcd00d 1306
JojoS 0:ab00d9bcd00d 1307 // INT_PIN_CFG register
JojoS 0:ab00d9bcd00d 1308
JojoS 0:ab00d9bcd00d 1309 /** Get interrupt logic level mode.
JojoS 0:ab00d9bcd00d 1310 * Will be set 0 for active-high, 1 for active-low.
JojoS 0:ab00d9bcd00d 1311 * @return Current interrupt mode (0=active-high, 1=active-low)
JojoS 0:ab00d9bcd00d 1312 * @see MPU9150_RA_INT_PIN_CFG
JojoS 0:ab00d9bcd00d 1313 * @see MPU9150_INTCFG_INT_LEVEL_BIT
JojoS 0:ab00d9bcd00d 1314 */
JojoS 0:ab00d9bcd00d 1315 bool MPU9150::getInterruptMode() {
JojoS 0:ab00d9bcd00d 1316 i2Cdev.readBit(devAddr, MPU9150_RA_INT_PIN_CFG, MPU9150_INTCFG_INT_LEVEL_BIT, buffer);
JojoS 0:ab00d9bcd00d 1317 return buffer[0];
JojoS 0:ab00d9bcd00d 1318 }
JojoS 0:ab00d9bcd00d 1319 /** Set interrupt logic level mode.
JojoS 0:ab00d9bcd00d 1320 * @param mode New interrupt mode (0=active-high, 1=active-low)
JojoS 0:ab00d9bcd00d 1321 * @see getInterruptMode()
JojoS 0:ab00d9bcd00d 1322 * @see MPU9150_RA_INT_PIN_CFG
JojoS 0:ab00d9bcd00d 1323 * @see MPU9150_INTCFG_INT_LEVEL_BIT
JojoS 0:ab00d9bcd00d 1324 */
JojoS 0:ab00d9bcd00d 1325 void MPU9150::setInterruptMode(bool mode) {
JojoS 0:ab00d9bcd00d 1326 i2Cdev.writeBit(devAddr, MPU9150_RA_INT_PIN_CFG, MPU9150_INTCFG_INT_LEVEL_BIT, mode);
JojoS 0:ab00d9bcd00d 1327 }
JojoS 0:ab00d9bcd00d 1328 /** Get interrupt drive mode.
JojoS 0:ab00d9bcd00d 1329 * Will be set 0 for push-pull, 1 for open-drain.
JojoS 0:ab00d9bcd00d 1330 * @return Current interrupt drive mode (0=push-pull, 1=open-drain)
JojoS 0:ab00d9bcd00d 1331 * @see MPU9150_RA_INT_PIN_CFG
JojoS 0:ab00d9bcd00d 1332 * @see MPU9150_INTCFG_INT_OPEN_BIT
JojoS 0:ab00d9bcd00d 1333 */
JojoS 0:ab00d9bcd00d 1334 bool MPU9150::getInterruptDrive() {
JojoS 0:ab00d9bcd00d 1335 i2Cdev.readBit(devAddr, MPU9150_RA_INT_PIN_CFG, MPU9150_INTCFG_INT_OPEN_BIT, buffer);
JojoS 0:ab00d9bcd00d 1336 return buffer[0];
JojoS 0:ab00d9bcd00d 1337 }
JojoS 0:ab00d9bcd00d 1338 /** Set interrupt drive mode.
JojoS 0:ab00d9bcd00d 1339 * @param drive New interrupt drive mode (0=push-pull, 1=open-drain)
JojoS 0:ab00d9bcd00d 1340 * @see getInterruptDrive()
JojoS 0:ab00d9bcd00d 1341 * @see MPU9150_RA_INT_PIN_CFG
JojoS 0:ab00d9bcd00d 1342 * @see MPU9150_INTCFG_INT_OPEN_BIT
JojoS 0:ab00d9bcd00d 1343 */
JojoS 0:ab00d9bcd00d 1344 void MPU9150::setInterruptDrive(bool drive) {
JojoS 0:ab00d9bcd00d 1345 i2Cdev.writeBit(devAddr, MPU9150_RA_INT_PIN_CFG, MPU9150_INTCFG_INT_OPEN_BIT, drive);
JojoS 0:ab00d9bcd00d 1346 }
JojoS 0:ab00d9bcd00d 1347 /** Get interrupt latch mode.
JojoS 0:ab00d9bcd00d 1348 * Will be set 0 for 50us-pulse, 1 for latch-until-int-cleared.
JojoS 0:ab00d9bcd00d 1349 * @return Current latch mode (0=50us-pulse, 1=latch-until-int-cleared)
JojoS 0:ab00d9bcd00d 1350 * @see MPU9150_RA_INT_PIN_CFG
JojoS 0:ab00d9bcd00d 1351 * @see MPU9150_INTCFG_LATCH_INT_EN_BIT
JojoS 0:ab00d9bcd00d 1352 */
JojoS 0:ab00d9bcd00d 1353 bool MPU9150::getInterruptLatch() {
JojoS 0:ab00d9bcd00d 1354 i2Cdev.readBit(devAddr, MPU9150_RA_INT_PIN_CFG, MPU9150_INTCFG_LATCH_INT_EN_BIT, buffer);
JojoS 0:ab00d9bcd00d 1355 return buffer[0];
JojoS 0:ab00d9bcd00d 1356 }
JojoS 0:ab00d9bcd00d 1357 /** Set interrupt latch mode.
JojoS 0:ab00d9bcd00d 1358 * @param latch New latch mode (0=50us-pulse, 1=latch-until-int-cleared)
JojoS 0:ab00d9bcd00d 1359 * @see getInterruptLatch()
JojoS 0:ab00d9bcd00d 1360 * @see MPU9150_RA_INT_PIN_CFG
JojoS 0:ab00d9bcd00d 1361 * @see MPU9150_INTCFG_LATCH_INT_EN_BIT
JojoS 0:ab00d9bcd00d 1362 */
JojoS 0:ab00d9bcd00d 1363 void MPU9150::setInterruptLatch(bool latch) {
JojoS 0:ab00d9bcd00d 1364 i2Cdev.writeBit(devAddr, MPU9150_RA_INT_PIN_CFG, MPU9150_INTCFG_LATCH_INT_EN_BIT, latch);
JojoS 0:ab00d9bcd00d 1365 }
JojoS 0:ab00d9bcd00d 1366 /** Get interrupt latch clear mode.
JojoS 0:ab00d9bcd00d 1367 * Will be set 0 for status-read-only, 1 for any-register-read.
JojoS 0:ab00d9bcd00d 1368 * @return Current latch clear mode (0=status-read-only, 1=any-register-read)
JojoS 0:ab00d9bcd00d 1369 * @see MPU9150_RA_INT_PIN_CFG
JojoS 0:ab00d9bcd00d 1370 * @see MPU9150_INTCFG_INT_RD_CLEAR_BIT
JojoS 0:ab00d9bcd00d 1371 */
JojoS 0:ab00d9bcd00d 1372 bool MPU9150::getInterruptLatchClear() {
JojoS 0:ab00d9bcd00d 1373 i2Cdev.readBit(devAddr, MPU9150_RA_INT_PIN_CFG, MPU9150_INTCFG_INT_RD_CLEAR_BIT, buffer);
JojoS 0:ab00d9bcd00d 1374 return buffer[0];
JojoS 0:ab00d9bcd00d 1375 }
JojoS 0:ab00d9bcd00d 1376 /** Set interrupt latch clear mode.
JojoS 0:ab00d9bcd00d 1377 * @param clear New latch clear mode (0=status-read-only, 1=any-register-read)
JojoS 0:ab00d9bcd00d 1378 * @see getInterruptLatchClear()
JojoS 0:ab00d9bcd00d 1379 * @see MPU9150_RA_INT_PIN_CFG
JojoS 0:ab00d9bcd00d 1380 * @see MPU9150_INTCFG_INT_RD_CLEAR_BIT
JojoS 0:ab00d9bcd00d 1381 */
JojoS 0:ab00d9bcd00d 1382 void MPU9150::setInterruptLatchClear(bool clear) {
JojoS 0:ab00d9bcd00d 1383 i2Cdev.writeBit(devAddr, MPU9150_RA_INT_PIN_CFG, MPU9150_INTCFG_INT_RD_CLEAR_BIT, clear);
JojoS 0:ab00d9bcd00d 1384 }
JojoS 0:ab00d9bcd00d 1385 /** Get FSYNC interrupt logic level mode.
JojoS 0:ab00d9bcd00d 1386 * @return Current FSYNC interrupt mode (0=active-high, 1=active-low)
JojoS 0:ab00d9bcd00d 1387 * @see getFSyncInterruptMode()
JojoS 0:ab00d9bcd00d 1388 * @see MPU9150_RA_INT_PIN_CFG
JojoS 0:ab00d9bcd00d 1389 * @see MPU9150_INTCFG_FSYNC_INT_LEVEL_BIT
JojoS 0:ab00d9bcd00d 1390 */
JojoS 0:ab00d9bcd00d 1391 bool MPU9150::getFSyncInterruptLevel() {
JojoS 0:ab00d9bcd00d 1392 i2Cdev.readBit(devAddr, MPU9150_RA_INT_PIN_CFG, MPU9150_INTCFG_FSYNC_INT_LEVEL_BIT, buffer);
JojoS 0:ab00d9bcd00d 1393 return buffer[0];
JojoS 0:ab00d9bcd00d 1394 }
JojoS 0:ab00d9bcd00d 1395 /** Set FSYNC interrupt logic level mode.
JojoS 0:ab00d9bcd00d 1396 * @param mode New FSYNC interrupt mode (0=active-high, 1=active-low)
JojoS 0:ab00d9bcd00d 1397 * @see getFSyncInterruptMode()
JojoS 0:ab00d9bcd00d 1398 * @see MPU9150_RA_INT_PIN_CFG
JojoS 0:ab00d9bcd00d 1399 * @see MPU9150_INTCFG_FSYNC_INT_LEVEL_BIT
JojoS 0:ab00d9bcd00d 1400 */
JojoS 0:ab00d9bcd00d 1401 void MPU9150::setFSyncInterruptLevel(bool level) {
JojoS 0:ab00d9bcd00d 1402 i2Cdev.writeBit(devAddr, MPU9150_RA_INT_PIN_CFG, MPU9150_INTCFG_FSYNC_INT_LEVEL_BIT, level);
JojoS 0:ab00d9bcd00d 1403 }
JojoS 0:ab00d9bcd00d 1404 /** Get FSYNC pin interrupt enabled setting.
JojoS 0:ab00d9bcd00d 1405 * Will be set 0 for disabled, 1 for enabled.
JojoS 0:ab00d9bcd00d 1406 * @return Current interrupt enabled setting
JojoS 0:ab00d9bcd00d 1407 * @see MPU9150_RA_INT_PIN_CFG
JojoS 0:ab00d9bcd00d 1408 * @see MPU9150_INTCFG_FSYNC_INT_EN_BIT
JojoS 0:ab00d9bcd00d 1409 */
JojoS 0:ab00d9bcd00d 1410 bool MPU9150::getFSyncInterruptEnabled() {
JojoS 0:ab00d9bcd00d 1411 i2Cdev.readBit(devAddr, MPU9150_RA_INT_PIN_CFG, MPU9150_INTCFG_FSYNC_INT_EN_BIT, buffer);
JojoS 0:ab00d9bcd00d 1412 return buffer[0];
JojoS 0:ab00d9bcd00d 1413 }
JojoS 0:ab00d9bcd00d 1414 /** Set FSYNC pin interrupt enabled setting.
JojoS 0:ab00d9bcd00d 1415 * @param enabled New FSYNC pin interrupt enabled setting
JojoS 0:ab00d9bcd00d 1416 * @see getFSyncInterruptEnabled()
JojoS 0:ab00d9bcd00d 1417 * @see MPU9150_RA_INT_PIN_CFG
JojoS 0:ab00d9bcd00d 1418 * @see MPU9150_INTCFG_FSYNC_INT_EN_BIT
JojoS 0:ab00d9bcd00d 1419 */
JojoS 0:ab00d9bcd00d 1420 void MPU9150::setFSyncInterruptEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 1421 i2Cdev.writeBit(devAddr, MPU9150_RA_INT_PIN_CFG, MPU9150_INTCFG_FSYNC_INT_EN_BIT, enabled);
JojoS 0:ab00d9bcd00d 1422 }
JojoS 0:ab00d9bcd00d 1423 /** Get I2C bypass enabled status.
JojoS 0:ab00d9bcd00d 1424 * When this bit is equal to 1 and I2C_MST_EN (Register 106 bit[5]) is equal to
JojoS 0:ab00d9bcd00d 1425 * 0, the host application processor will be able to directly access the
JojoS 0:ab00d9bcd00d 1426 * auxiliary I2C bus of the MPU-60X0. When this bit is equal to 0, the host
JojoS 0:ab00d9bcd00d 1427 * application processor will not be able to directly access the auxiliary I2C
JojoS 0:ab00d9bcd00d 1428 * bus of the MPU-60X0 regardless of the state of I2C_MST_EN (Register 106
JojoS 0:ab00d9bcd00d 1429 * bit[5]).
JojoS 0:ab00d9bcd00d 1430 * @return Current I2C bypass enabled status
JojoS 0:ab00d9bcd00d 1431 * @see MPU9150_RA_INT_PIN_CFG
JojoS 0:ab00d9bcd00d 1432 * @see MPU9150_INTCFG_I2C_BYPASS_EN_BIT
JojoS 0:ab00d9bcd00d 1433 */
JojoS 0:ab00d9bcd00d 1434 bool MPU9150::getI2CBypassEnabled() {
JojoS 0:ab00d9bcd00d 1435 i2Cdev.readBit(devAddr, MPU9150_RA_INT_PIN_CFG, MPU9150_INTCFG_I2C_BYPASS_EN_BIT, buffer);
JojoS 0:ab00d9bcd00d 1436 return buffer[0];
JojoS 0:ab00d9bcd00d 1437 }
JojoS 0:ab00d9bcd00d 1438 /** Set I2C bypass enabled status.
JojoS 0:ab00d9bcd00d 1439 * When this bit is equal to 1 and I2C_MST_EN (Register 106 bit[5]) is equal to
JojoS 0:ab00d9bcd00d 1440 * 0, the host application processor will be able to directly access the
JojoS 0:ab00d9bcd00d 1441 * auxiliary I2C bus of the MPU-60X0. When this bit is equal to 0, the host
JojoS 0:ab00d9bcd00d 1442 * application processor will not be able to directly access the auxiliary I2C
JojoS 0:ab00d9bcd00d 1443 * bus of the MPU-60X0 regardless of the state of I2C_MST_EN (Register 106
JojoS 0:ab00d9bcd00d 1444 * bit[5]).
JojoS 0:ab00d9bcd00d 1445 * @param enabled New I2C bypass enabled status
JojoS 0:ab00d9bcd00d 1446 * @see MPU9150_RA_INT_PIN_CFG
JojoS 0:ab00d9bcd00d 1447 * @see MPU9150_INTCFG_I2C_BYPASS_EN_BIT
JojoS 0:ab00d9bcd00d 1448 */
JojoS 0:ab00d9bcd00d 1449 void MPU9150::setI2CBypassEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 1450 i2Cdev.writeBit(devAddr, MPU9150_RA_INT_PIN_CFG, MPU9150_INTCFG_I2C_BYPASS_EN_BIT, enabled);
JojoS 0:ab00d9bcd00d 1451 }
JojoS 0:ab00d9bcd00d 1452 /** Get reference clock output enabled status.
JojoS 0:ab00d9bcd00d 1453 * When this bit is equal to 1, a reference clock output is provided at the
JojoS 0:ab00d9bcd00d 1454 * CLKOUT pin. When this bit is equal to 0, the clock output is disabled. For
JojoS 0:ab00d9bcd00d 1455 * further information regarding CLKOUT, please refer to the MPU-60X0 Product
JojoS 0:ab00d9bcd00d 1456 * Specification document.
JojoS 0:ab00d9bcd00d 1457 * @return Current reference clock output enabled status
JojoS 0:ab00d9bcd00d 1458 * @see MPU9150_RA_INT_PIN_CFG
JojoS 0:ab00d9bcd00d 1459 * @see MPU9150_INTCFG_CLKOUT_EN_BIT
JojoS 0:ab00d9bcd00d 1460 */
JojoS 0:ab00d9bcd00d 1461 bool MPU9150::getClockOutputEnabled() {
JojoS 0:ab00d9bcd00d 1462 i2Cdev.readBit(devAddr, MPU9150_RA_INT_PIN_CFG, MPU9150_INTCFG_CLKOUT_EN_BIT, buffer);
JojoS 0:ab00d9bcd00d 1463 return buffer[0];
JojoS 0:ab00d9bcd00d 1464 }
JojoS 0:ab00d9bcd00d 1465 /** Set reference clock output enabled status.
JojoS 0:ab00d9bcd00d 1466 * When this bit is equal to 1, a reference clock output is provided at the
JojoS 0:ab00d9bcd00d 1467 * CLKOUT pin. When this bit is equal to 0, the clock output is disabled. For
JojoS 0:ab00d9bcd00d 1468 * further information regarding CLKOUT, please refer to the MPU-60X0 Product
JojoS 0:ab00d9bcd00d 1469 * Specification document.
JojoS 0:ab00d9bcd00d 1470 * @param enabled New reference clock output enabled status
JojoS 0:ab00d9bcd00d 1471 * @see MPU9150_RA_INT_PIN_CFG
JojoS 0:ab00d9bcd00d 1472 * @see MPU9150_INTCFG_CLKOUT_EN_BIT
JojoS 0:ab00d9bcd00d 1473 */
JojoS 0:ab00d9bcd00d 1474 void MPU9150::setClockOutputEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 1475 i2Cdev.writeBit(devAddr, MPU9150_RA_INT_PIN_CFG, MPU9150_INTCFG_CLKOUT_EN_BIT, enabled);
JojoS 0:ab00d9bcd00d 1476 }
JojoS 0:ab00d9bcd00d 1477
JojoS 0:ab00d9bcd00d 1478 // INT_ENABLE register
JojoS 0:ab00d9bcd00d 1479
JojoS 0:ab00d9bcd00d 1480 /** Get full interrupt enabled status.
JojoS 0:ab00d9bcd00d 1481 * Full register byte for all interrupts, for quick reading. Each bit will be
JojoS 0:ab00d9bcd00d 1482 * set 0 for disabled, 1 for enabled.
JojoS 0:ab00d9bcd00d 1483 * @return Current interrupt enabled status
JojoS 0:ab00d9bcd00d 1484 * @see MPU9150_RA_INT_ENABLE
JojoS 0:ab00d9bcd00d 1485 * @see MPU9150_INTERRUPT_FF_BIT
JojoS 0:ab00d9bcd00d 1486 **/
JojoS 0:ab00d9bcd00d 1487 uint8_t MPU9150::getIntEnabled() {
JojoS 0:ab00d9bcd00d 1488 i2Cdev.readByte(devAddr, MPU9150_RA_INT_ENABLE, buffer);
JojoS 0:ab00d9bcd00d 1489 return buffer[0];
JojoS 0:ab00d9bcd00d 1490 }
JojoS 0:ab00d9bcd00d 1491 /** Set full interrupt enabled status.
JojoS 0:ab00d9bcd00d 1492 * Full register byte for all interrupts, for quick reading. Each bit should be
JojoS 0:ab00d9bcd00d 1493 * set 0 for disabled, 1 for enabled.
JojoS 0:ab00d9bcd00d 1494 * @param enabled New interrupt enabled status
JojoS 0:ab00d9bcd00d 1495 * @see getIntFreefallEnabled()
JojoS 0:ab00d9bcd00d 1496 * @see MPU9150_RA_INT_ENABLE
JojoS 0:ab00d9bcd00d 1497 * @see MPU9150_INTERRUPT_FF_BIT
JojoS 0:ab00d9bcd00d 1498 **/
JojoS 0:ab00d9bcd00d 1499 void MPU9150::setIntEnabled(uint8_t enabled) {
JojoS 0:ab00d9bcd00d 1500 i2Cdev.writeByte(devAddr, MPU9150_RA_INT_ENABLE, enabled);
JojoS 0:ab00d9bcd00d 1501 }
JojoS 0:ab00d9bcd00d 1502 /** Get Free Fall interrupt enabled status.
JojoS 0:ab00d9bcd00d 1503 * Will be set 0 for disabled, 1 for enabled.
JojoS 0:ab00d9bcd00d 1504 * @return Current interrupt enabled status
JojoS 0:ab00d9bcd00d 1505 * @see MPU9150_RA_INT_ENABLE
JojoS 0:ab00d9bcd00d 1506 * @see MPU9150_INTERRUPT_FF_BIT
JojoS 0:ab00d9bcd00d 1507 **/
JojoS 0:ab00d9bcd00d 1508 bool MPU9150::getIntFreefallEnabled() {
JojoS 0:ab00d9bcd00d 1509 i2Cdev.readBit(devAddr, MPU9150_RA_INT_ENABLE, MPU9150_INTERRUPT_FF_BIT, buffer);
JojoS 0:ab00d9bcd00d 1510 return buffer[0];
JojoS 0:ab00d9bcd00d 1511 }
JojoS 0:ab00d9bcd00d 1512 /** Set Free Fall interrupt enabled status.
JojoS 0:ab00d9bcd00d 1513 * @param enabled New interrupt enabled status
JojoS 0:ab00d9bcd00d 1514 * @see getIntFreefallEnabled()
JojoS 0:ab00d9bcd00d 1515 * @see MPU9150_RA_INT_ENABLE
JojoS 0:ab00d9bcd00d 1516 * @see MPU9150_INTERRUPT_FF_BIT
JojoS 0:ab00d9bcd00d 1517 **/
JojoS 0:ab00d9bcd00d 1518 void MPU9150::setIntFreefallEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 1519 i2Cdev.writeBit(devAddr, MPU9150_RA_INT_ENABLE, MPU9150_INTERRUPT_FF_BIT, enabled);
JojoS 0:ab00d9bcd00d 1520 }
JojoS 0:ab00d9bcd00d 1521 /** Get Motion Detection interrupt enabled status.
JojoS 0:ab00d9bcd00d 1522 * Will be set 0 for disabled, 1 for enabled.
JojoS 0:ab00d9bcd00d 1523 * @return Current interrupt enabled status
JojoS 0:ab00d9bcd00d 1524 * @see MPU9150_RA_INT_ENABLE
JojoS 0:ab00d9bcd00d 1525 * @see MPU9150_INTERRUPT_MOT_BIT
JojoS 0:ab00d9bcd00d 1526 **/
JojoS 0:ab00d9bcd00d 1527 bool MPU9150::getIntMotionEnabled() {
JojoS 0:ab00d9bcd00d 1528 i2Cdev.readBit(devAddr, MPU9150_RA_INT_ENABLE, MPU9150_INTERRUPT_MOT_BIT, buffer);
JojoS 0:ab00d9bcd00d 1529 return buffer[0];
JojoS 0:ab00d9bcd00d 1530 }
JojoS 0:ab00d9bcd00d 1531 /** Set Motion Detection interrupt enabled status.
JojoS 0:ab00d9bcd00d 1532 * @param enabled New interrupt enabled status
JojoS 0:ab00d9bcd00d 1533 * @see getIntMotionEnabled()
JojoS 0:ab00d9bcd00d 1534 * @see MPU9150_RA_INT_ENABLE
JojoS 0:ab00d9bcd00d 1535 * @see MPU9150_INTERRUPT_MOT_BIT
JojoS 0:ab00d9bcd00d 1536 **/
JojoS 0:ab00d9bcd00d 1537 void MPU9150::setIntMotionEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 1538 i2Cdev.writeBit(devAddr, MPU9150_RA_INT_ENABLE, MPU9150_INTERRUPT_MOT_BIT, enabled);
JojoS 0:ab00d9bcd00d 1539 }
JojoS 0:ab00d9bcd00d 1540 /** Get Zero Motion Detection interrupt enabled status.
JojoS 0:ab00d9bcd00d 1541 * Will be set 0 for disabled, 1 for enabled.
JojoS 0:ab00d9bcd00d 1542 * @return Current interrupt enabled status
JojoS 0:ab00d9bcd00d 1543 * @see MPU9150_RA_INT_ENABLE
JojoS 0:ab00d9bcd00d 1544 * @see MPU9150_INTERRUPT_ZMOT_BIT
JojoS 0:ab00d9bcd00d 1545 **/
JojoS 0:ab00d9bcd00d 1546 bool MPU9150::getIntZeroMotionEnabled() {
JojoS 0:ab00d9bcd00d 1547 i2Cdev.readBit(devAddr, MPU9150_RA_INT_ENABLE, MPU9150_INTERRUPT_ZMOT_BIT, buffer);
JojoS 0:ab00d9bcd00d 1548 return buffer[0];
JojoS 0:ab00d9bcd00d 1549 }
JojoS 0:ab00d9bcd00d 1550 /** Set Zero Motion Detection interrupt enabled status.
JojoS 0:ab00d9bcd00d 1551 * @param enabled New interrupt enabled status
JojoS 0:ab00d9bcd00d 1552 * @see getIntZeroMotionEnabled()
JojoS 0:ab00d9bcd00d 1553 * @see MPU9150_RA_INT_ENABLE
JojoS 0:ab00d9bcd00d 1554 * @see MPU9150_INTERRUPT_ZMOT_BIT
JojoS 0:ab00d9bcd00d 1555 **/
JojoS 0:ab00d9bcd00d 1556 void MPU9150::setIntZeroMotionEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 1557 i2Cdev.writeBit(devAddr, MPU9150_RA_INT_ENABLE, MPU9150_INTERRUPT_ZMOT_BIT, enabled);
JojoS 0:ab00d9bcd00d 1558 }
JojoS 0:ab00d9bcd00d 1559 /** Get FIFO Buffer Overflow interrupt enabled status.
JojoS 0:ab00d9bcd00d 1560 * Will be set 0 for disabled, 1 for enabled.
JojoS 0:ab00d9bcd00d 1561 * @return Current interrupt enabled status
JojoS 0:ab00d9bcd00d 1562 * @see MPU9150_RA_INT_ENABLE
JojoS 0:ab00d9bcd00d 1563 * @see MPU9150_INTERRUPT_FIFO_OFLOW_BIT
JojoS 0:ab00d9bcd00d 1564 **/
JojoS 0:ab00d9bcd00d 1565 bool MPU9150::getIntFIFOBufferOverflowEnabled() {
JojoS 0:ab00d9bcd00d 1566 i2Cdev.readBit(devAddr, MPU9150_RA_INT_ENABLE, MPU9150_INTERRUPT_FIFO_OFLOW_BIT, buffer);
JojoS 0:ab00d9bcd00d 1567 return buffer[0];
JojoS 0:ab00d9bcd00d 1568 }
JojoS 0:ab00d9bcd00d 1569 /** Set FIFO Buffer Overflow interrupt enabled status.
JojoS 0:ab00d9bcd00d 1570 * @param enabled New interrupt enabled status
JojoS 0:ab00d9bcd00d 1571 * @see getIntFIFOBufferOverflowEnabled()
JojoS 0:ab00d9bcd00d 1572 * @see MPU9150_RA_INT_ENABLE
JojoS 0:ab00d9bcd00d 1573 * @see MPU9150_INTERRUPT_FIFO_OFLOW_BIT
JojoS 0:ab00d9bcd00d 1574 **/
JojoS 0:ab00d9bcd00d 1575 void MPU9150::setIntFIFOBufferOverflowEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 1576 i2Cdev.writeBit(devAddr, MPU9150_RA_INT_ENABLE, MPU9150_INTERRUPT_FIFO_OFLOW_BIT, enabled);
JojoS 0:ab00d9bcd00d 1577 }
JojoS 0:ab00d9bcd00d 1578 /** Get I2C Master interrupt enabled status.
JojoS 0:ab00d9bcd00d 1579 * This enables any of the I2C Master interrupt sources to generate an
JojoS 0:ab00d9bcd00d 1580 * interrupt. Will be set 0 for disabled, 1 for enabled.
JojoS 0:ab00d9bcd00d 1581 * @return Current interrupt enabled status
JojoS 0:ab00d9bcd00d 1582 * @see MPU9150_RA_INT_ENABLE
JojoS 0:ab00d9bcd00d 1583 * @see MPU9150_INTERRUPT_I2C_MST_INT_BIT
JojoS 0:ab00d9bcd00d 1584 **/
JojoS 0:ab00d9bcd00d 1585 bool MPU9150::getIntI2CMasterEnabled() {
JojoS 0:ab00d9bcd00d 1586 i2Cdev.readBit(devAddr, MPU9150_RA_INT_ENABLE, MPU9150_INTERRUPT_I2C_MST_INT_BIT, buffer);
JojoS 0:ab00d9bcd00d 1587 return buffer[0];
JojoS 0:ab00d9bcd00d 1588 }
JojoS 0:ab00d9bcd00d 1589 /** Set I2C Master interrupt enabled status.
JojoS 0:ab00d9bcd00d 1590 * @param enabled New interrupt enabled status
JojoS 0:ab00d9bcd00d 1591 * @see getIntI2CMasterEnabled()
JojoS 0:ab00d9bcd00d 1592 * @see MPU9150_RA_INT_ENABLE
JojoS 0:ab00d9bcd00d 1593 * @see MPU9150_INTERRUPT_I2C_MST_INT_BIT
JojoS 0:ab00d9bcd00d 1594 **/
JojoS 0:ab00d9bcd00d 1595 void MPU9150::setIntI2CMasterEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 1596 i2Cdev.writeBit(devAddr, MPU9150_RA_INT_ENABLE, MPU9150_INTERRUPT_I2C_MST_INT_BIT, enabled);
JojoS 0:ab00d9bcd00d 1597 }
JojoS 0:ab00d9bcd00d 1598 /** Get Data Ready interrupt enabled setting.
JojoS 0:ab00d9bcd00d 1599 * This event occurs each time a write operation to all of the sensor registers
JojoS 0:ab00d9bcd00d 1600 * has been completed. Will be set 0 for disabled, 1 for enabled.
JojoS 0:ab00d9bcd00d 1601 * @return Current interrupt enabled status
JojoS 0:ab00d9bcd00d 1602 * @see MPU9150_RA_INT_ENABLE
JojoS 0:ab00d9bcd00d 1603 * @see MPU9150_INTERRUPT_DATA_RDY_BIT
JojoS 0:ab00d9bcd00d 1604 */
JojoS 0:ab00d9bcd00d 1605 bool MPU9150::getIntDataReadyEnabled() {
JojoS 0:ab00d9bcd00d 1606 i2Cdev.readBit(devAddr, MPU9150_RA_INT_ENABLE, MPU9150_INTERRUPT_DATA_RDY_BIT, buffer);
JojoS 0:ab00d9bcd00d 1607 return buffer[0];
JojoS 0:ab00d9bcd00d 1608 }
JojoS 0:ab00d9bcd00d 1609 /** Set Data Ready interrupt enabled status.
JojoS 0:ab00d9bcd00d 1610 * @param enabled New interrupt enabled status
JojoS 0:ab00d9bcd00d 1611 * @see getIntDataReadyEnabled()
JojoS 0:ab00d9bcd00d 1612 * @see MPU9150_RA_INT_CFG
JojoS 0:ab00d9bcd00d 1613 * @see MPU9150_INTERRUPT_DATA_RDY_BIT
JojoS 0:ab00d9bcd00d 1614 */
JojoS 0:ab00d9bcd00d 1615 void MPU9150::setIntDataReadyEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 1616 i2Cdev.writeBit(devAddr, MPU9150_RA_INT_ENABLE, MPU9150_INTERRUPT_DATA_RDY_BIT, enabled);
JojoS 0:ab00d9bcd00d 1617 }
JojoS 0:ab00d9bcd00d 1618
JojoS 0:ab00d9bcd00d 1619 // INT_STATUS register
JojoS 0:ab00d9bcd00d 1620
JojoS 0:ab00d9bcd00d 1621 /** Get full set of interrupt status bits.
JojoS 0:ab00d9bcd00d 1622 * These bits clear to 0 after the register has been read. Very useful
JojoS 0:ab00d9bcd00d 1623 * for getting multiple INT statuses, since each single bit read clears
JojoS 0:ab00d9bcd00d 1624 * all of them because it has to read the whole byte.
JojoS 0:ab00d9bcd00d 1625 * @return Current interrupt status
JojoS 0:ab00d9bcd00d 1626 * @see MPU9150_RA_INT_STATUS
JojoS 0:ab00d9bcd00d 1627 */
JojoS 0:ab00d9bcd00d 1628 uint8_t MPU9150::getIntStatus() {
JojoS 0:ab00d9bcd00d 1629 i2Cdev.readByte(devAddr, MPU9150_RA_INT_STATUS, buffer);
JojoS 0:ab00d9bcd00d 1630 return buffer[0];
JojoS 0:ab00d9bcd00d 1631 }
JojoS 0:ab00d9bcd00d 1632 /** Get Free Fall interrupt status.
JojoS 0:ab00d9bcd00d 1633 * This bit automatically sets to 1 when a Free Fall interrupt has been
JojoS 0:ab00d9bcd00d 1634 * generated. The bit clears to 0 after the register has been read.
JojoS 0:ab00d9bcd00d 1635 * @return Current interrupt status
JojoS 0:ab00d9bcd00d 1636 * @see MPU9150_RA_INT_STATUS
JojoS 0:ab00d9bcd00d 1637 * @see MPU9150_INTERRUPT_FF_BIT
JojoS 0:ab00d9bcd00d 1638 */
JojoS 0:ab00d9bcd00d 1639 bool MPU9150::getIntFreefallStatus() {
JojoS 0:ab00d9bcd00d 1640 i2Cdev.readBit(devAddr, MPU9150_RA_INT_STATUS, MPU9150_INTERRUPT_FF_BIT, buffer);
JojoS 0:ab00d9bcd00d 1641 return buffer[0];
JojoS 0:ab00d9bcd00d 1642 }
JojoS 0:ab00d9bcd00d 1643 /** Get Motion Detection interrupt status.
JojoS 0:ab00d9bcd00d 1644 * This bit automatically sets to 1 when a Motion Detection interrupt has been
JojoS 0:ab00d9bcd00d 1645 * generated. The bit clears to 0 after the register has been read.
JojoS 0:ab00d9bcd00d 1646 * @return Current interrupt status
JojoS 0:ab00d9bcd00d 1647 * @see MPU9150_RA_INT_STATUS
JojoS 0:ab00d9bcd00d 1648 * @see MPU9150_INTERRUPT_MOT_BIT
JojoS 0:ab00d9bcd00d 1649 */
JojoS 0:ab00d9bcd00d 1650 bool MPU9150::getIntMotionStatus() {
JojoS 0:ab00d9bcd00d 1651 i2Cdev.readBit(devAddr, MPU9150_RA_INT_STATUS, MPU9150_INTERRUPT_MOT_BIT, buffer);
JojoS 0:ab00d9bcd00d 1652 return buffer[0];
JojoS 0:ab00d9bcd00d 1653 }
JojoS 0:ab00d9bcd00d 1654 /** Get Zero Motion Detection interrupt status.
JojoS 0:ab00d9bcd00d 1655 * This bit automatically sets to 1 when a Zero Motion Detection interrupt has
JojoS 0:ab00d9bcd00d 1656 * been generated. The bit clears to 0 after the register has been read.
JojoS 0:ab00d9bcd00d 1657 * @return Current interrupt status
JojoS 0:ab00d9bcd00d 1658 * @see MPU9150_RA_INT_STATUS
JojoS 0:ab00d9bcd00d 1659 * @see MPU9150_INTERRUPT_ZMOT_BIT
JojoS 0:ab00d9bcd00d 1660 */
JojoS 0:ab00d9bcd00d 1661 bool MPU9150::getIntZeroMotionStatus() {
JojoS 0:ab00d9bcd00d 1662 i2Cdev.readBit(devAddr, MPU9150_RA_INT_STATUS, MPU9150_INTERRUPT_ZMOT_BIT, buffer);
JojoS 0:ab00d9bcd00d 1663 return buffer[0];
JojoS 0:ab00d9bcd00d 1664 }
JojoS 0:ab00d9bcd00d 1665 /** Get FIFO Buffer Overflow interrupt status.
JojoS 0:ab00d9bcd00d 1666 * This bit automatically sets to 1 when a Free Fall interrupt has been
JojoS 0:ab00d9bcd00d 1667 * generated. The bit clears to 0 after the register has been read.
JojoS 0:ab00d9bcd00d 1668 * @return Current interrupt status
JojoS 0:ab00d9bcd00d 1669 * @see MPU9150_RA_INT_STATUS
JojoS 0:ab00d9bcd00d 1670 * @see MPU9150_INTERRUPT_FIFO_OFLOW_BIT
JojoS 0:ab00d9bcd00d 1671 */
JojoS 0:ab00d9bcd00d 1672 bool MPU9150::getIntFIFOBufferOverflowStatus() {
JojoS 0:ab00d9bcd00d 1673 i2Cdev.readBit(devAddr, MPU9150_RA_INT_STATUS, MPU9150_INTERRUPT_FIFO_OFLOW_BIT, buffer);
JojoS 0:ab00d9bcd00d 1674 return buffer[0];
JojoS 0:ab00d9bcd00d 1675 }
JojoS 0:ab00d9bcd00d 1676 /** Get I2C Master interrupt status.
JojoS 0:ab00d9bcd00d 1677 * This bit automatically sets to 1 when an I2C Master interrupt has been
JojoS 0:ab00d9bcd00d 1678 * generated. For a list of I2C Master interrupts, please refer to Register 54.
JojoS 0:ab00d9bcd00d 1679 * The bit clears to 0 after the register has been read.
JojoS 0:ab00d9bcd00d 1680 * @return Current interrupt status
JojoS 0:ab00d9bcd00d 1681 * @see MPU9150_RA_INT_STATUS
JojoS 0:ab00d9bcd00d 1682 * @see MPU9150_INTERRUPT_I2C_MST_INT_BIT
JojoS 0:ab00d9bcd00d 1683 */
JojoS 0:ab00d9bcd00d 1684 bool MPU9150::getIntI2CMasterStatus() {
JojoS 0:ab00d9bcd00d 1685 i2Cdev.readBit(devAddr, MPU9150_RA_INT_STATUS, MPU9150_INTERRUPT_I2C_MST_INT_BIT, buffer);
JojoS 0:ab00d9bcd00d 1686 return buffer[0];
JojoS 0:ab00d9bcd00d 1687 }
JojoS 0:ab00d9bcd00d 1688 /** Get Data Ready interrupt status.
JojoS 0:ab00d9bcd00d 1689 * This bit automatically sets to 1 when a Data Ready interrupt has been
JojoS 0:ab00d9bcd00d 1690 * generated. The bit clears to 0 after the register has been read.
JojoS 0:ab00d9bcd00d 1691 * @return Current interrupt status
JojoS 0:ab00d9bcd00d 1692 * @see MPU9150_RA_INT_STATUS
JojoS 0:ab00d9bcd00d 1693 * @see MPU9150_INTERRUPT_DATA_RDY_BIT
JojoS 0:ab00d9bcd00d 1694 */
JojoS 0:ab00d9bcd00d 1695 bool MPU9150::getIntDataReadyStatus() {
JojoS 0:ab00d9bcd00d 1696 i2Cdev.readBit(devAddr, MPU9150_RA_INT_STATUS, MPU9150_INTERRUPT_DATA_RDY_BIT, buffer);
JojoS 0:ab00d9bcd00d 1697 return buffer[0];
JojoS 0:ab00d9bcd00d 1698 }
JojoS 0:ab00d9bcd00d 1699
JojoS 0:ab00d9bcd00d 1700 // ACCEL_*OUT_* registers
JojoS 0:ab00d9bcd00d 1701
JojoS 0:ab00d9bcd00d 1702 /** Get raw 9-axis motion sensor readings (accel/gyro/compass).
JojoS 0:ab00d9bcd00d 1703 * FUNCTION NOT FULLY IMPLEMENTED YET.
JojoS 0:ab00d9bcd00d 1704 * @param ax 16-bit signed integer container for accelerometer X-axis value
JojoS 0:ab00d9bcd00d 1705 * @param ay 16-bit signed integer container for accelerometer Y-axis value
JojoS 0:ab00d9bcd00d 1706 * @param az 16-bit signed integer container for accelerometer Z-axis value
JojoS 0:ab00d9bcd00d 1707 * @param gx 16-bit signed integer container for gyroscope X-axis value
JojoS 0:ab00d9bcd00d 1708 * @param gy 16-bit signed integer container for gyroscope Y-axis value
JojoS 0:ab00d9bcd00d 1709 * @param gz 16-bit signed integer container for gyroscope Z-axis value
JojoS 0:ab00d9bcd00d 1710 * @param mx 16-bit signed integer container for magnetometer X-axis value
JojoS 0:ab00d9bcd00d 1711 * @param my 16-bit signed integer container for magnetometer Y-axis value
JojoS 0:ab00d9bcd00d 1712 * @param mz 16-bit signed integer container for magnetometer Z-axis value
JojoS 0:ab00d9bcd00d 1713 * @see getMotion6()
JojoS 0:ab00d9bcd00d 1714 * @see getAcceleration()
JojoS 0:ab00d9bcd00d 1715 * @see getRotation()
JojoS 0:ab00d9bcd00d 1716 * @see MPU9150_RA_ACCEL_XOUT_H
JojoS 0:ab00d9bcd00d 1717 */
JojoS 0:ab00d9bcd00d 1718 void MPU9150::getMotion9(int16_t* ax, int16_t* ay, int16_t* az, int16_t* gx, int16_t* gy, int16_t* gz, int16_t* mx, int16_t* my, int16_t* mz) {
JojoS 0:ab00d9bcd00d 1719
JojoS 0:ab00d9bcd00d 1720 //get accel and gyro
JojoS 0:ab00d9bcd00d 1721 getMotion6(ax, ay, az, gx, gy, gz);
JojoS 0:ab00d9bcd00d 1722
JojoS 0:ab00d9bcd00d 1723 //read mag
JojoS 0:ab00d9bcd00d 1724 i2Cdev.writeByte(devAddr, MPU9150_RA_INT_PIN_CFG, 0x02); //set i2c bypass enable pin to true to access magnetometer
JojoS 0:ab00d9bcd00d 1725 wait_ms(10);
JojoS 0:ab00d9bcd00d 1726 i2Cdev.writeByte(MPU9150_RA_MAG_ADDRESS, 0x0A, 0x01); //enable the magnetometer
JojoS 0:ab00d9bcd00d 1727 wait_ms(10);
JojoS 0:ab00d9bcd00d 1728 i2Cdev.readBytes(MPU9150_RA_MAG_ADDRESS, MPU9150_RA_MAG_XOUT_L, 6, buffer);
JojoS 0:ab00d9bcd00d 1729 *mx = (((int16_t)buffer[0]) << 8) | buffer[1];
JojoS 0:ab00d9bcd00d 1730 *my = (((int16_t)buffer[2]) << 8) | buffer[3];
JojoS 0:ab00d9bcd00d 1731 *mz = (((int16_t)buffer[4]) << 8) | buffer[5];
JojoS 0:ab00d9bcd00d 1732 }
JojoS 0:ab00d9bcd00d 1733 /** Get raw 6-axis motion sensor readings (accel/gyro).
JojoS 0:ab00d9bcd00d 1734 * Retrieves all currently available motion sensor values.
JojoS 0:ab00d9bcd00d 1735 * @param ax 16-bit signed integer container for accelerometer X-axis value
JojoS 0:ab00d9bcd00d 1736 * @param ay 16-bit signed integer container for accelerometer Y-axis value
JojoS 0:ab00d9bcd00d 1737 * @param az 16-bit signed integer container for accelerometer Z-axis value
JojoS 0:ab00d9bcd00d 1738 * @param gx 16-bit signed integer container for gyroscope X-axis value
JojoS 0:ab00d9bcd00d 1739 * @param gy 16-bit signed integer container for gyroscope Y-axis value
JojoS 0:ab00d9bcd00d 1740 * @param gz 16-bit signed integer container for gyroscope Z-axis value
JojoS 0:ab00d9bcd00d 1741 * @see getAcceleration()
JojoS 0:ab00d9bcd00d 1742 * @see getRotation()
JojoS 0:ab00d9bcd00d 1743 * @see MPU9150_RA_ACCEL_XOUT_H
JojoS 0:ab00d9bcd00d 1744 */
JojoS 0:ab00d9bcd00d 1745 void MPU9150::getMotion6(int16_t* ax, int16_t* ay, int16_t* az, int16_t* gx, int16_t* gy, int16_t* gz) {
JojoS 0:ab00d9bcd00d 1746 i2Cdev.readBytes(devAddr, MPU9150_RA_ACCEL_XOUT_H, 14, buffer);
JojoS 0:ab00d9bcd00d 1747 *ax = (((int16_t)buffer[0]) << 8) | buffer[1];
JojoS 0:ab00d9bcd00d 1748 *ay = (((int16_t)buffer[2]) << 8) | buffer[3];
JojoS 0:ab00d9bcd00d 1749 *az = (((int16_t)buffer[4]) << 8) | buffer[5];
JojoS 0:ab00d9bcd00d 1750 *gx = (((int16_t)buffer[8]) << 8) | buffer[9];
JojoS 0:ab00d9bcd00d 1751 *gy = (((int16_t)buffer[10]) << 8) | buffer[11];
JojoS 0:ab00d9bcd00d 1752 *gz = (((int16_t)buffer[12]) << 8) | buffer[13];
JojoS 0:ab00d9bcd00d 1753 }
JojoS 0:ab00d9bcd00d 1754 /** Get 3-axis accelerometer readings.
JojoS 0:ab00d9bcd00d 1755 * These registers store the most recent accelerometer measurements.
JojoS 0:ab00d9bcd00d 1756 * Accelerometer measurements are written to these registers at the Sample Rate
JojoS 0:ab00d9bcd00d 1757 * as defined in Register 25.
JojoS 0:ab00d9bcd00d 1758 *
JojoS 0:ab00d9bcd00d 1759 * The accelerometer measurement registers, along with the temperature
JojoS 0:ab00d9bcd00d 1760 * measurement registers, gyroscope measurement registers, and external sensor
JojoS 0:ab00d9bcd00d 1761 * data registers, are composed of two sets of registers: an internal register
JojoS 0:ab00d9bcd00d 1762 * set and a user-facing read register set.
JojoS 0:ab00d9bcd00d 1763 *
JojoS 0:ab00d9bcd00d 1764 * The data within the accelerometer sensors' internal register set is always
JojoS 0:ab00d9bcd00d 1765 * updated at the Sample Rate. Meanwhile, the user-facing read register set
JojoS 0:ab00d9bcd00d 1766 * duplicates the internal register set's data values whenever the serial
JojoS 0:ab00d9bcd00d 1767 * interface is idle. This guarantees that a burst read of sensor registers will
JojoS 0:ab00d9bcd00d 1768 * read measurements from the same sampling instant. Note that if burst reads
JojoS 0:ab00d9bcd00d 1769 * are not used, the user is responsible for ensuring a set of single byte reads
JojoS 0:ab00d9bcd00d 1770 * correspond to a single sampling instant by checking the Data Ready interrupt.
JojoS 0:ab00d9bcd00d 1771 *
JojoS 0:ab00d9bcd00d 1772 * Each 16-bit accelerometer measurement has a full scale defined in ACCEL_FS
JojoS 0:ab00d9bcd00d 1773 * (Register 28). For each full scale setting, the accelerometers' sensitivity
JojoS 0:ab00d9bcd00d 1774 * per LSB in ACCEL_xOUT is shown in the table below:
JojoS 0:ab00d9bcd00d 1775 *
JojoS 0:ab00d9bcd00d 1776 * <pre>
JojoS 0:ab00d9bcd00d 1777 * AFS_SEL | Full Scale Range | LSB Sensitivity
JojoS 0:ab00d9bcd00d 1778 * --------+------------------+----------------
JojoS 0:ab00d9bcd00d 1779 * 0 | +/- 2g | 8192 LSB/mg
JojoS 0:ab00d9bcd00d 1780 * 1 | +/- 4g | 4096 LSB/mg
JojoS 0:ab00d9bcd00d 1781 * 2 | +/- 8g | 2048 LSB/mg
JojoS 0:ab00d9bcd00d 1782 * 3 | +/- 16g | 1024 LSB/mg
JojoS 0:ab00d9bcd00d 1783 * </pre>
JojoS 0:ab00d9bcd00d 1784 *
JojoS 0:ab00d9bcd00d 1785 * @param x 16-bit signed integer container for X-axis acceleration
JojoS 0:ab00d9bcd00d 1786 * @param y 16-bit signed integer container for Y-axis acceleration
JojoS 0:ab00d9bcd00d 1787 * @param z 16-bit signed integer container for Z-axis acceleration
JojoS 0:ab00d9bcd00d 1788 * @see MPU9150_RA_GYRO_XOUT_H
JojoS 0:ab00d9bcd00d 1789 */
JojoS 0:ab00d9bcd00d 1790 void MPU9150::getAcceleration(int16_t* x, int16_t* y, int16_t* z) {
JojoS 0:ab00d9bcd00d 1791 i2Cdev.readBytes(devAddr, MPU9150_RA_ACCEL_XOUT_H, 6, buffer);
JojoS 0:ab00d9bcd00d 1792 *x = (((int16_t)buffer[0]) << 8) | buffer[1];
JojoS 0:ab00d9bcd00d 1793 *y = (((int16_t)buffer[2]) << 8) | buffer[3];
JojoS 0:ab00d9bcd00d 1794 *z = (((int16_t)buffer[4]) << 8) | buffer[5];
JojoS 0:ab00d9bcd00d 1795 }
JojoS 0:ab00d9bcd00d 1796 /** Get X-axis accelerometer reading.
JojoS 0:ab00d9bcd00d 1797 * @return X-axis acceleration measurement in 16-bit 2's complement format
JojoS 0:ab00d9bcd00d 1798 * @see getMotion6()
JojoS 0:ab00d9bcd00d 1799 * @see MPU9150_RA_ACCEL_XOUT_H
JojoS 0:ab00d9bcd00d 1800 */
JojoS 0:ab00d9bcd00d 1801 int16_t MPU9150::getAccelerationX() {
JojoS 0:ab00d9bcd00d 1802 i2Cdev.readBytes(devAddr, MPU9150_RA_ACCEL_XOUT_H, 2, buffer);
JojoS 0:ab00d9bcd00d 1803 return (((int16_t)buffer[0]) << 8) | buffer[1];
JojoS 0:ab00d9bcd00d 1804 }
JojoS 0:ab00d9bcd00d 1805 /** Get Y-axis accelerometer reading.
JojoS 0:ab00d9bcd00d 1806 * @return Y-axis acceleration measurement in 16-bit 2's complement format
JojoS 0:ab00d9bcd00d 1807 * @see getMotion6()
JojoS 0:ab00d9bcd00d 1808 * @see MPU9150_RA_ACCEL_YOUT_H
JojoS 0:ab00d9bcd00d 1809 */
JojoS 0:ab00d9bcd00d 1810 int16_t MPU9150::getAccelerationY() {
JojoS 0:ab00d9bcd00d 1811 i2Cdev.readBytes(devAddr, MPU9150_RA_ACCEL_YOUT_H, 2, buffer);
JojoS 0:ab00d9bcd00d 1812 return (((int16_t)buffer[0]) << 8) | buffer[1];
JojoS 0:ab00d9bcd00d 1813 }
JojoS 0:ab00d9bcd00d 1814 /** Get Z-axis accelerometer reading.
JojoS 0:ab00d9bcd00d 1815 * @return Z-axis acceleration measurement in 16-bit 2's complement format
JojoS 0:ab00d9bcd00d 1816 * @see getMotion6()
JojoS 0:ab00d9bcd00d 1817 * @see MPU9150_RA_ACCEL_ZOUT_H
JojoS 0:ab00d9bcd00d 1818 */
JojoS 0:ab00d9bcd00d 1819 int16_t MPU9150::getAccelerationZ() {
JojoS 0:ab00d9bcd00d 1820 i2Cdev.readBytes(devAddr, MPU9150_RA_ACCEL_ZOUT_H, 2, buffer);
JojoS 0:ab00d9bcd00d 1821 return (((int16_t)buffer[0]) << 8) | buffer[1];
JojoS 0:ab00d9bcd00d 1822 }
JojoS 0:ab00d9bcd00d 1823
JojoS 0:ab00d9bcd00d 1824 // TEMP_OUT_* registers
JojoS 0:ab00d9bcd00d 1825
JojoS 0:ab00d9bcd00d 1826 /** Get current internal temperature.
JojoS 0:ab00d9bcd00d 1827 * @return Temperature reading in 16-bit 2's complement format
JojoS 0:ab00d9bcd00d 1828 * @see MPU9150_RA_TEMP_OUT_H
JojoS 0:ab00d9bcd00d 1829 */
JojoS 0:ab00d9bcd00d 1830 int16_t MPU9150::getTemperature() {
JojoS 0:ab00d9bcd00d 1831 i2Cdev.readBytes(devAddr, MPU9150_RA_TEMP_OUT_H, 2, buffer);
JojoS 0:ab00d9bcd00d 1832 return (((int16_t)buffer[0]) << 8) | buffer[1];
JojoS 0:ab00d9bcd00d 1833 }
JojoS 0:ab00d9bcd00d 1834
JojoS 0:ab00d9bcd00d 1835 // GYRO_*OUT_* registers
JojoS 0:ab00d9bcd00d 1836
JojoS 0:ab00d9bcd00d 1837 /** Get 3-axis gyroscope readings.
JojoS 0:ab00d9bcd00d 1838 * These gyroscope measurement registers, along with the accelerometer
JojoS 0:ab00d9bcd00d 1839 * measurement registers, temperature measurement registers, and external sensor
JojoS 0:ab00d9bcd00d 1840 * data registers, are composed of two sets of registers: an internal register
JojoS 0:ab00d9bcd00d 1841 * set and a user-facing read register set.
JojoS 0:ab00d9bcd00d 1842 * The data within the gyroscope sensors' internal register set is always
JojoS 0:ab00d9bcd00d 1843 * updated at the Sample Rate. Meanwhile, the user-facing read register set
JojoS 0:ab00d9bcd00d 1844 * duplicates the internal register set's data values whenever the serial
JojoS 0:ab00d9bcd00d 1845 * interface is idle. This guarantees that a burst read of sensor registers will
JojoS 0:ab00d9bcd00d 1846 * read measurements from the same sampling instant. Note that if burst reads
JojoS 0:ab00d9bcd00d 1847 * are not used, the user is responsible for ensuring a set of single byte reads
JojoS 0:ab00d9bcd00d 1848 * correspond to a single sampling instant by checking the Data Ready interrupt.
JojoS 0:ab00d9bcd00d 1849 *
JojoS 0:ab00d9bcd00d 1850 * Each 16-bit gyroscope measurement has a full scale defined in FS_SEL
JojoS 0:ab00d9bcd00d 1851 * (Register 27). For each full scale setting, the gyroscopes' sensitivity per
JojoS 0:ab00d9bcd00d 1852 * LSB in GYRO_xOUT is shown in the table below:
JojoS 0:ab00d9bcd00d 1853 *
JojoS 0:ab00d9bcd00d 1854 * <pre>
JojoS 0:ab00d9bcd00d 1855 * FS_SEL | Full Scale Range | LSB Sensitivity
JojoS 0:ab00d9bcd00d 1856 * -------+--------------------+----------------
JojoS 0:ab00d9bcd00d 1857 * 0 | +/- 250 degrees/s | 131 LSB/deg/s
JojoS 0:ab00d9bcd00d 1858 * 1 | +/- 500 degrees/s | 65.5 LSB/deg/s
JojoS 0:ab00d9bcd00d 1859 * 2 | +/- 1000 degrees/s | 32.8 LSB/deg/s
JojoS 0:ab00d9bcd00d 1860 * 3 | +/- 2000 degrees/s | 16.4 LSB/deg/s
JojoS 0:ab00d9bcd00d 1861 * </pre>
JojoS 0:ab00d9bcd00d 1862 *
JojoS 0:ab00d9bcd00d 1863 * @param x 16-bit signed integer container for X-axis rotation
JojoS 0:ab00d9bcd00d 1864 * @param y 16-bit signed integer container for Y-axis rotation
JojoS 0:ab00d9bcd00d 1865 * @param z 16-bit signed integer container for Z-axis rotation
JojoS 0:ab00d9bcd00d 1866 * @see getMotion6()
JojoS 0:ab00d9bcd00d 1867 * @see MPU9150_RA_GYRO_XOUT_H
JojoS 0:ab00d9bcd00d 1868 */
JojoS 0:ab00d9bcd00d 1869 void MPU9150::getRotation(int16_t* x, int16_t* y, int16_t* z) {
JojoS 0:ab00d9bcd00d 1870 i2Cdev.readBytes(devAddr, MPU9150_RA_GYRO_XOUT_H, 6, buffer);
JojoS 0:ab00d9bcd00d 1871 *x = (((int16_t)buffer[0]) << 8) | buffer[1];
JojoS 0:ab00d9bcd00d 1872 *y = (((int16_t)buffer[2]) << 8) | buffer[3];
JojoS 0:ab00d9bcd00d 1873 *z = (((int16_t)buffer[4]) << 8) | buffer[5];
JojoS 0:ab00d9bcd00d 1874 }
JojoS 0:ab00d9bcd00d 1875 /** Get X-axis gyroscope reading.
JojoS 0:ab00d9bcd00d 1876 * @return X-axis rotation measurement in 16-bit 2's complement format
JojoS 0:ab00d9bcd00d 1877 * @see getMotion6()
JojoS 0:ab00d9bcd00d 1878 * @see MPU9150_RA_GYRO_XOUT_H
JojoS 0:ab00d9bcd00d 1879 */
JojoS 0:ab00d9bcd00d 1880 int16_t MPU9150::getRotationX() {
JojoS 0:ab00d9bcd00d 1881 i2Cdev.readBytes(devAddr, MPU9150_RA_GYRO_XOUT_H, 2, buffer);
JojoS 0:ab00d9bcd00d 1882 return (((int16_t)buffer[0]) << 8) | buffer[1];
JojoS 0:ab00d9bcd00d 1883 }
JojoS 0:ab00d9bcd00d 1884 /** Get Y-axis gyroscope reading.
JojoS 0:ab00d9bcd00d 1885 * @return Y-axis rotation measurement in 16-bit 2's complement format
JojoS 0:ab00d9bcd00d 1886 * @see getMotion6()
JojoS 0:ab00d9bcd00d 1887 * @see MPU9150_RA_GYRO_YOUT_H
JojoS 0:ab00d9bcd00d 1888 */
JojoS 0:ab00d9bcd00d 1889 int16_t MPU9150::getRotationY() {
JojoS 0:ab00d9bcd00d 1890 i2Cdev.readBytes(devAddr, MPU9150_RA_GYRO_YOUT_H, 2, buffer);
JojoS 0:ab00d9bcd00d 1891 return (((int16_t)buffer[0]) << 8) | buffer[1];
JojoS 0:ab00d9bcd00d 1892 }
JojoS 0:ab00d9bcd00d 1893 /** Get Z-axis gyroscope reading.
JojoS 0:ab00d9bcd00d 1894 * @return Z-axis rotation measurement in 16-bit 2's complement format
JojoS 0:ab00d9bcd00d 1895 * @see getMotion6()
JojoS 0:ab00d9bcd00d 1896 * @see MPU9150_RA_GYRO_ZOUT_H
JojoS 0:ab00d9bcd00d 1897 */
JojoS 0:ab00d9bcd00d 1898 int16_t MPU9150::getRotationZ() {
JojoS 0:ab00d9bcd00d 1899 i2Cdev.readBytes(devAddr, MPU9150_RA_GYRO_ZOUT_H, 2, buffer);
JojoS 0:ab00d9bcd00d 1900 return (((int16_t)buffer[0]) << 8) | buffer[1];
JojoS 0:ab00d9bcd00d 1901 }
JojoS 0:ab00d9bcd00d 1902
JojoS 0:ab00d9bcd00d 1903 // EXT_SENS_DATA_* registers
JojoS 0:ab00d9bcd00d 1904
JojoS 0:ab00d9bcd00d 1905 /** Read single byte from external sensor data register.
JojoS 0:ab00d9bcd00d 1906 * These registers store data read from external sensors by the Slave 0, 1, 2,
JojoS 0:ab00d9bcd00d 1907 * and 3 on the auxiliary I2C interface. Data read by Slave 4 is stored in
JojoS 0:ab00d9bcd00d 1908 * I2C_SLV4_DI (Register 53).
JojoS 0:ab00d9bcd00d 1909 *
JojoS 0:ab00d9bcd00d 1910 * External sensor data is written to these registers at the Sample Rate as
JojoS 0:ab00d9bcd00d 1911 * defined in Register 25. This access rate can be reduced by using the Slave
JojoS 0:ab00d9bcd00d 1912 * Delay Enable registers (Register 103).
JojoS 0:ab00d9bcd00d 1913 *
JojoS 0:ab00d9bcd00d 1914 * External sensor data registers, along with the gyroscope measurement
JojoS 0:ab00d9bcd00d 1915 * registers, accelerometer measurement registers, and temperature measurement
JojoS 0:ab00d9bcd00d 1916 * registers, are composed of two sets of registers: an internal register set
JojoS 0:ab00d9bcd00d 1917 * and a user-facing read register set.
JojoS 0:ab00d9bcd00d 1918 *
JojoS 0:ab00d9bcd00d 1919 * The data within the external sensors' internal register set is always updated
JojoS 0:ab00d9bcd00d 1920 * at the Sample Rate (or the reduced access rate) whenever the serial interface
JojoS 0:ab00d9bcd00d 1921 * is idle. This guarantees that a burst read of sensor registers will read
JojoS 0:ab00d9bcd00d 1922 * measurements from the same sampling instant. Note that if burst reads are not
JojoS 0:ab00d9bcd00d 1923 * used, the user is responsible for ensuring a set of single byte reads
JojoS 0:ab00d9bcd00d 1924 * correspond to a single sampling instant by checking the Data Ready interrupt.
JojoS 0:ab00d9bcd00d 1925 *
JojoS 0:ab00d9bcd00d 1926 * Data is placed in these external sensor data registers according to
JojoS 0:ab00d9bcd00d 1927 * I2C_SLV0_CTRL, I2C_SLV1_CTRL, I2C_SLV2_CTRL, and I2C_SLV3_CTRL (Registers 39,
JojoS 0:ab00d9bcd00d 1928 * 42, 45, and 48). When more than zero bytes are read (I2C_SLVx_LEN > 0) from
JojoS 0:ab00d9bcd00d 1929 * an enabled slave (I2C_SLVx_EN = 1), the slave is read at the Sample Rate (as
JojoS 0:ab00d9bcd00d 1930 * defined in Register 25) or delayed rate (if specified in Register 52 and
JojoS 0:ab00d9bcd00d 1931 * 103). During each Sample cycle, slave reads are performed in order of Slave
JojoS 0:ab00d9bcd00d 1932 * number. If all slaves are enabled with more than zero bytes to be read, the
JojoS 0:ab00d9bcd00d 1933 * order will be Slave 0, followed by Slave 1, Slave 2, and Slave 3.
JojoS 0:ab00d9bcd00d 1934 *
JojoS 0:ab00d9bcd00d 1935 * Each enabled slave will have EXT_SENS_DATA registers associated with it by
JojoS 0:ab00d9bcd00d 1936 * number of bytes read (I2C_SLVx_LEN) in order of slave number, starting from
JojoS 0:ab00d9bcd00d 1937 * EXT_SENS_DATA_00. Note that this means enabling or disabling a slave may
JojoS 0:ab00d9bcd00d 1938 * change the higher numbered slaves' associated registers. Furthermore, if
JojoS 0:ab00d9bcd00d 1939 * fewer total bytes are being read from the external sensors as a result of
JojoS 0:ab00d9bcd00d 1940 * such a change, then the data remaining in the registers which no longer have
JojoS 0:ab00d9bcd00d 1941 * an associated slave device (i.e. high numbered registers) will remain in
JojoS 0:ab00d9bcd00d 1942 * these previously allocated registers unless reset.
JojoS 0:ab00d9bcd00d 1943 *
JojoS 0:ab00d9bcd00d 1944 * If the sum of the read lengths of all SLVx transactions exceed the number of
JojoS 0:ab00d9bcd00d 1945 * available EXT_SENS_DATA registers, the excess bytes will be dropped. There
JojoS 0:ab00d9bcd00d 1946 * are 24 EXT_SENS_DATA registers and hence the total read lengths between all
JojoS 0:ab00d9bcd00d 1947 * the slaves cannot be greater than 24 or some bytes will be lost.
JojoS 0:ab00d9bcd00d 1948 *
JojoS 0:ab00d9bcd00d 1949 * Note: Slave 4's behavior is distinct from that of Slaves 0-3. For further
JojoS 0:ab00d9bcd00d 1950 * information regarding the characteristics of Slave 4, please refer to
JojoS 0:ab00d9bcd00d 1951 * Registers 49 to 53.
JojoS 0:ab00d9bcd00d 1952 *
JojoS 0:ab00d9bcd00d 1953 * EXAMPLE:
JojoS 0:ab00d9bcd00d 1954 * Suppose that Slave 0 is enabled with 4 bytes to be read (I2C_SLV0_EN = 1 and
JojoS 0:ab00d9bcd00d 1955 * I2C_SLV0_LEN = 4) while Slave 1 is enabled with 2 bytes to be read so that
JojoS 0:ab00d9bcd00d 1956 * I2C_SLV1_EN = 1 and I2C_SLV1_LEN = 2. In such a situation, EXT_SENS_DATA _00
JojoS 0:ab00d9bcd00d 1957 * through _03 will be associated with Slave 0, while EXT_SENS_DATA _04 and 05
JojoS 0:ab00d9bcd00d 1958 * will be associated with Slave 1. If Slave 2 is enabled as well, registers
JojoS 0:ab00d9bcd00d 1959 * starting from EXT_SENS_DATA_06 will be allocated to Slave 2.
JojoS 0:ab00d9bcd00d 1960 *
JojoS 0:ab00d9bcd00d 1961 * If Slave 2 is disabled while Slave 3 is enabled in this same situation, then
JojoS 0:ab00d9bcd00d 1962 * registers starting from EXT_SENS_DATA_06 will be allocated to Slave 3
JojoS 0:ab00d9bcd00d 1963 * instead.
JojoS 0:ab00d9bcd00d 1964 *
JojoS 0:ab00d9bcd00d 1965 * REGISTER ALLOCATION FOR DYNAMIC DISABLE VS. NORMAL DISABLE:
JojoS 0:ab00d9bcd00d 1966 * If a slave is disabled at any time, the space initially allocated to the
JojoS 0:ab00d9bcd00d 1967 * slave in the EXT_SENS_DATA register, will remain associated with that slave.
JojoS 0:ab00d9bcd00d 1968 * This is to avoid dynamic adjustment of the register allocation.
JojoS 0:ab00d9bcd00d 1969 *
JojoS 0:ab00d9bcd00d 1970 * The allocation of the EXT_SENS_DATA registers is recomputed only when (1) all
JojoS 0:ab00d9bcd00d 1971 * slaves are disabled, or (2) the I2C_MST_RST bit is set (Register 106).
JojoS 0:ab00d9bcd00d 1972 *
JojoS 0:ab00d9bcd00d 1973 * This above is also true if one of the slaves gets NACKed and stops
JojoS 0:ab00d9bcd00d 1974 * functioning.
JojoS 0:ab00d9bcd00d 1975 *
JojoS 0:ab00d9bcd00d 1976 * @param position Starting position (0-23)
JojoS 0:ab00d9bcd00d 1977 * @return Byte read from register
JojoS 0:ab00d9bcd00d 1978 */
JojoS 0:ab00d9bcd00d 1979 uint8_t MPU9150::getExternalSensorByte(int position) {
JojoS 0:ab00d9bcd00d 1980 i2Cdev.readByte(devAddr, MPU9150_RA_EXT_SENS_DATA_00 + position, buffer);
JojoS 0:ab00d9bcd00d 1981 return buffer[0];
JojoS 0:ab00d9bcd00d 1982 }
JojoS 0:ab00d9bcd00d 1983 /** Read word (2 bytes) from external sensor data registers.
JojoS 0:ab00d9bcd00d 1984 * @param position Starting position (0-21)
JojoS 0:ab00d9bcd00d 1985 * @return Word read from register
JojoS 0:ab00d9bcd00d 1986 * @see getExternalSensorByte()
JojoS 0:ab00d9bcd00d 1987 */
JojoS 0:ab00d9bcd00d 1988 uint16_t MPU9150::getExternalSensorWord(int position) {
JojoS 0:ab00d9bcd00d 1989 i2Cdev.readBytes(devAddr, MPU9150_RA_EXT_SENS_DATA_00 + position, 2, buffer);
JojoS 0:ab00d9bcd00d 1990 return (((uint16_t)buffer[0]) << 8) | buffer[1];
JojoS 0:ab00d9bcd00d 1991 }
JojoS 0:ab00d9bcd00d 1992 /** Read double word (4 bytes) from external sensor data registers.
JojoS 0:ab00d9bcd00d 1993 * @param position Starting position (0-20)
JojoS 0:ab00d9bcd00d 1994 * @return Double word read from registers
JojoS 0:ab00d9bcd00d 1995 * @see getExternalSensorByte()
JojoS 0:ab00d9bcd00d 1996 */
JojoS 0:ab00d9bcd00d 1997 uint32_t MPU9150::getExternalSensorDWord(int position) {
JojoS 0:ab00d9bcd00d 1998 i2Cdev.readBytes(devAddr, MPU9150_RA_EXT_SENS_DATA_00 + position, 4, buffer);
JojoS 0:ab00d9bcd00d 1999 return (((uint32_t)buffer[0]) << 24) | (((uint32_t)buffer[1]) << 16) | (((uint16_t)buffer[2]) << 8) | buffer[3];
JojoS 0:ab00d9bcd00d 2000 }
JojoS 0:ab00d9bcd00d 2001
JojoS 0:ab00d9bcd00d 2002 // MOT_DETECT_STATUS register
JojoS 0:ab00d9bcd00d 2003
JojoS 0:ab00d9bcd00d 2004 /** Get X-axis negative motion detection interrupt status.
JojoS 0:ab00d9bcd00d 2005 * @return Motion detection status
JojoS 0:ab00d9bcd00d 2006 * @see MPU9150_RA_MOT_DETECT_STATUS
JojoS 0:ab00d9bcd00d 2007 * @see MPU9150_MOTION_MOT_XNEG_BIT
JojoS 0:ab00d9bcd00d 2008 */
JojoS 0:ab00d9bcd00d 2009 bool MPU9150::getXNegMotionDetected() {
JojoS 0:ab00d9bcd00d 2010 i2Cdev.readBit(devAddr, MPU9150_RA_MOT_DETECT_STATUS, MPU9150_MOTION_MOT_XNEG_BIT, buffer);
JojoS 0:ab00d9bcd00d 2011 return buffer[0];
JojoS 0:ab00d9bcd00d 2012 }
JojoS 0:ab00d9bcd00d 2013 /** Get X-axis positive motion detection interrupt status.
JojoS 0:ab00d9bcd00d 2014 * @return Motion detection status
JojoS 0:ab00d9bcd00d 2015 * @see MPU9150_RA_MOT_DETECT_STATUS
JojoS 0:ab00d9bcd00d 2016 * @see MPU9150_MOTION_MOT_XPOS_BIT
JojoS 0:ab00d9bcd00d 2017 */
JojoS 0:ab00d9bcd00d 2018 bool MPU9150::getXPosMotionDetected() {
JojoS 0:ab00d9bcd00d 2019 i2Cdev.readBit(devAddr, MPU9150_RA_MOT_DETECT_STATUS, MPU9150_MOTION_MOT_XPOS_BIT, buffer);
JojoS 0:ab00d9bcd00d 2020 return buffer[0];
JojoS 0:ab00d9bcd00d 2021 }
JojoS 0:ab00d9bcd00d 2022 /** Get Y-axis negative motion detection interrupt status.
JojoS 0:ab00d9bcd00d 2023 * @return Motion detection status
JojoS 0:ab00d9bcd00d 2024 * @see MPU9150_RA_MOT_DETECT_STATUS
JojoS 0:ab00d9bcd00d 2025 * @see MPU9150_MOTION_MOT_YNEG_BIT
JojoS 0:ab00d9bcd00d 2026 */
JojoS 0:ab00d9bcd00d 2027 bool MPU9150::getYNegMotionDetected() {
JojoS 0:ab00d9bcd00d 2028 i2Cdev.readBit(devAddr, MPU9150_RA_MOT_DETECT_STATUS, MPU9150_MOTION_MOT_YNEG_BIT, buffer);
JojoS 0:ab00d9bcd00d 2029 return buffer[0];
JojoS 0:ab00d9bcd00d 2030 }
JojoS 0:ab00d9bcd00d 2031 /** Get Y-axis positive motion detection interrupt status.
JojoS 0:ab00d9bcd00d 2032 * @return Motion detection status
JojoS 0:ab00d9bcd00d 2033 * @see MPU9150_RA_MOT_DETECT_STATUS
JojoS 0:ab00d9bcd00d 2034 * @see MPU9150_MOTION_MOT_YPOS_BIT
JojoS 0:ab00d9bcd00d 2035 */
JojoS 0:ab00d9bcd00d 2036 bool MPU9150::getYPosMotionDetected() {
JojoS 0:ab00d9bcd00d 2037 i2Cdev.readBit(devAddr, MPU9150_RA_MOT_DETECT_STATUS, MPU9150_MOTION_MOT_YPOS_BIT, buffer);
JojoS 0:ab00d9bcd00d 2038 return buffer[0];
JojoS 0:ab00d9bcd00d 2039 }
JojoS 0:ab00d9bcd00d 2040 /** Get Z-axis negative motion detection interrupt status.
JojoS 0:ab00d9bcd00d 2041 * @return Motion detection status
JojoS 0:ab00d9bcd00d 2042 * @see MPU9150_RA_MOT_DETECT_STATUS
JojoS 0:ab00d9bcd00d 2043 * @see MPU9150_MOTION_MOT_ZNEG_BIT
JojoS 0:ab00d9bcd00d 2044 */
JojoS 0:ab00d9bcd00d 2045 bool MPU9150::getZNegMotionDetected() {
JojoS 0:ab00d9bcd00d 2046 i2Cdev.readBit(devAddr, MPU9150_RA_MOT_DETECT_STATUS, MPU9150_MOTION_MOT_ZNEG_BIT, buffer);
JojoS 0:ab00d9bcd00d 2047 return buffer[0];
JojoS 0:ab00d9bcd00d 2048 }
JojoS 0:ab00d9bcd00d 2049 /** Get Z-axis positive motion detection interrupt status.
JojoS 0:ab00d9bcd00d 2050 * @return Motion detection status
JojoS 0:ab00d9bcd00d 2051 * @see MPU9150_RA_MOT_DETECT_STATUS
JojoS 0:ab00d9bcd00d 2052 * @see MPU9150_MOTION_MOT_ZPOS_BIT
JojoS 0:ab00d9bcd00d 2053 */
JojoS 0:ab00d9bcd00d 2054 bool MPU9150::getZPosMotionDetected() {
JojoS 0:ab00d9bcd00d 2055 i2Cdev.readBit(devAddr, MPU9150_RA_MOT_DETECT_STATUS, MPU9150_MOTION_MOT_ZPOS_BIT, buffer);
JojoS 0:ab00d9bcd00d 2056 return buffer[0];
JojoS 0:ab00d9bcd00d 2057 }
JojoS 0:ab00d9bcd00d 2058 /** Get zero motion detection interrupt status.
JojoS 0:ab00d9bcd00d 2059 * @return Motion detection status
JojoS 0:ab00d9bcd00d 2060 * @see MPU9150_RA_MOT_DETECT_STATUS
JojoS 0:ab00d9bcd00d 2061 * @see MPU9150_MOTION_MOT_ZRMOT_BIT
JojoS 0:ab00d9bcd00d 2062 */
JojoS 0:ab00d9bcd00d 2063 bool MPU9150::getZeroMotionDetected() {
JojoS 0:ab00d9bcd00d 2064 i2Cdev.readBit(devAddr, MPU9150_RA_MOT_DETECT_STATUS, MPU9150_MOTION_MOT_ZRMOT_BIT, buffer);
JojoS 0:ab00d9bcd00d 2065 return buffer[0];
JojoS 0:ab00d9bcd00d 2066 }
JojoS 0:ab00d9bcd00d 2067
JojoS 0:ab00d9bcd00d 2068 // I2C_SLV*_DO register
JojoS 0:ab00d9bcd00d 2069
JojoS 0:ab00d9bcd00d 2070 /** Write byte to Data Output container for specified slave.
JojoS 0:ab00d9bcd00d 2071 * This register holds the output data written into Slave when Slave is set to
JojoS 0:ab00d9bcd00d 2072 * write mode. For further information regarding Slave control, please
JojoS 0:ab00d9bcd00d 2073 * refer to Registers 37 to 39 and immediately following.
JojoS 0:ab00d9bcd00d 2074 * @param num Slave number (0-3)
JojoS 0:ab00d9bcd00d 2075 * @param data Byte to write
JojoS 0:ab00d9bcd00d 2076 * @see MPU9150_RA_I2C_SLV0_DO
JojoS 0:ab00d9bcd00d 2077 */
JojoS 0:ab00d9bcd00d 2078 void MPU9150::setSlaveOutputByte(uint8_t num, uint8_t data) {
JojoS 0:ab00d9bcd00d 2079 if (num > 3) return;
JojoS 0:ab00d9bcd00d 2080 i2Cdev.writeByte(devAddr, MPU9150_RA_I2C_SLV0_DO + num, data);
JojoS 0:ab00d9bcd00d 2081 }
JojoS 0:ab00d9bcd00d 2082
JojoS 0:ab00d9bcd00d 2083 // I2C_MST_DELAY_CTRL register
JojoS 0:ab00d9bcd00d 2084
JojoS 0:ab00d9bcd00d 2085 /** Get external data shadow delay enabled status.
JojoS 0:ab00d9bcd00d 2086 * This register is used to specify the timing of external sensor data
JojoS 0:ab00d9bcd00d 2087 * shadowing. When DELAY_ES_SHADOW is set to 1, shadowing of external
JojoS 0:ab00d9bcd00d 2088 * sensor data is delayed until all data has been received.
JojoS 0:ab00d9bcd00d 2089 * @return Current external data shadow delay enabled status.
JojoS 0:ab00d9bcd00d 2090 * @see MPU9150_RA_I2C_MST_DELAY_CTRL
JojoS 0:ab00d9bcd00d 2091 * @see MPU9150_DELAYCTRL_DELAY_ES_SHADOW_BIT
JojoS 0:ab00d9bcd00d 2092 */
JojoS 0:ab00d9bcd00d 2093 bool MPU9150::getExternalShadowDelayEnabled() {
JojoS 0:ab00d9bcd00d 2094 i2Cdev.readBit(devAddr, MPU9150_RA_I2C_MST_DELAY_CTRL, MPU9150_DELAYCTRL_DELAY_ES_SHADOW_BIT, buffer);
JojoS 0:ab00d9bcd00d 2095 return buffer[0];
JojoS 0:ab00d9bcd00d 2096 }
JojoS 0:ab00d9bcd00d 2097 /** Set external data shadow delay enabled status.
JojoS 0:ab00d9bcd00d 2098 * @param enabled New external data shadow delay enabled status.
JojoS 0:ab00d9bcd00d 2099 * @see getExternalShadowDelayEnabled()
JojoS 0:ab00d9bcd00d 2100 * @see MPU9150_RA_I2C_MST_DELAY_CTRL
JojoS 0:ab00d9bcd00d 2101 * @see MPU9150_DELAYCTRL_DELAY_ES_SHADOW_BIT
JojoS 0:ab00d9bcd00d 2102 */
JojoS 0:ab00d9bcd00d 2103 void MPU9150::setExternalShadowDelayEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 2104 i2Cdev.writeBit(devAddr, MPU9150_RA_I2C_MST_DELAY_CTRL, MPU9150_DELAYCTRL_DELAY_ES_SHADOW_BIT, enabled);
JojoS 0:ab00d9bcd00d 2105 }
JojoS 0:ab00d9bcd00d 2106 /** Get slave delay enabled status.
JojoS 0:ab00d9bcd00d 2107 * When a particular slave delay is enabled, the rate of access for the that
JojoS 0:ab00d9bcd00d 2108 * slave device is reduced. When a slave's access rate is decreased relative to
JojoS 0:ab00d9bcd00d 2109 * the Sample Rate, the slave is accessed every:
JojoS 0:ab00d9bcd00d 2110 *
JojoS 0:ab00d9bcd00d 2111 * 1 / (1 + I2C_MST_DLY) Samples
JojoS 0:ab00d9bcd00d 2112 *
JojoS 0:ab00d9bcd00d 2113 * This base Sample Rate in turn is determined by SMPLRT_DIV (register * 25)
JojoS 0:ab00d9bcd00d 2114 * and DLPF_CFG (register 26).
JojoS 0:ab00d9bcd00d 2115 *
JojoS 0:ab00d9bcd00d 2116 * For further information regarding I2C_MST_DLY, please refer to register 52.
JojoS 0:ab00d9bcd00d 2117 * For further information regarding the Sample Rate, please refer to register 25.
JojoS 0:ab00d9bcd00d 2118 *
JojoS 0:ab00d9bcd00d 2119 * @param num Slave number (0-4)
JojoS 0:ab00d9bcd00d 2120 * @return Current slave delay enabled status.
JojoS 0:ab00d9bcd00d 2121 * @see MPU9150_RA_I2C_MST_DELAY_CTRL
JojoS 0:ab00d9bcd00d 2122 * @see MPU9150_DELAYCTRL_I2C_SLV0_DLY_EN_BIT
JojoS 0:ab00d9bcd00d 2123 */
JojoS 0:ab00d9bcd00d 2124 bool MPU9150::getSlaveDelayEnabled(uint8_t num) {
JojoS 0:ab00d9bcd00d 2125 // MPU9150_DELAYCTRL_I2C_SLV4_DLY_EN_BIT is 4, SLV3 is 3, etc.
JojoS 0:ab00d9bcd00d 2126 if (num > 4) return 0;
JojoS 0:ab00d9bcd00d 2127 i2Cdev.readBit(devAddr, MPU9150_RA_I2C_MST_DELAY_CTRL, num, buffer);
JojoS 0:ab00d9bcd00d 2128 return buffer[0];
JojoS 0:ab00d9bcd00d 2129 }
JojoS 0:ab00d9bcd00d 2130 /** Set slave delay enabled status.
JojoS 0:ab00d9bcd00d 2131 * @param num Slave number (0-4)
JojoS 0:ab00d9bcd00d 2132 * @param enabled New slave delay enabled status.
JojoS 0:ab00d9bcd00d 2133 * @see MPU9150_RA_I2C_MST_DELAY_CTRL
JojoS 0:ab00d9bcd00d 2134 * @see MPU9150_DELAYCTRL_I2C_SLV0_DLY_EN_BIT
JojoS 0:ab00d9bcd00d 2135 */
JojoS 0:ab00d9bcd00d 2136 void MPU9150::setSlaveDelayEnabled(uint8_t num, bool enabled) {
JojoS 0:ab00d9bcd00d 2137 i2Cdev.writeBit(devAddr, MPU9150_RA_I2C_MST_DELAY_CTRL, num, enabled);
JojoS 0:ab00d9bcd00d 2138 }
JojoS 0:ab00d9bcd00d 2139
JojoS 0:ab00d9bcd00d 2140 // SIGNAL_PATH_RESET register
JojoS 0:ab00d9bcd00d 2141
JojoS 0:ab00d9bcd00d 2142 /** Reset gyroscope signal path.
JojoS 0:ab00d9bcd00d 2143 * The reset will revert the signal path analog to digital converters and
JojoS 0:ab00d9bcd00d 2144 * filters to their power up configurations.
JojoS 0:ab00d9bcd00d 2145 * @see MPU9150_RA_SIGNAL_PATH_RESET
JojoS 0:ab00d9bcd00d 2146 * @see MPU9150_PATHRESET_GYRO_RESET_BIT
JojoS 0:ab00d9bcd00d 2147 */
JojoS 0:ab00d9bcd00d 2148 void MPU9150::resetGyroscopePath() {
JojoS 0:ab00d9bcd00d 2149 i2Cdev.writeBit(devAddr, MPU9150_RA_SIGNAL_PATH_RESET, MPU9150_PATHRESET_GYRO_RESET_BIT, true);
JojoS 0:ab00d9bcd00d 2150 }
JojoS 0:ab00d9bcd00d 2151 /** Reset accelerometer signal path.
JojoS 0:ab00d9bcd00d 2152 * The reset will revert the signal path analog to digital converters and
JojoS 0:ab00d9bcd00d 2153 * filters to their power up configurations.
JojoS 0:ab00d9bcd00d 2154 * @see MPU9150_RA_SIGNAL_PATH_RESET
JojoS 0:ab00d9bcd00d 2155 * @see MPU9150_PATHRESET_ACCEL_RESET_BIT
JojoS 0:ab00d9bcd00d 2156 */
JojoS 0:ab00d9bcd00d 2157 void MPU9150::resetAccelerometerPath() {
JojoS 0:ab00d9bcd00d 2158 i2Cdev.writeBit(devAddr, MPU9150_RA_SIGNAL_PATH_RESET, MPU9150_PATHRESET_ACCEL_RESET_BIT, true);
JojoS 0:ab00d9bcd00d 2159 }
JojoS 0:ab00d9bcd00d 2160 /** Reset temperature sensor signal path.
JojoS 0:ab00d9bcd00d 2161 * The reset will revert the signal path analog to digital converters and
JojoS 0:ab00d9bcd00d 2162 * filters to their power up configurations.
JojoS 0:ab00d9bcd00d 2163 * @see MPU9150_RA_SIGNAL_PATH_RESET
JojoS 0:ab00d9bcd00d 2164 * @see MPU9150_PATHRESET_TEMP_RESET_BIT
JojoS 0:ab00d9bcd00d 2165 */
JojoS 0:ab00d9bcd00d 2166 void MPU9150::resetTemperaturePath() {
JojoS 0:ab00d9bcd00d 2167 i2Cdev.writeBit(devAddr, MPU9150_RA_SIGNAL_PATH_RESET, MPU9150_PATHRESET_TEMP_RESET_BIT, true);
JojoS 0:ab00d9bcd00d 2168 }
JojoS 0:ab00d9bcd00d 2169
JojoS 0:ab00d9bcd00d 2170 // MOT_DETECT_CTRL register
JojoS 0:ab00d9bcd00d 2171
JojoS 0:ab00d9bcd00d 2172 /** Get accelerometer power-on delay.
JojoS 0:ab00d9bcd00d 2173 * The accelerometer data path provides samples to the sensor registers, Motion
JojoS 0:ab00d9bcd00d 2174 * detection, Zero Motion detection, and Free Fall detection modules. The
JojoS 0:ab00d9bcd00d 2175 * signal path contains filters which must be flushed on wake-up with new
JojoS 0:ab00d9bcd00d 2176 * samples before the detection modules begin operations. The default wake-up
JojoS 0:ab00d9bcd00d 2177 * delay, of 4ms can be lengthened by up to 3ms. This additional delay is
JojoS 0:ab00d9bcd00d 2178 * specified in ACCEL_ON_DELAY in units of 1 LSB = 1 ms. The user may select
JojoS 0:ab00d9bcd00d 2179 * any value above zero unless instructed otherwise by InvenSense. Please refer
JojoS 0:ab00d9bcd00d 2180 * to Section 8 of the MPU-6000/MPU-9150 Product Specification document for
JojoS 0:ab00d9bcd00d 2181 * further information regarding the detection modules.
JojoS 0:ab00d9bcd00d 2182 * @return Current accelerometer power-on delay
JojoS 0:ab00d9bcd00d 2183 * @see MPU9150_RA_MOT_DETECT_CTRL
JojoS 0:ab00d9bcd00d 2184 * @see MPU9150_DETECT_ACCEL_ON_DELAY_BIT
JojoS 0:ab00d9bcd00d 2185 */
JojoS 0:ab00d9bcd00d 2186 uint8_t MPU9150::getAccelerometerPowerOnDelay() {
JojoS 0:ab00d9bcd00d 2187 i2Cdev.readBits(devAddr, MPU9150_RA_MOT_DETECT_CTRL, MPU9150_DETECT_ACCEL_ON_DELAY_BIT, MPU9150_DETECT_ACCEL_ON_DELAY_LENGTH, buffer);
JojoS 0:ab00d9bcd00d 2188 return buffer[0];
JojoS 0:ab00d9bcd00d 2189 }
JojoS 0:ab00d9bcd00d 2190 /** Set accelerometer power-on delay.
JojoS 0:ab00d9bcd00d 2191 * @param delay New accelerometer power-on delay (0-3)
JojoS 0:ab00d9bcd00d 2192 * @see getAccelerometerPowerOnDelay()
JojoS 0:ab00d9bcd00d 2193 * @see MPU9150_RA_MOT_DETECT_CTRL
JojoS 0:ab00d9bcd00d 2194 * @see MPU9150_DETECT_ACCEL_ON_DELAY_BIT
JojoS 0:ab00d9bcd00d 2195 */
JojoS 0:ab00d9bcd00d 2196 void MPU9150::setAccelerometerPowerOnDelay(uint8_t delay) {
JojoS 0:ab00d9bcd00d 2197 i2Cdev.writeBits(devAddr, MPU9150_RA_MOT_DETECT_CTRL, MPU9150_DETECT_ACCEL_ON_DELAY_BIT, MPU9150_DETECT_ACCEL_ON_DELAY_LENGTH, delay);
JojoS 0:ab00d9bcd00d 2198 }
JojoS 0:ab00d9bcd00d 2199 /** Get Free Fall detection counter decrement configuration.
JojoS 0:ab00d9bcd00d 2200 * Detection is registered by the Free Fall detection module after accelerometer
JojoS 0:ab00d9bcd00d 2201 * measurements meet their respective threshold conditions over a specified
JojoS 0:ab00d9bcd00d 2202 * number of samples. When the threshold conditions are met, the corresponding
JojoS 0:ab00d9bcd00d 2203 * detection counter increments by 1. The user may control the rate at which the
JojoS 0:ab00d9bcd00d 2204 * detection counter decrements when the threshold condition is not met by
JojoS 0:ab00d9bcd00d 2205 * configuring FF_COUNT. The decrement rate can be set according to the
JojoS 0:ab00d9bcd00d 2206 * following table:
JojoS 0:ab00d9bcd00d 2207 *
JojoS 0:ab00d9bcd00d 2208 * <pre>
JojoS 0:ab00d9bcd00d 2209 * FF_COUNT | Counter Decrement
JojoS 0:ab00d9bcd00d 2210 * ---------+------------------
JojoS 0:ab00d9bcd00d 2211 * 0 | Reset
JojoS 0:ab00d9bcd00d 2212 * 1 | 1
JojoS 0:ab00d9bcd00d 2213 * 2 | 2
JojoS 0:ab00d9bcd00d 2214 * 3 | 4
JojoS 0:ab00d9bcd00d 2215 * </pre>
JojoS 0:ab00d9bcd00d 2216 *
JojoS 0:ab00d9bcd00d 2217 * When FF_COUNT is configured to 0 (reset), any non-qualifying sample will
JojoS 0:ab00d9bcd00d 2218 * reset the counter to 0. For further information on Free Fall detection,
JojoS 0:ab00d9bcd00d 2219 * please refer to Registers 29 to 32.
JojoS 0:ab00d9bcd00d 2220 *
JojoS 0:ab00d9bcd00d 2221 * @return Current decrement configuration
JojoS 0:ab00d9bcd00d 2222 * @see MPU9150_RA_MOT_DETECT_CTRL
JojoS 0:ab00d9bcd00d 2223 * @see MPU9150_DETECT_FF_COUNT_BIT
JojoS 0:ab00d9bcd00d 2224 */
JojoS 0:ab00d9bcd00d 2225 uint8_t MPU9150::getFreefallDetectionCounterDecrement() {
JojoS 0:ab00d9bcd00d 2226 i2Cdev.readBits(devAddr, MPU9150_RA_MOT_DETECT_CTRL, MPU9150_DETECT_FF_COUNT_BIT, MPU9150_DETECT_FF_COUNT_LENGTH, buffer);
JojoS 0:ab00d9bcd00d 2227 return buffer[0];
JojoS 0:ab00d9bcd00d 2228 }
JojoS 0:ab00d9bcd00d 2229 /** Set Free Fall detection counter decrement configuration.
JojoS 0:ab00d9bcd00d 2230 * @param decrement New decrement configuration value
JojoS 0:ab00d9bcd00d 2231 * @see getFreefallDetectionCounterDecrement()
JojoS 0:ab00d9bcd00d 2232 * @see MPU9150_RA_MOT_DETECT_CTRL
JojoS 0:ab00d9bcd00d 2233 * @see MPU9150_DETECT_FF_COUNT_BIT
JojoS 0:ab00d9bcd00d 2234 */
JojoS 0:ab00d9bcd00d 2235 void MPU9150::setFreefallDetectionCounterDecrement(uint8_t decrement) {
JojoS 0:ab00d9bcd00d 2236 i2Cdev.writeBits(devAddr, MPU9150_RA_MOT_DETECT_CTRL, MPU9150_DETECT_FF_COUNT_BIT, MPU9150_DETECT_FF_COUNT_LENGTH, decrement);
JojoS 0:ab00d9bcd00d 2237 }
JojoS 0:ab00d9bcd00d 2238 /** Get Motion detection counter decrement configuration.
JojoS 0:ab00d9bcd00d 2239 * Detection is registered by the Motion detection module after accelerometer
JojoS 0:ab00d9bcd00d 2240 * measurements meet their respective threshold conditions over a specified
JojoS 0:ab00d9bcd00d 2241 * number of samples. When the threshold conditions are met, the corresponding
JojoS 0:ab00d9bcd00d 2242 * detection counter increments by 1. The user may control the rate at which the
JojoS 0:ab00d9bcd00d 2243 * detection counter decrements when the threshold condition is not met by
JojoS 0:ab00d9bcd00d 2244 * configuring MOT_COUNT. The decrement rate can be set according to the
JojoS 0:ab00d9bcd00d 2245 * following table:
JojoS 0:ab00d9bcd00d 2246 *
JojoS 0:ab00d9bcd00d 2247 * <pre>
JojoS 0:ab00d9bcd00d 2248 * MOT_COUNT | Counter Decrement
JojoS 0:ab00d9bcd00d 2249 * ----------+------------------
JojoS 0:ab00d9bcd00d 2250 * 0 | Reset
JojoS 0:ab00d9bcd00d 2251 * 1 | 1
JojoS 0:ab00d9bcd00d 2252 * 2 | 2
JojoS 0:ab00d9bcd00d 2253 * 3 | 4
JojoS 0:ab00d9bcd00d 2254 * </pre>
JojoS 0:ab00d9bcd00d 2255 *
JojoS 0:ab00d9bcd00d 2256 * When MOT_COUNT is configured to 0 (reset), any non-qualifying sample will
JojoS 0:ab00d9bcd00d 2257 * reset the counter to 0. For further information on Motion detection,
JojoS 0:ab00d9bcd00d 2258 * please refer to Registers 29 to 32.
JojoS 0:ab00d9bcd00d 2259 *
JojoS 0:ab00d9bcd00d 2260 */
JojoS 0:ab00d9bcd00d 2261 uint8_t MPU9150::getMotionDetectionCounterDecrement() {
JojoS 0:ab00d9bcd00d 2262 i2Cdev.readBits(devAddr, MPU9150_RA_MOT_DETECT_CTRL, MPU9150_DETECT_MOT_COUNT_BIT, MPU9150_DETECT_MOT_COUNT_LENGTH, buffer);
JojoS 0:ab00d9bcd00d 2263 return buffer[0];
JojoS 0:ab00d9bcd00d 2264 }
JojoS 0:ab00d9bcd00d 2265 /** Set Motion detection counter decrement configuration.
JojoS 0:ab00d9bcd00d 2266 * @param decrement New decrement configuration value
JojoS 0:ab00d9bcd00d 2267 * @see getMotionDetectionCounterDecrement()
JojoS 0:ab00d9bcd00d 2268 * @see MPU9150_RA_MOT_DETECT_CTRL
JojoS 0:ab00d9bcd00d 2269 * @see MPU9150_DETECT_MOT_COUNT_BIT
JojoS 0:ab00d9bcd00d 2270 */
JojoS 0:ab00d9bcd00d 2271 void MPU9150::setMotionDetectionCounterDecrement(uint8_t decrement) {
JojoS 0:ab00d9bcd00d 2272 i2Cdev.writeBits(devAddr, MPU9150_RA_MOT_DETECT_CTRL, MPU9150_DETECT_MOT_COUNT_BIT, MPU9150_DETECT_MOT_COUNT_LENGTH, decrement);
JojoS 0:ab00d9bcd00d 2273 }
JojoS 0:ab00d9bcd00d 2274
JojoS 0:ab00d9bcd00d 2275 // USER_CTRL register
JojoS 0:ab00d9bcd00d 2276
JojoS 0:ab00d9bcd00d 2277 /** Get FIFO enabled status.
JojoS 0:ab00d9bcd00d 2278 * When this bit is set to 0, the FIFO buffer is disabled. The FIFO buffer
JojoS 0:ab00d9bcd00d 2279 * cannot be written to or read from while disabled. The FIFO buffer's state
JojoS 0:ab00d9bcd00d 2280 * does not change unless the MPU-60X0 is power cycled.
JojoS 0:ab00d9bcd00d 2281 * @return Current FIFO enabled status
JojoS 0:ab00d9bcd00d 2282 * @see MPU9150_RA_USER_CTRL
JojoS 0:ab00d9bcd00d 2283 * @see MPU9150_USERCTRL_FIFO_EN_BIT
JojoS 0:ab00d9bcd00d 2284 */
JojoS 0:ab00d9bcd00d 2285 bool MPU9150::getFIFOEnabled() {
JojoS 0:ab00d9bcd00d 2286 i2Cdev.readBit(devAddr, MPU9150_RA_USER_CTRL, MPU9150_USERCTRL_FIFO_EN_BIT, buffer);
JojoS 0:ab00d9bcd00d 2287 return buffer[0];
JojoS 0:ab00d9bcd00d 2288 }
JojoS 0:ab00d9bcd00d 2289 /** Set FIFO enabled status.
JojoS 0:ab00d9bcd00d 2290 * @param enabled New FIFO enabled status
JojoS 0:ab00d9bcd00d 2291 * @see getFIFOEnabled()
JojoS 0:ab00d9bcd00d 2292 * @see MPU9150_RA_USER_CTRL
JojoS 0:ab00d9bcd00d 2293 * @see MPU9150_USERCTRL_FIFO_EN_BIT
JojoS 0:ab00d9bcd00d 2294 */
JojoS 0:ab00d9bcd00d 2295 void MPU9150::setFIFOEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 2296 i2Cdev.writeBit(devAddr, MPU9150_RA_USER_CTRL, MPU9150_USERCTRL_FIFO_EN_BIT, enabled);
JojoS 0:ab00d9bcd00d 2297 }
JojoS 0:ab00d9bcd00d 2298 /** Get I2C Master Mode enabled status.
JojoS 0:ab00d9bcd00d 2299 * When this mode is enabled, the MPU-60X0 acts as the I2C Master to the
JojoS 0:ab00d9bcd00d 2300 * external sensor slave devices on the auxiliary I2C bus. When this bit is
JojoS 0:ab00d9bcd00d 2301 * cleared to 0, the auxiliary I2C bus lines (AUX_DA and AUX_CL) are logically
JojoS 0:ab00d9bcd00d 2302 * driven by the primary I2C bus (SDA and SCL). This is a precondition to
JojoS 0:ab00d9bcd00d 2303 * enabling Bypass Mode. For further information regarding Bypass Mode, please
JojoS 0:ab00d9bcd00d 2304 * refer to Register 55.
JojoS 0:ab00d9bcd00d 2305 * @return Current I2C Master Mode enabled status
JojoS 0:ab00d9bcd00d 2306 * @see MPU9150_RA_USER_CTRL
JojoS 0:ab00d9bcd00d 2307 * @see MPU9150_USERCTRL_I2C_MST_EN_BIT
JojoS 0:ab00d9bcd00d 2308 */
JojoS 0:ab00d9bcd00d 2309 bool MPU9150::getI2CMasterModeEnabled() {
JojoS 0:ab00d9bcd00d 2310 i2Cdev.readBit(devAddr, MPU9150_RA_USER_CTRL, MPU9150_USERCTRL_I2C_MST_EN_BIT, buffer);
JojoS 0:ab00d9bcd00d 2311 return buffer[0];
JojoS 0:ab00d9bcd00d 2312 }
JojoS 0:ab00d9bcd00d 2313 /** Set I2C Master Mode enabled status.
JojoS 0:ab00d9bcd00d 2314 * @param enabled New I2C Master Mode enabled status
JojoS 0:ab00d9bcd00d 2315 * @see getI2CMasterModeEnabled()
JojoS 0:ab00d9bcd00d 2316 * @see MPU9150_RA_USER_CTRL
JojoS 0:ab00d9bcd00d 2317 * @see MPU9150_USERCTRL_I2C_MST_EN_BIT
JojoS 0:ab00d9bcd00d 2318 */
JojoS 0:ab00d9bcd00d 2319 void MPU9150::setI2CMasterModeEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 2320 i2Cdev.writeBit(devAddr, MPU9150_RA_USER_CTRL, MPU9150_USERCTRL_I2C_MST_EN_BIT, enabled);
JojoS 0:ab00d9bcd00d 2321 }
JojoS 0:ab00d9bcd00d 2322 /** Switch from I2C to SPI mode (MPU-6000 only)
JojoS 0:ab00d9bcd00d 2323 * If this is set, the primary SPI interface will be enabled in place of the
JojoS 0:ab00d9bcd00d 2324 * disabled primary I2C interface.
JojoS 0:ab00d9bcd00d 2325 */
JojoS 0:ab00d9bcd00d 2326 void MPU9150::switchSPIEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 2327 i2Cdev.writeBit(devAddr, MPU9150_RA_USER_CTRL, MPU9150_USERCTRL_I2C_IF_DIS_BIT, enabled);
JojoS 0:ab00d9bcd00d 2328 }
JojoS 0:ab00d9bcd00d 2329 /** Reset the FIFO.
JojoS 0:ab00d9bcd00d 2330 * This bit resets the FIFO buffer when set to 1 while FIFO_EN equals 0. This
JojoS 0:ab00d9bcd00d 2331 * bit automatically clears to 0 after the reset has been triggered.
JojoS 0:ab00d9bcd00d 2332 * @see MPU9150_RA_USER_CTRL
JojoS 0:ab00d9bcd00d 2333 * @see MPU9150_USERCTRL_FIFO_RESET_BIT
JojoS 0:ab00d9bcd00d 2334 */
JojoS 0:ab00d9bcd00d 2335 void MPU9150::resetFIFO() {
JojoS 0:ab00d9bcd00d 2336 i2Cdev.writeBit(devAddr, MPU9150_RA_USER_CTRL, MPU9150_USERCTRL_FIFO_RESET_BIT, true);
JojoS 0:ab00d9bcd00d 2337 }
JojoS 0:ab00d9bcd00d 2338 /** Reset the I2C Master.
JojoS 0:ab00d9bcd00d 2339 * This bit resets the I2C Master when set to 1 while I2C_MST_EN equals 0.
JojoS 0:ab00d9bcd00d 2340 * This bit automatically clears to 0 after the reset has been triggered.
JojoS 0:ab00d9bcd00d 2341 * @see MPU9150_RA_USER_CTRL
JojoS 0:ab00d9bcd00d 2342 * @see MPU9150_USERCTRL_I2C_MST_RESET_BIT
JojoS 0:ab00d9bcd00d 2343 */
JojoS 0:ab00d9bcd00d 2344 void MPU9150::resetI2CMaster() {
JojoS 0:ab00d9bcd00d 2345 i2Cdev.writeBit(devAddr, MPU9150_RA_USER_CTRL, MPU9150_USERCTRL_I2C_MST_RESET_BIT, true);
JojoS 0:ab00d9bcd00d 2346 }
JojoS 0:ab00d9bcd00d 2347 /** Reset all sensor registers and signal paths.
JojoS 0:ab00d9bcd00d 2348 * When set to 1, this bit resets the signal paths for all sensors (gyroscopes,
JojoS 0:ab00d9bcd00d 2349 * accelerometers, and temperature sensor). This operation will also clear the
JojoS 0:ab00d9bcd00d 2350 * sensor registers. This bit automatically clears to 0 after the reset has been
JojoS 0:ab00d9bcd00d 2351 * triggered.
JojoS 0:ab00d9bcd00d 2352 *
JojoS 0:ab00d9bcd00d 2353 * When resetting only the signal path (and not the sensor registers), please
JojoS 0:ab00d9bcd00d 2354 * use Register 104, SIGNAL_PATH_RESET.
JojoS 0:ab00d9bcd00d 2355 *
JojoS 0:ab00d9bcd00d 2356 * @see MPU9150_RA_USER_CTRL
JojoS 0:ab00d9bcd00d 2357 * @see MPU9150_USERCTRL_SIG_COND_RESET_BIT
JojoS 0:ab00d9bcd00d 2358 */
JojoS 0:ab00d9bcd00d 2359 void MPU9150::resetSensors() {
JojoS 0:ab00d9bcd00d 2360 i2Cdev.writeBit(devAddr, MPU9150_RA_USER_CTRL, MPU9150_USERCTRL_SIG_COND_RESET_BIT, true);
JojoS 0:ab00d9bcd00d 2361 }
JojoS 0:ab00d9bcd00d 2362
JojoS 0:ab00d9bcd00d 2363 // PWR_MGMT_1 register
JojoS 0:ab00d9bcd00d 2364
JojoS 0:ab00d9bcd00d 2365 /** Trigger a full device reset.
JojoS 0:ab00d9bcd00d 2366 * A small delay of ~50ms may be desirable after triggering a reset.
JojoS 0:ab00d9bcd00d 2367 * @see MPU9150_RA_PWR_MGMT_1
JojoS 0:ab00d9bcd00d 2368 * @see MPU9150_PWR1_DEVICE_RESET_BIT
JojoS 0:ab00d9bcd00d 2369 */
JojoS 0:ab00d9bcd00d 2370 void MPU9150::reset() {
JojoS 0:ab00d9bcd00d 2371 i2Cdev.writeBit(devAddr, MPU9150_RA_PWR_MGMT_1, MPU9150_PWR1_DEVICE_RESET_BIT, true);
JojoS 0:ab00d9bcd00d 2372 }
JojoS 0:ab00d9bcd00d 2373 /** Get sleep mode status.
JojoS 0:ab00d9bcd00d 2374 * Setting the SLEEP bit in the register puts the device into very low power
JojoS 0:ab00d9bcd00d 2375 * sleep mode. In this mode, only the serial interface and internal registers
JojoS 0:ab00d9bcd00d 2376 * remain active, allowing for a very low standby current. Clearing this bit
JojoS 0:ab00d9bcd00d 2377 * puts the device back into normal mode. To save power, the individual standby
JojoS 0:ab00d9bcd00d 2378 * selections for each of the gyros should be used if any gyro axis is not used
JojoS 0:ab00d9bcd00d 2379 * by the application.
JojoS 0:ab00d9bcd00d 2380 * @return Current sleep mode enabled status
JojoS 0:ab00d9bcd00d 2381 * @see MPU9150_RA_PWR_MGMT_1
JojoS 0:ab00d9bcd00d 2382 * @see MPU9150_PWR1_SLEEP_BIT
JojoS 0:ab00d9bcd00d 2383 */
JojoS 0:ab00d9bcd00d 2384 bool MPU9150::getSleepEnabled() {
JojoS 0:ab00d9bcd00d 2385 i2Cdev.readBit(devAddr, MPU9150_RA_PWR_MGMT_1, MPU9150_PWR1_SLEEP_BIT, buffer);
JojoS 0:ab00d9bcd00d 2386 return buffer[0];
JojoS 0:ab00d9bcd00d 2387 }
JojoS 0:ab00d9bcd00d 2388 /** Set sleep mode status.
JojoS 0:ab00d9bcd00d 2389 * @param enabled New sleep mode enabled status
JojoS 0:ab00d9bcd00d 2390 * @see getSleepEnabled()
JojoS 0:ab00d9bcd00d 2391 * @see MPU9150_RA_PWR_MGMT_1
JojoS 0:ab00d9bcd00d 2392 * @see MPU9150_PWR1_SLEEP_BIT
JojoS 0:ab00d9bcd00d 2393 */
JojoS 0:ab00d9bcd00d 2394 void MPU9150::setSleepEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 2395 i2Cdev.writeBit(devAddr, MPU9150_RA_PWR_MGMT_1, MPU9150_PWR1_SLEEP_BIT, enabled);
JojoS 0:ab00d9bcd00d 2396 }
JojoS 0:ab00d9bcd00d 2397 /** Get wake cycle enabled status.
JojoS 0:ab00d9bcd00d 2398 * When this bit is set to 1 and SLEEP is disabled, the MPU-60X0 will cycle
JojoS 0:ab00d9bcd00d 2399 * between sleep mode and waking up to take a single sample of data from active
JojoS 0:ab00d9bcd00d 2400 * sensors at a rate determined by LP_WAKE_CTRL (register 108).
JojoS 0:ab00d9bcd00d 2401 * @return Current sleep mode enabled status
JojoS 0:ab00d9bcd00d 2402 * @see MPU9150_RA_PWR_MGMT_1
JojoS 0:ab00d9bcd00d 2403 * @see MPU9150_PWR1_CYCLE_BIT
JojoS 0:ab00d9bcd00d 2404 */
JojoS 0:ab00d9bcd00d 2405 bool MPU9150::getWakeCycleEnabled() {
JojoS 0:ab00d9bcd00d 2406 i2Cdev.readBit(devAddr, MPU9150_RA_PWR_MGMT_1, MPU9150_PWR1_CYCLE_BIT, buffer);
JojoS 0:ab00d9bcd00d 2407 return buffer[0];
JojoS 0:ab00d9bcd00d 2408 }
JojoS 0:ab00d9bcd00d 2409 /** Set wake cycle enabled status.
JojoS 0:ab00d9bcd00d 2410 * @param enabled New sleep mode enabled status
JojoS 0:ab00d9bcd00d 2411 * @see getWakeCycleEnabled()
JojoS 0:ab00d9bcd00d 2412 * @see MPU9150_RA_PWR_MGMT_1
JojoS 0:ab00d9bcd00d 2413 * @see MPU9150_PWR1_CYCLE_BIT
JojoS 0:ab00d9bcd00d 2414 */
JojoS 0:ab00d9bcd00d 2415 void MPU9150::setWakeCycleEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 2416 i2Cdev.writeBit(devAddr, MPU9150_RA_PWR_MGMT_1, MPU9150_PWR1_CYCLE_BIT, enabled);
JojoS 0:ab00d9bcd00d 2417 }
JojoS 0:ab00d9bcd00d 2418 /** Get temperature sensor enabled status.
JojoS 0:ab00d9bcd00d 2419 * Control the usage of the internal temperature sensor.
JojoS 0:ab00d9bcd00d 2420 *
JojoS 0:ab00d9bcd00d 2421 * Note: this register stores the *disabled* value, but for consistency with the
JojoS 0:ab00d9bcd00d 2422 * rest of the code, the function is named and used with standard true/false
JojoS 0:ab00d9bcd00d 2423 * values to indicate whether the sensor is enabled or disabled, respectively.
JojoS 0:ab00d9bcd00d 2424 *
JojoS 0:ab00d9bcd00d 2425 * @return Current temperature sensor enabled status
JojoS 0:ab00d9bcd00d 2426 * @see MPU9150_RA_PWR_MGMT_1
JojoS 0:ab00d9bcd00d 2427 * @see MPU9150_PWR1_TEMP_DIS_BIT
JojoS 0:ab00d9bcd00d 2428 */
JojoS 0:ab00d9bcd00d 2429 bool MPU9150::getTempSensorEnabled() {
JojoS 0:ab00d9bcd00d 2430 i2Cdev.readBit(devAddr, MPU9150_RA_PWR_MGMT_1, MPU9150_PWR1_TEMP_DIS_BIT, buffer);
JojoS 0:ab00d9bcd00d 2431 return buffer[0] == 0; // 1 is actually disabled here
JojoS 0:ab00d9bcd00d 2432 }
JojoS 0:ab00d9bcd00d 2433 /** Set temperature sensor enabled status.
JojoS 0:ab00d9bcd00d 2434 * Note: this register stores the *disabled* value, but for consistency with the
JojoS 0:ab00d9bcd00d 2435 * rest of the code, the function is named and used with standard true/false
JojoS 0:ab00d9bcd00d 2436 * values to indicate whether the sensor is enabled or disabled, respectively.
JojoS 0:ab00d9bcd00d 2437 *
JojoS 0:ab00d9bcd00d 2438 * @param enabled New temperature sensor enabled status
JojoS 0:ab00d9bcd00d 2439 * @see getTempSensorEnabled()
JojoS 0:ab00d9bcd00d 2440 * @see MPU9150_RA_PWR_MGMT_1
JojoS 0:ab00d9bcd00d 2441 * @see MPU9150_PWR1_TEMP_DIS_BIT
JojoS 0:ab00d9bcd00d 2442 */
JojoS 0:ab00d9bcd00d 2443 void MPU9150::setTempSensorEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 2444 // 1 is actually disabled here
JojoS 0:ab00d9bcd00d 2445 i2Cdev.writeBit(devAddr, MPU9150_RA_PWR_MGMT_1, MPU9150_PWR1_TEMP_DIS_BIT, !enabled);
JojoS 0:ab00d9bcd00d 2446 }
JojoS 0:ab00d9bcd00d 2447 /** Get clock source setting.
JojoS 0:ab00d9bcd00d 2448 * @return Current clock source setting
JojoS 0:ab00d9bcd00d 2449 * @see MPU9150_RA_PWR_MGMT_1
JojoS 0:ab00d9bcd00d 2450 * @see MPU9150_PWR1_CLKSEL_BIT
JojoS 0:ab00d9bcd00d 2451 * @see MPU9150_PWR1_CLKSEL_LENGTH
JojoS 0:ab00d9bcd00d 2452 */
JojoS 0:ab00d9bcd00d 2453 uint8_t MPU9150::getClockSource() {
JojoS 0:ab00d9bcd00d 2454 i2Cdev.readBits(devAddr, MPU9150_RA_PWR_MGMT_1, MPU9150_PWR1_CLKSEL_BIT, MPU9150_PWR1_CLKSEL_LENGTH, buffer);
JojoS 0:ab00d9bcd00d 2455 return buffer[0];
JojoS 0:ab00d9bcd00d 2456 }
JojoS 0:ab00d9bcd00d 2457 /** Set clock source setting.
JojoS 0:ab00d9bcd00d 2458 * An internal 8MHz oscillator, gyroscope based clock, or external sources can
JojoS 0:ab00d9bcd00d 2459 * be selected as the MPU-60X0 clock source. When the internal 8 MHz oscillator
JojoS 0:ab00d9bcd00d 2460 * or an external source is chosen as the clock source, the MPU-60X0 can operate
JojoS 0:ab00d9bcd00d 2461 * in low power modes with the gyroscopes disabled.
JojoS 0:ab00d9bcd00d 2462 *
JojoS 0:ab00d9bcd00d 2463 * Upon power up, the MPU-60X0 clock source defaults to the internal oscillator.
JojoS 0:ab00d9bcd00d 2464 * However, it is highly recommended that the device be configured to use one of
JojoS 0:ab00d9bcd00d 2465 * the gyroscopes (or an external clock source) as the clock reference for
JojoS 0:ab00d9bcd00d 2466 * improved stability. The clock source can be selected according to the following table:
JojoS 0:ab00d9bcd00d 2467 *
JojoS 0:ab00d9bcd00d 2468 * <pre>
JojoS 0:ab00d9bcd00d 2469 * CLK_SEL | Clock Source
JojoS 0:ab00d9bcd00d 2470 * --------+--------------------------------------
JojoS 0:ab00d9bcd00d 2471 * 0 | Internal oscillator
JojoS 0:ab00d9bcd00d 2472 * 1 | PLL with X Gyro reference
JojoS 0:ab00d9bcd00d 2473 * 2 | PLL with Y Gyro reference
JojoS 0:ab00d9bcd00d 2474 * 3 | PLL with Z Gyro reference
JojoS 0:ab00d9bcd00d 2475 * 4 | PLL with external 32.768kHz reference
JojoS 0:ab00d9bcd00d 2476 * 5 | PLL with external 19.2MHz reference
JojoS 0:ab00d9bcd00d 2477 * 6 | Reserved
JojoS 0:ab00d9bcd00d 2478 * 7 | Stops the clock and keeps the timing generator in reset
JojoS 0:ab00d9bcd00d 2479 * </pre>
JojoS 0:ab00d9bcd00d 2480 *
JojoS 0:ab00d9bcd00d 2481 * @param source New clock source setting
JojoS 0:ab00d9bcd00d 2482 * @see getClockSource()
JojoS 0:ab00d9bcd00d 2483 * @see MPU9150_RA_PWR_MGMT_1
JojoS 0:ab00d9bcd00d 2484 * @see MPU9150_PWR1_CLKSEL_BIT
JojoS 0:ab00d9bcd00d 2485 * @see MPU9150_PWR1_CLKSEL_LENGTH
JojoS 0:ab00d9bcd00d 2486 */
JojoS 0:ab00d9bcd00d 2487 void MPU9150::setClockSource(uint8_t source) {
JojoS 0:ab00d9bcd00d 2488 i2Cdev.writeBits(devAddr, MPU9150_RA_PWR_MGMT_1, MPU9150_PWR1_CLKSEL_BIT, MPU9150_PWR1_CLKSEL_LENGTH, source);
JojoS 0:ab00d9bcd00d 2489 }
JojoS 0:ab00d9bcd00d 2490
JojoS 0:ab00d9bcd00d 2491 // PWR_MGMT_2 register
JojoS 0:ab00d9bcd00d 2492
JojoS 0:ab00d9bcd00d 2493 /** Get wake frequency in Accel-Only Low Power Mode.
JojoS 0:ab00d9bcd00d 2494 * The MPU-60X0 can be put into Accerlerometer Only Low Power Mode by setting
JojoS 0:ab00d9bcd00d 2495 * PWRSEL to 1 in the Power Management 1 register (Register 107). In this mode,
JojoS 0:ab00d9bcd00d 2496 * the device will power off all devices except for the primary I2C interface,
JojoS 0:ab00d9bcd00d 2497 * waking only the accelerometer at fixed intervals to take a single
JojoS 0:ab00d9bcd00d 2498 * measurement. The frequency of wake-ups can be configured with LP_WAKE_CTRL
JojoS 0:ab00d9bcd00d 2499 * as shown below:
JojoS 0:ab00d9bcd00d 2500 *
JojoS 0:ab00d9bcd00d 2501 * <pre>
JojoS 0:ab00d9bcd00d 2502 * LP_WAKE_CTRL | Wake-up Frequency
JojoS 0:ab00d9bcd00d 2503 * -------------+------------------
JojoS 0:ab00d9bcd00d 2504 * 0 | 1.25 Hz
JojoS 0:ab00d9bcd00d 2505 * 1 | 2.5 Hz
JojoS 0:ab00d9bcd00d 2506 * 2 | 5 Hz
JojoS 0:ab00d9bcd00d 2507 * 3 | 10 Hz
JojoS 0:ab00d9bcd00d 2508 * <pre>
JojoS 0:ab00d9bcd00d 2509 *
JojoS 0:ab00d9bcd00d 2510 * For further information regarding the MPU-60X0's power modes, please refer to
JojoS 0:ab00d9bcd00d 2511 * Register 107.
JojoS 0:ab00d9bcd00d 2512 *
JojoS 0:ab00d9bcd00d 2513 * @return Current wake frequency
JojoS 0:ab00d9bcd00d 2514 * @see MPU9150_RA_PWR_MGMT_2
JojoS 0:ab00d9bcd00d 2515 */
JojoS 0:ab00d9bcd00d 2516 uint8_t MPU9150::getWakeFrequency() {
JojoS 0:ab00d9bcd00d 2517 i2Cdev.readBits(devAddr, MPU9150_RA_PWR_MGMT_2, MPU9150_PWR2_LP_WAKE_CTRL_BIT, MPU9150_PWR2_LP_WAKE_CTRL_LENGTH, buffer);
JojoS 0:ab00d9bcd00d 2518 return buffer[0];
JojoS 0:ab00d9bcd00d 2519 }
JojoS 0:ab00d9bcd00d 2520 /** Set wake frequency in Accel-Only Low Power Mode.
JojoS 0:ab00d9bcd00d 2521 * @param frequency New wake frequency
JojoS 0:ab00d9bcd00d 2522 * @see MPU9150_RA_PWR_MGMT_2
JojoS 0:ab00d9bcd00d 2523 */
JojoS 0:ab00d9bcd00d 2524 void MPU9150::setWakeFrequency(uint8_t frequency) {
JojoS 0:ab00d9bcd00d 2525 i2Cdev.writeBits(devAddr, MPU9150_RA_PWR_MGMT_2, MPU9150_PWR2_LP_WAKE_CTRL_BIT, MPU9150_PWR2_LP_WAKE_CTRL_LENGTH, frequency);
JojoS 0:ab00d9bcd00d 2526 }
JojoS 0:ab00d9bcd00d 2527
JojoS 0:ab00d9bcd00d 2528 /** Get X-axis accelerometer standby enabled status.
JojoS 0:ab00d9bcd00d 2529 * If enabled, the X-axis will not gather or report data (or use power).
JojoS 0:ab00d9bcd00d 2530 * @return Current X-axis standby enabled status
JojoS 0:ab00d9bcd00d 2531 * @see MPU9150_RA_PWR_MGMT_2
JojoS 0:ab00d9bcd00d 2532 * @see MPU9150_PWR2_STBY_XA_BIT
JojoS 0:ab00d9bcd00d 2533 */
JojoS 0:ab00d9bcd00d 2534 bool MPU9150::getStandbyXAccelEnabled() {
JojoS 0:ab00d9bcd00d 2535 i2Cdev.readBit(devAddr, MPU9150_RA_PWR_MGMT_2, MPU9150_PWR2_STBY_XA_BIT, buffer);
JojoS 0:ab00d9bcd00d 2536 return buffer[0];
JojoS 0:ab00d9bcd00d 2537 }
JojoS 0:ab00d9bcd00d 2538 /** Set X-axis accelerometer standby enabled status.
JojoS 0:ab00d9bcd00d 2539 * @param New X-axis standby enabled status
JojoS 0:ab00d9bcd00d 2540 * @see getStandbyXAccelEnabled()
JojoS 0:ab00d9bcd00d 2541 * @see MPU9150_RA_PWR_MGMT_2
JojoS 0:ab00d9bcd00d 2542 * @see MPU9150_PWR2_STBY_XA_BIT
JojoS 0:ab00d9bcd00d 2543 */
JojoS 0:ab00d9bcd00d 2544 void MPU9150::setStandbyXAccelEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 2545 i2Cdev.writeBit(devAddr, MPU9150_RA_PWR_MGMT_2, MPU9150_PWR2_STBY_XA_BIT, enabled);
JojoS 0:ab00d9bcd00d 2546 }
JojoS 0:ab00d9bcd00d 2547 /** Get Y-axis accelerometer standby enabled status.
JojoS 0:ab00d9bcd00d 2548 * If enabled, the Y-axis will not gather or report data (or use power).
JojoS 0:ab00d9bcd00d 2549 * @return Current Y-axis standby enabled status
JojoS 0:ab00d9bcd00d 2550 * @see MPU9150_RA_PWR_MGMT_2
JojoS 0:ab00d9bcd00d 2551 * @see MPU9150_PWR2_STBY_YA_BIT
JojoS 0:ab00d9bcd00d 2552 */
JojoS 0:ab00d9bcd00d 2553 bool MPU9150::getStandbyYAccelEnabled() {
JojoS 0:ab00d9bcd00d 2554 i2Cdev.readBit(devAddr, MPU9150_RA_PWR_MGMT_2, MPU9150_PWR2_STBY_YA_BIT, buffer);
JojoS 0:ab00d9bcd00d 2555 return buffer[0];
JojoS 0:ab00d9bcd00d 2556 }
JojoS 0:ab00d9bcd00d 2557 /** Set Y-axis accelerometer standby enabled status.
JojoS 0:ab00d9bcd00d 2558 * @param New Y-axis standby enabled status
JojoS 0:ab00d9bcd00d 2559 * @see getStandbyYAccelEnabled()
JojoS 0:ab00d9bcd00d 2560 * @see MPU9150_RA_PWR_MGMT_2
JojoS 0:ab00d9bcd00d 2561 * @see MPU9150_PWR2_STBY_YA_BIT
JojoS 0:ab00d9bcd00d 2562 */
JojoS 0:ab00d9bcd00d 2563 void MPU9150::setStandbyYAccelEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 2564 i2Cdev.writeBit(devAddr, MPU9150_RA_PWR_MGMT_2, MPU9150_PWR2_STBY_YA_BIT, enabled);
JojoS 0:ab00d9bcd00d 2565 }
JojoS 0:ab00d9bcd00d 2566 /** Get Z-axis accelerometer standby enabled status.
JojoS 0:ab00d9bcd00d 2567 * If enabled, the Z-axis will not gather or report data (or use power).
JojoS 0:ab00d9bcd00d 2568 * @return Current Z-axis standby enabled status
JojoS 0:ab00d9bcd00d 2569 * @see MPU9150_RA_PWR_MGMT_2
JojoS 0:ab00d9bcd00d 2570 * @see MPU9150_PWR2_STBY_ZA_BIT
JojoS 0:ab00d9bcd00d 2571 */
JojoS 0:ab00d9bcd00d 2572 bool MPU9150::getStandbyZAccelEnabled() {
JojoS 0:ab00d9bcd00d 2573 i2Cdev.readBit(devAddr, MPU9150_RA_PWR_MGMT_2, MPU9150_PWR2_STBY_ZA_BIT, buffer);
JojoS 0:ab00d9bcd00d 2574 return buffer[0];
JojoS 0:ab00d9bcd00d 2575 }
JojoS 0:ab00d9bcd00d 2576 /** Set Z-axis accelerometer standby enabled status.
JojoS 0:ab00d9bcd00d 2577 * @param New Z-axis standby enabled status
JojoS 0:ab00d9bcd00d 2578 * @see getStandbyZAccelEnabled()
JojoS 0:ab00d9bcd00d 2579 * @see MPU9150_RA_PWR_MGMT_2
JojoS 0:ab00d9bcd00d 2580 * @see MPU9150_PWR2_STBY_ZA_BIT
JojoS 0:ab00d9bcd00d 2581 */
JojoS 0:ab00d9bcd00d 2582 void MPU9150::setStandbyZAccelEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 2583 i2Cdev.writeBit(devAddr, MPU9150_RA_PWR_MGMT_2, MPU9150_PWR2_STBY_ZA_BIT, enabled);
JojoS 0:ab00d9bcd00d 2584 }
JojoS 0:ab00d9bcd00d 2585 /** Get X-axis gyroscope standby enabled status.
JojoS 0:ab00d9bcd00d 2586 * If enabled, the X-axis will not gather or report data (or use power).
JojoS 0:ab00d9bcd00d 2587 * @return Current X-axis standby enabled status
JojoS 0:ab00d9bcd00d 2588 * @see MPU9150_RA_PWR_MGMT_2
JojoS 0:ab00d9bcd00d 2589 * @see MPU9150_PWR2_STBY_XG_BIT
JojoS 0:ab00d9bcd00d 2590 */
JojoS 0:ab00d9bcd00d 2591 bool MPU9150::getStandbyXGyroEnabled() {
JojoS 0:ab00d9bcd00d 2592 i2Cdev.readBit(devAddr, MPU9150_RA_PWR_MGMT_2, MPU9150_PWR2_STBY_XG_BIT, buffer);
JojoS 0:ab00d9bcd00d 2593 return buffer[0];
JojoS 0:ab00d9bcd00d 2594 }
JojoS 0:ab00d9bcd00d 2595 /** Set X-axis gyroscope standby enabled status.
JojoS 0:ab00d9bcd00d 2596 * @param New X-axis standby enabled status
JojoS 0:ab00d9bcd00d 2597 * @see getStandbyXGyroEnabled()
JojoS 0:ab00d9bcd00d 2598 * @see MPU9150_RA_PWR_MGMT_2
JojoS 0:ab00d9bcd00d 2599 * @see MPU9150_PWR2_STBY_XG_BIT
JojoS 0:ab00d9bcd00d 2600 */
JojoS 0:ab00d9bcd00d 2601 void MPU9150::setStandbyXGyroEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 2602 i2Cdev.writeBit(devAddr, MPU9150_RA_PWR_MGMT_2, MPU9150_PWR2_STBY_XG_BIT, enabled);
JojoS 0:ab00d9bcd00d 2603 }
JojoS 0:ab00d9bcd00d 2604 /** Get Y-axis gyroscope standby enabled status.
JojoS 0:ab00d9bcd00d 2605 * If enabled, the Y-axis will not gather or report data (or use power).
JojoS 0:ab00d9bcd00d 2606 * @return Current Y-axis standby enabled status
JojoS 0:ab00d9bcd00d 2607 * @see MPU9150_RA_PWR_MGMT_2
JojoS 0:ab00d9bcd00d 2608 * @see MPU9150_PWR2_STBY_YG_BIT
JojoS 0:ab00d9bcd00d 2609 */
JojoS 0:ab00d9bcd00d 2610 bool MPU9150::getStandbyYGyroEnabled() {
JojoS 0:ab00d9bcd00d 2611 i2Cdev.readBit(devAddr, MPU9150_RA_PWR_MGMT_2, MPU9150_PWR2_STBY_YG_BIT, buffer);
JojoS 0:ab00d9bcd00d 2612 return buffer[0];
JojoS 0:ab00d9bcd00d 2613 }
JojoS 0:ab00d9bcd00d 2614 /** Set Y-axis gyroscope standby enabled status.
JojoS 0:ab00d9bcd00d 2615 * @param New Y-axis standby enabled status
JojoS 0:ab00d9bcd00d 2616 * @see getStandbyYGyroEnabled()
JojoS 0:ab00d9bcd00d 2617 * @see MPU9150_RA_PWR_MGMT_2
JojoS 0:ab00d9bcd00d 2618 * @see MPU9150_PWR2_STBY_YG_BIT
JojoS 0:ab00d9bcd00d 2619 */
JojoS 0:ab00d9bcd00d 2620 void MPU9150::setStandbyYGyroEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 2621 i2Cdev.writeBit(devAddr, MPU9150_RA_PWR_MGMT_2, MPU9150_PWR2_STBY_YG_BIT, enabled);
JojoS 0:ab00d9bcd00d 2622 }
JojoS 0:ab00d9bcd00d 2623 /** Get Z-axis gyroscope standby enabled status.
JojoS 0:ab00d9bcd00d 2624 * If enabled, the Z-axis will not gather or report data (or use power).
JojoS 0:ab00d9bcd00d 2625 * @return Current Z-axis standby enabled status
JojoS 0:ab00d9bcd00d 2626 * @see MPU9150_RA_PWR_MGMT_2
JojoS 0:ab00d9bcd00d 2627 * @see MPU9150_PWR2_STBY_ZG_BIT
JojoS 0:ab00d9bcd00d 2628 */
JojoS 0:ab00d9bcd00d 2629 bool MPU9150::getStandbyZGyroEnabled() {
JojoS 0:ab00d9bcd00d 2630 i2Cdev.readBit(devAddr, MPU9150_RA_PWR_MGMT_2, MPU9150_PWR2_STBY_ZG_BIT, buffer);
JojoS 0:ab00d9bcd00d 2631 return buffer[0];
JojoS 0:ab00d9bcd00d 2632 }
JojoS 0:ab00d9bcd00d 2633 /** Set Z-axis gyroscope standby enabled status.
JojoS 0:ab00d9bcd00d 2634 * @param New Z-axis standby enabled status
JojoS 0:ab00d9bcd00d 2635 * @see getStandbyZGyroEnabled()
JojoS 0:ab00d9bcd00d 2636 * @see MPU9150_RA_PWR_MGMT_2
JojoS 0:ab00d9bcd00d 2637 * @see MPU9150_PWR2_STBY_ZG_BIT
JojoS 0:ab00d9bcd00d 2638 */
JojoS 0:ab00d9bcd00d 2639 void MPU9150::setStandbyZGyroEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 2640 i2Cdev.writeBit(devAddr, MPU9150_RA_PWR_MGMT_2, MPU9150_PWR2_STBY_ZG_BIT, enabled);
JojoS 0:ab00d9bcd00d 2641 }
JojoS 0:ab00d9bcd00d 2642
JojoS 0:ab00d9bcd00d 2643 // FIFO_COUNT* registers
JojoS 0:ab00d9bcd00d 2644
JojoS 0:ab00d9bcd00d 2645 /** Get current FIFO buffer size.
JojoS 0:ab00d9bcd00d 2646 * This value indicates the number of bytes stored in the FIFO buffer. This
JojoS 0:ab00d9bcd00d 2647 * number is in turn the number of bytes that can be read from the FIFO buffer
JojoS 0:ab00d9bcd00d 2648 * and it is directly proportional to the number of samples available given the
JojoS 0:ab00d9bcd00d 2649 * set of sensor data bound to be stored in the FIFO (register 35 and 36).
JojoS 0:ab00d9bcd00d 2650 * @return Current FIFO buffer size
JojoS 0:ab00d9bcd00d 2651 */
JojoS 0:ab00d9bcd00d 2652 uint16_t MPU9150::getFIFOCount() {
JojoS 0:ab00d9bcd00d 2653 i2Cdev.readBytes(devAddr, MPU9150_RA_FIFO_COUNTH, 2, buffer);
JojoS 0:ab00d9bcd00d 2654 return (((uint16_t)buffer[0]) << 8) | buffer[1];
JojoS 0:ab00d9bcd00d 2655 }
JojoS 0:ab00d9bcd00d 2656
JojoS 0:ab00d9bcd00d 2657 // FIFO_R_W register
JojoS 0:ab00d9bcd00d 2658
JojoS 0:ab00d9bcd00d 2659 /** Get byte from FIFO buffer.
JojoS 0:ab00d9bcd00d 2660 * This register is used to read and write data from the FIFO buffer. Data is
JojoS 0:ab00d9bcd00d 2661 * written to the FIFO in order of register number (from lowest to highest). If
JojoS 0:ab00d9bcd00d 2662 * all the FIFO enable flags (see below) are enabled and all External Sensor
JojoS 0:ab00d9bcd00d 2663 * Data registers (Registers 73 to 96) are associated with a Slave device, the
JojoS 0:ab00d9bcd00d 2664 * contents of registers 59 through 96 will be written in order at the Sample
JojoS 0:ab00d9bcd00d 2665 * Rate.
JojoS 0:ab00d9bcd00d 2666 *
JojoS 0:ab00d9bcd00d 2667 * The contents of the sensor data registers (Registers 59 to 96) are written
JojoS 0:ab00d9bcd00d 2668 * into the FIFO buffer when their corresponding FIFO enable flags are set to 1
JojoS 0:ab00d9bcd00d 2669 * in FIFO_EN (Register 35). An additional flag for the sensor data registers
JojoS 0:ab00d9bcd00d 2670 * associated with I2C Slave 3 can be found in I2C_MST_CTRL (Register 36).
JojoS 0:ab00d9bcd00d 2671 *
JojoS 0:ab00d9bcd00d 2672 * If the FIFO buffer has overflowed, the status bit FIFO_OFLOW_INT is
JojoS 0:ab00d9bcd00d 2673 * automatically set to 1. This bit is located in INT_STATUS (Register 58).
JojoS 0:ab00d9bcd00d 2674 * When the FIFO buffer has overflowed, the oldest data will be lost and new
JojoS 0:ab00d9bcd00d 2675 * data will be written to the FIFO.
JojoS 0:ab00d9bcd00d 2676 *
JojoS 0:ab00d9bcd00d 2677 * If the FIFO buffer is empty, reading this register will return the last byte
JojoS 0:ab00d9bcd00d 2678 * that was previously read from the FIFO until new data is available. The user
JojoS 0:ab00d9bcd00d 2679 * should check FIFO_COUNT to ensure that the FIFO buffer is not read when
JojoS 0:ab00d9bcd00d 2680 * empty.
JojoS 0:ab00d9bcd00d 2681 *
JojoS 0:ab00d9bcd00d 2682 * @return Byte from FIFO buffer
JojoS 0:ab00d9bcd00d 2683 */
JojoS 0:ab00d9bcd00d 2684 uint8_t MPU9150::getFIFOByte() {
JojoS 0:ab00d9bcd00d 2685 i2Cdev.readByte(devAddr, MPU9150_RA_FIFO_R_W, buffer);
JojoS 0:ab00d9bcd00d 2686 return buffer[0];
JojoS 0:ab00d9bcd00d 2687 }
JojoS 0:ab00d9bcd00d 2688 void MPU9150::getFIFOBytes(uint8_t *data, uint8_t length) {
JojoS 0:ab00d9bcd00d 2689 i2Cdev.readBytes(devAddr, MPU9150_RA_FIFO_R_W, length, data);
JojoS 0:ab00d9bcd00d 2690 }
JojoS 0:ab00d9bcd00d 2691 /** Write byte to FIFO buffer.
JojoS 0:ab00d9bcd00d 2692 * @see getFIFOByte()
JojoS 0:ab00d9bcd00d 2693 * @see MPU9150_RA_FIFO_R_W
JojoS 0:ab00d9bcd00d 2694 */
JojoS 0:ab00d9bcd00d 2695 void MPU9150::setFIFOByte(uint8_t data) {
JojoS 0:ab00d9bcd00d 2696 i2Cdev.writeByte(devAddr, MPU9150_RA_FIFO_R_W, data);
JojoS 0:ab00d9bcd00d 2697 }
JojoS 0:ab00d9bcd00d 2698
JojoS 0:ab00d9bcd00d 2699 // WHO_AM_I register
JojoS 0:ab00d9bcd00d 2700
JojoS 0:ab00d9bcd00d 2701 /** Get Device ID.
JojoS 0:ab00d9bcd00d 2702 * This register is used to verify the identity of the device (0b110100, 0x34).
JojoS 0:ab00d9bcd00d 2703 * @return Device ID (6 bits only! should be 0x34)
JojoS 0:ab00d9bcd00d 2704 * @see MPU9150_RA_WHO_AM_I
JojoS 0:ab00d9bcd00d 2705 * @see MPU9150_WHO_AM_I_BIT
JojoS 0:ab00d9bcd00d 2706 * @see MPU9150_WHO_AM_I_LENGTH
JojoS 0:ab00d9bcd00d 2707 */
JojoS 0:ab00d9bcd00d 2708 uint8_t MPU9150::getDeviceID() {
JojoS 0:ab00d9bcd00d 2709 i2Cdev.readBits(devAddr, MPU9150_RA_WHO_AM_I, MPU9150_WHO_AM_I_BIT, MPU9150_WHO_AM_I_LENGTH, buffer);
JojoS 0:ab00d9bcd00d 2710 return buffer[0];
JojoS 0:ab00d9bcd00d 2711 }
JojoS 0:ab00d9bcd00d 2712 /** Set Device ID.
JojoS 0:ab00d9bcd00d 2713 * Write a new ID into the WHO_AM_I register (no idea why this should ever be
JojoS 0:ab00d9bcd00d 2714 * necessary though).
JojoS 0:ab00d9bcd00d 2715 * @param id New device ID to set.
JojoS 0:ab00d9bcd00d 2716 * @see getDeviceID()
JojoS 0:ab00d9bcd00d 2717 * @see MPU9150_RA_WHO_AM_I
JojoS 0:ab00d9bcd00d 2718 * @see MPU9150_WHO_AM_I_BIT
JojoS 0:ab00d9bcd00d 2719 * @see MPU9150_WHO_AM_I_LENGTH
JojoS 0:ab00d9bcd00d 2720 */
JojoS 0:ab00d9bcd00d 2721 void MPU9150::setDeviceID(uint8_t id) {
JojoS 0:ab00d9bcd00d 2722 i2Cdev.writeBits(devAddr, MPU9150_RA_WHO_AM_I, MPU9150_WHO_AM_I_BIT, MPU9150_WHO_AM_I_LENGTH, id);
JojoS 0:ab00d9bcd00d 2723 }
JojoS 0:ab00d9bcd00d 2724
JojoS 0:ab00d9bcd00d 2725 // ======== UNDOCUMENTED/DMP REGISTERS/METHODS ========
JojoS 0:ab00d9bcd00d 2726
JojoS 0:ab00d9bcd00d 2727 // XG_OFFS_TC register
JojoS 0:ab00d9bcd00d 2728
JojoS 0:ab00d9bcd00d 2729 uint8_t MPU9150::getOTPBankValid() {
JojoS 0:ab00d9bcd00d 2730 i2Cdev.readBit(devAddr, MPU9150_RA_XG_OFFS_TC, MPU9150_TC_OTP_BNK_VLD_BIT, buffer);
JojoS 0:ab00d9bcd00d 2731 return buffer[0];
JojoS 0:ab00d9bcd00d 2732 }
JojoS 0:ab00d9bcd00d 2733 void MPU9150::setOTPBankValid(bool enabled) {
JojoS 0:ab00d9bcd00d 2734 i2Cdev.writeBit(devAddr, MPU9150_RA_XG_OFFS_TC, MPU9150_TC_OTP_BNK_VLD_BIT, enabled);
JojoS 0:ab00d9bcd00d 2735 }
JojoS 0:ab00d9bcd00d 2736 int8_t MPU9150::getXGyroOffsetTC() {
JojoS 0:ab00d9bcd00d 2737 i2Cdev.readBits(devAddr, MPU9150_RA_XG_OFFS_TC, MPU9150_TC_OFFSET_BIT, MPU9150_TC_OFFSET_LENGTH, buffer);
JojoS 0:ab00d9bcd00d 2738 return buffer[0];
JojoS 0:ab00d9bcd00d 2739 }
JojoS 0:ab00d9bcd00d 2740 void MPU9150::setXGyroOffsetTC(int8_t offset) {
JojoS 0:ab00d9bcd00d 2741 i2Cdev.writeBits(devAddr, MPU9150_RA_XG_OFFS_TC, MPU9150_TC_OFFSET_BIT, MPU9150_TC_OFFSET_LENGTH, offset);
JojoS 0:ab00d9bcd00d 2742 }
JojoS 0:ab00d9bcd00d 2743
JojoS 0:ab00d9bcd00d 2744 // YG_OFFS_TC register
JojoS 0:ab00d9bcd00d 2745
JojoS 0:ab00d9bcd00d 2746 int8_t MPU9150::getYGyroOffsetTC() {
JojoS 0:ab00d9bcd00d 2747 i2Cdev.readBits(devAddr, MPU9150_RA_YG_OFFS_TC, MPU9150_TC_OFFSET_BIT, MPU9150_TC_OFFSET_LENGTH, buffer);
JojoS 0:ab00d9bcd00d 2748 return buffer[0];
JojoS 0:ab00d9bcd00d 2749 }
JojoS 0:ab00d9bcd00d 2750 void MPU9150::setYGyroOffsetTC(int8_t offset) {
JojoS 0:ab00d9bcd00d 2751 i2Cdev.writeBits(devAddr, MPU9150_RA_YG_OFFS_TC, MPU9150_TC_OFFSET_BIT, MPU9150_TC_OFFSET_LENGTH, offset);
JojoS 0:ab00d9bcd00d 2752 }
JojoS 0:ab00d9bcd00d 2753
JojoS 0:ab00d9bcd00d 2754 // ZG_OFFS_TC register
JojoS 0:ab00d9bcd00d 2755
JojoS 0:ab00d9bcd00d 2756 int8_t MPU9150::getZGyroOffsetTC() {
JojoS 0:ab00d9bcd00d 2757 i2Cdev.readBits(devAddr, MPU9150_RA_ZG_OFFS_TC, MPU9150_TC_OFFSET_BIT, MPU9150_TC_OFFSET_LENGTH, buffer);
JojoS 0:ab00d9bcd00d 2758 return buffer[0];
JojoS 0:ab00d9bcd00d 2759 }
JojoS 0:ab00d9bcd00d 2760 void MPU9150::setZGyroOffsetTC(int8_t offset) {
JojoS 0:ab00d9bcd00d 2761 i2Cdev.writeBits(devAddr, MPU9150_RA_ZG_OFFS_TC, MPU9150_TC_OFFSET_BIT, MPU9150_TC_OFFSET_LENGTH, offset);
JojoS 0:ab00d9bcd00d 2762 }
JojoS 0:ab00d9bcd00d 2763
JojoS 0:ab00d9bcd00d 2764 // X_FINE_GAIN register
JojoS 0:ab00d9bcd00d 2765
JojoS 0:ab00d9bcd00d 2766 int8_t MPU9150::getXFineGain() {
JojoS 0:ab00d9bcd00d 2767 i2Cdev.readByte(devAddr, MPU9150_RA_X_FINE_GAIN, buffer);
JojoS 0:ab00d9bcd00d 2768 return buffer[0];
JojoS 0:ab00d9bcd00d 2769 }
JojoS 0:ab00d9bcd00d 2770 void MPU9150::setXFineGain(int8_t gain) {
JojoS 0:ab00d9bcd00d 2771 i2Cdev.writeByte(devAddr, MPU9150_RA_X_FINE_GAIN, gain);
JojoS 0:ab00d9bcd00d 2772 }
JojoS 0:ab00d9bcd00d 2773
JojoS 0:ab00d9bcd00d 2774 // Y_FINE_GAIN register
JojoS 0:ab00d9bcd00d 2775
JojoS 0:ab00d9bcd00d 2776 int8_t MPU9150::getYFineGain() {
JojoS 0:ab00d9bcd00d 2777 i2Cdev.readByte(devAddr, MPU9150_RA_Y_FINE_GAIN, buffer);
JojoS 0:ab00d9bcd00d 2778 return buffer[0];
JojoS 0:ab00d9bcd00d 2779 }
JojoS 0:ab00d9bcd00d 2780 void MPU9150::setYFineGain(int8_t gain) {
JojoS 0:ab00d9bcd00d 2781 i2Cdev.writeByte(devAddr, MPU9150_RA_Y_FINE_GAIN, gain);
JojoS 0:ab00d9bcd00d 2782 }
JojoS 0:ab00d9bcd00d 2783
JojoS 0:ab00d9bcd00d 2784 // Z_FINE_GAIN register
JojoS 0:ab00d9bcd00d 2785
JojoS 0:ab00d9bcd00d 2786 int8_t MPU9150::getZFineGain() {
JojoS 0:ab00d9bcd00d 2787 i2Cdev.readByte(devAddr, MPU9150_RA_Z_FINE_GAIN, buffer);
JojoS 0:ab00d9bcd00d 2788 return buffer[0];
JojoS 0:ab00d9bcd00d 2789 }
JojoS 0:ab00d9bcd00d 2790 void MPU9150::setZFineGain(int8_t gain) {
JojoS 0:ab00d9bcd00d 2791 i2Cdev.writeByte(devAddr, MPU9150_RA_Z_FINE_GAIN, gain);
JojoS 0:ab00d9bcd00d 2792 }
JojoS 0:ab00d9bcd00d 2793
JojoS 0:ab00d9bcd00d 2794 // XA_OFFS_* registers
JojoS 0:ab00d9bcd00d 2795
JojoS 0:ab00d9bcd00d 2796 int16_t MPU9150::getXAccelOffset() {
JojoS 0:ab00d9bcd00d 2797 i2Cdev.readBytes(devAddr, MPU9150_RA_XA_OFFS_H, 2, buffer);
JojoS 0:ab00d9bcd00d 2798 return (((int16_t)buffer[0]) << 8) | buffer[1];
JojoS 0:ab00d9bcd00d 2799 }
JojoS 0:ab00d9bcd00d 2800 void MPU9150::setXAccelOffset(int16_t offset) {
JojoS 0:ab00d9bcd00d 2801 i2Cdev.writeWord(devAddr, MPU9150_RA_XA_OFFS_H, offset);
JojoS 0:ab00d9bcd00d 2802 }
JojoS 0:ab00d9bcd00d 2803
JojoS 0:ab00d9bcd00d 2804 // YA_OFFS_* register
JojoS 0:ab00d9bcd00d 2805
JojoS 0:ab00d9bcd00d 2806 int16_t MPU9150::getYAccelOffset() {
JojoS 0:ab00d9bcd00d 2807 i2Cdev.readBytes(devAddr, MPU9150_RA_YA_OFFS_H, 2, buffer);
JojoS 0:ab00d9bcd00d 2808 return (((int16_t)buffer[0]) << 8) | buffer[1];
JojoS 0:ab00d9bcd00d 2809 }
JojoS 0:ab00d9bcd00d 2810 void MPU9150::setYAccelOffset(int16_t offset) {
JojoS 0:ab00d9bcd00d 2811 i2Cdev.writeWord(devAddr, MPU9150_RA_YA_OFFS_H, offset);
JojoS 0:ab00d9bcd00d 2812 }
JojoS 0:ab00d9bcd00d 2813
JojoS 0:ab00d9bcd00d 2814 // ZA_OFFS_* register
JojoS 0:ab00d9bcd00d 2815
JojoS 0:ab00d9bcd00d 2816 int16_t MPU9150::getZAccelOffset() {
JojoS 0:ab00d9bcd00d 2817 i2Cdev.readBytes(devAddr, MPU9150_RA_ZA_OFFS_H, 2, buffer);
JojoS 0:ab00d9bcd00d 2818 return (((int16_t)buffer[0]) << 8) | buffer[1];
JojoS 0:ab00d9bcd00d 2819 }
JojoS 0:ab00d9bcd00d 2820 void MPU9150::setZAccelOffset(int16_t offset) {
JojoS 0:ab00d9bcd00d 2821 i2Cdev.writeWord(devAddr, MPU9150_RA_ZA_OFFS_H, offset);
JojoS 0:ab00d9bcd00d 2822 }
JojoS 0:ab00d9bcd00d 2823
JojoS 0:ab00d9bcd00d 2824 // XG_OFFS_USR* registers
JojoS 0:ab00d9bcd00d 2825
JojoS 0:ab00d9bcd00d 2826 int16_t MPU9150::getXGyroOffset() {
JojoS 0:ab00d9bcd00d 2827 i2Cdev.readBytes(devAddr, MPU9150_RA_XG_OFFS_USRH, 2, buffer);
JojoS 0:ab00d9bcd00d 2828 return (((int16_t)buffer[0]) << 8) | buffer[1];
JojoS 0:ab00d9bcd00d 2829 }
JojoS 0:ab00d9bcd00d 2830 void MPU9150::setXGyroOffset(int16_t offset) {
JojoS 0:ab00d9bcd00d 2831 i2Cdev.writeWord(devAddr, MPU9150_RA_XG_OFFS_USRH, offset);
JojoS 0:ab00d9bcd00d 2832 }
JojoS 0:ab00d9bcd00d 2833
JojoS 0:ab00d9bcd00d 2834 // YG_OFFS_USR* register
JojoS 0:ab00d9bcd00d 2835
JojoS 0:ab00d9bcd00d 2836 int16_t MPU9150::getYGyroOffset() {
JojoS 0:ab00d9bcd00d 2837 i2Cdev.readBytes(devAddr, MPU9150_RA_YG_OFFS_USRH, 2, buffer);
JojoS 0:ab00d9bcd00d 2838 return (((int16_t)buffer[0]) << 8) | buffer[1];
JojoS 0:ab00d9bcd00d 2839 }
JojoS 0:ab00d9bcd00d 2840 void MPU9150::setYGyroOffset(int16_t offset) {
JojoS 0:ab00d9bcd00d 2841 i2Cdev.writeWord(devAddr, MPU9150_RA_YG_OFFS_USRH, offset);
JojoS 0:ab00d9bcd00d 2842 }
JojoS 0:ab00d9bcd00d 2843
JojoS 0:ab00d9bcd00d 2844 // ZG_OFFS_USR* register
JojoS 0:ab00d9bcd00d 2845
JojoS 0:ab00d9bcd00d 2846 int16_t MPU9150::getZGyroOffset() {
JojoS 0:ab00d9bcd00d 2847 i2Cdev.readBytes(devAddr, MPU9150_RA_ZG_OFFS_USRH, 2, buffer);
JojoS 0:ab00d9bcd00d 2848 return (((int16_t)buffer[0]) << 8) | buffer[1];
JojoS 0:ab00d9bcd00d 2849 }
JojoS 0:ab00d9bcd00d 2850 void MPU9150::setZGyroOffset(int16_t offset) {
JojoS 0:ab00d9bcd00d 2851 i2Cdev.writeWord(devAddr, MPU9150_RA_ZG_OFFS_USRH, offset);
JojoS 0:ab00d9bcd00d 2852 }
JojoS 0:ab00d9bcd00d 2853
JojoS 0:ab00d9bcd00d 2854 // INT_ENABLE register (DMP functions)
JojoS 0:ab00d9bcd00d 2855
JojoS 0:ab00d9bcd00d 2856 bool MPU9150::getIntPLLReadyEnabled() {
JojoS 0:ab00d9bcd00d 2857 i2Cdev.readBit(devAddr, MPU9150_RA_INT_ENABLE, MPU9150_INTERRUPT_PLL_RDY_INT_BIT, buffer);
JojoS 0:ab00d9bcd00d 2858 return buffer[0];
JojoS 0:ab00d9bcd00d 2859 }
JojoS 0:ab00d9bcd00d 2860 void MPU9150::setIntPLLReadyEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 2861 i2Cdev.writeBit(devAddr, MPU9150_RA_INT_ENABLE, MPU9150_INTERRUPT_PLL_RDY_INT_BIT, enabled);
JojoS 0:ab00d9bcd00d 2862 }
JojoS 0:ab00d9bcd00d 2863 bool MPU9150::getIntDMPEnabled() {
JojoS 0:ab00d9bcd00d 2864 i2Cdev.readBit(devAddr, MPU9150_RA_INT_ENABLE, MPU9150_INTERRUPT_DMP_INT_BIT, buffer);
JojoS 0:ab00d9bcd00d 2865 return buffer[0];
JojoS 0:ab00d9bcd00d 2866 }
JojoS 0:ab00d9bcd00d 2867 void MPU9150::setIntDMPEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 2868 i2Cdev.writeBit(devAddr, MPU9150_RA_INT_ENABLE, MPU9150_INTERRUPT_DMP_INT_BIT, enabled);
JojoS 0:ab00d9bcd00d 2869 }
JojoS 0:ab00d9bcd00d 2870
JojoS 0:ab00d9bcd00d 2871 // DMP_INT_STATUS
JojoS 0:ab00d9bcd00d 2872
JojoS 0:ab00d9bcd00d 2873 bool MPU9150::getDMPInt5Status() {
JojoS 0:ab00d9bcd00d 2874 i2Cdev.readBit(devAddr, MPU9150_RA_DMP_INT_STATUS, MPU9150_DMPINT_5_BIT, buffer);
JojoS 0:ab00d9bcd00d 2875 return buffer[0];
JojoS 0:ab00d9bcd00d 2876 }
JojoS 0:ab00d9bcd00d 2877 bool MPU9150::getDMPInt4Status() {
JojoS 0:ab00d9bcd00d 2878 i2Cdev.readBit(devAddr, MPU9150_RA_DMP_INT_STATUS, MPU9150_DMPINT_4_BIT, buffer);
JojoS 0:ab00d9bcd00d 2879 return buffer[0];
JojoS 0:ab00d9bcd00d 2880 }
JojoS 0:ab00d9bcd00d 2881 bool MPU9150::getDMPInt3Status() {
JojoS 0:ab00d9bcd00d 2882 i2Cdev.readBit(devAddr, MPU9150_RA_DMP_INT_STATUS, MPU9150_DMPINT_3_BIT, buffer);
JojoS 0:ab00d9bcd00d 2883 return buffer[0];
JojoS 0:ab00d9bcd00d 2884 }
JojoS 0:ab00d9bcd00d 2885 bool MPU9150::getDMPInt2Status() {
JojoS 0:ab00d9bcd00d 2886 i2Cdev.readBit(devAddr, MPU9150_RA_DMP_INT_STATUS, MPU9150_DMPINT_2_BIT, buffer);
JojoS 0:ab00d9bcd00d 2887 return buffer[0];
JojoS 0:ab00d9bcd00d 2888 }
JojoS 0:ab00d9bcd00d 2889 bool MPU9150::getDMPInt1Status() {
JojoS 0:ab00d9bcd00d 2890 i2Cdev.readBit(devAddr, MPU9150_RA_DMP_INT_STATUS, MPU9150_DMPINT_1_BIT, buffer);
JojoS 0:ab00d9bcd00d 2891 return buffer[0];
JojoS 0:ab00d9bcd00d 2892 }
JojoS 0:ab00d9bcd00d 2893 bool MPU9150::getDMPInt0Status() {
JojoS 0:ab00d9bcd00d 2894 i2Cdev.readBit(devAddr, MPU9150_RA_DMP_INT_STATUS, MPU9150_DMPINT_0_BIT, buffer);
JojoS 0:ab00d9bcd00d 2895 return buffer[0];
JojoS 0:ab00d9bcd00d 2896 }
JojoS 0:ab00d9bcd00d 2897
JojoS 0:ab00d9bcd00d 2898 // INT_STATUS register (DMP functions)
JojoS 0:ab00d9bcd00d 2899
JojoS 0:ab00d9bcd00d 2900 bool MPU9150::getIntPLLReadyStatus() {
JojoS 0:ab00d9bcd00d 2901 i2Cdev.readBit(devAddr, MPU9150_RA_INT_STATUS, MPU9150_INTERRUPT_PLL_RDY_INT_BIT, buffer);
JojoS 0:ab00d9bcd00d 2902 return buffer[0];
JojoS 0:ab00d9bcd00d 2903 }
JojoS 0:ab00d9bcd00d 2904 bool MPU9150::getIntDMPStatus() {
JojoS 0:ab00d9bcd00d 2905 i2Cdev.readBit(devAddr, MPU9150_RA_INT_STATUS, MPU9150_INTERRUPT_DMP_INT_BIT, buffer);
JojoS 0:ab00d9bcd00d 2906 return buffer[0];
JojoS 0:ab00d9bcd00d 2907 }
JojoS 0:ab00d9bcd00d 2908
JojoS 0:ab00d9bcd00d 2909 // USER_CTRL register (DMP functions)
JojoS 0:ab00d9bcd00d 2910
JojoS 0:ab00d9bcd00d 2911 bool MPU9150::getDMPEnabled() {
JojoS 0:ab00d9bcd00d 2912 i2Cdev.readBit(devAddr, MPU9150_RA_USER_CTRL, MPU9150_USERCTRL_DMP_EN_BIT, buffer);
JojoS 0:ab00d9bcd00d 2913 return buffer[0];
JojoS 0:ab00d9bcd00d 2914 }
JojoS 0:ab00d9bcd00d 2915 void MPU9150::setDMPEnabled(bool enabled) {
JojoS 0:ab00d9bcd00d 2916 i2Cdev.writeBit(devAddr, MPU9150_RA_USER_CTRL, MPU9150_USERCTRL_DMP_EN_BIT, enabled);
JojoS 0:ab00d9bcd00d 2917 }
JojoS 0:ab00d9bcd00d 2918 void MPU9150::resetDMP() {
JojoS 0:ab00d9bcd00d 2919 i2Cdev.writeBit(devAddr, MPU9150_RA_USER_CTRL, MPU9150_USERCTRL_DMP_RESET_BIT, true);
JojoS 0:ab00d9bcd00d 2920 }
JojoS 0:ab00d9bcd00d 2921
JojoS 0:ab00d9bcd00d 2922 // BANK_SEL register
JojoS 0:ab00d9bcd00d 2923
JojoS 0:ab00d9bcd00d 2924 void MPU9150::setMemoryBank(uint8_t bank, bool prefetchEnabled, bool userBank) {
JojoS 0:ab00d9bcd00d 2925 bank &= 0x1F;
JojoS 0:ab00d9bcd00d 2926 if (userBank) bank |= 0x20;
JojoS 0:ab00d9bcd00d 2927 if (prefetchEnabled) bank |= 0x40;
JojoS 0:ab00d9bcd00d 2928 i2Cdev.writeByte(devAddr, MPU9150_RA_BANK_SEL, bank);
JojoS 0:ab00d9bcd00d 2929 }
JojoS 0:ab00d9bcd00d 2930
JojoS 0:ab00d9bcd00d 2931 // MEM_START_ADDR register
JojoS 0:ab00d9bcd00d 2932
JojoS 0:ab00d9bcd00d 2933 void MPU9150::setMemoryStartAddress(uint8_t address) {
JojoS 0:ab00d9bcd00d 2934 i2Cdev.writeByte(devAddr, MPU9150_RA_MEM_START_ADDR, address);
JojoS 0:ab00d9bcd00d 2935 }
JojoS 0:ab00d9bcd00d 2936
JojoS 0:ab00d9bcd00d 2937 // MEM_R_W register
JojoS 0:ab00d9bcd00d 2938
JojoS 0:ab00d9bcd00d 2939 uint8_t MPU9150::readMemoryByte() {
JojoS 0:ab00d9bcd00d 2940 i2Cdev.readByte(devAddr, MPU9150_RA_MEM_R_W, buffer);
JojoS 0:ab00d9bcd00d 2941 return buffer[0];
JojoS 0:ab00d9bcd00d 2942 }
JojoS 0:ab00d9bcd00d 2943 void MPU9150::writeMemoryByte(uint8_t data) {
JojoS 0:ab00d9bcd00d 2944 i2Cdev.writeByte(devAddr, MPU9150_RA_MEM_R_W, data);
JojoS 0:ab00d9bcd00d 2945 }
JojoS 0:ab00d9bcd00d 2946 void MPU9150::readMemoryBlock(uint8_t *data, uint16_t dataSize, uint8_t bank, uint8_t address) {
JojoS 0:ab00d9bcd00d 2947 setMemoryBank(bank);
JojoS 0:ab00d9bcd00d 2948 setMemoryStartAddress(address);
JojoS 0:ab00d9bcd00d 2949 uint8_t chunkSize;
JojoS 0:ab00d9bcd00d 2950 for (uint16_t i = 0; i < dataSize;) {
JojoS 0:ab00d9bcd00d 2951 // determine correct chunk size according to bank position and data size
JojoS 0:ab00d9bcd00d 2952 chunkSize = MPU9150_DMP_MEMORY_CHUNK_SIZE;
JojoS 0:ab00d9bcd00d 2953
JojoS 0:ab00d9bcd00d 2954 // make sure we don't go past the data size
JojoS 0:ab00d9bcd00d 2955 if (i + chunkSize > dataSize) chunkSize = dataSize - i;
JojoS 0:ab00d9bcd00d 2956
JojoS 0:ab00d9bcd00d 2957 // make sure this chunk doesn't go past the bank boundary (256 bytes)
JojoS 0:ab00d9bcd00d 2958 if (chunkSize > 256 - address) chunkSize = 256 - address;
JojoS 0:ab00d9bcd00d 2959
JojoS 0:ab00d9bcd00d 2960 // read the chunk of data as specified
JojoS 0:ab00d9bcd00d 2961 i2Cdev.readBytes(devAddr, MPU9150_RA_MEM_R_W, chunkSize, data + i);
JojoS 0:ab00d9bcd00d 2962
JojoS 0:ab00d9bcd00d 2963 // increase byte index by [chunkSize]
JojoS 0:ab00d9bcd00d 2964 i += chunkSize;
JojoS 0:ab00d9bcd00d 2965
JojoS 0:ab00d9bcd00d 2966 // uint8_t automatically wraps to 0 at 256
JojoS 0:ab00d9bcd00d 2967 address += chunkSize;
JojoS 0:ab00d9bcd00d 2968
JojoS 0:ab00d9bcd00d 2969 // if we aren't done, update bank (if necessary) and address
JojoS 0:ab00d9bcd00d 2970 if (i < dataSize) {
JojoS 0:ab00d9bcd00d 2971 if (address == 0) bank++;
JojoS 0:ab00d9bcd00d 2972 setMemoryBank(bank);
JojoS 0:ab00d9bcd00d 2973 setMemoryStartAddress(address);
JojoS 0:ab00d9bcd00d 2974 }
JojoS 0:ab00d9bcd00d 2975 }
JojoS 0:ab00d9bcd00d 2976 }
JojoS 0:ab00d9bcd00d 2977 bool MPU9150::writeMemoryBlock(const uint8_t *data, uint16_t dataSize, uint8_t bank, uint8_t address, bool verify, bool useProgMem) {
JojoS 0:ab00d9bcd00d 2978 setMemoryBank(bank);
JojoS 0:ab00d9bcd00d 2979 setMemoryStartAddress(address);
JojoS 0:ab00d9bcd00d 2980 uint8_t chunkSize;
JojoS 0:ab00d9bcd00d 2981 uint8_t *verifyBuffer=0;
JojoS 0:ab00d9bcd00d 2982 uint8_t *progBuffer=0;
JojoS 0:ab00d9bcd00d 2983 uint16_t i;
JojoS 0:ab00d9bcd00d 2984 uint8_t j;
JojoS 0:ab00d9bcd00d 2985 if (verify) verifyBuffer = (uint8_t *)malloc(MPU9150_DMP_MEMORY_CHUNK_SIZE);
JojoS 0:ab00d9bcd00d 2986 if (useProgMem) progBuffer = (uint8_t *)malloc(MPU9150_DMP_MEMORY_CHUNK_SIZE);
JojoS 0:ab00d9bcd00d 2987 for (i = 0; i < dataSize;) {
JojoS 0:ab00d9bcd00d 2988 // determine correct chunk size according to bank position and data size
JojoS 0:ab00d9bcd00d 2989 chunkSize = MPU9150_DMP_MEMORY_CHUNK_SIZE;
JojoS 0:ab00d9bcd00d 2990
JojoS 0:ab00d9bcd00d 2991 // make sure we don't go past the data size
JojoS 0:ab00d9bcd00d 2992 if (i + chunkSize > dataSize) chunkSize = dataSize - i;
JojoS 0:ab00d9bcd00d 2993
JojoS 0:ab00d9bcd00d 2994 // make sure this chunk doesn't go past the bank boundary (256 bytes)
JojoS 0:ab00d9bcd00d 2995 if (chunkSize > 256 - address) chunkSize = 256 - address;
JojoS 0:ab00d9bcd00d 2996
JojoS 0:ab00d9bcd00d 2997 if (useProgMem) {
JojoS 0:ab00d9bcd00d 2998 // write the chunk of data as specified
JojoS 0:ab00d9bcd00d 2999 for (j = 0; j < chunkSize; j++) progBuffer[j] = pgm_read_byte(data + i + j);
JojoS 0:ab00d9bcd00d 3000 } else {
JojoS 0:ab00d9bcd00d 3001 // write the chunk of data as specified
JojoS 0:ab00d9bcd00d 3002 progBuffer = (uint8_t *)data + i;
JojoS 0:ab00d9bcd00d 3003 }
JojoS 0:ab00d9bcd00d 3004
JojoS 0:ab00d9bcd00d 3005 i2Cdev.writeBytes(devAddr, MPU9150_RA_MEM_R_W, chunkSize, progBuffer);
JojoS 0:ab00d9bcd00d 3006
JojoS 0:ab00d9bcd00d 3007 // verify data if needed
JojoS 0:ab00d9bcd00d 3008 if (verify && verifyBuffer) {
JojoS 0:ab00d9bcd00d 3009 setMemoryBank(bank);
JojoS 0:ab00d9bcd00d 3010 setMemoryStartAddress(address);
JojoS 0:ab00d9bcd00d 3011 i2Cdev.readBytes(devAddr, MPU9150_RA_MEM_R_W, chunkSize, verifyBuffer);
JojoS 0:ab00d9bcd00d 3012 if (memcmp(progBuffer, verifyBuffer, chunkSize) != 0) {
JojoS 0:ab00d9bcd00d 3013 /*Serial.print("Block write verification error, bank ");
JojoS 0:ab00d9bcd00d 3014 Serial.print(bank, DEC);
JojoS 0:ab00d9bcd00d 3015 Serial.print(", address ");
JojoS 0:ab00d9bcd00d 3016 Serial.print(address, DEC);
JojoS 0:ab00d9bcd00d 3017 Serial.print("!\nExpected:");
JojoS 0:ab00d9bcd00d 3018 for (j = 0; j < chunkSize; j++) {
JojoS 0:ab00d9bcd00d 3019 Serial.print(" 0x");
JojoS 0:ab00d9bcd00d 3020 if (progBuffer[j] < 16) Serial.print("0");
JojoS 0:ab00d9bcd00d 3021 Serial.print(progBuffer[j], HEX);
JojoS 0:ab00d9bcd00d 3022 }
JojoS 0:ab00d9bcd00d 3023 Serial.print("\nReceived:");
JojoS 0:ab00d9bcd00d 3024 for (uint8_t j = 0; j < chunkSize; j++) {
JojoS 0:ab00d9bcd00d 3025 Serial.print(" 0x");
JojoS 0:ab00d9bcd00d 3026 if (verifyBuffer[i + j] < 16) Serial.print("0");
JojoS 0:ab00d9bcd00d 3027 Serial.print(verifyBuffer[i + j], HEX);
JojoS 0:ab00d9bcd00d 3028 }
JojoS 0:ab00d9bcd00d 3029 Serial.print("\n");*/
JojoS 0:ab00d9bcd00d 3030 free(verifyBuffer);
JojoS 0:ab00d9bcd00d 3031 if (useProgMem) free(progBuffer);
JojoS 0:ab00d9bcd00d 3032 return false; // uh oh.
JojoS 0:ab00d9bcd00d 3033 }
JojoS 0:ab00d9bcd00d 3034 }
JojoS 0:ab00d9bcd00d 3035
JojoS 0:ab00d9bcd00d 3036 // increase byte index by [chunkSize]
JojoS 0:ab00d9bcd00d 3037 i += chunkSize;
JojoS 0:ab00d9bcd00d 3038
JojoS 0:ab00d9bcd00d 3039 // uint8_t automatically wraps to 0 at 256
JojoS 0:ab00d9bcd00d 3040 address += chunkSize;
JojoS 0:ab00d9bcd00d 3041
JojoS 0:ab00d9bcd00d 3042 // if we aren't done, update bank (if necessary) and address
JojoS 0:ab00d9bcd00d 3043 if (i < dataSize) {
JojoS 0:ab00d9bcd00d 3044 if (address == 0) bank++;
JojoS 0:ab00d9bcd00d 3045 setMemoryBank(bank);
JojoS 0:ab00d9bcd00d 3046 setMemoryStartAddress(address);
JojoS 0:ab00d9bcd00d 3047 }
JojoS 0:ab00d9bcd00d 3048 }
JojoS 0:ab00d9bcd00d 3049 if (verify) free(verifyBuffer);
JojoS 0:ab00d9bcd00d 3050 if (useProgMem) free(progBuffer);
JojoS 0:ab00d9bcd00d 3051 return true;
JojoS 0:ab00d9bcd00d 3052 }
JojoS 0:ab00d9bcd00d 3053 bool MPU9150::writeProgMemoryBlock(const uint8_t *data, uint16_t dataSize, uint8_t bank, uint8_t address, bool verify) {
JojoS 0:ab00d9bcd00d 3054 return writeMemoryBlock(data, dataSize, bank, address, verify, true);
JojoS 0:ab00d9bcd00d 3055 }
JojoS 0:ab00d9bcd00d 3056 bool MPU9150::writeDMPConfigurationSet(const uint8_t *data, uint16_t dataSize, bool useProgMem) {
JojoS 0:ab00d9bcd00d 3057 uint8_t *progBuffer=0, success, special;
JojoS 0:ab00d9bcd00d 3058 uint16_t i, j;
JojoS 0:ab00d9bcd00d 3059 if (useProgMem) {
JojoS 0:ab00d9bcd00d 3060 progBuffer = (uint8_t *)malloc(8); // assume 8-byte blocks, realloc later if necessary
JojoS 0:ab00d9bcd00d 3061 }
JojoS 0:ab00d9bcd00d 3062
JojoS 0:ab00d9bcd00d 3063 // config set data is a long string of blocks with the following structure:
JojoS 0:ab00d9bcd00d 3064 // [bank] [offset] [length] [byte[0], byte[1], ..., byte[length]]
JojoS 0:ab00d9bcd00d 3065 uint8_t bank, offset, length;
JojoS 0:ab00d9bcd00d 3066 for (i = 0; i < dataSize;) {
JojoS 0:ab00d9bcd00d 3067 if (useProgMem) {
JojoS 0:ab00d9bcd00d 3068 bank = pgm_read_byte(data + i++);
JojoS 0:ab00d9bcd00d 3069 offset = pgm_read_byte(data + i++);
JojoS 0:ab00d9bcd00d 3070 length = pgm_read_byte(data + i++);
JojoS 0:ab00d9bcd00d 3071 } else {
JojoS 0:ab00d9bcd00d 3072 bank = data[i++];
JojoS 0:ab00d9bcd00d 3073 offset = data[i++];
JojoS 0:ab00d9bcd00d 3074 length = data[i++];
JojoS 0:ab00d9bcd00d 3075 }
JojoS 0:ab00d9bcd00d 3076
JojoS 0:ab00d9bcd00d 3077 // write data or perform special action
JojoS 0:ab00d9bcd00d 3078 if (length > 0) {
JojoS 0:ab00d9bcd00d 3079 // regular block of data to write
JojoS 0:ab00d9bcd00d 3080 /*Serial.print("Writing config block to bank ");
JojoS 0:ab00d9bcd00d 3081 Serial.print(bank);
JojoS 0:ab00d9bcd00d 3082 Serial.print(", offset ");
JojoS 0:ab00d9bcd00d 3083 Serial.print(offset);
JojoS 0:ab00d9bcd00d 3084 Serial.print(", length=");
JojoS 0:ab00d9bcd00d 3085 Serial.println(length);*/
JojoS 0:ab00d9bcd00d 3086 if (useProgMem) {
JojoS 0:ab00d9bcd00d 3087 if (sizeof(progBuffer) < length) progBuffer = (uint8_t *)realloc(progBuffer, length);
JojoS 0:ab00d9bcd00d 3088 for (j = 0; j < length; j++) progBuffer[j] = pgm_read_byte(data + i + j);
JojoS 0:ab00d9bcd00d 3089 } else {
JojoS 0:ab00d9bcd00d 3090 progBuffer = (uint8_t *)data + i;
JojoS 0:ab00d9bcd00d 3091 }
JojoS 0:ab00d9bcd00d 3092 success = writeMemoryBlock(progBuffer, length, bank, offset, true);
JojoS 0:ab00d9bcd00d 3093 i += length;
JojoS 0:ab00d9bcd00d 3094 } else {
JojoS 0:ab00d9bcd00d 3095 // special instruction
JojoS 0:ab00d9bcd00d 3096 // NOTE: this kind of behavior (what and when to do certain things)
JojoS 0:ab00d9bcd00d 3097 // is totally undocumented. This code is in here based on observed
JojoS 0:ab00d9bcd00d 3098 // behavior only, and exactly why (or even whether) it has to be here
JojoS 0:ab00d9bcd00d 3099 // is anybody's guess for now.
JojoS 0:ab00d9bcd00d 3100 if (useProgMem) {
JojoS 0:ab00d9bcd00d 3101 special = pgm_read_byte(data + i++);
JojoS 0:ab00d9bcd00d 3102 } else {
JojoS 0:ab00d9bcd00d 3103 special = data[i++];
JojoS 0:ab00d9bcd00d 3104 }
JojoS 0:ab00d9bcd00d 3105 /*Serial.print("Special command code ");
JojoS 0:ab00d9bcd00d 3106 Serial.print(special, HEX);
JojoS 0:ab00d9bcd00d 3107 Serial.println(" found...");*/
JojoS 0:ab00d9bcd00d 3108 if (special == 0x01) {
JojoS 0:ab00d9bcd00d 3109 // enable DMP-related interrupts
JojoS 0:ab00d9bcd00d 3110
JojoS 0:ab00d9bcd00d 3111 //setIntZeroMotionEnabled(true);
JojoS 0:ab00d9bcd00d 3112 //setIntFIFOBufferOverflowEnabled(true);
JojoS 0:ab00d9bcd00d 3113 //setIntDMPEnabled(true);
JojoS 0:ab00d9bcd00d 3114 i2Cdev.writeByte(devAddr, MPU9150_RA_INT_ENABLE, 0x32); // single operation
JojoS 0:ab00d9bcd00d 3115
JojoS 0:ab00d9bcd00d 3116 success = true;
JojoS 0:ab00d9bcd00d 3117 } else {
JojoS 0:ab00d9bcd00d 3118 // unknown special command
JojoS 0:ab00d9bcd00d 3119 success = false;
JojoS 0:ab00d9bcd00d 3120 }
JojoS 0:ab00d9bcd00d 3121 }
JojoS 0:ab00d9bcd00d 3122
JojoS 0:ab00d9bcd00d 3123 if (!success) {
JojoS 0:ab00d9bcd00d 3124 if (useProgMem) free(progBuffer);
JojoS 0:ab00d9bcd00d 3125 return false; // uh oh
JojoS 0:ab00d9bcd00d 3126 }
JojoS 0:ab00d9bcd00d 3127 }
JojoS 0:ab00d9bcd00d 3128 if (useProgMem) free(progBuffer);
JojoS 0:ab00d9bcd00d 3129 return true;
JojoS 0:ab00d9bcd00d 3130 }
JojoS 0:ab00d9bcd00d 3131 bool MPU9150::writeProgDMPConfigurationSet(const uint8_t *data, uint16_t dataSize) {
JojoS 0:ab00d9bcd00d 3132 return writeDMPConfigurationSet(data, dataSize, true);
JojoS 0:ab00d9bcd00d 3133 }
JojoS 0:ab00d9bcd00d 3134
JojoS 0:ab00d9bcd00d 3135 // DMP_CFG_1 register
JojoS 0:ab00d9bcd00d 3136
JojoS 0:ab00d9bcd00d 3137 uint8_t MPU9150::getDMPConfig1() {
JojoS 0:ab00d9bcd00d 3138 i2Cdev.readByte(devAddr, MPU9150_RA_DMP_CFG_1, buffer);
JojoS 0:ab00d9bcd00d 3139 return buffer[0];
JojoS 0:ab00d9bcd00d 3140 }
JojoS 0:ab00d9bcd00d 3141 void MPU9150::setDMPConfig1(uint8_t config) {
JojoS 0:ab00d9bcd00d 3142 i2Cdev.writeByte(devAddr, MPU9150_RA_DMP_CFG_1, config);
JojoS 0:ab00d9bcd00d 3143 }
JojoS 0:ab00d9bcd00d 3144
JojoS 0:ab00d9bcd00d 3145 // DMP_CFG_2 register
JojoS 0:ab00d9bcd00d 3146
JojoS 0:ab00d9bcd00d 3147 uint8_t MPU9150::getDMPConfig2() {
JojoS 0:ab00d9bcd00d 3148 i2Cdev.readByte(devAddr, MPU9150_RA_DMP_CFG_2, buffer);
JojoS 0:ab00d9bcd00d 3149 return buffer[0];
JojoS 0:ab00d9bcd00d 3150 }
JojoS 0:ab00d9bcd00d 3151 void MPU9150::setDMPConfig2(uint8_t config) {
JojoS 0:ab00d9bcd00d 3152 i2Cdev.writeByte(devAddr, MPU9150_RA_DMP_CFG_2, config);
JojoS 0:ab00d9bcd00d 3153 }