Code for our FYDP -only one IMU works right now -RTOS is working
Embed:
(wiki syntax)
Show/hide line numbers
MPU6051.cpp
00001 //ported from arduino library: https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6050 00002 //written by szymon gaertig (email: szymon@gaertig.com.pl) 00003 // 00004 //Changelog: 00005 //2013-01-08 - first beta release 00006 00007 // I2Cdev library collection - MPU6050 I2C device class 00008 // Based on InvenSense MPU-6050 register map document rev. 2.0, 5/19/2011 (RM-MPU-6000A-00) 00009 // 8/24/2011 by Jeff Rowberg <jeff@rowberg.net> 00010 // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib 00011 // 00012 // Changelog: 00013 // ... - ongoing debug release 00014 00015 // NOTE: THIS IS ONLY A PARIAL RELEASE. THIS DEVICE CLASS IS CURRENTLY UNDERGOING ACTIVE 00016 // DEVELOPMENT AND IS STILL MISSING SOME IMPORTANT FEATURES. PLEASE KEEP THIS IN MIND IF 00017 // YOU DECIDE TO USE THIS PARTICULAR CODE FOR ANYTHING. 00018 00019 /* ============================================ 00020 I2Cdev device library code is placed under the MIT license 00021 Copyright (c) 2012 Jeff Rowberg 00022 00023 Permission is hereby granted, free of charge, to any person obtaining a copy 00024 of this software and associated documentation files (the "Software"), to deal 00025 in the Software without restriction, including without limitation the rights 00026 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00027 copies of the Software, and to permit persons to whom the Software is 00028 furnished to do so, subject to the following conditions: 00029 00030 The above copyright notice and this permission notice shall be included in 00031 all copies or substantial portions of the Software. 00032 00033 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00034 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00035 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00036 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00037 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00038 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00039 THE SOFTWARE. 00040 =============================================== 00041 */ 00042 00043 #include "MPU6051.h" 00044 00045 //#define useDebugSerial 00046 00047 //instead of using pgmspace.h 00048 typedef const unsigned char prog_uchar; 00049 #define pgm_read_byte_near(x) (*(prog_uchar*)(x))//<- I modified here 00050 #define pgm_read_byte(x) (*(prog_uchar*)(x))//<- I modified here 00051 00052 /** Default constructor, uses default I2C address. 00053 * @see MPU6050_DEFAULT_ADDRESS 00054 */ 00055 MPU6051::MPU6051() //: debugSerial(USBTX, USBRX) 00056 { 00057 devAddr = MPU6050_DEFAULT_ADDRESS; 00058 } 00059 00060 /** Specific address constructor. 00061 * @param address I2C address 00062 * @see MPU6050_DEFAULT_ADDRESS 00063 * @see MPU6050_ADDRESS_AD0_LOW 00064 * @see MPU6050_ADDRESS_AD0_HIGH 00065 */ 00066 MPU6051::MPU6051(uint8_t address) //: debugSerial(USBTX, USBRX) 00067 { 00068 devAddr = address; 00069 } 00070 00071 /** Power on and prepare for general usage. 00072 * This will activate the device and take it out of sleep mode (which must be done 00073 * after start-up). This function also sets both the accelerometer and the gyroscope 00074 * to their most sensitive settings, namely +/- 2g and +/- 250 degrees/sec, and sets 00075 * the clock source to use the X Gyro for reference, which is slightly better than 00076 * the default internal clock source. 00077 */ 00078 void MPU6051::initialize() 00079 { 00080 00081 #ifdef useDebugSerial 00082 debugSerial.printf("MPU6050::initialize start\n"); 00083 #endif 00084 00085 setClockSource(MPU6050_CLOCK_PLL_XGYRO); 00086 setFullScaleGyroRange(MPU6050_GYRO_FS_250); 00087 setFullScaleAccelRange(MPU6050_ACCEL_FS_2); 00088 setSleepEnabled(false); // thanks to Jack Elston for pointing this one out! 00089 00090 #ifdef useDebugSerial 00091 debugSerial.printf("MPU6050::initialize end\n"); 00092 #endif 00093 } 00094 00095 /** Verify the I2C connection. 00096 * Make sure the device is connected and responds as expected. 00097 * @return True if connection is valid, false otherwise 00098 */ 00099 bool MPU6051::testConnection() 00100 { 00101 #ifdef useDebugSerial 00102 debugSerial.printf("MPU6050::testConnection start\n"); 00103 #endif 00104 uint8_t deviceId = getDeviceID(); 00105 #ifdef useDebugSerial 00106 debugSerial.printf("DeviceId = %d\n",deviceId); 00107 #endif 00108 return deviceId == 0x34; 00109 } 00110 00111 // AUX_VDDIO register (InvenSense demo code calls this RA_*G_OFFS_TC) 00112 00113 /** Get the auxiliary I2C supply voltage level. 00114 * When set to 1, the auxiliary I2C bus high logic level is VDD. When cleared to 00115 * 0, the auxiliary I2C bus high logic level is VLOGIC. This does not apply to 00116 * the MPU-6000, which does not have a VLOGIC pin. 00117 * @return I2C supply voltage level (0=VLOGIC, 1=VDD) 00118 */ 00119 uint8_t MPU6051::getAuxVDDIOLevel() 00120 { 00121 i2Cdev.readBit(devAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_PWR_MODE_BIT, buffer); 00122 return buffer[0]; 00123 } 00124 /** Set the auxiliary I2C supply voltage level. 00125 * When set to 1, the auxiliary I2C bus high logic level is VDD. When cleared to 00126 * 0, the auxiliary I2C bus high logic level is VLOGIC. This does not apply to 00127 * the MPU-6000, which does not have a VLOGIC pin. 00128 * @param level I2C supply voltage level (0=VLOGIC, 1=VDD) 00129 */ 00130 void MPU6051::setAuxVDDIOLevel(uint8_t level) 00131 { 00132 i2Cdev.writeBit(devAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_PWR_MODE_BIT, level); 00133 } 00134 00135 // SMPLRT_DIV register 00136 00137 /** Get gyroscope output rate divider. 00138 * The sensor register output, FIFO output, DMP sampling, Motion detection, Zero 00139 * Motion detection, and Free Fall detection are all based on the Sample Rate. 00140 * The Sample Rate is generated by dividing the gyroscope output rate by 00141 * SMPLRT_DIV: 00142 * 00143 * Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV) 00144 * 00145 * where Gyroscope Output Rate = 8kHz when the DLPF is disabled (DLPF_CFG = 0 or 00146 * 7), and 1kHz when the DLPF is enabled (see Register 26). 00147 * 00148 * Note: The accelerometer output rate is 1kHz. This means that for a Sample 00149 * Rate greater than 1kHz, the same accelerometer sample may be output to the 00150 * FIFO, DMP, and sensor registers more than once. 00151 * 00152 * For a diagram of the gyroscope and accelerometer signal paths, see Section 8 00153 * of the MPU-6000/MPU-6050 Product Specification document. 00154 * 00155 * @return Current sample rate 00156 * @see MPU6050_RA_SMPLRT_DIV 00157 */ 00158 uint8_t MPU6051::getRate() 00159 { 00160 i2Cdev.readByte(devAddr, MPU6050_RA_SMPLRT_DIV, buffer); 00161 return buffer[0]; 00162 } 00163 /** Set gyroscope sample rate divider. 00164 * @param rate New sample rate divider 00165 * @see getRate() 00166 * @see MPU6050_RA_SMPLRT_DIV 00167 */ 00168 void MPU6051::setRate(uint8_t rate) 00169 { 00170 i2Cdev.writeByte(devAddr, MPU6050_RA_SMPLRT_DIV, rate); 00171 } 00172 00173 // CONFIG register 00174 00175 /** Get external FSYNC configuration. 00176 * Configures the external Frame Synchronization (FSYNC) pin sampling. An 00177 * external signal connected to the FSYNC pin can be sampled by configuring 00178 * EXT_SYNC_SET. Signal changes to the FSYNC pin are latched so that short 00179 * strobes may be captured. The latched FSYNC signal will be sampled at the 00180 * Sampling Rate, as defined in register 25. After sampling, the latch will 00181 * reset to the current FSYNC signal state. 00182 * 00183 * The sampled value will be reported in place of the least significant bit in 00184 * a sensor data register determined by the value of EXT_SYNC_SET according to 00185 * the following table. 00186 * 00187 * <pre> 00188 * EXT_SYNC_SET | FSYNC Bit Location 00189 * -------------+------------------- 00190 * 0 | Input disabled 00191 * 1 | TEMP_OUT_L[0] 00192 * 2 | GYRO_XOUT_L[0] 00193 * 3 | GYRO_YOUT_L[0] 00194 * 4 | GYRO_ZOUT_L[0] 00195 * 5 | ACCEL_XOUT_L[0] 00196 * 6 | ACCEL_YOUT_L[0] 00197 * 7 | ACCEL_ZOUT_L[0] 00198 * </pre> 00199 * 00200 * @return FSYNC configuration value 00201 */ 00202 uint8_t MPU6051::getExternalFrameSync() 00203 { 00204 i2Cdev.readBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_EXT_SYNC_SET_BIT, MPU6050_CFG_EXT_SYNC_SET_LENGTH, buffer); 00205 return buffer[0]; 00206 } 00207 /** Set external FSYNC configuration. 00208 * @see getExternalFrameSync() 00209 * @see MPU6050_RA_CONFIG 00210 * @param sync New FSYNC configuration value 00211 */ 00212 void MPU6051::setExternalFrameSync(uint8_t sync) 00213 { 00214 i2Cdev.writeBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_EXT_SYNC_SET_BIT, MPU6050_CFG_EXT_SYNC_SET_LENGTH, sync); 00215 } 00216 /** Get digital low-pass filter configuration. 00217 * The DLPF_CFG parameter sets the digital low pass filter configuration. It 00218 * also determines the internal sampling rate used by the device as shown in 00219 * the table below. 00220 * 00221 * Note: The accelerometer output rate is 1kHz. This means that for a Sample 00222 * Rate greater than 1kHz, the same accelerometer sample may be output to the 00223 * FIFO, DMP, and sensor registers more than once. 00224 * 00225 * <pre> 00226 * | ACCELEROMETER | GYROSCOPE 00227 * DLPF_CFG | Bandwidth | Delay | Bandwidth | Delay | Sample Rate 00228 * ---------+-----------+--------+-----------+--------+------------- 00229 * 0 | 260Hz | 0ms | 256Hz | 0.98ms | 8kHz 00230 * 1 | 184Hz | 2.0ms | 188Hz | 1.9ms | 1kHz 00231 * 2 | 94Hz | 3.0ms | 98Hz | 2.8ms | 1kHz 00232 * 3 | 44Hz | 4.9ms | 42Hz | 4.8ms | 1kHz 00233 * 4 | 21Hz | 8.5ms | 20Hz | 8.3ms | 1kHz 00234 * 5 | 10Hz | 13.8ms | 10Hz | 13.4ms | 1kHz 00235 * 6 | 5Hz | 19.0ms | 5Hz | 18.6ms | 1kHz 00236 * 7 | -- Reserved -- | -- Reserved -- | Reserved 00237 * </pre> 00238 * 00239 * @return DLFP configuration 00240 * @see MPU6050_RA_CONFIG 00241 * @see MPU6050_CFG_DLPF_CFG_BIT 00242 * @see MPU6050_CFG_DLPF_CFG_LENGTH 00243 */ 00244 uint8_t MPU6051::getDLPFMode() 00245 { 00246 i2Cdev.readBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_DLPF_CFG_BIT, MPU6050_CFG_DLPF_CFG_LENGTH, buffer); 00247 return buffer[0]; 00248 } 00249 /** Set digital low-pass filter configuration. 00250 * @param mode New DLFP configuration setting 00251 * @see getDLPFBandwidth() 00252 * @see MPU6050_DLPF_BW_256 00253 * @see MPU6050_RA_CONFIG 00254 * @see MPU6050_CFG_DLPF_CFG_BIT 00255 * @see MPU6050_CFG_DLPF_CFG_LENGTH 00256 */ 00257 void MPU6051::setDLPFMode(uint8_t mode) 00258 { 00259 i2Cdev.writeBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_DLPF_CFG_BIT, MPU6050_CFG_DLPF_CFG_LENGTH, mode); 00260 } 00261 00262 // GYRO_CONFIG register 00263 00264 /** Get full-scale gyroscope range. 00265 * The FS_SEL parameter allows setting the full-scale range of the gyro sensors, 00266 * as described in the table below. 00267 * 00268 * <pre> 00269 * 0 = +/- 250 degrees/sec 00270 * 1 = +/- 500 degrees/sec 00271 * 2 = +/- 1000 degrees/sec 00272 * 3 = +/- 2000 degrees/sec 00273 * </pre> 00274 * 00275 * @return Current full-scale gyroscope range setting 00276 * @see MPU6050_GYRO_FS_250 00277 * @see MPU6050_RA_GYRO_CONFIG 00278 * @see MPU6050_GCONFIG_FS_SEL_BIT 00279 * @see MPU6050_GCONFIG_FS_SEL_LENGTH 00280 */ 00281 uint8_t MPU6051::getFullScaleGyroRange() 00282 { 00283 i2Cdev.readBits(devAddr, MPU6050_RA_GYRO_CONFIG, MPU6050_GCONFIG_FS_SEL_BIT, MPU6050_GCONFIG_FS_SEL_LENGTH, buffer); 00284 return buffer[0]; 00285 } 00286 /** Set full-scale gyroscope range. 00287 * @param range New full-scale gyroscope range value 00288 * @see getFullScaleRange() 00289 * @see MPU6050_GYRO_FS_250 00290 * @see MPU6050_RA_GYRO_CONFIG 00291 * @see MPU6050_GCONFIG_FS_SEL_BIT 00292 * @see MPU6050_GCONFIG_FS_SEL_LENGTH 00293 */ 00294 void MPU6051::setFullScaleGyroRange(uint8_t range) 00295 { 00296 i2Cdev.writeBits(devAddr, MPU6050_RA_GYRO_CONFIG, MPU6050_GCONFIG_FS_SEL_BIT, MPU6050_GCONFIG_FS_SEL_LENGTH, range); 00297 } 00298 00299 // ACCEL_CONFIG register 00300 00301 /** Get self-test enabled setting for accelerometer X axis. 00302 * @return Self-test enabled value 00303 * @see MPU6050_RA_ACCEL_CONFIG 00304 */ 00305 bool MPU6051::getAccelXSelfTest() 00306 { 00307 i2Cdev.readBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_XA_ST_BIT, buffer); 00308 return buffer[0]; 00309 } 00310 /** Get self-test enabled setting for accelerometer X axis. 00311 * @param enabled Self-test enabled value 00312 * @see MPU6050_RA_ACCEL_CONFIG 00313 */ 00314 void MPU6051::setAccelXSelfTest(bool enabled) 00315 { 00316 i2Cdev.writeBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_XA_ST_BIT, enabled); 00317 } 00318 /** Get self-test enabled value for accelerometer Y axis. 00319 * @return Self-test enabled value 00320 * @see MPU6050_RA_ACCEL_CONFIG 00321 */ 00322 bool MPU6051::getAccelYSelfTest() 00323 { 00324 i2Cdev.readBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_YA_ST_BIT, buffer); 00325 return buffer[0]; 00326 } 00327 /** Get self-test enabled value for accelerometer Y axis. 00328 * @param enabled Self-test enabled value 00329 * @see MPU6050_RA_ACCEL_CONFIG 00330 */ 00331 void MPU6051::setAccelYSelfTest(bool enabled) 00332 { 00333 i2Cdev.writeBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_YA_ST_BIT, enabled); 00334 } 00335 /** Get self-test enabled value for accelerometer Z axis. 00336 * @return Self-test enabled value 00337 * @see MPU6050_RA_ACCEL_CONFIG 00338 */ 00339 bool MPU6051::getAccelZSelfTest() 00340 { 00341 i2Cdev.readBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ZA_ST_BIT, buffer); 00342 return buffer[0]; 00343 } 00344 /** Set self-test enabled value for accelerometer Z axis. 00345 * @param enabled Self-test enabled value 00346 * @see MPU6050_RA_ACCEL_CONFIG 00347 */ 00348 void MPU6051::setAccelZSelfTest(bool enabled) 00349 { 00350 i2Cdev.writeBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ZA_ST_BIT, enabled); 00351 } 00352 /** Get full-scale accelerometer range. 00353 * The FS_SEL parameter allows setting the full-scale range of the accelerometer 00354 * sensors, as described in the table below. 00355 * 00356 * <pre> 00357 * 0 = +/- 2g 00358 * 1 = +/- 4g 00359 * 2 = +/- 8g 00360 * 3 = +/- 16g 00361 * </pre> 00362 * 00363 * @return Current full-scale accelerometer range setting 00364 * @see MPU6050_ACCEL_FS_2 00365 * @see MPU6050_RA_ACCEL_CONFIG 00366 * @see MPU6050_ACONFIG_AFS_SEL_BIT 00367 * @see MPU6050_ACONFIG_AFS_SEL_LENGTH 00368 */ 00369 uint8_t MPU6051::getFullScaleAccelRange() 00370 { 00371 i2Cdev.readBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_AFS_SEL_BIT, MPU6050_ACONFIG_AFS_SEL_LENGTH, buffer); 00372 return buffer[0]; 00373 } 00374 /** Set full-scale accelerometer range. 00375 * @param range New full-scale accelerometer range setting 00376 * @see getFullScaleAccelRange() 00377 */ 00378 void MPU6051::setFullScaleAccelRange(uint8_t range) 00379 { 00380 i2Cdev.writeBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_AFS_SEL_BIT, MPU6050_ACONFIG_AFS_SEL_LENGTH, range); 00381 } 00382 /** Get the high-pass filter configuration. 00383 * The DHPF is a filter module in the path leading to motion detectors (Free 00384 * Fall, Motion threshold, and Zero Motion). The high pass filter output is not 00385 * available to the data registers (see Figure in Section 8 of the MPU-6000/ 00386 * MPU-6050 Product Specification document). 00387 * 00388 * The high pass filter has three modes: 00389 * 00390 * <pre> 00391 * Reset: The filter output settles to zero within one sample. This 00392 * effectively disables the high pass filter. This mode may be toggled 00393 * to quickly settle the filter. 00394 * 00395 * On: The high pass filter will pass signals above the cut off frequency. 00396 * 00397 * Hold: When triggered, the filter holds the present sample. The filter 00398 * output will be the difference between the input sample and the held 00399 * sample. 00400 * </pre> 00401 * 00402 * <pre> 00403 * ACCEL_HPF | Filter Mode | Cut-off Frequency 00404 * ----------+-------------+------------------ 00405 * 0 | Reset | None 00406 * 1 | On | 5Hz 00407 * 2 | On | 2.5Hz 00408 * 3 | On | 1.25Hz 00409 * 4 | On | 0.63Hz 00410 * 7 | Hold | None 00411 * </pre> 00412 * 00413 * @return Current high-pass filter configuration 00414 * @see MPU6050_DHPF_RESET 00415 * @see MPU6050_RA_ACCEL_CONFIG 00416 */ 00417 uint8_t MPU6051::getDHPFMode() 00418 { 00419 i2Cdev.readBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ACCEL_HPF_BIT, MPU6050_ACONFIG_ACCEL_HPF_LENGTH, buffer); 00420 return buffer[0]; 00421 } 00422 /** Set the high-pass filter configuration. 00423 * @param bandwidth New high-pass filter configuration 00424 * @see setDHPFMode() 00425 * @see MPU6050_DHPF_RESET 00426 * @see MPU6050_RA_ACCEL_CONFIG 00427 */ 00428 void MPU6051::setDHPFMode(uint8_t bandwidth) 00429 { 00430 i2Cdev.writeBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ACCEL_HPF_BIT, MPU6050_ACONFIG_ACCEL_HPF_LENGTH, bandwidth); 00431 } 00432 00433 // FF_THR register 00434 00435 /** Get free-fall event acceleration threshold. 00436 * This register configures the detection threshold for Free Fall event 00437 * detection. The unit of FF_THR is 1LSB = 2mg. Free Fall is detected when the 00438 * absolute value of the accelerometer measurements for the three axes are each 00439 * less than the detection threshold. This condition increments the Free Fall 00440 * duration counter (Register 30). The Free Fall interrupt is triggered when the 00441 * Free Fall duration counter reaches the time specified in FF_DUR. 00442 * 00443 * For more details on the Free Fall detection interrupt, see Section 8.2 of the 00444 * MPU-6000/MPU-6050 Product Specification document as well as Registers 56 and 00445 * 58 of this document. 00446 * 00447 * @return Current free-fall acceleration threshold value (LSB = 2mg) 00448 * @see MPU6050_RA_FF_THR 00449 */ 00450 uint8_t MPU6051::getFreefallDetectionThreshold() 00451 { 00452 i2Cdev.readByte(devAddr, MPU6050_RA_FF_THR, buffer); 00453 return buffer[0]; 00454 } 00455 /** Get free-fall event acceleration threshold. 00456 * @param threshold New free-fall acceleration threshold value (LSB = 2mg) 00457 * @see getFreefallDetectionThreshold() 00458 * @see MPU6050_RA_FF_THR 00459 */ 00460 void MPU6051::setFreefallDetectionThreshold(uint8_t threshold) 00461 { 00462 i2Cdev.writeByte(devAddr, MPU6050_RA_FF_THR, threshold); 00463 } 00464 00465 // FF_DUR register 00466 00467 /** Get free-fall event duration threshold. 00468 * This register configures the duration counter threshold for Free Fall event 00469 * detection. The duration counter ticks at 1kHz, therefore FF_DUR has a unit 00470 * of 1 LSB = 1 ms. 00471 * 00472 * The Free Fall duration counter increments while the absolute value of the 00473 * accelerometer measurements are each less than the detection threshold 00474 * (Register 29). The Free Fall interrupt is triggered when the Free Fall 00475 * duration counter reaches the time specified in this register. 00476 * 00477 * For more details on the Free Fall detection interrupt, see Section 8.2 of 00478 * the MPU-6000/MPU-6050 Product Specification document as well as Registers 56 00479 * and 58 of this document. 00480 * 00481 * @return Current free-fall duration threshold value (LSB = 1ms) 00482 * @see MPU6050_RA_FF_DUR 00483 */ 00484 uint8_t MPU6051::getFreefallDetectionDuration() 00485 { 00486 i2Cdev.readByte(devAddr, MPU6050_RA_FF_DUR, buffer); 00487 return buffer[0]; 00488 } 00489 /** Get free-fall event duration threshold. 00490 * @param duration New free-fall duration threshold value (LSB = 1ms) 00491 * @see getFreefallDetectionDuration() 00492 * @see MPU6050_RA_FF_DUR 00493 */ 00494 void MPU6051::setFreefallDetectionDuration(uint8_t duration) 00495 { 00496 i2Cdev.writeByte(devAddr, MPU6050_RA_FF_DUR, duration); 00497 } 00498 00499 // MOT_THR register 00500 00501 /** Get motion detection event acceleration threshold. 00502 * This register configures the detection threshold for Motion interrupt 00503 * generation. The unit of MOT_THR is 1LSB = 2mg. Motion is detected when the 00504 * absolute value of any of the accelerometer measurements exceeds this Motion 00505 * detection threshold. This condition increments the Motion detection duration 00506 * counter (Register 32). The Motion detection interrupt is triggered when the 00507 * Motion Detection counter reaches the time count specified in MOT_DUR 00508 * (Register 32). 00509 * 00510 * The Motion interrupt will indicate the axis and polarity of detected motion 00511 * in MOT_DETECT_STATUS (Register 97). 00512 * 00513 * For more details on the Motion detection interrupt, see Section 8.3 of the 00514 * MPU-6000/MPU-6050 Product Specification document as well as Registers 56 and 00515 * 58 of this document. 00516 * 00517 * @return Current motion detection acceleration threshold value (LSB = 2mg) 00518 * @see MPU6050_RA_MOT_THR 00519 */ 00520 uint8_t MPU6051::getMotionDetectionThreshold() 00521 { 00522 i2Cdev.readByte(devAddr, MPU6050_RA_MOT_THR, buffer); 00523 return buffer[0]; 00524 } 00525 /** Set free-fall event acceleration threshold. 00526 * @param threshold New motion detection acceleration threshold value (LSB = 2mg) 00527 * @see getMotionDetectionThreshold() 00528 * @see MPU6050_RA_MOT_THR 00529 */ 00530 void MPU6051::setMotionDetectionThreshold(uint8_t threshold) 00531 { 00532 i2Cdev.writeByte(devAddr, MPU6050_RA_MOT_THR, threshold); 00533 } 00534 00535 // MOT_DUR register 00536 00537 /** Get motion detection event duration threshold. 00538 * This register configures the duration counter threshold for Motion interrupt 00539 * generation. The duration counter ticks at 1 kHz, therefore MOT_DUR has a unit 00540 * of 1LSB = 1ms. The Motion detection duration counter increments when the 00541 * absolute value of any of the accelerometer measurements exceeds the Motion 00542 * detection threshold (Register 31). The Motion detection interrupt is 00543 * triggered when the Motion detection counter reaches the time count specified 00544 * in this register. 00545 * 00546 * For more details on the Motion detection interrupt, see Section 8.3 of the 00547 * MPU-6000/MPU-6050 Product Specification document. 00548 * 00549 * @return Current motion detection duration threshold value (LSB = 1ms) 00550 * @see MPU6050_RA_MOT_DUR 00551 */ 00552 uint8_t MPU6051::getMotionDetectionDuration() 00553 { 00554 i2Cdev.readByte(devAddr, MPU6050_RA_MOT_DUR, buffer); 00555 return buffer[0]; 00556 } 00557 /** Set motion detection event duration threshold. 00558 * @param duration New motion detection duration threshold value (LSB = 1ms) 00559 * @see getMotionDetectionDuration() 00560 * @see MPU6050_RA_MOT_DUR 00561 */ 00562 void MPU6051::setMotionDetectionDuration(uint8_t duration) 00563 { 00564 i2Cdev.writeByte(devAddr, MPU6050_RA_MOT_DUR, duration); 00565 } 00566 00567 // ZRMOT_THR register 00568 00569 /** Get zero motion detection event acceleration threshold. 00570 * This register configures the detection threshold for Zero Motion interrupt 00571 * generation. The unit of ZRMOT_THR is 1LSB = 2mg. Zero Motion is detected when 00572 * the absolute value of the accelerometer measurements for the 3 axes are each 00573 * less than the detection threshold. This condition increments the Zero Motion 00574 * duration counter (Register 34). The Zero Motion interrupt is triggered when 00575 * the Zero Motion duration counter reaches the time count specified in 00576 * ZRMOT_DUR (Register 34). 00577 * 00578 * Unlike Free Fall or Motion detection, Zero Motion detection triggers an 00579 * interrupt both when Zero Motion is first detected and when Zero Motion is no 00580 * longer detected. 00581 * 00582 * When a zero motion event is detected, a Zero Motion Status will be indicated 00583 * in the MOT_DETECT_STATUS register (Register 97). When a motion-to-zero-motion 00584 * condition is detected, the status bit is set to 1. When a zero-motion-to- 00585 * motion condition is detected, the status bit is set to 0. 00586 * 00587 * For more details on the Zero Motion detection interrupt, see Section 8.4 of 00588 * the MPU-6000/MPU-6050 Product Specification document as well as Registers 56 00589 * and 58 of this document. 00590 * 00591 * @return Current zero motion detection acceleration threshold value (LSB = 2mg) 00592 * @see MPU6050_RA_ZRMOT_THR 00593 */ 00594 uint8_t MPU6051::getZeroMotionDetectionThreshold() 00595 { 00596 i2Cdev.readByte(devAddr, MPU6050_RA_ZRMOT_THR, buffer); 00597 return buffer[0]; 00598 } 00599 /** Set zero motion detection event acceleration threshold. 00600 * @param threshold New zero motion detection acceleration threshold value (LSB = 2mg) 00601 * @see getZeroMotionDetectionThreshold() 00602 * @see MPU6050_RA_ZRMOT_THR 00603 */ 00604 void MPU6051::setZeroMotionDetectionThreshold(uint8_t threshold) 00605 { 00606 i2Cdev.writeByte(devAddr, MPU6050_RA_ZRMOT_THR, threshold); 00607 } 00608 00609 // ZRMOT_DUR register 00610 00611 /** Get zero motion detection event duration threshold. 00612 * This register configures the duration counter threshold for Zero Motion 00613 * interrupt generation. The duration counter ticks at 16 Hz, therefore 00614 * ZRMOT_DUR has a unit of 1 LSB = 64 ms. The Zero Motion duration counter 00615 * increments while the absolute value of the accelerometer measurements are 00616 * each less than the detection threshold (Register 33). The Zero Motion 00617 * interrupt is triggered when the Zero Motion duration counter reaches the time 00618 * count specified in this register. 00619 * 00620 * For more details on the Zero Motion detection interrupt, see Section 8.4 of 00621 * the MPU-6000/MPU-6050 Product Specification document, as well as Registers 56 00622 * and 58 of this document. 00623 * 00624 * @return Current zero motion detection duration threshold value (LSB = 64ms) 00625 * @see MPU6050_RA_ZRMOT_DUR 00626 */ 00627 uint8_t MPU6051::getZeroMotionDetectionDuration() 00628 { 00629 i2Cdev.readByte(devAddr, MPU6050_RA_ZRMOT_DUR, buffer); 00630 return buffer[0]; 00631 } 00632 /** Set zero motion detection event duration threshold. 00633 * @param duration New zero motion detection duration threshold value (LSB = 1ms) 00634 * @see getZeroMotionDetectionDuration() 00635 * @see MPU6050_RA_ZRMOT_DUR 00636 */ 00637 void MPU6051::setZeroMotionDetectionDuration(uint8_t duration) 00638 { 00639 i2Cdev.writeByte(devAddr, MPU6050_RA_ZRMOT_DUR, duration); 00640 } 00641 00642 // FIFO_EN register 00643 00644 /** Get temperature FIFO enabled value. 00645 * When set to 1, this bit enables TEMP_OUT_H and TEMP_OUT_L (Registers 65 and 00646 * 66) to be written into the FIFO buffer. 00647 * @return Current temperature FIFO enabled value 00648 * @see MPU6050_RA_FIFO_EN 00649 */ 00650 bool MPU6051::getTempFIFOEnabled() 00651 { 00652 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_TEMP_FIFO_EN_BIT, buffer); 00653 return buffer[0]; 00654 } 00655 /** Set temperature FIFO enabled value. 00656 * @param enabled New temperature FIFO enabled value 00657 * @see getTempFIFOEnabled() 00658 * @see MPU6050_RA_FIFO_EN 00659 */ 00660 void MPU6051::setTempFIFOEnabled(bool enabled) 00661 { 00662 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_TEMP_FIFO_EN_BIT, enabled); 00663 } 00664 /** Get gyroscope X-axis FIFO enabled value. 00665 * When set to 1, this bit enables GYRO_XOUT_H and GYRO_XOUT_L (Registers 67 and 00666 * 68) to be written into the FIFO buffer. 00667 * @return Current gyroscope X-axis FIFO enabled value 00668 * @see MPU6050_RA_FIFO_EN 00669 */ 00670 bool MPU6051::getXGyroFIFOEnabled() 00671 { 00672 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_XG_FIFO_EN_BIT, buffer); 00673 return buffer[0]; 00674 } 00675 /** Set gyroscope X-axis FIFO enabled value. 00676 * @param enabled New gyroscope X-axis FIFO enabled value 00677 * @see getXGyroFIFOEnabled() 00678 * @see MPU6050_RA_FIFO_EN 00679 */ 00680 void MPU6051::setXGyroFIFOEnabled(bool enabled) 00681 { 00682 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_XG_FIFO_EN_BIT, enabled); 00683 } 00684 /** Get gyroscope Y-axis FIFO enabled value. 00685 * When set to 1, this bit enables GYRO_YOUT_H and GYRO_YOUT_L (Registers 69 and 00686 * 70) to be written into the FIFO buffer. 00687 * @return Current gyroscope Y-axis FIFO enabled value 00688 * @see MPU6050_RA_FIFO_EN 00689 */ 00690 bool MPU6051::getYGyroFIFOEnabled() 00691 { 00692 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_YG_FIFO_EN_BIT, buffer); 00693 return buffer[0]; 00694 } 00695 /** Set gyroscope Y-axis FIFO enabled value. 00696 * @param enabled New gyroscope Y-axis FIFO enabled value 00697 * @see getYGyroFIFOEnabled() 00698 * @see MPU6050_RA_FIFO_EN 00699 */ 00700 void MPU6051::setYGyroFIFOEnabled(bool enabled) 00701 { 00702 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_YG_FIFO_EN_BIT, enabled); 00703 } 00704 /** Get gyroscope Z-axis FIFO enabled value. 00705 * When set to 1, this bit enables GYRO_ZOUT_H and GYRO_ZOUT_L (Registers 71 and 00706 * 72) to be written into the FIFO buffer. 00707 * @return Current gyroscope Z-axis FIFO enabled value 00708 * @see MPU6050_RA_FIFO_EN 00709 */ 00710 bool MPU6051::getZGyroFIFOEnabled() 00711 { 00712 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_ZG_FIFO_EN_BIT, buffer); 00713 return buffer[0]; 00714 } 00715 /** Set gyroscope Z-axis FIFO enabled value. 00716 * @param enabled New gyroscope Z-axis FIFO enabled value 00717 * @see getZGyroFIFOEnabled() 00718 * @see MPU6050_RA_FIFO_EN 00719 */ 00720 void MPU6051::setZGyroFIFOEnabled(bool enabled) 00721 { 00722 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_ZG_FIFO_EN_BIT, enabled); 00723 } 00724 /** Get accelerometer FIFO enabled value. 00725 * When set to 1, this bit enables ACCEL_XOUT_H, ACCEL_XOUT_L, ACCEL_YOUT_H, 00726 * ACCEL_YOUT_L, ACCEL_ZOUT_H, and ACCEL_ZOUT_L (Registers 59 to 64) to be 00727 * written into the FIFO buffer. 00728 * @return Current accelerometer FIFO enabled value 00729 * @see MPU6050_RA_FIFO_EN 00730 */ 00731 bool MPU6051::getAccelFIFOEnabled() 00732 { 00733 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_ACCEL_FIFO_EN_BIT, buffer); 00734 return buffer[0]; 00735 } 00736 /** Set accelerometer FIFO enabled value. 00737 * @param enabled New accelerometer FIFO enabled value 00738 * @see getAccelFIFOEnabled() 00739 * @see MPU6050_RA_FIFO_EN 00740 */ 00741 void MPU6051::setAccelFIFOEnabled(bool enabled) 00742 { 00743 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_ACCEL_FIFO_EN_BIT, enabled); 00744 } 00745 /** Get Slave 2 FIFO enabled value. 00746 * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96) 00747 * associated with Slave 2 to be written into the FIFO buffer. 00748 * @return Current Slave 2 FIFO enabled value 00749 * @see MPU6050_RA_FIFO_EN 00750 */ 00751 bool MPU6051::getSlave2FIFOEnabled() 00752 { 00753 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV2_FIFO_EN_BIT, buffer); 00754 return buffer[0]; 00755 } 00756 /** Set Slave 2 FIFO enabled value. 00757 * @param enabled New Slave 2 FIFO enabled value 00758 * @see getSlave2FIFOEnabled() 00759 * @see MPU6050_RA_FIFO_EN 00760 */ 00761 void MPU6051::setSlave2FIFOEnabled(bool enabled) 00762 { 00763 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV2_FIFO_EN_BIT, enabled); 00764 } 00765 /** Get Slave 1 FIFO enabled value. 00766 * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96) 00767 * associated with Slave 1 to be written into the FIFO buffer. 00768 * @return Current Slave 1 FIFO enabled value 00769 * @see MPU6050_RA_FIFO_EN 00770 */ 00771 bool MPU6051::getSlave1FIFOEnabled() 00772 { 00773 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV1_FIFO_EN_BIT, buffer); 00774 return buffer[0]; 00775 } 00776 /** Set Slave 1 FIFO enabled value. 00777 * @param enabled New Slave 1 FIFO enabled value 00778 * @see getSlave1FIFOEnabled() 00779 * @see MPU6050_RA_FIFO_EN 00780 */ 00781 void MPU6051::setSlave1FIFOEnabled(bool enabled) 00782 { 00783 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV1_FIFO_EN_BIT, enabled); 00784 } 00785 /** Get Slave 0 FIFO enabled value. 00786 * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96) 00787 * associated with Slave 0 to be written into the FIFO buffer. 00788 * @return Current Slave 0 FIFO enabled value 00789 * @see MPU6050_RA_FIFO_EN 00790 */ 00791 bool MPU6051::getSlave0FIFOEnabled() 00792 { 00793 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV0_FIFO_EN_BIT, buffer); 00794 return buffer[0]; 00795 } 00796 /** Set Slave 0 FIFO enabled value. 00797 * @param enabled New Slave 0 FIFO enabled value 00798 * @see getSlave0FIFOEnabled() 00799 * @see MPU6050_RA_FIFO_EN 00800 */ 00801 void MPU6051::setSlave0FIFOEnabled(bool enabled) 00802 { 00803 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV0_FIFO_EN_BIT, enabled); 00804 } 00805 00806 // I2C_MST_CTRL register 00807 00808 /** Get multi-master enabled value. 00809 * Multi-master capability allows multiple I2C masters to operate on the same 00810 * bus. In circuits where multi-master capability is required, set MULT_MST_EN 00811 * to 1. This will increase current drawn by approximately 30uA. 00812 * 00813 * In circuits where multi-master capability is required, the state of the I2C 00814 * bus must always be monitored by each separate I2C Master. Before an I2C 00815 * Master can assume arbitration of the bus, it must first confirm that no other 00816 * I2C Master has arbitration of the bus. When MULT_MST_EN is set to 1, the 00817 * MPU-60X0's bus arbitration detection logic is turned on, enabling it to 00818 * detect when the bus is available. 00819 * 00820 * @return Current multi-master enabled value 00821 * @see MPU6050_RA_I2C_MST_CTRL 00822 */ 00823 bool MPU6051::getMultiMasterEnabled() 00824 { 00825 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_MULT_MST_EN_BIT, buffer); 00826 return buffer[0]; 00827 } 00828 /** Set multi-master enabled value. 00829 * @param enabled New multi-master enabled value 00830 * @see getMultiMasterEnabled() 00831 * @see MPU6050_RA_I2C_MST_CTRL 00832 */ 00833 void MPU6051::setMultiMasterEnabled(bool enabled) 00834 { 00835 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_MULT_MST_EN_BIT, enabled); 00836 } 00837 /** Get wait-for-external-sensor-data enabled value. 00838 * When the WAIT_FOR_ES bit is set to 1, the Data Ready interrupt will be 00839 * delayed until External Sensor data from the Slave Devices are loaded into the 00840 * EXT_SENS_DATA registers. This is used to ensure that both the internal sensor 00841 * data (i.e. from gyro and accel) and external sensor data have been loaded to 00842 * their respective data registers (i.e. the data is synced) when the Data Ready 00843 * interrupt is triggered. 00844 * 00845 * @return Current wait-for-external-sensor-data enabled value 00846 * @see MPU6050_RA_I2C_MST_CTRL 00847 */ 00848 bool MPU6051::getWaitForExternalSensorEnabled() 00849 { 00850 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_WAIT_FOR_ES_BIT, buffer); 00851 return buffer[0]; 00852 } 00853 /** Set wait-for-external-sensor-data enabled value. 00854 * @param enabled New wait-for-external-sensor-data enabled value 00855 * @see getWaitForExternalSensorEnabled() 00856 * @see MPU6050_RA_I2C_MST_CTRL 00857 */ 00858 void MPU6051::setWaitForExternalSensorEnabled(bool enabled) 00859 { 00860 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_WAIT_FOR_ES_BIT, enabled); 00861 } 00862 /** Get Slave 3 FIFO enabled value. 00863 * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96) 00864 * associated with Slave 3 to be written into the FIFO buffer. 00865 * @return Current Slave 3 FIFO enabled value 00866 * @see MPU6050_RA_MST_CTRL 00867 */ 00868 bool MPU6051::getSlave3FIFOEnabled() 00869 { 00870 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_SLV_3_FIFO_EN_BIT, buffer); 00871 return buffer[0]; 00872 } 00873 /** Set Slave 3 FIFO enabled value. 00874 * @param enabled New Slave 3 FIFO enabled value 00875 * @see getSlave3FIFOEnabled() 00876 * @see MPU6050_RA_MST_CTRL 00877 */ 00878 void MPU6051::setSlave3FIFOEnabled(bool enabled) 00879 { 00880 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_SLV_3_FIFO_EN_BIT, enabled); 00881 } 00882 /** Get slave read/write transition enabled value. 00883 * The I2C_MST_P_NSR bit configures the I2C Master's transition from one slave 00884 * read to the next slave read. If the bit equals 0, there will be a restart 00885 * between reads. If the bit equals 1, there will be a stop followed by a start 00886 * of the following read. When a write transaction follows a read transaction, 00887 * the stop followed by a start of the successive write will be always used. 00888 * 00889 * @return Current slave read/write transition enabled value 00890 * @see MPU6050_RA_I2C_MST_CTRL 00891 */ 00892 bool MPU6051::getSlaveReadWriteTransitionEnabled() 00893 { 00894 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_P_NSR_BIT, buffer); 00895 return buffer[0]; 00896 } 00897 /** Set slave read/write transition enabled value. 00898 * @param enabled New slave read/write transition enabled value 00899 * @see getSlaveReadWriteTransitionEnabled() 00900 * @see MPU6050_RA_I2C_MST_CTRL 00901 */ 00902 void MPU6051::setSlaveReadWriteTransitionEnabled(bool enabled) 00903 { 00904 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_P_NSR_BIT, enabled); 00905 } 00906 /** Get I2C master clock speed. 00907 * I2C_MST_CLK is a 4 bit unsigned value which configures a divider on the 00908 * MPU-60X0 internal 8MHz clock. It sets the I2C master clock speed according to 00909 * the following table: 00910 * 00911 * <pre> 00912 * I2C_MST_CLK | I2C Master Clock Speed | 8MHz Clock Divider 00913 * ------------+------------------------+------------------- 00914 * 0 | 348kHz | 23 00915 * 1 | 333kHz | 24 00916 * 2 | 320kHz | 25 00917 * 3 | 308kHz | 26 00918 * 4 | 296kHz | 27 00919 * 5 | 286kHz | 28 00920 * 6 | 276kHz | 29 00921 * 7 | 267kHz | 30 00922 * 8 | 258kHz | 31 00923 * 9 | 500kHz | 16 00924 * 10 | 471kHz | 17 00925 * 11 | 444kHz | 18 00926 * 12 | 421kHz | 19 00927 * 13 | 400kHz | 20 00928 * 14 | 381kHz | 21 00929 * 15 | 364kHz | 22 00930 * </pre> 00931 * 00932 * @return Current I2C master clock speed 00933 * @see MPU6050_RA_I2C_MST_CTRL 00934 */ 00935 uint8_t MPU6051::getMasterClockSpeed() 00936 { 00937 i2Cdev.readBits(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_CLK_BIT, MPU6050_I2C_MST_CLK_LENGTH, buffer); 00938 return buffer[0]; 00939 } 00940 /** Set I2C master clock speed. 00941 * @reparam speed Current I2C master clock speed 00942 * @see MPU6050_RA_I2C_MST_CTRL 00943 */ 00944 void MPU6051::setMasterClockSpeed(uint8_t speed) 00945 { 00946 i2Cdev.writeBits(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_CLK_BIT, MPU6050_I2C_MST_CLK_LENGTH, speed); 00947 } 00948 00949 // I2C_SLV* registers (Slave 0-3) 00950 00951 /** Get the I2C address of the specified slave (0-3). 00952 * Note that Bit 7 (MSB) controls read/write mode. If Bit 7 is set, it's a read 00953 * operation, and if it is cleared, then it's a write operation. The remaining 00954 * bits (6-0) are the 7-bit device address of the slave device. 00955 * 00956 * In read mode, the result of the read is placed in the lowest available 00957 * EXT_SENS_DATA register. For further information regarding the allocation of 00958 * read results, please refer to the EXT_SENS_DATA register description 00959 * (Registers 73 - 96). 00960 * 00961 * The MPU-6050 supports a total of five slaves, but Slave 4 has unique 00962 * characteristics, and so it has its own functions (getSlave4* and setSlave4*). 00963 * 00964 * I2C data transactions are performed at the Sample Rate, as defined in 00965 * Register 25. The user is responsible for ensuring that I2C data transactions 00966 * to and from each enabled Slave can be completed within a single period of the 00967 * Sample Rate. 00968 * 00969 * The I2C slave access rate can be reduced relative to the Sample Rate. This 00970 * reduced access rate is determined by I2C_MST_DLY (Register 52). Whether a 00971 * slave's access rate is reduced relative to the Sample Rate is determined by 00972 * I2C_MST_DELAY_CTRL (Register 103). 00973 * 00974 * The processing order for the slaves is fixed. The sequence followed for 00975 * processing the slaves is Slave 0, Slave 1, Slave 2, Slave 3 and Slave 4. If a 00976 * particular Slave is disabled it will be skipped. 00977 * 00978 * Each slave can either be accessed at the sample rate or at a reduced sample 00979 * rate. In a case where some slaves are accessed at the Sample Rate and some 00980 * slaves are accessed at the reduced rate, the sequence of accessing the slaves 00981 * (Slave 0 to Slave 4) is still followed. However, the reduced rate slaves will 00982 * be skipped if their access rate dictates that they should not be accessed 00983 * during that particular cycle. For further information regarding the reduced 00984 * access rate, please refer to Register 52. Whether a slave is accessed at the 00985 * Sample Rate or at the reduced rate is determined by the Delay Enable bits in 00986 * Register 103. 00987 * 00988 * @param num Slave number (0-3) 00989 * @return Current address for specified slave 00990 * @see MPU6050_RA_I2C_SLV0_ADDR 00991 */ 00992 uint8_t MPU6051::getSlaveAddress(uint8_t num) 00993 { 00994 if (num > 3) return 0; 00995 i2Cdev.readByte(devAddr, MPU6050_RA_I2C_SLV0_ADDR + num*3, buffer); 00996 return buffer[0]; 00997 } 00998 /** Set the I2C address of the specified slave (0-3). 00999 * @param num Slave number (0-3) 01000 * @param address New address for specified slave 01001 * @see getSlaveAddress() 01002 * @see MPU6050_RA_I2C_SLV0_ADDR 01003 */ 01004 void MPU6051::setSlaveAddress(uint8_t num, uint8_t address) 01005 { 01006 if (num > 3) return; 01007 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV0_ADDR + num*3, address); 01008 } 01009 /** Get the active internal register for the specified slave (0-3). 01010 * Read/write operations for this slave will be done to whatever internal 01011 * register address is stored in this MPU register. 01012 * 01013 * The MPU-6050 supports a total of five slaves, but Slave 4 has unique 01014 * characteristics, and so it has its own functions. 01015 * 01016 * @param num Slave number (0-3) 01017 * @return Current active register for specified slave 01018 * @see MPU6050_RA_I2C_SLV0_REG 01019 */ 01020 uint8_t MPU6051::getSlaveRegister(uint8_t num) 01021 { 01022 if (num > 3) return 0; 01023 i2Cdev.readByte(devAddr, MPU6050_RA_I2C_SLV0_REG + num*3, buffer); 01024 return buffer[0]; 01025 } 01026 /** Set the active internal register for the specified slave (0-3). 01027 * @param num Slave number (0-3) 01028 * @param reg New active register for specified slave 01029 * @see getSlaveRegister() 01030 * @see MPU6050_RA_I2C_SLV0_REG 01031 */ 01032 void MPU6051::setSlaveRegister(uint8_t num, uint8_t reg) 01033 { 01034 if (num > 3) return; 01035 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV0_REG + num*3, reg); 01036 } 01037 /** Get the enabled value for the specified slave (0-3). 01038 * When set to 1, this bit enables Slave 0 for data transfer operations. When 01039 * cleared to 0, this bit disables Slave 0 from data transfer operations. 01040 * @param num Slave number (0-3) 01041 * @return Current enabled value for specified slave 01042 * @see MPU6050_RA_I2C_SLV0_CTRL 01043 */ 01044 bool MPU6051::getSlaveEnabled(uint8_t num) 01045 { 01046 if (num > 3) return 0; 01047 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_EN_BIT, buffer); 01048 return buffer[0]; 01049 } 01050 /** Set the enabled value for the specified slave (0-3). 01051 * @param num Slave number (0-3) 01052 * @param enabled New enabled value for specified slave 01053 * @see getSlaveEnabled() 01054 * @see MPU6050_RA_I2C_SLV0_CTRL 01055 */ 01056 void MPU6051::setSlaveEnabled(uint8_t num, bool enabled) 01057 { 01058 if (num > 3) return; 01059 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_EN_BIT, enabled); 01060 } 01061 /** Get word pair byte-swapping enabled for the specified slave (0-3). 01062 * When set to 1, this bit enables byte swapping. When byte swapping is enabled, 01063 * the high and low bytes of a word pair are swapped. Please refer to 01064 * I2C_SLV0_GRP for the pairing convention of the word pairs. When cleared to 0, 01065 * bytes transferred to and from Slave 0 will be written to EXT_SENS_DATA 01066 * registers in the order they were transferred. 01067 * 01068 * @param num Slave number (0-3) 01069 * @return Current word pair byte-swapping enabled value for specified slave 01070 * @see MPU6050_RA_I2C_SLV0_CTRL 01071 */ 01072 bool MPU6051::getSlaveWordByteSwap(uint8_t num) 01073 { 01074 if (num > 3) return 0; 01075 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_BYTE_SW_BIT, buffer); 01076 return buffer[0]; 01077 } 01078 /** Set word pair byte-swapping enabled for the specified slave (0-3). 01079 * @param num Slave number (0-3) 01080 * @param enabled New word pair byte-swapping enabled value for specified slave 01081 * @see getSlaveWordByteSwap() 01082 * @see MPU6050_RA_I2C_SLV0_CTRL 01083 */ 01084 void MPU6051::setSlaveWordByteSwap(uint8_t num, bool enabled) 01085 { 01086 if (num > 3) return; 01087 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_BYTE_SW_BIT, enabled); 01088 } 01089 /** Get write mode for the specified slave (0-3). 01090 * When set to 1, the transaction will read or write data only. When cleared to 01091 * 0, the transaction will write a register address prior to reading or writing 01092 * data. This should equal 0 when specifying the register address within the 01093 * Slave device to/from which the ensuing data transaction will take place. 01094 * 01095 * @param num Slave number (0-3) 01096 * @return Current write mode for specified slave (0 = register address + data, 1 = data only) 01097 * @see MPU6050_RA_I2C_SLV0_CTRL 01098 */ 01099 bool MPU6051::getSlaveWriteMode(uint8_t num) 01100 { 01101 if (num > 3) return 0; 01102 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_REG_DIS_BIT, buffer); 01103 return buffer[0]; 01104 } 01105 /** Set write mode for the specified slave (0-3). 01106 * @param num Slave number (0-3) 01107 * @param mode New write mode for specified slave (0 = register address + data, 1 = data only) 01108 * @see getSlaveWriteMode() 01109 * @see MPU6050_RA_I2C_SLV0_CTRL 01110 */ 01111 void MPU6051::setSlaveWriteMode(uint8_t num, bool mode) 01112 { 01113 if (num > 3) return; 01114 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_REG_DIS_BIT, mode); 01115 } 01116 /** Get word pair grouping order offset for the specified slave (0-3). 01117 * This sets specifies the grouping order of word pairs received from registers. 01118 * When cleared to 0, bytes from register addresses 0 and 1, 2 and 3, etc (even, 01119 * then odd register addresses) are paired to form a word. When set to 1, bytes 01120 * from register addresses are paired 1 and 2, 3 and 4, etc. (odd, then even 01121 * register addresses) are paired to form a word. 01122 * 01123 * @param num Slave number (0-3) 01124 * @return Current word pair grouping order offset for specified slave 01125 * @see MPU6050_RA_I2C_SLV0_CTRL 01126 */ 01127 bool MPU6051::getSlaveWordGroupOffset(uint8_t num) 01128 { 01129 if (num > 3) return 0; 01130 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_GRP_BIT, buffer); 01131 return buffer[0]; 01132 } 01133 /** Set word pair grouping order offset for the specified slave (0-3). 01134 * @param num Slave number (0-3) 01135 * @param enabled New word pair grouping order offset for specified slave 01136 * @see getSlaveWordGroupOffset() 01137 * @see MPU6050_RA_I2C_SLV0_CTRL 01138 */ 01139 void MPU6051::setSlaveWordGroupOffset(uint8_t num, bool enabled) 01140 { 01141 if (num > 3) return; 01142 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_GRP_BIT, enabled); 01143 } 01144 /** Get number of bytes to read for the specified slave (0-3). 01145 * Specifies the number of bytes transferred to and from Slave 0. Clearing this 01146 * bit to 0 is equivalent to disabling the register by writing 0 to I2C_SLV0_EN. 01147 * @param num Slave number (0-3) 01148 * @return Number of bytes to read for specified slave 01149 * @see MPU6050_RA_I2C_SLV0_CTRL 01150 */ 01151 uint8_t MPU6051::getSlaveDataLength(uint8_t num) 01152 { 01153 if (num > 3) return 0; 01154 i2Cdev.readBits(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_LEN_BIT, MPU6050_I2C_SLV_LEN_LENGTH, buffer); 01155 return buffer[0]; 01156 } 01157 /** Set number of bytes to read for the specified slave (0-3). 01158 * @param num Slave number (0-3) 01159 * @param length Number of bytes to read for specified slave 01160 * @see getSlaveDataLength() 01161 * @see MPU6050_RA_I2C_SLV0_CTRL 01162 */ 01163 void MPU6051::setSlaveDataLength(uint8_t num, uint8_t length) 01164 { 01165 if (num > 3) return; 01166 i2Cdev.writeBits(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_LEN_BIT, MPU6050_I2C_SLV_LEN_LENGTH, length); 01167 } 01168 01169 // I2C_SLV* registers (Slave 4) 01170 01171 /** Get the I2C address of Slave 4. 01172 * Note that Bit 7 (MSB) controls read/write mode. If Bit 7 is set, it's a read 01173 * operation, and if it is cleared, then it's a write operation. The remaining 01174 * bits (6-0) are the 7-bit device address of the slave device. 01175 * 01176 * @return Current address for Slave 4 01177 * @see getSlaveAddress() 01178 * @see MPU6050_RA_I2C_SLV4_ADDR 01179 */ 01180 uint8_t MPU6051::getSlave4Address() 01181 { 01182 i2Cdev.readByte(devAddr, MPU6050_RA_I2C_SLV4_ADDR, buffer); 01183 return buffer[0]; 01184 } 01185 /** Set the I2C address of Slave 4. 01186 * @param address New address for Slave 4 01187 * @see getSlave4Address() 01188 * @see MPU6050_RA_I2C_SLV4_ADDR 01189 */ 01190 void MPU6051::setSlave4Address(uint8_t address) 01191 { 01192 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV4_ADDR, address); 01193 } 01194 /** Get the active internal register for the Slave 4. 01195 * Read/write operations for this slave will be done to whatever internal 01196 * register address is stored in this MPU register. 01197 * 01198 * @return Current active register for Slave 4 01199 * @see MPU6050_RA_I2C_SLV4_REG 01200 */ 01201 uint8_t MPU6051::getSlave4Register() 01202 { 01203 i2Cdev.readByte(devAddr, MPU6050_RA_I2C_SLV4_REG, buffer); 01204 return buffer[0]; 01205 } 01206 /** Set the active internal register for Slave 4. 01207 * @param reg New active register for Slave 4 01208 * @see getSlave4Register() 01209 * @see MPU6050_RA_I2C_SLV4_REG 01210 */ 01211 void MPU6051::setSlave4Register(uint8_t reg) 01212 { 01213 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV4_REG, reg); 01214 } 01215 /** Set new byte to write to Slave 4. 01216 * This register stores the data to be written into the Slave 4. If I2C_SLV4_RW 01217 * is set 1 (set to read), this register has no effect. 01218 * @param data New byte to write to Slave 4 01219 * @see MPU6050_RA_I2C_SLV4_DO 01220 */ 01221 void MPU6051::setSlave4OutputByte(uint8_t data) 01222 { 01223 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV4_DO, data); 01224 } 01225 /** Get the enabled value for the Slave 4. 01226 * When set to 1, this bit enables Slave 4 for data transfer operations. When 01227 * cleared to 0, this bit disables Slave 4 from data transfer operations. 01228 * @return Current enabled value for Slave 4 01229 * @see MPU6050_RA_I2C_SLV4_CTRL 01230 */ 01231 bool MPU6051::getSlave4Enabled() 01232 { 01233 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_EN_BIT, buffer); 01234 return buffer[0]; 01235 } 01236 /** Set the enabled value for Slave 4. 01237 * @param enabled New enabled value for Slave 4 01238 * @see getSlave4Enabled() 01239 * @see MPU6050_RA_I2C_SLV4_CTRL 01240 */ 01241 void MPU6051::setSlave4Enabled(bool enabled) 01242 { 01243 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_EN_BIT, enabled); 01244 } 01245 /** Get the enabled value for Slave 4 transaction interrupts. 01246 * When set to 1, this bit enables the generation of an interrupt signal upon 01247 * completion of a Slave 4 transaction. When cleared to 0, this bit disables the 01248 * generation of an interrupt signal upon completion of a Slave 4 transaction. 01249 * The interrupt status can be observed in Register 54. 01250 * 01251 * @return Current enabled value for Slave 4 transaction interrupts. 01252 * @see MPU6050_RA_I2C_SLV4_CTRL 01253 */ 01254 bool MPU6051::getSlave4InterruptEnabled() 01255 { 01256 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_INT_EN_BIT, buffer); 01257 return buffer[0]; 01258 } 01259 /** Set the enabled value for Slave 4 transaction interrupts. 01260 * @param enabled New enabled value for Slave 4 transaction interrupts. 01261 * @see getSlave4InterruptEnabled() 01262 * @see MPU6050_RA_I2C_SLV4_CTRL 01263 */ 01264 void MPU6051::setSlave4InterruptEnabled(bool enabled) 01265 { 01266 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_INT_EN_BIT, enabled); 01267 } 01268 /** Get write mode for Slave 4. 01269 * When set to 1, the transaction will read or write data only. When cleared to 01270 * 0, the transaction will write a register address prior to reading or writing 01271 * data. This should equal 0 when specifying the register address within the 01272 * Slave device to/from which the ensuing data transaction will take place. 01273 * 01274 * @return Current write mode for Slave 4 (0 = register address + data, 1 = data only) 01275 * @see MPU6050_RA_I2C_SLV4_CTRL 01276 */ 01277 bool MPU6051::getSlave4WriteMode() 01278 { 01279 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_REG_DIS_BIT, buffer); 01280 return buffer[0]; 01281 } 01282 /** Set write mode for the Slave 4. 01283 * @param mode New write mode for Slave 4 (0 = register address + data, 1 = data only) 01284 * @see getSlave4WriteMode() 01285 * @see MPU6050_RA_I2C_SLV4_CTRL 01286 */ 01287 void MPU6051::setSlave4WriteMode(bool mode) 01288 { 01289 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_REG_DIS_BIT, mode); 01290 } 01291 /** Get Slave 4 master delay value. 01292 * This configures the reduced access rate of I2C slaves relative to the Sample 01293 * Rate. When a slave's access rate is decreased relative to the Sample Rate, 01294 * the slave is accessed every: 01295 * 01296 * 1 / (1 + I2C_MST_DLY) samples 01297 * 01298 * This base Sample Rate in turn is determined by SMPLRT_DIV (register 25) and 01299 * DLPF_CFG (register 26). Whether a slave's access rate is reduced relative to 01300 * the Sample Rate is determined by I2C_MST_DELAY_CTRL (register 103). For 01301 * further information regarding the Sample Rate, please refer to register 25. 01302 * 01303 * @return Current Slave 4 master delay value 01304 * @see MPU6050_RA_I2C_SLV4_CTRL 01305 */ 01306 uint8_t MPU6051::getSlave4MasterDelay() 01307 { 01308 i2Cdev.readBits(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_MST_DLY_BIT, MPU6050_I2C_SLV4_MST_DLY_LENGTH, buffer); 01309 return buffer[0]; 01310 } 01311 /** Set Slave 4 master delay value. 01312 * @param delay New Slave 4 master delay value 01313 * @see getSlave4MasterDelay() 01314 * @see MPU6050_RA_I2C_SLV4_CTRL 01315 */ 01316 void MPU6051::setSlave4MasterDelay(uint8_t delay) 01317 { 01318 i2Cdev.writeBits(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_MST_DLY_BIT, MPU6050_I2C_SLV4_MST_DLY_LENGTH, delay); 01319 } 01320 /** Get last available byte read from Slave 4. 01321 * This register stores the data read from Slave 4. This field is populated 01322 * after a read transaction. 01323 * @return Last available byte read from to Slave 4 01324 * @see MPU6050_RA_I2C_SLV4_DI 01325 */ 01326 uint8_t MPU6051::getSlate4InputByte() 01327 { 01328 i2Cdev.readByte(devAddr, MPU6050_RA_I2C_SLV4_DI, buffer); 01329 return buffer[0]; 01330 } 01331 01332 // I2C_MST_STATUS register 01333 01334 /** Get FSYNC interrupt status. 01335 * This bit reflects the status of the FSYNC interrupt from an external device 01336 * into the MPU-60X0. This is used as a way to pass an external interrupt 01337 * through the MPU-60X0 to the host application processor. When set to 1, this 01338 * bit will cause an interrupt if FSYNC_INT_EN is asserted in INT_PIN_CFG 01339 * (Register 55). 01340 * @return FSYNC interrupt status 01341 * @see MPU6050_RA_I2C_MST_STATUS 01342 */ 01343 bool MPU6051::getPassthroughStatus() 01344 { 01345 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_PASS_THROUGH_BIT, buffer); 01346 return buffer[0]; 01347 } 01348 /** Get Slave 4 transaction done status. 01349 * Automatically sets to 1 when a Slave 4 transaction has completed. This 01350 * triggers an interrupt if the I2C_MST_INT_EN bit in the INT_ENABLE register 01351 * (Register 56) is asserted and if the SLV_4_DONE_INT bit is asserted in the 01352 * I2C_SLV4_CTRL register (Register 52). 01353 * @return Slave 4 transaction done status 01354 * @see MPU6050_RA_I2C_MST_STATUS 01355 */ 01356 bool MPU6051::getSlave4IsDone() 01357 { 01358 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV4_DONE_BIT, buffer); 01359 return buffer[0]; 01360 } 01361 /** Get master arbitration lost status. 01362 * This bit automatically sets to 1 when the I2C Master has lost arbitration of 01363 * the auxiliary I2C bus (an error condition). This triggers an interrupt if the 01364 * I2C_MST_INT_EN bit in the INT_ENABLE register (Register 56) is asserted. 01365 * @return Master arbitration lost status 01366 * @see MPU6050_RA_I2C_MST_STATUS 01367 */ 01368 bool MPU6051::getLostArbitration() 01369 { 01370 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_LOST_ARB_BIT, buffer); 01371 return buffer[0]; 01372 } 01373 /** Get Slave 4 NACK status. 01374 * This bit automatically sets to 1 when the I2C Master receives a NACK in a 01375 * transaction with Slave 4. This triggers an interrupt if the I2C_MST_INT_EN 01376 * bit in the INT_ENABLE register (Register 56) is asserted. 01377 * @return Slave 4 NACK interrupt status 01378 * @see MPU6050_RA_I2C_MST_STATUS 01379 */ 01380 bool MPU6051::getSlave4Nack() 01381 { 01382 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV4_NACK_BIT, buffer); 01383 return buffer[0]; 01384 } 01385 /** Get Slave 3 NACK status. 01386 * This bit automatically sets to 1 when the I2C Master receives a NACK in a 01387 * transaction with Slave 3. This triggers an interrupt if the I2C_MST_INT_EN 01388 * bit in the INT_ENABLE register (Register 56) is asserted. 01389 * @return Slave 3 NACK interrupt status 01390 * @see MPU6050_RA_I2C_MST_STATUS 01391 */ 01392 bool MPU6051::getSlave3Nack() 01393 { 01394 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV3_NACK_BIT, buffer); 01395 return buffer[0]; 01396 } 01397 /** Get Slave 2 NACK status. 01398 * This bit automatically sets to 1 when the I2C Master receives a NACK in a 01399 * transaction with Slave 2. This triggers an interrupt if the I2C_MST_INT_EN 01400 * bit in the INT_ENABLE register (Register 56) is asserted. 01401 * @return Slave 2 NACK interrupt status 01402 * @see MPU6050_RA_I2C_MST_STATUS 01403 */ 01404 bool MPU6051::getSlave2Nack() 01405 { 01406 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV2_NACK_BIT, buffer); 01407 return buffer[0]; 01408 } 01409 /** Get Slave 1 NACK status. 01410 * This bit automatically sets to 1 when the I2C Master receives a NACK in a 01411 * transaction with Slave 1. This triggers an interrupt if the I2C_MST_INT_EN 01412 * bit in the INT_ENABLE register (Register 56) is asserted. 01413 * @return Slave 1 NACK interrupt status 01414 * @see MPU6050_RA_I2C_MST_STATUS 01415 */ 01416 bool MPU6051::getSlave1Nack() 01417 { 01418 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV1_NACK_BIT, buffer); 01419 return buffer[0]; 01420 } 01421 /** Get Slave 0 NACK status. 01422 * This bit automatically sets to 1 when the I2C Master receives a NACK in a 01423 * transaction with Slave 0. This triggers an interrupt if the I2C_MST_INT_EN 01424 * bit in the INT_ENABLE register (Register 56) is asserted. 01425 * @return Slave 0 NACK interrupt status 01426 * @see MPU6050_RA_I2C_MST_STATUS 01427 */ 01428 bool MPU6051::getSlave0Nack() 01429 { 01430 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV0_NACK_BIT, buffer); 01431 return buffer[0]; 01432 } 01433 01434 // INT_PIN_CFG register 01435 01436 /** Get interrupt logic level mode. 01437 * Will be set 0 for active-high, 1 for active-low. 01438 * @return Current interrupt mode (0=active-high, 1=active-low) 01439 * @see MPU6050_RA_INT_PIN_CFG 01440 * @see MPU6050_INTCFG_INT_LEVEL_BIT 01441 */ 01442 bool MPU6051::getInterruptMode() 01443 { 01444 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_LEVEL_BIT, buffer); 01445 return buffer[0]; 01446 } 01447 /** Set interrupt logic level mode. 01448 * @param mode New interrupt mode (0=active-high, 1=active-low) 01449 * @see getInterruptMode() 01450 * @see MPU6050_RA_INT_PIN_CFG 01451 * @see MPU6050_INTCFG_INT_LEVEL_BIT 01452 */ 01453 void MPU6051::setInterruptMode(bool mode) 01454 { 01455 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_LEVEL_BIT, mode); 01456 } 01457 /** Get interrupt drive mode. 01458 * Will be set 0 for push-pull, 1 for open-drain. 01459 * @return Current interrupt drive mode (0=push-pull, 1=open-drain) 01460 * @see MPU6050_RA_INT_PIN_CFG 01461 * @see MPU6050_INTCFG_INT_OPEN_BIT 01462 */ 01463 bool MPU6051::getInterruptDrive() 01464 { 01465 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_OPEN_BIT, buffer); 01466 return buffer[0]; 01467 } 01468 /** Set interrupt drive mode. 01469 * @param drive New interrupt drive mode (0=push-pull, 1=open-drain) 01470 * @see getInterruptDrive() 01471 * @see MPU6050_RA_INT_PIN_CFG 01472 * @see MPU6050_INTCFG_INT_OPEN_BIT 01473 */ 01474 void MPU6051::setInterruptDrive(bool drive) 01475 { 01476 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_OPEN_BIT, drive); 01477 } 01478 /** Get interrupt latch mode. 01479 * Will be set 0 for 50us-pulse, 1 for latch-until-int-cleared. 01480 * @return Current latch mode (0=50us-pulse, 1=latch-until-int-cleared) 01481 * @see MPU6050_RA_INT_PIN_CFG 01482 * @see MPU6050_INTCFG_LATCH_INT_EN_BIT 01483 */ 01484 bool MPU6051::getInterruptLatch() 01485 { 01486 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_LATCH_INT_EN_BIT, buffer); 01487 return buffer[0]; 01488 } 01489 /** Set interrupt latch mode. 01490 * @param latch New latch mode (0=50us-pulse, 1=latch-until-int-cleared) 01491 * @see getInterruptLatch() 01492 * @see MPU6050_RA_INT_PIN_CFG 01493 * @see MPU6050_INTCFG_LATCH_INT_EN_BIT 01494 */ 01495 void MPU6051::setInterruptLatch(bool latch) 01496 { 01497 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_LATCH_INT_EN_BIT, latch); 01498 } 01499 /** Get interrupt latch clear mode. 01500 * Will be set 0 for status-read-only, 1 for any-register-read. 01501 * @return Current latch clear mode (0=status-read-only, 1=any-register-read) 01502 * @see MPU6050_RA_INT_PIN_CFG 01503 * @see MPU6050_INTCFG_INT_RD_CLEAR_BIT 01504 */ 01505 bool MPU6051::getInterruptLatchClear() 01506 { 01507 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_RD_CLEAR_BIT, buffer); 01508 return buffer[0]; 01509 } 01510 /** Set interrupt latch clear mode. 01511 * @param clear New latch clear mode (0=status-read-only, 1=any-register-read) 01512 * @see getInterruptLatchClear() 01513 * @see MPU6050_RA_INT_PIN_CFG 01514 * @see MPU6050_INTCFG_INT_RD_CLEAR_BIT 01515 */ 01516 void MPU6051::setInterruptLatchClear(bool clear) 01517 { 01518 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_RD_CLEAR_BIT, clear); 01519 } 01520 /** Get FSYNC interrupt logic level mode. 01521 * @return Current FSYNC interrupt mode (0=active-high, 1=active-low) 01522 * @see getFSyncInterruptMode() 01523 * @see MPU6050_RA_INT_PIN_CFG 01524 * @see MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT 01525 */ 01526 bool MPU6051::getFSyncInterruptLevel() 01527 { 01528 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT, buffer); 01529 return buffer[0]; 01530 } 01531 /** Set FSYNC interrupt logic level mode. 01532 * @param mode New FSYNC interrupt mode (0=active-high, 1=active-low) 01533 * @see getFSyncInterruptMode() 01534 * @see MPU6050_RA_INT_PIN_CFG 01535 * @see MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT 01536 */ 01537 void MPU6051::setFSyncInterruptLevel(bool level) 01538 { 01539 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT, level); 01540 } 01541 /** Get FSYNC pin interrupt enabled setting. 01542 * Will be set 0 for disabled, 1 for enabled. 01543 * @return Current interrupt enabled setting 01544 * @see MPU6050_RA_INT_PIN_CFG 01545 * @see MPU6050_INTCFG_FSYNC_INT_EN_BIT 01546 */ 01547 bool MPU6051::getFSyncInterruptEnabled() 01548 { 01549 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_EN_BIT, buffer); 01550 return buffer[0]; 01551 } 01552 /** Set FSYNC pin interrupt enabled setting. 01553 * @param enabled New FSYNC pin interrupt enabled setting 01554 * @see getFSyncInterruptEnabled() 01555 * @see MPU6050_RA_INT_PIN_CFG 01556 * @see MPU6050_INTCFG_FSYNC_INT_EN_BIT 01557 */ 01558 void MPU6051::setFSyncInterruptEnabled(bool enabled) 01559 { 01560 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_EN_BIT, enabled); 01561 } 01562 /** Get I2C bypass enabled status. 01563 * When this bit is equal to 1 and I2C_MST_EN (Register 106 bit[5]) is equal to 01564 * 0, the host application processor will be able to directly access the 01565 * auxiliary I2C bus of the MPU-60X0. When this bit is equal to 0, the host 01566 * application processor will not be able to directly access the auxiliary I2C 01567 * bus of the MPU-60X0 regardless of the state of I2C_MST_EN (Register 106 01568 * bit[5]). 01569 * @return Current I2C bypass enabled status 01570 * @see MPU6050_RA_INT_PIN_CFG 01571 * @see MPU6050_INTCFG_I2C_BYPASS_EN_BIT 01572 */ 01573 bool MPU6051::getI2CBypassEnabled() 01574 { 01575 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_I2C_BYPASS_EN_BIT, buffer); 01576 return buffer[0]; 01577 } 01578 /** Set I2C bypass enabled status. 01579 * When this bit is equal to 1 and I2C_MST_EN (Register 106 bit[5]) is equal to 01580 * 0, the host application processor will be able to directly access the 01581 * auxiliary I2C bus of the MPU-60X0. When this bit is equal to 0, the host 01582 * application processor will not be able to directly access the auxiliary I2C 01583 * bus of the MPU-60X0 regardless of the state of I2C_MST_EN (Register 106 01584 * bit[5]). 01585 * @param enabled New I2C bypass enabled status 01586 * @see MPU6050_RA_INT_PIN_CFG 01587 * @see MPU6050_INTCFG_I2C_BYPASS_EN_BIT 01588 */ 01589 void MPU6051::setI2CBypassEnabled(bool enabled) 01590 { 01591 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_I2C_BYPASS_EN_BIT, enabled); 01592 } 01593 /** Get reference clock output enabled status. 01594 * When this bit is equal to 1, a reference clock output is provided at the 01595 * CLKOUT pin. When this bit is equal to 0, the clock output is disabled. For 01596 * further information regarding CLKOUT, please refer to the MPU-60X0 Product 01597 * Specification document. 01598 * @return Current reference clock output enabled status 01599 * @see MPU6050_RA_INT_PIN_CFG 01600 * @see MPU6050_INTCFG_CLKOUT_EN_BIT 01601 */ 01602 bool MPU6051::getClockOutputEnabled() 01603 { 01604 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_CLKOUT_EN_BIT, buffer); 01605 return buffer[0]; 01606 } 01607 /** Set reference clock output enabled status. 01608 * When this bit is equal to 1, a reference clock output is provided at the 01609 * CLKOUT pin. When this bit is equal to 0, the clock output is disabled. For 01610 * further information regarding CLKOUT, please refer to the MPU-60X0 Product 01611 * Specification document. 01612 * @param enabled New reference clock output enabled status 01613 * @see MPU6050_RA_INT_PIN_CFG 01614 * @see MPU6050_INTCFG_CLKOUT_EN_BIT 01615 */ 01616 void MPU6051::setClockOutputEnabled(bool enabled) 01617 { 01618 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_CLKOUT_EN_BIT, enabled); 01619 } 01620 01621 // INT_ENABLE register 01622 01623 /** Get full interrupt enabled status. 01624 * Full register byte for all interrupts, for quick reading. Each bit will be 01625 * set 0 for disabled, 1 for enabled. 01626 * @return Current interrupt enabled status 01627 * @see MPU6050_RA_INT_ENABLE 01628 * @see MPU6050_INTERRUPT_FF_BIT 01629 **/ 01630 uint8_t MPU6051::getIntEnabled() 01631 { 01632 i2Cdev.readByte(devAddr, MPU6050_RA_INT_ENABLE, buffer); 01633 return buffer[0]; 01634 } 01635 /** Set full interrupt enabled status. 01636 * Full register byte for all interrupts, for quick reading. Each bit should be 01637 * set 0 for disabled, 1 for enabled. 01638 * @param enabled New interrupt enabled status 01639 * @see getIntFreefallEnabled() 01640 * @see MPU6050_RA_INT_ENABLE 01641 * @see MPU6050_INTERRUPT_FF_BIT 01642 **/ 01643 void MPU6051::setIntEnabled(uint8_t enabled) 01644 { 01645 i2Cdev.writeByte(devAddr, MPU6050_RA_INT_ENABLE, enabled); 01646 } 01647 /** Get Free Fall interrupt enabled status. 01648 * Will be set 0 for disabled, 1 for enabled. 01649 * @return Current interrupt enabled status 01650 * @see MPU6050_RA_INT_ENABLE 01651 * @see MPU6050_INTERRUPT_FF_BIT 01652 **/ 01653 bool MPU6051::getIntFreefallEnabled() 01654 { 01655 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FF_BIT, buffer); 01656 return buffer[0]; 01657 } 01658 /** Set Free Fall interrupt enabled status. 01659 * @param enabled New interrupt enabled status 01660 * @see getIntFreefallEnabled() 01661 * @see MPU6050_RA_INT_ENABLE 01662 * @see MPU6050_INTERRUPT_FF_BIT 01663 **/ 01664 void MPU6051::setIntFreefallEnabled(bool enabled) 01665 { 01666 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FF_BIT, enabled); 01667 } 01668 /** Get Motion Detection interrupt enabled status. 01669 * Will be set 0 for disabled, 1 for enabled. 01670 * @return Current interrupt enabled status 01671 * @see MPU6050_RA_INT_ENABLE 01672 * @see MPU6050_INTERRUPT_MOT_BIT 01673 **/ 01674 bool MPU6051::getIntMotionEnabled() 01675 { 01676 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_MOT_BIT, buffer); 01677 return buffer[0]; 01678 } 01679 /** Set Motion Detection interrupt enabled status. 01680 * @param enabled New interrupt enabled status 01681 * @see getIntMotionEnabled() 01682 * @see MPU6050_RA_INT_ENABLE 01683 * @see MPU6050_INTERRUPT_MOT_BIT 01684 **/ 01685 void MPU6051::setIntMotionEnabled(bool enabled) 01686 { 01687 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_MOT_BIT, enabled); 01688 } 01689 /** Get Zero Motion Detection interrupt enabled status. 01690 * Will be set 0 for disabled, 1 for enabled. 01691 * @return Current interrupt enabled status 01692 * @see MPU6050_RA_INT_ENABLE 01693 * @see MPU6050_INTERRUPT_ZMOT_BIT 01694 **/ 01695 bool MPU6051::getIntZeroMotionEnabled() 01696 { 01697 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_ZMOT_BIT, buffer); 01698 return buffer[0]; 01699 } 01700 /** Set Zero Motion Detection interrupt enabled status. 01701 * @param enabled New interrupt enabled status 01702 * @see getIntZeroMotionEnabled() 01703 * @see MPU6050_RA_INT_ENABLE 01704 * @see MPU6050_INTERRUPT_ZMOT_BIT 01705 **/ 01706 void MPU6051::setIntZeroMotionEnabled(bool enabled) 01707 { 01708 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_ZMOT_BIT, enabled); 01709 } 01710 /** Get FIFO Buffer Overflow interrupt enabled status. 01711 * Will be set 0 for disabled, 1 for enabled. 01712 * @return Current interrupt enabled status 01713 * @see MPU6050_RA_INT_ENABLE 01714 * @see MPU6050_INTERRUPT_FIFO_OFLOW_BIT 01715 **/ 01716 bool MPU6051::getIntFIFOBufferOverflowEnabled() 01717 { 01718 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FIFO_OFLOW_BIT, buffer); 01719 return buffer[0]; 01720 } 01721 /** Set FIFO Buffer Overflow interrupt enabled status. 01722 * @param enabled New interrupt enabled status 01723 * @see getIntFIFOBufferOverflowEnabled() 01724 * @see MPU6050_RA_INT_ENABLE 01725 * @see MPU6050_INTERRUPT_FIFO_OFLOW_BIT 01726 **/ 01727 void MPU6051::setIntFIFOBufferOverflowEnabled(bool enabled) 01728 { 01729 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FIFO_OFLOW_BIT, enabled); 01730 } 01731 /** Get I2C Master interrupt enabled status. 01732 * This enables any of the I2C Master interrupt sources to generate an 01733 * interrupt. Will be set 0 for disabled, 1 for enabled. 01734 * @return Current interrupt enabled status 01735 * @see MPU6050_RA_INT_ENABLE 01736 * @see MPU6050_INTERRUPT_I2C_MST_INT_BIT 01737 **/ 01738 bool MPU6051::getIntI2CMasterEnabled() 01739 { 01740 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_I2C_MST_INT_BIT, buffer); 01741 return buffer[0]; 01742 } 01743 /** Set I2C Master interrupt enabled status. 01744 * @param enabled New interrupt enabled status 01745 * @see getIntI2CMasterEnabled() 01746 * @see MPU6050_RA_INT_ENABLE 01747 * @see MPU6050_INTERRUPT_I2C_MST_INT_BIT 01748 **/ 01749 void MPU6051::setIntI2CMasterEnabled(bool enabled) 01750 { 01751 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_I2C_MST_INT_BIT, enabled); 01752 } 01753 /** Get Data Ready interrupt enabled setting. 01754 * This event occurs each time a write operation to all of the sensor registers 01755 * has been completed. Will be set 0 for disabled, 1 for enabled. 01756 * @return Current interrupt enabled status 01757 * @see MPU6050_RA_INT_ENABLE 01758 * @see MPU6050_INTERRUPT_DATA_RDY_BIT 01759 */ 01760 bool MPU6051::getIntDataReadyEnabled() 01761 { 01762 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_DATA_RDY_BIT, buffer); 01763 return buffer[0]; 01764 } 01765 /** Set Data Ready interrupt enabled status. 01766 * @param enabled New interrupt enabled status 01767 * @see getIntDataReadyEnabled() 01768 * @see MPU6050_RA_INT_CFG 01769 * @see MPU6050_INTERRUPT_DATA_RDY_BIT 01770 */ 01771 void MPU6051::setIntDataReadyEnabled(bool enabled) 01772 { 01773 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_DATA_RDY_BIT, enabled); 01774 } 01775 01776 // INT_STATUS register 01777 01778 /** Get full set of interrupt status bits. 01779 * These bits clear to 0 after the register has been read. Very useful 01780 * for getting multiple INT statuses, since each single bit read clears 01781 * all of them because it has to read the whole byte. 01782 * @return Current interrupt status 01783 * @see MPU6050_RA_INT_STATUS 01784 */ 01785 uint8_t MPU6051::getIntStatus() 01786 { 01787 i2Cdev.readByte(devAddr, MPU6050_RA_INT_STATUS, buffer); 01788 return buffer[0]; 01789 } 01790 /** Get Free Fall interrupt status. 01791 * This bit automatically sets to 1 when a Free Fall interrupt has been 01792 * generated. The bit clears to 0 after the register has been read. 01793 * @return Current interrupt status 01794 * @see MPU6050_RA_INT_STATUS 01795 * @see MPU6050_INTERRUPT_FF_BIT 01796 */ 01797 bool MPU6051::getIntFreefallStatus() 01798 { 01799 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_FF_BIT, buffer); 01800 return buffer[0]; 01801 } 01802 /** Get Motion Detection interrupt status. 01803 * This bit automatically sets to 1 when a Motion Detection interrupt has been 01804 * generated. The bit clears to 0 after the register has been read. 01805 * @return Current interrupt status 01806 * @see MPU6050_RA_INT_STATUS 01807 * @see MPU6050_INTERRUPT_MOT_BIT 01808 */ 01809 bool MPU6051::getIntMotionStatus() 01810 { 01811 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_MOT_BIT, buffer); 01812 return buffer[0]; 01813 } 01814 /** Get Zero Motion Detection interrupt status. 01815 * This bit automatically sets to 1 when a Zero Motion Detection interrupt has 01816 * been generated. The bit clears to 0 after the register has been read. 01817 * @return Current interrupt status 01818 * @see MPU6050_RA_INT_STATUS 01819 * @see MPU6050_INTERRUPT_ZMOT_BIT 01820 */ 01821 bool MPU6051::getIntZeroMotionStatus() 01822 { 01823 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_ZMOT_BIT, buffer); 01824 return buffer[0]; 01825 } 01826 /** Get FIFO Buffer Overflow interrupt status. 01827 * This bit automatically sets to 1 when a Free Fall interrupt has been 01828 * generated. The bit clears to 0 after the register has been read. 01829 * @return Current interrupt status 01830 * @see MPU6050_RA_INT_STATUS 01831 * @see MPU6050_INTERRUPT_FIFO_OFLOW_BIT 01832 */ 01833 bool MPU6051::getIntFIFOBufferOverflowStatus() 01834 { 01835 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_FIFO_OFLOW_BIT, buffer); 01836 return buffer[0]; 01837 } 01838 /** Get I2C Master interrupt status. 01839 * This bit automatically sets to 1 when an I2C Master interrupt has been 01840 * generated. For a list of I2C Master interrupts, please refer to Register 54. 01841 * The bit clears to 0 after the register has been read. 01842 * @return Current interrupt status 01843 * @see MPU6050_RA_INT_STATUS 01844 * @see MPU6050_INTERRUPT_I2C_MST_INT_BIT 01845 */ 01846 bool MPU6051::getIntI2CMasterStatus() 01847 { 01848 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_I2C_MST_INT_BIT, buffer); 01849 return buffer[0]; 01850 } 01851 /** Get Data Ready interrupt status. 01852 * This bit automatically sets to 1 when a Data Ready interrupt has been 01853 * generated. The bit clears to 0 after the register has been read. 01854 * @return Current interrupt status 01855 * @see MPU6050_RA_INT_STATUS 01856 * @see MPU6050_INTERRUPT_DATA_RDY_BIT 01857 */ 01858 bool MPU6051::getIntDataReadyStatus() 01859 { 01860 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_DATA_RDY_BIT, buffer); 01861 return buffer[0]; 01862 } 01863 01864 // ACCEL_*OUT_* registers 01865 01866 /** Get raw 9-axis motion sensor readings (accel/gyro/compass). 01867 * FUNCTION NOT FULLY IMPLEMENTED YET. 01868 * @param ax 16-bit signed integer container for accelerometer X-axis value 01869 * @param ay 16-bit signed integer container for accelerometer Y-axis value 01870 * @param az 16-bit signed integer container for accelerometer Z-axis value 01871 * @param gx 16-bit signed integer container for gyroscope X-axis value 01872 * @param gy 16-bit signed integer container for gyroscope Y-axis value 01873 * @param gz 16-bit signed integer container for gyroscope Z-axis value 01874 * @param mx 16-bit signed integer container for magnetometer X-axis value 01875 * @param my 16-bit signed integer container for magnetometer Y-axis value 01876 * @param mz 16-bit signed integer container for magnetometer Z-axis value 01877 * @see getMotion6() 01878 * @see getAcceleration() 01879 * @see getRotation() 01880 * @see MPU6050_RA_ACCEL_XOUT_H 01881 */ 01882 void MPU6051::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) 01883 { 01884 getMotion6(ax, ay, az, gx, gy, gz); 01885 01886 // magnetometer reading 01887 i2Cdev.writeByte(devAddr, MPU6050_RA_INT_PIN_CFG, 0x02); //set i2c bypass enable pin to true to access magnetometer 01888 wait_ms(10); // necessary wait >=6ms 01889 i2Cdev.writeByte(MPU9150_RA_MAG_ADDRESS, 0x0A, 0x01); // enable the magnetometer 01890 wait_ms(10); // necessary wait >=6ms 01891 i2Cdev.readBytes(MPU9150_RA_MAG_ADDRESS, MPU9150_RA_MAG_XOUT_L, 6, buffer); 01892 *mx = (((int16_t)buffer[0]) << 8) | buffer[1]; 01893 *my = (((int16_t)buffer[2]) << 8) | buffer[3]; 01894 *mz = (((int16_t)buffer[4]) << 8) | buffer[5]; 01895 } 01896 /** Get raw 6-axis motion sensor readings (accel/gyro). 01897 * Retrieves all currently available motion sensor values. 01898 * @param ax 16-bit signed integer container for accelerometer X-axis value 01899 * @param ay 16-bit signed integer container for accelerometer Y-axis value 01900 * @param az 16-bit signed integer container for accelerometer Z-axis value 01901 * @param gx 16-bit signed integer container for gyroscope X-axis value 01902 * @param gy 16-bit signed integer container for gyroscope Y-axis value 01903 * @param gz 16-bit signed integer container for gyroscope Z-axis value 01904 * @see getAcceleration() 01905 * @see getRotation() 01906 * @see MPU6050_RA_ACCEL_XOUT_H 01907 */ 01908 void MPU6051::getMotion6(int16_t* ax, int16_t* ay, int16_t* az, int16_t* gx, int16_t* gy, int16_t* gz) 01909 { 01910 i2Cdev.readBytes(devAddr, MPU6050_RA_ACCEL_XOUT_H, 14, buffer); 01911 *ax = (((int16_t)buffer[0]) << 8) | buffer[1]; 01912 *ay = (((int16_t)buffer[2]) << 8) | buffer[3]; 01913 *az = (((int16_t)buffer[4]) << 8) | buffer[5]; 01914 *gx = (((int16_t)buffer[8]) << 8) | buffer[9]; 01915 *gy = (((int16_t)buffer[10]) << 8) | buffer[11]; 01916 *gz = (((int16_t)buffer[12]) << 8) | buffer[13]; 01917 } 01918 /** Get 3-axis accelerometer readings. 01919 * These registers store the most recent accelerometer measurements. 01920 * Accelerometer measurements are written to these registers at the Sample Rate 01921 * as defined in Register 25. 01922 * 01923 * The accelerometer measurement registers, along with the temperature 01924 * measurement registers, gyroscope measurement registers, and external sensor 01925 * data registers, are composed of two sets of registers: an internal register 01926 * set and a user-facing read register set. 01927 * 01928 * The data within the accelerometer sensors' internal register set is always 01929 * updated at the Sample Rate. Meanwhile, the user-facing read register set 01930 * duplicates the internal register set's data values whenever the serial 01931 * interface is idle. This guarantees that a burst read of sensor registers will 01932 * read measurements from the same sampling instant. Note that if burst reads 01933 * are not used, the user is responsible for ensuring a set of single byte reads 01934 * correspond to a single sampling instant by checking the Data Ready interrupt. 01935 * 01936 * Each 16-bit accelerometer measurement has a full scale defined in ACCEL_FS 01937 * (Register 28). For each full scale setting, the accelerometers' sensitivity 01938 * per LSB in ACCEL_xOUT is shown in the table below: 01939 * 01940 * <pre> 01941 * AFS_SEL | Full Scale Range | LSB Sensitivity 01942 * --------+------------------+---------------- 01943 * 0 | +/- 2g | 8192 LSB/mg 01944 * 1 | +/- 4g | 4096 LSB/mg 01945 * 2 | +/- 8g | 2048 LSB/mg 01946 * 3 | +/- 16g | 1024 LSB/mg 01947 * </pre> 01948 * 01949 * @param x 16-bit signed integer container for X-axis acceleration 01950 * @param y 16-bit signed integer container for Y-axis acceleration 01951 * @param z 16-bit signed integer container for Z-axis acceleration 01952 * @see MPU6050_RA_GYRO_XOUT_H 01953 */ 01954 void MPU6051::getAcceleration(int16_t* x, int16_t* y, int16_t* z) 01955 { 01956 i2Cdev.readBytes(devAddr, MPU6050_RA_ACCEL_XOUT_H, 6, buffer); 01957 *x = (((int16_t)buffer[0]) << 8) | buffer[1]; 01958 *y = (((int16_t)buffer[2]) << 8) | buffer[3]; 01959 *z = (((int16_t)buffer[4]) << 8) | buffer[5]; 01960 } 01961 /** Get X-axis accelerometer reading. 01962 * @return X-axis acceleration measurement in 16-bit 2's complement format 01963 * @see getMotion6() 01964 * @see MPU6050_RA_ACCEL_XOUT_H 01965 */ 01966 int16_t MPU6051::getAccelerationX() 01967 { 01968 i2Cdev.readBytes(devAddr, MPU6050_RA_ACCEL_XOUT_H, 2, buffer); 01969 return (((int16_t)buffer[0]) << 8) | buffer[1]; 01970 } 01971 /** Get Y-axis accelerometer reading. 01972 * @return Y-axis acceleration measurement in 16-bit 2's complement format 01973 * @see getMotion6() 01974 * @see MPU6050_RA_ACCEL_YOUT_H 01975 */ 01976 int16_t MPU6051::getAccelerationY() 01977 { 01978 i2Cdev.readBytes(devAddr, MPU6050_RA_ACCEL_YOUT_H, 2, buffer); 01979 return (((int16_t)buffer[0]) << 8) | buffer[1]; 01980 } 01981 /** Get Z-axis accelerometer reading. 01982 * @return Z-axis acceleration measurement in 16-bit 2's complement format 01983 * @see getMotion6() 01984 * @see MPU6050_RA_ACCEL_ZOUT_H 01985 */ 01986 int16_t MPU6051::getAccelerationZ() 01987 { 01988 i2Cdev.readBytes(devAddr, MPU6050_RA_ACCEL_ZOUT_H, 2, buffer); 01989 return (((int16_t)buffer[0]) << 8) | buffer[1]; 01990 } 01991 01992 // TEMP_OUT_* registers 01993 01994 /** Get current internal temperature. 01995 * @return Temperature reading in 16-bit 2's complement format 01996 * @see MPU6050_RA_TEMP_OUT_H 01997 */ 01998 int16_t MPU6051::getTemperature() 01999 { 02000 i2Cdev.readBytes(devAddr, MPU6050_RA_TEMP_OUT_H, 2, buffer); 02001 return (((int16_t)buffer[0]) << 8) | buffer[1]; 02002 } 02003 02004 // GYRO_*OUT_* registers 02005 02006 /** Get 3-axis gyroscope readings. 02007 * These gyroscope measurement registers, along with the accelerometer 02008 * measurement registers, temperature measurement registers, and external sensor 02009 * data registers, are composed of two sets of registers: an internal register 02010 * set and a user-facing read register set. 02011 * The data within the gyroscope sensors' internal register set is always 02012 * updated at the Sample Rate. Meanwhile, the user-facing read register set 02013 * duplicates the internal register set's data values whenever the serial 02014 * interface is idle. This guarantees that a burst read of sensor registers will 02015 * read measurements from the same sampling instant. Note that if burst reads 02016 * are not used, the user is responsible for ensuring a set of single byte reads 02017 * correspond to a single sampling instant by checking the Data Ready interrupt. 02018 * 02019 * Each 16-bit gyroscope measurement has a full scale defined in FS_SEL 02020 * (Register 27). For each full scale setting, the gyroscopes' sensitivity per 02021 * LSB in GYRO_xOUT is shown in the table below: 02022 * 02023 * <pre> 02024 * FS_SEL | Full Scale Range | LSB Sensitivity 02025 * -------+--------------------+---------------- 02026 * 0 | +/- 250 degrees/s | 131 LSB/deg/s 02027 * 1 | +/- 500 degrees/s | 65.5 LSB/deg/s 02028 * 2 | +/- 1000 degrees/s | 32.8 LSB/deg/s 02029 * 3 | +/- 2000 degrees/s | 16.4 LSB/deg/s 02030 * </pre> 02031 * 02032 * @param x 16-bit signed integer container for X-axis rotation 02033 * @param y 16-bit signed integer container for Y-axis rotation 02034 * @param z 16-bit signed integer container for Z-axis rotation 02035 * @see getMotion6() 02036 * @see MPU6050_RA_GYRO_XOUT_H 02037 */ 02038 void MPU6051::getRotation(int16_t* x, int16_t* y, int16_t* z) 02039 { 02040 i2Cdev.readBytes(devAddr, MPU6050_RA_GYRO_XOUT_H, 6, buffer); 02041 *x = (((int16_t)buffer[0]) << 8) | buffer[1]; 02042 *y = (((int16_t)buffer[2]) << 8) | buffer[3]; 02043 *z = (((int16_t)buffer[4]) << 8) | buffer[5]; 02044 } 02045 /** Get X-axis gyroscope reading. 02046 * @return X-axis rotation measurement in 16-bit 2's complement format 02047 * @see getMotion6() 02048 * @see MPU6050_RA_GYRO_XOUT_H 02049 */ 02050 int16_t MPU6051::getRotationX() 02051 { 02052 i2Cdev.readBytes(devAddr, MPU6050_RA_GYRO_XOUT_H, 2, buffer); 02053 return (((int16_t)buffer[0]) << 8) | buffer[1]; 02054 } 02055 /** Get Y-axis gyroscope reading. 02056 * @return Y-axis rotation measurement in 16-bit 2's complement format 02057 * @see getMotion6() 02058 * @see MPU6050_RA_GYRO_YOUT_H 02059 */ 02060 int16_t MPU6051::getRotationY() 02061 { 02062 i2Cdev.readBytes(devAddr, MPU6050_RA_GYRO_YOUT_H, 2, buffer); 02063 return (((int16_t)buffer[0]) << 8) | buffer[1]; 02064 } 02065 /** Get Z-axis gyroscope reading. 02066 * @return Z-axis rotation measurement in 16-bit 2's complement format 02067 * @see getMotion6() 02068 * @see MPU6050_RA_GYRO_ZOUT_H 02069 */ 02070 int16_t MPU6051::getRotationZ() 02071 { 02072 i2Cdev.readBytes(devAddr, MPU6050_RA_GYRO_ZOUT_H, 2, buffer); 02073 return (((int16_t)buffer[0]) << 8) | buffer[1]; 02074 } 02075 02076 // EXT_SENS_DATA_* registers 02077 02078 /** Read single byte from external sensor data register. 02079 * These registers store data read from external sensors by the Slave 0, 1, 2, 02080 * and 3 on the auxiliary I2C interface. Data read by Slave 4 is stored in 02081 * I2C_SLV4_DI (Register 53). 02082 * 02083 * External sensor data is written to these registers at the Sample Rate as 02084 * defined in Register 25. This access rate can be reduced by using the Slave 02085 * Delay Enable registers (Register 103). 02086 * 02087 * External sensor data registers, along with the gyroscope measurement 02088 * registers, accelerometer measurement registers, and temperature measurement 02089 * registers, are composed of two sets of registers: an internal register set 02090 * and a user-facing read register set. 02091 * 02092 * The data within the external sensors' internal register set is always updated 02093 * at the Sample Rate (or the reduced access rate) whenever the serial interface 02094 * is idle. This guarantees that a burst read of sensor registers will read 02095 * measurements from the same sampling instant. Note that if burst reads are not 02096 * used, the user is responsible for ensuring a set of single byte reads 02097 * correspond to a single sampling instant by checking the Data Ready interrupt. 02098 * 02099 * Data is placed in these external sensor data registers according to 02100 * I2C_SLV0_CTRL, I2C_SLV1_CTRL, I2C_SLV2_CTRL, and I2C_SLV3_CTRL (Registers 39, 02101 * 42, 45, and 48). When more than zero bytes are read (I2C_SLVx_LEN > 0) from 02102 * an enabled slave (I2C_SLVx_EN = 1), the slave is read at the Sample Rate (as 02103 * defined in Register 25) or delayed rate (if specified in Register 52 and 02104 * 103). During each Sample cycle, slave reads are performed in order of Slave 02105 * number. If all slaves are enabled with more than zero bytes to be read, the 02106 * order will be Slave 0, followed by Slave 1, Slave 2, and Slave 3. 02107 * 02108 * Each enabled slave will have EXT_SENS_DATA registers associated with it by 02109 * number of bytes read (I2C_SLVx_LEN) in order of slave number, starting from 02110 * EXT_SENS_DATA_00. Note that this means enabling or disabling a slave may 02111 * change the higher numbered slaves' associated registers. Furthermore, if 02112 * fewer total bytes are being read from the external sensors as a result of 02113 * such a change, then the data remaining in the registers which no longer have 02114 * an associated slave device (i.e. high numbered registers) will remain in 02115 * these previously allocated registers unless reset. 02116 * 02117 * If the sum of the read lengths of all SLVx transactions exceed the number of 02118 * available EXT_SENS_DATA registers, the excess bytes will be dropped. There 02119 * are 24 EXT_SENS_DATA registers and hence the total read lengths between all 02120 * the slaves cannot be greater than 24 or some bytes will be lost. 02121 * 02122 * Note: Slave 4's behavior is distinct from that of Slaves 0-3. For further 02123 * information regarding the characteristics of Slave 4, please refer to 02124 * Registers 49 to 53. 02125 * 02126 * EXAMPLE: 02127 * Suppose that Slave 0 is enabled with 4 bytes to be read (I2C_SLV0_EN = 1 and 02128 * I2C_SLV0_LEN = 4) while Slave 1 is enabled with 2 bytes to be read so that 02129 * I2C_SLV1_EN = 1 and I2C_SLV1_LEN = 2. In such a situation, EXT_SENS_DATA _00 02130 * through _03 will be associated with Slave 0, while EXT_SENS_DATA _04 and 05 02131 * will be associated with Slave 1. If Slave 2 is enabled as well, registers 02132 * starting from EXT_SENS_DATA_06 will be allocated to Slave 2. 02133 * 02134 * If Slave 2 is disabled while Slave 3 is enabled in this same situation, then 02135 * registers starting from EXT_SENS_DATA_06 will be allocated to Slave 3 02136 * instead. 02137 * 02138 * REGISTER ALLOCATION FOR DYNAMIC DISABLE VS. NORMAL DISABLE: 02139 * If a slave is disabled at any time, the space initially allocated to the 02140 * slave in the EXT_SENS_DATA register, will remain associated with that slave. 02141 * This is to avoid dynamic adjustment of the register allocation. 02142 * 02143 * The allocation of the EXT_SENS_DATA registers is recomputed only when (1) all 02144 * slaves are disabled, or (2) the I2C_MST_RST bit is set (Register 106). 02145 * 02146 * This above is also true if one of the slaves gets NACKed and stops 02147 * functioning. 02148 * 02149 * @param position Starting position (0-23) 02150 * @return Byte read from register 02151 */ 02152 uint8_t MPU6051::getExternalSensorByte(int position) 02153 { 02154 i2Cdev.readByte(devAddr, MPU6050_RA_EXT_SENS_DATA_00 + position, buffer); 02155 return buffer[0]; 02156 } 02157 /** Read word (2 bytes) from external sensor data registers. 02158 * @param position Starting position (0-21) 02159 * @return Word read from register 02160 * @see getExternalSensorByte() 02161 */ 02162 uint16_t MPU6051::getExternalSensorWord(int position) 02163 { 02164 i2Cdev.readBytes(devAddr, MPU6050_RA_EXT_SENS_DATA_00 + position, 2, buffer); 02165 return (((uint16_t)buffer[0]) << 8) | buffer[1]; 02166 } 02167 /** Read double word (4 bytes) from external sensor data registers. 02168 * @param position Starting position (0-20) 02169 * @return Double word read from registers 02170 * @see getExternalSensorByte() 02171 */ 02172 uint32_t MPU6051::getExternalSensorDWord(int position) 02173 { 02174 i2Cdev.readBytes(devAddr, MPU6050_RA_EXT_SENS_DATA_00 + position, 4, buffer); 02175 return (((uint32_t)buffer[0]) << 24) | (((uint32_t)buffer[1]) << 16) | (((uint16_t)buffer[2]) << 8) | buffer[3]; 02176 } 02177 02178 // MOT_DETECT_STATUS register 02179 02180 /** Get X-axis negative motion detection interrupt status. 02181 * @return Motion detection status 02182 * @see MPU6050_RA_MOT_DETECT_STATUS 02183 * @see MPU6050_MOTION_MOT_XNEG_BIT 02184 */ 02185 bool MPU6051::getXNegMotionDetected() 02186 { 02187 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_XNEG_BIT, buffer); 02188 return buffer[0]; 02189 } 02190 /** Get X-axis positive motion detection interrupt status. 02191 * @return Motion detection status 02192 * @see MPU6050_RA_MOT_DETECT_STATUS 02193 * @see MPU6050_MOTION_MOT_XPOS_BIT 02194 */ 02195 bool MPU6051::getXPosMotionDetected() 02196 { 02197 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_XPOS_BIT, buffer); 02198 return buffer[0]; 02199 } 02200 /** Get Y-axis negative motion detection interrupt status. 02201 * @return Motion detection status 02202 * @see MPU6050_RA_MOT_DETECT_STATUS 02203 * @see MPU6050_MOTION_MOT_YNEG_BIT 02204 */ 02205 bool MPU6051::getYNegMotionDetected() 02206 { 02207 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_YNEG_BIT, buffer); 02208 return buffer[0]; 02209 } 02210 /** Get Y-axis positive motion detection interrupt status. 02211 * @return Motion detection status 02212 * @see MPU6050_RA_MOT_DETECT_STATUS 02213 * @see MPU6050_MOTION_MOT_YPOS_BIT 02214 */ 02215 bool MPU6051::getYPosMotionDetected() 02216 { 02217 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_YPOS_BIT, buffer); 02218 return buffer[0]; 02219 } 02220 /** Get Z-axis negative motion detection interrupt status. 02221 * @return Motion detection status 02222 * @see MPU6050_RA_MOT_DETECT_STATUS 02223 * @see MPU6050_MOTION_MOT_ZNEG_BIT 02224 */ 02225 bool MPU6051::getZNegMotionDetected() 02226 { 02227 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_ZNEG_BIT, buffer); 02228 return buffer[0]; 02229 } 02230 /** Get Z-axis positive motion detection interrupt status. 02231 * @return Motion detection status 02232 * @see MPU6050_RA_MOT_DETECT_STATUS 02233 * @see MPU6050_MOTION_MOT_ZPOS_BIT 02234 */ 02235 bool MPU6051::getZPosMotionDetected() 02236 { 02237 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_ZPOS_BIT, buffer); 02238 return buffer[0]; 02239 } 02240 /** Get zero motion detection interrupt status. 02241 * @return Motion detection status 02242 * @see MPU6050_RA_MOT_DETECT_STATUS 02243 * @see MPU6050_MOTION_MOT_ZRMOT_BIT 02244 */ 02245 bool MPU6051::getZeroMotionDetected() 02246 { 02247 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_ZRMOT_BIT, buffer); 02248 return buffer[0]; 02249 } 02250 02251 // I2C_SLV*_DO register 02252 02253 /** Write byte to Data Output container for specified slave. 02254 * This register holds the output data written into Slave when Slave is set to 02255 * write mode. For further information regarding Slave control, please 02256 * refer to Registers 37 to 39 and immediately following. 02257 * @param num Slave number (0-3) 02258 * @param data Byte to write 02259 * @see MPU6050_RA_I2C_SLV0_DO 02260 */ 02261 void MPU6051::setSlaveOutputByte(uint8_t num, uint8_t data) 02262 { 02263 if (num > 3) return; 02264 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV0_DO + num, data); 02265 } 02266 02267 // I2C_MST_DELAY_CTRL register 02268 02269 /** Get external data shadow delay enabled status. 02270 * This register is used to specify the timing of external sensor data 02271 * shadowing. When DELAY_ES_SHADOW is set to 1, shadowing of external 02272 * sensor data is delayed until all data has been received. 02273 * @return Current external data shadow delay enabled status. 02274 * @see MPU6050_RA_I2C_MST_DELAY_CTRL 02275 * @see MPU6050_DELAYCTRL_DELAY_ES_SHADOW_BIT 02276 */ 02277 bool MPU6051::getExternalShadowDelayEnabled() 02278 { 02279 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_DELAY_CTRL, MPU6050_DELAYCTRL_DELAY_ES_SHADOW_BIT, buffer); 02280 return buffer[0]; 02281 } 02282 /** Set external data shadow delay enabled status. 02283 * @param enabled New external data shadow delay enabled status. 02284 * @see getExternalShadowDelayEnabled() 02285 * @see MPU6050_RA_I2C_MST_DELAY_CTRL 02286 * @see MPU6050_DELAYCTRL_DELAY_ES_SHADOW_BIT 02287 */ 02288 void MPU6051::setExternalShadowDelayEnabled(bool enabled) 02289 { 02290 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_DELAY_CTRL, MPU6050_DELAYCTRL_DELAY_ES_SHADOW_BIT, enabled); 02291 } 02292 /** Get slave delay enabled status. 02293 * When a particular slave delay is enabled, the rate of access for the that 02294 * slave device is reduced. When a slave's access rate is decreased relative to 02295 * the Sample Rate, the slave is accessed every: 02296 * 02297 * 1 / (1 + I2C_MST_DLY) Samples 02298 * 02299 * This base Sample Rate in turn is determined by SMPLRT_DIV (register * 25) 02300 * and DLPF_CFG (register 26). 02301 * 02302 * For further information regarding I2C_MST_DLY, please refer to register 52. 02303 * For further information regarding the Sample Rate, please refer to register 25. 02304 * 02305 * @param num Slave number (0-4) 02306 * @return Current slave delay enabled status. 02307 * @see MPU6050_RA_I2C_MST_DELAY_CTRL 02308 * @see MPU6050_DELAYCTRL_I2C_SLV0_DLY_EN_BIT 02309 */ 02310 bool MPU6051::getSlaveDelayEnabled(uint8_t num) 02311 { 02312 // MPU6050_DELAYCTRL_I2C_SLV4_DLY_EN_BIT is 4, SLV3 is 3, etc. 02313 if (num > 4) return 0; 02314 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_DELAY_CTRL, num, buffer); 02315 return buffer[0]; 02316 } 02317 /** Set slave delay enabled status. 02318 * @param num Slave number (0-4) 02319 * @param enabled New slave delay enabled status. 02320 * @see MPU6050_RA_I2C_MST_DELAY_CTRL 02321 * @see MPU6050_DELAYCTRL_I2C_SLV0_DLY_EN_BIT 02322 */ 02323 void MPU6051::setSlaveDelayEnabled(uint8_t num, bool enabled) 02324 { 02325 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_DELAY_CTRL, num, enabled); 02326 } 02327 02328 // SIGNAL_PATH_RESET register 02329 02330 /** Reset gyroscope signal path. 02331 * The reset will revert the signal path analog to digital converters and 02332 * filters to their power up configurations. 02333 * @see MPU6050_RA_SIGNAL_PATH_RESET 02334 * @see MPU6050_PATHRESET_GYRO_RESET_BIT 02335 */ 02336 void MPU6051::resetGyroscopePath() 02337 { 02338 i2Cdev.writeBit(devAddr, MPU6050_RA_SIGNAL_PATH_RESET, MPU6050_PATHRESET_GYRO_RESET_BIT, true); 02339 } 02340 /** Reset accelerometer signal path. 02341 * The reset will revert the signal path analog to digital converters and 02342 * filters to their power up configurations. 02343 * @see MPU6050_RA_SIGNAL_PATH_RESET 02344 * @see MPU6050_PATHRESET_ACCEL_RESET_BIT 02345 */ 02346 void MPU6051::resetAccelerometerPath() 02347 { 02348 i2Cdev.writeBit(devAddr, MPU6050_RA_SIGNAL_PATH_RESET, MPU6050_PATHRESET_ACCEL_RESET_BIT, true); 02349 } 02350 /** Reset temperature sensor signal path. 02351 * The reset will revert the signal path analog to digital converters and 02352 * filters to their power up configurations. 02353 * @see MPU6050_RA_SIGNAL_PATH_RESET 02354 * @see MPU6050_PATHRESET_TEMP_RESET_BIT 02355 */ 02356 void MPU6051::resetTemperaturePath() 02357 { 02358 i2Cdev.writeBit(devAddr, MPU6050_RA_SIGNAL_PATH_RESET, MPU6050_PATHRESET_TEMP_RESET_BIT, true); 02359 } 02360 02361 // MOT_DETECT_CTRL register 02362 02363 /** Get accelerometer power-on delay. 02364 * The accelerometer data path provides samples to the sensor registers, Motion 02365 * detection, Zero Motion detection, and Free Fall detection modules. The 02366 * signal path contains filters which must be flushed on wake-up with new 02367 * samples before the detection modules begin operations. The default wake-up 02368 * delay, of 4ms can be lengthened by up to 3ms. This additional delay is 02369 * specified in ACCEL_ON_DELAY in units of 1 LSB = 1 ms. The user may select 02370 * any value above zero unless instructed otherwise by InvenSense. Please refer 02371 * to Section 8 of the MPU-6000/MPU-6050 Product Specification document for 02372 * further information regarding the detection modules. 02373 * @return Current accelerometer power-on delay 02374 * @see MPU6050_RA_MOT_DETECT_CTRL 02375 * @see MPU6050_DETECT_ACCEL_ON_DELAY_BIT 02376 */ 02377 uint8_t MPU6051::getAccelerometerPowerOnDelay() 02378 { 02379 i2Cdev.readBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_ACCEL_ON_DELAY_BIT, MPU6050_DETECT_ACCEL_ON_DELAY_LENGTH, buffer); 02380 return buffer[0]; 02381 } 02382 /** Set accelerometer power-on delay. 02383 * @param delay New accelerometer power-on delay (0-3) 02384 * @see getAccelerometerPowerOnDelay() 02385 * @see MPU6050_RA_MOT_DETECT_CTRL 02386 * @see MPU6050_DETECT_ACCEL_ON_DELAY_BIT 02387 */ 02388 void MPU6051::setAccelerometerPowerOnDelay(uint8_t delay) 02389 { 02390 i2Cdev.writeBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_ACCEL_ON_DELAY_BIT, MPU6050_DETECT_ACCEL_ON_DELAY_LENGTH, delay); 02391 } 02392 /** Get Free Fall detection counter decrement configuration. 02393 * Detection is registered by the Free Fall detection module after accelerometer 02394 * measurements meet their respective threshold conditions over a specified 02395 * number of samples. When the threshold conditions are met, the corresponding 02396 * detection counter increments by 1. The user may control the rate at which the 02397 * detection counter decrements when the threshold condition is not met by 02398 * configuring FF_COUNT. The decrement rate can be set according to the 02399 * following table: 02400 * 02401 * <pre> 02402 * FF_COUNT | Counter Decrement 02403 * ---------+------------------ 02404 * 0 | Reset 02405 * 1 | 1 02406 * 2 | 2 02407 * 3 | 4 02408 * </pre> 02409 * 02410 * When FF_COUNT is configured to 0 (reset), any non-qualifying sample will 02411 * reset the counter to 0. For further information on Free Fall detection, 02412 * please refer to Registers 29 to 32. 02413 * 02414 * @return Current decrement configuration 02415 * @see MPU6050_RA_MOT_DETECT_CTRL 02416 * @see MPU6050_DETECT_FF_COUNT_BIT 02417 */ 02418 uint8_t MPU6051::getFreefallDetectionCounterDecrement() 02419 { 02420 i2Cdev.readBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_FF_COUNT_BIT, MPU6050_DETECT_FF_COUNT_LENGTH, buffer); 02421 return buffer[0]; 02422 } 02423 /** Set Free Fall detection counter decrement configuration. 02424 * @param decrement New decrement configuration value 02425 * @see getFreefallDetectionCounterDecrement() 02426 * @see MPU6050_RA_MOT_DETECT_CTRL 02427 * @see MPU6050_DETECT_FF_COUNT_BIT 02428 */ 02429 void MPU6051::setFreefallDetectionCounterDecrement(uint8_t decrement) 02430 { 02431 i2Cdev.writeBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_FF_COUNT_BIT, MPU6050_DETECT_FF_COUNT_LENGTH, decrement); 02432 } 02433 /** Get Motion detection counter decrement configuration. 02434 * Detection is registered by the Motion detection module after accelerometer 02435 * measurements meet their respective threshold conditions over a specified 02436 * number of samples. When the threshold conditions are met, the corresponding 02437 * detection counter increments by 1. The user may control the rate at which the 02438 * detection counter decrements when the threshold condition is not met by 02439 * configuring MOT_COUNT. The decrement rate can be set according to the 02440 * following table: 02441 * 02442 * <pre> 02443 * MOT_COUNT | Counter Decrement 02444 * ----------+------------------ 02445 * 0 | Reset 02446 * 1 | 1 02447 * 2 | 2 02448 * 3 | 4 02449 * </pre> 02450 * 02451 * When MOT_COUNT is configured to 0 (reset), any non-qualifying sample will 02452 * reset the counter to 0. For further information on Motion detection, 02453 * please refer to Registers 29 to 32. 02454 * 02455 */ 02456 uint8_t MPU6051::getMotionDetectionCounterDecrement() 02457 { 02458 i2Cdev.readBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_MOT_COUNT_BIT, MPU6050_DETECT_MOT_COUNT_LENGTH, buffer); 02459 return buffer[0]; 02460 } 02461 /** Set Motion detection counter decrement configuration. 02462 * @param decrement New decrement configuration value 02463 * @see getMotionDetectionCounterDecrement() 02464 * @see MPU6050_RA_MOT_DETECT_CTRL 02465 * @see MPU6050_DETECT_MOT_COUNT_BIT 02466 */ 02467 void MPU6051::setMotionDetectionCounterDecrement(uint8_t decrement) 02468 { 02469 i2Cdev.writeBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_MOT_COUNT_BIT, MPU6050_DETECT_MOT_COUNT_LENGTH, decrement); 02470 } 02471 02472 // USER_CTRL register 02473 02474 /** Get FIFO enabled status. 02475 * When this bit is set to 0, the FIFO buffer is disabled. The FIFO buffer 02476 * cannot be written to or read from while disabled. The FIFO buffer's state 02477 * does not change unless the MPU-60X0 is power cycled. 02478 * @return Current FIFO enabled status 02479 * @see MPU6050_RA_USER_CTRL 02480 * @see MPU6050_USERCTRL_FIFO_EN_BIT 02481 */ 02482 bool MPU6051::getFIFOEnabled() 02483 { 02484 i2Cdev.readBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_FIFO_EN_BIT, buffer); 02485 return buffer[0]; 02486 } 02487 /** Set FIFO enabled status. 02488 * @param enabled New FIFO enabled status 02489 * @see getFIFOEnabled() 02490 * @see MPU6050_RA_USER_CTRL 02491 * @see MPU6050_USERCTRL_FIFO_EN_BIT 02492 */ 02493 void MPU6051::setFIFOEnabled(bool enabled) 02494 { 02495 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_FIFO_EN_BIT, enabled); 02496 } 02497 /** Get I2C Master Mode enabled status. 02498 * When this mode is enabled, the MPU-60X0 acts as the I2C Master to the 02499 * external sensor slave devices on the auxiliary I2C bus. When this bit is 02500 * cleared to 0, the auxiliary I2C bus lines (AUX_DA and AUX_CL) are logically 02501 * driven by the primary I2C bus (SDA and SCL). This is a precondition to 02502 * enabling Bypass Mode. For further information regarding Bypass Mode, please 02503 * refer to Register 55. 02504 * @return Current I2C Master Mode enabled status 02505 * @see MPU6050_RA_USER_CTRL 02506 * @see MPU6050_USERCTRL_I2C_MST_EN_BIT 02507 */ 02508 bool MPU6051::getI2CMasterModeEnabled() 02509 { 02510 i2Cdev.readBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_I2C_MST_EN_BIT, buffer); 02511 return buffer[0]; 02512 } 02513 /** Set I2C Master Mode enabled status. 02514 * @param enabled New I2C Master Mode enabled status 02515 * @see getI2CMasterModeEnabled() 02516 * @see MPU6050_RA_USER_CTRL 02517 * @see MPU6050_USERCTRL_I2C_MST_EN_BIT 02518 */ 02519 void MPU6051::setI2CMasterModeEnabled(bool enabled) 02520 { 02521 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_I2C_MST_EN_BIT, enabled); 02522 } 02523 /** Switch from I2C to SPI mode (MPU-6000 only) 02524 * If this is set, the primary SPI interface will be enabled in place of the 02525 * disabled primary I2C interface. 02526 */ 02527 void MPU6051::switchSPIEnabled(bool enabled) 02528 { 02529 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_I2C_IF_DIS_BIT, enabled); 02530 } 02531 /** Reset the FIFO. 02532 * This bit resets the FIFO buffer when set to 1 while FIFO_EN equals 0. This 02533 * bit automatically clears to 0 after the reset has been triggered. 02534 * @see MPU6050_RA_USER_CTRL 02535 * @see MPU6050_USERCTRL_FIFO_RESET_BIT 02536 */ 02537 void MPU6051::resetFIFO() 02538 { 02539 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_FIFO_RESET_BIT, true); 02540 } 02541 /** Reset the I2C Master. 02542 * This bit resets the I2C Master when set to 1 while I2C_MST_EN equals 0. 02543 * This bit automatically clears to 0 after the reset has been triggered. 02544 * @see MPU6050_RA_USER_CTRL 02545 * @see MPU6050_USERCTRL_I2C_MST_RESET_BIT 02546 */ 02547 void MPU6051::resetI2CMaster() 02548 { 02549 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_I2C_MST_RESET_BIT, true); 02550 } 02551 /** Reset all sensor registers and signal paths. 02552 * When set to 1, this bit resets the signal paths for all sensors (gyroscopes, 02553 * accelerometers, and temperature sensor). This operation will also clear the 02554 * sensor registers. This bit automatically clears to 0 after the reset has been 02555 * triggered. 02556 * 02557 * When resetting only the signal path (and not the sensor registers), please 02558 * use Register 104, SIGNAL_PATH_RESET. 02559 * 02560 * @see MPU6050_RA_USER_CTRL 02561 * @see MPU6050_USERCTRL_SIG_COND_RESET_BIT 02562 */ 02563 void MPU6051::resetSensors() 02564 { 02565 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_SIG_COND_RESET_BIT, true); 02566 } 02567 02568 // PWR_MGMT_1 register 02569 02570 /** Trigger a full device reset. 02571 * A small delay of ~50ms may be desirable after triggering a reset. 02572 * @see MPU6050_RA_PWR_MGMT_1 02573 * @see MPU6050_PWR1_DEVICE_RESET_BIT 02574 */ 02575 void MPU6051::reset() 02576 { 02577 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_DEVICE_RESET_BIT, true); 02578 } 02579 /** Get sleep mode status. 02580 * Setting the SLEEP bit in the register puts the device into very low power 02581 * sleep mode. In this mode, only the serial interface and internal registers 02582 * remain active, allowing for a very low standby current. Clearing this bit 02583 * puts the device back into normal mode. To save power, the individual standby 02584 * selections for each of the gyros should be used if any gyro axis is not used 02585 * by the application. 02586 * @return Current sleep mode enabled status 02587 * @see MPU6050_RA_PWR_MGMT_1 02588 * @see MPU6050_PWR1_SLEEP_BIT 02589 */ 02590 bool MPU6051::getSleepEnabled() 02591 { 02592 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_SLEEP_BIT, buffer); 02593 return buffer[0]; 02594 } 02595 /** Set sleep mode status. 02596 * @param enabled New sleep mode enabled status 02597 * @see getSleepEnabled() 02598 * @see MPU6050_RA_PWR_MGMT_1 02599 * @see MPU6050_PWR1_SLEEP_BIT 02600 */ 02601 void MPU6051::setSleepEnabled(bool enabled) 02602 { 02603 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_SLEEP_BIT, enabled); 02604 } 02605 /** Get wake cycle enabled status. 02606 * When this bit is set to 1 and SLEEP is disabled, the MPU-60X0 will cycle 02607 * between sleep mode and waking up to take a single sample of data from active 02608 * sensors at a rate determined by LP_WAKE_CTRL (register 108). 02609 * @return Current sleep mode enabled status 02610 * @see MPU6050_RA_PWR_MGMT_1 02611 * @see MPU6050_PWR1_CYCLE_BIT 02612 */ 02613 bool MPU6051::getWakeCycleEnabled() 02614 { 02615 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_CYCLE_BIT, buffer); 02616 return buffer[0]; 02617 } 02618 /** Set wake cycle enabled status. 02619 * @param enabled New sleep mode enabled status 02620 * @see getWakeCycleEnabled() 02621 * @see MPU6050_RA_PWR_MGMT_1 02622 * @see MPU6050_PWR1_CYCLE_BIT 02623 */ 02624 void MPU6051::setWakeCycleEnabled(bool enabled) 02625 { 02626 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_CYCLE_BIT, enabled); 02627 } 02628 /** Get temperature sensor enabled status. 02629 * Control the usage of the internal temperature sensor. 02630 * 02631 * Note: this register stores the *disabled* value, but for consistency with the 02632 * rest of the code, the function is named and used with standard true/false 02633 * values to indicate whether the sensor is enabled or disabled, respectively. 02634 * 02635 * @return Current temperature sensor enabled status 02636 * @see MPU6050_RA_PWR_MGMT_1 02637 * @see MPU6050_PWR1_TEMP_DIS_BIT 02638 */ 02639 bool MPU6051::getTempSensorEnabled() 02640 { 02641 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_TEMP_DIS_BIT, buffer); 02642 return buffer[0] == 0; // 1 is actually disabled here 02643 } 02644 /** Set temperature sensor enabled status. 02645 * Note: this register stores the *disabled* value, but for consistency with the 02646 * rest of the code, the function is named and used with standard true/false 02647 * values to indicate whether the sensor is enabled or disabled, respectively. 02648 * 02649 * @param enabled New temperature sensor enabled status 02650 * @see getTempSensorEnabled() 02651 * @see MPU6050_RA_PWR_MGMT_1 02652 * @see MPU6050_PWR1_TEMP_DIS_BIT 02653 */ 02654 void MPU6051::setTempSensorEnabled(bool enabled) 02655 { 02656 // 1 is actually disabled here 02657 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_TEMP_DIS_BIT, !enabled); 02658 } 02659 /** Get clock source setting. 02660 * @return Current clock source setting 02661 * @see MPU6050_RA_PWR_MGMT_1 02662 * @see MPU6050_PWR1_CLKSEL_BIT 02663 * @see MPU6050_PWR1_CLKSEL_LENGTH 02664 */ 02665 uint8_t MPU6051::getClockSource() 02666 { 02667 i2Cdev.readBits(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_CLKSEL_BIT, MPU6050_PWR1_CLKSEL_LENGTH, buffer); 02668 return buffer[0]; 02669 } 02670 /** Set clock source setting. 02671 * An internal 8MHz oscillator, gyroscope based clock, or external sources can 02672 * be selected as the MPU-60X0 clock source. When the internal 8 MHz oscillator 02673 * or an external source is chosen as the clock source, the MPU-60X0 can operate 02674 * in low power modes with the gyroscopes disabled. 02675 * 02676 * Upon power up, the MPU-60X0 clock source defaults to the internal oscillator. 02677 * However, it is highly recommended that the device be configured to use one of 02678 * the gyroscopes (or an external clock source) as the clock reference for 02679 * improved stability. The clock source can be selected according to the following table: 02680 * 02681 * <pre> 02682 * CLK_SEL | Clock Source 02683 * --------+-------------------------------------- 02684 * 0 | Internal oscillator 02685 * 1 | PLL with X Gyro reference 02686 * 2 | PLL with Y Gyro reference 02687 * 3 | PLL with Z Gyro reference 02688 * 4 | PLL with external 32.768kHz reference 02689 * 5 | PLL with external 19.2MHz reference 02690 * 6 | Reserved 02691 * 7 | Stops the clock and keeps the timing generator in reset 02692 * </pre> 02693 * 02694 * @param source New clock source setting 02695 * @see getClockSource() 02696 * @see MPU6050_RA_PWR_MGMT_1 02697 * @see MPU6050_PWR1_CLKSEL_BIT 02698 * @see MPU6050_PWR1_CLKSEL_LENGTH 02699 */ 02700 void MPU6051::setClockSource(uint8_t source) 02701 { 02702 i2Cdev.writeBits(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_CLKSEL_BIT, MPU6050_PWR1_CLKSEL_LENGTH, source); 02703 } 02704 02705 // PWR_MGMT_2 register 02706 02707 /** Get wake frequency in Accel-Only Low Power Mode. 02708 * The MPU-60X0 can be put into Accerlerometer Only Low Power Mode by setting 02709 * PWRSEL to 1 in the Power Management 1 register (Register 107). In this mode, 02710 * the device will power off all devices except for the primary I2C interface, 02711 * waking only the accelerometer at fixed intervals to take a single 02712 * measurement. The frequency of wake-ups can be configured with LP_WAKE_CTRL 02713 * as shown below: 02714 * 02715 * <pre> 02716 * LP_WAKE_CTRL | Wake-up Frequency 02717 * -------------+------------------ 02718 * 0 | 1.25 Hz 02719 * 1 | 2.5 Hz 02720 * 2 | 5 Hz 02721 * 3 | 10 Hz 02722 * <pre> 02723 * 02724 * For further information regarding the MPU-60X0's power modes, please refer to 02725 * Register 107. 02726 * 02727 * @return Current wake frequency 02728 * @see MPU6050_RA_PWR_MGMT_2 02729 */ 02730 uint8_t MPU6051::getWakeFrequency() 02731 { 02732 i2Cdev.readBits(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_LP_WAKE_CTRL_BIT, MPU6050_PWR2_LP_WAKE_CTRL_LENGTH, buffer); 02733 return buffer[0]; 02734 } 02735 /** Set wake frequency in Accel-Only Low Power Mode. 02736 * @param frequency New wake frequency 02737 * @see MPU6050_RA_PWR_MGMT_2 02738 */ 02739 void MPU6051::setWakeFrequency(uint8_t frequency) 02740 { 02741 i2Cdev.writeBits(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_LP_WAKE_CTRL_BIT, MPU6050_PWR2_LP_WAKE_CTRL_LENGTH, frequency); 02742 } 02743 02744 /** Get X-axis accelerometer standby enabled status. 02745 * If enabled, the X-axis will not gather or report data (or use power). 02746 * @return Current X-axis standby enabled status 02747 * @see MPU6050_RA_PWR_MGMT_2 02748 * @see MPU6050_PWR2_STBY_XA_BIT 02749 */ 02750 bool MPU6051::getStandbyXAccelEnabled() 02751 { 02752 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_XA_BIT, buffer); 02753 return buffer[0]; 02754 } 02755 /** Set X-axis accelerometer standby enabled status. 02756 * @param New X-axis standby enabled status 02757 * @see getStandbyXAccelEnabled() 02758 * @see MPU6050_RA_PWR_MGMT_2 02759 * @see MPU6050_PWR2_STBY_XA_BIT 02760 */ 02761 void MPU6051::setStandbyXAccelEnabled(bool enabled) 02762 { 02763 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_XA_BIT, enabled); 02764 } 02765 /** Get Y-axis accelerometer standby enabled status. 02766 * If enabled, the Y-axis will not gather or report data (or use power). 02767 * @return Current Y-axis standby enabled status 02768 * @see MPU6050_RA_PWR_MGMT_2 02769 * @see MPU6050_PWR2_STBY_YA_BIT 02770 */ 02771 bool MPU6051::getStandbyYAccelEnabled() 02772 { 02773 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_YA_BIT, buffer); 02774 return buffer[0]; 02775 } 02776 /** Set Y-axis accelerometer standby enabled status. 02777 * @param New Y-axis standby enabled status 02778 * @see getStandbyYAccelEnabled() 02779 * @see MPU6050_RA_PWR_MGMT_2 02780 * @see MPU6050_PWR2_STBY_YA_BIT 02781 */ 02782 void MPU6051::setStandbyYAccelEnabled(bool enabled) 02783 { 02784 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_YA_BIT, enabled); 02785 } 02786 /** Get Z-axis accelerometer standby enabled status. 02787 * If enabled, the Z-axis will not gather or report data (or use power). 02788 * @return Current Z-axis standby enabled status 02789 * @see MPU6050_RA_PWR_MGMT_2 02790 * @see MPU6050_PWR2_STBY_ZA_BIT 02791 */ 02792 bool MPU6051::getStandbyZAccelEnabled() 02793 { 02794 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_ZA_BIT, buffer); 02795 return buffer[0]; 02796 } 02797 /** Set Z-axis accelerometer standby enabled status. 02798 * @param New Z-axis standby enabled status 02799 * @see getStandbyZAccelEnabled() 02800 * @see MPU6050_RA_PWR_MGMT_2 02801 * @see MPU6050_PWR2_STBY_ZA_BIT 02802 */ 02803 void MPU6051::setStandbyZAccelEnabled(bool enabled) 02804 { 02805 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_ZA_BIT, enabled); 02806 } 02807 /** Get X-axis gyroscope standby enabled status. 02808 * If enabled, the X-axis will not gather or report data (or use power). 02809 * @return Current X-axis standby enabled status 02810 * @see MPU6050_RA_PWR_MGMT_2 02811 * @see MPU6050_PWR2_STBY_XG_BIT 02812 */ 02813 bool MPU6051::getStandbyXGyroEnabled() 02814 { 02815 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_XG_BIT, buffer); 02816 return buffer[0]; 02817 } 02818 /** Set X-axis gyroscope standby enabled status. 02819 * @param New X-axis standby enabled status 02820 * @see getStandbyXGyroEnabled() 02821 * @see MPU6050_RA_PWR_MGMT_2 02822 * @see MPU6050_PWR2_STBY_XG_BIT 02823 */ 02824 void MPU6051::setStandbyXGyroEnabled(bool enabled) 02825 { 02826 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_XG_BIT, enabled); 02827 } 02828 /** Get Y-axis gyroscope standby enabled status. 02829 * If enabled, the Y-axis will not gather or report data (or use power). 02830 * @return Current Y-axis standby enabled status 02831 * @see MPU6050_RA_PWR_MGMT_2 02832 * @see MPU6050_PWR2_STBY_YG_BIT 02833 */ 02834 bool MPU6051::getStandbyYGyroEnabled() 02835 { 02836 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_YG_BIT, buffer); 02837 return buffer[0]; 02838 } 02839 /** Set Y-axis gyroscope standby enabled status. 02840 * @param New Y-axis standby enabled status 02841 * @see getStandbyYGyroEnabled() 02842 * @see MPU6050_RA_PWR_MGMT_2 02843 * @see MPU6050_PWR2_STBY_YG_BIT 02844 */ 02845 void MPU6051::setStandbyYGyroEnabled(bool enabled) 02846 { 02847 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_YG_BIT, enabled); 02848 } 02849 /** Get Z-axis gyroscope standby enabled status. 02850 * If enabled, the Z-axis will not gather or report data (or use power). 02851 * @return Current Z-axis standby enabled status 02852 * @see MPU6050_RA_PWR_MGMT_2 02853 * @see MPU6050_PWR2_STBY_ZG_BIT 02854 */ 02855 bool MPU6051::getStandbyZGyroEnabled() 02856 { 02857 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_ZG_BIT, buffer); 02858 return buffer[0]; 02859 } 02860 /** Set Z-axis gyroscope standby enabled status. 02861 * @param New Z-axis standby enabled status 02862 * @see getStandbyZGyroEnabled() 02863 * @see MPU6050_RA_PWR_MGMT_2 02864 * @see MPU6050_PWR2_STBY_ZG_BIT 02865 */ 02866 void MPU6051::setStandbyZGyroEnabled(bool enabled) 02867 { 02868 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_ZG_BIT, enabled); 02869 } 02870 02871 // FIFO_COUNT* registers 02872 02873 /** Get current FIFO buffer size. 02874 * This value indicates the number of bytes stored in the FIFO buffer. This 02875 * number is in turn the number of bytes that can be read from the FIFO buffer 02876 * and it is directly proportional to the number of samples available given the 02877 * set of sensor data bound to be stored in the FIFO (register 35 and 36). 02878 * @return Current FIFO buffer size 02879 */ 02880 uint16_t MPU6051::getFIFOCount() 02881 { 02882 i2Cdev.readBytes(devAddr, MPU6050_RA_FIFO_COUNTH, 2, buffer); 02883 return (((uint16_t)buffer[0]) << 8) | buffer[1]; 02884 } 02885 02886 // FIFO_R_W register 02887 02888 /** Get byte from FIFO buffer. 02889 * This register is used to read and write data from the FIFO buffer. Data is 02890 * written to the FIFO in order of register number (from lowest to highest). If 02891 * all the FIFO enable flags (see below) are enabled and all External Sensor 02892 * Data registers (Registers 73 to 96) are associated with a Slave device, the 02893 * contents of registers 59 through 96 will be written in order at the Sample 02894 * Rate. 02895 * 02896 * The contents of the sensor data registers (Registers 59 to 96) are written 02897 * into the FIFO buffer when their corresponding FIFO enable flags are set to 1 02898 * in FIFO_EN (Register 35). An additional flag for the sensor data registers 02899 * associated with I2C Slave 3 can be found in I2C_MST_CTRL (Register 36). 02900 * 02901 * If the FIFO buffer has overflowed, the status bit FIFO_OFLOW_INT is 02902 * automatically set to 1. This bit is located in INT_STATUS (Register 58). 02903 * When the FIFO buffer has overflowed, the oldest data will be lost and new 02904 * data will be written to the FIFO. 02905 * 02906 * If the FIFO buffer is empty, reading this register will return the last byte 02907 * that was previously read from the FIFO until new data is available. The user 02908 * should check FIFO_COUNT to ensure that the FIFO buffer is not read when 02909 * empty. 02910 * 02911 * @return Byte from FIFO buffer 02912 */ 02913 uint8_t MPU6051::getFIFOByte() 02914 { 02915 i2Cdev.readByte(devAddr, MPU6050_RA_FIFO_R_W, buffer); 02916 return buffer[0]; 02917 } 02918 void MPU6051::getFIFOBytes(uint8_t *data, uint8_t length) 02919 { 02920 i2Cdev.readBytes(devAddr, MPU6050_RA_FIFO_R_W, length, data); 02921 } 02922 /** Write byte to FIFO buffer. 02923 * @see getFIFOByte() 02924 * @see MPU6050_RA_FIFO_R_W 02925 */ 02926 void MPU6051::setFIFOByte(uint8_t data) 02927 { 02928 i2Cdev.writeByte(devAddr, MPU6050_RA_FIFO_R_W, data); 02929 } 02930 02931 // WHO_AM_I register 02932 02933 /** Get Device ID. 02934 * This register is used to verify the identity of the device (0b110100, 0x34). 02935 * @return Device ID (6 bits only! should be 0x34) 02936 * @see MPU6050_RA_WHO_AM_I 02937 * @see MPU6050_WHO_AM_I_BIT 02938 * @see MPU6050_WHO_AM_I_LENGTH 02939 */ 02940 uint8_t MPU6051::getDeviceID() 02941 { 02942 i2Cdev.readBits(devAddr, MPU6050_RA_WHO_AM_I, MPU6050_WHO_AM_I_BIT, MPU6050_WHO_AM_I_LENGTH, buffer); 02943 return buffer[0]; 02944 } 02945 /** Set Device ID. 02946 * Write a new ID into the WHO_AM_I register (no idea why this should ever be 02947 * necessary though). 02948 * @param id New device ID to set. 02949 * @see getDeviceID() 02950 * @see MPU6050_RA_WHO_AM_I 02951 * @see MPU6050_WHO_AM_I_BIT 02952 * @see MPU6050_WHO_AM_I_LENGTH 02953 */ 02954 void MPU6051::setDeviceID(uint8_t id) 02955 { 02956 i2Cdev.writeBits(devAddr, MPU6050_RA_WHO_AM_I, MPU6050_WHO_AM_I_BIT, MPU6050_WHO_AM_I_LENGTH, id); 02957 } 02958 02959 // ======== UNDOCUMENTED/DMP REGISTERS/METHODS ======== 02960 02961 // XG_OFFS_TC register 02962 02963 uint8_t MPU6051::getOTPBankValid() 02964 { 02965 i2Cdev.readBit(devAddr, MPU6050_RA_XG_OFFS_TC, MPU6050_TC_OTP_BNK_VLD_BIT, buffer); 02966 return buffer[0]; 02967 } 02968 void MPU6051::setOTPBankValid(bool enabled) 02969 { 02970 i2Cdev.writeBit(devAddr, MPU6050_RA_XG_OFFS_TC, MPU6050_TC_OTP_BNK_VLD_BIT, enabled); 02971 } 02972 int8_t MPU6051::getXGyroOffset() 02973 { 02974 i2Cdev.readBits(devAddr, MPU6050_RA_XG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, buffer); 02975 return buffer[0]; 02976 } 02977 void MPU6051::setXGyroOffset(int8_t offset) 02978 { 02979 i2Cdev.writeBits(devAddr, MPU6050_RA_XG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, offset); 02980 } 02981 02982 // YG_OFFS_TC register 02983 02984 int8_t MPU6051::getYGyroOffset() 02985 { 02986 i2Cdev.readBits(devAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, buffer); 02987 return buffer[0]; 02988 } 02989 void MPU6051::setYGyroOffset(int8_t offset) 02990 { 02991 i2Cdev.writeBits(devAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, offset); 02992 } 02993 02994 // ZG_OFFS_TC register 02995 02996 int8_t MPU6051::getZGyroOffset() 02997 { 02998 i2Cdev.readBits(devAddr, MPU6050_RA_ZG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, buffer); 02999 return buffer[0]; 03000 } 03001 void MPU6051::setZGyroOffset(int8_t offset) 03002 { 03003 i2Cdev.writeBits(devAddr, MPU6050_RA_ZG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, offset); 03004 } 03005 03006 // X_FINE_GAIN register 03007 03008 int8_t MPU6051::getXFineGain() 03009 { 03010 i2Cdev.readByte(devAddr, MPU6050_RA_X_FINE_GAIN, buffer); 03011 return buffer[0]; 03012 } 03013 void MPU6051::setXFineGain(int8_t gain) 03014 { 03015 i2Cdev.writeByte(devAddr, MPU6050_RA_X_FINE_GAIN, gain); 03016 } 03017 03018 // Y_FINE_GAIN register 03019 03020 int8_t MPU6051::getYFineGain() 03021 { 03022 i2Cdev.readByte(devAddr, MPU6050_RA_Y_FINE_GAIN, buffer); 03023 return buffer[0]; 03024 } 03025 void MPU6051::setYFineGain(int8_t gain) 03026 { 03027 i2Cdev.writeByte(devAddr, MPU6050_RA_Y_FINE_GAIN, gain); 03028 } 03029 03030 // Z_FINE_GAIN register 03031 03032 int8_t MPU6051::getZFineGain() 03033 { 03034 i2Cdev.readByte(devAddr, MPU6050_RA_Z_FINE_GAIN, buffer); 03035 return buffer[0]; 03036 } 03037 void MPU6051::setZFineGain(int8_t gain) 03038 { 03039 i2Cdev.writeByte(devAddr, MPU6050_RA_Z_FINE_GAIN, gain); 03040 } 03041 03042 // XA_OFFS_* registers 03043 03044 int16_t MPU6051::getXAccelOffset() 03045 { 03046 i2Cdev.readBytes(devAddr, MPU6050_RA_XA_OFFS_H, 2, buffer); 03047 return (((int16_t)buffer[0]) << 8) | buffer[1]; 03048 } 03049 void MPU6051::setXAccelOffset(int16_t offset) 03050 { 03051 i2Cdev.writeWord(devAddr, MPU6050_RA_XA_OFFS_H, offset); 03052 } 03053 03054 // YA_OFFS_* register 03055 03056 int16_t MPU6051::getYAccelOffset() 03057 { 03058 i2Cdev.readBytes(devAddr, MPU6050_RA_YA_OFFS_H, 2, buffer); 03059 return (((int16_t)buffer[0]) << 8) | buffer[1]; 03060 } 03061 void MPU6051::setYAccelOffset(int16_t offset) 03062 { 03063 i2Cdev.writeWord(devAddr, MPU6050_RA_YA_OFFS_H, offset); 03064 } 03065 03066 // ZA_OFFS_* register 03067 03068 int16_t MPU6051::getZAccelOffset() 03069 { 03070 i2Cdev.readBytes(devAddr, MPU6050_RA_ZA_OFFS_H, 2, buffer); 03071 return (((int16_t)buffer[0]) << 8) | buffer[1]; 03072 } 03073 void MPU6051::setZAccelOffset(int16_t offset) 03074 { 03075 i2Cdev.writeWord(devAddr, MPU6050_RA_ZA_OFFS_H, offset); 03076 } 03077 03078 // XG_OFFS_USR* registers 03079 03080 int16_t MPU6051::getXGyroOffsetUser() 03081 { 03082 i2Cdev.readBytes(devAddr, MPU6050_RA_XG_OFFS_USRH, 2, buffer); 03083 return (((int16_t)buffer[0]) << 8) | buffer[1]; 03084 } 03085 void MPU6051::setXGyroOffsetUser(int16_t offset) 03086 { 03087 i2Cdev.writeWord(devAddr, MPU6050_RA_XG_OFFS_USRH, offset); 03088 } 03089 03090 // YG_OFFS_USR* register 03091 03092 int16_t MPU6051::getYGyroOffsetUser() 03093 { 03094 i2Cdev.readBytes(devAddr, MPU6050_RA_YG_OFFS_USRH, 2, buffer); 03095 return (((int16_t)buffer[0]) << 8) | buffer[1]; 03096 } 03097 void MPU6051::setYGyroOffsetUser(int16_t offset) 03098 { 03099 i2Cdev.writeWord(devAddr, MPU6050_RA_YG_OFFS_USRH, offset); 03100 } 03101 03102 // ZG_OFFS_USR* register 03103 03104 int16_t MPU6051::getZGyroOffsetUser() 03105 { 03106 i2Cdev.readBytes(devAddr, MPU6050_RA_ZG_OFFS_USRH, 2, buffer); 03107 return (((int16_t)buffer[0]) << 8) | buffer[1]; 03108 } 03109 void MPU6051::setZGyroOffsetUser(int16_t offset) 03110 { 03111 i2Cdev.writeWord(devAddr, MPU6050_RA_ZG_OFFS_USRH, offset); 03112 } 03113 03114 // INT_ENABLE register (DMP functions) 03115 03116 bool MPU6051::getIntPLLReadyEnabled() 03117 { 03118 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_PLL_RDY_INT_BIT, buffer); 03119 return buffer[0]; 03120 } 03121 void MPU6051::setIntPLLReadyEnabled(bool enabled) 03122 { 03123 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_PLL_RDY_INT_BIT, enabled); 03124 } 03125 bool MPU6051::getIntDMPEnabled() 03126 { 03127 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_DMP_INT_BIT, buffer); 03128 return buffer[0]; 03129 } 03130 void MPU6051::setIntDMPEnabled(bool enabled) 03131 { 03132 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_DMP_INT_BIT, enabled); 03133 } 03134 03135 // DMP_INT_STATUS 03136 03137 bool MPU6051::getDMPInt5Status() 03138 { 03139 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_5_BIT, buffer); 03140 return buffer[0]; 03141 } 03142 bool MPU6051::getDMPInt4Status() 03143 { 03144 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_4_BIT, buffer); 03145 return buffer[0]; 03146 } 03147 bool MPU6051::getDMPInt3Status() 03148 { 03149 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_3_BIT, buffer); 03150 return buffer[0]; 03151 } 03152 bool MPU6051::getDMPInt2Status() 03153 { 03154 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_2_BIT, buffer); 03155 return buffer[0]; 03156 } 03157 bool MPU6051::getDMPInt1Status() 03158 { 03159 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_1_BIT, buffer); 03160 return buffer[0]; 03161 } 03162 bool MPU6051::getDMPInt0Status() 03163 { 03164 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_0_BIT, buffer); 03165 return buffer[0]; 03166 } 03167 03168 // INT_STATUS register (DMP functions) 03169 03170 bool MPU6051::getIntPLLReadyStatus() 03171 { 03172 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_PLL_RDY_INT_BIT, buffer); 03173 return buffer[0]; 03174 } 03175 bool MPU6051::getIntDMPStatus() 03176 { 03177 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_DMP_INT_BIT, buffer); 03178 return buffer[0]; 03179 } 03180 03181 // USER_CTRL register (DMP functions) 03182 03183 bool MPU6051::getDMPEnabled() 03184 { 03185 i2Cdev.readBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_DMP_EN_BIT, buffer); 03186 return buffer[0]; 03187 } 03188 void MPU6051::setDMPEnabled(bool enabled) 03189 { 03190 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_DMP_EN_BIT, enabled); 03191 } 03192 void MPU6051::resetDMP() 03193 { 03194 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_DMP_RESET_BIT, true); 03195 } 03196 03197 // BANK_SEL register 03198 03199 void MPU6051::setMemoryBank(uint8_t bank, bool prefetchEnabled, bool userBank) 03200 { 03201 bank &= 0x1F; 03202 if (userBank) bank |= 0x20; 03203 if (prefetchEnabled) bank |= 0x40; 03204 i2Cdev.writeByte(devAddr, MPU6050_RA_BANK_SEL, bank); 03205 } 03206 03207 // MEM_START_ADDR register 03208 03209 void MPU6051::setMemoryStartAddress(uint8_t address) 03210 { 03211 i2Cdev.writeByte(devAddr, MPU6050_RA_MEM_START_ADDR, address); 03212 } 03213 03214 // MEM_R_W register 03215 03216 uint8_t MPU6051::readMemoryByte() 03217 { 03218 i2Cdev.readByte(devAddr, MPU6050_RA_MEM_R_W, buffer); 03219 return buffer[0]; 03220 } 03221 void MPU6051::writeMemoryByte(uint8_t data) 03222 { 03223 i2Cdev.writeByte(devAddr, MPU6050_RA_MEM_R_W, data); 03224 } 03225 void MPU6051::readMemoryBlock(uint8_t *data, uint16_t dataSize, uint8_t bank, uint8_t address) 03226 { 03227 setMemoryBank(bank); 03228 setMemoryStartAddress(address); 03229 uint8_t chunkSize; 03230 for (uint16_t i = 0; i < dataSize;) { 03231 // determine correct chunk size according to bank position and data size 03232 chunkSize = MPU6050_DMP_MEMORY_CHUNK_SIZE; 03233 03234 // make sure we don't go past the data size 03235 if (i + chunkSize > dataSize) chunkSize = dataSize - i; 03236 03237 // make sure this chunk doesn't go past the bank boundary (256 bytes) 03238 if (chunkSize > 256 - address) chunkSize = 256 - address; 03239 03240 // read the chunk of data as specified 03241 i2Cdev.readBytes(devAddr, MPU6050_RA_MEM_R_W, chunkSize, data + i); 03242 03243 // increase byte index by [chunkSize] 03244 i += chunkSize; 03245 03246 // uint8_t automatically wraps to 0 at 256 03247 address += chunkSize; 03248 03249 // if we aren't done, update bank (if necessary) and address 03250 if (i < dataSize) { 03251 if (address == 0) bank++; 03252 setMemoryBank(bank); 03253 setMemoryStartAddress(address); 03254 } 03255 } 03256 } 03257 bool MPU6051::writeMemoryBlock(const uint8_t *data, uint16_t dataSize, uint8_t bank, uint8_t address, bool verify, bool useProgMem) 03258 { 03259 setMemoryBank(bank); 03260 setMemoryStartAddress(address); 03261 uint8_t chunkSize; 03262 uint8_t *verifyBuffer = NULL; 03263 uint8_t *progBuffer = NULL; 03264 uint16_t i; 03265 uint8_t j; 03266 if (verify) verifyBuffer = (uint8_t *)malloc(MPU6050_DMP_MEMORY_CHUNK_SIZE); 03267 if (useProgMem) progBuffer = (uint8_t *)malloc(MPU6050_DMP_MEMORY_CHUNK_SIZE); 03268 for (i = 0; i < dataSize;) { 03269 // determine correct chunk size according to bank position and data size 03270 chunkSize = MPU6050_DMP_MEMORY_CHUNK_SIZE; 03271 03272 // make sure we don't go past the data size 03273 if (i + chunkSize > dataSize) chunkSize = dataSize - i; 03274 03275 // make sure this chunk doesn't go past the bank boundary (256 bytes) 03276 if (chunkSize > 256 - address) chunkSize = 256 - address; 03277 03278 if (useProgMem) { 03279 // write the chunk of data as specified 03280 for (j = 0; j < chunkSize; j++) progBuffer[j] = pgm_read_byte(data + i + j); 03281 } else { 03282 // write the chunk of data as specified 03283 progBuffer = (uint8_t *)data + i; 03284 } 03285 03286 i2Cdev.writeBytes(devAddr, MPU6050_RA_MEM_R_W, chunkSize, progBuffer); 03287 03288 // verify data if needed 03289 if (verify && verifyBuffer) { 03290 setMemoryBank(bank); 03291 setMemoryStartAddress(address); 03292 i2Cdev.readBytes(devAddr, MPU6050_RA_MEM_R_W, chunkSize, verifyBuffer); 03293 if (memcmp(progBuffer, verifyBuffer, chunkSize) != 0) { 03294 /*Serial.print("Block write verification error, bank "); 03295 Serial.print(bank, DEC); 03296 Serial.print(", address "); 03297 Serial.print(address, DEC); 03298 Serial.print("!\nExpected:"); 03299 for (j = 0; j < chunkSize; j++) { 03300 Serial.print(" 0x"); 03301 if (progBuffer[j] < 16) Serial.print("0"); 03302 Serial.print(progBuffer[j], HEX); 03303 } 03304 Serial.print("\nReceived:"); 03305 for (uint8_t j = 0; j < chunkSize; j++) { 03306 Serial.print(" 0x"); 03307 if (verifyBuffer[i + j] < 16) Serial.print("0"); 03308 Serial.print(verifyBuffer[i + j], HEX); 03309 } 03310 Serial.print("\n");*/ 03311 free(verifyBuffer); 03312 if (useProgMem) free(progBuffer); 03313 return false; // uh oh. 03314 } 03315 } 03316 03317 // increase byte index by [chunkSize] 03318 i += chunkSize; 03319 03320 // uint8_t automatically wraps to 0 at 256 03321 address += chunkSize; 03322 03323 // if we aren't done, update bank (if necessary) and address 03324 if (i < dataSize) { 03325 if (address == 0) bank++; 03326 setMemoryBank(bank); 03327 setMemoryStartAddress(address); 03328 } 03329 } 03330 if (verify) free(verifyBuffer); 03331 if (useProgMem) free(progBuffer); 03332 return true; 03333 } 03334 bool MPU6051::writeProgMemoryBlock(const uint8_t *data, uint16_t dataSize, uint8_t bank, uint8_t address, bool verify) 03335 { 03336 return writeMemoryBlock(data, dataSize, bank, address, verify, true); 03337 } 03338 bool MPU6051::writeDMPConfigurationSet(const uint8_t *data, uint16_t dataSize, bool useProgMem) 03339 { 03340 uint8_t success, special; 03341 uint8_t *progBuffer = NULL; 03342 uint16_t i, j; 03343 if (useProgMem) { 03344 progBuffer = (uint8_t *)malloc(8); // assume 8-byte blocks, realloc later if necessary 03345 } 03346 03347 // config set data is a long string of blocks with the following structure: 03348 // [bank] [offset] [length] [byte[0], byte[1], ..., byte[length]] 03349 uint8_t bank, offset, length; 03350 for (i = 0; i < dataSize;) { 03351 if (useProgMem) { 03352 bank = pgm_read_byte(data + i++); 03353 offset = pgm_read_byte(data + i++); 03354 length = pgm_read_byte(data + i++); 03355 } else { 03356 bank = data[i++]; 03357 offset = data[i++]; 03358 length = data[i++]; 03359 } 03360 03361 // write data or perform special action 03362 if (length > 0) { 03363 // regular block of data to write 03364 /*Serial.print("Writing config block to bank "); 03365 Serial.print(bank); 03366 Serial.print(", offset "); 03367 Serial.print(offset); 03368 Serial.print(", length="); 03369 Serial.println(length);*/ 03370 if (useProgMem) { 03371 if (sizeof(progBuffer) < length) progBuffer = (uint8_t *)realloc(progBuffer, length); 03372 for (j = 0; j < length; j++) progBuffer[j] = pgm_read_byte(data + i + j); 03373 } else { 03374 progBuffer = (uint8_t *)data + i; 03375 } 03376 success = writeMemoryBlock(progBuffer, length, bank, offset, true); 03377 i += length; 03378 } else { 03379 // special instruction 03380 // NOTE: this kind of behavior (what and when to do certain things) 03381 // is totally undocumented. This code is in here based on observed 03382 // behavior only, and exactly why (or even whether) it has to be here 03383 // is anybody's guess for now. 03384 if (useProgMem) { 03385 special = pgm_read_byte(data + i++); 03386 } else { 03387 special = data[i++]; 03388 } 03389 /*Serial.print("Special command code "); 03390 Serial.print(special, HEX); 03391 Serial.println(" found...");*/ 03392 if (special == 0x01) { 03393 // enable DMP-related interrupts 03394 03395 //setIntZeroMotionEnabled(true); 03396 //setIntFIFOBufferOverflowEnabled(true); 03397 //setIntDMPEnabled(true); 03398 i2Cdev.writeByte(devAddr, MPU6050_RA_INT_ENABLE, 0x32); // single operation 03399 03400 success = true; 03401 } else { 03402 // unknown special command 03403 success = false; 03404 } 03405 } 03406 03407 if (!success) { 03408 if (useProgMem) free(progBuffer); 03409 return false; // uh oh 03410 } 03411 } 03412 if (useProgMem) free(progBuffer); 03413 return true; 03414 } 03415 bool MPU6051::writeProgDMPConfigurationSet(const uint8_t *data, uint16_t dataSize) 03416 { 03417 return writeDMPConfigurationSet(data, dataSize, false); 03418 } 03419 03420 // DMP_CFG_1 register 03421 03422 uint8_t MPU6051::getDMPConfig1() 03423 { 03424 i2Cdev.readByte(devAddr, MPU6050_RA_DMP_CFG_1, buffer); 03425 return buffer[0]; 03426 } 03427 void MPU6051::setDMPConfig1(uint8_t config) 03428 { 03429 i2Cdev.writeByte(devAddr, MPU6050_RA_DMP_CFG_1, config); 03430 } 03431 03432 // DMP_CFG_2 register 03433 03434 uint8_t MPU6051::getDMPConfig2() 03435 { 03436 i2Cdev.readByte(devAddr, MPU6050_RA_DMP_CFG_2, buffer); 03437 return buffer[0]; 03438 } 03439 void MPU6051::setDMPConfig2(uint8_t config) 03440 { 03441 i2Cdev.writeByte(devAddr, MPU6050_RA_DMP_CFG_2, config); 03442 }
Generated on Wed Jul 13 2022 10:32:23 by 1.7.2