MPU9250

Dependents:   FreeIMU

Fork of MPU6050 by Yifei Teng

Committer:
tyftyftyf
Date:
Wed Apr 18 20:24:13 2018 +0000
Revision:
22:ec9725201195
Parent:
21:ae3ee2d811eb
change to I2C 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
garfieldsg 0:662207e34fba 1 //ported from arduino library: https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6050
garfieldsg 0:662207e34fba 2 //written by szymon gaertig (email: szymon@gaertig.com.pl)
garfieldsg 0:662207e34fba 3 //
garfieldsg 0:662207e34fba 4 //Changelog:
garfieldsg 0:662207e34fba 5 //2013-01-08 - first beta release
garfieldsg 0:662207e34fba 6
garfieldsg 0:662207e34fba 7 // I2Cdev library collection - MPU6050 I2C device class
garfieldsg 0:662207e34fba 8 // Based on InvenSense MPU-6050 register map document rev. 2.0, 5/19/2011 (RM-MPU-6000A-00)
garfieldsg 0:662207e34fba 9 // 8/24/2011 by Jeff Rowberg <jeff@rowberg.net>
garfieldsg 0:662207e34fba 10 // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
garfieldsg 0:662207e34fba 11 //
garfieldsg 0:662207e34fba 12 // Changelog:
garfieldsg 0:662207e34fba 13 // ... - ongoing debug release
garfieldsg 0:662207e34fba 14
garfieldsg 0:662207e34fba 15 // NOTE: THIS IS ONLY A PARIAL RELEASE. THIS DEVICE CLASS IS CURRENTLY UNDERGOING ACTIVE
garfieldsg 0:662207e34fba 16 // DEVELOPMENT AND IS STILL MISSING SOME IMPORTANT FEATURES. PLEASE KEEP THIS IN MIND IF
garfieldsg 0:662207e34fba 17 // YOU DECIDE TO USE THIS PARTICULAR CODE FOR ANYTHING.
garfieldsg 0:662207e34fba 18
garfieldsg 0:662207e34fba 19 /* ============================================
garfieldsg 0:662207e34fba 20 I2Cdev device library code is placed under the MIT license
garfieldsg 0:662207e34fba 21 Copyright (c) 2012 Jeff Rowberg
garfieldsg 0:662207e34fba 22
garfieldsg 0:662207e34fba 23 Permission is hereby granted, free of charge, to any person obtaining a copy
garfieldsg 0:662207e34fba 24 of this software and associated documentation files (the "Software"), to deal
garfieldsg 0:662207e34fba 25 in the Software without restriction, including without limitation the rights
garfieldsg 0:662207e34fba 26 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
garfieldsg 0:662207e34fba 27 copies of the Software, and to permit persons to whom the Software is
garfieldsg 0:662207e34fba 28 furnished to do so, subject to the following conditions:
garfieldsg 0:662207e34fba 29
garfieldsg 0:662207e34fba 30 The above copyright notice and this permission notice shall be included in
garfieldsg 0:662207e34fba 31 all copies or substantial portions of the Software.
garfieldsg 0:662207e34fba 32
garfieldsg 0:662207e34fba 33 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
garfieldsg 0:662207e34fba 34 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
garfieldsg 0:662207e34fba 35 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
garfieldsg 0:662207e34fba 36 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
garfieldsg 0:662207e34fba 37 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
garfieldsg 0:662207e34fba 38 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
garfieldsg 0:662207e34fba 39 THE SOFTWARE.
garfieldsg 0:662207e34fba 40 ===============================================
garfieldsg 0:662207e34fba 41 */
garfieldsg 0:662207e34fba 42
tyftyftyf 13:a74f2d622b54 43 #include "mbed.h"
garfieldsg 0:662207e34fba 44 #include "MPU6050.h"
tyftyftyf 9:d879deb55ae1 45 #include "rtos.h"
garfieldsg 0:662207e34fba 46
pommzorz 3:8a6a7d6b6816 47 //#define useDebugSerial
garfieldsg 0:662207e34fba 48
garfieldsg 0:662207e34fba 49 //instead of using pgmspace.h
garfieldsg 0:662207e34fba 50 typedef const unsigned char prog_uchar;
garfieldsg 0:662207e34fba 51 #define pgm_read_byte_near(x) (*(prog_uchar*)x)
garfieldsg 0:662207e34fba 52 #define pgm_read_byte(x) (*(prog_uchar*)x)
garfieldsg 0:662207e34fba 53
garfieldsg 0:662207e34fba 54 /** Default constructor, uses default I2C address.
garfieldsg 0:662207e34fba 55 * @see MPU6050_DEFAULT_ADDRESS
garfieldsg 0:662207e34fba 56 */
tyftyftyf 11:9549be34fa7f 57 MPU6050::MPU6050() : debugSerial(USBTX, USBRX), sampling(false)
garfieldsg 0:662207e34fba 58 {
tyftyftyf 9:d879deb55ae1 59 devAddr = MPU6050_DEFAULT_ADDRESS << 1;
tyftyftyf 9:d879deb55ae1 60 debugSerial.baud(115200);
tyftyftyf 21:ae3ee2d811eb 61
tyftyftyf 21:ae3ee2d811eb 62 tX[0] = 0; tX[1] = 1; tX[2] = 0;
tyftyftyf 21:ae3ee2d811eb 63 tY[0] = 1; tY[1] = 0; tY[2] = 0;
tyftyftyf 21:ae3ee2d811eb 64 tZ[0] = 0; tZ[1] = 0; tZ[2] = -1;
tyftyftyf 7:95e74f827c08 65 }
tyftyftyf 7:95e74f827c08 66
garfieldsg 0:662207e34fba 67 /** Specific address constructor.
garfieldsg 0:662207e34fba 68 * @param address I2C address
garfieldsg 0:662207e34fba 69 * @see MPU6050_DEFAULT_ADDRESS
garfieldsg 0:662207e34fba 70 * @see MPU6050_ADDRESS_AD0_LOW
garfieldsg 0:662207e34fba 71 * @see MPU6050_ADDRESS_AD0_HIGH
garfieldsg 0:662207e34fba 72 */
tyftyftyf 11:9549be34fa7f 73 MPU6050::MPU6050(uint8_t address) : debugSerial(USBTX, USBRX), sampling(false)
garfieldsg 0:662207e34fba 74 {
tyftyftyf 9:d879deb55ae1 75 devAddr = address << 1;
tyftyftyf 9:d879deb55ae1 76 debugSerial.baud(115200);
tyftyftyf 21:ae3ee2d811eb 77
tyftyftyf 21:ae3ee2d811eb 78 tX[0] = 0; tX[1] = 1; tX[2] = 0;
tyftyftyf 21:ae3ee2d811eb 79 tY[0] = 1; tY[1] = 0; tY[2] = 0;
tyftyftyf 21:ae3ee2d811eb 80 tZ[0] = 0; tZ[1] = 0; tZ[2] = -1;
garfieldsg 0:662207e34fba 81 }
garfieldsg 0:662207e34fba 82
garfieldsg 0:662207e34fba 83 /** Power on and prepare for general usage.
garfieldsg 0:662207e34fba 84 * This will activate the device and take it out of sleep mode (which must be done
garfieldsg 0:662207e34fba 85 * after start-up). This function also sets both the accelerometer and the gyroscope
garfieldsg 0:662207e34fba 86 * to their most sensitive settings, namely +/- 2g and +/- 250 degrees/sec, and sets
garfieldsg 0:662207e34fba 87 * the clock source to use the X Gyro for reference, which is slightly better than
garfieldsg 0:662207e34fba 88 * the default internal clock source.
garfieldsg 0:662207e34fba 89 */
garfieldsg 0:662207e34fba 90 void MPU6050::initialize()
garfieldsg 0:662207e34fba 91 {
tyftyftyf 7:95e74f827c08 92 #ifdef useDebugSerial
garfieldsg 0:662207e34fba 93 debugSerial.printf("MPU6050::initialize start\n");
garfieldsg 0:662207e34fba 94 #endif
garfieldsg 0:662207e34fba 95 setClockSource(MPU6050_CLOCK_PLL_XGYRO);
garfieldsg 0:662207e34fba 96 setFullScaleGyroRange(MPU6050_GYRO_FS_250);
garfieldsg 0:662207e34fba 97 setFullScaleAccelRange(MPU6050_ACCEL_FS_2);
garfieldsg 0:662207e34fba 98 setSleepEnabled(false); // thanks to Jack Elston for pointing this one out!
garfieldsg 0:662207e34fba 99
garfieldsg 0:662207e34fba 100 #ifdef useDebugSerial
garfieldsg 0:662207e34fba 101 debugSerial.printf("MPU6050::initialize end\n");
garfieldsg 0:662207e34fba 102 #endif
garfieldsg 0:662207e34fba 103 }
garfieldsg 0:662207e34fba 104
garfieldsg 0:662207e34fba 105 /** Verify the I2C connection.
garfieldsg 0:662207e34fba 106 * Make sure the device is connected and responds as expected.
garfieldsg 0:662207e34fba 107 * @return True if connection is valid, false otherwise
garfieldsg 0:662207e34fba 108 */
garfieldsg 0:662207e34fba 109 bool MPU6050::testConnection()
garfieldsg 0:662207e34fba 110 {
garfieldsg 0:662207e34fba 111 #ifdef useDebugSerial
garfieldsg 0:662207e34fba 112 debugSerial.printf("MPU6050::testConnection start\n");
garfieldsg 0:662207e34fba 113 #endif
garfieldsg 0:662207e34fba 114 uint8_t deviceId = getDeviceID();
garfieldsg 0:662207e34fba 115 #ifdef useDebugSerial
garfieldsg 0:662207e34fba 116 debugSerial.printf("DeviceId = %d\n",deviceId);
garfieldsg 0:662207e34fba 117 #endif
garfieldsg 0:662207e34fba 118 return deviceId == 0x34;
garfieldsg 0:662207e34fba 119 }
garfieldsg 0:662207e34fba 120
garfieldsg 0:662207e34fba 121 // AUX_VDDIO register (InvenSense demo code calls this RA_*G_OFFS_TC)
garfieldsg 0:662207e34fba 122
garfieldsg 0:662207e34fba 123 /** Get the auxiliary I2C supply voltage level.
garfieldsg 0:662207e34fba 124 * When set to 1, the auxiliary I2C bus high logic level is VDD. When cleared to
garfieldsg 0:662207e34fba 125 * 0, the auxiliary I2C bus high logic level is VLOGIC. This does not apply to
garfieldsg 0:662207e34fba 126 * the MPU-6000, which does not have a VLOGIC pin.
garfieldsg 0:662207e34fba 127 * @return I2C supply voltage level (0=VLOGIC, 1=VDD)
garfieldsg 0:662207e34fba 128 */
garfieldsg 0:662207e34fba 129 uint8_t MPU6050::getAuxVDDIOLevel()
garfieldsg 0:662207e34fba 130 {
garfieldsg 0:662207e34fba 131 i2Cdev.readBit(devAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_PWR_MODE_BIT, buffer);
garfieldsg 0:662207e34fba 132 return buffer[0];
garfieldsg 0:662207e34fba 133 }
garfieldsg 0:662207e34fba 134 /** Set the auxiliary I2C supply voltage level.
garfieldsg 0:662207e34fba 135 * When set to 1, the auxiliary I2C bus high logic level is VDD. When cleared to
garfieldsg 0:662207e34fba 136 * 0, the auxiliary I2C bus high logic level is VLOGIC. This does not apply to
garfieldsg 0:662207e34fba 137 * the MPU-6000, which does not have a VLOGIC pin.
garfieldsg 0:662207e34fba 138 * @param level I2C supply voltage level (0=VLOGIC, 1=VDD)
garfieldsg 0:662207e34fba 139 */
garfieldsg 0:662207e34fba 140 void MPU6050::setAuxVDDIOLevel(uint8_t level)
garfieldsg 0:662207e34fba 141 {
garfieldsg 0:662207e34fba 142 i2Cdev.writeBit(devAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_PWR_MODE_BIT, level);
garfieldsg 0:662207e34fba 143 }
garfieldsg 0:662207e34fba 144
garfieldsg 0:662207e34fba 145 // SMPLRT_DIV register
garfieldsg 0:662207e34fba 146
garfieldsg 0:662207e34fba 147 /** Get gyroscope output rate divider.
garfieldsg 0:662207e34fba 148 * The sensor register output, FIFO output, DMP sampling, Motion detection, Zero
garfieldsg 0:662207e34fba 149 * Motion detection, and Free Fall detection are all based on the Sample Rate.
garfieldsg 0:662207e34fba 150 * The Sample Rate is generated by dividing the gyroscope output rate by
garfieldsg 0:662207e34fba 151 * SMPLRT_DIV:
garfieldsg 0:662207e34fba 152 *
garfieldsg 0:662207e34fba 153 * Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV)
garfieldsg 0:662207e34fba 154 *
garfieldsg 0:662207e34fba 155 * where Gyroscope Output Rate = 8kHz when the DLPF is disabled (DLPF_CFG = 0 or
garfieldsg 0:662207e34fba 156 * 7), and 1kHz when the DLPF is enabled (see Register 26).
garfieldsg 0:662207e34fba 157 *
garfieldsg 0:662207e34fba 158 * Note: The accelerometer output rate is 1kHz. This means that for a Sample
garfieldsg 0:662207e34fba 159 * Rate greater than 1kHz, the same accelerometer sample may be output to the
garfieldsg 0:662207e34fba 160 * FIFO, DMP, and sensor registers more than once.
garfieldsg 0:662207e34fba 161 *
garfieldsg 0:662207e34fba 162 * For a diagram of the gyroscope and accelerometer signal paths, see Section 8
garfieldsg 0:662207e34fba 163 * of the MPU-6000/MPU-6050 Product Specification document.
garfieldsg 0:662207e34fba 164 *
garfieldsg 0:662207e34fba 165 * @return Current sample rate
garfieldsg 0:662207e34fba 166 * @see MPU6050_RA_SMPLRT_DIV
garfieldsg 0:662207e34fba 167 */
garfieldsg 0:662207e34fba 168 uint8_t MPU6050::getRate()
garfieldsg 0:662207e34fba 169 {
garfieldsg 0:662207e34fba 170 i2Cdev.readByte(devAddr, MPU6050_RA_SMPLRT_DIV, buffer);
garfieldsg 0:662207e34fba 171 return buffer[0];
garfieldsg 0:662207e34fba 172 }
garfieldsg 0:662207e34fba 173 /** Set gyroscope sample rate divider.
garfieldsg 0:662207e34fba 174 * @param rate New sample rate divider
garfieldsg 0:662207e34fba 175 * @see getRate()
garfieldsg 0:662207e34fba 176 * @see MPU6050_RA_SMPLRT_DIV
garfieldsg 0:662207e34fba 177 */
garfieldsg 0:662207e34fba 178 void MPU6050::setRate(uint8_t rate)
garfieldsg 0:662207e34fba 179 {
garfieldsg 0:662207e34fba 180 i2Cdev.writeByte(devAddr, MPU6050_RA_SMPLRT_DIV, rate);
garfieldsg 0:662207e34fba 181 }
garfieldsg 0:662207e34fba 182
pommzorz 6:40ac13ef7290 183
garfieldsg 0:662207e34fba 184 // CONFIG register
garfieldsg 0:662207e34fba 185
garfieldsg 0:662207e34fba 186 /** Get external FSYNC configuration.
garfieldsg 0:662207e34fba 187 * Configures the external Frame Synchronization (FSYNC) pin sampling. An
garfieldsg 0:662207e34fba 188 * external signal connected to the FSYNC pin can be sampled by configuring
garfieldsg 0:662207e34fba 189 * EXT_SYNC_SET. Signal changes to the FSYNC pin are latched so that short
garfieldsg 0:662207e34fba 190 * strobes may be captured. The latched FSYNC signal will be sampled at the
garfieldsg 0:662207e34fba 191 * Sampling Rate, as defined in register 25. After sampling, the latch will
garfieldsg 0:662207e34fba 192 * reset to the current FSYNC signal state.
garfieldsg 0:662207e34fba 193 *
garfieldsg 0:662207e34fba 194 * The sampled value will be reported in place of the least significant bit in
garfieldsg 0:662207e34fba 195 * a sensor data register determined by the value of EXT_SYNC_SET according to
garfieldsg 0:662207e34fba 196 * the following table.
garfieldsg 0:662207e34fba 197 *
garfieldsg 0:662207e34fba 198 * <pre>
garfieldsg 0:662207e34fba 199 * EXT_SYNC_SET | FSYNC Bit Location
garfieldsg 0:662207e34fba 200 * -------------+-------------------
garfieldsg 0:662207e34fba 201 * 0 | Input disabled
garfieldsg 0:662207e34fba 202 * 1 | TEMP_OUT_L[0]
garfieldsg 0:662207e34fba 203 * 2 | GYRO_XOUT_L[0]
garfieldsg 0:662207e34fba 204 * 3 | GYRO_YOUT_L[0]
garfieldsg 0:662207e34fba 205 * 4 | GYRO_ZOUT_L[0]
garfieldsg 0:662207e34fba 206 * 5 | ACCEL_XOUT_L[0]
garfieldsg 0:662207e34fba 207 * 6 | ACCEL_YOUT_L[0]
garfieldsg 0:662207e34fba 208 * 7 | ACCEL_ZOUT_L[0]
garfieldsg 0:662207e34fba 209 * </pre>
garfieldsg 0:662207e34fba 210 *
garfieldsg 0:662207e34fba 211 * @return FSYNC configuration value
garfieldsg 0:662207e34fba 212 */
garfieldsg 0:662207e34fba 213 uint8_t MPU6050::getExternalFrameSync()
garfieldsg 0:662207e34fba 214 {
garfieldsg 0:662207e34fba 215 i2Cdev.readBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_EXT_SYNC_SET_BIT, MPU6050_CFG_EXT_SYNC_SET_LENGTH, buffer);
garfieldsg 0:662207e34fba 216 return buffer[0];
garfieldsg 0:662207e34fba 217 }
garfieldsg 0:662207e34fba 218 /** Set external FSYNC configuration.
garfieldsg 0:662207e34fba 219 * @see getExternalFrameSync()
garfieldsg 0:662207e34fba 220 * @see MPU6050_RA_CONFIG
garfieldsg 0:662207e34fba 221 * @param sync New FSYNC configuration value
garfieldsg 0:662207e34fba 222 */
garfieldsg 0:662207e34fba 223 void MPU6050::setExternalFrameSync(uint8_t sync)
garfieldsg 0:662207e34fba 224 {
garfieldsg 0:662207e34fba 225 i2Cdev.writeBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_EXT_SYNC_SET_BIT, MPU6050_CFG_EXT_SYNC_SET_LENGTH, sync);
garfieldsg 0:662207e34fba 226 }
garfieldsg 0:662207e34fba 227 /** Get digital low-pass filter configuration.
garfieldsg 0:662207e34fba 228 * The DLPF_CFG parameter sets the digital low pass filter configuration. It
garfieldsg 0:662207e34fba 229 * also determines the internal sampling rate used by the device as shown in
garfieldsg 0:662207e34fba 230 * the table below.
garfieldsg 0:662207e34fba 231 *
garfieldsg 0:662207e34fba 232 * Note: The accelerometer output rate is 1kHz. This means that for a Sample
garfieldsg 0:662207e34fba 233 * Rate greater than 1kHz, the same accelerometer sample may be output to the
garfieldsg 0:662207e34fba 234 * FIFO, DMP, and sensor registers more than once.
garfieldsg 0:662207e34fba 235 *
garfieldsg 0:662207e34fba 236 * <pre>
garfieldsg 0:662207e34fba 237 * | ACCELEROMETER | GYROSCOPE
garfieldsg 0:662207e34fba 238 * DLPF_CFG | Bandwidth | Delay | Bandwidth | Delay | Sample Rate
garfieldsg 0:662207e34fba 239 * ---------+-----------+--------+-----------+--------+-------------
garfieldsg 0:662207e34fba 240 * 0 | 260Hz | 0ms | 256Hz | 0.98ms | 8kHz
garfieldsg 0:662207e34fba 241 * 1 | 184Hz | 2.0ms | 188Hz | 1.9ms | 1kHz
garfieldsg 0:662207e34fba 242 * 2 | 94Hz | 3.0ms | 98Hz | 2.8ms | 1kHz
garfieldsg 0:662207e34fba 243 * 3 | 44Hz | 4.9ms | 42Hz | 4.8ms | 1kHz
garfieldsg 0:662207e34fba 244 * 4 | 21Hz | 8.5ms | 20Hz | 8.3ms | 1kHz
garfieldsg 0:662207e34fba 245 * 5 | 10Hz | 13.8ms | 10Hz | 13.4ms | 1kHz
garfieldsg 0:662207e34fba 246 * 6 | 5Hz | 19.0ms | 5Hz | 18.6ms | 1kHz
garfieldsg 0:662207e34fba 247 * 7 | -- Reserved -- | -- Reserved -- | Reserved
garfieldsg 0:662207e34fba 248 * </pre>
garfieldsg 0:662207e34fba 249 *
garfieldsg 0:662207e34fba 250 * @return DLFP configuration
garfieldsg 0:662207e34fba 251 * @see MPU6050_RA_CONFIG
garfieldsg 0:662207e34fba 252 * @see MPU6050_CFG_DLPF_CFG_BIT
garfieldsg 0:662207e34fba 253 * @see MPU6050_CFG_DLPF_CFG_LENGTH
garfieldsg 0:662207e34fba 254 */
garfieldsg 0:662207e34fba 255 uint8_t MPU6050::getDLPFMode()
garfieldsg 0:662207e34fba 256 {
garfieldsg 0:662207e34fba 257 i2Cdev.readBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_DLPF_CFG_BIT, MPU6050_CFG_DLPF_CFG_LENGTH, buffer);
garfieldsg 0:662207e34fba 258 return buffer[0];
garfieldsg 0:662207e34fba 259 }
garfieldsg 0:662207e34fba 260 /** Set digital low-pass filter configuration.
garfieldsg 0:662207e34fba 261 * @param mode New DLFP configuration setting
garfieldsg 0:662207e34fba 262 * @see getDLPFBandwidth()
garfieldsg 0:662207e34fba 263 * @see MPU6050_DLPF_BW_256
garfieldsg 0:662207e34fba 264 * @see MPU6050_RA_CONFIG
garfieldsg 0:662207e34fba 265 * @see MPU6050_CFG_DLPF_CFG_BIT
garfieldsg 0:662207e34fba 266 * @see MPU6050_CFG_DLPF_CFG_LENGTH
garfieldsg 0:662207e34fba 267 */
garfieldsg 0:662207e34fba 268 void MPU6050::setDLPFMode(uint8_t mode)
garfieldsg 0:662207e34fba 269 {
garfieldsg 0:662207e34fba 270 i2Cdev.writeBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_DLPF_CFG_BIT, MPU6050_CFG_DLPF_CFG_LENGTH, mode);
garfieldsg 0:662207e34fba 271 }
garfieldsg 0:662207e34fba 272
garfieldsg 0:662207e34fba 273 // GYRO_CONFIG register
garfieldsg 0:662207e34fba 274
garfieldsg 0:662207e34fba 275 /** Get full-scale gyroscope range.
garfieldsg 0:662207e34fba 276 * The FS_SEL parameter allows setting the full-scale range of the gyro sensors,
garfieldsg 0:662207e34fba 277 * as described in the table below.
garfieldsg 0:662207e34fba 278 *
garfieldsg 0:662207e34fba 279 * <pre>
garfieldsg 0:662207e34fba 280 * 0 = +/- 250 degrees/sec
garfieldsg 0:662207e34fba 281 * 1 = +/- 500 degrees/sec
garfieldsg 0:662207e34fba 282 * 2 = +/- 1000 degrees/sec
garfieldsg 0:662207e34fba 283 * 3 = +/- 2000 degrees/sec
garfieldsg 0:662207e34fba 284 * </pre>
garfieldsg 0:662207e34fba 285 *
garfieldsg 0:662207e34fba 286 * @return Current full-scale gyroscope range setting
garfieldsg 0:662207e34fba 287 * @see MPU6050_GYRO_FS_250
garfieldsg 0:662207e34fba 288 * @see MPU6050_RA_GYRO_CONFIG
garfieldsg 0:662207e34fba 289 * @see MPU6050_GCONFIG_FS_SEL_BIT
garfieldsg 0:662207e34fba 290 * @see MPU6050_GCONFIG_FS_SEL_LENGTH
garfieldsg 0:662207e34fba 291 */
garfieldsg 0:662207e34fba 292 uint8_t MPU6050::getFullScaleGyroRange()
garfieldsg 0:662207e34fba 293 {
garfieldsg 0:662207e34fba 294 i2Cdev.readBits(devAddr, MPU6050_RA_GYRO_CONFIG, MPU6050_GCONFIG_FS_SEL_BIT, MPU6050_GCONFIG_FS_SEL_LENGTH, buffer);
garfieldsg 0:662207e34fba 295 return buffer[0];
garfieldsg 0:662207e34fba 296 }
garfieldsg 0:662207e34fba 297 /** Set full-scale gyroscope range.
garfieldsg 0:662207e34fba 298 * @param range New full-scale gyroscope range value
garfieldsg 0:662207e34fba 299 * @see getFullScaleRange()
garfieldsg 0:662207e34fba 300 * @see MPU6050_GYRO_FS_250
garfieldsg 0:662207e34fba 301 * @see MPU6050_RA_GYRO_CONFIG
garfieldsg 0:662207e34fba 302 * @see MPU6050_GCONFIG_FS_SEL_BIT
garfieldsg 0:662207e34fba 303 * @see MPU6050_GCONFIG_FS_SEL_LENGTH
garfieldsg 0:662207e34fba 304 */
garfieldsg 0:662207e34fba 305 void MPU6050::setFullScaleGyroRange(uint8_t range)
garfieldsg 0:662207e34fba 306 {
garfieldsg 0:662207e34fba 307 i2Cdev.writeBits(devAddr, MPU6050_RA_GYRO_CONFIG, MPU6050_GCONFIG_FS_SEL_BIT, MPU6050_GCONFIG_FS_SEL_LENGTH, range);
garfieldsg 0:662207e34fba 308 }
garfieldsg 0:662207e34fba 309
garfieldsg 0:662207e34fba 310 // ACCEL_CONFIG register
garfieldsg 0:662207e34fba 311
garfieldsg 0:662207e34fba 312 /** Get self-test enabled setting for accelerometer X axis.
garfieldsg 0:662207e34fba 313 * @return Self-test enabled value
garfieldsg 0:662207e34fba 314 * @see MPU6050_RA_ACCEL_CONFIG
garfieldsg 0:662207e34fba 315 */
garfieldsg 0:662207e34fba 316 bool MPU6050::getAccelXSelfTest()
garfieldsg 0:662207e34fba 317 {
garfieldsg 0:662207e34fba 318 i2Cdev.readBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_XA_ST_BIT, buffer);
garfieldsg 0:662207e34fba 319 return buffer[0];
garfieldsg 0:662207e34fba 320 }
garfieldsg 0:662207e34fba 321 /** Get self-test enabled setting for accelerometer X axis.
garfieldsg 0:662207e34fba 322 * @param enabled Self-test enabled value
garfieldsg 0:662207e34fba 323 * @see MPU6050_RA_ACCEL_CONFIG
garfieldsg 0:662207e34fba 324 */
garfieldsg 0:662207e34fba 325 void MPU6050::setAccelXSelfTest(bool enabled)
garfieldsg 0:662207e34fba 326 {
garfieldsg 0:662207e34fba 327 i2Cdev.writeBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_XA_ST_BIT, enabled);
garfieldsg 0:662207e34fba 328 }
garfieldsg 0:662207e34fba 329 /** Get self-test enabled value for accelerometer Y axis.
garfieldsg 0:662207e34fba 330 * @return Self-test enabled value
garfieldsg 0:662207e34fba 331 * @see MPU6050_RA_ACCEL_CONFIG
garfieldsg 0:662207e34fba 332 */
garfieldsg 0:662207e34fba 333 bool MPU6050::getAccelYSelfTest()
garfieldsg 0:662207e34fba 334 {
garfieldsg 0:662207e34fba 335 i2Cdev.readBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_YA_ST_BIT, buffer);
garfieldsg 0:662207e34fba 336 return buffer[0];
garfieldsg 0:662207e34fba 337 }
garfieldsg 0:662207e34fba 338 /** Get self-test enabled value for accelerometer Y axis.
garfieldsg 0:662207e34fba 339 * @param enabled Self-test enabled value
garfieldsg 0:662207e34fba 340 * @see MPU6050_RA_ACCEL_CONFIG
garfieldsg 0:662207e34fba 341 */
garfieldsg 0:662207e34fba 342 void MPU6050::setAccelYSelfTest(bool enabled)
garfieldsg 0:662207e34fba 343 {
garfieldsg 0:662207e34fba 344 i2Cdev.writeBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_YA_ST_BIT, enabled);
garfieldsg 0:662207e34fba 345 }
garfieldsg 0:662207e34fba 346 /** Get self-test enabled value for accelerometer Z axis.
garfieldsg 0:662207e34fba 347 * @return Self-test enabled value
garfieldsg 0:662207e34fba 348 * @see MPU6050_RA_ACCEL_CONFIG
garfieldsg 0:662207e34fba 349 */
garfieldsg 0:662207e34fba 350 bool MPU6050::getAccelZSelfTest()
garfieldsg 0:662207e34fba 351 {
garfieldsg 0:662207e34fba 352 i2Cdev.readBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ZA_ST_BIT, buffer);
garfieldsg 0:662207e34fba 353 return buffer[0];
garfieldsg 0:662207e34fba 354 }
garfieldsg 0:662207e34fba 355 /** Set self-test enabled value for accelerometer Z axis.
garfieldsg 0:662207e34fba 356 * @param enabled Self-test enabled value
garfieldsg 0:662207e34fba 357 * @see MPU6050_RA_ACCEL_CONFIG
garfieldsg 0:662207e34fba 358 */
garfieldsg 0:662207e34fba 359 void MPU6050::setAccelZSelfTest(bool enabled)
garfieldsg 0:662207e34fba 360 {
garfieldsg 0:662207e34fba 361 i2Cdev.writeBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ZA_ST_BIT, enabled);
garfieldsg 0:662207e34fba 362 }
garfieldsg 0:662207e34fba 363 /** Get full-scale accelerometer range.
garfieldsg 0:662207e34fba 364 * The FS_SEL parameter allows setting the full-scale range of the accelerometer
garfieldsg 0:662207e34fba 365 * sensors, as described in the table below.
garfieldsg 0:662207e34fba 366 *
garfieldsg 0:662207e34fba 367 * <pre>
garfieldsg 0:662207e34fba 368 * 0 = +/- 2g
garfieldsg 0:662207e34fba 369 * 1 = +/- 4g
garfieldsg 0:662207e34fba 370 * 2 = +/- 8g
garfieldsg 0:662207e34fba 371 * 3 = +/- 16g
garfieldsg 0:662207e34fba 372 * </pre>
garfieldsg 0:662207e34fba 373 *
garfieldsg 0:662207e34fba 374 * @return Current full-scale accelerometer range setting
garfieldsg 0:662207e34fba 375 * @see MPU6050_ACCEL_FS_2
garfieldsg 0:662207e34fba 376 * @see MPU6050_RA_ACCEL_CONFIG
garfieldsg 0:662207e34fba 377 * @see MPU6050_ACONFIG_AFS_SEL_BIT
garfieldsg 0:662207e34fba 378 * @see MPU6050_ACONFIG_AFS_SEL_LENGTH
garfieldsg 0:662207e34fba 379 */
garfieldsg 0:662207e34fba 380 uint8_t MPU6050::getFullScaleAccelRange()
garfieldsg 0:662207e34fba 381 {
garfieldsg 0:662207e34fba 382 i2Cdev.readBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_AFS_SEL_BIT, MPU6050_ACONFIG_AFS_SEL_LENGTH, buffer);
garfieldsg 0:662207e34fba 383 return buffer[0];
garfieldsg 0:662207e34fba 384 }
garfieldsg 0:662207e34fba 385 /** Set full-scale accelerometer range.
garfieldsg 0:662207e34fba 386 * @param range New full-scale accelerometer range setting
garfieldsg 0:662207e34fba 387 * @see getFullScaleAccelRange()
garfieldsg 0:662207e34fba 388 */
garfieldsg 0:662207e34fba 389 void MPU6050::setFullScaleAccelRange(uint8_t range)
garfieldsg 0:662207e34fba 390 {
garfieldsg 0:662207e34fba 391 i2Cdev.writeBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_AFS_SEL_BIT, MPU6050_ACONFIG_AFS_SEL_LENGTH, range);
garfieldsg 0:662207e34fba 392 }
garfieldsg 0:662207e34fba 393 /** Get the high-pass filter configuration.
garfieldsg 0:662207e34fba 394 * The DHPF is a filter module in the path leading to motion detectors (Free
garfieldsg 0:662207e34fba 395 * Fall, Motion threshold, and Zero Motion). The high pass filter output is not
garfieldsg 0:662207e34fba 396 * available to the data registers (see Figure in Section 8 of the MPU-6000/
garfieldsg 0:662207e34fba 397 * MPU-6050 Product Specification document).
garfieldsg 0:662207e34fba 398 *
garfieldsg 0:662207e34fba 399 * The high pass filter has three modes:
garfieldsg 0:662207e34fba 400 *
garfieldsg 0:662207e34fba 401 * <pre>
garfieldsg 0:662207e34fba 402 * Reset: The filter output settles to zero within one sample. This
garfieldsg 0:662207e34fba 403 * effectively disables the high pass filter. This mode may be toggled
garfieldsg 0:662207e34fba 404 * to quickly settle the filter.
garfieldsg 0:662207e34fba 405 *
garfieldsg 0:662207e34fba 406 * On: The high pass filter will pass signals above the cut off frequency.
garfieldsg 0:662207e34fba 407 *
garfieldsg 0:662207e34fba 408 * Hold: When triggered, the filter holds the present sample. The filter
garfieldsg 0:662207e34fba 409 * output will be the difference between the input sample and the held
garfieldsg 0:662207e34fba 410 * sample.
garfieldsg 0:662207e34fba 411 * </pre>
garfieldsg 0:662207e34fba 412 *
garfieldsg 0:662207e34fba 413 * <pre>
garfieldsg 0:662207e34fba 414 * ACCEL_HPF | Filter Mode | Cut-off Frequency
garfieldsg 0:662207e34fba 415 * ----------+-------------+------------------
garfieldsg 0:662207e34fba 416 * 0 | Reset | None
garfieldsg 0:662207e34fba 417 * 1 | On | 5Hz
garfieldsg 0:662207e34fba 418 * 2 | On | 2.5Hz
garfieldsg 0:662207e34fba 419 * 3 | On | 1.25Hz
garfieldsg 0:662207e34fba 420 * 4 | On | 0.63Hz
garfieldsg 0:662207e34fba 421 * 7 | Hold | None
garfieldsg 0:662207e34fba 422 * </pre>
garfieldsg 0:662207e34fba 423 *
garfieldsg 0:662207e34fba 424 * @return Current high-pass filter configuration
garfieldsg 0:662207e34fba 425 * @see MPU6050_DHPF_RESET
garfieldsg 0:662207e34fba 426 * @see MPU6050_RA_ACCEL_CONFIG
garfieldsg 0:662207e34fba 427 */
garfieldsg 0:662207e34fba 428 uint8_t MPU6050::getDHPFMode()
garfieldsg 0:662207e34fba 429 {
garfieldsg 0:662207e34fba 430 i2Cdev.readBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ACCEL_HPF_BIT, MPU6050_ACONFIG_ACCEL_HPF_LENGTH, buffer);
garfieldsg 0:662207e34fba 431 return buffer[0];
garfieldsg 0:662207e34fba 432 }
garfieldsg 0:662207e34fba 433 /** Set the high-pass filter configuration.
garfieldsg 0:662207e34fba 434 * @param bandwidth New high-pass filter configuration
garfieldsg 0:662207e34fba 435 * @see setDHPFMode()
garfieldsg 0:662207e34fba 436 * @see MPU6050_DHPF_RESET
garfieldsg 0:662207e34fba 437 * @see MPU6050_RA_ACCEL_CONFIG
garfieldsg 0:662207e34fba 438 */
garfieldsg 0:662207e34fba 439 void MPU6050::setDHPFMode(uint8_t bandwidth)
garfieldsg 0:662207e34fba 440 {
garfieldsg 0:662207e34fba 441 i2Cdev.writeBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ACCEL_HPF_BIT, MPU6050_ACONFIG_ACCEL_HPF_LENGTH, bandwidth);
garfieldsg 0:662207e34fba 442 }
garfieldsg 0:662207e34fba 443
garfieldsg 0:662207e34fba 444 // FF_THR register
garfieldsg 0:662207e34fba 445
garfieldsg 0:662207e34fba 446 /** Get free-fall event acceleration threshold.
garfieldsg 0:662207e34fba 447 * This register configures the detection threshold for Free Fall event
garfieldsg 0:662207e34fba 448 * detection. The unit of FF_THR is 1LSB = 2mg. Free Fall is detected when the
garfieldsg 0:662207e34fba 449 * absolute value of the accelerometer measurements for the three axes are each
garfieldsg 0:662207e34fba 450 * less than the detection threshold. This condition increments the Free Fall
garfieldsg 0:662207e34fba 451 * duration counter (Register 30). The Free Fall interrupt is triggered when the
garfieldsg 0:662207e34fba 452 * Free Fall duration counter reaches the time specified in FF_DUR.
garfieldsg 0:662207e34fba 453 *
garfieldsg 0:662207e34fba 454 * For more details on the Free Fall detection interrupt, see Section 8.2 of the
garfieldsg 0:662207e34fba 455 * MPU-6000/MPU-6050 Product Specification document as well as Registers 56 and
garfieldsg 0:662207e34fba 456 * 58 of this document.
garfieldsg 0:662207e34fba 457 *
garfieldsg 0:662207e34fba 458 * @return Current free-fall acceleration threshold value (LSB = 2mg)
garfieldsg 0:662207e34fba 459 * @see MPU6050_RA_FF_THR
garfieldsg 0:662207e34fba 460 */
garfieldsg 0:662207e34fba 461 uint8_t MPU6050::getFreefallDetectionThreshold()
garfieldsg 0:662207e34fba 462 {
garfieldsg 0:662207e34fba 463 i2Cdev.readByte(devAddr, MPU6050_RA_FF_THR, buffer);
garfieldsg 0:662207e34fba 464 return buffer[0];
garfieldsg 0:662207e34fba 465 }
garfieldsg 0:662207e34fba 466 /** Get free-fall event acceleration threshold.
garfieldsg 0:662207e34fba 467 * @param threshold New free-fall acceleration threshold value (LSB = 2mg)
garfieldsg 0:662207e34fba 468 * @see getFreefallDetectionThreshold()
garfieldsg 0:662207e34fba 469 * @see MPU6050_RA_FF_THR
garfieldsg 0:662207e34fba 470 */
garfieldsg 0:662207e34fba 471 void MPU6050::setFreefallDetectionThreshold(uint8_t threshold)
garfieldsg 0:662207e34fba 472 {
garfieldsg 0:662207e34fba 473 i2Cdev.writeByte(devAddr, MPU6050_RA_FF_THR, threshold);
garfieldsg 0:662207e34fba 474 }
garfieldsg 0:662207e34fba 475
garfieldsg 0:662207e34fba 476 // FF_DUR register
garfieldsg 0:662207e34fba 477
garfieldsg 0:662207e34fba 478 /** Get free-fall event duration threshold.
garfieldsg 0:662207e34fba 479 * This register configures the duration counter threshold for Free Fall event
garfieldsg 0:662207e34fba 480 * detection. The duration counter ticks at 1kHz, therefore FF_DUR has a unit
garfieldsg 0:662207e34fba 481 * of 1 LSB = 1 ms.
garfieldsg 0:662207e34fba 482 *
garfieldsg 0:662207e34fba 483 * The Free Fall duration counter increments while the absolute value of the
garfieldsg 0:662207e34fba 484 * accelerometer measurements are each less than the detection threshold
garfieldsg 0:662207e34fba 485 * (Register 29). The Free Fall interrupt is triggered when the Free Fall
garfieldsg 0:662207e34fba 486 * duration counter reaches the time specified in this register.
garfieldsg 0:662207e34fba 487 *
garfieldsg 0:662207e34fba 488 * For more details on the Free Fall detection interrupt, see Section 8.2 of
garfieldsg 0:662207e34fba 489 * the MPU-6000/MPU-6050 Product Specification document as well as Registers 56
garfieldsg 0:662207e34fba 490 * and 58 of this document.
garfieldsg 0:662207e34fba 491 *
garfieldsg 0:662207e34fba 492 * @return Current free-fall duration threshold value (LSB = 1ms)
garfieldsg 0:662207e34fba 493 * @see MPU6050_RA_FF_DUR
garfieldsg 0:662207e34fba 494 */
garfieldsg 0:662207e34fba 495 uint8_t MPU6050::getFreefallDetectionDuration()
garfieldsg 0:662207e34fba 496 {
garfieldsg 0:662207e34fba 497 i2Cdev.readByte(devAddr, MPU6050_RA_FF_DUR, buffer);
garfieldsg 0:662207e34fba 498 return buffer[0];
garfieldsg 0:662207e34fba 499 }
garfieldsg 0:662207e34fba 500 /** Get free-fall event duration threshold.
garfieldsg 0:662207e34fba 501 * @param duration New free-fall duration threshold value (LSB = 1ms)
garfieldsg 0:662207e34fba 502 * @see getFreefallDetectionDuration()
garfieldsg 0:662207e34fba 503 * @see MPU6050_RA_FF_DUR
garfieldsg 0:662207e34fba 504 */
garfieldsg 0:662207e34fba 505 void MPU6050::setFreefallDetectionDuration(uint8_t duration)
garfieldsg 0:662207e34fba 506 {
garfieldsg 0:662207e34fba 507 i2Cdev.writeByte(devAddr, MPU6050_RA_FF_DUR, duration);
garfieldsg 0:662207e34fba 508 }
garfieldsg 0:662207e34fba 509
garfieldsg 0:662207e34fba 510 // MOT_THR register
garfieldsg 0:662207e34fba 511
garfieldsg 0:662207e34fba 512 /** Get motion detection event acceleration threshold.
garfieldsg 0:662207e34fba 513 * This register configures the detection threshold for Motion interrupt
garfieldsg 0:662207e34fba 514 * generation. The unit of MOT_THR is 1LSB = 2mg. Motion is detected when the
garfieldsg 0:662207e34fba 515 * absolute value of any of the accelerometer measurements exceeds this Motion
garfieldsg 0:662207e34fba 516 * detection threshold. This condition increments the Motion detection duration
garfieldsg 0:662207e34fba 517 * counter (Register 32). The Motion detection interrupt is triggered when the
garfieldsg 0:662207e34fba 518 * Motion Detection counter reaches the time count specified in MOT_DUR
garfieldsg 0:662207e34fba 519 * (Register 32).
garfieldsg 0:662207e34fba 520 *
garfieldsg 0:662207e34fba 521 * The Motion interrupt will indicate the axis and polarity of detected motion
garfieldsg 0:662207e34fba 522 * in MOT_DETECT_STATUS (Register 97).
garfieldsg 0:662207e34fba 523 *
garfieldsg 0:662207e34fba 524 * For more details on the Motion detection interrupt, see Section 8.3 of the
garfieldsg 0:662207e34fba 525 * MPU-6000/MPU-6050 Product Specification document as well as Registers 56 and
garfieldsg 0:662207e34fba 526 * 58 of this document.
garfieldsg 0:662207e34fba 527 *
garfieldsg 0:662207e34fba 528 * @return Current motion detection acceleration threshold value (LSB = 2mg)
garfieldsg 0:662207e34fba 529 * @see MPU6050_RA_MOT_THR
garfieldsg 0:662207e34fba 530 */
garfieldsg 0:662207e34fba 531 uint8_t MPU6050::getMotionDetectionThreshold()
garfieldsg 0:662207e34fba 532 {
garfieldsg 0:662207e34fba 533 i2Cdev.readByte(devAddr, MPU6050_RA_MOT_THR, buffer);
garfieldsg 0:662207e34fba 534 return buffer[0];
garfieldsg 0:662207e34fba 535 }
garfieldsg 0:662207e34fba 536 /** Set free-fall event acceleration threshold.
garfieldsg 0:662207e34fba 537 * @param threshold New motion detection acceleration threshold value (LSB = 2mg)
garfieldsg 0:662207e34fba 538 * @see getMotionDetectionThreshold()
garfieldsg 0:662207e34fba 539 * @see MPU6050_RA_MOT_THR
garfieldsg 0:662207e34fba 540 */
garfieldsg 0:662207e34fba 541 void MPU6050::setMotionDetectionThreshold(uint8_t threshold)
garfieldsg 0:662207e34fba 542 {
garfieldsg 0:662207e34fba 543 i2Cdev.writeByte(devAddr, MPU6050_RA_MOT_THR, threshold);
garfieldsg 0:662207e34fba 544 }
garfieldsg 0:662207e34fba 545
garfieldsg 0:662207e34fba 546 // MOT_DUR register
garfieldsg 0:662207e34fba 547
garfieldsg 0:662207e34fba 548 /** Get motion detection event duration threshold.
garfieldsg 0:662207e34fba 549 * This register configures the duration counter threshold for Motion interrupt
garfieldsg 0:662207e34fba 550 * generation. The duration counter ticks at 1 kHz, therefore MOT_DUR has a unit
garfieldsg 0:662207e34fba 551 * of 1LSB = 1ms. The Motion detection duration counter increments when the
garfieldsg 0:662207e34fba 552 * absolute value of any of the accelerometer measurements exceeds the Motion
garfieldsg 0:662207e34fba 553 * detection threshold (Register 31). The Motion detection interrupt is
garfieldsg 0:662207e34fba 554 * triggered when the Motion detection counter reaches the time count specified
garfieldsg 0:662207e34fba 555 * in this register.
garfieldsg 0:662207e34fba 556 *
garfieldsg 0:662207e34fba 557 * For more details on the Motion detection interrupt, see Section 8.3 of the
garfieldsg 0:662207e34fba 558 * MPU-6000/MPU-6050 Product Specification document.
garfieldsg 0:662207e34fba 559 *
garfieldsg 0:662207e34fba 560 * @return Current motion detection duration threshold value (LSB = 1ms)
garfieldsg 0:662207e34fba 561 * @see MPU6050_RA_MOT_DUR
garfieldsg 0:662207e34fba 562 */
garfieldsg 0:662207e34fba 563 uint8_t MPU6050::getMotionDetectionDuration()
garfieldsg 0:662207e34fba 564 {
garfieldsg 0:662207e34fba 565 i2Cdev.readByte(devAddr, MPU6050_RA_MOT_DUR, buffer);
garfieldsg 0:662207e34fba 566 return buffer[0];
garfieldsg 0:662207e34fba 567 }
garfieldsg 0:662207e34fba 568 /** Set motion detection event duration threshold.
garfieldsg 0:662207e34fba 569 * @param duration New motion detection duration threshold value (LSB = 1ms)
garfieldsg 0:662207e34fba 570 * @see getMotionDetectionDuration()
garfieldsg 0:662207e34fba 571 * @see MPU6050_RA_MOT_DUR
garfieldsg 0:662207e34fba 572 */
garfieldsg 0:662207e34fba 573 void MPU6050::setMotionDetectionDuration(uint8_t duration)
garfieldsg 0:662207e34fba 574 {
garfieldsg 0:662207e34fba 575 i2Cdev.writeByte(devAddr, MPU6050_RA_MOT_DUR, duration);
garfieldsg 0:662207e34fba 576 }
garfieldsg 0:662207e34fba 577
garfieldsg 0:662207e34fba 578 // ZRMOT_THR register
garfieldsg 0:662207e34fba 579
garfieldsg 0:662207e34fba 580 /** Get zero motion detection event acceleration threshold.
garfieldsg 0:662207e34fba 581 * This register configures the detection threshold for Zero Motion interrupt
garfieldsg 0:662207e34fba 582 * generation. The unit of ZRMOT_THR is 1LSB = 2mg. Zero Motion is detected when
garfieldsg 0:662207e34fba 583 * the absolute value of the accelerometer measurements for the 3 axes are each
garfieldsg 0:662207e34fba 584 * less than the detection threshold. This condition increments the Zero Motion
garfieldsg 0:662207e34fba 585 * duration counter (Register 34). The Zero Motion interrupt is triggered when
garfieldsg 0:662207e34fba 586 * the Zero Motion duration counter reaches the time count specified in
garfieldsg 0:662207e34fba 587 * ZRMOT_DUR (Register 34).
garfieldsg 0:662207e34fba 588 *
garfieldsg 0:662207e34fba 589 * Unlike Free Fall or Motion detection, Zero Motion detection triggers an
garfieldsg 0:662207e34fba 590 * interrupt both when Zero Motion is first detected and when Zero Motion is no
garfieldsg 0:662207e34fba 591 * longer detected.
garfieldsg 0:662207e34fba 592 *
garfieldsg 0:662207e34fba 593 * When a zero motion event is detected, a Zero Motion Status will be indicated
garfieldsg 0:662207e34fba 594 * in the MOT_DETECT_STATUS register (Register 97). When a motion-to-zero-motion
garfieldsg 0:662207e34fba 595 * condition is detected, the status bit is set to 1. When a zero-motion-to-
garfieldsg 0:662207e34fba 596 * motion condition is detected, the status bit is set to 0.
garfieldsg 0:662207e34fba 597 *
garfieldsg 0:662207e34fba 598 * For more details on the Zero Motion detection interrupt, see Section 8.4 of
garfieldsg 0:662207e34fba 599 * the MPU-6000/MPU-6050 Product Specification document as well as Registers 56
garfieldsg 0:662207e34fba 600 * and 58 of this document.
garfieldsg 0:662207e34fba 601 *
garfieldsg 0:662207e34fba 602 * @return Current zero motion detection acceleration threshold value (LSB = 2mg)
garfieldsg 0:662207e34fba 603 * @see MPU6050_RA_ZRMOT_THR
garfieldsg 0:662207e34fba 604 */
garfieldsg 0:662207e34fba 605 uint8_t MPU6050::getZeroMotionDetectionThreshold()
garfieldsg 0:662207e34fba 606 {
garfieldsg 0:662207e34fba 607 i2Cdev.readByte(devAddr, MPU6050_RA_ZRMOT_THR, buffer);
garfieldsg 0:662207e34fba 608 return buffer[0];
garfieldsg 0:662207e34fba 609 }
garfieldsg 0:662207e34fba 610 /** Set zero motion detection event acceleration threshold.
garfieldsg 0:662207e34fba 611 * @param threshold New zero motion detection acceleration threshold value (LSB = 2mg)
garfieldsg 0:662207e34fba 612 * @see getZeroMotionDetectionThreshold()
garfieldsg 0:662207e34fba 613 * @see MPU6050_RA_ZRMOT_THR
garfieldsg 0:662207e34fba 614 */
garfieldsg 0:662207e34fba 615 void MPU6050::setZeroMotionDetectionThreshold(uint8_t threshold)
garfieldsg 0:662207e34fba 616 {
garfieldsg 0:662207e34fba 617 i2Cdev.writeByte(devAddr, MPU6050_RA_ZRMOT_THR, threshold);
garfieldsg 0:662207e34fba 618 }
garfieldsg 0:662207e34fba 619
garfieldsg 0:662207e34fba 620 // ZRMOT_DUR register
garfieldsg 0:662207e34fba 621
garfieldsg 0:662207e34fba 622 /** Get zero motion detection event duration threshold.
garfieldsg 0:662207e34fba 623 * This register configures the duration counter threshold for Zero Motion
garfieldsg 0:662207e34fba 624 * interrupt generation. The duration counter ticks at 16 Hz, therefore
garfieldsg 0:662207e34fba 625 * ZRMOT_DUR has a unit of 1 LSB = 64 ms. The Zero Motion duration counter
garfieldsg 0:662207e34fba 626 * increments while the absolute value of the accelerometer measurements are
garfieldsg 0:662207e34fba 627 * each less than the detection threshold (Register 33). The Zero Motion
garfieldsg 0:662207e34fba 628 * interrupt is triggered when the Zero Motion duration counter reaches the time
garfieldsg 0:662207e34fba 629 * count specified in this register.
garfieldsg 0:662207e34fba 630 *
garfieldsg 0:662207e34fba 631 * For more details on the Zero Motion detection interrupt, see Section 8.4 of
garfieldsg 0:662207e34fba 632 * the MPU-6000/MPU-6050 Product Specification document, as well as Registers 56
garfieldsg 0:662207e34fba 633 * and 58 of this document.
garfieldsg 0:662207e34fba 634 *
garfieldsg 0:662207e34fba 635 * @return Current zero motion detection duration threshold value (LSB = 64ms)
garfieldsg 0:662207e34fba 636 * @see MPU6050_RA_ZRMOT_DUR
garfieldsg 0:662207e34fba 637 */
garfieldsg 0:662207e34fba 638 uint8_t MPU6050::getZeroMotionDetectionDuration()
garfieldsg 0:662207e34fba 639 {
garfieldsg 0:662207e34fba 640 i2Cdev.readByte(devAddr, MPU6050_RA_ZRMOT_DUR, buffer);
garfieldsg 0:662207e34fba 641 return buffer[0];
garfieldsg 0:662207e34fba 642 }
garfieldsg 0:662207e34fba 643 /** Set zero motion detection event duration threshold.
garfieldsg 0:662207e34fba 644 * @param duration New zero motion detection duration threshold value (LSB = 1ms)
garfieldsg 0:662207e34fba 645 * @see getZeroMotionDetectionDuration()
garfieldsg 0:662207e34fba 646 * @see MPU6050_RA_ZRMOT_DUR
garfieldsg 0:662207e34fba 647 */
garfieldsg 0:662207e34fba 648 void MPU6050::setZeroMotionDetectionDuration(uint8_t duration)
garfieldsg 0:662207e34fba 649 {
garfieldsg 0:662207e34fba 650 i2Cdev.writeByte(devAddr, MPU6050_RA_ZRMOT_DUR, duration);
garfieldsg 0:662207e34fba 651 }
garfieldsg 0:662207e34fba 652
garfieldsg 0:662207e34fba 653 // FIFO_EN register
garfieldsg 0:662207e34fba 654
garfieldsg 0:662207e34fba 655 /** Get temperature FIFO enabled value.
garfieldsg 0:662207e34fba 656 * When set to 1, this bit enables TEMP_OUT_H and TEMP_OUT_L (Registers 65 and
garfieldsg 0:662207e34fba 657 * 66) to be written into the FIFO buffer.
garfieldsg 0:662207e34fba 658 * @return Current temperature FIFO enabled value
garfieldsg 0:662207e34fba 659 * @see MPU6050_RA_FIFO_EN
garfieldsg 0:662207e34fba 660 */
garfieldsg 0:662207e34fba 661 bool MPU6050::getTempFIFOEnabled()
garfieldsg 0:662207e34fba 662 {
garfieldsg 0:662207e34fba 663 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_TEMP_FIFO_EN_BIT, buffer);
garfieldsg 0:662207e34fba 664 return buffer[0];
garfieldsg 0:662207e34fba 665 }
garfieldsg 0:662207e34fba 666 /** Set temperature FIFO enabled value.
garfieldsg 0:662207e34fba 667 * @param enabled New temperature FIFO enabled value
garfieldsg 0:662207e34fba 668 * @see getTempFIFOEnabled()
garfieldsg 0:662207e34fba 669 * @see MPU6050_RA_FIFO_EN
garfieldsg 0:662207e34fba 670 */
garfieldsg 0:662207e34fba 671 void MPU6050::setTempFIFOEnabled(bool enabled)
garfieldsg 0:662207e34fba 672 {
garfieldsg 0:662207e34fba 673 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_TEMP_FIFO_EN_BIT, enabled);
garfieldsg 0:662207e34fba 674 }
garfieldsg 0:662207e34fba 675 /** Get gyroscope X-axis FIFO enabled value.
garfieldsg 0:662207e34fba 676 * When set to 1, this bit enables GYRO_XOUT_H and GYRO_XOUT_L (Registers 67 and
garfieldsg 0:662207e34fba 677 * 68) to be written into the FIFO buffer.
garfieldsg 0:662207e34fba 678 * @return Current gyroscope X-axis FIFO enabled value
garfieldsg 0:662207e34fba 679 * @see MPU6050_RA_FIFO_EN
garfieldsg 0:662207e34fba 680 */
garfieldsg 0:662207e34fba 681 bool MPU6050::getXGyroFIFOEnabled()
garfieldsg 0:662207e34fba 682 {
garfieldsg 0:662207e34fba 683 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_XG_FIFO_EN_BIT, buffer);
garfieldsg 0:662207e34fba 684 return buffer[0];
garfieldsg 0:662207e34fba 685 }
garfieldsg 0:662207e34fba 686 /** Set gyroscope X-axis FIFO enabled value.
garfieldsg 0:662207e34fba 687 * @param enabled New gyroscope X-axis FIFO enabled value
garfieldsg 0:662207e34fba 688 * @see getXGyroFIFOEnabled()
garfieldsg 0:662207e34fba 689 * @see MPU6050_RA_FIFO_EN
garfieldsg 0:662207e34fba 690 */
garfieldsg 0:662207e34fba 691 void MPU6050::setXGyroFIFOEnabled(bool enabled)
garfieldsg 0:662207e34fba 692 {
garfieldsg 0:662207e34fba 693 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_XG_FIFO_EN_BIT, enabled);
garfieldsg 0:662207e34fba 694 }
garfieldsg 0:662207e34fba 695 /** Get gyroscope Y-axis FIFO enabled value.
garfieldsg 0:662207e34fba 696 * When set to 1, this bit enables GYRO_YOUT_H and GYRO_YOUT_L (Registers 69 and
garfieldsg 0:662207e34fba 697 * 70) to be written into the FIFO buffer.
garfieldsg 0:662207e34fba 698 * @return Current gyroscope Y-axis FIFO enabled value
garfieldsg 0:662207e34fba 699 * @see MPU6050_RA_FIFO_EN
garfieldsg 0:662207e34fba 700 */
garfieldsg 0:662207e34fba 701 bool MPU6050::getYGyroFIFOEnabled()
garfieldsg 0:662207e34fba 702 {
garfieldsg 0:662207e34fba 703 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_YG_FIFO_EN_BIT, buffer);
garfieldsg 0:662207e34fba 704 return buffer[0];
garfieldsg 0:662207e34fba 705 }
garfieldsg 0:662207e34fba 706 /** Set gyroscope Y-axis FIFO enabled value.
garfieldsg 0:662207e34fba 707 * @param enabled New gyroscope Y-axis FIFO enabled value
garfieldsg 0:662207e34fba 708 * @see getYGyroFIFOEnabled()
garfieldsg 0:662207e34fba 709 * @see MPU6050_RA_FIFO_EN
garfieldsg 0:662207e34fba 710 */
garfieldsg 0:662207e34fba 711 void MPU6050::setYGyroFIFOEnabled(bool enabled)
garfieldsg 0:662207e34fba 712 {
garfieldsg 0:662207e34fba 713 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_YG_FIFO_EN_BIT, enabled);
garfieldsg 0:662207e34fba 714 }
garfieldsg 0:662207e34fba 715 /** Get gyroscope Z-axis FIFO enabled value.
garfieldsg 0:662207e34fba 716 * When set to 1, this bit enables GYRO_ZOUT_H and GYRO_ZOUT_L (Registers 71 and
garfieldsg 0:662207e34fba 717 * 72) to be written into the FIFO buffer.
garfieldsg 0:662207e34fba 718 * @return Current gyroscope Z-axis FIFO enabled value
garfieldsg 0:662207e34fba 719 * @see MPU6050_RA_FIFO_EN
garfieldsg 0:662207e34fba 720 */
garfieldsg 0:662207e34fba 721 bool MPU6050::getZGyroFIFOEnabled()
garfieldsg 0:662207e34fba 722 {
garfieldsg 0:662207e34fba 723 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_ZG_FIFO_EN_BIT, buffer);
garfieldsg 0:662207e34fba 724 return buffer[0];
garfieldsg 0:662207e34fba 725 }
garfieldsg 0:662207e34fba 726 /** Set gyroscope Z-axis FIFO enabled value.
garfieldsg 0:662207e34fba 727 * @param enabled New gyroscope Z-axis FIFO enabled value
garfieldsg 0:662207e34fba 728 * @see getZGyroFIFOEnabled()
garfieldsg 0:662207e34fba 729 * @see MPU6050_RA_FIFO_EN
garfieldsg 0:662207e34fba 730 */
garfieldsg 0:662207e34fba 731 void MPU6050::setZGyroFIFOEnabled(bool enabled)
garfieldsg 0:662207e34fba 732 {
garfieldsg 0:662207e34fba 733 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_ZG_FIFO_EN_BIT, enabled);
garfieldsg 0:662207e34fba 734 }
garfieldsg 0:662207e34fba 735 /** Get accelerometer FIFO enabled value.
garfieldsg 0:662207e34fba 736 * When set to 1, this bit enables ACCEL_XOUT_H, ACCEL_XOUT_L, ACCEL_YOUT_H,
garfieldsg 0:662207e34fba 737 * ACCEL_YOUT_L, ACCEL_ZOUT_H, and ACCEL_ZOUT_L (Registers 59 to 64) to be
garfieldsg 0:662207e34fba 738 * written into the FIFO buffer.
garfieldsg 0:662207e34fba 739 * @return Current accelerometer FIFO enabled value
garfieldsg 0:662207e34fba 740 * @see MPU6050_RA_FIFO_EN
garfieldsg 0:662207e34fba 741 */
garfieldsg 0:662207e34fba 742 bool MPU6050::getAccelFIFOEnabled()
garfieldsg 0:662207e34fba 743 {
garfieldsg 0:662207e34fba 744 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_ACCEL_FIFO_EN_BIT, buffer);
garfieldsg 0:662207e34fba 745 return buffer[0];
garfieldsg 0:662207e34fba 746 }
garfieldsg 0:662207e34fba 747 /** Set accelerometer FIFO enabled value.
garfieldsg 0:662207e34fba 748 * @param enabled New accelerometer FIFO enabled value
garfieldsg 0:662207e34fba 749 * @see getAccelFIFOEnabled()
garfieldsg 0:662207e34fba 750 * @see MPU6050_RA_FIFO_EN
garfieldsg 0:662207e34fba 751 */
garfieldsg 0:662207e34fba 752 void MPU6050::setAccelFIFOEnabled(bool enabled)
garfieldsg 0:662207e34fba 753 {
garfieldsg 0:662207e34fba 754 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_ACCEL_FIFO_EN_BIT, enabled);
garfieldsg 0:662207e34fba 755 }
garfieldsg 0:662207e34fba 756 /** Get Slave 2 FIFO enabled value.
garfieldsg 0:662207e34fba 757 * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96)
garfieldsg 0:662207e34fba 758 * associated with Slave 2 to be written into the FIFO buffer.
garfieldsg 0:662207e34fba 759 * @return Current Slave 2 FIFO enabled value
garfieldsg 0:662207e34fba 760 * @see MPU6050_RA_FIFO_EN
garfieldsg 0:662207e34fba 761 */
garfieldsg 0:662207e34fba 762 bool MPU6050::getSlave2FIFOEnabled()
garfieldsg 0:662207e34fba 763 {
garfieldsg 0:662207e34fba 764 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV2_FIFO_EN_BIT, buffer);
garfieldsg 0:662207e34fba 765 return buffer[0];
garfieldsg 0:662207e34fba 766 }
garfieldsg 0:662207e34fba 767 /** Set Slave 2 FIFO enabled value.
garfieldsg 0:662207e34fba 768 * @param enabled New Slave 2 FIFO enabled value
garfieldsg 0:662207e34fba 769 * @see getSlave2FIFOEnabled()
garfieldsg 0:662207e34fba 770 * @see MPU6050_RA_FIFO_EN
garfieldsg 0:662207e34fba 771 */
garfieldsg 0:662207e34fba 772 void MPU6050::setSlave2FIFOEnabled(bool enabled)
garfieldsg 0:662207e34fba 773 {
garfieldsg 0:662207e34fba 774 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV2_FIFO_EN_BIT, enabled);
garfieldsg 0:662207e34fba 775 }
garfieldsg 0:662207e34fba 776 /** Get Slave 1 FIFO enabled value.
garfieldsg 0:662207e34fba 777 * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96)
garfieldsg 0:662207e34fba 778 * associated with Slave 1 to be written into the FIFO buffer.
garfieldsg 0:662207e34fba 779 * @return Current Slave 1 FIFO enabled value
garfieldsg 0:662207e34fba 780 * @see MPU6050_RA_FIFO_EN
garfieldsg 0:662207e34fba 781 */
garfieldsg 0:662207e34fba 782 bool MPU6050::getSlave1FIFOEnabled()
garfieldsg 0:662207e34fba 783 {
garfieldsg 0:662207e34fba 784 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV1_FIFO_EN_BIT, buffer);
garfieldsg 0:662207e34fba 785 return buffer[0];
garfieldsg 0:662207e34fba 786 }
garfieldsg 0:662207e34fba 787 /** Set Slave 1 FIFO enabled value.
garfieldsg 0:662207e34fba 788 * @param enabled New Slave 1 FIFO enabled value
garfieldsg 0:662207e34fba 789 * @see getSlave1FIFOEnabled()
garfieldsg 0:662207e34fba 790 * @see MPU6050_RA_FIFO_EN
garfieldsg 0:662207e34fba 791 */
garfieldsg 0:662207e34fba 792 void MPU6050::setSlave1FIFOEnabled(bool enabled)
garfieldsg 0:662207e34fba 793 {
garfieldsg 0:662207e34fba 794 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV1_FIFO_EN_BIT, enabled);
garfieldsg 0:662207e34fba 795 }
garfieldsg 0:662207e34fba 796 /** Get Slave 0 FIFO enabled value.
garfieldsg 0:662207e34fba 797 * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96)
garfieldsg 0:662207e34fba 798 * associated with Slave 0 to be written into the FIFO buffer.
garfieldsg 0:662207e34fba 799 * @return Current Slave 0 FIFO enabled value
garfieldsg 0:662207e34fba 800 * @see MPU6050_RA_FIFO_EN
garfieldsg 0:662207e34fba 801 */
garfieldsg 0:662207e34fba 802 bool MPU6050::getSlave0FIFOEnabled()
garfieldsg 0:662207e34fba 803 {
garfieldsg 0:662207e34fba 804 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV0_FIFO_EN_BIT, buffer);
garfieldsg 0:662207e34fba 805 return buffer[0];
garfieldsg 0:662207e34fba 806 }
garfieldsg 0:662207e34fba 807 /** Set Slave 0 FIFO enabled value.
garfieldsg 0:662207e34fba 808 * @param enabled New Slave 0 FIFO enabled value
garfieldsg 0:662207e34fba 809 * @see getSlave0FIFOEnabled()
garfieldsg 0:662207e34fba 810 * @see MPU6050_RA_FIFO_EN
garfieldsg 0:662207e34fba 811 */
garfieldsg 0:662207e34fba 812 void MPU6050::setSlave0FIFOEnabled(bool enabled)
garfieldsg 0:662207e34fba 813 {
garfieldsg 0:662207e34fba 814 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV0_FIFO_EN_BIT, enabled);
garfieldsg 0:662207e34fba 815 }
garfieldsg 0:662207e34fba 816
garfieldsg 0:662207e34fba 817 // I2C_MST_CTRL register
garfieldsg 0:662207e34fba 818
garfieldsg 0:662207e34fba 819 /** Get multi-master enabled value.
garfieldsg 0:662207e34fba 820 * Multi-master capability allows multiple I2C masters to operate on the same
garfieldsg 0:662207e34fba 821 * bus. In circuits where multi-master capability is required, set MULT_MST_EN
garfieldsg 0:662207e34fba 822 * to 1. This will increase current drawn by approximately 30uA.
garfieldsg 0:662207e34fba 823 *
garfieldsg 0:662207e34fba 824 * In circuits where multi-master capability is required, the state of the I2C
garfieldsg 0:662207e34fba 825 * bus must always be monitored by each separate I2C Master. Before an I2C
garfieldsg 0:662207e34fba 826 * Master can assume arbitration of the bus, it must first confirm that no other
garfieldsg 0:662207e34fba 827 * I2C Master has arbitration of the bus. When MULT_MST_EN is set to 1, the
garfieldsg 0:662207e34fba 828 * MPU-60X0's bus arbitration detection logic is turned on, enabling it to
garfieldsg 0:662207e34fba 829 * detect when the bus is available.
garfieldsg 0:662207e34fba 830 *
garfieldsg 0:662207e34fba 831 * @return Current multi-master enabled value
garfieldsg 0:662207e34fba 832 * @see MPU6050_RA_I2C_MST_CTRL
garfieldsg 0:662207e34fba 833 */
garfieldsg 0:662207e34fba 834 bool MPU6050::getMultiMasterEnabled()
garfieldsg 0:662207e34fba 835 {
garfieldsg 0:662207e34fba 836 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_MULT_MST_EN_BIT, buffer);
garfieldsg 0:662207e34fba 837 return buffer[0];
garfieldsg 0:662207e34fba 838 }
garfieldsg 0:662207e34fba 839 /** Set multi-master enabled value.
garfieldsg 0:662207e34fba 840 * @param enabled New multi-master enabled value
garfieldsg 0:662207e34fba 841 * @see getMultiMasterEnabled()
garfieldsg 0:662207e34fba 842 * @see MPU6050_RA_I2C_MST_CTRL
garfieldsg 0:662207e34fba 843 */
garfieldsg 0:662207e34fba 844 void MPU6050::setMultiMasterEnabled(bool enabled)
garfieldsg 0:662207e34fba 845 {
garfieldsg 0:662207e34fba 846 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_MULT_MST_EN_BIT, enabled);
garfieldsg 0:662207e34fba 847 }
garfieldsg 0:662207e34fba 848 /** Get wait-for-external-sensor-data enabled value.
garfieldsg 0:662207e34fba 849 * When the WAIT_FOR_ES bit is set to 1, the Data Ready interrupt will be
garfieldsg 0:662207e34fba 850 * delayed until External Sensor data from the Slave Devices are loaded into the
garfieldsg 0:662207e34fba 851 * EXT_SENS_DATA registers. This is used to ensure that both the internal sensor
garfieldsg 0:662207e34fba 852 * data (i.e. from gyro and accel) and external sensor data have been loaded to
garfieldsg 0:662207e34fba 853 * their respective data registers (i.e. the data is synced) when the Data Ready
garfieldsg 0:662207e34fba 854 * interrupt is triggered.
garfieldsg 0:662207e34fba 855 *
garfieldsg 0:662207e34fba 856 * @return Current wait-for-external-sensor-data enabled value
garfieldsg 0:662207e34fba 857 * @see MPU6050_RA_I2C_MST_CTRL
garfieldsg 0:662207e34fba 858 */
garfieldsg 0:662207e34fba 859 bool MPU6050::getWaitForExternalSensorEnabled()
garfieldsg 0:662207e34fba 860 {
garfieldsg 0:662207e34fba 861 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_WAIT_FOR_ES_BIT, buffer);
garfieldsg 0:662207e34fba 862 return buffer[0];
garfieldsg 0:662207e34fba 863 }
garfieldsg 0:662207e34fba 864 /** Set wait-for-external-sensor-data enabled value.
garfieldsg 0:662207e34fba 865 * @param enabled New wait-for-external-sensor-data enabled value
garfieldsg 0:662207e34fba 866 * @see getWaitForExternalSensorEnabled()
garfieldsg 0:662207e34fba 867 * @see MPU6050_RA_I2C_MST_CTRL
garfieldsg 0:662207e34fba 868 */
garfieldsg 0:662207e34fba 869 void MPU6050::setWaitForExternalSensorEnabled(bool enabled)
garfieldsg 0:662207e34fba 870 {
garfieldsg 0:662207e34fba 871 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_WAIT_FOR_ES_BIT, enabled);
garfieldsg 0:662207e34fba 872 }
garfieldsg 0:662207e34fba 873 /** Get Slave 3 FIFO enabled value.
garfieldsg 0:662207e34fba 874 * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96)
garfieldsg 0:662207e34fba 875 * associated with Slave 3 to be written into the FIFO buffer.
garfieldsg 0:662207e34fba 876 * @return Current Slave 3 FIFO enabled value
garfieldsg 0:662207e34fba 877 * @see MPU6050_RA_MST_CTRL
garfieldsg 0:662207e34fba 878 */
garfieldsg 0:662207e34fba 879 bool MPU6050::getSlave3FIFOEnabled()
garfieldsg 0:662207e34fba 880 {
garfieldsg 0:662207e34fba 881 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_SLV_3_FIFO_EN_BIT, buffer);
garfieldsg 0:662207e34fba 882 return buffer[0];
garfieldsg 0:662207e34fba 883 }
garfieldsg 0:662207e34fba 884 /** Set Slave 3 FIFO enabled value.
garfieldsg 0:662207e34fba 885 * @param enabled New Slave 3 FIFO enabled value
garfieldsg 0:662207e34fba 886 * @see getSlave3FIFOEnabled()
garfieldsg 0:662207e34fba 887 * @see MPU6050_RA_MST_CTRL
garfieldsg 0:662207e34fba 888 */
garfieldsg 0:662207e34fba 889 void MPU6050::setSlave3FIFOEnabled(bool enabled)
garfieldsg 0:662207e34fba 890 {
garfieldsg 0:662207e34fba 891 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_SLV_3_FIFO_EN_BIT, enabled);
garfieldsg 0:662207e34fba 892 }
garfieldsg 0:662207e34fba 893 /** Get slave read/write transition enabled value.
garfieldsg 0:662207e34fba 894 * The I2C_MST_P_NSR bit configures the I2C Master's transition from one slave
garfieldsg 0:662207e34fba 895 * read to the next slave read. If the bit equals 0, there will be a restart
garfieldsg 0:662207e34fba 896 * between reads. If the bit equals 1, there will be a stop followed by a start
garfieldsg 0:662207e34fba 897 * of the following read. When a write transaction follows a read transaction,
garfieldsg 0:662207e34fba 898 * the stop followed by a start of the successive write will be always used.
garfieldsg 0:662207e34fba 899 *
garfieldsg 0:662207e34fba 900 * @return Current slave read/write transition enabled value
garfieldsg 0:662207e34fba 901 * @see MPU6050_RA_I2C_MST_CTRL
garfieldsg 0:662207e34fba 902 */
garfieldsg 0:662207e34fba 903 bool MPU6050::getSlaveReadWriteTransitionEnabled()
garfieldsg 0:662207e34fba 904 {
garfieldsg 0:662207e34fba 905 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_P_NSR_BIT, buffer);
garfieldsg 0:662207e34fba 906 return buffer[0];
garfieldsg 0:662207e34fba 907 }
garfieldsg 0:662207e34fba 908 /** Set slave read/write transition enabled value.
garfieldsg 0:662207e34fba 909 * @param enabled New slave read/write transition enabled value
garfieldsg 0:662207e34fba 910 * @see getSlaveReadWriteTransitionEnabled()
garfieldsg 0:662207e34fba 911 * @see MPU6050_RA_I2C_MST_CTRL
garfieldsg 0:662207e34fba 912 */
garfieldsg 0:662207e34fba 913 void MPU6050::setSlaveReadWriteTransitionEnabled(bool enabled)
garfieldsg 0:662207e34fba 914 {
garfieldsg 0:662207e34fba 915 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_P_NSR_BIT, enabled);
garfieldsg 0:662207e34fba 916 }
garfieldsg 0:662207e34fba 917 /** Get I2C master clock speed.
garfieldsg 0:662207e34fba 918 * I2C_MST_CLK is a 4 bit unsigned value which configures a divider on the
garfieldsg 0:662207e34fba 919 * MPU-60X0 internal 8MHz clock. It sets the I2C master clock speed according to
garfieldsg 0:662207e34fba 920 * the following table:
garfieldsg 0:662207e34fba 921 *
garfieldsg 0:662207e34fba 922 * <pre>
garfieldsg 0:662207e34fba 923 * I2C_MST_CLK | I2C Master Clock Speed | 8MHz Clock Divider
garfieldsg 0:662207e34fba 924 * ------------+------------------------+-------------------
garfieldsg 0:662207e34fba 925 * 0 | 348kHz | 23
garfieldsg 0:662207e34fba 926 * 1 | 333kHz | 24
garfieldsg 0:662207e34fba 927 * 2 | 320kHz | 25
garfieldsg 0:662207e34fba 928 * 3 | 308kHz | 26
garfieldsg 0:662207e34fba 929 * 4 | 296kHz | 27
garfieldsg 0:662207e34fba 930 * 5 | 286kHz | 28
garfieldsg 0:662207e34fba 931 * 6 | 276kHz | 29
garfieldsg 0:662207e34fba 932 * 7 | 267kHz | 30
garfieldsg 0:662207e34fba 933 * 8 | 258kHz | 31
garfieldsg 0:662207e34fba 934 * 9 | 500kHz | 16
garfieldsg 0:662207e34fba 935 * 10 | 471kHz | 17
garfieldsg 0:662207e34fba 936 * 11 | 444kHz | 18
garfieldsg 0:662207e34fba 937 * 12 | 421kHz | 19
garfieldsg 0:662207e34fba 938 * 13 | 400kHz | 20
garfieldsg 0:662207e34fba 939 * 14 | 381kHz | 21
garfieldsg 0:662207e34fba 940 * 15 | 364kHz | 22
garfieldsg 0:662207e34fba 941 * </pre>
garfieldsg 0:662207e34fba 942 *
garfieldsg 0:662207e34fba 943 * @return Current I2C master clock speed
garfieldsg 0:662207e34fba 944 * @see MPU6050_RA_I2C_MST_CTRL
garfieldsg 0:662207e34fba 945 */
garfieldsg 0:662207e34fba 946 uint8_t MPU6050::getMasterClockSpeed()
garfieldsg 0:662207e34fba 947 {
garfieldsg 0:662207e34fba 948 i2Cdev.readBits(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_CLK_BIT, MPU6050_I2C_MST_CLK_LENGTH, buffer);
garfieldsg 0:662207e34fba 949 return buffer[0];
garfieldsg 0:662207e34fba 950 }
garfieldsg 0:662207e34fba 951 /** Set I2C master clock speed.
garfieldsg 0:662207e34fba 952 * @reparam speed Current I2C master clock speed
garfieldsg 0:662207e34fba 953 * @see MPU6050_RA_I2C_MST_CTRL
garfieldsg 0:662207e34fba 954 */
garfieldsg 0:662207e34fba 955 void MPU6050::setMasterClockSpeed(uint8_t speed)
garfieldsg 0:662207e34fba 956 {
garfieldsg 0:662207e34fba 957 i2Cdev.writeBits(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_CLK_BIT, MPU6050_I2C_MST_CLK_LENGTH, speed);
garfieldsg 0:662207e34fba 958 }
garfieldsg 0:662207e34fba 959
garfieldsg 0:662207e34fba 960 // I2C_SLV* registers (Slave 0-3)
garfieldsg 0:662207e34fba 961
garfieldsg 0:662207e34fba 962 /** Get the I2C address of the specified slave (0-3).
garfieldsg 0:662207e34fba 963 * Note that Bit 7 (MSB) controls read/write mode. If Bit 7 is set, it's a read
garfieldsg 0:662207e34fba 964 * operation, and if it is cleared, then it's a write operation. The remaining
garfieldsg 0:662207e34fba 965 * bits (6-0) are the 7-bit device address of the slave device.
garfieldsg 0:662207e34fba 966 *
garfieldsg 0:662207e34fba 967 * In read mode, the result of the read is placed in the lowest available
garfieldsg 0:662207e34fba 968 * EXT_SENS_DATA register. For further information regarding the allocation of
garfieldsg 0:662207e34fba 969 * read results, please refer to the EXT_SENS_DATA register description
garfieldsg 0:662207e34fba 970 * (Registers 73 - 96).
garfieldsg 0:662207e34fba 971 *
garfieldsg 0:662207e34fba 972 * The MPU-6050 supports a total of five slaves, but Slave 4 has unique
garfieldsg 0:662207e34fba 973 * characteristics, and so it has its own functions (getSlave4* and setSlave4*).
garfieldsg 0:662207e34fba 974 *
garfieldsg 0:662207e34fba 975 * I2C data transactions are performed at the Sample Rate, as defined in
garfieldsg 0:662207e34fba 976 * Register 25. The user is responsible for ensuring that I2C data transactions
garfieldsg 0:662207e34fba 977 * to and from each enabled Slave can be completed within a single period of the
garfieldsg 0:662207e34fba 978 * Sample Rate.
garfieldsg 0:662207e34fba 979 *
garfieldsg 0:662207e34fba 980 * The I2C slave access rate can be reduced relative to the Sample Rate. This
garfieldsg 0:662207e34fba 981 * reduced access rate is determined by I2C_MST_DLY (Register 52). Whether a
garfieldsg 0:662207e34fba 982 * slave's access rate is reduced relative to the Sample Rate is determined by
garfieldsg 0:662207e34fba 983 * I2C_MST_DELAY_CTRL (Register 103).
garfieldsg 0:662207e34fba 984 *
garfieldsg 0:662207e34fba 985 * The processing order for the slaves is fixed. The sequence followed for
garfieldsg 0:662207e34fba 986 * processing the slaves is Slave 0, Slave 1, Slave 2, Slave 3 and Slave 4. If a
garfieldsg 0:662207e34fba 987 * particular Slave is disabled it will be skipped.
garfieldsg 0:662207e34fba 988 *
garfieldsg 0:662207e34fba 989 * Each slave can either be accessed at the sample rate or at a reduced sample
garfieldsg 0:662207e34fba 990 * rate. In a case where some slaves are accessed at the Sample Rate and some
garfieldsg 0:662207e34fba 991 * slaves are accessed at the reduced rate, the sequence of accessing the slaves
garfieldsg 0:662207e34fba 992 * (Slave 0 to Slave 4) is still followed. However, the reduced rate slaves will
garfieldsg 0:662207e34fba 993 * be skipped if their access rate dictates that they should not be accessed
garfieldsg 0:662207e34fba 994 * during that particular cycle. For further information regarding the reduced
garfieldsg 0:662207e34fba 995 * access rate, please refer to Register 52. Whether a slave is accessed at the
garfieldsg 0:662207e34fba 996 * Sample Rate or at the reduced rate is determined by the Delay Enable bits in
garfieldsg 0:662207e34fba 997 * Register 103.
garfieldsg 0:662207e34fba 998 *
garfieldsg 0:662207e34fba 999 * @param num Slave number (0-3)
garfieldsg 0:662207e34fba 1000 * @return Current address for specified slave
garfieldsg 0:662207e34fba 1001 * @see MPU6050_RA_I2C_SLV0_ADDR
garfieldsg 0:662207e34fba 1002 */
garfieldsg 0:662207e34fba 1003 uint8_t MPU6050::getSlaveAddress(uint8_t num)
garfieldsg 0:662207e34fba 1004 {
garfieldsg 0:662207e34fba 1005 if (num > 3) return 0;
garfieldsg 0:662207e34fba 1006 i2Cdev.readByte(devAddr, MPU6050_RA_I2C_SLV0_ADDR + num*3, buffer);
garfieldsg 0:662207e34fba 1007 return buffer[0];
garfieldsg 0:662207e34fba 1008 }
garfieldsg 0:662207e34fba 1009 /** Set the I2C address of the specified slave (0-3).
garfieldsg 0:662207e34fba 1010 * @param num Slave number (0-3)
garfieldsg 0:662207e34fba 1011 * @param address New address for specified slave
garfieldsg 0:662207e34fba 1012 * @see getSlaveAddress()
garfieldsg 0:662207e34fba 1013 * @see MPU6050_RA_I2C_SLV0_ADDR
garfieldsg 0:662207e34fba 1014 */
garfieldsg 0:662207e34fba 1015 void MPU6050::setSlaveAddress(uint8_t num, uint8_t address)
garfieldsg 0:662207e34fba 1016 {
garfieldsg 0:662207e34fba 1017 if (num > 3) return;
garfieldsg 0:662207e34fba 1018 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV0_ADDR + num*3, address);
garfieldsg 0:662207e34fba 1019 }
garfieldsg 0:662207e34fba 1020 /** Get the active internal register for the specified slave (0-3).
garfieldsg 0:662207e34fba 1021 * Read/write operations for this slave will be done to whatever internal
garfieldsg 0:662207e34fba 1022 * register address is stored in this MPU register.
garfieldsg 0:662207e34fba 1023 *
garfieldsg 0:662207e34fba 1024 * The MPU-6050 supports a total of five slaves, but Slave 4 has unique
garfieldsg 0:662207e34fba 1025 * characteristics, and so it has its own functions.
garfieldsg 0:662207e34fba 1026 *
garfieldsg 0:662207e34fba 1027 * @param num Slave number (0-3)
garfieldsg 0:662207e34fba 1028 * @return Current active register for specified slave
garfieldsg 0:662207e34fba 1029 * @see MPU6050_RA_I2C_SLV0_REG
garfieldsg 0:662207e34fba 1030 */
garfieldsg 0:662207e34fba 1031 uint8_t MPU6050::getSlaveRegister(uint8_t num)
garfieldsg 0:662207e34fba 1032 {
garfieldsg 0:662207e34fba 1033 if (num > 3) return 0;
garfieldsg 0:662207e34fba 1034 i2Cdev.readByte(devAddr, MPU6050_RA_I2C_SLV0_REG + num*3, buffer);
garfieldsg 0:662207e34fba 1035 return buffer[0];
garfieldsg 0:662207e34fba 1036 }
garfieldsg 0:662207e34fba 1037 /** Set the active internal register for the specified slave (0-3).
garfieldsg 0:662207e34fba 1038 * @param num Slave number (0-3)
garfieldsg 0:662207e34fba 1039 * @param reg New active register for specified slave
garfieldsg 0:662207e34fba 1040 * @see getSlaveRegister()
garfieldsg 0:662207e34fba 1041 * @see MPU6050_RA_I2C_SLV0_REG
garfieldsg 0:662207e34fba 1042 */
garfieldsg 0:662207e34fba 1043 void MPU6050::setSlaveRegister(uint8_t num, uint8_t reg)
garfieldsg 0:662207e34fba 1044 {
garfieldsg 0:662207e34fba 1045 if (num > 3) return;
garfieldsg 0:662207e34fba 1046 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV0_REG + num*3, reg);
garfieldsg 0:662207e34fba 1047 }
garfieldsg 0:662207e34fba 1048 /** Get the enabled value for the specified slave (0-3).
garfieldsg 0:662207e34fba 1049 * When set to 1, this bit enables Slave 0 for data transfer operations. When
garfieldsg 0:662207e34fba 1050 * cleared to 0, this bit disables Slave 0 from data transfer operations.
garfieldsg 0:662207e34fba 1051 * @param num Slave number (0-3)
garfieldsg 0:662207e34fba 1052 * @return Current enabled value for specified slave
garfieldsg 0:662207e34fba 1053 * @see MPU6050_RA_I2C_SLV0_CTRL
garfieldsg 0:662207e34fba 1054 */
garfieldsg 0:662207e34fba 1055 bool MPU6050::getSlaveEnabled(uint8_t num)
garfieldsg 0:662207e34fba 1056 {
garfieldsg 0:662207e34fba 1057 if (num > 3) return 0;
garfieldsg 0:662207e34fba 1058 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_EN_BIT, buffer);
garfieldsg 0:662207e34fba 1059 return buffer[0];
garfieldsg 0:662207e34fba 1060 }
garfieldsg 0:662207e34fba 1061 /** Set the enabled value for the specified slave (0-3).
garfieldsg 0:662207e34fba 1062 * @param num Slave number (0-3)
garfieldsg 0:662207e34fba 1063 * @param enabled New enabled value for specified slave
garfieldsg 0:662207e34fba 1064 * @see getSlaveEnabled()
garfieldsg 0:662207e34fba 1065 * @see MPU6050_RA_I2C_SLV0_CTRL
garfieldsg 0:662207e34fba 1066 */
garfieldsg 0:662207e34fba 1067 void MPU6050::setSlaveEnabled(uint8_t num, bool enabled)
garfieldsg 0:662207e34fba 1068 {
garfieldsg 0:662207e34fba 1069 if (num > 3) return;
garfieldsg 0:662207e34fba 1070 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_EN_BIT, enabled);
garfieldsg 0:662207e34fba 1071 }
garfieldsg 0:662207e34fba 1072 /** Get word pair byte-swapping enabled for the specified slave (0-3).
garfieldsg 0:662207e34fba 1073 * When set to 1, this bit enables byte swapping. When byte swapping is enabled,
garfieldsg 0:662207e34fba 1074 * the high and low bytes of a word pair are swapped. Please refer to
garfieldsg 0:662207e34fba 1075 * I2C_SLV0_GRP for the pairing convention of the word pairs. When cleared to 0,
garfieldsg 0:662207e34fba 1076 * bytes transferred to and from Slave 0 will be written to EXT_SENS_DATA
garfieldsg 0:662207e34fba 1077 * registers in the order they were transferred.
garfieldsg 0:662207e34fba 1078 *
garfieldsg 0:662207e34fba 1079 * @param num Slave number (0-3)
garfieldsg 0:662207e34fba 1080 * @return Current word pair byte-swapping enabled value for specified slave
garfieldsg 0:662207e34fba 1081 * @see MPU6050_RA_I2C_SLV0_CTRL
garfieldsg 0:662207e34fba 1082 */
garfieldsg 0:662207e34fba 1083 bool MPU6050::getSlaveWordByteSwap(uint8_t num)
garfieldsg 0:662207e34fba 1084 {
garfieldsg 0:662207e34fba 1085 if (num > 3) return 0;
garfieldsg 0:662207e34fba 1086 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_BYTE_SW_BIT, buffer);
garfieldsg 0:662207e34fba 1087 return buffer[0];
garfieldsg 0:662207e34fba 1088 }
garfieldsg 0:662207e34fba 1089 /** Set word pair byte-swapping enabled for the specified slave (0-3).
garfieldsg 0:662207e34fba 1090 * @param num Slave number (0-3)
garfieldsg 0:662207e34fba 1091 * @param enabled New word pair byte-swapping enabled value for specified slave
garfieldsg 0:662207e34fba 1092 * @see getSlaveWordByteSwap()
garfieldsg 0:662207e34fba 1093 * @see MPU6050_RA_I2C_SLV0_CTRL
garfieldsg 0:662207e34fba 1094 */
garfieldsg 0:662207e34fba 1095 void MPU6050::setSlaveWordByteSwap(uint8_t num, bool enabled)
garfieldsg 0:662207e34fba 1096 {
garfieldsg 0:662207e34fba 1097 if (num > 3) return;
garfieldsg 0:662207e34fba 1098 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_BYTE_SW_BIT, enabled);
garfieldsg 0:662207e34fba 1099 }
garfieldsg 0:662207e34fba 1100 /** Get write mode for the specified slave (0-3).
garfieldsg 0:662207e34fba 1101 * When set to 1, the transaction will read or write data only. When cleared to
garfieldsg 0:662207e34fba 1102 * 0, the transaction will write a register address prior to reading or writing
garfieldsg 0:662207e34fba 1103 * data. This should equal 0 when specifying the register address within the
garfieldsg 0:662207e34fba 1104 * Slave device to/from which the ensuing data transaction will take place.
garfieldsg 0:662207e34fba 1105 *
garfieldsg 0:662207e34fba 1106 * @param num Slave number (0-3)
garfieldsg 0:662207e34fba 1107 * @return Current write mode for specified slave (0 = register address + data, 1 = data only)
garfieldsg 0:662207e34fba 1108 * @see MPU6050_RA_I2C_SLV0_CTRL
garfieldsg 0:662207e34fba 1109 */
garfieldsg 0:662207e34fba 1110 bool MPU6050::getSlaveWriteMode(uint8_t num)
garfieldsg 0:662207e34fba 1111 {
garfieldsg 0:662207e34fba 1112 if (num > 3) return 0;
garfieldsg 0:662207e34fba 1113 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_REG_DIS_BIT, buffer);
garfieldsg 0:662207e34fba 1114 return buffer[0];
garfieldsg 0:662207e34fba 1115 }
garfieldsg 0:662207e34fba 1116 /** Set write mode for the specified slave (0-3).
garfieldsg 0:662207e34fba 1117 * @param num Slave number (0-3)
garfieldsg 0:662207e34fba 1118 * @param mode New write mode for specified slave (0 = register address + data, 1 = data only)
garfieldsg 0:662207e34fba 1119 * @see getSlaveWriteMode()
garfieldsg 0:662207e34fba 1120 * @see MPU6050_RA_I2C_SLV0_CTRL
garfieldsg 0:662207e34fba 1121 */
garfieldsg 0:662207e34fba 1122 void MPU6050::setSlaveWriteMode(uint8_t num, bool mode)
garfieldsg 0:662207e34fba 1123 {
garfieldsg 0:662207e34fba 1124 if (num > 3) return;
garfieldsg 0:662207e34fba 1125 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_REG_DIS_BIT, mode);
garfieldsg 0:662207e34fba 1126 }
garfieldsg 0:662207e34fba 1127 /** Get word pair grouping order offset for the specified slave (0-3).
garfieldsg 0:662207e34fba 1128 * This sets specifies the grouping order of word pairs received from registers.
garfieldsg 0:662207e34fba 1129 * When cleared to 0, bytes from register addresses 0 and 1, 2 and 3, etc (even,
garfieldsg 0:662207e34fba 1130 * then odd register addresses) are paired to form a word. When set to 1, bytes
garfieldsg 0:662207e34fba 1131 * from register addresses are paired 1 and 2, 3 and 4, etc. (odd, then even
garfieldsg 0:662207e34fba 1132 * register addresses) are paired to form a word.
garfieldsg 0:662207e34fba 1133 *
garfieldsg 0:662207e34fba 1134 * @param num Slave number (0-3)
garfieldsg 0:662207e34fba 1135 * @return Current word pair grouping order offset for specified slave
garfieldsg 0:662207e34fba 1136 * @see MPU6050_RA_I2C_SLV0_CTRL
garfieldsg 0:662207e34fba 1137 */
garfieldsg 0:662207e34fba 1138 bool MPU6050::getSlaveWordGroupOffset(uint8_t num)
garfieldsg 0:662207e34fba 1139 {
garfieldsg 0:662207e34fba 1140 if (num > 3) return 0;
garfieldsg 0:662207e34fba 1141 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_GRP_BIT, buffer);
garfieldsg 0:662207e34fba 1142 return buffer[0];
garfieldsg 0:662207e34fba 1143 }
garfieldsg 0:662207e34fba 1144 /** Set word pair grouping order offset for the specified slave (0-3).
garfieldsg 0:662207e34fba 1145 * @param num Slave number (0-3)
garfieldsg 0:662207e34fba 1146 * @param enabled New word pair grouping order offset for specified slave
garfieldsg 0:662207e34fba 1147 * @see getSlaveWordGroupOffset()
garfieldsg 0:662207e34fba 1148 * @see MPU6050_RA_I2C_SLV0_CTRL
garfieldsg 0:662207e34fba 1149 */
garfieldsg 0:662207e34fba 1150 void MPU6050::setSlaveWordGroupOffset(uint8_t num, bool enabled)
garfieldsg 0:662207e34fba 1151 {
garfieldsg 0:662207e34fba 1152 if (num > 3) return;
garfieldsg 0:662207e34fba 1153 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_GRP_BIT, enabled);
garfieldsg 0:662207e34fba 1154 }
garfieldsg 0:662207e34fba 1155 /** Get number of bytes to read for the specified slave (0-3).
garfieldsg 0:662207e34fba 1156 * Specifies the number of bytes transferred to and from Slave 0. Clearing this
garfieldsg 0:662207e34fba 1157 * bit to 0 is equivalent to disabling the register by writing 0 to I2C_SLV0_EN.
garfieldsg 0:662207e34fba 1158 * @param num Slave number (0-3)
garfieldsg 0:662207e34fba 1159 * @return Number of bytes to read for specified slave
garfieldsg 0:662207e34fba 1160 * @see MPU6050_RA_I2C_SLV0_CTRL
garfieldsg 0:662207e34fba 1161 */
garfieldsg 0:662207e34fba 1162 uint8_t MPU6050::getSlaveDataLength(uint8_t num)
garfieldsg 0:662207e34fba 1163 {
garfieldsg 0:662207e34fba 1164 if (num > 3) return 0;
garfieldsg 0:662207e34fba 1165 i2Cdev.readBits(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_LEN_BIT, MPU6050_I2C_SLV_LEN_LENGTH, buffer);
garfieldsg 0:662207e34fba 1166 return buffer[0];
garfieldsg 0:662207e34fba 1167 }
garfieldsg 0:662207e34fba 1168 /** Set number of bytes to read for the specified slave (0-3).
garfieldsg 0:662207e34fba 1169 * @param num Slave number (0-3)
garfieldsg 0:662207e34fba 1170 * @param length Number of bytes to read for specified slave
garfieldsg 0:662207e34fba 1171 * @see getSlaveDataLength()
garfieldsg 0:662207e34fba 1172 * @see MPU6050_RA_I2C_SLV0_CTRL
garfieldsg 0:662207e34fba 1173 */
garfieldsg 0:662207e34fba 1174 void MPU6050::setSlaveDataLength(uint8_t num, uint8_t length)
garfieldsg 0:662207e34fba 1175 {
garfieldsg 0:662207e34fba 1176 if (num > 3) return;
garfieldsg 0:662207e34fba 1177 i2Cdev.writeBits(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_LEN_BIT, MPU6050_I2C_SLV_LEN_LENGTH, length);
garfieldsg 0:662207e34fba 1178 }
garfieldsg 0:662207e34fba 1179
garfieldsg 0:662207e34fba 1180 // I2C_SLV* registers (Slave 4)
garfieldsg 0:662207e34fba 1181
garfieldsg 0:662207e34fba 1182 /** Get the I2C address of Slave 4.
garfieldsg 0:662207e34fba 1183 * Note that Bit 7 (MSB) controls read/write mode. If Bit 7 is set, it's a read
garfieldsg 0:662207e34fba 1184 * operation, and if it is cleared, then it's a write operation. The remaining
garfieldsg 0:662207e34fba 1185 * bits (6-0) are the 7-bit device address of the slave device.
garfieldsg 0:662207e34fba 1186 *
garfieldsg 0:662207e34fba 1187 * @return Current address for Slave 4
garfieldsg 0:662207e34fba 1188 * @see getSlaveAddress()
garfieldsg 0:662207e34fba 1189 * @see MPU6050_RA_I2C_SLV4_ADDR
garfieldsg 0:662207e34fba 1190 */
garfieldsg 0:662207e34fba 1191 uint8_t MPU6050::getSlave4Address()
garfieldsg 0:662207e34fba 1192 {
garfieldsg 0:662207e34fba 1193 i2Cdev.readByte(devAddr, MPU6050_RA_I2C_SLV4_ADDR, buffer);
garfieldsg 0:662207e34fba 1194 return buffer[0];
garfieldsg 0:662207e34fba 1195 }
garfieldsg 0:662207e34fba 1196 /** Set the I2C address of Slave 4.
garfieldsg 0:662207e34fba 1197 * @param address New address for Slave 4
garfieldsg 0:662207e34fba 1198 * @see getSlave4Address()
garfieldsg 0:662207e34fba 1199 * @see MPU6050_RA_I2C_SLV4_ADDR
garfieldsg 0:662207e34fba 1200 */
garfieldsg 0:662207e34fba 1201 void MPU6050::setSlave4Address(uint8_t address)
garfieldsg 0:662207e34fba 1202 {
garfieldsg 0:662207e34fba 1203 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV4_ADDR, address);
garfieldsg 0:662207e34fba 1204 }
garfieldsg 0:662207e34fba 1205 /** Get the active internal register for the Slave 4.
garfieldsg 0:662207e34fba 1206 * Read/write operations for this slave will be done to whatever internal
garfieldsg 0:662207e34fba 1207 * register address is stored in this MPU register.
garfieldsg 0:662207e34fba 1208 *
garfieldsg 0:662207e34fba 1209 * @return Current active register for Slave 4
garfieldsg 0:662207e34fba 1210 * @see MPU6050_RA_I2C_SLV4_REG
garfieldsg 0:662207e34fba 1211 */
garfieldsg 0:662207e34fba 1212 uint8_t MPU6050::getSlave4Register()
garfieldsg 0:662207e34fba 1213 {
garfieldsg 0:662207e34fba 1214 i2Cdev.readByte(devAddr, MPU6050_RA_I2C_SLV4_REG, buffer);
garfieldsg 0:662207e34fba 1215 return buffer[0];
garfieldsg 0:662207e34fba 1216 }
garfieldsg 0:662207e34fba 1217 /** Set the active internal register for Slave 4.
garfieldsg 0:662207e34fba 1218 * @param reg New active register for Slave 4
garfieldsg 0:662207e34fba 1219 * @see getSlave4Register()
garfieldsg 0:662207e34fba 1220 * @see MPU6050_RA_I2C_SLV4_REG
garfieldsg 0:662207e34fba 1221 */
garfieldsg 0:662207e34fba 1222 void MPU6050::setSlave4Register(uint8_t reg)
garfieldsg 0:662207e34fba 1223 {
garfieldsg 0:662207e34fba 1224 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV4_REG, reg);
garfieldsg 0:662207e34fba 1225 }
garfieldsg 0:662207e34fba 1226 /** Set new byte to write to Slave 4.
garfieldsg 0:662207e34fba 1227 * This register stores the data to be written into the Slave 4. If I2C_SLV4_RW
garfieldsg 0:662207e34fba 1228 * is set 1 (set to read), this register has no effect.
garfieldsg 0:662207e34fba 1229 * @param data New byte to write to Slave 4
garfieldsg 0:662207e34fba 1230 * @see MPU6050_RA_I2C_SLV4_DO
garfieldsg 0:662207e34fba 1231 */
garfieldsg 0:662207e34fba 1232 void MPU6050::setSlave4OutputByte(uint8_t data)
garfieldsg 0:662207e34fba 1233 {
garfieldsg 0:662207e34fba 1234 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV4_DO, data);
garfieldsg 0:662207e34fba 1235 }
garfieldsg 0:662207e34fba 1236 /** Get the enabled value for the Slave 4.
garfieldsg 0:662207e34fba 1237 * When set to 1, this bit enables Slave 4 for data transfer operations. When
garfieldsg 0:662207e34fba 1238 * cleared to 0, this bit disables Slave 4 from data transfer operations.
garfieldsg 0:662207e34fba 1239 * @return Current enabled value for Slave 4
garfieldsg 0:662207e34fba 1240 * @see MPU6050_RA_I2C_SLV4_CTRL
garfieldsg 0:662207e34fba 1241 */
garfieldsg 0:662207e34fba 1242 bool MPU6050::getSlave4Enabled()
garfieldsg 0:662207e34fba 1243 {
garfieldsg 0:662207e34fba 1244 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_EN_BIT, buffer);
garfieldsg 0:662207e34fba 1245 return buffer[0];
garfieldsg 0:662207e34fba 1246 }
garfieldsg 0:662207e34fba 1247 /** Set the enabled value for Slave 4.
garfieldsg 0:662207e34fba 1248 * @param enabled New enabled value for Slave 4
garfieldsg 0:662207e34fba 1249 * @see getSlave4Enabled()
garfieldsg 0:662207e34fba 1250 * @see MPU6050_RA_I2C_SLV4_CTRL
garfieldsg 0:662207e34fba 1251 */
garfieldsg 0:662207e34fba 1252 void MPU6050::setSlave4Enabled(bool enabled)
garfieldsg 0:662207e34fba 1253 {
garfieldsg 0:662207e34fba 1254 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_EN_BIT, enabled);
garfieldsg 0:662207e34fba 1255 }
garfieldsg 0:662207e34fba 1256 /** Get the enabled value for Slave 4 transaction interrupts.
garfieldsg 0:662207e34fba 1257 * When set to 1, this bit enables the generation of an interrupt signal upon
garfieldsg 0:662207e34fba 1258 * completion of a Slave 4 transaction. When cleared to 0, this bit disables the
garfieldsg 0:662207e34fba 1259 * generation of an interrupt signal upon completion of a Slave 4 transaction.
garfieldsg 0:662207e34fba 1260 * The interrupt status can be observed in Register 54.
garfieldsg 0:662207e34fba 1261 *
garfieldsg 0:662207e34fba 1262 * @return Current enabled value for Slave 4 transaction interrupts.
garfieldsg 0:662207e34fba 1263 * @see MPU6050_RA_I2C_SLV4_CTRL
garfieldsg 0:662207e34fba 1264 */
garfieldsg 0:662207e34fba 1265 bool MPU6050::getSlave4InterruptEnabled()
garfieldsg 0:662207e34fba 1266 {
garfieldsg 0:662207e34fba 1267 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_INT_EN_BIT, buffer);
garfieldsg 0:662207e34fba 1268 return buffer[0];
garfieldsg 0:662207e34fba 1269 }
garfieldsg 0:662207e34fba 1270 /** Set the enabled value for Slave 4 transaction interrupts.
garfieldsg 0:662207e34fba 1271 * @param enabled New enabled value for Slave 4 transaction interrupts.
garfieldsg 0:662207e34fba 1272 * @see getSlave4InterruptEnabled()
garfieldsg 0:662207e34fba 1273 * @see MPU6050_RA_I2C_SLV4_CTRL
garfieldsg 0:662207e34fba 1274 */
garfieldsg 0:662207e34fba 1275 void MPU6050::setSlave4InterruptEnabled(bool enabled)
garfieldsg 0:662207e34fba 1276 {
garfieldsg 0:662207e34fba 1277 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_INT_EN_BIT, enabled);
garfieldsg 0:662207e34fba 1278 }
garfieldsg 0:662207e34fba 1279 /** Get write mode for Slave 4.
garfieldsg 0:662207e34fba 1280 * When set to 1, the transaction will read or write data only. When cleared to
garfieldsg 0:662207e34fba 1281 * 0, the transaction will write a register address prior to reading or writing
garfieldsg 0:662207e34fba 1282 * data. This should equal 0 when specifying the register address within the
garfieldsg 0:662207e34fba 1283 * Slave device to/from which the ensuing data transaction will take place.
garfieldsg 0:662207e34fba 1284 *
garfieldsg 0:662207e34fba 1285 * @return Current write mode for Slave 4 (0 = register address + data, 1 = data only)
garfieldsg 0:662207e34fba 1286 * @see MPU6050_RA_I2C_SLV4_CTRL
garfieldsg 0:662207e34fba 1287 */
garfieldsg 0:662207e34fba 1288 bool MPU6050::getSlave4WriteMode()
garfieldsg 0:662207e34fba 1289 {
garfieldsg 0:662207e34fba 1290 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_REG_DIS_BIT, buffer);
garfieldsg 0:662207e34fba 1291 return buffer[0];
garfieldsg 0:662207e34fba 1292 }
garfieldsg 0:662207e34fba 1293 /** Set write mode for the Slave 4.
garfieldsg 0:662207e34fba 1294 * @param mode New write mode for Slave 4 (0 = register address + data, 1 = data only)
garfieldsg 0:662207e34fba 1295 * @see getSlave4WriteMode()
garfieldsg 0:662207e34fba 1296 * @see MPU6050_RA_I2C_SLV4_CTRL
garfieldsg 0:662207e34fba 1297 */
garfieldsg 0:662207e34fba 1298 void MPU6050::setSlave4WriteMode(bool mode)
garfieldsg 0:662207e34fba 1299 {
garfieldsg 0:662207e34fba 1300 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_REG_DIS_BIT, mode);
garfieldsg 0:662207e34fba 1301 }
garfieldsg 0:662207e34fba 1302 /** Get Slave 4 master delay value.
garfieldsg 0:662207e34fba 1303 * This configures the reduced access rate of I2C slaves relative to the Sample
garfieldsg 0:662207e34fba 1304 * Rate. When a slave's access rate is decreased relative to the Sample Rate,
garfieldsg 0:662207e34fba 1305 * the slave is accessed every:
garfieldsg 0:662207e34fba 1306 *
garfieldsg 0:662207e34fba 1307 * 1 / (1 + I2C_MST_DLY) samples
garfieldsg 0:662207e34fba 1308 *
garfieldsg 0:662207e34fba 1309 * This base Sample Rate in turn is determined by SMPLRT_DIV (register 25) and
garfieldsg 0:662207e34fba 1310 * DLPF_CFG (register 26). Whether a slave's access rate is reduced relative to
garfieldsg 0:662207e34fba 1311 * the Sample Rate is determined by I2C_MST_DELAY_CTRL (register 103). For
garfieldsg 0:662207e34fba 1312 * further information regarding the Sample Rate, please refer to register 25.
garfieldsg 0:662207e34fba 1313 *
garfieldsg 0:662207e34fba 1314 * @return Current Slave 4 master delay value
garfieldsg 0:662207e34fba 1315 * @see MPU6050_RA_I2C_SLV4_CTRL
garfieldsg 0:662207e34fba 1316 */
garfieldsg 0:662207e34fba 1317 uint8_t MPU6050::getSlave4MasterDelay()
garfieldsg 0:662207e34fba 1318 {
garfieldsg 0:662207e34fba 1319 i2Cdev.readBits(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_MST_DLY_BIT, MPU6050_I2C_SLV4_MST_DLY_LENGTH, buffer);
garfieldsg 0:662207e34fba 1320 return buffer[0];
garfieldsg 0:662207e34fba 1321 }
garfieldsg 0:662207e34fba 1322 /** Set Slave 4 master delay value.
garfieldsg 0:662207e34fba 1323 * @param delay New Slave 4 master delay value
garfieldsg 0:662207e34fba 1324 * @see getSlave4MasterDelay()
garfieldsg 0:662207e34fba 1325 * @see MPU6050_RA_I2C_SLV4_CTRL
garfieldsg 0:662207e34fba 1326 */
garfieldsg 0:662207e34fba 1327 void MPU6050::setSlave4MasterDelay(uint8_t delay)
garfieldsg 0:662207e34fba 1328 {
garfieldsg 0:662207e34fba 1329 i2Cdev.writeBits(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_MST_DLY_BIT, MPU6050_I2C_SLV4_MST_DLY_LENGTH, delay);
garfieldsg 0:662207e34fba 1330 }
garfieldsg 0:662207e34fba 1331 /** Get last available byte read from Slave 4.
garfieldsg 0:662207e34fba 1332 * This register stores the data read from Slave 4. This field is populated
garfieldsg 0:662207e34fba 1333 * after a read transaction.
garfieldsg 0:662207e34fba 1334 * @return Last available byte read from to Slave 4
garfieldsg 0:662207e34fba 1335 * @see MPU6050_RA_I2C_SLV4_DI
garfieldsg 0:662207e34fba 1336 */
garfieldsg 0:662207e34fba 1337 uint8_t MPU6050::getSlate4InputByte()
garfieldsg 0:662207e34fba 1338 {
garfieldsg 0:662207e34fba 1339 i2Cdev.readByte(devAddr, MPU6050_RA_I2C_SLV4_DI, buffer);
garfieldsg 0:662207e34fba 1340 return buffer[0];
garfieldsg 0:662207e34fba 1341 }
garfieldsg 0:662207e34fba 1342
garfieldsg 0:662207e34fba 1343 // I2C_MST_STATUS register
garfieldsg 0:662207e34fba 1344
garfieldsg 0:662207e34fba 1345 /** Get FSYNC interrupt status.
garfieldsg 0:662207e34fba 1346 * This bit reflects the status of the FSYNC interrupt from an external device
garfieldsg 0:662207e34fba 1347 * into the MPU-60X0. This is used as a way to pass an external interrupt
garfieldsg 0:662207e34fba 1348 * through the MPU-60X0 to the host application processor. When set to 1, this
garfieldsg 0:662207e34fba 1349 * bit will cause an interrupt if FSYNC_INT_EN is asserted in INT_PIN_CFG
garfieldsg 0:662207e34fba 1350 * (Register 55).
garfieldsg 0:662207e34fba 1351 * @return FSYNC interrupt status
garfieldsg 0:662207e34fba 1352 * @see MPU6050_RA_I2C_MST_STATUS
garfieldsg 0:662207e34fba 1353 */
garfieldsg 0:662207e34fba 1354 bool MPU6050::getPassthroughStatus()
garfieldsg 0:662207e34fba 1355 {
garfieldsg 0:662207e34fba 1356 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_PASS_THROUGH_BIT, buffer);
garfieldsg 0:662207e34fba 1357 return buffer[0];
garfieldsg 0:662207e34fba 1358 }
garfieldsg 0:662207e34fba 1359 /** Get Slave 4 transaction done status.
garfieldsg 0:662207e34fba 1360 * Automatically sets to 1 when a Slave 4 transaction has completed. This
garfieldsg 0:662207e34fba 1361 * triggers an interrupt if the I2C_MST_INT_EN bit in the INT_ENABLE register
garfieldsg 0:662207e34fba 1362 * (Register 56) is asserted and if the SLV_4_DONE_INT bit is asserted in the
garfieldsg 0:662207e34fba 1363 * I2C_SLV4_CTRL register (Register 52).
garfieldsg 0:662207e34fba 1364 * @return Slave 4 transaction done status
garfieldsg 0:662207e34fba 1365 * @see MPU6050_RA_I2C_MST_STATUS
garfieldsg 0:662207e34fba 1366 */
garfieldsg 0:662207e34fba 1367 bool MPU6050::getSlave4IsDone()
garfieldsg 0:662207e34fba 1368 {
garfieldsg 0:662207e34fba 1369 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV4_DONE_BIT, buffer);
garfieldsg 0:662207e34fba 1370 return buffer[0];
garfieldsg 0:662207e34fba 1371 }
garfieldsg 0:662207e34fba 1372 /** Get master arbitration lost status.
garfieldsg 0:662207e34fba 1373 * This bit automatically sets to 1 when the I2C Master has lost arbitration of
garfieldsg 0:662207e34fba 1374 * the auxiliary I2C bus (an error condition). This triggers an interrupt if the
garfieldsg 0:662207e34fba 1375 * I2C_MST_INT_EN bit in the INT_ENABLE register (Register 56) is asserted.
garfieldsg 0:662207e34fba 1376 * @return Master arbitration lost status
garfieldsg 0:662207e34fba 1377 * @see MPU6050_RA_I2C_MST_STATUS
garfieldsg 0:662207e34fba 1378 */
garfieldsg 0:662207e34fba 1379 bool MPU6050::getLostArbitration()
garfieldsg 0:662207e34fba 1380 {
garfieldsg 0:662207e34fba 1381 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_LOST_ARB_BIT, buffer);
garfieldsg 0:662207e34fba 1382 return buffer[0];
garfieldsg 0:662207e34fba 1383 }
garfieldsg 0:662207e34fba 1384 /** Get Slave 4 NACK status.
garfieldsg 0:662207e34fba 1385 * This bit automatically sets to 1 when the I2C Master receives a NACK in a
garfieldsg 0:662207e34fba 1386 * transaction with Slave 4. This triggers an interrupt if the I2C_MST_INT_EN
garfieldsg 0:662207e34fba 1387 * bit in the INT_ENABLE register (Register 56) is asserted.
garfieldsg 0:662207e34fba 1388 * @return Slave 4 NACK interrupt status
garfieldsg 0:662207e34fba 1389 * @see MPU6050_RA_I2C_MST_STATUS
garfieldsg 0:662207e34fba 1390 */
garfieldsg 0:662207e34fba 1391 bool MPU6050::getSlave4Nack()
garfieldsg 0:662207e34fba 1392 {
garfieldsg 0:662207e34fba 1393 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV4_NACK_BIT, buffer);
garfieldsg 0:662207e34fba 1394 return buffer[0];
garfieldsg 0:662207e34fba 1395 }
garfieldsg 0:662207e34fba 1396 /** Get Slave 3 NACK status.
garfieldsg 0:662207e34fba 1397 * This bit automatically sets to 1 when the I2C Master receives a NACK in a
garfieldsg 0:662207e34fba 1398 * transaction with Slave 3. This triggers an interrupt if the I2C_MST_INT_EN
garfieldsg 0:662207e34fba 1399 * bit in the INT_ENABLE register (Register 56) is asserted.
garfieldsg 0:662207e34fba 1400 * @return Slave 3 NACK interrupt status
garfieldsg 0:662207e34fba 1401 * @see MPU6050_RA_I2C_MST_STATUS
garfieldsg 0:662207e34fba 1402 */
garfieldsg 0:662207e34fba 1403 bool MPU6050::getSlave3Nack()
garfieldsg 0:662207e34fba 1404 {
garfieldsg 0:662207e34fba 1405 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV3_NACK_BIT, buffer);
garfieldsg 0:662207e34fba 1406 return buffer[0];
garfieldsg 0:662207e34fba 1407 }
garfieldsg 0:662207e34fba 1408 /** Get Slave 2 NACK status.
garfieldsg 0:662207e34fba 1409 * This bit automatically sets to 1 when the I2C Master receives a NACK in a
garfieldsg 0:662207e34fba 1410 * transaction with Slave 2. This triggers an interrupt if the I2C_MST_INT_EN
garfieldsg 0:662207e34fba 1411 * bit in the INT_ENABLE register (Register 56) is asserted.
garfieldsg 0:662207e34fba 1412 * @return Slave 2 NACK interrupt status
garfieldsg 0:662207e34fba 1413 * @see MPU6050_RA_I2C_MST_STATUS
garfieldsg 0:662207e34fba 1414 */
garfieldsg 0:662207e34fba 1415 bool MPU6050::getSlave2Nack()
garfieldsg 0:662207e34fba 1416 {
garfieldsg 0:662207e34fba 1417 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV2_NACK_BIT, buffer);
garfieldsg 0:662207e34fba 1418 return buffer[0];
garfieldsg 0:662207e34fba 1419 }
garfieldsg 0:662207e34fba 1420 /** Get Slave 1 NACK status.
garfieldsg 0:662207e34fba 1421 * This bit automatically sets to 1 when the I2C Master receives a NACK in a
garfieldsg 0:662207e34fba 1422 * transaction with Slave 1. This triggers an interrupt if the I2C_MST_INT_EN
garfieldsg 0:662207e34fba 1423 * bit in the INT_ENABLE register (Register 56) is asserted.
garfieldsg 0:662207e34fba 1424 * @return Slave 1 NACK interrupt status
garfieldsg 0:662207e34fba 1425 * @see MPU6050_RA_I2C_MST_STATUS
garfieldsg 0:662207e34fba 1426 */
garfieldsg 0:662207e34fba 1427 bool MPU6050::getSlave1Nack()
garfieldsg 0:662207e34fba 1428 {
garfieldsg 0:662207e34fba 1429 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV1_NACK_BIT, buffer);
garfieldsg 0:662207e34fba 1430 return buffer[0];
garfieldsg 0:662207e34fba 1431 }
garfieldsg 0:662207e34fba 1432 /** Get Slave 0 NACK status.
garfieldsg 0:662207e34fba 1433 * This bit automatically sets to 1 when the I2C Master receives a NACK in a
garfieldsg 0:662207e34fba 1434 * transaction with Slave 0. This triggers an interrupt if the I2C_MST_INT_EN
garfieldsg 0:662207e34fba 1435 * bit in the INT_ENABLE register (Register 56) is asserted.
garfieldsg 0:662207e34fba 1436 * @return Slave 0 NACK interrupt status
garfieldsg 0:662207e34fba 1437 * @see MPU6050_RA_I2C_MST_STATUS
garfieldsg 0:662207e34fba 1438 */
garfieldsg 0:662207e34fba 1439 bool MPU6050::getSlave0Nack()
garfieldsg 0:662207e34fba 1440 {
garfieldsg 0:662207e34fba 1441 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV0_NACK_BIT, buffer);
garfieldsg 0:662207e34fba 1442 return buffer[0];
garfieldsg 0:662207e34fba 1443 }
garfieldsg 0:662207e34fba 1444
garfieldsg 0:662207e34fba 1445 // INT_PIN_CFG register
garfieldsg 0:662207e34fba 1446
garfieldsg 0:662207e34fba 1447 /** Get interrupt logic level mode.
garfieldsg 0:662207e34fba 1448 * Will be set 0 for active-high, 1 for active-low.
garfieldsg 0:662207e34fba 1449 * @return Current interrupt mode (0=active-high, 1=active-low)
garfieldsg 0:662207e34fba 1450 * @see MPU6050_RA_INT_PIN_CFG
garfieldsg 0:662207e34fba 1451 * @see MPU6050_INTCFG_INT_LEVEL_BIT
garfieldsg 0:662207e34fba 1452 */
garfieldsg 0:662207e34fba 1453 bool MPU6050::getInterruptMode()
garfieldsg 0:662207e34fba 1454 {
garfieldsg 0:662207e34fba 1455 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_LEVEL_BIT, buffer);
garfieldsg 0:662207e34fba 1456 return buffer[0];
garfieldsg 0:662207e34fba 1457 }
garfieldsg 0:662207e34fba 1458 /** Set interrupt logic level mode.
garfieldsg 0:662207e34fba 1459 * @param mode New interrupt mode (0=active-high, 1=active-low)
garfieldsg 0:662207e34fba 1460 * @see getInterruptMode()
garfieldsg 0:662207e34fba 1461 * @see MPU6050_RA_INT_PIN_CFG
garfieldsg 0:662207e34fba 1462 * @see MPU6050_INTCFG_INT_LEVEL_BIT
garfieldsg 0:662207e34fba 1463 */
garfieldsg 0:662207e34fba 1464 void MPU6050::setInterruptMode(bool mode)
garfieldsg 0:662207e34fba 1465 {
garfieldsg 0:662207e34fba 1466 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_LEVEL_BIT, mode);
garfieldsg 0:662207e34fba 1467 }
garfieldsg 0:662207e34fba 1468 /** Get interrupt drive mode.
garfieldsg 0:662207e34fba 1469 * Will be set 0 for push-pull, 1 for open-drain.
garfieldsg 0:662207e34fba 1470 * @return Current interrupt drive mode (0=push-pull, 1=open-drain)
garfieldsg 0:662207e34fba 1471 * @see MPU6050_RA_INT_PIN_CFG
garfieldsg 0:662207e34fba 1472 * @see MPU6050_INTCFG_INT_OPEN_BIT
garfieldsg 0:662207e34fba 1473 */
garfieldsg 0:662207e34fba 1474 bool MPU6050::getInterruptDrive()
garfieldsg 0:662207e34fba 1475 {
garfieldsg 0:662207e34fba 1476 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_OPEN_BIT, buffer);
garfieldsg 0:662207e34fba 1477 return buffer[0];
garfieldsg 0:662207e34fba 1478 }
garfieldsg 0:662207e34fba 1479 /** Set interrupt drive mode.
garfieldsg 0:662207e34fba 1480 * @param drive New interrupt drive mode (0=push-pull, 1=open-drain)
garfieldsg 0:662207e34fba 1481 * @see getInterruptDrive()
garfieldsg 0:662207e34fba 1482 * @see MPU6050_RA_INT_PIN_CFG
garfieldsg 0:662207e34fba 1483 * @see MPU6050_INTCFG_INT_OPEN_BIT
garfieldsg 0:662207e34fba 1484 */
garfieldsg 0:662207e34fba 1485 void MPU6050::setInterruptDrive(bool drive)
garfieldsg 0:662207e34fba 1486 {
garfieldsg 0:662207e34fba 1487 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_OPEN_BIT, drive);
garfieldsg 0:662207e34fba 1488 }
garfieldsg 0:662207e34fba 1489 /** Get interrupt latch mode.
garfieldsg 0:662207e34fba 1490 * Will be set 0 for 50us-pulse, 1 for latch-until-int-cleared.
garfieldsg 0:662207e34fba 1491 * @return Current latch mode (0=50us-pulse, 1=latch-until-int-cleared)
garfieldsg 0:662207e34fba 1492 * @see MPU6050_RA_INT_PIN_CFG
garfieldsg 0:662207e34fba 1493 * @see MPU6050_INTCFG_LATCH_INT_EN_BIT
garfieldsg 0:662207e34fba 1494 */
garfieldsg 0:662207e34fba 1495 bool MPU6050::getInterruptLatch()
garfieldsg 0:662207e34fba 1496 {
garfieldsg 0:662207e34fba 1497 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_LATCH_INT_EN_BIT, buffer);
garfieldsg 0:662207e34fba 1498 return buffer[0];
garfieldsg 0:662207e34fba 1499 }
garfieldsg 0:662207e34fba 1500 /** Set interrupt latch mode.
garfieldsg 0:662207e34fba 1501 * @param latch New latch mode (0=50us-pulse, 1=latch-until-int-cleared)
garfieldsg 0:662207e34fba 1502 * @see getInterruptLatch()
garfieldsg 0:662207e34fba 1503 * @see MPU6050_RA_INT_PIN_CFG
garfieldsg 0:662207e34fba 1504 * @see MPU6050_INTCFG_LATCH_INT_EN_BIT
garfieldsg 0:662207e34fba 1505 */
garfieldsg 0:662207e34fba 1506 void MPU6050::setInterruptLatch(bool latch)
garfieldsg 0:662207e34fba 1507 {
garfieldsg 0:662207e34fba 1508 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_LATCH_INT_EN_BIT, latch);
garfieldsg 0:662207e34fba 1509 }
garfieldsg 0:662207e34fba 1510 /** Get interrupt latch clear mode.
garfieldsg 0:662207e34fba 1511 * Will be set 0 for status-read-only, 1 for any-register-read.
garfieldsg 0:662207e34fba 1512 * @return Current latch clear mode (0=status-read-only, 1=any-register-read)
garfieldsg 0:662207e34fba 1513 * @see MPU6050_RA_INT_PIN_CFG
garfieldsg 0:662207e34fba 1514 * @see MPU6050_INTCFG_INT_RD_CLEAR_BIT
garfieldsg 0:662207e34fba 1515 */
garfieldsg 0:662207e34fba 1516 bool MPU6050::getInterruptLatchClear()
garfieldsg 0:662207e34fba 1517 {
garfieldsg 0:662207e34fba 1518 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_RD_CLEAR_BIT, buffer);
garfieldsg 0:662207e34fba 1519 return buffer[0];
garfieldsg 0:662207e34fba 1520 }
garfieldsg 0:662207e34fba 1521 /** Set interrupt latch clear mode.
garfieldsg 0:662207e34fba 1522 * @param clear New latch clear mode (0=status-read-only, 1=any-register-read)
garfieldsg 0:662207e34fba 1523 * @see getInterruptLatchClear()
garfieldsg 0:662207e34fba 1524 * @see MPU6050_RA_INT_PIN_CFG
garfieldsg 0:662207e34fba 1525 * @see MPU6050_INTCFG_INT_RD_CLEAR_BIT
garfieldsg 0:662207e34fba 1526 */
garfieldsg 0:662207e34fba 1527 void MPU6050::setInterruptLatchClear(bool clear)
garfieldsg 0:662207e34fba 1528 {
garfieldsg 0:662207e34fba 1529 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_RD_CLEAR_BIT, clear);
garfieldsg 0:662207e34fba 1530 }
garfieldsg 0:662207e34fba 1531 /** Get FSYNC interrupt logic level mode.
garfieldsg 0:662207e34fba 1532 * @return Current FSYNC interrupt mode (0=active-high, 1=active-low)
garfieldsg 0:662207e34fba 1533 * @see getFSyncInterruptMode()
garfieldsg 0:662207e34fba 1534 * @see MPU6050_RA_INT_PIN_CFG
garfieldsg 0:662207e34fba 1535 * @see MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT
garfieldsg 0:662207e34fba 1536 */
garfieldsg 0:662207e34fba 1537 bool MPU6050::getFSyncInterruptLevel()
garfieldsg 0:662207e34fba 1538 {
garfieldsg 0:662207e34fba 1539 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT, buffer);
garfieldsg 0:662207e34fba 1540 return buffer[0];
garfieldsg 0:662207e34fba 1541 }
garfieldsg 0:662207e34fba 1542 /** Set FSYNC interrupt logic level mode.
garfieldsg 0:662207e34fba 1543 * @param mode New FSYNC interrupt mode (0=active-high, 1=active-low)
garfieldsg 0:662207e34fba 1544 * @see getFSyncInterruptMode()
garfieldsg 0:662207e34fba 1545 * @see MPU6050_RA_INT_PIN_CFG
garfieldsg 0:662207e34fba 1546 * @see MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT
garfieldsg 0:662207e34fba 1547 */
garfieldsg 0:662207e34fba 1548 void MPU6050::setFSyncInterruptLevel(bool level)
garfieldsg 0:662207e34fba 1549 {
garfieldsg 0:662207e34fba 1550 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT, level);
garfieldsg 0:662207e34fba 1551 }
garfieldsg 0:662207e34fba 1552 /** Get FSYNC pin interrupt enabled setting.
garfieldsg 0:662207e34fba 1553 * Will be set 0 for disabled, 1 for enabled.
garfieldsg 0:662207e34fba 1554 * @return Current interrupt enabled setting
garfieldsg 0:662207e34fba 1555 * @see MPU6050_RA_INT_PIN_CFG
garfieldsg 0:662207e34fba 1556 * @see MPU6050_INTCFG_FSYNC_INT_EN_BIT
garfieldsg 0:662207e34fba 1557 */
garfieldsg 0:662207e34fba 1558 bool MPU6050::getFSyncInterruptEnabled()
garfieldsg 0:662207e34fba 1559 {
garfieldsg 0:662207e34fba 1560 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_EN_BIT, buffer);
garfieldsg 0:662207e34fba 1561 return buffer[0];
garfieldsg 0:662207e34fba 1562 }
garfieldsg 0:662207e34fba 1563 /** Set FSYNC pin interrupt enabled setting.
garfieldsg 0:662207e34fba 1564 * @param enabled New FSYNC pin interrupt enabled setting
garfieldsg 0:662207e34fba 1565 * @see getFSyncInterruptEnabled()
garfieldsg 0:662207e34fba 1566 * @see MPU6050_RA_INT_PIN_CFG
garfieldsg 0:662207e34fba 1567 * @see MPU6050_INTCFG_FSYNC_INT_EN_BIT
garfieldsg 0:662207e34fba 1568 */
garfieldsg 0:662207e34fba 1569 void MPU6050::setFSyncInterruptEnabled(bool enabled)
garfieldsg 0:662207e34fba 1570 {
garfieldsg 0:662207e34fba 1571 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_EN_BIT, enabled);
garfieldsg 0:662207e34fba 1572 }
garfieldsg 0:662207e34fba 1573 /** Get I2C bypass enabled status.
garfieldsg 0:662207e34fba 1574 * When this bit is equal to 1 and I2C_MST_EN (Register 106 bit[5]) is equal to
garfieldsg 0:662207e34fba 1575 * 0, the host application processor will be able to directly access the
garfieldsg 0:662207e34fba 1576 * auxiliary I2C bus of the MPU-60X0. When this bit is equal to 0, the host
garfieldsg 0:662207e34fba 1577 * application processor will not be able to directly access the auxiliary I2C
garfieldsg 0:662207e34fba 1578 * bus of the MPU-60X0 regardless of the state of I2C_MST_EN (Register 106
garfieldsg 0:662207e34fba 1579 * bit[5]).
garfieldsg 0:662207e34fba 1580 * @return Current I2C bypass enabled status
garfieldsg 0:662207e34fba 1581 * @see MPU6050_RA_INT_PIN_CFG
garfieldsg 0:662207e34fba 1582 * @see MPU6050_INTCFG_I2C_BYPASS_EN_BIT
garfieldsg 0:662207e34fba 1583 */
garfieldsg 0:662207e34fba 1584 bool MPU6050::getI2CBypassEnabled()
garfieldsg 0:662207e34fba 1585 {
garfieldsg 0:662207e34fba 1586 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_I2C_BYPASS_EN_BIT, buffer);
garfieldsg 0:662207e34fba 1587 return buffer[0];
garfieldsg 0:662207e34fba 1588 }
garfieldsg 0:662207e34fba 1589 /** Set I2C bypass enabled status.
garfieldsg 0:662207e34fba 1590 * When this bit is equal to 1 and I2C_MST_EN (Register 106 bit[5]) is equal to
garfieldsg 0:662207e34fba 1591 * 0, the host application processor will be able to directly access the
garfieldsg 0:662207e34fba 1592 * auxiliary I2C bus of the MPU-60X0. When this bit is equal to 0, the host
garfieldsg 0:662207e34fba 1593 * application processor will not be able to directly access the auxiliary I2C
garfieldsg 0:662207e34fba 1594 * bus of the MPU-60X0 regardless of the state of I2C_MST_EN (Register 106
garfieldsg 0:662207e34fba 1595 * bit[5]).
garfieldsg 0:662207e34fba 1596 * @param enabled New I2C bypass enabled status
garfieldsg 0:662207e34fba 1597 * @see MPU6050_RA_INT_PIN_CFG
garfieldsg 0:662207e34fba 1598 * @see MPU6050_INTCFG_I2C_BYPASS_EN_BIT
garfieldsg 0:662207e34fba 1599 */
garfieldsg 0:662207e34fba 1600 void MPU6050::setI2CBypassEnabled(bool enabled)
garfieldsg 0:662207e34fba 1601 {
garfieldsg 0:662207e34fba 1602 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_I2C_BYPASS_EN_BIT, enabled);
garfieldsg 0:662207e34fba 1603 }
garfieldsg 0:662207e34fba 1604 /** Get reference clock output enabled status.
garfieldsg 0:662207e34fba 1605 * When this bit is equal to 1, a reference clock output is provided at the
garfieldsg 0:662207e34fba 1606 * CLKOUT pin. When this bit is equal to 0, the clock output is disabled. For
garfieldsg 0:662207e34fba 1607 * further information regarding CLKOUT, please refer to the MPU-60X0 Product
garfieldsg 0:662207e34fba 1608 * Specification document.
garfieldsg 0:662207e34fba 1609 * @return Current reference clock output enabled status
garfieldsg 0:662207e34fba 1610 * @see MPU6050_RA_INT_PIN_CFG
garfieldsg 0:662207e34fba 1611 * @see MPU6050_INTCFG_CLKOUT_EN_BIT
garfieldsg 0:662207e34fba 1612 */
garfieldsg 0:662207e34fba 1613 bool MPU6050::getClockOutputEnabled()
garfieldsg 0:662207e34fba 1614 {
garfieldsg 0:662207e34fba 1615 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_CLKOUT_EN_BIT, buffer);
garfieldsg 0:662207e34fba 1616 return buffer[0];
garfieldsg 0:662207e34fba 1617 }
garfieldsg 0:662207e34fba 1618 /** Set reference clock output enabled status.
garfieldsg 0:662207e34fba 1619 * When this bit is equal to 1, a reference clock output is provided at the
garfieldsg 0:662207e34fba 1620 * CLKOUT pin. When this bit is equal to 0, the clock output is disabled. For
garfieldsg 0:662207e34fba 1621 * further information regarding CLKOUT, please refer to the MPU-60X0 Product
garfieldsg 0:662207e34fba 1622 * Specification document.
garfieldsg 0:662207e34fba 1623 * @param enabled New reference clock output enabled status
garfieldsg 0:662207e34fba 1624 * @see MPU6050_RA_INT_PIN_CFG
garfieldsg 0:662207e34fba 1625 * @see MPU6050_INTCFG_CLKOUT_EN_BIT
garfieldsg 0:662207e34fba 1626 */
garfieldsg 0:662207e34fba 1627 void MPU6050::setClockOutputEnabled(bool enabled)
garfieldsg 0:662207e34fba 1628 {
garfieldsg 0:662207e34fba 1629 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_CLKOUT_EN_BIT, enabled);
garfieldsg 0:662207e34fba 1630 }
garfieldsg 0:662207e34fba 1631
garfieldsg 0:662207e34fba 1632 // INT_ENABLE register
garfieldsg 0:662207e34fba 1633
garfieldsg 0:662207e34fba 1634 /** Get full interrupt enabled status.
garfieldsg 0:662207e34fba 1635 * Full register byte for all interrupts, for quick reading. Each bit will be
garfieldsg 0:662207e34fba 1636 * set 0 for disabled, 1 for enabled.
garfieldsg 0:662207e34fba 1637 * @return Current interrupt enabled status
garfieldsg 0:662207e34fba 1638 * @see MPU6050_RA_INT_ENABLE
garfieldsg 0:662207e34fba 1639 * @see MPU6050_INTERRUPT_FF_BIT
garfieldsg 0:662207e34fba 1640 **/
garfieldsg 0:662207e34fba 1641 uint8_t MPU6050::getIntEnabled()
garfieldsg 0:662207e34fba 1642 {
garfieldsg 0:662207e34fba 1643 i2Cdev.readByte(devAddr, MPU6050_RA_INT_ENABLE, buffer);
garfieldsg 0:662207e34fba 1644 return buffer[0];
garfieldsg 0:662207e34fba 1645 }
garfieldsg 0:662207e34fba 1646 /** Set full interrupt enabled status.
garfieldsg 0:662207e34fba 1647 * Full register byte for all interrupts, for quick reading. Each bit should be
garfieldsg 0:662207e34fba 1648 * set 0 for disabled, 1 for enabled.
garfieldsg 0:662207e34fba 1649 * @param enabled New interrupt enabled status
garfieldsg 0:662207e34fba 1650 * @see getIntFreefallEnabled()
garfieldsg 0:662207e34fba 1651 * @see MPU6050_RA_INT_ENABLE
garfieldsg 0:662207e34fba 1652 * @see MPU6050_INTERRUPT_FF_BIT
garfieldsg 0:662207e34fba 1653 **/
garfieldsg 0:662207e34fba 1654 void MPU6050::setIntEnabled(uint8_t enabled)
garfieldsg 0:662207e34fba 1655 {
garfieldsg 0:662207e34fba 1656 i2Cdev.writeByte(devAddr, MPU6050_RA_INT_ENABLE, enabled);
garfieldsg 0:662207e34fba 1657 }
garfieldsg 0:662207e34fba 1658 /** Get Free Fall interrupt enabled status.
garfieldsg 0:662207e34fba 1659 * Will be set 0 for disabled, 1 for enabled.
garfieldsg 0:662207e34fba 1660 * @return Current interrupt enabled status
garfieldsg 0:662207e34fba 1661 * @see MPU6050_RA_INT_ENABLE
garfieldsg 0:662207e34fba 1662 * @see MPU6050_INTERRUPT_FF_BIT
garfieldsg 0:662207e34fba 1663 **/
garfieldsg 0:662207e34fba 1664 bool MPU6050::getIntFreefallEnabled()
garfieldsg 0:662207e34fba 1665 {
garfieldsg 0:662207e34fba 1666 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FF_BIT, buffer);
garfieldsg 0:662207e34fba 1667 return buffer[0];
garfieldsg 0:662207e34fba 1668 }
garfieldsg 0:662207e34fba 1669 /** Set Free Fall interrupt enabled status.
garfieldsg 0:662207e34fba 1670 * @param enabled New interrupt enabled status
garfieldsg 0:662207e34fba 1671 * @see getIntFreefallEnabled()
garfieldsg 0:662207e34fba 1672 * @see MPU6050_RA_INT_ENABLE
garfieldsg 0:662207e34fba 1673 * @see MPU6050_INTERRUPT_FF_BIT
garfieldsg 0:662207e34fba 1674 **/
garfieldsg 0:662207e34fba 1675 void MPU6050::setIntFreefallEnabled(bool enabled)
garfieldsg 0:662207e34fba 1676 {
garfieldsg 0:662207e34fba 1677 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FF_BIT, enabled);
garfieldsg 0:662207e34fba 1678 }
garfieldsg 0:662207e34fba 1679 /** Get Motion Detection interrupt enabled status.
garfieldsg 0:662207e34fba 1680 * Will be set 0 for disabled, 1 for enabled.
garfieldsg 0:662207e34fba 1681 * @return Current interrupt enabled status
garfieldsg 0:662207e34fba 1682 * @see MPU6050_RA_INT_ENABLE
garfieldsg 0:662207e34fba 1683 * @see MPU6050_INTERRUPT_MOT_BIT
garfieldsg 0:662207e34fba 1684 **/
garfieldsg 0:662207e34fba 1685 bool MPU6050::getIntMotionEnabled()
garfieldsg 0:662207e34fba 1686 {
garfieldsg 0:662207e34fba 1687 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_MOT_BIT, buffer);
garfieldsg 0:662207e34fba 1688 return buffer[0];
garfieldsg 0:662207e34fba 1689 }
garfieldsg 0:662207e34fba 1690 /** Set Motion Detection interrupt enabled status.
garfieldsg 0:662207e34fba 1691 * @param enabled New interrupt enabled status
garfieldsg 0:662207e34fba 1692 * @see getIntMotionEnabled()
garfieldsg 0:662207e34fba 1693 * @see MPU6050_RA_INT_ENABLE
garfieldsg 0:662207e34fba 1694 * @see MPU6050_INTERRUPT_MOT_BIT
garfieldsg 0:662207e34fba 1695 **/
garfieldsg 0:662207e34fba 1696 void MPU6050::setIntMotionEnabled(bool enabled)
garfieldsg 0:662207e34fba 1697 {
garfieldsg 0:662207e34fba 1698 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_MOT_BIT, enabled);
garfieldsg 0:662207e34fba 1699 }
garfieldsg 0:662207e34fba 1700 /** Get Zero Motion Detection interrupt enabled status.
garfieldsg 0:662207e34fba 1701 * Will be set 0 for disabled, 1 for enabled.
garfieldsg 0:662207e34fba 1702 * @return Current interrupt enabled status
garfieldsg 0:662207e34fba 1703 * @see MPU6050_RA_INT_ENABLE
garfieldsg 0:662207e34fba 1704 * @see MPU6050_INTERRUPT_ZMOT_BIT
garfieldsg 0:662207e34fba 1705 **/
garfieldsg 0:662207e34fba 1706 bool MPU6050::getIntZeroMotionEnabled()
garfieldsg 0:662207e34fba 1707 {
garfieldsg 0:662207e34fba 1708 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_ZMOT_BIT, buffer);
garfieldsg 0:662207e34fba 1709 return buffer[0];
garfieldsg 0:662207e34fba 1710 }
garfieldsg 0:662207e34fba 1711 /** Set Zero Motion Detection interrupt enabled status.
garfieldsg 0:662207e34fba 1712 * @param enabled New interrupt enabled status
garfieldsg 0:662207e34fba 1713 * @see getIntZeroMotionEnabled()
garfieldsg 0:662207e34fba 1714 * @see MPU6050_RA_INT_ENABLE
garfieldsg 0:662207e34fba 1715 * @see MPU6050_INTERRUPT_ZMOT_BIT
garfieldsg 0:662207e34fba 1716 **/
garfieldsg 0:662207e34fba 1717 void MPU6050::setIntZeroMotionEnabled(bool enabled)
garfieldsg 0:662207e34fba 1718 {
garfieldsg 0:662207e34fba 1719 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_ZMOT_BIT, enabled);
garfieldsg 0:662207e34fba 1720 }
garfieldsg 0:662207e34fba 1721 /** Get FIFO Buffer Overflow interrupt enabled status.
garfieldsg 0:662207e34fba 1722 * Will be set 0 for disabled, 1 for enabled.
garfieldsg 0:662207e34fba 1723 * @return Current interrupt enabled status
garfieldsg 0:662207e34fba 1724 * @see MPU6050_RA_INT_ENABLE
garfieldsg 0:662207e34fba 1725 * @see MPU6050_INTERRUPT_FIFO_OFLOW_BIT
garfieldsg 0:662207e34fba 1726 **/
garfieldsg 0:662207e34fba 1727 bool MPU6050::getIntFIFOBufferOverflowEnabled()
garfieldsg 0:662207e34fba 1728 {
garfieldsg 0:662207e34fba 1729 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FIFO_OFLOW_BIT, buffer);
garfieldsg 0:662207e34fba 1730 return buffer[0];
garfieldsg 0:662207e34fba 1731 }
garfieldsg 0:662207e34fba 1732 /** Set FIFO Buffer Overflow interrupt enabled status.
garfieldsg 0:662207e34fba 1733 * @param enabled New interrupt enabled status
garfieldsg 0:662207e34fba 1734 * @see getIntFIFOBufferOverflowEnabled()
garfieldsg 0:662207e34fba 1735 * @see MPU6050_RA_INT_ENABLE
garfieldsg 0:662207e34fba 1736 * @see MPU6050_INTERRUPT_FIFO_OFLOW_BIT
garfieldsg 0:662207e34fba 1737 **/
garfieldsg 0:662207e34fba 1738 void MPU6050::setIntFIFOBufferOverflowEnabled(bool enabled)
garfieldsg 0:662207e34fba 1739 {
garfieldsg 0:662207e34fba 1740 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FIFO_OFLOW_BIT, enabled);
garfieldsg 0:662207e34fba 1741 }
garfieldsg 0:662207e34fba 1742 /** Get I2C Master interrupt enabled status.
garfieldsg 0:662207e34fba 1743 * This enables any of the I2C Master interrupt sources to generate an
garfieldsg 0:662207e34fba 1744 * interrupt. Will be set 0 for disabled, 1 for enabled.
garfieldsg 0:662207e34fba 1745 * @return Current interrupt enabled status
garfieldsg 0:662207e34fba 1746 * @see MPU6050_RA_INT_ENABLE
garfieldsg 0:662207e34fba 1747 * @see MPU6050_INTERRUPT_I2C_MST_INT_BIT
garfieldsg 0:662207e34fba 1748 **/
garfieldsg 0:662207e34fba 1749 bool MPU6050::getIntI2CMasterEnabled()
garfieldsg 0:662207e34fba 1750 {
garfieldsg 0:662207e34fba 1751 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_I2C_MST_INT_BIT, buffer);
garfieldsg 0:662207e34fba 1752 return buffer[0];
garfieldsg 0:662207e34fba 1753 }
garfieldsg 0:662207e34fba 1754 /** Set I2C Master interrupt enabled status.
garfieldsg 0:662207e34fba 1755 * @param enabled New interrupt enabled status
garfieldsg 0:662207e34fba 1756 * @see getIntI2CMasterEnabled()
garfieldsg 0:662207e34fba 1757 * @see MPU6050_RA_INT_ENABLE
garfieldsg 0:662207e34fba 1758 * @see MPU6050_INTERRUPT_I2C_MST_INT_BIT
garfieldsg 0:662207e34fba 1759 **/
garfieldsg 0:662207e34fba 1760 void MPU6050::setIntI2CMasterEnabled(bool enabled)
garfieldsg 0:662207e34fba 1761 {
garfieldsg 0:662207e34fba 1762 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_I2C_MST_INT_BIT, enabled);
garfieldsg 0:662207e34fba 1763 }
garfieldsg 0:662207e34fba 1764 /** Get Data Ready interrupt enabled setting.
garfieldsg 0:662207e34fba 1765 * This event occurs each time a write operation to all of the sensor registers
garfieldsg 0:662207e34fba 1766 * has been completed. Will be set 0 for disabled, 1 for enabled.
garfieldsg 0:662207e34fba 1767 * @return Current interrupt enabled status
garfieldsg 0:662207e34fba 1768 * @see MPU6050_RA_INT_ENABLE
garfieldsg 0:662207e34fba 1769 * @see MPU6050_INTERRUPT_DATA_RDY_BIT
garfieldsg 0:662207e34fba 1770 */
garfieldsg 0:662207e34fba 1771 bool MPU6050::getIntDataReadyEnabled()
garfieldsg 0:662207e34fba 1772 {
garfieldsg 0:662207e34fba 1773 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_DATA_RDY_BIT, buffer);
garfieldsg 0:662207e34fba 1774 return buffer[0];
garfieldsg 0:662207e34fba 1775 }
garfieldsg 0:662207e34fba 1776 /** Set Data Ready interrupt enabled status.
garfieldsg 0:662207e34fba 1777 * @param enabled New interrupt enabled status
garfieldsg 0:662207e34fba 1778 * @see getIntDataReadyEnabled()
garfieldsg 0:662207e34fba 1779 * @see MPU6050_RA_INT_CFG
garfieldsg 0:662207e34fba 1780 * @see MPU6050_INTERRUPT_DATA_RDY_BIT
garfieldsg 0:662207e34fba 1781 */
garfieldsg 0:662207e34fba 1782 void MPU6050::setIntDataReadyEnabled(bool enabled)
garfieldsg 0:662207e34fba 1783 {
garfieldsg 0:662207e34fba 1784 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_DATA_RDY_BIT, enabled);
garfieldsg 0:662207e34fba 1785 }
garfieldsg 0:662207e34fba 1786
garfieldsg 0:662207e34fba 1787 // INT_STATUS register
garfieldsg 0:662207e34fba 1788
garfieldsg 0:662207e34fba 1789 /** Get full set of interrupt status bits.
garfieldsg 0:662207e34fba 1790 * These bits clear to 0 after the register has been read. Very useful
garfieldsg 0:662207e34fba 1791 * for getting multiple INT statuses, since each single bit read clears
garfieldsg 0:662207e34fba 1792 * all of them because it has to read the whole byte.
garfieldsg 0:662207e34fba 1793 * @return Current interrupt status
garfieldsg 0:662207e34fba 1794 * @see MPU6050_RA_INT_STATUS
garfieldsg 0:662207e34fba 1795 */
garfieldsg 0:662207e34fba 1796 uint8_t MPU6050::getIntStatus()
garfieldsg 0:662207e34fba 1797 {
garfieldsg 0:662207e34fba 1798 i2Cdev.readByte(devAddr, MPU6050_RA_INT_STATUS, buffer);
garfieldsg 0:662207e34fba 1799 return buffer[0];
garfieldsg 0:662207e34fba 1800 }
garfieldsg 0:662207e34fba 1801 /** Get Free Fall interrupt status.
garfieldsg 0:662207e34fba 1802 * This bit automatically sets to 1 when a Free Fall interrupt has been
garfieldsg 0:662207e34fba 1803 * generated. The bit clears to 0 after the register has been read.
garfieldsg 0:662207e34fba 1804 * @return Current interrupt status
garfieldsg 0:662207e34fba 1805 * @see MPU6050_RA_INT_STATUS
garfieldsg 0:662207e34fba 1806 * @see MPU6050_INTERRUPT_FF_BIT
garfieldsg 0:662207e34fba 1807 */
garfieldsg 0:662207e34fba 1808 bool MPU6050::getIntFreefallStatus()
garfieldsg 0:662207e34fba 1809 {
garfieldsg 0:662207e34fba 1810 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_FF_BIT, buffer);
garfieldsg 0:662207e34fba 1811 return buffer[0];
garfieldsg 0:662207e34fba 1812 }
garfieldsg 0:662207e34fba 1813 /** Get Motion Detection interrupt status.
garfieldsg 0:662207e34fba 1814 * This bit automatically sets to 1 when a Motion Detection interrupt has been
garfieldsg 0:662207e34fba 1815 * generated. The bit clears to 0 after the register has been read.
garfieldsg 0:662207e34fba 1816 * @return Current interrupt status
garfieldsg 0:662207e34fba 1817 * @see MPU6050_RA_INT_STATUS
garfieldsg 0:662207e34fba 1818 * @see MPU6050_INTERRUPT_MOT_BIT
garfieldsg 0:662207e34fba 1819 */
garfieldsg 0:662207e34fba 1820 bool MPU6050::getIntMotionStatus()
garfieldsg 0:662207e34fba 1821 {
garfieldsg 0:662207e34fba 1822 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_MOT_BIT, buffer);
garfieldsg 0:662207e34fba 1823 return buffer[0];
garfieldsg 0:662207e34fba 1824 }
garfieldsg 0:662207e34fba 1825 /** Get Zero Motion Detection interrupt status.
garfieldsg 0:662207e34fba 1826 * This bit automatically sets to 1 when a Zero Motion Detection interrupt has
garfieldsg 0:662207e34fba 1827 * been generated. The bit clears to 0 after the register has been read.
garfieldsg 0:662207e34fba 1828 * @return Current interrupt status
garfieldsg 0:662207e34fba 1829 * @see MPU6050_RA_INT_STATUS
garfieldsg 0:662207e34fba 1830 * @see MPU6050_INTERRUPT_ZMOT_BIT
garfieldsg 0:662207e34fba 1831 */
garfieldsg 0:662207e34fba 1832 bool MPU6050::getIntZeroMotionStatus()
garfieldsg 0:662207e34fba 1833 {
garfieldsg 0:662207e34fba 1834 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_ZMOT_BIT, buffer);
garfieldsg 0:662207e34fba 1835 return buffer[0];
garfieldsg 0:662207e34fba 1836 }
garfieldsg 0:662207e34fba 1837 /** Get FIFO Buffer Overflow interrupt status.
garfieldsg 0:662207e34fba 1838 * This bit automatically sets to 1 when a Free Fall interrupt has been
garfieldsg 0:662207e34fba 1839 * generated. The bit clears to 0 after the register has been read.
garfieldsg 0:662207e34fba 1840 * @return Current interrupt status
garfieldsg 0:662207e34fba 1841 * @see MPU6050_RA_INT_STATUS
garfieldsg 0:662207e34fba 1842 * @see MPU6050_INTERRUPT_FIFO_OFLOW_BIT
garfieldsg 0:662207e34fba 1843 */
garfieldsg 0:662207e34fba 1844 bool MPU6050::getIntFIFOBufferOverflowStatus()
garfieldsg 0:662207e34fba 1845 {
garfieldsg 0:662207e34fba 1846 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_FIFO_OFLOW_BIT, buffer);
garfieldsg 0:662207e34fba 1847 return buffer[0];
garfieldsg 0:662207e34fba 1848 }
garfieldsg 0:662207e34fba 1849 /** Get I2C Master interrupt status.
garfieldsg 0:662207e34fba 1850 * This bit automatically sets to 1 when an I2C Master interrupt has been
garfieldsg 0:662207e34fba 1851 * generated. For a list of I2C Master interrupts, please refer to Register 54.
garfieldsg 0:662207e34fba 1852 * The bit clears to 0 after the register has been read.
garfieldsg 0:662207e34fba 1853 * @return Current interrupt status
garfieldsg 0:662207e34fba 1854 * @see MPU6050_RA_INT_STATUS
garfieldsg 0:662207e34fba 1855 * @see MPU6050_INTERRUPT_I2C_MST_INT_BIT
garfieldsg 0:662207e34fba 1856 */
garfieldsg 0:662207e34fba 1857 bool MPU6050::getIntI2CMasterStatus()
garfieldsg 0:662207e34fba 1858 {
garfieldsg 0:662207e34fba 1859 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_I2C_MST_INT_BIT, buffer);
garfieldsg 0:662207e34fba 1860 return buffer[0];
garfieldsg 0:662207e34fba 1861 }
garfieldsg 0:662207e34fba 1862 /** Get Data Ready interrupt status.
garfieldsg 0:662207e34fba 1863 * This bit automatically sets to 1 when a Data Ready interrupt has been
garfieldsg 0:662207e34fba 1864 * generated. The bit clears to 0 after the register has been read.
garfieldsg 0:662207e34fba 1865 * @return Current interrupt status
garfieldsg 0:662207e34fba 1866 * @see MPU6050_RA_INT_STATUS
garfieldsg 0:662207e34fba 1867 * @see MPU6050_INTERRUPT_DATA_RDY_BIT
garfieldsg 0:662207e34fba 1868 */
garfieldsg 0:662207e34fba 1869 bool MPU6050::getIntDataReadyStatus()
garfieldsg 0:662207e34fba 1870 {
garfieldsg 0:662207e34fba 1871 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_DATA_RDY_BIT, buffer);
garfieldsg 0:662207e34fba 1872 return buffer[0];
garfieldsg 0:662207e34fba 1873 }
garfieldsg 0:662207e34fba 1874
garfieldsg 0:662207e34fba 1875 // ACCEL_*OUT_* registers
garfieldsg 0:662207e34fba 1876
garfieldsg 0:662207e34fba 1877 /** Get raw 9-axis motion sensor readings (accel/gyro/compass).
garfieldsg 0:662207e34fba 1878 * FUNCTION NOT FULLY IMPLEMENTED YET.
garfieldsg 0:662207e34fba 1879 * @param ax 16-bit signed integer container for accelerometer X-axis value
garfieldsg 0:662207e34fba 1880 * @param ay 16-bit signed integer container for accelerometer Y-axis value
garfieldsg 0:662207e34fba 1881 * @param az 16-bit signed integer container for accelerometer Z-axis value
garfieldsg 0:662207e34fba 1882 * @param gx 16-bit signed integer container for gyroscope X-axis value
garfieldsg 0:662207e34fba 1883 * @param gy 16-bit signed integer container for gyroscope Y-axis value
garfieldsg 0:662207e34fba 1884 * @param gz 16-bit signed integer container for gyroscope Z-axis value
garfieldsg 0:662207e34fba 1885 * @param mx 16-bit signed integer container for magnetometer X-axis value
garfieldsg 0:662207e34fba 1886 * @param my 16-bit signed integer container for magnetometer Y-axis value
garfieldsg 0:662207e34fba 1887 * @param mz 16-bit signed integer container for magnetometer Z-axis value
garfieldsg 0:662207e34fba 1888 * @see getMotion6()
garfieldsg 0:662207e34fba 1889 * @see getAcceleration()
garfieldsg 0:662207e34fba 1890 * @see getRotation()
garfieldsg 0:662207e34fba 1891 * @see MPU6050_RA_ACCEL_XOUT_H
garfieldsg 0:662207e34fba 1892 */
garfieldsg 0:662207e34fba 1893 void MPU6050::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)
garfieldsg 0:662207e34fba 1894 {
tyftyftyf 17:902d1491455f 1895 #ifdef MPU9250
tyftyftyf 17:902d1491455f 1896 get9250Motion9Counts(ax, ay, az, gx, gy, gz, mx, my, mz);
tyftyftyf 17:902d1491455f 1897 #else
garfieldsg 0:662207e34fba 1898 getMotion6(ax, ay, az, gx, gy, gz);
garfieldsg 0:662207e34fba 1899 // TODO: magnetometer integration
tyftyftyf 17:902d1491455f 1900 #endif
garfieldsg 0:662207e34fba 1901 }
tyftyftyf 9:d879deb55ae1 1902
garfieldsg 0:662207e34fba 1903 /** Get raw 6-axis motion sensor readings (accel/gyro).
garfieldsg 0:662207e34fba 1904 * Retrieves all currently available motion sensor values.
garfieldsg 0:662207e34fba 1905 * @param ax 16-bit signed integer container for accelerometer X-axis value
garfieldsg 0:662207e34fba 1906 * @param ay 16-bit signed integer container for accelerometer Y-axis value
garfieldsg 0:662207e34fba 1907 * @param az 16-bit signed integer container for accelerometer Z-axis value
garfieldsg 0:662207e34fba 1908 * @param gx 16-bit signed integer container for gyroscope X-axis value
garfieldsg 0:662207e34fba 1909 * @param gy 16-bit signed integer container for gyroscope Y-axis value
garfieldsg 0:662207e34fba 1910 * @param gz 16-bit signed integer container for gyroscope Z-axis value
garfieldsg 0:662207e34fba 1911 * @see getAcceleration()
garfieldsg 0:662207e34fba 1912 * @see getRotation()
garfieldsg 0:662207e34fba 1913 * @see MPU6050_RA_ACCEL_XOUT_H
garfieldsg 0:662207e34fba 1914 */
garfieldsg 0:662207e34fba 1915 void MPU6050::getMotion6(int16_t* ax, int16_t* ay, int16_t* az, int16_t* gx, int16_t* gy, int16_t* gz)
garfieldsg 0:662207e34fba 1916 {
tyftyftyf 9:d879deb55ae1 1917 *ax = ax_cache;
tyftyftyf 9:d879deb55ae1 1918 *ay = ay_cache;
tyftyftyf 9:d879deb55ae1 1919 *az = az_cache;
tyftyftyf 9:d879deb55ae1 1920 *gx = gx_cache;
tyftyftyf 9:d879deb55ae1 1921 *gy = gy_cache;
tyftyftyf 9:d879deb55ae1 1922 *gz = gz_cache;
garfieldsg 0:662207e34fba 1923 }
tyftyftyf 9:d879deb55ae1 1924
tyftyftyf 11:9549be34fa7f 1925 /*
tyftyftyf 11:9549be34fa7f 1926 * Populate readings cache when finished reading from I2C
tyftyftyf 11:9549be34fa7f 1927 */
tyftyftyf 9:d879deb55ae1 1928 uint32_t mpureadfin(uint32_t param){
tyftyftyf 9:d879deb55ae1 1929 MPU6050* ins = (MPU6050*)param;
tyftyftyf 13:a74f2d622b54 1930 #ifdef MPU9250
tyftyftyf 9:d879deb55ae1 1931 ins->ax_cache = (((int16_t)ins->mpu_buffer[0]) << 8) | ins->mpu_buffer[1];
tyftyftyf 9:d879deb55ae1 1932 ins->ay_cache = (((int16_t)ins->mpu_buffer[2]) << 8) | ins->mpu_buffer[3];
tyftyftyf 9:d879deb55ae1 1933 ins->az_cache = (((int16_t)ins->mpu_buffer[4]) << 8) | ins->mpu_buffer[5];
tyftyftyf 9:d879deb55ae1 1934 ins->gx_cache = (((int16_t)ins->mpu_buffer[8]) << 8) | ins->mpu_buffer[9];
tyftyftyf 9:d879deb55ae1 1935 ins->gy_cache = (((int16_t)ins->mpu_buffer[10]) << 8) | ins->mpu_buffer[11];
tyftyftyf 9:d879deb55ae1 1936 ins->gz_cache = (((int16_t)ins->mpu_buffer[12]) << 8) | ins->mpu_buffer[13];
tyftyftyf 13:a74f2d622b54 1937 ins->hx_cache = (((int16_t)ins->mpu_buffer[15]) << 8) | ins->mpu_buffer[14];
tyftyftyf 13:a74f2d622b54 1938 ins->hy_cache = (((int16_t)ins->mpu_buffer[17]) << 8) | ins->mpu_buffer[16];
tyftyftyf 13:a74f2d622b54 1939 ins->hz_cache = (((int16_t)ins->mpu_buffer[19]) << 8) | ins->mpu_buffer[18];
tyftyftyf 13:a74f2d622b54 1940 #else
tyftyftyf 13:a74f2d622b54 1941 ins->ax_cache = (((int16_t)ins->mpu_buffer[0]) << 8) | ins->mpu_buffer[1];
tyftyftyf 13:a74f2d622b54 1942 ins->ay_cache = (((int16_t)ins->mpu_buffer[2]) << 8) | ins->mpu_buffer[3];
tyftyftyf 13:a74f2d622b54 1943 ins->az_cache = (((int16_t)ins->mpu_buffer[4]) << 8) | ins->mpu_buffer[5];
tyftyftyf 13:a74f2d622b54 1944 ins->gx_cache = (((int16_t)ins->mpu_buffer[8]) << 8) | ins->mpu_buffer[9];
tyftyftyf 13:a74f2d622b54 1945 ins->gy_cache = (((int16_t)ins->mpu_buffer[10]) << 8) | ins->mpu_buffer[11];
tyftyftyf 13:a74f2d622b54 1946 ins->gz_cache = (((int16_t)ins->mpu_buffer[12]) << 8) | ins->mpu_buffer[13];
tyftyftyf 13:a74f2d622b54 1947 #endif
tyftyftyf 9:d879deb55ae1 1948 return 0;
tyftyftyf 9:d879deb55ae1 1949 }
tyftyftyf 9:d879deb55ae1 1950
tyftyftyf 11:9549be34fa7f 1951 void MPU6050::sample(bool on){
tyftyftyf 11:9549be34fa7f 1952 if (sampling && !on)
tyftyftyf 11:9549be34fa7f 1953 mpu_sampling.detach();
tyftyftyf 11:9549be34fa7f 1954 if (!sampling && on)
tyftyftyf 11:9549be34fa7f 1955 start_sampling();
tyftyftyf 11:9549be34fa7f 1956 sampling = on;
tyftyftyf 11:9549be34fa7f 1957 }
tyftyftyf 11:9549be34fa7f 1958
tyftyftyf 21:ae3ee2d811eb 1959 #ifdef MPU9250
tyftyftyf 21:ae3ee2d811eb 1960 volatile bool magn_valid_9250 = false;
tyftyftyf 21:ae3ee2d811eb 1961 #endif
tyftyftyf 21:ae3ee2d811eb 1962
tyftyftyf 21:ae3ee2d811eb 1963 void MPU6050::mpu_sample_func(){
tyftyftyf 13:a74f2d622b54 1964 #ifdef MPU9250
tyftyftyf 13:a74f2d622b54 1965 i2Cdev.readBytes_nb(devAddr, &mpu_cmd, 21, (uint8_t*)mpu_buffer, &mpureadfin, this);
tyftyftyf 21:ae3ee2d811eb 1966 static uint32_t counter = 0;
tyftyftyf 21:ae3ee2d811eb 1967 counter += 1;
tyftyftyf 21:ae3ee2d811eb 1968 if (counter % 5 == 0) {
tyftyftyf 21:ae3ee2d811eb 1969 magn_valid_9250 = true;
tyftyftyf 21:ae3ee2d811eb 1970 }
tyftyftyf 13:a74f2d622b54 1971 #else
tyftyftyf 9:d879deb55ae1 1972 i2Cdev.readBytes_nb(devAddr, &mpu_cmd, 14, (uint8_t*)mpu_buffer, &mpureadfin, this);
tyftyftyf 13:a74f2d622b54 1973 #endif
tyftyftyf 9:d879deb55ae1 1974 }
tyftyftyf 9:d879deb55ae1 1975
tyftyftyf 9:d879deb55ae1 1976 void MPU6050::start_sampling(){
tyftyftyf 11:9549be34fa7f 1977 sampling = true;
tyftyftyf 9:d879deb55ae1 1978 mpu_cmd = MPU6050_RA_ACCEL_XOUT_H;
tyftyftyf 21:ae3ee2d811eb 1979 // sample at 500hz
tyftyftyf 9:d879deb55ae1 1980 mpu_sampling.attach_us(this, &MPU6050::mpu_sample_func, 2000);
tyftyftyf 9:d879deb55ae1 1981 }
tyftyftyf 9:d879deb55ae1 1982
garfieldsg 0:662207e34fba 1983 /** Get 3-axis accelerometer readings.
garfieldsg 0:662207e34fba 1984 * These registers store the most recent accelerometer measurements.
garfieldsg 0:662207e34fba 1985 * Accelerometer measurements are written to these registers at the Sample Rate
garfieldsg 0:662207e34fba 1986 * as defined in Register 25.
garfieldsg 0:662207e34fba 1987 *
garfieldsg 0:662207e34fba 1988 * The accelerometer measurement registers, along with the temperature
garfieldsg 0:662207e34fba 1989 * measurement registers, gyroscope measurement registers, and external sensor
garfieldsg 0:662207e34fba 1990 * data registers, are composed of two sets of registers: an internal register
garfieldsg 0:662207e34fba 1991 * set and a user-facing read register set.
garfieldsg 0:662207e34fba 1992 *
garfieldsg 0:662207e34fba 1993 * The data within the accelerometer sensors' internal register set is always
garfieldsg 0:662207e34fba 1994 * updated at the Sample Rate. Meanwhile, the user-facing read register set
garfieldsg 0:662207e34fba 1995 * duplicates the internal register set's data values whenever the serial
garfieldsg 0:662207e34fba 1996 * interface is idle. This guarantees that a burst read of sensor registers will
garfieldsg 0:662207e34fba 1997 * read measurements from the same sampling instant. Note that if burst reads
garfieldsg 0:662207e34fba 1998 * are not used, the user is responsible for ensuring a set of single byte reads
garfieldsg 0:662207e34fba 1999 * correspond to a single sampling instant by checking the Data Ready interrupt.
garfieldsg 0:662207e34fba 2000 *
garfieldsg 0:662207e34fba 2001 * Each 16-bit accelerometer measurement has a full scale defined in ACCEL_FS
garfieldsg 0:662207e34fba 2002 * (Register 28). For each full scale setting, the accelerometers' sensitivity
garfieldsg 0:662207e34fba 2003 * per LSB in ACCEL_xOUT is shown in the table below:
garfieldsg 0:662207e34fba 2004 *
garfieldsg 0:662207e34fba 2005 * <pre>
garfieldsg 0:662207e34fba 2006 * AFS_SEL | Full Scale Range | LSB Sensitivity
garfieldsg 0:662207e34fba 2007 * --------+------------------+----------------
garfieldsg 0:662207e34fba 2008 * 0 | +/- 2g | 8192 LSB/mg
garfieldsg 0:662207e34fba 2009 * 1 | +/- 4g | 4096 LSB/mg
garfieldsg 0:662207e34fba 2010 * 2 | +/- 8g | 2048 LSB/mg
garfieldsg 0:662207e34fba 2011 * 3 | +/- 16g | 1024 LSB/mg
garfieldsg 0:662207e34fba 2012 * </pre>
garfieldsg 0:662207e34fba 2013 *
garfieldsg 0:662207e34fba 2014 * @param x 16-bit signed integer container for X-axis acceleration
garfieldsg 0:662207e34fba 2015 * @param y 16-bit signed integer container for Y-axis acceleration
garfieldsg 0:662207e34fba 2016 * @param z 16-bit signed integer container for Z-axis acceleration
garfieldsg 0:662207e34fba 2017 * @see MPU6050_RA_GYRO_XOUT_H
garfieldsg 0:662207e34fba 2018 */
garfieldsg 0:662207e34fba 2019 void MPU6050::getAcceleration(int16_t* x, int16_t* y, int16_t* z)
garfieldsg 0:662207e34fba 2020 {
garfieldsg 0:662207e34fba 2021 i2Cdev.readBytes(devAddr, MPU6050_RA_ACCEL_XOUT_H, 6, buffer);
garfieldsg 0:662207e34fba 2022 *x = (((int16_t)buffer[0]) << 8) | buffer[1];
garfieldsg 0:662207e34fba 2023 *y = (((int16_t)buffer[2]) << 8) | buffer[3];
garfieldsg 0:662207e34fba 2024 *z = (((int16_t)buffer[4]) << 8) | buffer[5];
garfieldsg 0:662207e34fba 2025 }
garfieldsg 0:662207e34fba 2026 /** Get X-axis accelerometer reading.
garfieldsg 0:662207e34fba 2027 * @return X-axis acceleration measurement in 16-bit 2's complement format
garfieldsg 0:662207e34fba 2028 * @see getMotion6()
garfieldsg 0:662207e34fba 2029 * @see MPU6050_RA_ACCEL_XOUT_H
garfieldsg 0:662207e34fba 2030 */
garfieldsg 0:662207e34fba 2031 int16_t MPU6050::getAccelerationX()
garfieldsg 0:662207e34fba 2032 {
garfieldsg 0:662207e34fba 2033 i2Cdev.readBytes(devAddr, MPU6050_RA_ACCEL_XOUT_H, 2, buffer);
garfieldsg 0:662207e34fba 2034 return (((int16_t)buffer[0]) << 8) | buffer[1];
garfieldsg 0:662207e34fba 2035 }
garfieldsg 0:662207e34fba 2036 /** Get Y-axis accelerometer reading.
garfieldsg 0:662207e34fba 2037 * @return Y-axis acceleration measurement in 16-bit 2's complement format
garfieldsg 0:662207e34fba 2038 * @see getMotion6()
garfieldsg 0:662207e34fba 2039 * @see MPU6050_RA_ACCEL_YOUT_H
garfieldsg 0:662207e34fba 2040 */
garfieldsg 0:662207e34fba 2041 int16_t MPU6050::getAccelerationY()
garfieldsg 0:662207e34fba 2042 {
garfieldsg 0:662207e34fba 2043 i2Cdev.readBytes(devAddr, MPU6050_RA_ACCEL_YOUT_H, 2, buffer);
garfieldsg 0:662207e34fba 2044 return (((int16_t)buffer[0]) << 8) | buffer[1];
garfieldsg 0:662207e34fba 2045 }
garfieldsg 0:662207e34fba 2046 /** Get Z-axis accelerometer reading.
garfieldsg 0:662207e34fba 2047 * @return Z-axis acceleration measurement in 16-bit 2's complement format
garfieldsg 0:662207e34fba 2048 * @see getMotion6()
garfieldsg 0:662207e34fba 2049 * @see MPU6050_RA_ACCEL_ZOUT_H
garfieldsg 0:662207e34fba 2050 */
garfieldsg 0:662207e34fba 2051 int16_t MPU6050::getAccelerationZ()
garfieldsg 0:662207e34fba 2052 {
garfieldsg 0:662207e34fba 2053 i2Cdev.readBytes(devAddr, MPU6050_RA_ACCEL_ZOUT_H, 2, buffer);
garfieldsg 0:662207e34fba 2054 return (((int16_t)buffer[0]) << 8) | buffer[1];
garfieldsg 0:662207e34fba 2055 }
garfieldsg 0:662207e34fba 2056
garfieldsg 0:662207e34fba 2057 // TEMP_OUT_* registers
garfieldsg 0:662207e34fba 2058
garfieldsg 0:662207e34fba 2059 /** Get current internal temperature.
garfieldsg 0:662207e34fba 2060 * @return Temperature reading in 16-bit 2's complement format
garfieldsg 0:662207e34fba 2061 * @see MPU6050_RA_TEMP_OUT_H
garfieldsg 0:662207e34fba 2062 */
garfieldsg 0:662207e34fba 2063 int16_t MPU6050::getTemperature()
garfieldsg 0:662207e34fba 2064 {
garfieldsg 0:662207e34fba 2065 i2Cdev.readBytes(devAddr, MPU6050_RA_TEMP_OUT_H, 2, buffer);
garfieldsg 0:662207e34fba 2066 return (((int16_t)buffer[0]) << 8) | buffer[1];
garfieldsg 0:662207e34fba 2067 }
garfieldsg 0:662207e34fba 2068
garfieldsg 0:662207e34fba 2069 // GYRO_*OUT_* registers
garfieldsg 0:662207e34fba 2070
garfieldsg 0:662207e34fba 2071 /** Get 3-axis gyroscope readings.
garfieldsg 0:662207e34fba 2072 * These gyroscope measurement registers, along with the accelerometer
garfieldsg 0:662207e34fba 2073 * measurement registers, temperature measurement registers, and external sensor
garfieldsg 0:662207e34fba 2074 * data registers, are composed of two sets of registers: an internal register
garfieldsg 0:662207e34fba 2075 * set and a user-facing read register set.
garfieldsg 0:662207e34fba 2076 * The data within the gyroscope sensors' internal register set is always
garfieldsg 0:662207e34fba 2077 * updated at the Sample Rate. Meanwhile, the user-facing read register set
garfieldsg 0:662207e34fba 2078 * duplicates the internal register set's data values whenever the serial
garfieldsg 0:662207e34fba 2079 * interface is idle. This guarantees that a burst read of sensor registers will
garfieldsg 0:662207e34fba 2080 * read measurements from the same sampling instant. Note that if burst reads
garfieldsg 0:662207e34fba 2081 * are not used, the user is responsible for ensuring a set of single byte reads
garfieldsg 0:662207e34fba 2082 * correspond to a single sampling instant by checking the Data Ready interrupt.
garfieldsg 0:662207e34fba 2083 *
garfieldsg 0:662207e34fba 2084 * Each 16-bit gyroscope measurement has a full scale defined in FS_SEL
garfieldsg 0:662207e34fba 2085 * (Register 27). For each full scale setting, the gyroscopes' sensitivity per
garfieldsg 0:662207e34fba 2086 * LSB in GYRO_xOUT is shown in the table below:
garfieldsg 0:662207e34fba 2087 *
garfieldsg 0:662207e34fba 2088 * <pre>
garfieldsg 0:662207e34fba 2089 * FS_SEL | Full Scale Range | LSB Sensitivity
garfieldsg 0:662207e34fba 2090 * -------+--------------------+----------------
garfieldsg 0:662207e34fba 2091 * 0 | +/- 250 degrees/s | 131 LSB/deg/s
garfieldsg 0:662207e34fba 2092 * 1 | +/- 500 degrees/s | 65.5 LSB/deg/s
garfieldsg 0:662207e34fba 2093 * 2 | +/- 1000 degrees/s | 32.8 LSB/deg/s
garfieldsg 0:662207e34fba 2094 * 3 | +/- 2000 degrees/s | 16.4 LSB/deg/s
garfieldsg 0:662207e34fba 2095 * </pre>
garfieldsg 0:662207e34fba 2096 *
garfieldsg 0:662207e34fba 2097 * @param x 16-bit signed integer container for X-axis rotation
garfieldsg 0:662207e34fba 2098 * @param y 16-bit signed integer container for Y-axis rotation
garfieldsg 0:662207e34fba 2099 * @param z 16-bit signed integer container for Z-axis rotation
garfieldsg 0:662207e34fba 2100 * @see getMotion6()
garfieldsg 0:662207e34fba 2101 * @see MPU6050_RA_GYRO_XOUT_H
garfieldsg 0:662207e34fba 2102 */
garfieldsg 0:662207e34fba 2103 void MPU6050::getRotation(int16_t* x, int16_t* y, int16_t* z)
garfieldsg 0:662207e34fba 2104 {
garfieldsg 0:662207e34fba 2105 i2Cdev.readBytes(devAddr, MPU6050_RA_GYRO_XOUT_H, 6, buffer);
garfieldsg 0:662207e34fba 2106 *x = (((int16_t)buffer[0]) << 8) | buffer[1];
garfieldsg 0:662207e34fba 2107 *y = (((int16_t)buffer[2]) << 8) | buffer[3];
garfieldsg 0:662207e34fba 2108 *z = (((int16_t)buffer[4]) << 8) | buffer[5];
garfieldsg 0:662207e34fba 2109 }
garfieldsg 0:662207e34fba 2110 /** Get X-axis gyroscope reading.
garfieldsg 0:662207e34fba 2111 * @return X-axis rotation measurement in 16-bit 2's complement format
garfieldsg 0:662207e34fba 2112 * @see getMotion6()
garfieldsg 0:662207e34fba 2113 * @see MPU6050_RA_GYRO_XOUT_H
garfieldsg 0:662207e34fba 2114 */
garfieldsg 0:662207e34fba 2115 int16_t MPU6050::getRotationX()
garfieldsg 0:662207e34fba 2116 {
garfieldsg 0:662207e34fba 2117 i2Cdev.readBytes(devAddr, MPU6050_RA_GYRO_XOUT_H, 2, buffer);
garfieldsg 0:662207e34fba 2118 return (((int16_t)buffer[0]) << 8) | buffer[1];
garfieldsg 0:662207e34fba 2119 }
garfieldsg 0:662207e34fba 2120 /** Get Y-axis gyroscope reading.
garfieldsg 0:662207e34fba 2121 * @return Y-axis rotation measurement in 16-bit 2's complement format
garfieldsg 0:662207e34fba 2122 * @see getMotion6()
garfieldsg 0:662207e34fba 2123 * @see MPU6050_RA_GYRO_YOUT_H
garfieldsg 0:662207e34fba 2124 */
garfieldsg 0:662207e34fba 2125 int16_t MPU6050::getRotationY()
garfieldsg 0:662207e34fba 2126 {
garfieldsg 0:662207e34fba 2127 i2Cdev.readBytes(devAddr, MPU6050_RA_GYRO_YOUT_H, 2, buffer);
garfieldsg 0:662207e34fba 2128 return (((int16_t)buffer[0]) << 8) | buffer[1];
garfieldsg 0:662207e34fba 2129 }
garfieldsg 0:662207e34fba 2130 /** Get Z-axis gyroscope reading.
garfieldsg 0:662207e34fba 2131 * @return Z-axis rotation measurement in 16-bit 2's complement format
garfieldsg 0:662207e34fba 2132 * @see getMotion6()
garfieldsg 0:662207e34fba 2133 * @see MPU6050_RA_GYRO_ZOUT_H
garfieldsg 0:662207e34fba 2134 */
garfieldsg 0:662207e34fba 2135 int16_t MPU6050::getRotationZ()
garfieldsg 0:662207e34fba 2136 {
garfieldsg 0:662207e34fba 2137 i2Cdev.readBytes(devAddr, MPU6050_RA_GYRO_ZOUT_H, 2, buffer);
garfieldsg 0:662207e34fba 2138 return (((int16_t)buffer[0]) << 8) | buffer[1];
garfieldsg 0:662207e34fba 2139 }
garfieldsg 0:662207e34fba 2140
garfieldsg 0:662207e34fba 2141 // EXT_SENS_DATA_* registers
garfieldsg 0:662207e34fba 2142
garfieldsg 0:662207e34fba 2143 /** Read single byte from external sensor data register.
garfieldsg 0:662207e34fba 2144 * These registers store data read from external sensors by the Slave 0, 1, 2,
garfieldsg 0:662207e34fba 2145 * and 3 on the auxiliary I2C interface. Data read by Slave 4 is stored in
garfieldsg 0:662207e34fba 2146 * I2C_SLV4_DI (Register 53).
garfieldsg 0:662207e34fba 2147 *
garfieldsg 0:662207e34fba 2148 * External sensor data is written to these registers at the Sample Rate as
garfieldsg 0:662207e34fba 2149 * defined in Register 25. This access rate can be reduced by using the Slave
garfieldsg 0:662207e34fba 2150 * Delay Enable registers (Register 103).
garfieldsg 0:662207e34fba 2151 *
garfieldsg 0:662207e34fba 2152 * External sensor data registers, along with the gyroscope measurement
garfieldsg 0:662207e34fba 2153 * registers, accelerometer measurement registers, and temperature measurement
garfieldsg 0:662207e34fba 2154 * registers, are composed of two sets of registers: an internal register set
garfieldsg 0:662207e34fba 2155 * and a user-facing read register set.
garfieldsg 0:662207e34fba 2156 *
garfieldsg 0:662207e34fba 2157 * The data within the external sensors' internal register set is always updated
garfieldsg 0:662207e34fba 2158 * at the Sample Rate (or the reduced access rate) whenever the serial interface
garfieldsg 0:662207e34fba 2159 * is idle. This guarantees that a burst read of sensor registers will read
garfieldsg 0:662207e34fba 2160 * measurements from the same sampling instant. Note that if burst reads are not
garfieldsg 0:662207e34fba 2161 * used, the user is responsible for ensuring a set of single byte reads
garfieldsg 0:662207e34fba 2162 * correspond to a single sampling instant by checking the Data Ready interrupt.
garfieldsg 0:662207e34fba 2163 *
garfieldsg 0:662207e34fba 2164 * Data is placed in these external sensor data registers according to
garfieldsg 0:662207e34fba 2165 * I2C_SLV0_CTRL, I2C_SLV1_CTRL, I2C_SLV2_CTRL, and I2C_SLV3_CTRL (Registers 39,
garfieldsg 0:662207e34fba 2166 * 42, 45, and 48). When more than zero bytes are read (I2C_SLVx_LEN > 0) from
garfieldsg 0:662207e34fba 2167 * an enabled slave (I2C_SLVx_EN = 1), the slave is read at the Sample Rate (as
garfieldsg 0:662207e34fba 2168 * defined in Register 25) or delayed rate (if specified in Register 52 and
garfieldsg 0:662207e34fba 2169 * 103). During each Sample cycle, slave reads are performed in order of Slave
garfieldsg 0:662207e34fba 2170 * number. If all slaves are enabled with more than zero bytes to be read, the
garfieldsg 0:662207e34fba 2171 * order will be Slave 0, followed by Slave 1, Slave 2, and Slave 3.
garfieldsg 0:662207e34fba 2172 *
garfieldsg 0:662207e34fba 2173 * Each enabled slave will have EXT_SENS_DATA registers associated with it by
garfieldsg 0:662207e34fba 2174 * number of bytes read (I2C_SLVx_LEN) in order of slave number, starting from
garfieldsg 0:662207e34fba 2175 * EXT_SENS_DATA_00. Note that this means enabling or disabling a slave may
garfieldsg 0:662207e34fba 2176 * change the higher numbered slaves' associated registers. Furthermore, if
garfieldsg 0:662207e34fba 2177 * fewer total bytes are being read from the external sensors as a result of
garfieldsg 0:662207e34fba 2178 * such a change, then the data remaining in the registers which no longer have
garfieldsg 0:662207e34fba 2179 * an associated slave device (i.e. high numbered registers) will remain in
garfieldsg 0:662207e34fba 2180 * these previously allocated registers unless reset.
garfieldsg 0:662207e34fba 2181 *
garfieldsg 0:662207e34fba 2182 * If the sum of the read lengths of all SLVx transactions exceed the number of
garfieldsg 0:662207e34fba 2183 * available EXT_SENS_DATA registers, the excess bytes will be dropped. There
garfieldsg 0:662207e34fba 2184 * are 24 EXT_SENS_DATA registers and hence the total read lengths between all
garfieldsg 0:662207e34fba 2185 * the slaves cannot be greater than 24 or some bytes will be lost.
garfieldsg 0:662207e34fba 2186 *
garfieldsg 0:662207e34fba 2187 * Note: Slave 4's behavior is distinct from that of Slaves 0-3. For further
garfieldsg 0:662207e34fba 2188 * information regarding the characteristics of Slave 4, please refer to
garfieldsg 0:662207e34fba 2189 * Registers 49 to 53.
garfieldsg 0:662207e34fba 2190 *
garfieldsg 0:662207e34fba 2191 * EXAMPLE:
garfieldsg 0:662207e34fba 2192 * Suppose that Slave 0 is enabled with 4 bytes to be read (I2C_SLV0_EN = 1 and
garfieldsg 0:662207e34fba 2193 * I2C_SLV0_LEN = 4) while Slave 1 is enabled with 2 bytes to be read so that
garfieldsg 0:662207e34fba 2194 * I2C_SLV1_EN = 1 and I2C_SLV1_LEN = 2. In such a situation, EXT_SENS_DATA _00
garfieldsg 0:662207e34fba 2195 * through _03 will be associated with Slave 0, while EXT_SENS_DATA _04 and 05
garfieldsg 0:662207e34fba 2196 * will be associated with Slave 1. If Slave 2 is enabled as well, registers
garfieldsg 0:662207e34fba 2197 * starting from EXT_SENS_DATA_06 will be allocated to Slave 2.
garfieldsg 0:662207e34fba 2198 *
garfieldsg 0:662207e34fba 2199 * If Slave 2 is disabled while Slave 3 is enabled in this same situation, then
garfieldsg 0:662207e34fba 2200 * registers starting from EXT_SENS_DATA_06 will be allocated to Slave 3
garfieldsg 0:662207e34fba 2201 * instead.
garfieldsg 0:662207e34fba 2202 *
garfieldsg 0:662207e34fba 2203 * REGISTER ALLOCATION FOR DYNAMIC DISABLE VS. NORMAL DISABLE:
garfieldsg 0:662207e34fba 2204 * If a slave is disabled at any time, the space initially allocated to the
garfieldsg 0:662207e34fba 2205 * slave in the EXT_SENS_DATA register, will remain associated with that slave.
garfieldsg 0:662207e34fba 2206 * This is to avoid dynamic adjustment of the register allocation.
garfieldsg 0:662207e34fba 2207 *
garfieldsg 0:662207e34fba 2208 * The allocation of the EXT_SENS_DATA registers is recomputed only when (1) all
garfieldsg 0:662207e34fba 2209 * slaves are disabled, or (2) the I2C_MST_RST bit is set (Register 106).
garfieldsg 0:662207e34fba 2210 *
garfieldsg 0:662207e34fba 2211 * This above is also true if one of the slaves gets NACKed and stops
garfieldsg 0:662207e34fba 2212 * functioning.
garfieldsg 0:662207e34fba 2213 *
garfieldsg 0:662207e34fba 2214 * @param position Starting position (0-23)
garfieldsg 0:662207e34fba 2215 * @return Byte read from register
garfieldsg 0:662207e34fba 2216 */
garfieldsg 0:662207e34fba 2217 uint8_t MPU6050::getExternalSensorByte(int position)
garfieldsg 0:662207e34fba 2218 {
garfieldsg 0:662207e34fba 2219 i2Cdev.readByte(devAddr, MPU6050_RA_EXT_SENS_DATA_00 + position, buffer);
garfieldsg 0:662207e34fba 2220 return buffer[0];
garfieldsg 0:662207e34fba 2221 }
garfieldsg 0:662207e34fba 2222 /** Read word (2 bytes) from external sensor data registers.
garfieldsg 0:662207e34fba 2223 * @param position Starting position (0-21)
garfieldsg 0:662207e34fba 2224 * @return Word read from register
garfieldsg 0:662207e34fba 2225 * @see getExternalSensorByte()
garfieldsg 0:662207e34fba 2226 */
garfieldsg 0:662207e34fba 2227 uint16_t MPU6050::getExternalSensorWord(int position)
garfieldsg 0:662207e34fba 2228 {
garfieldsg 0:662207e34fba 2229 i2Cdev.readBytes(devAddr, MPU6050_RA_EXT_SENS_DATA_00 + position, 2, buffer);
garfieldsg 0:662207e34fba 2230 return (((uint16_t)buffer[0]) << 8) | buffer[1];
garfieldsg 0:662207e34fba 2231 }
garfieldsg 0:662207e34fba 2232 /** Read double word (4 bytes) from external sensor data registers.
garfieldsg 0:662207e34fba 2233 * @param position Starting position (0-20)
garfieldsg 0:662207e34fba 2234 * @return Double word read from registers
garfieldsg 0:662207e34fba 2235 * @see getExternalSensorByte()
garfieldsg 0:662207e34fba 2236 */
garfieldsg 0:662207e34fba 2237 uint32_t MPU6050::getExternalSensorDWord(int position)
garfieldsg 0:662207e34fba 2238 {
garfieldsg 0:662207e34fba 2239 i2Cdev.readBytes(devAddr, MPU6050_RA_EXT_SENS_DATA_00 + position, 4, buffer);
garfieldsg 0:662207e34fba 2240 return (((uint32_t)buffer[0]) << 24) | (((uint32_t)buffer[1]) << 16) | (((uint16_t)buffer[2]) << 8) | buffer[3];
garfieldsg 0:662207e34fba 2241 }
garfieldsg 0:662207e34fba 2242
garfieldsg 0:662207e34fba 2243 // MOT_DETECT_STATUS register
garfieldsg 0:662207e34fba 2244
garfieldsg 0:662207e34fba 2245 /** Get X-axis negative motion detection interrupt status.
garfieldsg 0:662207e34fba 2246 * @return Motion detection status
garfieldsg 0:662207e34fba 2247 * @see MPU6050_RA_MOT_DETECT_STATUS
garfieldsg 0:662207e34fba 2248 * @see MPU6050_MOTION_MOT_XNEG_BIT
garfieldsg 0:662207e34fba 2249 */
garfieldsg 0:662207e34fba 2250 bool MPU6050::getXNegMotionDetected()
garfieldsg 0:662207e34fba 2251 {
garfieldsg 0:662207e34fba 2252 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_XNEG_BIT, buffer);
garfieldsg 0:662207e34fba 2253 return buffer[0];
garfieldsg 0:662207e34fba 2254 }
garfieldsg 0:662207e34fba 2255 /** Get X-axis positive motion detection interrupt status.
garfieldsg 0:662207e34fba 2256 * @return Motion detection status
garfieldsg 0:662207e34fba 2257 * @see MPU6050_RA_MOT_DETECT_STATUS
garfieldsg 0:662207e34fba 2258 * @see MPU6050_MOTION_MOT_XPOS_BIT
garfieldsg 0:662207e34fba 2259 */
garfieldsg 0:662207e34fba 2260 bool MPU6050::getXPosMotionDetected()
garfieldsg 0:662207e34fba 2261 {
garfieldsg 0:662207e34fba 2262 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_XPOS_BIT, buffer);
garfieldsg 0:662207e34fba 2263 return buffer[0];
garfieldsg 0:662207e34fba 2264 }
garfieldsg 0:662207e34fba 2265 /** Get Y-axis negative motion detection interrupt status.
garfieldsg 0:662207e34fba 2266 * @return Motion detection status
garfieldsg 0:662207e34fba 2267 * @see MPU6050_RA_MOT_DETECT_STATUS
garfieldsg 0:662207e34fba 2268 * @see MPU6050_MOTION_MOT_YNEG_BIT
garfieldsg 0:662207e34fba 2269 */
garfieldsg 0:662207e34fba 2270 bool MPU6050::getYNegMotionDetected()
garfieldsg 0:662207e34fba 2271 {
garfieldsg 0:662207e34fba 2272 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_YNEG_BIT, buffer);
garfieldsg 0:662207e34fba 2273 return buffer[0];
garfieldsg 0:662207e34fba 2274 }
garfieldsg 0:662207e34fba 2275 /** Get Y-axis positive motion detection interrupt status.
garfieldsg 0:662207e34fba 2276 * @return Motion detection status
garfieldsg 0:662207e34fba 2277 * @see MPU6050_RA_MOT_DETECT_STATUS
garfieldsg 0:662207e34fba 2278 * @see MPU6050_MOTION_MOT_YPOS_BIT
garfieldsg 0:662207e34fba 2279 */
garfieldsg 0:662207e34fba 2280 bool MPU6050::getYPosMotionDetected()
garfieldsg 0:662207e34fba 2281 {
garfieldsg 0:662207e34fba 2282 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_YPOS_BIT, buffer);
garfieldsg 0:662207e34fba 2283 return buffer[0];
garfieldsg 0:662207e34fba 2284 }
garfieldsg 0:662207e34fba 2285 /** Get Z-axis negative motion detection interrupt status.
garfieldsg 0:662207e34fba 2286 * @return Motion detection status
garfieldsg 0:662207e34fba 2287 * @see MPU6050_RA_MOT_DETECT_STATUS
garfieldsg 0:662207e34fba 2288 * @see MPU6050_MOTION_MOT_ZNEG_BIT
garfieldsg 0:662207e34fba 2289 */
garfieldsg 0:662207e34fba 2290 bool MPU6050::getZNegMotionDetected()
garfieldsg 0:662207e34fba 2291 {
garfieldsg 0:662207e34fba 2292 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_ZNEG_BIT, buffer);
garfieldsg 0:662207e34fba 2293 return buffer[0];
garfieldsg 0:662207e34fba 2294 }
garfieldsg 0:662207e34fba 2295 /** Get Z-axis positive motion detection interrupt status.
garfieldsg 0:662207e34fba 2296 * @return Motion detection status
garfieldsg 0:662207e34fba 2297 * @see MPU6050_RA_MOT_DETECT_STATUS
garfieldsg 0:662207e34fba 2298 * @see MPU6050_MOTION_MOT_ZPOS_BIT
garfieldsg 0:662207e34fba 2299 */
garfieldsg 0:662207e34fba 2300 bool MPU6050::getZPosMotionDetected()
garfieldsg 0:662207e34fba 2301 {
garfieldsg 0:662207e34fba 2302 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_ZPOS_BIT, buffer);
garfieldsg 0:662207e34fba 2303 return buffer[0];
garfieldsg 0:662207e34fba 2304 }
garfieldsg 0:662207e34fba 2305 /** Get zero motion detection interrupt status.
garfieldsg 0:662207e34fba 2306 * @return Motion detection status
garfieldsg 0:662207e34fba 2307 * @see MPU6050_RA_MOT_DETECT_STATUS
garfieldsg 0:662207e34fba 2308 * @see MPU6050_MOTION_MOT_ZRMOT_BIT
garfieldsg 0:662207e34fba 2309 */
garfieldsg 0:662207e34fba 2310 bool MPU6050::getZeroMotionDetected()
garfieldsg 0:662207e34fba 2311 {
garfieldsg 0:662207e34fba 2312 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_ZRMOT_BIT, buffer);
garfieldsg 0:662207e34fba 2313 return buffer[0];
garfieldsg 0:662207e34fba 2314 }
garfieldsg 0:662207e34fba 2315
garfieldsg 0:662207e34fba 2316 // I2C_SLV*_DO register
garfieldsg 0:662207e34fba 2317
garfieldsg 0:662207e34fba 2318 /** Write byte to Data Output container for specified slave.
garfieldsg 0:662207e34fba 2319 * This register holds the output data written into Slave when Slave is set to
garfieldsg 0:662207e34fba 2320 * write mode. For further information regarding Slave control, please
garfieldsg 0:662207e34fba 2321 * refer to Registers 37 to 39 and immediately following.
garfieldsg 0:662207e34fba 2322 * @param num Slave number (0-3)
garfieldsg 0:662207e34fba 2323 * @param data Byte to write
garfieldsg 0:662207e34fba 2324 * @see MPU6050_RA_I2C_SLV0_DO
garfieldsg 0:662207e34fba 2325 */
garfieldsg 0:662207e34fba 2326 void MPU6050::setSlaveOutputByte(uint8_t num, uint8_t data)
garfieldsg 0:662207e34fba 2327 {
garfieldsg 0:662207e34fba 2328 if (num > 3) return;
garfieldsg 0:662207e34fba 2329 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV0_DO + num, data);
garfieldsg 0:662207e34fba 2330 }
garfieldsg 0:662207e34fba 2331
garfieldsg 0:662207e34fba 2332 // I2C_MST_DELAY_CTRL register
garfieldsg 0:662207e34fba 2333
garfieldsg 0:662207e34fba 2334 /** Get external data shadow delay enabled status.
garfieldsg 0:662207e34fba 2335 * This register is used to specify the timing of external sensor data
garfieldsg 0:662207e34fba 2336 * shadowing. When DELAY_ES_SHADOW is set to 1, shadowing of external
garfieldsg 0:662207e34fba 2337 * sensor data is delayed until all data has been received.
garfieldsg 0:662207e34fba 2338 * @return Current external data shadow delay enabled status.
garfieldsg 0:662207e34fba 2339 * @see MPU6050_RA_I2C_MST_DELAY_CTRL
garfieldsg 0:662207e34fba 2340 * @see MPU6050_DELAYCTRL_DELAY_ES_SHADOW_BIT
garfieldsg 0:662207e34fba 2341 */
garfieldsg 0:662207e34fba 2342 bool MPU6050::getExternalShadowDelayEnabled()
garfieldsg 0:662207e34fba 2343 {
garfieldsg 0:662207e34fba 2344 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_DELAY_CTRL, MPU6050_DELAYCTRL_DELAY_ES_SHADOW_BIT, buffer);
garfieldsg 0:662207e34fba 2345 return buffer[0];
garfieldsg 0:662207e34fba 2346 }
garfieldsg 0:662207e34fba 2347 /** Set external data shadow delay enabled status.
garfieldsg 0:662207e34fba 2348 * @param enabled New external data shadow delay enabled status.
garfieldsg 0:662207e34fba 2349 * @see getExternalShadowDelayEnabled()
garfieldsg 0:662207e34fba 2350 * @see MPU6050_RA_I2C_MST_DELAY_CTRL
garfieldsg 0:662207e34fba 2351 * @see MPU6050_DELAYCTRL_DELAY_ES_SHADOW_BIT
garfieldsg 0:662207e34fba 2352 */
garfieldsg 0:662207e34fba 2353 void MPU6050::setExternalShadowDelayEnabled(bool enabled)
garfieldsg 0:662207e34fba 2354 {
garfieldsg 0:662207e34fba 2355 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_DELAY_CTRL, MPU6050_DELAYCTRL_DELAY_ES_SHADOW_BIT, enabled);
garfieldsg 0:662207e34fba 2356 }
garfieldsg 0:662207e34fba 2357 /** Get slave delay enabled status.
garfieldsg 0:662207e34fba 2358 * When a particular slave delay is enabled, the rate of access for the that
garfieldsg 0:662207e34fba 2359 * slave device is reduced. When a slave's access rate is decreased relative to
garfieldsg 0:662207e34fba 2360 * the Sample Rate, the slave is accessed every:
garfieldsg 0:662207e34fba 2361 *
garfieldsg 0:662207e34fba 2362 * 1 / (1 + I2C_MST_DLY) Samples
garfieldsg 0:662207e34fba 2363 *
garfieldsg 0:662207e34fba 2364 * This base Sample Rate in turn is determined by SMPLRT_DIV (register * 25)
garfieldsg 0:662207e34fba 2365 * and DLPF_CFG (register 26).
garfieldsg 0:662207e34fba 2366 *
garfieldsg 0:662207e34fba 2367 * For further information regarding I2C_MST_DLY, please refer to register 52.
garfieldsg 0:662207e34fba 2368 * For further information regarding the Sample Rate, please refer to register 25.
garfieldsg 0:662207e34fba 2369 *
garfieldsg 0:662207e34fba 2370 * @param num Slave number (0-4)
garfieldsg 0:662207e34fba 2371 * @return Current slave delay enabled status.
garfieldsg 0:662207e34fba 2372 * @see MPU6050_RA_I2C_MST_DELAY_CTRL
garfieldsg 0:662207e34fba 2373 * @see MPU6050_DELAYCTRL_I2C_SLV0_DLY_EN_BIT
garfieldsg 0:662207e34fba 2374 */
garfieldsg 0:662207e34fba 2375 bool MPU6050::getSlaveDelayEnabled(uint8_t num)
garfieldsg 0:662207e34fba 2376 {
garfieldsg 0:662207e34fba 2377 // MPU6050_DELAYCTRL_I2C_SLV4_DLY_EN_BIT is 4, SLV3 is 3, etc.
garfieldsg 0:662207e34fba 2378 if (num > 4) return 0;
garfieldsg 0:662207e34fba 2379 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_DELAY_CTRL, num, buffer);
garfieldsg 0:662207e34fba 2380 return buffer[0];
garfieldsg 0:662207e34fba 2381 }
garfieldsg 0:662207e34fba 2382 /** Set slave delay enabled status.
garfieldsg 0:662207e34fba 2383 * @param num Slave number (0-4)
garfieldsg 0:662207e34fba 2384 * @param enabled New slave delay enabled status.
garfieldsg 0:662207e34fba 2385 * @see MPU6050_RA_I2C_MST_DELAY_CTRL
garfieldsg 0:662207e34fba 2386 * @see MPU6050_DELAYCTRL_I2C_SLV0_DLY_EN_BIT
garfieldsg 0:662207e34fba 2387 */
garfieldsg 0:662207e34fba 2388 void MPU6050::setSlaveDelayEnabled(uint8_t num, bool enabled)
garfieldsg 0:662207e34fba 2389 {
garfieldsg 0:662207e34fba 2390 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_DELAY_CTRL, num, enabled);
garfieldsg 0:662207e34fba 2391 }
garfieldsg 0:662207e34fba 2392
garfieldsg 0:662207e34fba 2393 // SIGNAL_PATH_RESET register
garfieldsg 0:662207e34fba 2394
garfieldsg 0:662207e34fba 2395 /** Reset gyroscope signal path.
garfieldsg 0:662207e34fba 2396 * The reset will revert the signal path analog to digital converters and
garfieldsg 0:662207e34fba 2397 * filters to their power up configurations.
garfieldsg 0:662207e34fba 2398 * @see MPU6050_RA_SIGNAL_PATH_RESET
garfieldsg 0:662207e34fba 2399 * @see MPU6050_PATHRESET_GYRO_RESET_BIT
garfieldsg 0:662207e34fba 2400 */
garfieldsg 0:662207e34fba 2401 void MPU6050::resetGyroscopePath()
garfieldsg 0:662207e34fba 2402 {
garfieldsg 0:662207e34fba 2403 i2Cdev.writeBit(devAddr, MPU6050_RA_SIGNAL_PATH_RESET, MPU6050_PATHRESET_GYRO_RESET_BIT, true);
garfieldsg 0:662207e34fba 2404 }
garfieldsg 0:662207e34fba 2405 /** Reset accelerometer signal path.
garfieldsg 0:662207e34fba 2406 * The reset will revert the signal path analog to digital converters and
garfieldsg 0:662207e34fba 2407 * filters to their power up configurations.
garfieldsg 0:662207e34fba 2408 * @see MPU6050_RA_SIGNAL_PATH_RESET
garfieldsg 0:662207e34fba 2409 * @see MPU6050_PATHRESET_ACCEL_RESET_BIT
garfieldsg 0:662207e34fba 2410 */
garfieldsg 0:662207e34fba 2411 void MPU6050::resetAccelerometerPath()
garfieldsg 0:662207e34fba 2412 {
garfieldsg 0:662207e34fba 2413 i2Cdev.writeBit(devAddr, MPU6050_RA_SIGNAL_PATH_RESET, MPU6050_PATHRESET_ACCEL_RESET_BIT, true);
garfieldsg 0:662207e34fba 2414 }
garfieldsg 0:662207e34fba 2415 /** Reset temperature sensor signal path.
garfieldsg 0:662207e34fba 2416 * The reset will revert the signal path analog to digital converters and
garfieldsg 0:662207e34fba 2417 * filters to their power up configurations.
garfieldsg 0:662207e34fba 2418 * @see MPU6050_RA_SIGNAL_PATH_RESET
garfieldsg 0:662207e34fba 2419 * @see MPU6050_PATHRESET_TEMP_RESET_BIT
garfieldsg 0:662207e34fba 2420 */
garfieldsg 0:662207e34fba 2421 void MPU6050::resetTemperaturePath()
garfieldsg 0:662207e34fba 2422 {
garfieldsg 0:662207e34fba 2423 i2Cdev.writeBit(devAddr, MPU6050_RA_SIGNAL_PATH_RESET, MPU6050_PATHRESET_TEMP_RESET_BIT, true);
garfieldsg 0:662207e34fba 2424 }
garfieldsg 0:662207e34fba 2425
garfieldsg 0:662207e34fba 2426 // MOT_DETECT_CTRL register
garfieldsg 0:662207e34fba 2427
garfieldsg 0:662207e34fba 2428 /** Get accelerometer power-on delay.
garfieldsg 0:662207e34fba 2429 * The accelerometer data path provides samples to the sensor registers, Motion
garfieldsg 0:662207e34fba 2430 * detection, Zero Motion detection, and Free Fall detection modules. The
garfieldsg 0:662207e34fba 2431 * signal path contains filters which must be flushed on wake-up with new
garfieldsg 0:662207e34fba 2432 * samples before the detection modules begin operations. The default wake-up
garfieldsg 0:662207e34fba 2433 * delay, of 4ms can be lengthened by up to 3ms. This additional delay is
garfieldsg 0:662207e34fba 2434 * specified in ACCEL_ON_DELAY in units of 1 LSB = 1 ms. The user may select
garfieldsg 0:662207e34fba 2435 * any value above zero unless instructed otherwise by InvenSense. Please refer
garfieldsg 0:662207e34fba 2436 * to Section 8 of the MPU-6000/MPU-6050 Product Specification document for
garfieldsg 0:662207e34fba 2437 * further information regarding the detection modules.
garfieldsg 0:662207e34fba 2438 * @return Current accelerometer power-on delay
garfieldsg 0:662207e34fba 2439 * @see MPU6050_RA_MOT_DETECT_CTRL
garfieldsg 0:662207e34fba 2440 * @see MPU6050_DETECT_ACCEL_ON_DELAY_BIT
garfieldsg 0:662207e34fba 2441 */
garfieldsg 0:662207e34fba 2442 uint8_t MPU6050::getAccelerometerPowerOnDelay()
garfieldsg 0:662207e34fba 2443 {
garfieldsg 0:662207e34fba 2444 i2Cdev.readBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_ACCEL_ON_DELAY_BIT, MPU6050_DETECT_ACCEL_ON_DELAY_LENGTH, buffer);
garfieldsg 0:662207e34fba 2445 return buffer[0];
garfieldsg 0:662207e34fba 2446 }
garfieldsg 0:662207e34fba 2447 /** Set accelerometer power-on delay.
garfieldsg 0:662207e34fba 2448 * @param delay New accelerometer power-on delay (0-3)
garfieldsg 0:662207e34fba 2449 * @see getAccelerometerPowerOnDelay()
garfieldsg 0:662207e34fba 2450 * @see MPU6050_RA_MOT_DETECT_CTRL
garfieldsg 0:662207e34fba 2451 * @see MPU6050_DETECT_ACCEL_ON_DELAY_BIT
garfieldsg 0:662207e34fba 2452 */
garfieldsg 0:662207e34fba 2453 void MPU6050::setAccelerometerPowerOnDelay(uint8_t delay)
garfieldsg 0:662207e34fba 2454 {
garfieldsg 0:662207e34fba 2455 i2Cdev.writeBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_ACCEL_ON_DELAY_BIT, MPU6050_DETECT_ACCEL_ON_DELAY_LENGTH, delay);
garfieldsg 0:662207e34fba 2456 }
garfieldsg 0:662207e34fba 2457 /** Get Free Fall detection counter decrement configuration.
garfieldsg 0:662207e34fba 2458 * Detection is registered by the Free Fall detection module after accelerometer
garfieldsg 0:662207e34fba 2459 * measurements meet their respective threshold conditions over a specified
garfieldsg 0:662207e34fba 2460 * number of samples. When the threshold conditions are met, the corresponding
garfieldsg 0:662207e34fba 2461 * detection counter increments by 1. The user may control the rate at which the
garfieldsg 0:662207e34fba 2462 * detection counter decrements when the threshold condition is not met by
garfieldsg 0:662207e34fba 2463 * configuring FF_COUNT. The decrement rate can be set according to the
garfieldsg 0:662207e34fba 2464 * following table:
garfieldsg 0:662207e34fba 2465 *
garfieldsg 0:662207e34fba 2466 * <pre>
garfieldsg 0:662207e34fba 2467 * FF_COUNT | Counter Decrement
garfieldsg 0:662207e34fba 2468 * ---------+------------------
garfieldsg 0:662207e34fba 2469 * 0 | Reset
garfieldsg 0:662207e34fba 2470 * 1 | 1
garfieldsg 0:662207e34fba 2471 * 2 | 2
garfieldsg 0:662207e34fba 2472 * 3 | 4
garfieldsg 0:662207e34fba 2473 * </pre>
garfieldsg 0:662207e34fba 2474 *
garfieldsg 0:662207e34fba 2475 * When FF_COUNT is configured to 0 (reset), any non-qualifying sample will
garfieldsg 0:662207e34fba 2476 * reset the counter to 0. For further information on Free Fall detection,
garfieldsg 0:662207e34fba 2477 * please refer to Registers 29 to 32.
garfieldsg 0:662207e34fba 2478 *
garfieldsg 0:662207e34fba 2479 * @return Current decrement configuration
garfieldsg 0:662207e34fba 2480 * @see MPU6050_RA_MOT_DETECT_CTRL
garfieldsg 0:662207e34fba 2481 * @see MPU6050_DETECT_FF_COUNT_BIT
garfieldsg 0:662207e34fba 2482 */
garfieldsg 0:662207e34fba 2483 uint8_t MPU6050::getFreefallDetectionCounterDecrement()
garfieldsg 0:662207e34fba 2484 {
garfieldsg 0:662207e34fba 2485 i2Cdev.readBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_FF_COUNT_BIT, MPU6050_DETECT_FF_COUNT_LENGTH, buffer);
garfieldsg 0:662207e34fba 2486 return buffer[0];
garfieldsg 0:662207e34fba 2487 }
garfieldsg 0:662207e34fba 2488 /** Set Free Fall detection counter decrement configuration.
garfieldsg 0:662207e34fba 2489 * @param decrement New decrement configuration value
garfieldsg 0:662207e34fba 2490 * @see getFreefallDetectionCounterDecrement()
garfieldsg 0:662207e34fba 2491 * @see MPU6050_RA_MOT_DETECT_CTRL
garfieldsg 0:662207e34fba 2492 * @see MPU6050_DETECT_FF_COUNT_BIT
garfieldsg 0:662207e34fba 2493 */
garfieldsg 0:662207e34fba 2494 void MPU6050::setFreefallDetectionCounterDecrement(uint8_t decrement)
garfieldsg 0:662207e34fba 2495 {
garfieldsg 0:662207e34fba 2496 i2Cdev.writeBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_FF_COUNT_BIT, MPU6050_DETECT_FF_COUNT_LENGTH, decrement);
garfieldsg 0:662207e34fba 2497 }
garfieldsg 0:662207e34fba 2498 /** Get Motion detection counter decrement configuration.
garfieldsg 0:662207e34fba 2499 * Detection is registered by the Motion detection module after accelerometer
garfieldsg 0:662207e34fba 2500 * measurements meet their respective threshold conditions over a specified
garfieldsg 0:662207e34fba 2501 * number of samples. When the threshold conditions are met, the corresponding
garfieldsg 0:662207e34fba 2502 * detection counter increments by 1. The user may control the rate at which the
garfieldsg 0:662207e34fba 2503 * detection counter decrements when the threshold condition is not met by
garfieldsg 0:662207e34fba 2504 * configuring MOT_COUNT. The decrement rate can be set according to the
garfieldsg 0:662207e34fba 2505 * following table:
garfieldsg 0:662207e34fba 2506 *
garfieldsg 0:662207e34fba 2507 * <pre>
garfieldsg 0:662207e34fba 2508 * MOT_COUNT | Counter Decrement
garfieldsg 0:662207e34fba 2509 * ----------+------------------
garfieldsg 0:662207e34fba 2510 * 0 | Reset
garfieldsg 0:662207e34fba 2511 * 1 | 1
garfieldsg 0:662207e34fba 2512 * 2 | 2
garfieldsg 0:662207e34fba 2513 * 3 | 4
garfieldsg 0:662207e34fba 2514 * </pre>
garfieldsg 0:662207e34fba 2515 *
garfieldsg 0:662207e34fba 2516 * When MOT_COUNT is configured to 0 (reset), any non-qualifying sample will
garfieldsg 0:662207e34fba 2517 * reset the counter to 0. For further information on Motion detection,
garfieldsg 0:662207e34fba 2518 * please refer to Registers 29 to 32.
garfieldsg 0:662207e34fba 2519 *
garfieldsg 0:662207e34fba 2520 */
garfieldsg 0:662207e34fba 2521 uint8_t MPU6050::getMotionDetectionCounterDecrement()
garfieldsg 0:662207e34fba 2522 {
garfieldsg 0:662207e34fba 2523 i2Cdev.readBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_MOT_COUNT_BIT, MPU6050_DETECT_MOT_COUNT_LENGTH, buffer);
garfieldsg 0:662207e34fba 2524 return buffer[0];
garfieldsg 0:662207e34fba 2525 }
garfieldsg 0:662207e34fba 2526 /** Set Motion detection counter decrement configuration.
garfieldsg 0:662207e34fba 2527 * @param decrement New decrement configuration value
garfieldsg 0:662207e34fba 2528 * @see getMotionDetectionCounterDecrement()
garfieldsg 0:662207e34fba 2529 * @see MPU6050_RA_MOT_DETECT_CTRL
garfieldsg 0:662207e34fba 2530 * @see MPU6050_DETECT_MOT_COUNT_BIT
garfieldsg 0:662207e34fba 2531 */
garfieldsg 0:662207e34fba 2532 void MPU6050::setMotionDetectionCounterDecrement(uint8_t decrement)
garfieldsg 0:662207e34fba 2533 {
garfieldsg 0:662207e34fba 2534 i2Cdev.writeBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_MOT_COUNT_BIT, MPU6050_DETECT_MOT_COUNT_LENGTH, decrement);
garfieldsg 0:662207e34fba 2535 }
garfieldsg 0:662207e34fba 2536
garfieldsg 0:662207e34fba 2537 // USER_CTRL register
garfieldsg 0:662207e34fba 2538
garfieldsg 0:662207e34fba 2539 /** Get FIFO enabled status.
garfieldsg 0:662207e34fba 2540 * When this bit is set to 0, the FIFO buffer is disabled. The FIFO buffer
garfieldsg 0:662207e34fba 2541 * cannot be written to or read from while disabled. The FIFO buffer's state
garfieldsg 0:662207e34fba 2542 * does not change unless the MPU-60X0 is power cycled.
garfieldsg 0:662207e34fba 2543 * @return Current FIFO enabled status
garfieldsg 0:662207e34fba 2544 * @see MPU6050_RA_USER_CTRL
garfieldsg 0:662207e34fba 2545 * @see MPU6050_USERCTRL_FIFO_EN_BIT
garfieldsg 0:662207e34fba 2546 */
garfieldsg 0:662207e34fba 2547 bool MPU6050::getFIFOEnabled()
garfieldsg 0:662207e34fba 2548 {
garfieldsg 0:662207e34fba 2549 i2Cdev.readBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_FIFO_EN_BIT, buffer);
garfieldsg 0:662207e34fba 2550 return buffer[0];
garfieldsg 0:662207e34fba 2551 }
garfieldsg 0:662207e34fba 2552 /** Set FIFO enabled status.
garfieldsg 0:662207e34fba 2553 * @param enabled New FIFO enabled status
garfieldsg 0:662207e34fba 2554 * @see getFIFOEnabled()
garfieldsg 0:662207e34fba 2555 * @see MPU6050_RA_USER_CTRL
garfieldsg 0:662207e34fba 2556 * @see MPU6050_USERCTRL_FIFO_EN_BIT
garfieldsg 0:662207e34fba 2557 */
garfieldsg 0:662207e34fba 2558 void MPU6050::setFIFOEnabled(bool enabled)
garfieldsg 0:662207e34fba 2559 {
garfieldsg 0:662207e34fba 2560 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_FIFO_EN_BIT, enabled);
garfieldsg 0:662207e34fba 2561 }
garfieldsg 0:662207e34fba 2562 /** Get I2C Master Mode enabled status.
garfieldsg 0:662207e34fba 2563 * When this mode is enabled, the MPU-60X0 acts as the I2C Master to the
garfieldsg 0:662207e34fba 2564 * external sensor slave devices on the auxiliary I2C bus. When this bit is
garfieldsg 0:662207e34fba 2565 * cleared to 0, the auxiliary I2C bus lines (AUX_DA and AUX_CL) are logically
garfieldsg 0:662207e34fba 2566 * driven by the primary I2C bus (SDA and SCL). This is a precondition to
garfieldsg 0:662207e34fba 2567 * enabling Bypass Mode. For further information regarding Bypass Mode, please
garfieldsg 0:662207e34fba 2568 * refer to Register 55.
garfieldsg 0:662207e34fba 2569 * @return Current I2C Master Mode enabled status
garfieldsg 0:662207e34fba 2570 * @see MPU6050_RA_USER_CTRL
garfieldsg 0:662207e34fba 2571 * @see MPU6050_USERCTRL_I2C_MST_EN_BIT
garfieldsg 0:662207e34fba 2572 */
garfieldsg 0:662207e34fba 2573 bool MPU6050::getI2CMasterModeEnabled()
garfieldsg 0:662207e34fba 2574 {
garfieldsg 0:662207e34fba 2575 i2Cdev.readBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_I2C_MST_EN_BIT, buffer);
garfieldsg 0:662207e34fba 2576 return buffer[0];
garfieldsg 0:662207e34fba 2577 }
garfieldsg 0:662207e34fba 2578 /** Set I2C Master Mode enabled status.
garfieldsg 0:662207e34fba 2579 * @param enabled New I2C Master Mode enabled status
garfieldsg 0:662207e34fba 2580 * @see getI2CMasterModeEnabled()
garfieldsg 0:662207e34fba 2581 * @see MPU6050_RA_USER_CTRL
garfieldsg 0:662207e34fba 2582 * @see MPU6050_USERCTRL_I2C_MST_EN_BIT
garfieldsg 0:662207e34fba 2583 */
garfieldsg 0:662207e34fba 2584 void MPU6050::setI2CMasterModeEnabled(bool enabled)
garfieldsg 0:662207e34fba 2585 {
garfieldsg 0:662207e34fba 2586 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_I2C_MST_EN_BIT, enabled);
garfieldsg 0:662207e34fba 2587 }
garfieldsg 0:662207e34fba 2588 /** Switch from I2C to SPI mode (MPU-6000 only)
garfieldsg 0:662207e34fba 2589 * If this is set, the primary SPI interface will be enabled in place of the
garfieldsg 0:662207e34fba 2590 * disabled primary I2C interface.
garfieldsg 0:662207e34fba 2591 */
garfieldsg 0:662207e34fba 2592 void MPU6050::switchSPIEnabled(bool enabled)
garfieldsg 0:662207e34fba 2593 {
garfieldsg 0:662207e34fba 2594 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_I2C_IF_DIS_BIT, enabled);
garfieldsg 0:662207e34fba 2595 }
garfieldsg 0:662207e34fba 2596 /** Reset the FIFO.
garfieldsg 0:662207e34fba 2597 * This bit resets the FIFO buffer when set to 1 while FIFO_EN equals 0. This
garfieldsg 0:662207e34fba 2598 * bit automatically clears to 0 after the reset has been triggered.
garfieldsg 0:662207e34fba 2599 * @see MPU6050_RA_USER_CTRL
garfieldsg 0:662207e34fba 2600 * @see MPU6050_USERCTRL_FIFO_RESET_BIT
garfieldsg 0:662207e34fba 2601 */
garfieldsg 0:662207e34fba 2602 void MPU6050::resetFIFO()
garfieldsg 0:662207e34fba 2603 {
garfieldsg 0:662207e34fba 2604 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_FIFO_RESET_BIT, true);
garfieldsg 0:662207e34fba 2605 }
garfieldsg 0:662207e34fba 2606 /** Reset the I2C Master.
garfieldsg 0:662207e34fba 2607 * This bit resets the I2C Master when set to 1 while I2C_MST_EN equals 0.
garfieldsg 0:662207e34fba 2608 * This bit automatically clears to 0 after the reset has been triggered.
garfieldsg 0:662207e34fba 2609 * @see MPU6050_RA_USER_CTRL
garfieldsg 0:662207e34fba 2610 * @see MPU6050_USERCTRL_I2C_MST_RESET_BIT
garfieldsg 0:662207e34fba 2611 */
garfieldsg 0:662207e34fba 2612 void MPU6050::resetI2CMaster()
garfieldsg 0:662207e34fba 2613 {
garfieldsg 0:662207e34fba 2614 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_I2C_MST_RESET_BIT, true);
garfieldsg 0:662207e34fba 2615 }
garfieldsg 0:662207e34fba 2616 /** Reset all sensor registers and signal paths.
garfieldsg 0:662207e34fba 2617 * When set to 1, this bit resets the signal paths for all sensors (gyroscopes,
garfieldsg 0:662207e34fba 2618 * accelerometers, and temperature sensor). This operation will also clear the
garfieldsg 0:662207e34fba 2619 * sensor registers. This bit automatically clears to 0 after the reset has been
garfieldsg 0:662207e34fba 2620 * triggered.
garfieldsg 0:662207e34fba 2621 *
garfieldsg 0:662207e34fba 2622 * When resetting only the signal path (and not the sensor registers), please
garfieldsg 0:662207e34fba 2623 * use Register 104, SIGNAL_PATH_RESET.
garfieldsg 0:662207e34fba 2624 *
garfieldsg 0:662207e34fba 2625 * @see MPU6050_RA_USER_CTRL
garfieldsg 0:662207e34fba 2626 * @see MPU6050_USERCTRL_SIG_COND_RESET_BIT
garfieldsg 0:662207e34fba 2627 */
garfieldsg 0:662207e34fba 2628 void MPU6050::resetSensors()
garfieldsg 0:662207e34fba 2629 {
garfieldsg 0:662207e34fba 2630 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_SIG_COND_RESET_BIT, true);
garfieldsg 0:662207e34fba 2631 }
garfieldsg 0:662207e34fba 2632
garfieldsg 0:662207e34fba 2633 // PWR_MGMT_1 register
garfieldsg 0:662207e34fba 2634
garfieldsg 0:662207e34fba 2635 /** Trigger a full device reset.
garfieldsg 0:662207e34fba 2636 * A small delay of ~50ms may be desirable after triggering a reset.
garfieldsg 0:662207e34fba 2637 * @see MPU6050_RA_PWR_MGMT_1
garfieldsg 0:662207e34fba 2638 * @see MPU6050_PWR1_DEVICE_RESET_BIT
garfieldsg 0:662207e34fba 2639 */
garfieldsg 0:662207e34fba 2640 void MPU6050::reset()
garfieldsg 0:662207e34fba 2641 {
garfieldsg 0:662207e34fba 2642 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_DEVICE_RESET_BIT, true);
garfieldsg 0:662207e34fba 2643 }
garfieldsg 0:662207e34fba 2644 /** Get sleep mode status.
garfieldsg 0:662207e34fba 2645 * Setting the SLEEP bit in the register puts the device into very low power
garfieldsg 0:662207e34fba 2646 * sleep mode. In this mode, only the serial interface and internal registers
garfieldsg 0:662207e34fba 2647 * remain active, allowing for a very low standby current. Clearing this bit
garfieldsg 0:662207e34fba 2648 * puts the device back into normal mode. To save power, the individual standby
garfieldsg 0:662207e34fba 2649 * selections for each of the gyros should be used if any gyro axis is not used
garfieldsg 0:662207e34fba 2650 * by the application.
garfieldsg 0:662207e34fba 2651 * @return Current sleep mode enabled status
garfieldsg 0:662207e34fba 2652 * @see MPU6050_RA_PWR_MGMT_1
garfieldsg 0:662207e34fba 2653 * @see MPU6050_PWR1_SLEEP_BIT
garfieldsg 0:662207e34fba 2654 */
garfieldsg 0:662207e34fba 2655 bool MPU6050::getSleepEnabled()
garfieldsg 0:662207e34fba 2656 {
garfieldsg 0:662207e34fba 2657 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_SLEEP_BIT, buffer);
garfieldsg 0:662207e34fba 2658 return buffer[0];
garfieldsg 0:662207e34fba 2659 }
garfieldsg 0:662207e34fba 2660 /** Set sleep mode status.
garfieldsg 0:662207e34fba 2661 * @param enabled New sleep mode enabled status
garfieldsg 0:662207e34fba 2662 * @see getSleepEnabled()
garfieldsg 0:662207e34fba 2663 * @see MPU6050_RA_PWR_MGMT_1
garfieldsg 0:662207e34fba 2664 * @see MPU6050_PWR1_SLEEP_BIT
garfieldsg 0:662207e34fba 2665 */
garfieldsg 0:662207e34fba 2666 void MPU6050::setSleepEnabled(bool enabled)
garfieldsg 0:662207e34fba 2667 {
garfieldsg 0:662207e34fba 2668 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_SLEEP_BIT, enabled);
garfieldsg 0:662207e34fba 2669 }
garfieldsg 0:662207e34fba 2670 /** Get wake cycle enabled status.
garfieldsg 0:662207e34fba 2671 * When this bit is set to 1 and SLEEP is disabled, the MPU-60X0 will cycle
garfieldsg 0:662207e34fba 2672 * between sleep mode and waking up to take a single sample of data from active
garfieldsg 0:662207e34fba 2673 * sensors at a rate determined by LP_WAKE_CTRL (register 108).
garfieldsg 0:662207e34fba 2674 * @return Current sleep mode enabled status
garfieldsg 0:662207e34fba 2675 * @see MPU6050_RA_PWR_MGMT_1
garfieldsg 0:662207e34fba 2676 * @see MPU6050_PWR1_CYCLE_BIT
garfieldsg 0:662207e34fba 2677 */
garfieldsg 0:662207e34fba 2678 bool MPU6050::getWakeCycleEnabled()
garfieldsg 0:662207e34fba 2679 {
garfieldsg 0:662207e34fba 2680 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_CYCLE_BIT, buffer);
garfieldsg 0:662207e34fba 2681 return buffer[0];
garfieldsg 0:662207e34fba 2682 }
garfieldsg 0:662207e34fba 2683 /** Set wake cycle enabled status.
garfieldsg 0:662207e34fba 2684 * @param enabled New sleep mode enabled status
garfieldsg 0:662207e34fba 2685 * @see getWakeCycleEnabled()
garfieldsg 0:662207e34fba 2686 * @see MPU6050_RA_PWR_MGMT_1
garfieldsg 0:662207e34fba 2687 * @see MPU6050_PWR1_CYCLE_BIT
garfieldsg 0:662207e34fba 2688 */
garfieldsg 0:662207e34fba 2689 void MPU6050::setWakeCycleEnabled(bool enabled)
garfieldsg 0:662207e34fba 2690 {
garfieldsg 0:662207e34fba 2691 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_CYCLE_BIT, enabled);
garfieldsg 0:662207e34fba 2692 }
garfieldsg 0:662207e34fba 2693 /** Get temperature sensor enabled status.
garfieldsg 0:662207e34fba 2694 * Control the usage of the internal temperature sensor.
garfieldsg 0:662207e34fba 2695 *
garfieldsg 0:662207e34fba 2696 * Note: this register stores the *disabled* value, but for consistency with the
garfieldsg 0:662207e34fba 2697 * rest of the code, the function is named and used with standard true/false
garfieldsg 0:662207e34fba 2698 * values to indicate whether the sensor is enabled or disabled, respectively.
garfieldsg 0:662207e34fba 2699 *
garfieldsg 0:662207e34fba 2700 * @return Current temperature sensor enabled status
garfieldsg 0:662207e34fba 2701 * @see MPU6050_RA_PWR_MGMT_1
garfieldsg 0:662207e34fba 2702 * @see MPU6050_PWR1_TEMP_DIS_BIT
garfieldsg 0:662207e34fba 2703 */
garfieldsg 0:662207e34fba 2704 bool MPU6050::getTempSensorEnabled()
garfieldsg 0:662207e34fba 2705 {
garfieldsg 0:662207e34fba 2706 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_TEMP_DIS_BIT, buffer);
garfieldsg 0:662207e34fba 2707 return buffer[0] == 0; // 1 is actually disabled here
garfieldsg 0:662207e34fba 2708 }
garfieldsg 0:662207e34fba 2709 /** Set temperature sensor enabled status.
garfieldsg 0:662207e34fba 2710 * Note: this register stores the *disabled* value, but for consistency with the
garfieldsg 0:662207e34fba 2711 * rest of the code, the function is named and used with standard true/false
garfieldsg 0:662207e34fba 2712 * values to indicate whether the sensor is enabled or disabled, respectively.
garfieldsg 0:662207e34fba 2713 *
garfieldsg 0:662207e34fba 2714 * @param enabled New temperature sensor enabled status
garfieldsg 0:662207e34fba 2715 * @see getTempSensorEnabled()
garfieldsg 0:662207e34fba 2716 * @see MPU6050_RA_PWR_MGMT_1
garfieldsg 0:662207e34fba 2717 * @see MPU6050_PWR1_TEMP_DIS_BIT
garfieldsg 0:662207e34fba 2718 */
garfieldsg 0:662207e34fba 2719 void MPU6050::setTempSensorEnabled(bool enabled)
garfieldsg 0:662207e34fba 2720 {
garfieldsg 0:662207e34fba 2721 // 1 is actually disabled here
garfieldsg 0:662207e34fba 2722 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_TEMP_DIS_BIT, !enabled);
garfieldsg 0:662207e34fba 2723 }
garfieldsg 0:662207e34fba 2724 /** Get clock source setting.
garfieldsg 0:662207e34fba 2725 * @return Current clock source setting
garfieldsg 0:662207e34fba 2726 * @see MPU6050_RA_PWR_MGMT_1
garfieldsg 0:662207e34fba 2727 * @see MPU6050_PWR1_CLKSEL_BIT
garfieldsg 0:662207e34fba 2728 * @see MPU6050_PWR1_CLKSEL_LENGTH
garfieldsg 0:662207e34fba 2729 */
garfieldsg 0:662207e34fba 2730 uint8_t MPU6050::getClockSource()
garfieldsg 0:662207e34fba 2731 {
garfieldsg 0:662207e34fba 2732 i2Cdev.readBits(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_CLKSEL_BIT, MPU6050_PWR1_CLKSEL_LENGTH, buffer);
garfieldsg 0:662207e34fba 2733 return buffer[0];
garfieldsg 0:662207e34fba 2734 }
garfieldsg 0:662207e34fba 2735 /** Set clock source setting.
garfieldsg 0:662207e34fba 2736 * An internal 8MHz oscillator, gyroscope based clock, or external sources can
garfieldsg 0:662207e34fba 2737 * be selected as the MPU-60X0 clock source. When the internal 8 MHz oscillator
garfieldsg 0:662207e34fba 2738 * or an external source is chosen as the clock source, the MPU-60X0 can operate
garfieldsg 0:662207e34fba 2739 * in low power modes with the gyroscopes disabled.
garfieldsg 0:662207e34fba 2740 *
garfieldsg 0:662207e34fba 2741 * Upon power up, the MPU-60X0 clock source defaults to the internal oscillator.
garfieldsg 0:662207e34fba 2742 * However, it is highly recommended that the device be configured to use one of
garfieldsg 0:662207e34fba 2743 * the gyroscopes (or an external clock source) as the clock reference for
garfieldsg 0:662207e34fba 2744 * improved stability. The clock source can be selected according to the following table:
garfieldsg 0:662207e34fba 2745 *
garfieldsg 0:662207e34fba 2746 * <pre>
garfieldsg 0:662207e34fba 2747 * CLK_SEL | Clock Source
garfieldsg 0:662207e34fba 2748 * --------+--------------------------------------
garfieldsg 0:662207e34fba 2749 * 0 | Internal oscillator
garfieldsg 0:662207e34fba 2750 * 1 | PLL with X Gyro reference
garfieldsg 0:662207e34fba 2751 * 2 | PLL with Y Gyro reference
garfieldsg 0:662207e34fba 2752 * 3 | PLL with Z Gyro reference
garfieldsg 0:662207e34fba 2753 * 4 | PLL with external 32.768kHz reference
garfieldsg 0:662207e34fba 2754 * 5 | PLL with external 19.2MHz reference
garfieldsg 0:662207e34fba 2755 * 6 | Reserved
garfieldsg 0:662207e34fba 2756 * 7 | Stops the clock and keeps the timing generator in reset
garfieldsg 0:662207e34fba 2757 * </pre>
garfieldsg 0:662207e34fba 2758 *
garfieldsg 0:662207e34fba 2759 * @param source New clock source setting
garfieldsg 0:662207e34fba 2760 * @see getClockSource()
garfieldsg 0:662207e34fba 2761 * @see MPU6050_RA_PWR_MGMT_1
garfieldsg 0:662207e34fba 2762 * @see MPU6050_PWR1_CLKSEL_BIT
garfieldsg 0:662207e34fba 2763 * @see MPU6050_PWR1_CLKSEL_LENGTH
garfieldsg 0:662207e34fba 2764 */
garfieldsg 0:662207e34fba 2765 void MPU6050::setClockSource(uint8_t source)
garfieldsg 0:662207e34fba 2766 {
garfieldsg 0:662207e34fba 2767 i2Cdev.writeBits(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_CLKSEL_BIT, MPU6050_PWR1_CLKSEL_LENGTH, source);
garfieldsg 0:662207e34fba 2768 }
garfieldsg 0:662207e34fba 2769
garfieldsg 0:662207e34fba 2770 // PWR_MGMT_2 register
garfieldsg 0:662207e34fba 2771
garfieldsg 0:662207e34fba 2772 /** Get wake frequency in Accel-Only Low Power Mode.
garfieldsg 0:662207e34fba 2773 * The MPU-60X0 can be put into Accerlerometer Only Low Power Mode by setting
garfieldsg 0:662207e34fba 2774 * PWRSEL to 1 in the Power Management 1 register (Register 107). In this mode,
garfieldsg 0:662207e34fba 2775 * the device will power off all devices except for the primary I2C interface,
garfieldsg 0:662207e34fba 2776 * waking only the accelerometer at fixed intervals to take a single
garfieldsg 0:662207e34fba 2777 * measurement. The frequency of wake-ups can be configured with LP_WAKE_CTRL
garfieldsg 0:662207e34fba 2778 * as shown below:
garfieldsg 0:662207e34fba 2779 *
garfieldsg 0:662207e34fba 2780 * <pre>
garfieldsg 0:662207e34fba 2781 * LP_WAKE_CTRL | Wake-up Frequency
garfieldsg 0:662207e34fba 2782 * -------------+------------------
garfieldsg 0:662207e34fba 2783 * 0 | 1.25 Hz
garfieldsg 0:662207e34fba 2784 * 1 | 2.5 Hz
garfieldsg 0:662207e34fba 2785 * 2 | 5 Hz
garfieldsg 0:662207e34fba 2786 * 3 | 10 Hz
garfieldsg 0:662207e34fba 2787 * <pre>
garfieldsg 0:662207e34fba 2788 *
garfieldsg 0:662207e34fba 2789 * For further information regarding the MPU-60X0's power modes, please refer to
garfieldsg 0:662207e34fba 2790 * Register 107.
garfieldsg 0:662207e34fba 2791 *
garfieldsg 0:662207e34fba 2792 * @return Current wake frequency
garfieldsg 0:662207e34fba 2793 * @see MPU6050_RA_PWR_MGMT_2
garfieldsg 0:662207e34fba 2794 */
garfieldsg 0:662207e34fba 2795 uint8_t MPU6050::getWakeFrequency()
garfieldsg 0:662207e34fba 2796 {
garfieldsg 0:662207e34fba 2797 i2Cdev.readBits(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_LP_WAKE_CTRL_BIT, MPU6050_PWR2_LP_WAKE_CTRL_LENGTH, buffer);
garfieldsg 0:662207e34fba 2798 return buffer[0];
garfieldsg 0:662207e34fba 2799 }
garfieldsg 0:662207e34fba 2800 /** Set wake frequency in Accel-Only Low Power Mode.
garfieldsg 0:662207e34fba 2801 * @param frequency New wake frequency
garfieldsg 0:662207e34fba 2802 * @see MPU6050_RA_PWR_MGMT_2
garfieldsg 0:662207e34fba 2803 */
garfieldsg 0:662207e34fba 2804 void MPU6050::setWakeFrequency(uint8_t frequency)
garfieldsg 0:662207e34fba 2805 {
garfieldsg 0:662207e34fba 2806 i2Cdev.writeBits(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_LP_WAKE_CTRL_BIT, MPU6050_PWR2_LP_WAKE_CTRL_LENGTH, frequency);
garfieldsg 0:662207e34fba 2807 }
garfieldsg 0:662207e34fba 2808
garfieldsg 0:662207e34fba 2809 /** Get X-axis accelerometer standby enabled status.
garfieldsg 0:662207e34fba 2810 * If enabled, the X-axis will not gather or report data (or use power).
garfieldsg 0:662207e34fba 2811 * @return Current X-axis standby enabled status
garfieldsg 0:662207e34fba 2812 * @see MPU6050_RA_PWR_MGMT_2
garfieldsg 0:662207e34fba 2813 * @see MPU6050_PWR2_STBY_XA_BIT
garfieldsg 0:662207e34fba 2814 */
garfieldsg 0:662207e34fba 2815 bool MPU6050::getStandbyXAccelEnabled()
garfieldsg 0:662207e34fba 2816 {
garfieldsg 0:662207e34fba 2817 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_XA_BIT, buffer);
garfieldsg 0:662207e34fba 2818 return buffer[0];
garfieldsg 0:662207e34fba 2819 }
garfieldsg 0:662207e34fba 2820 /** Set X-axis accelerometer standby enabled status.
garfieldsg 0:662207e34fba 2821 * @param New X-axis standby enabled status
garfieldsg 0:662207e34fba 2822 * @see getStandbyXAccelEnabled()
garfieldsg 0:662207e34fba 2823 * @see MPU6050_RA_PWR_MGMT_2
garfieldsg 0:662207e34fba 2824 * @see MPU6050_PWR2_STBY_XA_BIT
garfieldsg 0:662207e34fba 2825 */
garfieldsg 0:662207e34fba 2826 void MPU6050::setStandbyXAccelEnabled(bool enabled)
garfieldsg 0:662207e34fba 2827 {
garfieldsg 0:662207e34fba 2828 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_XA_BIT, enabled);
garfieldsg 0:662207e34fba 2829 }
garfieldsg 0:662207e34fba 2830 /** Get Y-axis accelerometer standby enabled status.
garfieldsg 0:662207e34fba 2831 * If enabled, the Y-axis will not gather or report data (or use power).
garfieldsg 0:662207e34fba 2832 * @return Current Y-axis standby enabled status
garfieldsg 0:662207e34fba 2833 * @see MPU6050_RA_PWR_MGMT_2
garfieldsg 0:662207e34fba 2834 * @see MPU6050_PWR2_STBY_YA_BIT
garfieldsg 0:662207e34fba 2835 */
garfieldsg 0:662207e34fba 2836 bool MPU6050::getStandbyYAccelEnabled()
garfieldsg 0:662207e34fba 2837 {
garfieldsg 0:662207e34fba 2838 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_YA_BIT, buffer);
garfieldsg 0:662207e34fba 2839 return buffer[0];
garfieldsg 0:662207e34fba 2840 }
garfieldsg 0:662207e34fba 2841 /** Set Y-axis accelerometer standby enabled status.
garfieldsg 0:662207e34fba 2842 * @param New Y-axis standby enabled status
garfieldsg 0:662207e34fba 2843 * @see getStandbyYAccelEnabled()
garfieldsg 0:662207e34fba 2844 * @see MPU6050_RA_PWR_MGMT_2
garfieldsg 0:662207e34fba 2845 * @see MPU6050_PWR2_STBY_YA_BIT
garfieldsg 0:662207e34fba 2846 */
garfieldsg 0:662207e34fba 2847 void MPU6050::setStandbyYAccelEnabled(bool enabled)
garfieldsg 0:662207e34fba 2848 {
garfieldsg 0:662207e34fba 2849 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_YA_BIT, enabled);
garfieldsg 0:662207e34fba 2850 }
garfieldsg 0:662207e34fba 2851 /** Get Z-axis accelerometer standby enabled status.
garfieldsg 0:662207e34fba 2852 * If enabled, the Z-axis will not gather or report data (or use power).
garfieldsg 0:662207e34fba 2853 * @return Current Z-axis standby enabled status
garfieldsg 0:662207e34fba 2854 * @see MPU6050_RA_PWR_MGMT_2
garfieldsg 0:662207e34fba 2855 * @see MPU6050_PWR2_STBY_ZA_BIT
garfieldsg 0:662207e34fba 2856 */
garfieldsg 0:662207e34fba 2857 bool MPU6050::getStandbyZAccelEnabled()
garfieldsg 0:662207e34fba 2858 {
garfieldsg 0:662207e34fba 2859 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_ZA_BIT, buffer);
garfieldsg 0:662207e34fba 2860 return buffer[0];
garfieldsg 0:662207e34fba 2861 }
garfieldsg 0:662207e34fba 2862 /** Set Z-axis accelerometer standby enabled status.
garfieldsg 0:662207e34fba 2863 * @param New Z-axis standby enabled status
garfieldsg 0:662207e34fba 2864 * @see getStandbyZAccelEnabled()
garfieldsg 0:662207e34fba 2865 * @see MPU6050_RA_PWR_MGMT_2
garfieldsg 0:662207e34fba 2866 * @see MPU6050_PWR2_STBY_ZA_BIT
garfieldsg 0:662207e34fba 2867 */
garfieldsg 0:662207e34fba 2868 void MPU6050::setStandbyZAccelEnabled(bool enabled)
garfieldsg 0:662207e34fba 2869 {
garfieldsg 0:662207e34fba 2870 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_ZA_BIT, enabled);
garfieldsg 0:662207e34fba 2871 }
garfieldsg 0:662207e34fba 2872 /** Get X-axis gyroscope standby enabled status.
garfieldsg 0:662207e34fba 2873 * If enabled, the X-axis will not gather or report data (or use power).
garfieldsg 0:662207e34fba 2874 * @return Current X-axis standby enabled status
garfieldsg 0:662207e34fba 2875 * @see MPU6050_RA_PWR_MGMT_2
garfieldsg 0:662207e34fba 2876 * @see MPU6050_PWR2_STBY_XG_BIT
garfieldsg 0:662207e34fba 2877 */
garfieldsg 0:662207e34fba 2878 bool MPU6050::getStandbyXGyroEnabled()
garfieldsg 0:662207e34fba 2879 {
garfieldsg 0:662207e34fba 2880 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_XG_BIT, buffer);
garfieldsg 0:662207e34fba 2881 return buffer[0];
garfieldsg 0:662207e34fba 2882 }
garfieldsg 0:662207e34fba 2883 /** Set X-axis gyroscope standby enabled status.
garfieldsg 0:662207e34fba 2884 * @param New X-axis standby enabled status
garfieldsg 0:662207e34fba 2885 * @see getStandbyXGyroEnabled()
garfieldsg 0:662207e34fba 2886 * @see MPU6050_RA_PWR_MGMT_2
garfieldsg 0:662207e34fba 2887 * @see MPU6050_PWR2_STBY_XG_BIT
garfieldsg 0:662207e34fba 2888 */
garfieldsg 0:662207e34fba 2889 void MPU6050::setStandbyXGyroEnabled(bool enabled)
garfieldsg 0:662207e34fba 2890 {
garfieldsg 0:662207e34fba 2891 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_XG_BIT, enabled);
garfieldsg 0:662207e34fba 2892 }
garfieldsg 0:662207e34fba 2893 /** Get Y-axis gyroscope standby enabled status.
garfieldsg 0:662207e34fba 2894 * If enabled, the Y-axis will not gather or report data (or use power).
garfieldsg 0:662207e34fba 2895 * @return Current Y-axis standby enabled status
garfieldsg 0:662207e34fba 2896 * @see MPU6050_RA_PWR_MGMT_2
garfieldsg 0:662207e34fba 2897 * @see MPU6050_PWR2_STBY_YG_BIT
garfieldsg 0:662207e34fba 2898 */
garfieldsg 0:662207e34fba 2899 bool MPU6050::getStandbyYGyroEnabled()
garfieldsg 0:662207e34fba 2900 {
garfieldsg 0:662207e34fba 2901 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_YG_BIT, buffer);
garfieldsg 0:662207e34fba 2902 return buffer[0];
garfieldsg 0:662207e34fba 2903 }
garfieldsg 0:662207e34fba 2904 /** Set Y-axis gyroscope standby enabled status.
garfieldsg 0:662207e34fba 2905 * @param New Y-axis standby enabled status
garfieldsg 0:662207e34fba 2906 * @see getStandbyYGyroEnabled()
garfieldsg 0:662207e34fba 2907 * @see MPU6050_RA_PWR_MGMT_2
garfieldsg 0:662207e34fba 2908 * @see MPU6050_PWR2_STBY_YG_BIT
garfieldsg 0:662207e34fba 2909 */
garfieldsg 0:662207e34fba 2910 void MPU6050::setStandbyYGyroEnabled(bool enabled)
garfieldsg 0:662207e34fba 2911 {
garfieldsg 0:662207e34fba 2912 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_YG_BIT, enabled);
garfieldsg 0:662207e34fba 2913 }
garfieldsg 0:662207e34fba 2914 /** Get Z-axis gyroscope standby enabled status.
garfieldsg 0:662207e34fba 2915 * If enabled, the Z-axis will not gather or report data (or use power).
garfieldsg 0:662207e34fba 2916 * @return Current Z-axis standby enabled status
garfieldsg 0:662207e34fba 2917 * @see MPU6050_RA_PWR_MGMT_2
garfieldsg 0:662207e34fba 2918 * @see MPU6050_PWR2_STBY_ZG_BIT
garfieldsg 0:662207e34fba 2919 */
garfieldsg 0:662207e34fba 2920 bool MPU6050::getStandbyZGyroEnabled()
garfieldsg 0:662207e34fba 2921 {
garfieldsg 0:662207e34fba 2922 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_ZG_BIT, buffer);
garfieldsg 0:662207e34fba 2923 return buffer[0];
garfieldsg 0:662207e34fba 2924 }
garfieldsg 0:662207e34fba 2925 /** Set Z-axis gyroscope standby enabled status.
garfieldsg 0:662207e34fba 2926 * @param New Z-axis standby enabled status
garfieldsg 0:662207e34fba 2927 * @see getStandbyZGyroEnabled()
garfieldsg 0:662207e34fba 2928 * @see MPU6050_RA_PWR_MGMT_2
garfieldsg 0:662207e34fba 2929 * @see MPU6050_PWR2_STBY_ZG_BIT
garfieldsg 0:662207e34fba 2930 */
garfieldsg 0:662207e34fba 2931 void MPU6050::setStandbyZGyroEnabled(bool enabled)
garfieldsg 0:662207e34fba 2932 {
garfieldsg 0:662207e34fba 2933 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_ZG_BIT, enabled);
garfieldsg 0:662207e34fba 2934 }
garfieldsg 0:662207e34fba 2935
garfieldsg 0:662207e34fba 2936 // FIFO_COUNT* registers
garfieldsg 0:662207e34fba 2937
garfieldsg 0:662207e34fba 2938 /** Get current FIFO buffer size.
garfieldsg 0:662207e34fba 2939 * This value indicates the number of bytes stored in the FIFO buffer. This
garfieldsg 0:662207e34fba 2940 * number is in turn the number of bytes that can be read from the FIFO buffer
garfieldsg 0:662207e34fba 2941 * and it is directly proportional to the number of samples available given the
garfieldsg 0:662207e34fba 2942 * set of sensor data bound to be stored in the FIFO (register 35 and 36).
garfieldsg 0:662207e34fba 2943 * @return Current FIFO buffer size
garfieldsg 0:662207e34fba 2944 */
garfieldsg 0:662207e34fba 2945 uint16_t MPU6050::getFIFOCount()
garfieldsg 0:662207e34fba 2946 {
garfieldsg 0:662207e34fba 2947 i2Cdev.readBytes(devAddr, MPU6050_RA_FIFO_COUNTH, 2, buffer);
garfieldsg 0:662207e34fba 2948 return (((uint16_t)buffer[0]) << 8) | buffer[1];
garfieldsg 0:662207e34fba 2949 }
garfieldsg 0:662207e34fba 2950
garfieldsg 0:662207e34fba 2951 // FIFO_R_W register
garfieldsg 0:662207e34fba 2952
garfieldsg 0:662207e34fba 2953 /** Get byte from FIFO buffer.
garfieldsg 0:662207e34fba 2954 * This register is used to read and write data from the FIFO buffer. Data is
garfieldsg 0:662207e34fba 2955 * written to the FIFO in order of register number (from lowest to highest). If
garfieldsg 0:662207e34fba 2956 * all the FIFO enable flags (see below) are enabled and all External Sensor
garfieldsg 0:662207e34fba 2957 * Data registers (Registers 73 to 96) are associated with a Slave device, the
garfieldsg 0:662207e34fba 2958 * contents of registers 59 through 96 will be written in order at the Sample
garfieldsg 0:662207e34fba 2959 * Rate.
garfieldsg 0:662207e34fba 2960 *
garfieldsg 0:662207e34fba 2961 * The contents of the sensor data registers (Registers 59 to 96) are written
garfieldsg 0:662207e34fba 2962 * into the FIFO buffer when their corresponding FIFO enable flags are set to 1
garfieldsg 0:662207e34fba 2963 * in FIFO_EN (Register 35). An additional flag for the sensor data registers
garfieldsg 0:662207e34fba 2964 * associated with I2C Slave 3 can be found in I2C_MST_CTRL (Register 36).
garfieldsg 0:662207e34fba 2965 *
garfieldsg 0:662207e34fba 2966 * If the FIFO buffer has overflowed, the status bit FIFO_OFLOW_INT is
garfieldsg 0:662207e34fba 2967 * automatically set to 1. This bit is located in INT_STATUS (Register 58).
garfieldsg 0:662207e34fba 2968 * When the FIFO buffer has overflowed, the oldest data will be lost and new
garfieldsg 0:662207e34fba 2969 * data will be written to the FIFO.
garfieldsg 0:662207e34fba 2970 *
garfieldsg 0:662207e34fba 2971 * If the FIFO buffer is empty, reading this register will return the last byte
garfieldsg 0:662207e34fba 2972 * that was previously read from the FIFO until new data is available. The user
garfieldsg 0:662207e34fba 2973 * should check FIFO_COUNT to ensure that the FIFO buffer is not read when
garfieldsg 0:662207e34fba 2974 * empty.
garfieldsg 0:662207e34fba 2975 *
garfieldsg 0:662207e34fba 2976 * @return Byte from FIFO buffer
garfieldsg 0:662207e34fba 2977 */
garfieldsg 0:662207e34fba 2978 uint8_t MPU6050::getFIFOByte()
garfieldsg 0:662207e34fba 2979 {
garfieldsg 0:662207e34fba 2980 i2Cdev.readByte(devAddr, MPU6050_RA_FIFO_R_W, buffer);
garfieldsg 0:662207e34fba 2981 return buffer[0];
garfieldsg 0:662207e34fba 2982 }
garfieldsg 0:662207e34fba 2983 void MPU6050::getFIFOBytes(uint8_t *data, uint8_t length)
garfieldsg 0:662207e34fba 2984 {
garfieldsg 0:662207e34fba 2985 i2Cdev.readBytes(devAddr, MPU6050_RA_FIFO_R_W, length, data);
garfieldsg 0:662207e34fba 2986 }
garfieldsg 0:662207e34fba 2987 /** Write byte to FIFO buffer.
garfieldsg 0:662207e34fba 2988 * @see getFIFOByte()
garfieldsg 0:662207e34fba 2989 * @see MPU6050_RA_FIFO_R_W
garfieldsg 0:662207e34fba 2990 */
garfieldsg 0:662207e34fba 2991 void MPU6050::setFIFOByte(uint8_t data)
garfieldsg 0:662207e34fba 2992 {
garfieldsg 0:662207e34fba 2993 i2Cdev.writeByte(devAddr, MPU6050_RA_FIFO_R_W, data);
garfieldsg 0:662207e34fba 2994 }
garfieldsg 0:662207e34fba 2995
garfieldsg 0:662207e34fba 2996 // WHO_AM_I register
garfieldsg 0:662207e34fba 2997
garfieldsg 0:662207e34fba 2998 /** Get Device ID.
garfieldsg 0:662207e34fba 2999 * This register is used to verify the identity of the device (0b110100, 0x34).
garfieldsg 0:662207e34fba 3000 * @return Device ID (6 bits only! should be 0x34)
garfieldsg 0:662207e34fba 3001 * @see MPU6050_RA_WHO_AM_I
garfieldsg 0:662207e34fba 3002 * @see MPU6050_WHO_AM_I_BIT
garfieldsg 0:662207e34fba 3003 * @see MPU6050_WHO_AM_I_LENGTH
garfieldsg 0:662207e34fba 3004 */
garfieldsg 0:662207e34fba 3005 uint8_t MPU6050::getDeviceID()
garfieldsg 0:662207e34fba 3006 {
garfieldsg 0:662207e34fba 3007 i2Cdev.readBits(devAddr, MPU6050_RA_WHO_AM_I, MPU6050_WHO_AM_I_BIT, MPU6050_WHO_AM_I_LENGTH, buffer);
garfieldsg 0:662207e34fba 3008 return buffer[0];
garfieldsg 0:662207e34fba 3009 }
garfieldsg 0:662207e34fba 3010 /** Set Device ID.
garfieldsg 0:662207e34fba 3011 * Write a new ID into the WHO_AM_I register (no idea why this should ever be
garfieldsg 0:662207e34fba 3012 * necessary though).
garfieldsg 0:662207e34fba 3013 * @param id New device ID to set.
garfieldsg 0:662207e34fba 3014 * @see getDeviceID()
garfieldsg 0:662207e34fba 3015 * @see MPU6050_RA_WHO_AM_I
garfieldsg 0:662207e34fba 3016 * @see MPU6050_WHO_AM_I_BIT
garfieldsg 0:662207e34fba 3017 * @see MPU6050_WHO_AM_I_LENGTH
garfieldsg 0:662207e34fba 3018 */
garfieldsg 0:662207e34fba 3019 void MPU6050::setDeviceID(uint8_t id)
garfieldsg 0:662207e34fba 3020 {
garfieldsg 0:662207e34fba 3021 i2Cdev.writeBits(devAddr, MPU6050_RA_WHO_AM_I, MPU6050_WHO_AM_I_BIT, MPU6050_WHO_AM_I_LENGTH, id);
garfieldsg 0:662207e34fba 3022 }
garfieldsg 0:662207e34fba 3023
garfieldsg 0:662207e34fba 3024 // ======== UNDOCUMENTED/DMP REGISTERS/METHODS ========
garfieldsg 0:662207e34fba 3025
garfieldsg 0:662207e34fba 3026 // XG_OFFS_TC register
garfieldsg 0:662207e34fba 3027
garfieldsg 0:662207e34fba 3028 uint8_t MPU6050::getOTPBankValid()
garfieldsg 0:662207e34fba 3029 {
garfieldsg 0:662207e34fba 3030 i2Cdev.readBit(devAddr, MPU6050_RA_XG_OFFS_TC, MPU6050_TC_OTP_BNK_VLD_BIT, buffer);
garfieldsg 0:662207e34fba 3031 return buffer[0];
garfieldsg 0:662207e34fba 3032 }
garfieldsg 0:662207e34fba 3033 void MPU6050::setOTPBankValid(bool enabled)
garfieldsg 0:662207e34fba 3034 {
garfieldsg 0:662207e34fba 3035 i2Cdev.writeBit(devAddr, MPU6050_RA_XG_OFFS_TC, MPU6050_TC_OTP_BNK_VLD_BIT, enabled);
garfieldsg 0:662207e34fba 3036 }
garfieldsg 0:662207e34fba 3037 int8_t MPU6050::getXGyroOffset()
garfieldsg 0:662207e34fba 3038 {
garfieldsg 0:662207e34fba 3039 i2Cdev.readBits(devAddr, MPU6050_RA_XG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, buffer);
garfieldsg 0:662207e34fba 3040 return buffer[0];
garfieldsg 0:662207e34fba 3041 }
garfieldsg 0:662207e34fba 3042 void MPU6050::setXGyroOffset(int8_t offset)
garfieldsg 0:662207e34fba 3043 {
garfieldsg 0:662207e34fba 3044 i2Cdev.writeBits(devAddr, MPU6050_RA_XG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, offset);
garfieldsg 0:662207e34fba 3045 }
garfieldsg 0:662207e34fba 3046
garfieldsg 0:662207e34fba 3047 // YG_OFFS_TC register
garfieldsg 0:662207e34fba 3048
garfieldsg 0:662207e34fba 3049 int8_t MPU6050::getYGyroOffset()
garfieldsg 0:662207e34fba 3050 {
garfieldsg 0:662207e34fba 3051 i2Cdev.readBits(devAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, buffer);
garfieldsg 0:662207e34fba 3052 return buffer[0];
garfieldsg 0:662207e34fba 3053 }
garfieldsg 0:662207e34fba 3054 void MPU6050::setYGyroOffset(int8_t offset)
garfieldsg 0:662207e34fba 3055 {
garfieldsg 0:662207e34fba 3056 i2Cdev.writeBits(devAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, offset);
garfieldsg 0:662207e34fba 3057 }
garfieldsg 0:662207e34fba 3058
garfieldsg 0:662207e34fba 3059 // ZG_OFFS_TC register
garfieldsg 0:662207e34fba 3060
garfieldsg 0:662207e34fba 3061 int8_t MPU6050::getZGyroOffset()
garfieldsg 0:662207e34fba 3062 {
garfieldsg 0:662207e34fba 3063 i2Cdev.readBits(devAddr, MPU6050_RA_ZG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, buffer);
garfieldsg 0:662207e34fba 3064 return buffer[0];
garfieldsg 0:662207e34fba 3065 }
garfieldsg 0:662207e34fba 3066 void MPU6050::setZGyroOffset(int8_t offset)
garfieldsg 0:662207e34fba 3067 {
garfieldsg 0:662207e34fba 3068 i2Cdev.writeBits(devAddr, MPU6050_RA_ZG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, offset);
garfieldsg 0:662207e34fba 3069 }
garfieldsg 0:662207e34fba 3070
garfieldsg 0:662207e34fba 3071 // X_FINE_GAIN register
garfieldsg 0:662207e34fba 3072
garfieldsg 0:662207e34fba 3073 int8_t MPU6050::getXFineGain()
garfieldsg 0:662207e34fba 3074 {
garfieldsg 0:662207e34fba 3075 i2Cdev.readByte(devAddr, MPU6050_RA_X_FINE_GAIN, buffer);
garfieldsg 0:662207e34fba 3076 return buffer[0];
garfieldsg 0:662207e34fba 3077 }
garfieldsg 0:662207e34fba 3078 void MPU6050::setXFineGain(int8_t gain)
garfieldsg 0:662207e34fba 3079 {
garfieldsg 0:662207e34fba 3080 i2Cdev.writeByte(devAddr, MPU6050_RA_X_FINE_GAIN, gain);
garfieldsg 0:662207e34fba 3081 }
garfieldsg 0:662207e34fba 3082
garfieldsg 0:662207e34fba 3083 // Y_FINE_GAIN register
garfieldsg 0:662207e34fba 3084
garfieldsg 0:662207e34fba 3085 int8_t MPU6050::getYFineGain()
garfieldsg 0:662207e34fba 3086 {
garfieldsg 0:662207e34fba 3087 i2Cdev.readByte(devAddr, MPU6050_RA_Y_FINE_GAIN, buffer);
garfieldsg 0:662207e34fba 3088 return buffer[0];
garfieldsg 0:662207e34fba 3089 }
garfieldsg 0:662207e34fba 3090 void MPU6050::setYFineGain(int8_t gain)
garfieldsg 0:662207e34fba 3091 {
garfieldsg 0:662207e34fba 3092 i2Cdev.writeByte(devAddr, MPU6050_RA_Y_FINE_GAIN, gain);
garfieldsg 0:662207e34fba 3093 }
garfieldsg 0:662207e34fba 3094
garfieldsg 0:662207e34fba 3095 // Z_FINE_GAIN register
garfieldsg 0:662207e34fba 3096
garfieldsg 0:662207e34fba 3097 int8_t MPU6050::getZFineGain()
garfieldsg 0:662207e34fba 3098 {
garfieldsg 0:662207e34fba 3099 i2Cdev.readByte(devAddr, MPU6050_RA_Z_FINE_GAIN, buffer);
garfieldsg 0:662207e34fba 3100 return buffer[0];
garfieldsg 0:662207e34fba 3101 }
garfieldsg 0:662207e34fba 3102 void MPU6050::setZFineGain(int8_t gain)
garfieldsg 0:662207e34fba 3103 {
garfieldsg 0:662207e34fba 3104 i2Cdev.writeByte(devAddr, MPU6050_RA_Z_FINE_GAIN, gain);
garfieldsg 0:662207e34fba 3105 }
garfieldsg 0:662207e34fba 3106
garfieldsg 0:662207e34fba 3107 // XA_OFFS_* registers
garfieldsg 0:662207e34fba 3108
garfieldsg 0:662207e34fba 3109 int16_t MPU6050::getXAccelOffset()
garfieldsg 0:662207e34fba 3110 {
garfieldsg 0:662207e34fba 3111 i2Cdev.readBytes(devAddr, MPU6050_RA_XA_OFFS_H, 2, buffer);
garfieldsg 0:662207e34fba 3112 return (((int16_t)buffer[0]) << 8) | buffer[1];
garfieldsg 0:662207e34fba 3113 }
garfieldsg 0:662207e34fba 3114 void MPU6050::setXAccelOffset(int16_t offset)
garfieldsg 0:662207e34fba 3115 {
garfieldsg 0:662207e34fba 3116 i2Cdev.writeWord(devAddr, MPU6050_RA_XA_OFFS_H, offset);
garfieldsg 0:662207e34fba 3117 }
garfieldsg 0:662207e34fba 3118
garfieldsg 0:662207e34fba 3119 // YA_OFFS_* register
garfieldsg 0:662207e34fba 3120
garfieldsg 0:662207e34fba 3121 int16_t MPU6050::getYAccelOffset()
garfieldsg 0:662207e34fba 3122 {
garfieldsg 0:662207e34fba 3123 i2Cdev.readBytes(devAddr, MPU6050_RA_YA_OFFS_H, 2, buffer);
garfieldsg 0:662207e34fba 3124 return (((int16_t)buffer[0]) << 8) | buffer[1];
garfieldsg 0:662207e34fba 3125 }
garfieldsg 0:662207e34fba 3126 void MPU6050::setYAccelOffset(int16_t offset)
garfieldsg 0:662207e34fba 3127 {
garfieldsg 0:662207e34fba 3128 i2Cdev.writeWord(devAddr, MPU6050_RA_YA_OFFS_H, offset);
garfieldsg 0:662207e34fba 3129 }
garfieldsg 0:662207e34fba 3130
garfieldsg 0:662207e34fba 3131 // ZA_OFFS_* register
garfieldsg 0:662207e34fba 3132
garfieldsg 0:662207e34fba 3133 int16_t MPU6050::getZAccelOffset()
garfieldsg 0:662207e34fba 3134 {
garfieldsg 0:662207e34fba 3135 i2Cdev.readBytes(devAddr, MPU6050_RA_ZA_OFFS_H, 2, buffer);
garfieldsg 0:662207e34fba 3136 return (((int16_t)buffer[0]) << 8) | buffer[1];
garfieldsg 0:662207e34fba 3137 }
garfieldsg 0:662207e34fba 3138 void MPU6050::setZAccelOffset(int16_t offset)
garfieldsg 0:662207e34fba 3139 {
garfieldsg 0:662207e34fba 3140 i2Cdev.writeWord(devAddr, MPU6050_RA_ZA_OFFS_H, offset);
garfieldsg 0:662207e34fba 3141 }
garfieldsg 0:662207e34fba 3142
garfieldsg 0:662207e34fba 3143 // XG_OFFS_USR* registers
garfieldsg 0:662207e34fba 3144
garfieldsg 0:662207e34fba 3145 int16_t MPU6050::getXGyroOffsetUser()
garfieldsg 0:662207e34fba 3146 {
garfieldsg 0:662207e34fba 3147 i2Cdev.readBytes(devAddr, MPU6050_RA_XG_OFFS_USRH, 2, buffer);
garfieldsg 0:662207e34fba 3148 return (((int16_t)buffer[0]) << 8) | buffer[1];
garfieldsg 0:662207e34fba 3149 }
garfieldsg 0:662207e34fba 3150 void MPU6050::setXGyroOffsetUser(int16_t offset)
garfieldsg 0:662207e34fba 3151 {
garfieldsg 0:662207e34fba 3152 i2Cdev.writeWord(devAddr, MPU6050_RA_XG_OFFS_USRH, offset);
garfieldsg 0:662207e34fba 3153 }
garfieldsg 0:662207e34fba 3154
garfieldsg 0:662207e34fba 3155 // YG_OFFS_USR* register
garfieldsg 0:662207e34fba 3156
garfieldsg 0:662207e34fba 3157 int16_t MPU6050::getYGyroOffsetUser()
garfieldsg 0:662207e34fba 3158 {
garfieldsg 0:662207e34fba 3159 i2Cdev.readBytes(devAddr, MPU6050_RA_YG_OFFS_USRH, 2, buffer);
garfieldsg 0:662207e34fba 3160 return (((int16_t)buffer[0]) << 8) | buffer[1];
garfieldsg 0:662207e34fba 3161 }
garfieldsg 0:662207e34fba 3162 void MPU6050::setYGyroOffsetUser(int16_t offset)
garfieldsg 0:662207e34fba 3163 {
garfieldsg 0:662207e34fba 3164 i2Cdev.writeWord(devAddr, MPU6050_RA_YG_OFFS_USRH, offset);
garfieldsg 0:662207e34fba 3165 }
garfieldsg 0:662207e34fba 3166
garfieldsg 0:662207e34fba 3167 // ZG_OFFS_USR* register
garfieldsg 0:662207e34fba 3168
garfieldsg 0:662207e34fba 3169 int16_t MPU6050::getZGyroOffsetUser()
garfieldsg 0:662207e34fba 3170 {
garfieldsg 0:662207e34fba 3171 i2Cdev.readBytes(devAddr, MPU6050_RA_ZG_OFFS_USRH, 2, buffer);
garfieldsg 0:662207e34fba 3172 return (((int16_t)buffer[0]) << 8) | buffer[1];
garfieldsg 0:662207e34fba 3173 }
garfieldsg 0:662207e34fba 3174 void MPU6050::setZGyroOffsetUser(int16_t offset)
garfieldsg 0:662207e34fba 3175 {
garfieldsg 0:662207e34fba 3176 i2Cdev.writeWord(devAddr, MPU6050_RA_ZG_OFFS_USRH, offset);
garfieldsg 0:662207e34fba 3177 }
garfieldsg 0:662207e34fba 3178
garfieldsg 0:662207e34fba 3179 // INT_ENABLE register (DMP functions)
garfieldsg 0:662207e34fba 3180
garfieldsg 0:662207e34fba 3181 bool MPU6050::getIntPLLReadyEnabled()
garfieldsg 0:662207e34fba 3182 {
garfieldsg 0:662207e34fba 3183 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_PLL_RDY_INT_BIT, buffer);
garfieldsg 0:662207e34fba 3184 return buffer[0];
garfieldsg 0:662207e34fba 3185 }
garfieldsg 0:662207e34fba 3186 void MPU6050::setIntPLLReadyEnabled(bool enabled)
garfieldsg 0:662207e34fba 3187 {
garfieldsg 0:662207e34fba 3188 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_PLL_RDY_INT_BIT, enabled);
garfieldsg 0:662207e34fba 3189 }
garfieldsg 0:662207e34fba 3190 bool MPU6050::getIntDMPEnabled()
garfieldsg 0:662207e34fba 3191 {
garfieldsg 0:662207e34fba 3192 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_DMP_INT_BIT, buffer);
garfieldsg 0:662207e34fba 3193 return buffer[0];
garfieldsg 0:662207e34fba 3194 }
garfieldsg 0:662207e34fba 3195 void MPU6050::setIntDMPEnabled(bool enabled)
garfieldsg 0:662207e34fba 3196 {
garfieldsg 0:662207e34fba 3197 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_DMP_INT_BIT, enabled);
garfieldsg 0:662207e34fba 3198 }
garfieldsg 0:662207e34fba 3199
garfieldsg 0:662207e34fba 3200 // DMP_INT_STATUS
garfieldsg 0:662207e34fba 3201
garfieldsg 0:662207e34fba 3202 bool MPU6050::getDMPInt5Status()
garfieldsg 0:662207e34fba 3203 {
garfieldsg 0:662207e34fba 3204 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_5_BIT, buffer);
garfieldsg 0:662207e34fba 3205 return buffer[0];
garfieldsg 0:662207e34fba 3206 }
garfieldsg 0:662207e34fba 3207 bool MPU6050::getDMPInt4Status()
garfieldsg 0:662207e34fba 3208 {
garfieldsg 0:662207e34fba 3209 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_4_BIT, buffer);
garfieldsg 0:662207e34fba 3210 return buffer[0];
garfieldsg 0:662207e34fba 3211 }
garfieldsg 0:662207e34fba 3212 bool MPU6050::getDMPInt3Status()
garfieldsg 0:662207e34fba 3213 {
garfieldsg 0:662207e34fba 3214 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_3_BIT, buffer);
garfieldsg 0:662207e34fba 3215 return buffer[0];
garfieldsg 0:662207e34fba 3216 }
garfieldsg 0:662207e34fba 3217 bool MPU6050::getDMPInt2Status()
garfieldsg 0:662207e34fba 3218 {
garfieldsg 0:662207e34fba 3219 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_2_BIT, buffer);
garfieldsg 0:662207e34fba 3220 return buffer[0];
garfieldsg 0:662207e34fba 3221 }
garfieldsg 0:662207e34fba 3222 bool MPU6050::getDMPInt1Status()
garfieldsg 0:662207e34fba 3223 {
garfieldsg 0:662207e34fba 3224 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_1_BIT, buffer);
garfieldsg 0:662207e34fba 3225 return buffer[0];
garfieldsg 0:662207e34fba 3226 }
garfieldsg 0:662207e34fba 3227 bool MPU6050::getDMPInt0Status()
garfieldsg 0:662207e34fba 3228 {
garfieldsg 0:662207e34fba 3229 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_0_BIT, buffer);
garfieldsg 0:662207e34fba 3230 return buffer[0];
garfieldsg 0:662207e34fba 3231 }
garfieldsg 0:662207e34fba 3232
garfieldsg 0:662207e34fba 3233 // INT_STATUS register (DMP functions)
garfieldsg 0:662207e34fba 3234
garfieldsg 0:662207e34fba 3235 bool MPU6050::getIntPLLReadyStatus()
garfieldsg 0:662207e34fba 3236 {
garfieldsg 0:662207e34fba 3237 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_PLL_RDY_INT_BIT, buffer);
garfieldsg 0:662207e34fba 3238 return buffer[0];
garfieldsg 0:662207e34fba 3239 }
garfieldsg 0:662207e34fba 3240 bool MPU6050::getIntDMPStatus()
garfieldsg 0:662207e34fba 3241 {
garfieldsg 0:662207e34fba 3242 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_DMP_INT_BIT, buffer);
garfieldsg 0:662207e34fba 3243 return buffer[0];
garfieldsg 0:662207e34fba 3244 }
garfieldsg 0:662207e34fba 3245
garfieldsg 0:662207e34fba 3246 // USER_CTRL register (DMP functions)
garfieldsg 0:662207e34fba 3247
garfieldsg 0:662207e34fba 3248 bool MPU6050::getDMPEnabled()
garfieldsg 0:662207e34fba 3249 {
garfieldsg 0:662207e34fba 3250 i2Cdev.readBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_DMP_EN_BIT, buffer);
garfieldsg 0:662207e34fba 3251 return buffer[0];
garfieldsg 0:662207e34fba 3252 }
garfieldsg 0:662207e34fba 3253 void MPU6050::setDMPEnabled(bool enabled)
garfieldsg 0:662207e34fba 3254 {
garfieldsg 0:662207e34fba 3255 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_DMP_EN_BIT, enabled);
garfieldsg 0:662207e34fba 3256 }
garfieldsg 0:662207e34fba 3257 void MPU6050::resetDMP()
garfieldsg 0:662207e34fba 3258 {
garfieldsg 0:662207e34fba 3259 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_DMP_RESET_BIT, true);
garfieldsg 0:662207e34fba 3260 }
garfieldsg 0:662207e34fba 3261
garfieldsg 0:662207e34fba 3262 // BANK_SEL register
garfieldsg 0:662207e34fba 3263
garfieldsg 0:662207e34fba 3264 void MPU6050::setMemoryBank(uint8_t bank, bool prefetchEnabled, bool userBank)
garfieldsg 0:662207e34fba 3265 {
garfieldsg 0:662207e34fba 3266 bank &= 0x1F;
garfieldsg 0:662207e34fba 3267 if (userBank) bank |= 0x20;
garfieldsg 0:662207e34fba 3268 if (prefetchEnabled) bank |= 0x40;
garfieldsg 0:662207e34fba 3269 i2Cdev.writeByte(devAddr, MPU6050_RA_BANK_SEL, bank);
garfieldsg 0:662207e34fba 3270 }
garfieldsg 0:662207e34fba 3271
garfieldsg 0:662207e34fba 3272 // MEM_START_ADDR register
garfieldsg 0:662207e34fba 3273
garfieldsg 0:662207e34fba 3274 void MPU6050::setMemoryStartAddress(uint8_t address)
garfieldsg 0:662207e34fba 3275 {
garfieldsg 0:662207e34fba 3276 i2Cdev.writeByte(devAddr, MPU6050_RA_MEM_START_ADDR, address);
garfieldsg 0:662207e34fba 3277 }
garfieldsg 0:662207e34fba 3278
garfieldsg 0:662207e34fba 3279 // MEM_R_W register
garfieldsg 0:662207e34fba 3280
garfieldsg 0:662207e34fba 3281 uint8_t MPU6050::readMemoryByte()
garfieldsg 0:662207e34fba 3282 {
garfieldsg 0:662207e34fba 3283 i2Cdev.readByte(devAddr, MPU6050_RA_MEM_R_W, buffer);
garfieldsg 0:662207e34fba 3284 return buffer[0];
garfieldsg 0:662207e34fba 3285 }
garfieldsg 0:662207e34fba 3286 void MPU6050::writeMemoryByte(uint8_t data)
garfieldsg 0:662207e34fba 3287 {
garfieldsg 0:662207e34fba 3288 i2Cdev.writeByte(devAddr, MPU6050_RA_MEM_R_W, data);
garfieldsg 0:662207e34fba 3289 }
garfieldsg 0:662207e34fba 3290 void MPU6050::readMemoryBlock(uint8_t *data, uint16_t dataSize, uint8_t bank, uint8_t address)
garfieldsg 0:662207e34fba 3291 {
garfieldsg 0:662207e34fba 3292 setMemoryBank(bank);
garfieldsg 0:662207e34fba 3293 setMemoryStartAddress(address);
garfieldsg 0:662207e34fba 3294 uint8_t chunkSize;
garfieldsg 0:662207e34fba 3295 for (uint16_t i = 0; i < dataSize;) {
garfieldsg 0:662207e34fba 3296 // determine correct chunk size according to bank position and data size
garfieldsg 0:662207e34fba 3297 chunkSize = MPU6050_DMP_MEMORY_CHUNK_SIZE;
garfieldsg 0:662207e34fba 3298
garfieldsg 0:662207e34fba 3299 // make sure we don't go past the data size
garfieldsg 0:662207e34fba 3300 if (i + chunkSize > dataSize) chunkSize = dataSize - i;
garfieldsg 0:662207e34fba 3301
garfieldsg 0:662207e34fba 3302 // make sure this chunk doesn't go past the bank boundary (256 bytes)
garfieldsg 0:662207e34fba 3303 if (chunkSize > 256 - address) chunkSize = 256 - address;
garfieldsg 0:662207e34fba 3304
garfieldsg 0:662207e34fba 3305 // read the chunk of data as specified
garfieldsg 0:662207e34fba 3306 i2Cdev.readBytes(devAddr, MPU6050_RA_MEM_R_W, chunkSize, data + i);
garfieldsg 0:662207e34fba 3307
garfieldsg 0:662207e34fba 3308 // increase byte index by [chunkSize]
garfieldsg 0:662207e34fba 3309 i += chunkSize;
garfieldsg 0:662207e34fba 3310
garfieldsg 0:662207e34fba 3311 // uint8_t automatically wraps to 0 at 256
garfieldsg 0:662207e34fba 3312 address += chunkSize;
garfieldsg 0:662207e34fba 3313
garfieldsg 0:662207e34fba 3314 // if we aren't done, update bank (if necessary) and address
garfieldsg 0:662207e34fba 3315 if (i < dataSize) {
garfieldsg 0:662207e34fba 3316 if (address == 0) bank++;
garfieldsg 0:662207e34fba 3317 setMemoryBank(bank);
garfieldsg 0:662207e34fba 3318 setMemoryStartAddress(address);
garfieldsg 0:662207e34fba 3319 }
garfieldsg 0:662207e34fba 3320 }
garfieldsg 0:662207e34fba 3321 }
garfieldsg 0:662207e34fba 3322 bool MPU6050::writeMemoryBlock(const uint8_t *data, uint16_t dataSize, uint8_t bank, uint8_t address, bool verify, bool useProgMem)
garfieldsg 0:662207e34fba 3323 {
garfieldsg 0:662207e34fba 3324 setMemoryBank(bank);
garfieldsg 0:662207e34fba 3325 setMemoryStartAddress(address);
garfieldsg 0:662207e34fba 3326 uint8_t chunkSize;
garfieldsg 0:662207e34fba 3327 uint8_t *verifyBuffer;
garfieldsg 0:662207e34fba 3328 uint8_t *progBuffer;
garfieldsg 0:662207e34fba 3329 uint16_t i;
garfieldsg 0:662207e34fba 3330 uint8_t j;
garfieldsg 0:662207e34fba 3331 if (verify) verifyBuffer = (uint8_t *)malloc(MPU6050_DMP_MEMORY_CHUNK_SIZE);
garfieldsg 0:662207e34fba 3332 if (useProgMem) progBuffer = (uint8_t *)malloc(MPU6050_DMP_MEMORY_CHUNK_SIZE);
garfieldsg 0:662207e34fba 3333 for (i = 0; i < dataSize;) {
garfieldsg 0:662207e34fba 3334 // determine correct chunk size according to bank position and data size
garfieldsg 0:662207e34fba 3335 chunkSize = MPU6050_DMP_MEMORY_CHUNK_SIZE;
garfieldsg 0:662207e34fba 3336
garfieldsg 0:662207e34fba 3337 // make sure we don't go past the data size
garfieldsg 0:662207e34fba 3338 if (i + chunkSize > dataSize) chunkSize = dataSize - i;
garfieldsg 0:662207e34fba 3339
garfieldsg 0:662207e34fba 3340 // make sure this chunk doesn't go past the bank boundary (256 bytes)
garfieldsg 0:662207e34fba 3341 if (chunkSize > 256 - address) chunkSize = 256 - address;
garfieldsg 0:662207e34fba 3342
garfieldsg 0:662207e34fba 3343 if (useProgMem) {
garfieldsg 0:662207e34fba 3344 // write the chunk of data as specified
garfieldsg 0:662207e34fba 3345 for (j = 0; j < chunkSize; j++) progBuffer[j] = pgm_read_byte(data + i + j);
garfieldsg 0:662207e34fba 3346 } else {
garfieldsg 0:662207e34fba 3347 // write the chunk of data as specified
garfieldsg 0:662207e34fba 3348 progBuffer = (uint8_t *)data + i;
garfieldsg 0:662207e34fba 3349 }
garfieldsg 0:662207e34fba 3350
garfieldsg 0:662207e34fba 3351 i2Cdev.writeBytes(devAddr, MPU6050_RA_MEM_R_W, chunkSize, progBuffer);
garfieldsg 0:662207e34fba 3352
garfieldsg 0:662207e34fba 3353 // verify data if needed
garfieldsg 0:662207e34fba 3354 if (verify && verifyBuffer) {
garfieldsg 0:662207e34fba 3355 setMemoryBank(bank);
garfieldsg 0:662207e34fba 3356 setMemoryStartAddress(address);
garfieldsg 0:662207e34fba 3357 i2Cdev.readBytes(devAddr, MPU6050_RA_MEM_R_W, chunkSize, verifyBuffer);
garfieldsg 0:662207e34fba 3358 if (memcmp(progBuffer, verifyBuffer, chunkSize) != 0) {
garfieldsg 0:662207e34fba 3359 /*Serial.print("Block write verification error, bank ");
garfieldsg 0:662207e34fba 3360 Serial.print(bank, DEC);
garfieldsg 0:662207e34fba 3361 Serial.print(", address ");
garfieldsg 0:662207e34fba 3362 Serial.print(address, DEC);
garfieldsg 0:662207e34fba 3363 Serial.print("!\nExpected:");
garfieldsg 0:662207e34fba 3364 for (j = 0; j < chunkSize; j++) {
garfieldsg 0:662207e34fba 3365 Serial.print(" 0x");
garfieldsg 0:662207e34fba 3366 if (progBuffer[j] < 16) Serial.print("0");
garfieldsg 0:662207e34fba 3367 Serial.print(progBuffer[j], HEX);
garfieldsg 0:662207e34fba 3368 }
garfieldsg 0:662207e34fba 3369 Serial.print("\nReceived:");
garfieldsg 0:662207e34fba 3370 for (uint8_t j = 0; j < chunkSize; j++) {
garfieldsg 0:662207e34fba 3371 Serial.print(" 0x");
garfieldsg 0:662207e34fba 3372 if (verifyBuffer[i + j] < 16) Serial.print("0");
garfieldsg 0:662207e34fba 3373 Serial.print(verifyBuffer[i + j], HEX);
garfieldsg 0:662207e34fba 3374 }
garfieldsg 0:662207e34fba 3375 Serial.print("\n");*/
garfieldsg 0:662207e34fba 3376 free(verifyBuffer);
garfieldsg 0:662207e34fba 3377 if (useProgMem) free(progBuffer);
garfieldsg 0:662207e34fba 3378 return false; // uh oh.
garfieldsg 0:662207e34fba 3379 }
garfieldsg 0:662207e34fba 3380 }
garfieldsg 0:662207e34fba 3381
garfieldsg 0:662207e34fba 3382 // increase byte index by [chunkSize]
garfieldsg 0:662207e34fba 3383 i += chunkSize;
garfieldsg 0:662207e34fba 3384
garfieldsg 0:662207e34fba 3385 // uint8_t automatically wraps to 0 at 256
garfieldsg 0:662207e34fba 3386 address += chunkSize;
garfieldsg 0:662207e34fba 3387
garfieldsg 0:662207e34fba 3388 // if we aren't done, update bank (if necessary) and address
garfieldsg 0:662207e34fba 3389 if (i < dataSize) {
garfieldsg 0:662207e34fba 3390 if (address == 0) bank++;
garfieldsg 0:662207e34fba 3391 setMemoryBank(bank);
garfieldsg 0:662207e34fba 3392 setMemoryStartAddress(address);
garfieldsg 0:662207e34fba 3393 }
garfieldsg 0:662207e34fba 3394 }
garfieldsg 0:662207e34fba 3395 if (verify) free(verifyBuffer);
garfieldsg 0:662207e34fba 3396 if (useProgMem) free(progBuffer);
garfieldsg 0:662207e34fba 3397 return true;
garfieldsg 0:662207e34fba 3398 }
garfieldsg 0:662207e34fba 3399 bool MPU6050::writeProgMemoryBlock(const uint8_t *data, uint16_t dataSize, uint8_t bank, uint8_t address, bool verify)
garfieldsg 0:662207e34fba 3400 {
garfieldsg 0:662207e34fba 3401 return writeMemoryBlock(data, dataSize, bank, address, verify, true);
garfieldsg 0:662207e34fba 3402 }
garfieldsg 0:662207e34fba 3403 bool MPU6050::writeDMPConfigurationSet(const uint8_t *data, uint16_t dataSize, bool useProgMem)
garfieldsg 0:662207e34fba 3404 {
garfieldsg 0:662207e34fba 3405 uint8_t *progBuffer, success, special;
garfieldsg 0:662207e34fba 3406 uint16_t i, j;
garfieldsg 0:662207e34fba 3407 if (useProgMem) {
garfieldsg 0:662207e34fba 3408 progBuffer = (uint8_t *)malloc(8); // assume 8-byte blocks, realloc later if necessary
garfieldsg 0:662207e34fba 3409 }
garfieldsg 0:662207e34fba 3410
garfieldsg 0:662207e34fba 3411 // config set data is a long string of blocks with the following structure:
garfieldsg 0:662207e34fba 3412 // [bank] [offset] [length] [byte[0], byte[1], ..., byte[length]]
garfieldsg 0:662207e34fba 3413 uint8_t bank, offset, length;
garfieldsg 0:662207e34fba 3414 for (i = 0; i < dataSize;) {
garfieldsg 0:662207e34fba 3415 if (useProgMem) {
garfieldsg 0:662207e34fba 3416 bank = pgm_read_byte(data + i++);
garfieldsg 0:662207e34fba 3417 offset = pgm_read_byte(data + i++);
garfieldsg 0:662207e34fba 3418 length = pgm_read_byte(data + i++);
garfieldsg 0:662207e34fba 3419 } else {
garfieldsg 0:662207e34fba 3420 bank = data[i++];
garfieldsg 0:662207e34fba 3421 offset = data[i++];
garfieldsg 0:662207e34fba 3422 length = data[i++];
garfieldsg 0:662207e34fba 3423 }
garfieldsg 0:662207e34fba 3424
garfieldsg 0:662207e34fba 3425 // write data or perform special action
garfieldsg 0:662207e34fba 3426 if (length > 0) {
garfieldsg 0:662207e34fba 3427 // regular block of data to write
garfieldsg 0:662207e34fba 3428 /*Serial.print("Writing config block to bank ");
garfieldsg 0:662207e34fba 3429 Serial.print(bank);
garfieldsg 0:662207e34fba 3430 Serial.print(", offset ");
garfieldsg 0:662207e34fba 3431 Serial.print(offset);
garfieldsg 0:662207e34fba 3432 Serial.print(", length=");
garfieldsg 0:662207e34fba 3433 Serial.println(length);*/
garfieldsg 0:662207e34fba 3434 if (useProgMem) {
garfieldsg 0:662207e34fba 3435 if (sizeof(progBuffer) < length) progBuffer = (uint8_t *)realloc(progBuffer, length);
garfieldsg 0:662207e34fba 3436 for (j = 0; j < length; j++) progBuffer[j] = pgm_read_byte(data + i + j);
garfieldsg 0:662207e34fba 3437 } else {
garfieldsg 0:662207e34fba 3438 progBuffer = (uint8_t *)data + i;
garfieldsg 0:662207e34fba 3439 }
garfieldsg 0:662207e34fba 3440 success = writeMemoryBlock(progBuffer, length, bank, offset, true);
garfieldsg 0:662207e34fba 3441 i += length;
garfieldsg 0:662207e34fba 3442 } else {
garfieldsg 0:662207e34fba 3443 // special instruction
garfieldsg 0:662207e34fba 3444 // NOTE: this kind of behavior (what and when to do certain things)
garfieldsg 0:662207e34fba 3445 // is totally undocumented. This code is in here based on observed
garfieldsg 0:662207e34fba 3446 // behavior only, and exactly why (or even whether) it has to be here
garfieldsg 0:662207e34fba 3447 // is anybody's guess for now.
garfieldsg 0:662207e34fba 3448 if (useProgMem) {
garfieldsg 0:662207e34fba 3449 special = pgm_read_byte(data + i++);
garfieldsg 0:662207e34fba 3450 } else {
garfieldsg 0:662207e34fba 3451 special = data[i++];
garfieldsg 0:662207e34fba 3452 }
garfieldsg 0:662207e34fba 3453 /*Serial.print("Special command code ");
garfieldsg 0:662207e34fba 3454 Serial.print(special, HEX);
garfieldsg 0:662207e34fba 3455 Serial.println(" found...");*/
garfieldsg 0:662207e34fba 3456 if (special == 0x01) {
garfieldsg 0:662207e34fba 3457 // enable DMP-related interrupts
garfieldsg 0:662207e34fba 3458
garfieldsg 0:662207e34fba 3459 //setIntZeroMotionEnabled(true);
garfieldsg 0:662207e34fba 3460 //setIntFIFOBufferOverflowEnabled(true);
garfieldsg 0:662207e34fba 3461 //setIntDMPEnabled(true);
garfieldsg 0:662207e34fba 3462 i2Cdev.writeByte(devAddr, MPU6050_RA_INT_ENABLE, 0x32); // single operation
garfieldsg 0:662207e34fba 3463
garfieldsg 0:662207e34fba 3464 success = true;
garfieldsg 0:662207e34fba 3465 } else {
garfieldsg 0:662207e34fba 3466 // unknown special command
garfieldsg 0:662207e34fba 3467 success = false;
garfieldsg 0:662207e34fba 3468 }
garfieldsg 0:662207e34fba 3469 }
garfieldsg 0:662207e34fba 3470
garfieldsg 0:662207e34fba 3471 if (!success) {
garfieldsg 0:662207e34fba 3472 if (useProgMem) free(progBuffer);
garfieldsg 0:662207e34fba 3473 return false; // uh oh
garfieldsg 0:662207e34fba 3474 }
garfieldsg 0:662207e34fba 3475 }
garfieldsg 0:662207e34fba 3476 if (useProgMem) free(progBuffer);
garfieldsg 0:662207e34fba 3477 return true;
garfieldsg 0:662207e34fba 3478 }
garfieldsg 0:662207e34fba 3479 bool MPU6050::writeProgDMPConfigurationSet(const uint8_t *data, uint16_t dataSize)
garfieldsg 0:662207e34fba 3480 {
garfieldsg 0:662207e34fba 3481 return writeDMPConfigurationSet(data, dataSize, false);
garfieldsg 0:662207e34fba 3482 }
garfieldsg 0:662207e34fba 3483
garfieldsg 0:662207e34fba 3484 // DMP_CFG_1 register
garfieldsg 0:662207e34fba 3485
garfieldsg 0:662207e34fba 3486 uint8_t MPU6050::getDMPConfig1()
garfieldsg 0:662207e34fba 3487 {
garfieldsg 0:662207e34fba 3488 i2Cdev.readByte(devAddr, MPU6050_RA_DMP_CFG_1, buffer);
garfieldsg 0:662207e34fba 3489 return buffer[0];
garfieldsg 0:662207e34fba 3490 }
garfieldsg 0:662207e34fba 3491 void MPU6050::setDMPConfig1(uint8_t config)
garfieldsg 0:662207e34fba 3492 {
garfieldsg 0:662207e34fba 3493 i2Cdev.writeByte(devAddr, MPU6050_RA_DMP_CFG_1, config);
garfieldsg 0:662207e34fba 3494 }
garfieldsg 0:662207e34fba 3495
garfieldsg 0:662207e34fba 3496 // DMP_CFG_2 register
garfieldsg 0:662207e34fba 3497
garfieldsg 0:662207e34fba 3498 uint8_t MPU6050::getDMPConfig2()
garfieldsg 0:662207e34fba 3499 {
garfieldsg 0:662207e34fba 3500 i2Cdev.readByte(devAddr, MPU6050_RA_DMP_CFG_2, buffer);
garfieldsg 0:662207e34fba 3501 return buffer[0];
garfieldsg 0:662207e34fba 3502 }
garfieldsg 0:662207e34fba 3503 void MPU6050::setDMPConfig2(uint8_t config)
garfieldsg 0:662207e34fba 3504 {
garfieldsg 0:662207e34fba 3505 i2Cdev.writeByte(devAddr, MPU6050_RA_DMP_CFG_2, config);
tyftyftyf 12:e32a6beb0a41 3506 }
tyftyftyf 12:e32a6beb0a41 3507
tyftyftyf 12:e32a6beb0a41 3508 // =============================================================================
tyftyftyf 12:e32a6beb0a41 3509 // =============================================================================
tyftyftyf 12:e32a6beb0a41 3510 // =============================================================================
tyftyftyf 12:e32a6beb0a41 3511 // =============================================================================
tyftyftyf 12:e32a6beb0a41 3512 // =============================================================================
tyftyftyf 12:e32a6beb0a41 3513 // =============================================================================
tyftyftyf 12:e32a6beb0a41 3514 // ============================== MPU 9250 parts ===============================
tyftyftyf 12:e32a6beb0a41 3515 // =============================================================================
tyftyftyf 12:e32a6beb0a41 3516 // =============================================================================
tyftyftyf 12:e32a6beb0a41 3517 // =============================================================================
tyftyftyf 12:e32a6beb0a41 3518 // =============================================================================
tyftyftyf 12:e32a6beb0a41 3519 // =============================================================================
tyftyftyf 12:e32a6beb0a41 3520
tyftyftyf 13:a74f2d622b54 3521 #ifdef MPU9250
tyftyftyf 12:e32a6beb0a41 3522
tyftyftyf 12:e32a6beb0a41 3523 /** initialize 9250.
tyftyftyf 12:e32a6beb0a41 3524 *
tyftyftyf 12:e32a6beb0a41 3525 */
tyftyftyf 12:e32a6beb0a41 3526 void MPU6050::initialize9250() {
tyftyftyf 12:e32a6beb0a41 3527 reset();
tyftyftyf 12:e32a6beb0a41 3528 Thread::wait(50);
tyftyftyf 12:e32a6beb0a41 3529 setStandbyDisable();
tyftyftyf 12:e32a6beb0a41 3530 setSleepEnabled(false); // thanks to Jack Elston for pointing this one out!
tyftyftyf 12:e32a6beb0a41 3531 setClockSource(MPU60X0_CLOCK_PLL_XGYRO);
tyftyftyf 12:e32a6beb0a41 3532 setFullScaleGyroRange(MPU60X0_GYRO_FS_2000);
tyftyftyf 12:e32a6beb0a41 3533 setFullScaleAccelRange(MPU60X0_ACCEL_FS_2);
tyftyftyf 12:e32a6beb0a41 3534
tyftyftyf 12:e32a6beb0a41 3535 //data = 1000 / rate - 1;
tyftyftyf 12:e32a6beb0a41 3536 //setRate(data);
tyftyftyf 12:e32a6beb0a41 3537 }
tyftyftyf 12:e32a6beb0a41 3538
tyftyftyf 15:09f072efa71e 3539 /** Disable MPU-6050 standby mode. Really only used for MPU-9250
tyftyftyf 15:09f072efa71e 3540 * @param
tyftyftyf 15:09f072efa71e 3541 * @see
tyftyftyf 15:09f072efa71e 3542 * @see MPU60X0_RA_PWR_MGMT_2
tyftyftyf 15:09f072efa71e 3543 * @see
tyftyftyf 15:09f072efa71e 3544 */
tyftyftyf 15:09f072efa71e 3545 void MPU6050::setStandbyDisable() {
tyftyftyf 15:09f072efa71e 3546 i2Cdev.writeByte(devAddr, MPU60X0_RA_PWR_MGMT_2, 0x00);
tyftyftyf 15:09f072efa71e 3547 }
tyftyftyf 15:09f072efa71e 3548
tyftyftyf 16:6be55d010301 3549 void MPU6050::initialize9250MasterMode() {
tyftyftyf 12:e32a6beb0a41 3550
tyftyftyf 12:e32a6beb0a41 3551 uint8_t buff[3];
tyftyftyf 12:e32a6beb0a41 3552 uint8_t data[7];
tyftyftyf 12:e32a6beb0a41 3553 float _magScaleX, _magScaleY, _magScaleZ;
tyftyftyf 12:e32a6beb0a41 3554
tyftyftyf 12:e32a6beb0a41 3555 //set dev address for magnetometer
tyftyftyf 12:e32a6beb0a41 3556 magDevAddr = AK8963_DEFAULT_ADDRESS;
tyftyftyf 12:e32a6beb0a41 3557
tyftyftyf 12:e32a6beb0a41 3558 Thread::wait(50);
tyftyftyf 12:e32a6beb0a41 3559 reset();
tyftyftyf 12:e32a6beb0a41 3560
tyftyftyf 12:e32a6beb0a41 3561 setStandbyDisable();
tyftyftyf 12:e32a6beb0a41 3562 setSleepEnabled(false);
tyftyftyf 12:e32a6beb0a41 3563
tyftyftyf 12:e32a6beb0a41 3564 // select clock source to gyro
tyftyftyf 12:e32a6beb0a41 3565 if( !writeRegister(MPU60X0_RA_PWR_MGMT_1, MPU60X0_CLOCK_PLL_XGYRO) ){
tyftyftyf 12:e32a6beb0a41 3566 debugSerial.printf("Clock Source Not Set\n");
tyftyftyf 12:e32a6beb0a41 3567 }
tyftyftyf 12:e32a6beb0a41 3568
tyftyftyf 12:e32a6beb0a41 3569 // enable I2C master mode
tyftyftyf 12:e32a6beb0a41 3570 if( !writeRegister(MPU60X0_RA_USER_CTRL,I2C_MST_EN) ){
tyftyftyf 12:e32a6beb0a41 3571 debugSerial.printf("Master Mode Not Set\n");
tyftyftyf 12:e32a6beb0a41 3572 }
tyftyftyf 12:e32a6beb0a41 3573
tyftyftyf 12:e32a6beb0a41 3574 // set the I2C bus speed to 400 kHz
tyftyftyf 12:e32a6beb0a41 3575 if( !writeRegister(MPU60X0_RA_I2C_MST_CTRL,I2C_MST_CLK) ){
tyftyftyf 12:e32a6beb0a41 3576 debugSerial.printf("I2C Bus Speed Not Set\n");
tyftyftyf 12:e32a6beb0a41 3577 }
tyftyftyf 12:e32a6beb0a41 3578
tyftyftyf 12:e32a6beb0a41 3579 // set AK8963 to Power Down
tyftyftyf 12:e32a6beb0a41 3580 if( !writeAKRegister(AK8963_RA_CNTL1, AK8963_MODE_POWERDOWN) ){
tyftyftyf 12:e32a6beb0a41 3581 debugSerial.printf("AK Not Powered Down\n");
tyftyftyf 12:e32a6beb0a41 3582 }
tyftyftyf 12:e32a6beb0a41 3583
tyftyftyf 12:e32a6beb0a41 3584 // reset the MPU9250
tyftyftyf 12:e32a6beb0a41 3585 writeRegister(MPU60X0_RA_PWR_MGMT_1, PWR_RESET);
tyftyftyf 12:e32a6beb0a41 3586
tyftyftyf 12:e32a6beb0a41 3587 // wait for MPU-9250 to come back up
tyftyftyf 16:6be55d010301 3588 Thread::wait(2);
tyftyftyf 12:e32a6beb0a41 3589
tyftyftyf 12:e32a6beb0a41 3590 // reset the AK8963
tyftyftyf 12:e32a6beb0a41 3591 writeAKRegister(AK8963_RA_CNTL2, PWR_RESET);
tyftyftyf 12:e32a6beb0a41 3592
tyftyftyf 12:e32a6beb0a41 3593 // select clock source to gyro
tyftyftyf 12:e32a6beb0a41 3594 if( !writeRegister(MPU60X0_RA_PWR_MGMT_1, MPU60X0_CLOCK_PLL_XGYRO) ){
tyftyftyf 12:e32a6beb0a41 3595 debugSerial.printf("Clock Source Not Set\n");
tyftyftyf 12:e32a6beb0a41 3596 }
tyftyftyf 12:e32a6beb0a41 3597
tyftyftyf 12:e32a6beb0a41 3598 // check the WHO AM I byte, expected value is 0x71 (decimal 113)
tyftyftyf 12:e32a6beb0a41 3599 readRegister(MPU60X0_RA_WHO_AM_I, 1,&buff[0]);
tyftyftyf 12:e32a6beb0a41 3600 if( buff[0] != 113 ){
tyftyftyf 12:e32a6beb0a41 3601 debugSerial.printf("9250 Not Recognized\n");
tyftyftyf 12:e32a6beb0a41 3602 }
tyftyftyf 12:e32a6beb0a41 3603
tyftyftyf 12:e32a6beb0a41 3604 // enable accelerometer and gyro
tyftyftyf 12:e32a6beb0a41 3605 if( !writeRegister(MPU60X0_RA_PWR_MGMT_2, SEN_ENABLE) ){
tyftyftyf 12:e32a6beb0a41 3606 debugSerial.printf("Accel and Gyro not Enabled\n");
tyftyftyf 12:e32a6beb0a41 3607 }
tyftyftyf 12:e32a6beb0a41 3608
tyftyftyf 12:e32a6beb0a41 3609 /* setup the accel and gyro ranges */
tyftyftyf 12:e32a6beb0a41 3610 setFullScaleGyroRange(MPU60X0_GYRO_FS_2000); // set gyro range to +/- 250 deg/second
tyftyftyf 12:e32a6beb0a41 3611 setFullScaleAccelRange(MPU60X0_ACCEL_FS_2); // set accel range to +- 2g
tyftyftyf 12:e32a6beb0a41 3612 //setFilt9250(DLPF_BANDWIDTH_92HZ, 4);
tyftyftyf 12:e32a6beb0a41 3613
tyftyftyf 12:e32a6beb0a41 3614 // enable I2C master mode
tyftyftyf 12:e32a6beb0a41 3615 if( !writeRegister(MPU60X0_RA_USER_CTRL,I2C_MST_EN) ){
tyftyftyf 12:e32a6beb0a41 3616 debugSerial.printf("Master Mode Set\n");
tyftyftyf 12:e32a6beb0a41 3617 }
tyftyftyf 12:e32a6beb0a41 3618
tyftyftyf 12:e32a6beb0a41 3619 // set the I2C bus speed to 400 kHz
tyftyftyf 12:e32a6beb0a41 3620 if( !writeRegister(MPU60X0_RA_I2C_MST_CTRL,MPU60X0_CLOCK_PLL_XGYRO) ){
tyftyftyf 12:e32a6beb0a41 3621 debugSerial.printf("I2C Bus Set\n");
tyftyftyf 12:e32a6beb0a41 3622 }
tyftyftyf 12:e32a6beb0a41 3623
tyftyftyf 12:e32a6beb0a41 3624 // check AK8963 WHO AM I register, expected value is 0x48 (decimal 72)
tyftyftyf 12:e32a6beb0a41 3625 readAKRegisters(AK8963_RA_WIA, sizeof(buff), &buff[0]);
tyftyftyf 12:e32a6beb0a41 3626 if( buff[0] != 72 ){
tyftyftyf 12:e32a6beb0a41 3627 debugSerial.printf("%d", buff[0]);
tyftyftyf 12:e32a6beb0a41 3628 debugSerial.printf(", ");
tyftyftyf 15:09f072efa71e 3629 debugSerial.printf("AK does not match\n");
tyftyftyf 12:e32a6beb0a41 3630 }
tyftyftyf 12:e32a6beb0a41 3631
tyftyftyf 12:e32a6beb0a41 3632 /* get the magnetometer calibration */
tyftyftyf 12:e32a6beb0a41 3633
tyftyftyf 12:e32a6beb0a41 3634 // set AK8963 to Power Down
tyftyftyf 12:e32a6beb0a41 3635 if( !writeAKRegister(AK8963_RA_CNTL1, AK8963_MODE_POWERDOWN) ){
tyftyftyf 12:e32a6beb0a41 3636 debugSerial.printf("AK Not Powered Down\n");
tyftyftyf 12:e32a6beb0a41 3637 }
tyftyftyf 12:e32a6beb0a41 3638 Thread::wait(100); // long wait between AK8963 mode changes
tyftyftyf 12:e32a6beb0a41 3639
tyftyftyf 12:e32a6beb0a41 3640 // set AK8963 to FUSE ROM access
tyftyftyf 12:e32a6beb0a41 3641 if( !writeAKRegister(AK8963_RA_CNTL1, AK8963_MODE_FUSEROM)){
tyftyftyf 12:e32a6beb0a41 3642 debugSerial.printf("FUSE ROM Access Not set\n");
tyftyftyf 12:e32a6beb0a41 3643 }
tyftyftyf 12:e32a6beb0a41 3644
tyftyftyf 12:e32a6beb0a41 3645 Thread::wait(100); // long wait between AK8963 mode changes
tyftyftyf 12:e32a6beb0a41 3646
tyftyftyf 12:e32a6beb0a41 3647 // read the AK8963 ASA registers and compute magnetometer scale factors
tyftyftyf 12:e32a6beb0a41 3648 readAKRegisters(AK8963_RA_ASAX, sizeof(buff), &buff[0]);
tyftyftyf 12:e32a6beb0a41 3649 //_magScaleX = ((((float)buff[0]) - 128.0f)/(256.0f) + 1.0f) * 4912.0f / 32760.0f; // micro Tesla
tyftyftyf 12:e32a6beb0a41 3650 //_magScaleY = ((((float)buff[1]) - 128.0f)/(256.0f) + 1.0f) * 4912.0f / 32760.0f; // micro Tesla
tyftyftyf 12:e32a6beb0a41 3651 //_magScaleZ = ((((float)buff[2]) - 128.0f)/(256.0f) + 1.0f) * 4912.0f / 32760.0f; // micro Tesla
tyftyftyf 12:e32a6beb0a41 3652 _magScaleX = buff[0];
tyftyftyf 12:e32a6beb0a41 3653 _magScaleY = buff[1];
tyftyftyf 12:e32a6beb0a41 3654 _magScaleZ = buff[2];
tyftyftyf 12:e32a6beb0a41 3655 //Serial.print(_magScaleX); Serial.print(", "); Serial.print(_magScaleY);
tyftyftyf 12:e32a6beb0a41 3656 //Serial.print(", "); Serial.println(_magScaleZ);
tyftyftyf 12:e32a6beb0a41 3657
tyftyftyf 12:e32a6beb0a41 3658 // set AK8963 to Power Down
tyftyftyf 12:e32a6beb0a41 3659 if( !writeAKRegister(AK8963_RA_CNTL1, AK8963_MODE_POWERDOWN) ){
tyftyftyf 12:e32a6beb0a41 3660 debugSerial.printf("AK Not Powered Down\n");
tyftyftyf 12:e32a6beb0a41 3661 }
tyftyftyf 12:e32a6beb0a41 3662 Thread::wait(100); // long wait between AK8963 mode changes
tyftyftyf 12:e32a6beb0a41 3663
tyftyftyf 12:e32a6beb0a41 3664 // set AK8963 to 16 bit resolution, 100 Hz update rate
tyftyftyf 12:e32a6beb0a41 3665 if( !writeAKRegister(AK8963_RA_CNTL1, 0x16) ){
tyftyftyf 12:e32a6beb0a41 3666 debugSerial.printf("Res Not Set\n");
tyftyftyf 12:e32a6beb0a41 3667 }
tyftyftyf 12:e32a6beb0a41 3668 Thread::wait(100); // long wait between AK8963 mode changes
tyftyftyf 12:e32a6beb0a41 3669
tyftyftyf 12:e32a6beb0a41 3670 // select clock source to gyro
tyftyftyf 12:e32a6beb0a41 3671 if( !writeRegister(MPU60X0_RA_PWR_MGMT_1, MPU60X0_CLOCK_PLL_XGYRO) ){
tyftyftyf 12:e32a6beb0a41 3672 debugSerial.printf("Clock Source Not Set\n");
tyftyftyf 12:e32a6beb0a41 3673 }
tyftyftyf 12:e32a6beb0a41 3674
tyftyftyf 12:e32a6beb0a41 3675 // instruct the MPU9250 to get 7 bytes of data from the AK8963 at the sample rate
tyftyftyf 12:e32a6beb0a41 3676 readAKRegisters(AK8963_RA_HXL,sizeof(data),&data[0]);
tyftyftyf 12:e32a6beb0a41 3677 //Serial.println((((int16_t)data[1]) << 8) | data[0]);
tyftyftyf 12:e32a6beb0a41 3678 //Serial.println((((int16_t)data[3]) << 8) | data[2]);
tyftyftyf 12:e32a6beb0a41 3679 //Serial.println((((int16_t)data[5]) << 8) | data[4]);
tyftyftyf 12:e32a6beb0a41 3680
tyftyftyf 12:e32a6beb0a41 3681 // successful init, return 0
tyftyftyf 17:902d1491455f 3682 // debugSerial.printf("FINISHED\n");
tyftyftyf 12:e32a6beb0a41 3683 }
tyftyftyf 12:e32a6beb0a41 3684
tyftyftyf 12:e32a6beb0a41 3685
tyftyftyf 12:e32a6beb0a41 3686 /* sets the DLPF and interrupt settings */
tyftyftyf 12:e32a6beb0a41 3687 int MPU6050::setFilt9250(mpu9250_dlpf_bandwidth bandwidth, uint8_t SRD){
tyftyftyf 12:e32a6beb0a41 3688 uint8_t data[7];
tyftyftyf 12:e32a6beb0a41 3689
tyftyftyf 12:e32a6beb0a41 3690 switch(bandwidth) {
tyftyftyf 12:e32a6beb0a41 3691 case DLPF_BANDWIDTH_184HZ:
tyftyftyf 12:e32a6beb0a41 3692 if( !writeRegister(MPU9250_RA_ACCEL_CONFIG2, MPU60X0_DLPF_BW_184) ){ // setting accel bandwidth to 184Hz
tyftyftyf 12:e32a6beb0a41 3693 return -1;
tyftyftyf 12:e32a6beb0a41 3694 }
tyftyftyf 12:e32a6beb0a41 3695 if( !writeRegister(MPU60X0_RA_GYRO_CONFIG, MPU60X0_DLPF_BW_184) ){ // setting gyro bandwidth to 184Hz
tyftyftyf 12:e32a6beb0a41 3696 return -1;
tyftyftyf 12:e32a6beb0a41 3697 }
tyftyftyf 12:e32a6beb0a41 3698 break;
tyftyftyf 12:e32a6beb0a41 3699
tyftyftyf 12:e32a6beb0a41 3700 case DLPF_BANDWIDTH_92HZ:
tyftyftyf 12:e32a6beb0a41 3701 if( !writeRegister(MPU9250_RA_ACCEL_CONFIG2, MPU60X0_DLPF_BW_98) ){ // setting accel bandwidth to 92Hz
tyftyftyf 12:e32a6beb0a41 3702 return -1;
tyftyftyf 12:e32a6beb0a41 3703 }
tyftyftyf 12:e32a6beb0a41 3704 if( !writeRegister(MPU60X0_RA_GYRO_CONFIG, MPU60X0_DLPF_BW_98) ){ // setting gyro bandwidth to 92Hz
tyftyftyf 12:e32a6beb0a41 3705 return -1;
tyftyftyf 12:e32a6beb0a41 3706 }
tyftyftyf 12:e32a6beb0a41 3707 break;
tyftyftyf 12:e32a6beb0a41 3708
tyftyftyf 12:e32a6beb0a41 3709 case DLPF_BANDWIDTH_41HZ:
tyftyftyf 12:e32a6beb0a41 3710 if( !writeRegister(MPU9250_RA_ACCEL_CONFIG2, MPU60X0_DLPF_BW_42) ){ // setting accel bandwidth to 41Hz
tyftyftyf 12:e32a6beb0a41 3711 return -1;
tyftyftyf 12:e32a6beb0a41 3712 }
tyftyftyf 12:e32a6beb0a41 3713 if( !writeRegister(MPU60X0_RA_GYRO_CONFIG, MPU60X0_DLPF_BW_42) ){ // setting gyro bandwidth to 41Hz
tyftyftyf 12:e32a6beb0a41 3714 return -1;
tyftyftyf 12:e32a6beb0a41 3715 }
tyftyftyf 12:e32a6beb0a41 3716 break;
tyftyftyf 12:e32a6beb0a41 3717
tyftyftyf 12:e32a6beb0a41 3718 case DLPF_BANDWIDTH_20HZ:
tyftyftyf 12:e32a6beb0a41 3719 if( !writeRegister(MPU9250_RA_ACCEL_CONFIG2, MPU60X0_DLPF_BW_20) ){ // setting accel bandwidth to 20Hz
tyftyftyf 12:e32a6beb0a41 3720 return -1;
tyftyftyf 12:e32a6beb0a41 3721 }
tyftyftyf 12:e32a6beb0a41 3722 if( !writeRegister(MPU60X0_RA_GYRO_CONFIG, MPU60X0_DLPF_BW_20) ){ // setting gyro bandwidth to 20Hz
tyftyftyf 12:e32a6beb0a41 3723 return -1;
tyftyftyf 12:e32a6beb0a41 3724 }
tyftyftyf 12:e32a6beb0a41 3725 break;
tyftyftyf 12:e32a6beb0a41 3726
tyftyftyf 12:e32a6beb0a41 3727 case DLPF_BANDWIDTH_10HZ:
tyftyftyf 12:e32a6beb0a41 3728 if( !writeRegister(MPU9250_RA_ACCEL_CONFIG2,MPU60X0_DLPF_BW_10) ){ // setting accel bandwidth to 10Hz
tyftyftyf 12:e32a6beb0a41 3729 return -1;
tyftyftyf 12:e32a6beb0a41 3730 }
tyftyftyf 12:e32a6beb0a41 3731 if( !writeRegister(MPU60X0_RA_GYRO_CONFIG,MPU60X0_DLPF_BW_10) ){ // setting gyro bandwidth to 10Hz
tyftyftyf 12:e32a6beb0a41 3732 return -1;
tyftyftyf 12:e32a6beb0a41 3733 }
tyftyftyf 12:e32a6beb0a41 3734 break;
tyftyftyf 12:e32a6beb0a41 3735
tyftyftyf 12:e32a6beb0a41 3736 case DLPF_BANDWIDTH_5HZ:
tyftyftyf 12:e32a6beb0a41 3737 if( !writeRegister(MPU9250_RA_ACCEL_CONFIG2,MPU60X0_DLPF_BW_5) ){ // setting accel bandwidth to 5Hz
tyftyftyf 12:e32a6beb0a41 3738 return -1;
tyftyftyf 12:e32a6beb0a41 3739 }
tyftyftyf 12:e32a6beb0a41 3740 if( !writeRegister(MPU60X0_RA_GYRO_CONFIG,MPU60X0_DLPF_BW_5) ){ // setting gyro bandwidth to 5Hz
tyftyftyf 12:e32a6beb0a41 3741 return -1;
tyftyftyf 12:e32a6beb0a41 3742 }
tyftyftyf 12:e32a6beb0a41 3743 break;
tyftyftyf 12:e32a6beb0a41 3744 }
tyftyftyf 12:e32a6beb0a41 3745
tyftyftyf 12:e32a6beb0a41 3746 /* setting the sample rate divider */
tyftyftyf 12:e32a6beb0a41 3747 if( !writeRegister(MPU60X0_RA_SMPLRT_DIV,SRD) ){ // setting the sample rate divider
tyftyftyf 12:e32a6beb0a41 3748 return -1;
tyftyftyf 12:e32a6beb0a41 3749 }
tyftyftyf 12:e32a6beb0a41 3750
tyftyftyf 12:e32a6beb0a41 3751 if(SRD > 9){
tyftyftyf 12:e32a6beb0a41 3752
tyftyftyf 12:e32a6beb0a41 3753 // set AK8963 to Power Down
tyftyftyf 12:e32a6beb0a41 3754 if( !writeAKRegister(AK8963_RA_CNTL1, AK8963_MODE_POWERDOWN) ){
tyftyftyf 12:e32a6beb0a41 3755 return -1;
tyftyftyf 12:e32a6beb0a41 3756 }
tyftyftyf 15:09f072efa71e 3757 Thread::wait(100); // long wait between AK8963 mode changes
tyftyftyf 12:e32a6beb0a41 3758
tyftyftyf 12:e32a6beb0a41 3759 // set AK8963 to 16 bit resolution, 8 Hz update rate
tyftyftyf 21:ae3ee2d811eb 3760 if( !writeAKRegister(AK8963_RA_CNTL1, 0x12) ){
tyftyftyf 12:e32a6beb0a41 3761 return -1;
tyftyftyf 12:e32a6beb0a41 3762 }
tyftyftyf 15:09f072efa71e 3763 Thread::wait(100); // long wait between AK8963 mode changes
tyftyftyf 12:e32a6beb0a41 3764
tyftyftyf 12:e32a6beb0a41 3765 // instruct the MPU9250 to get 7 bytes of data from the AK8963 at the sample rate
tyftyftyf 12:e32a6beb0a41 3766 readAKRegisters(AK8963_RA_HXL,sizeof(data),&data[0]);
tyftyftyf 12:e32a6beb0a41 3767 }
tyftyftyf 12:e32a6beb0a41 3768
tyftyftyf 12:e32a6beb0a41 3769 /* setting the interrupt */
tyftyftyf 12:e32a6beb0a41 3770 if( !writeRegister(MPU60X0_RA_INT_PIN_CFG,INT_PULSE_50US) ){ // setup interrupt, 50 us pulse
tyftyftyf 12:e32a6beb0a41 3771 return -1;
tyftyftyf 12:e32a6beb0a41 3772 }
tyftyftyf 12:e32a6beb0a41 3773 if( !writeRegister(MPU60X0_RA_INT_ENABLE,INT_RAW_RDY_EN) ){ // set to data ready
tyftyftyf 12:e32a6beb0a41 3774 return -1;
tyftyftyf 12:e32a6beb0a41 3775 }
tyftyftyf 12:e32a6beb0a41 3776
tyftyftyf 12:e32a6beb0a41 3777 // successful filter setup, return 0
tyftyftyf 12:e32a6beb0a41 3778 return 0;
tyftyftyf 12:e32a6beb0a41 3779 }
tyftyftyf 12:e32a6beb0a41 3780
tyftyftyf 12:e32a6beb0a41 3781 /* enables and disables the interrupt */
tyftyftyf 12:e32a6beb0a41 3782 int MPU6050::enableInt9250(bool enable){
tyftyftyf 12:e32a6beb0a41 3783
tyftyftyf 12:e32a6beb0a41 3784 if(enable){
tyftyftyf 12:e32a6beb0a41 3785 /* setting the interrupt */
tyftyftyf 12:e32a6beb0a41 3786 if( !writeRegister(MPU60X0_RA_INT_PIN_CFG,INT_PULSE_50US) ){ // setup interrupt, 50 us pulse
tyftyftyf 12:e32a6beb0a41 3787 return -1;
tyftyftyf 12:e32a6beb0a41 3788 }
tyftyftyf 12:e32a6beb0a41 3789 if( !writeRegister(MPU60X0_RA_INT_ENABLE,INT_RAW_RDY_EN) ){ // set to data ready
tyftyftyf 12:e32a6beb0a41 3790 return -1;
tyftyftyf 12:e32a6beb0a41 3791 }
tyftyftyf 12:e32a6beb0a41 3792 }
tyftyftyf 12:e32a6beb0a41 3793 else{
tyftyftyf 12:e32a6beb0a41 3794 if( !writeRegister(MPU60X0_RA_INT_ENABLE,INT_DISABLE) ){ // disable interrupt
tyftyftyf 12:e32a6beb0a41 3795 return -1;
tyftyftyf 12:e32a6beb0a41 3796 }
tyftyftyf 12:e32a6beb0a41 3797 }
tyftyftyf 12:e32a6beb0a41 3798
tyftyftyf 12:e32a6beb0a41 3799 // successful interrupt setup, return 0
tyftyftyf 12:e32a6beb0a41 3800 return 0;
tyftyftyf 12:e32a6beb0a41 3801 }
tyftyftyf 12:e32a6beb0a41 3802
tyftyftyf 12:e32a6beb0a41 3803
tyftyftyf 12:e32a6beb0a41 3804
tyftyftyf 12:e32a6beb0a41 3805
tyftyftyf 12:e32a6beb0a41 3806 /* get accelerometer data given pointers to store the three values, return data as counts */
tyftyftyf 12:e32a6beb0a41 3807 void MPU6050::get9250AccelCounts(int16_t* ax, int16_t* ay, int16_t* az){
tyftyftyf 12:e32a6beb0a41 3808 uint8_t buff[6];
tyftyftyf 12:e32a6beb0a41 3809 int16_t axx, ayy, azz;
tyftyftyf 12:e32a6beb0a41 3810
tyftyftyf 12:e32a6beb0a41 3811 readRegister(MPU60X0_RA_ACCEL_XOUT_H, sizeof(buff), &buff[0]); // grab the data from the MPU9250
tyftyftyf 12:e32a6beb0a41 3812
tyftyftyf 12:e32a6beb0a41 3813 axx = (((int16_t)buff[0]) << 8) | buff[1]; // combine into 16 bit values
tyftyftyf 12:e32a6beb0a41 3814 ayy = (((int16_t)buff[2]) << 8) | buff[3];
tyftyftyf 12:e32a6beb0a41 3815 azz = (((int16_t)buff[4]) << 8) | buff[5];
tyftyftyf 12:e32a6beb0a41 3816
tyftyftyf 12:e32a6beb0a41 3817 *ax = tX[0]*axx + tX[1]*ayy + tX[2]*azz; // transform axes
tyftyftyf 12:e32a6beb0a41 3818 *ay = tY[0]*axx + tY[1]*ayy + tY[2]*azz;
tyftyftyf 12:e32a6beb0a41 3819 *az = tZ[0]*axx + tZ[1]*ayy + tZ[2]*azz;
tyftyftyf 12:e32a6beb0a41 3820 }
tyftyftyf 12:e32a6beb0a41 3821
tyftyftyf 12:e32a6beb0a41 3822
tyftyftyf 12:e32a6beb0a41 3823 /* get gyro data given pointers to store the three values, return data as counts */
tyftyftyf 12:e32a6beb0a41 3824 void MPU6050::get9250GyroCounts(int16_t* gx, int16_t* gy, int16_t* gz){
tyftyftyf 12:e32a6beb0a41 3825 uint8_t buff[6];
tyftyftyf 12:e32a6beb0a41 3826 int16_t gxx, gyy, gzz;
tyftyftyf 12:e32a6beb0a41 3827
tyftyftyf 12:e32a6beb0a41 3828 readRegister(MPU60X0_RA_GYRO_XOUT_H, sizeof(buff), &buff[0]); // grab the data from the MPU9250
tyftyftyf 12:e32a6beb0a41 3829
tyftyftyf 12:e32a6beb0a41 3830 gxx = (((int16_t)buff[0]) << 8) | buff[1]; // combine into 16 bit values
tyftyftyf 12:e32a6beb0a41 3831 gyy = (((int16_t)buff[2]) << 8) | buff[3];
tyftyftyf 12:e32a6beb0a41 3832 gzz = (((int16_t)buff[4]) << 8) | buff[5];
tyftyftyf 12:e32a6beb0a41 3833
tyftyftyf 12:e32a6beb0a41 3834 *gx = tX[0]*gxx + tX[1]*gyy + tX[2]*gzz; // transform axes
tyftyftyf 12:e32a6beb0a41 3835 *gy = tY[0]*gxx + tY[1]*gyy + tY[2]*gzz;
tyftyftyf 12:e32a6beb0a41 3836 *gz = tZ[0]*gxx + tZ[1]*gyy + tZ[2]*gzz;
tyftyftyf 12:e32a6beb0a41 3837 }
tyftyftyf 12:e32a6beb0a41 3838
tyftyftyf 12:e32a6beb0a41 3839 /* get magnetometer data given pointers to store the three values, return data as counts */
tyftyftyf 12:e32a6beb0a41 3840 void MPU6050::get9250MagCounts(int16_t* hx, int16_t* hy, int16_t* hz){
tyftyftyf 12:e32a6beb0a41 3841 uint8_t buff[7];
tyftyftyf 12:e32a6beb0a41 3842 // read the magnetometer data off the external sensor buffer
tyftyftyf 12:e32a6beb0a41 3843 readRegister(MPU60X0_RA_EXT_SENS_DATA_00,sizeof(buff),&buff[0]);
tyftyftyf 12:e32a6beb0a41 3844
tyftyftyf 12:e32a6beb0a41 3845 if( buff[6] == 0x10 ) { // check for overflow
tyftyftyf 12:e32a6beb0a41 3846 *hx = (((int16_t)buff[1]) << 8) | buff[0]; // combine into 16 bit values
tyftyftyf 12:e32a6beb0a41 3847 *hy = (((int16_t)buff[3]) << 8) | buff[2];
tyftyftyf 12:e32a6beb0a41 3848 *hz = (((int16_t)buff[5]) << 8) | buff[4];
tyftyftyf 12:e32a6beb0a41 3849 }
tyftyftyf 12:e32a6beb0a41 3850 else{
tyftyftyf 12:e32a6beb0a41 3851 *hx = 0;
tyftyftyf 12:e32a6beb0a41 3852 *hy = 0;
tyftyftyf 12:e32a6beb0a41 3853 *hz = 0;
tyftyftyf 12:e32a6beb0a41 3854 }
tyftyftyf 12:e32a6beb0a41 3855 }
tyftyftyf 12:e32a6beb0a41 3856
tyftyftyf 12:e32a6beb0a41 3857 /* get temperature data given pointer to store the value, return data as counts */
tyftyftyf 12:e32a6beb0a41 3858 void MPU6050::get9250TempCounts(int16_t* t){
tyftyftyf 12:e32a6beb0a41 3859 uint8_t buff[2];
tyftyftyf 12:e32a6beb0a41 3860
tyftyftyf 12:e32a6beb0a41 3861 readRegister(MPU60X0_RA_TEMP_OUT_H, sizeof(buff), &buff[0]); // grab the data from the MPU9250
tyftyftyf 12:e32a6beb0a41 3862
tyftyftyf 12:e32a6beb0a41 3863 *t = (((int16_t)buff[0]) << 8) | buff[1]; // combine into 16 bit value and return
tyftyftyf 12:e32a6beb0a41 3864 }
tyftyftyf 12:e32a6beb0a41 3865
tyftyftyf 12:e32a6beb0a41 3866 void MPU6050::get9250Motion9Counts(int16_t* ax, int16_t* ay, int16_t* az, int16_t* gx, int16_t* gy, int16_t* gz, int16_t* hx, int16_t* hy, int16_t* hz)
tyftyftyf 12:e32a6beb0a41 3867 {
tyftyftyf 12:e32a6beb0a41 3868 int16_t axx, ayy, azz, gxx, gyy, gzz;
tyftyftyf 12:e32a6beb0a41 3869
tyftyftyf 13:a74f2d622b54 3870 axx = ax_cache;
tyftyftyf 13:a74f2d622b54 3871 ayy = ay_cache;
tyftyftyf 13:a74f2d622b54 3872 azz = az_cache;
tyftyftyf 13:a74f2d622b54 3873 gxx = gx_cache;
tyftyftyf 13:a74f2d622b54 3874 gyy = gy_cache;
tyftyftyf 13:a74f2d622b54 3875 gzz = gz_cache;
tyftyftyf 13:a74f2d622b54 3876 *hx = hx_cache;
tyftyftyf 13:a74f2d622b54 3877 *hy = hy_cache;
tyftyftyf 13:a74f2d622b54 3878 *hz = hz_cache;
tyftyftyf 12:e32a6beb0a41 3879
tyftyftyf 20:0172ae63dd9e 3880 *ax = tX[0]*axx + tX[1]*ayy + tX[2]*azz; // transform axes
tyftyftyf 12:e32a6beb0a41 3881 *ay = tY[0]*axx + tY[1]*ayy + tY[2]*azz;
tyftyftyf 12:e32a6beb0a41 3882 *az = tZ[0]*axx + tZ[1]*ayy + tZ[2]*azz;
tyftyftyf 12:e32a6beb0a41 3883
tyftyftyf 12:e32a6beb0a41 3884 *gx = tX[0]*gxx + tX[1]*gyy + tX[2]*gzz;
tyftyftyf 12:e32a6beb0a41 3885 *gy = tY[0]*gxx + tY[1]*gyy + tY[2]*gzz;
tyftyftyf 20:0172ae63dd9e 3886 *gz = tZ[0]*gxx + tZ[1]*gyy + tZ[2]*gzz;
tyftyftyf 12:e32a6beb0a41 3887 }
tyftyftyf 12:e32a6beb0a41 3888
tyftyftyf 12:e32a6beb0a41 3889 /* get accelerometer, gyro, and magnetometer data given pointers to store values */
tyftyftyf 12:e32a6beb0a41 3890 void MPU6050::get9250Motion9(float* ax, float* ay, float* az, float* gx, float* gy, float* gz, float* hx, float* hy, float* hz){
tyftyftyf 12:e32a6beb0a41 3891 int16_t accel[3];
tyftyftyf 12:e32a6beb0a41 3892 int16_t gyro[3];
tyftyftyf 12:e32a6beb0a41 3893 int16_t mag[3];
tyftyftyf 12:e32a6beb0a41 3894
tyftyftyf 12:e32a6beb0a41 3895 get9250Motion9Counts(&accel[0], &accel[1], &accel[2], &gyro[0], &gyro[1], &gyro[2], &mag[0], &mag[1], &mag[2]);
tyftyftyf 12:e32a6beb0a41 3896
tyftyftyf 12:e32a6beb0a41 3897 *ax = ((float) accel[0]); // typecast and scale to values
tyftyftyf 12:e32a6beb0a41 3898 *ay = ((float) accel[1]);
tyftyftyf 12:e32a6beb0a41 3899 *az = ((float) accel[2]);
tyftyftyf 12:e32a6beb0a41 3900
tyftyftyf 12:e32a6beb0a41 3901 *gx = ((float) gyro[0]);
tyftyftyf 12:e32a6beb0a41 3902 *gy = ((float) gyro[1]);
tyftyftyf 12:e32a6beb0a41 3903 *gz = ((float) gyro[2]);
tyftyftyf 12:e32a6beb0a41 3904
tyftyftyf 12:e32a6beb0a41 3905 *hx = ((float) mag[0]);
tyftyftyf 12:e32a6beb0a41 3906 *hy = ((float) mag[1]);
tyftyftyf 12:e32a6beb0a41 3907 *hz = ((float) mag[2]);
tyftyftyf 12:e32a6beb0a41 3908
tyftyftyf 12:e32a6beb0a41 3909 }
tyftyftyf 12:e32a6beb0a41 3910
tyftyftyf 15:09f072efa71e 3911 void MPU6050::readRegister(uint8_t subAddress, uint8_t count, uint8_t* dest) {
tyftyftyf 12:e32a6beb0a41 3912 i2Cdev.readBytes(devAddr, subAddress, count, dest);
tyftyftyf 12:e32a6beb0a41 3913 }
tyftyftyf 12:e32a6beb0a41 3914
tyftyftyf 12:e32a6beb0a41 3915 /* writes a register to the AK8963 given a register address and data */
tyftyftyf 15:09f072efa71e 3916 bool MPU6050::writeAKRegister(uint8_t subAddress, uint8_t data) {
tyftyftyf 12:e32a6beb0a41 3917 uint8_t count = 1;
tyftyftyf 12:e32a6beb0a41 3918 uint8_t buff[1];
tyftyftyf 12:e32a6beb0a41 3919
tyftyftyf 12:e32a6beb0a41 3920 writeRegister(MPU60X0_RA_I2C_SLV0_ADDR, magDevAddr); // set slave 0 to the AK8963 and set for write
tyftyftyf 12:e32a6beb0a41 3921 writeRegister(MPU60X0_RA_I2C_SLV0_REG,subAddress); // set the register to the desired AK8963 sub address
tyftyftyf 12:e32a6beb0a41 3922 writeRegister(MPU60X0_RA_I2C_SLV0_DO, data); // store the data for write
tyftyftyf 12:e32a6beb0a41 3923 writeRegister(MPU60X0_RA_I2C_SLV0_CTRL, I2C_SLV0_EN | count); // enable I2C and send 1 byte
tyftyftyf 12:e32a6beb0a41 3924
tyftyftyf 12:e32a6beb0a41 3925 // read the register and confirm
tyftyftyf 12:e32a6beb0a41 3926 readAKRegisters(subAddress, sizeof(buff), &buff[0]);
tyftyftyf 12:e32a6beb0a41 3927
tyftyftyf 12:e32a6beb0a41 3928 if (buff[0] == data) {
tyftyftyf 12:e32a6beb0a41 3929 return true;
tyftyftyf 12:e32a6beb0a41 3930 } else {
tyftyftyf 12:e32a6beb0a41 3931 return false;
tyftyftyf 12:e32a6beb0a41 3932 }
tyftyftyf 12:e32a6beb0a41 3933 }
tyftyftyf 12:e32a6beb0a41 3934
tyftyftyf 12:e32a6beb0a41 3935
tyftyftyf 12:e32a6beb0a41 3936 /* reads registers from the AK8963 */
tyftyftyf 15:09f072efa71e 3937 void MPU6050::readAKRegisters(uint8_t subAddress, uint8_t count, uint8_t* dest) {
tyftyftyf 12:e32a6beb0a41 3938
tyftyftyf 12:e32a6beb0a41 3939 writeRegister(MPU60X0_RA_I2C_SLV0_ADDR, magDevAddr | I2C_READ_FLAG); // set slave 0 to the AK8963 and set for read
tyftyftyf 12:e32a6beb0a41 3940 writeRegister(MPU60X0_RA_I2C_SLV0_REG, subAddress); // set the register to the desired AK8963 sub address
tyftyftyf 12:e32a6beb0a41 3941 writeRegister(MPU60X0_RA_I2C_SLV0_CTRL, I2C_SLV0_EN | count); // enable I2C and request the bytes
tyftyftyf 12:e32a6beb0a41 3942 Thread::wait(1); // takes some time for these registers to fill
tyftyftyf 12:e32a6beb0a41 3943 readRegister(MPU60X0_RA_EXT_SENS_DATA_00, count, dest); // read the bytes off the MPU9250 EXT_SENS_DATA registers
tyftyftyf 12:e32a6beb0a41 3944
tyftyftyf 12:e32a6beb0a41 3945 }
tyftyftyf 12:e32a6beb0a41 3946
tyftyftyf 15:09f072efa71e 3947 bool MPU6050::writeRegister(uint8_t subAddress, uint8_t data) {
tyftyftyf 12:e32a6beb0a41 3948 uint8_t buff[1];
tyftyftyf 12:e32a6beb0a41 3949
tyftyftyf 12:e32a6beb0a41 3950 i2Cdev.writeByte(devAddr, subAddress, data);
tyftyftyf 12:e32a6beb0a41 3951
tyftyftyf 12:e32a6beb0a41 3952 Thread::wait(10); // need to slow down how fast I write to MPU9250
tyftyftyf 12:e32a6beb0a41 3953
tyftyftyf 12:e32a6beb0a41 3954 /* read back the register */
tyftyftyf 12:e32a6beb0a41 3955 readRegister(subAddress,sizeof(buff),&buff[0]);
tyftyftyf 12:e32a6beb0a41 3956
tyftyftyf 12:e32a6beb0a41 3957 /* check the read back register against the written register */
tyftyftyf 12:e32a6beb0a41 3958 if (buff[0] == data) {
tyftyftyf 12:e32a6beb0a41 3959 return true;
tyftyftyf 12:e32a6beb0a41 3960 } else {
tyftyftyf 12:e32a6beb0a41 3961 return false;
tyftyftyf 12:e32a6beb0a41 3962 }
tyftyftyf 12:e32a6beb0a41 3963
tyftyftyf 12:e32a6beb0a41 3964 }
tyftyftyf 12:e32a6beb0a41 3965
tyftyftyf 13:a74f2d622b54 3966 #endif // MPU9250
tyftyftyf 12:e32a6beb0a41 3967
tyftyftyf 12:e32a6beb0a41 3968
tyftyftyf 12:e32a6beb0a41 3969
tyftyftyf 12:e32a6beb0a41 3970