PageRenderTime 2717ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/MPU6050-ChibiOS-devel/MPU6050/MPU6050.c

https://github.com/adrianalin/rtos_STM32F4Discovery
C | 3113 lines | 1026 code | 144 blank | 1943 comment | 42 complexity | b76776af5863ccc8c02bc3e36fb3e54a MD5 | raw file
Possible License(s): GPL-2.0
  1. // I2Cdev library collection - MPU6050 I2C device class header file
  2. // Based on InvenSense MPU-6050 register map document rev. 2.0, 5/19/2011 (RM-MPU-6000A-00)
  3. // 10/3/2011 by Jeff Rowberg <jeff@rowberg.net>
  4. // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
  5. //
  6. // Changelog:
  7. // ... - ongoing debug release
  8. // NOTE: THIS IS ONLY A PARIAL RELEASE. THIS DEVICE CLASS IS CURRENTLY UNDERGOING ACTIVE
  9. // DEVELOPMENT AND IS STILL MISSING SOME IMPORTANT FEATURES. PLEASE KEEP THIS IN MIND IF
  10. // YOU DECIDE TO USE THIS PARTICULAR CODE FOR ANYTHING.
  11. /* ChibiOS I2Cdev MPU6050 device class conversion 2/5/2013 by Jan Schlemminger - C conversion, ChibiOS compliance
  12. * First release. I just tested a few functions so this should be considered HIGHLY EXPERIMENTAL!!!
  13. * Feel free to test and report bugs. Updates at https://github.com/jevermeister/MPU6050-ChibiOS
  14. */
  15. /* ============================================
  16. ChibiOS I2Cdev MPU6050 device class code is placed under the MIT license
  17. Copyright (c) 2012 Jan Schlemminger
  18. Permission is hereby granted, free of charge, to any person obtaining a copy
  19. of this software and associated documentation files (the "Software"), to deal
  20. in the Software without restriction, including without limitation the rights
  21. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  22. copies of the Software, and to permit persons to whom the Software is
  23. furnished to do so, subject to the following conditions:
  24. The above copyright notice and this permission notice shall be included in
  25. all copies or substantial portions of the Software.
  26. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  29. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  30. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  31. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  32. THE SOFTWARE.
  33. ===============================================
  34. */
  35. #include "ch.h"
  36. #include "hal.h"
  37. #include "MPU6050.h"
  38. #include "../i2cdev_chibi/i2cdev_chibi.h"
  39. #include "chprintf.h"
  40. // for memcmp
  41. #include <string.h>
  42. /* Global variables, will see how to get rid of them
  43. *
  44. */
  45. uint8_t MPUdevAddr;
  46. uint8_t MPUbuffer[14];
  47. uint16_t MPUfifoCount; // count of all bytes currently in FIFO
  48. uint8_t MPUfifoBuffer[64]; // FIFO storage buffer
  49. /** Default constructor, uses default I2C address.
  50. * @see MPU6050_DEFAULT_ADDRESS
  51. */
  52. /*MPU6050::MPU6050() {
  53. devAddr = MPU6050_DEFAULT_ADDRESS;
  54. }*/
  55. /** Specific address constructor.
  56. * @param address I2C address
  57. * @see MPU6050_DEFAULT_ADDRESS
  58. * @see MPU6050_ADDRESS_AD0_LOW
  59. * @see MPU6050_ADDRESS_AD0_HIGH
  60. */
  61. void MPU6050(uint8_t address) {
  62. MPUdevAddr = address;
  63. }
  64. /** Power on and prepare for general usage.
  65. * This will activate the device and take it out of sleep mode (which must be done
  66. * after start-up). This function also sets both the accelerometer and the gyroscope
  67. * to their most sensitive settings, namely +/- 2g and +/- 250 degrees/sec, and sets
  68. * the clock source to use the X Gyro for reference, which is slightly better than
  69. * the default internal clock source.
  70. */
  71. void MPUinitialize() {
  72. MPUsetClockSource(MPU6050_CLOCK_PLL_XGYRO);
  73. MPUsetFullScaleGyroRange(MPU6050_GYRO_FS_250);
  74. MPUsetFullScaleAccelRange(MPU6050_ACCEL_FS_2);
  75. MPUsetSleepEnabled(FALSE); // thanks to Jack Elston for pointing this one out!
  76. }
  77. /** Verify the I2C connection.
  78. * Make sure the device is connected and responds as expected.
  79. * @return TRUE if connection is valid, FALSE otherwise
  80. */
  81. bool_t MPUtestConnection() {
  82. return MPUgetDeviceID() == 0x34;
  83. }
  84. // AUX_VDDIO register (InvenSense demo code calls this RA_*G_OFFS_TC)
  85. /** Get the auxiliary I2C supply voltage level.
  86. * When set to 1, the auxiliary I2C bus high logic level is VDD. When cleared to
  87. * 0, the auxiliary I2C bus high logic level is VLOGIC. This does not apply to
  88. * the MPU-6000, which does not have a VLOGIC pin.
  89. * @return I2C supply voltage level (0=VLOGIC, 1=VDD)
  90. */
  91. uint8_t MPUgetAuxVDDIOLevel() {
  92. I2CdevreadBit(MPUdevAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_PWR_MODE_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  93. return MPUbuffer[0];
  94. }
  95. /** Set the auxiliary I2C supply voltage level.
  96. * When set to 1, the auxiliary I2C bus high logic level is VDD. When cleared to
  97. * 0, the auxiliary I2C bus high logic level is VLOGIC. This does not apply to
  98. * the MPU-6000, which does not have a VLOGIC pin.
  99. * @param level I2C supply voltage level (0=VLOGIC, 1=VDD)
  100. */
  101. void MPUsetAuxVDDIOLevel(uint8_t level) {
  102. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_PWR_MODE_BIT, level);
  103. }
  104. // SMPLRT_DIV register
  105. /** Get gyroscope output rate divider.
  106. * The sensor register output, FIFO output, DMP sampling, Motion detection, Zero
  107. * Motion detection, and Free Fall detection are all based on the Sample Rate.
  108. * The Sample Rate is generated by dividing the gyroscope output rate by
  109. * SMPLRT_DIV:
  110. *
  111. * Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV)
  112. *
  113. * where Gyroscope Output Rate = 8kHz when the DLPF is disabled (DLPF_CFG = 0 or
  114. * 7), and 1kHz when the DLPF is enabled (see Register 26).
  115. *
  116. * Note: The accelerometer output rate is 1kHz. This means that for a Sample
  117. * Rate greater than 1kHz, the same accelerometer sample may be output to the
  118. * FIFO, DMP, and sensor registers more than once.
  119. *
  120. * For a diagram of the gyroscope and accelerometer signal paths, see Section 8
  121. * of the MPU-6000/MPU-6050 Product Specification document.
  122. *
  123. * @return Current sample rate
  124. * @see MPU6050_RA_SMPLRT_DIV
  125. */
  126. uint8_t MPUgetRate() {
  127. I2CdevreadByte(MPUdevAddr, MPU6050_RA_SMPLRT_DIV, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  128. return MPUbuffer[0];
  129. }
  130. /** Set gyroscope sample rate divider.
  131. * @param rate New sample rate divider
  132. * @see getRate()
  133. * @see MPU6050_RA_SMPLRT_DIV
  134. */
  135. void MPUsetRate(uint8_t rate) {
  136. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_SMPLRT_DIV, rate);
  137. }
  138. // CONFIG register
  139. /** Get external FSYNC configuration.
  140. * Configures the external Frame Synchronization (FSYNC) pin sampling. An
  141. * external signal connected to the FSYNC pin can be sampled by configuring
  142. * EXT_SYNC_SET. Signal changes to the FSYNC pin are latched so that short
  143. * strobes may be captured. The latched FSYNC signal will be sampled at the
  144. * Sampling Rate, as defined in register 25. After sampling, the latch will
  145. * reset to the current FSYNC signal state.
  146. *
  147. * The sampled value will be reported in place of the least significant bit in
  148. * a sensor data register determined by the value of EXT_SYNC_SET according to
  149. * the following table.
  150. *
  151. * <pre>
  152. * EXT_SYNC_SET | FSYNC Bit Location
  153. * -------------+-------------------
  154. * 0 | Input disabled
  155. * 1 | TEMP_OUT_L[0]
  156. * 2 | GYRO_XOUT_L[0]
  157. * 3 | GYRO_YOUT_L[0]
  158. * 4 | GYRO_ZOUT_L[0]
  159. * 5 | ACCEL_XOUT_L[0]
  160. * 6 | ACCEL_YOUT_L[0]
  161. * 7 | ACCEL_ZOUT_L[0]
  162. * </pre>
  163. *
  164. * @return FSYNC configuration value
  165. */
  166. uint8_t MPUgetExternalFrameSync() {
  167. I2CdevreadBits(MPUdevAddr, MPU6050_RA_CONFIG, MPU6050_CFG_EXT_SYNC_SET_BIT, MPU6050_CFG_EXT_SYNC_SET_LENGTH, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  168. return MPUbuffer[0];
  169. }
  170. /** Set external FSYNC configuration.
  171. * @see getExternalFrameSync()
  172. * @see MPU6050_RA_CONFIG
  173. * @param sync New FSYNC configuration value
  174. */
  175. void MPUsetExternalFrameSync(uint8_t sync) {
  176. I2CdevwriteBits(MPUdevAddr, MPU6050_RA_CONFIG, MPU6050_CFG_EXT_SYNC_SET_BIT, MPU6050_CFG_EXT_SYNC_SET_LENGTH, sync);
  177. }
  178. /** Get digital low-pass filter configuration.
  179. * The DLPF_CFG parameter sets the digital low pass filter configuration. It
  180. * also determines the internal sampling rate used by the device as shown in
  181. * the table below.
  182. *
  183. * Note: The accelerometer output rate is 1kHz. This means that for a Sample
  184. * Rate greater than 1kHz, the same accelerometer sample may be output to the
  185. * FIFO, DMP, and sensor registers more than once.
  186. *
  187. * <pre>
  188. * | ACCELEROMETER | GYROSCOPE
  189. * DLPF_CFG | Bandwidth | Delay | Bandwidth | Delay | Sample Rate
  190. * ---------+-----------+--------+-----------+--------+-------------
  191. * 0 | 260Hz | 0ms | 256Hz | 0.98ms | 8kHz
  192. * 1 | 184Hz | 2.0ms | 188Hz | 1.9ms | 1kHz
  193. * 2 | 94Hz | 3.0ms | 98Hz | 2.8ms | 1kHz
  194. * 3 | 44Hz | 4.9ms | 42Hz | 4.8ms | 1kHz
  195. * 4 | 21Hz | 8.5ms | 20Hz | 8.3ms | 1kHz
  196. * 5 | 10Hz | 13.8ms | 10Hz | 13.4ms | 1kHz
  197. * 6 | 5Hz | 19.0ms | 5Hz | 18.6ms | 1kHz
  198. * 7 | -- Reserved -- | -- Reserved -- | Reserved
  199. * </pre>
  200. *
  201. * @return DLFP configuration
  202. * @see MPU6050_RA_CONFIG
  203. * @see MPU6050_CFG_DLPF_CFG_BIT
  204. * @see MPU6050_CFG_DLPF_CFG_LENGTH
  205. */
  206. uint8_t MPUgetDLPFMode() {
  207. I2CdevreadBits(MPUdevAddr, MPU6050_RA_CONFIG, MPU6050_CFG_DLPF_CFG_BIT, MPU6050_CFG_DLPF_CFG_LENGTH, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  208. return MPUbuffer[0];
  209. }
  210. /** Set digital low-pass filter configuration.
  211. * @param mode New DLFP configuration setting
  212. * @see getDLPFBandwidth()
  213. * @see MPU6050_DLPF_BW_256
  214. * @see MPU6050_RA_CONFIG
  215. * @see MPU6050_CFG_DLPF_CFG_BIT
  216. * @see MPU6050_CFG_DLPF_CFG_LENGTH
  217. */
  218. void MPUsetDLPFMode(uint8_t mode) {
  219. I2CdevwriteBits(MPUdevAddr, MPU6050_RA_CONFIG, MPU6050_CFG_DLPF_CFG_BIT, MPU6050_CFG_DLPF_CFG_LENGTH, mode);
  220. }
  221. // GYRO_CONFIG register
  222. /** Get full-scale gyroscope range.
  223. * The FS_SEL parameter allows setting the full-scale range of the gyro sensors,
  224. * as described in the table below.
  225. *
  226. * <pre>
  227. * 0 = +/- 250 degrees/sec
  228. * 1 = +/- 500 degrees/sec
  229. * 2 = +/- 1000 degrees/sec
  230. * 3 = +/- 2000 degrees/sec
  231. * </pre>
  232. *
  233. * @return Current full-scale gyroscope range setting
  234. * @see MPU6050_GYRO_FS_250
  235. * @see MPU6050_RA_GYRO_CONFIG
  236. * @see MPU6050_GCONFIG_FS_SEL_BIT
  237. * @see MPU6050_GCONFIG_FS_SEL_LENGTH
  238. */
  239. uint8_t MPUgetFullScaleGyroRange() {
  240. I2CdevreadBits(MPUdevAddr, MPU6050_RA_GYRO_CONFIG, MPU6050_GCONFIG_FS_SEL_BIT, MPU6050_GCONFIG_FS_SEL_LENGTH, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  241. return MPUbuffer[0];
  242. }
  243. /** Set full-scale gyroscope range.
  244. * @param range New full-scale gyroscope range value
  245. * @see getFullScaleRange()
  246. * @see MPU6050_GYRO_FS_250
  247. * @see MPU6050_RA_GYRO_CONFIG
  248. * @see MPU6050_GCONFIG_FS_SEL_BIT
  249. * @see MPU6050_GCONFIG_FS_SEL_LENGTH
  250. */
  251. void MPUsetFullScaleGyroRange(uint8_t range) {
  252. I2CdevwriteBits(MPUdevAddr, MPU6050_RA_GYRO_CONFIG, MPU6050_GCONFIG_FS_SEL_BIT, MPU6050_GCONFIG_FS_SEL_LENGTH, range);
  253. }
  254. // ACCEL_CONFIG register
  255. /** Get self-test enabled setting for accelerometer X axis.
  256. * @return Self-test enabled value
  257. * @see MPU6050_RA_ACCEL_CONFIG
  258. */
  259. bool_t MPUgetAccelXSelfTest() {
  260. I2CdevreadBit(MPUdevAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_XA_ST_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  261. return MPUbuffer[0];
  262. }
  263. /** Get self-test enabled setting for accelerometer X axis.
  264. * @param enabled Self-test enabled value
  265. * @see MPU6050_RA_ACCEL_CONFIG
  266. */
  267. void MPUsetAccelXSelfTest(bool_t enabled) {
  268. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_XA_ST_BIT, enabled);
  269. }
  270. /** Get self-test enabled value for accelerometer Y axis.
  271. * @return Self-test enabled value
  272. * @see MPU6050_RA_ACCEL_CONFIG
  273. */
  274. bool_t MPUgetAccelYSelfTest() {
  275. I2CdevreadBit(MPUdevAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_YA_ST_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  276. return MPUbuffer[0];
  277. }
  278. /** Get self-test enabled value for accelerometer Y axis.
  279. * @param enabled Self-test enabled value
  280. * @see MPU6050_RA_ACCEL_CONFIG
  281. */
  282. void MPUsetAccelYSelfTest(bool_t enabled) {
  283. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_YA_ST_BIT, enabled);
  284. }
  285. /** Get self-test enabled value for accelerometer Z axis.
  286. * @return Self-test enabled value
  287. * @see MPU6050_RA_ACCEL_CONFIG
  288. */
  289. bool_t MPUgetAccelZSelfTest() {
  290. I2CdevreadBit(MPUdevAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ZA_ST_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  291. return MPUbuffer[0];
  292. }
  293. /** Set self-test enabled value for accelerometer Z axis.
  294. * @param enabled Self-test enabled value
  295. * @see MPU6050_RA_ACCEL_CONFIG
  296. */
  297. void MPUsetAccelZSelfTest(bool_t enabled) {
  298. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ZA_ST_BIT, enabled);
  299. }
  300. /** Get full-scale accelerometer range.
  301. * The FS_SEL parameter allows setting the full-scale range of the accelerometer
  302. * sensors, as described in the table below.
  303. *
  304. * <pre>
  305. * 0 = +/- 2g
  306. * 1 = +/- 4g
  307. * 2 = +/- 8g
  308. * 3 = +/- 16g
  309. * </pre>
  310. *
  311. * @return Current full-scale accelerometer range setting
  312. * @see MPU6050_ACCEL_FS_2
  313. * @see MPU6050_RA_ACCEL_CONFIG
  314. * @see MPU6050_ACONFIG_AFS_SEL_BIT
  315. * @see MPU6050_ACONFIG_AFS_SEL_LENGTH
  316. */
  317. uint8_t MPUgetFullScaleAccelRange() {
  318. I2CdevreadBits(MPUdevAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_AFS_SEL_BIT, MPU6050_ACONFIG_AFS_SEL_LENGTH, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  319. return MPUbuffer[0];
  320. }
  321. /** Set full-scale accelerometer range.
  322. * @param range New full-scale accelerometer range setting
  323. * @see getFullScaleAccelRange()
  324. */
  325. void MPUsetFullScaleAccelRange(uint8_t range) {
  326. I2CdevwriteBits(MPUdevAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_AFS_SEL_BIT, MPU6050_ACONFIG_AFS_SEL_LENGTH, range);
  327. }
  328. /** Get the high-pass filter configuration.
  329. * The DHPF is a filter module in the path leading to motion detectors (Free
  330. * Fall, Motion threshold, and Zero Motion). The high pass filter output is not
  331. * available to the data registers (see Figure in Section 8 of the MPU-6000/
  332. * MPU-6050 Product Specification document).
  333. *
  334. * The high pass filter has three modes:
  335. *
  336. * <pre>
  337. * Reset: The filter output settles to zero within one sample. This
  338. * effectively disables the high pass filter. This mode may be toggled
  339. * to quickly settle the filter.
  340. *
  341. * On: The high pass filter will pass signals above the cut off frequency.
  342. *
  343. * Hold: When triggered, the filter holds the present sample. The filter
  344. * output will be the difference between the input sample and the held
  345. * sample.
  346. * </pre>
  347. *
  348. * <pre>
  349. * ACCEL_HPF | Filter Mode | Cut-off Frequency
  350. * ----------+-------------+------------------
  351. * 0 | Reset | None
  352. * 1 | On | 5Hz
  353. * 2 | On | 2.5Hz
  354. * 3 | On | 1.25Hz
  355. * 4 | On | 0.63Hz
  356. * 7 | Hold | None
  357. * </pre>
  358. *
  359. * @return Current high-pass filter configuration
  360. * @see MPU6050_DHPF_RESET
  361. * @see MPU6050_RA_ACCEL_CONFIG
  362. */
  363. uint8_t MPUgetDHPFMode() {
  364. I2CdevreadBits(MPUdevAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ACCEL_HPF_BIT, MPU6050_ACONFIG_ACCEL_HPF_LENGTH, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  365. return MPUbuffer[0];
  366. }
  367. /** Set the high-pass filter configuration.
  368. * @param bandwidth New high-pass filter configuration
  369. * @see setDHPFMode()
  370. * @see MPU6050_DHPF_RESET
  371. * @see MPU6050_RA_ACCEL_CONFIG
  372. */
  373. void MPUsetDHPFMode(uint8_t bandwidth) {
  374. I2CdevwriteBits(MPUdevAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ACCEL_HPF_BIT, MPU6050_ACONFIG_ACCEL_HPF_LENGTH, bandwidth);
  375. }
  376. // FF_THR register
  377. /** Get free-fall event acceleration threshold.
  378. * This register configures the detection threshold for Free Fall event
  379. * detection. The unit of FF_THR is 1LSB = 2mg. Free Fall is detected when the
  380. * absolute value of the accelerometer measurements for the three axes are each
  381. * less than the detection threshold. This condition increments the Free Fall
  382. * duration counter (Register 30). The Free Fall interrupt is triggered when the
  383. * Free Fall duration counter reaches the time specified in FF_DUR.
  384. *
  385. * For more details on the Free Fall detection interrupt, see Section 8.2 of the
  386. * MPU-6000/MPU-6050 Product Specification document as well as Registers 56 and
  387. * 58 of this document.
  388. *
  389. * @return Current free-fall acceleration threshold value (LSB = 2mg)
  390. * @see MPU6050_RA_FF_THR
  391. */
  392. uint8_t MPUgetFreefallDetectionThreshold() {
  393. I2CdevreadByte(MPUdevAddr, MPU6050_RA_FF_THR, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  394. return MPUbuffer[0];
  395. }
  396. /** Get free-fall event acceleration threshold.
  397. * @param threshold New free-fall acceleration threshold value (LSB = 2mg)
  398. * @see getFreefallDetectionThreshold()
  399. * @see MPU6050_RA_FF_THR
  400. */
  401. void MPUsetFreefallDetectionThreshold(uint8_t threshold) {
  402. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_FF_THR, threshold);
  403. }
  404. // FF_DUR register
  405. /** Get free-fall event duration threshold.
  406. * This register configures the duration counter threshold for Free Fall event
  407. * detection. The duration counter ticks at 1kHz, therefore FF_DUR has a unit
  408. * of 1 LSB = 1 ms.
  409. *
  410. * The Free Fall duration counter increments while the absolute value of the
  411. * accelerometer measurements are each less than the detection threshold
  412. * (Register 29). The Free Fall interrupt is triggered when the Free Fall
  413. * duration counter reaches the time specified in this register.
  414. *
  415. * For more details on the Free Fall detection interrupt, see Section 8.2 of
  416. * the MPU-6000/MPU-6050 Product Specification document as well as Registers 56
  417. * and 58 of this document.
  418. *
  419. * @return Current free-fall duration threshold value (LSB = 1ms)
  420. * @see MPU6050_RA_FF_DUR
  421. */
  422. uint8_t MPUgetFreefallDetectionDuration() {
  423. I2CdevreadByte(MPUdevAddr, MPU6050_RA_FF_DUR, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  424. return MPUbuffer[0];
  425. }
  426. /** Get free-fall event duration threshold.
  427. * @param duration New free-fall duration threshold value (LSB = 1ms)
  428. * @see getFreefallDetectionDuration()
  429. * @see MPU6050_RA_FF_DUR
  430. */
  431. void MPUsetFreefallDetectionDuration(uint8_t duration) {
  432. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_FF_DUR, duration);
  433. }
  434. // MOT_THR register
  435. /** Get motion detection event acceleration threshold.
  436. * This register configures the detection threshold for Motion interrupt
  437. * generation. The unit of MOT_THR is 1LSB = 2mg. Motion is detected when the
  438. * absolute value of any of the accelerometer measurements exceeds this Motion
  439. * detection threshold. This condition increments the Motion detection duration
  440. * counter (Register 32). The Motion detection interrupt is triggered when the
  441. * Motion Detection counter reaches the time count specified in MOT_DUR
  442. * (Register 32).
  443. *
  444. * The Motion interrupt will indicate the axis and polarity of detected motion
  445. * in MOT_DETECT_STATUS (Register 97).
  446. *
  447. * For more details on the Motion detection interrupt, see Section 8.3 of the
  448. * MPU-6000/MPU-6050 Product Specification document as well as Registers 56 and
  449. * 58 of this document.
  450. *
  451. * @return Current motion detection acceleration threshold value (LSB = 2mg)
  452. * @see MPU6050_RA_MOT_THR
  453. */
  454. uint8_t MPUgetMotionDetectionThreshold() {
  455. I2CdevreadByte(MPUdevAddr, MPU6050_RA_MOT_THR, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  456. return MPUbuffer[0];
  457. }
  458. /** Set free-fall event acceleration threshold.
  459. * @param threshold New motion detection acceleration threshold value (LSB = 2mg)
  460. * @see getMotionDetectionThreshold()
  461. * @see MPU6050_RA_MOT_THR
  462. */
  463. void MPUsetMotionDetectionThreshold(uint8_t threshold) {
  464. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_MOT_THR, threshold);
  465. }
  466. // MOT_DUR register
  467. /** Get motion detection event duration threshold.
  468. * This register configures the duration counter threshold for Motion interrupt
  469. * generation. The duration counter ticks at 1 kHz, therefore MOT_DUR has a unit
  470. * of 1LSB = 1ms. The Motion detection duration counter increments when the
  471. * absolute value of any of the accelerometer measurements exceeds the Motion
  472. * detection threshold (Register 31). The Motion detection interrupt is
  473. * triggered when the Motion detection counter reaches the time count specified
  474. * in this register.
  475. *
  476. * For more details on the Motion detection interrupt, see Section 8.3 of the
  477. * MPU-6000/MPU-6050 Product Specification document.
  478. *
  479. * @return Current motion detection duration threshold value (LSB = 1ms)
  480. * @see MPU6050_RA_MOT_DUR
  481. */
  482. uint8_t MPUgetMotionDetectionDuration() {
  483. I2CdevreadByte(MPUdevAddr, MPU6050_RA_MOT_DUR, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  484. return MPUbuffer[0];
  485. }
  486. /** Set motion detection event duration threshold.
  487. * @param duration New motion detection duration threshold value (LSB = 1ms)
  488. * @see getMotionDetectionDuration()
  489. * @see MPU6050_RA_MOT_DUR
  490. */
  491. void MPUsetMotionDetectionDuration(uint8_t duration) {
  492. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_MOT_DUR, duration);
  493. }
  494. // ZRMOT_THR register
  495. /** Get zero motion detection event acceleration threshold.
  496. * This register configures the detection threshold for Zero Motion interrupt
  497. * generation. The unit of ZRMOT_THR is 1LSB = 2mg. Zero Motion is detected when
  498. * the absolute value of the accelerometer measurements for the 3 axes are each
  499. * less than the detection threshold. This condition increments the Zero Motion
  500. * duration counter (Register 34). The Zero Motion interrupt is triggered when
  501. * the Zero Motion duration counter reaches the time count specified in
  502. * ZRMOT_DUR (Register 34).
  503. *
  504. * Unlike Free Fall or Motion detection, Zero Motion detection triggers an
  505. * interrupt both when Zero Motion is first detected and when Zero Motion is no
  506. * longer detected.
  507. *
  508. * When a zero motion event is detected, a Zero Motion Status will be indicated
  509. * in the MOT_DETECT_STATUS register (Register 97). When a motion-to-zero-motion
  510. * condition is detected, the status bit is set to 1. When a zero-motion-to-
  511. * motion condition is detected, the status bit is set to 0.
  512. *
  513. * For more details on the Zero Motion detection interrupt, see Section 8.4 of
  514. * the MPU-6000/MPU-6050 Product Specification document as well as Registers 56
  515. * and 58 of this document.
  516. *
  517. * @return Current zero motion detection acceleration threshold value (LSB = 2mg)
  518. * @see MPU6050_RA_ZRMOT_THR
  519. */
  520. uint8_t MPUgetZeroMotionDetectionThreshold() {
  521. I2CdevreadByte(MPUdevAddr, MPU6050_RA_ZRMOT_THR, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  522. return MPUbuffer[0];
  523. }
  524. /** Set zero motion detection event acceleration threshold.
  525. * @param threshold New zero motion detection acceleration threshold value (LSB = 2mg)
  526. * @see getZeroMotionDetectionThreshold()
  527. * @see MPU6050_RA_ZRMOT_THR
  528. */
  529. void MPUsetZeroMotionDetectionThreshold(uint8_t threshold) {
  530. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_ZRMOT_THR, threshold);
  531. }
  532. // ZRMOT_DUR register
  533. /** Get zero motion detection event duration threshold.
  534. * This register configures the duration counter threshold for Zero Motion
  535. * interrupt generation. The duration counter ticks at 16 Hz, therefore
  536. * ZRMOT_DUR has a unit of 1 LSB = 64 ms. The Zero Motion duration counter
  537. * increments while the absolute value of the accelerometer measurements are
  538. * each less than the detection threshold (Register 33). The Zero Motion
  539. * interrupt is triggered when the Zero Motion duration counter reaches the time
  540. * count specified in this register.
  541. *
  542. * For more details on the Zero Motion detection interrupt, see Section 8.4 of
  543. * the MPU-6000/MPU-6050 Product Specification document, as well as Registers 56
  544. * and 58 of this document.
  545. *
  546. * @return Current zero motion detection duration threshold value (LSB = 64ms)
  547. * @see MPU6050_RA_ZRMOT_DUR
  548. */
  549. uint8_t MPUgetZeroMotionDetectionDuration() {
  550. I2CdevreadByte(MPUdevAddr, MPU6050_RA_ZRMOT_DUR, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  551. return MPUbuffer[0];
  552. }
  553. /** Set zero motion detection event duration threshold.
  554. * @param duration New zero motion detection duration threshold value (LSB = 1ms)
  555. * @see getZeroMotionDetectionDuration()
  556. * @see MPU6050_RA_ZRMOT_DUR
  557. */
  558. void MPUsetZeroMotionDetectionDuration(uint8_t duration) {
  559. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_ZRMOT_DUR, duration);
  560. }
  561. // FIFO_EN register
  562. /** Get temperature FIFO enabled value.
  563. * When set to 1, this bit enables TEMP_OUT_H and TEMP_OUT_L (Registers 65 and
  564. * 66) to be written into the FIFO buffer.
  565. * @return Current temperature FIFO enabled value
  566. * @see MPU6050_RA_FIFO_EN
  567. */
  568. bool_t MPUgetTempFIFOEnabled() {
  569. I2CdevreadBit(MPUdevAddr, MPU6050_RA_FIFO_EN, MPU6050_TEMP_FIFO_EN_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  570. return MPUbuffer[0];
  571. }
  572. /** Set temperature FIFO enabled value.
  573. * @param enabled New temperature FIFO enabled value
  574. * @see getTempFIFOEnabled()
  575. * @see MPU6050_RA_FIFO_EN
  576. */
  577. void MPUsetTempFIFOEnabled(bool_t enabled) {
  578. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_FIFO_EN, MPU6050_TEMP_FIFO_EN_BIT, enabled);
  579. }
  580. /** Get gyroscope X-axis FIFO enabled value.
  581. * When set to 1, this bit enables GYRO_XOUT_H and GYRO_XOUT_L (Registers 67 and
  582. * 68) to be written into the FIFO buffer.
  583. * @return Current gyroscope X-axis FIFO enabled value
  584. * @see MPU6050_RA_FIFO_EN
  585. */
  586. bool_t MPUgetXGyroFIFOEnabled() {
  587. I2CdevreadBit(MPUdevAddr, MPU6050_RA_FIFO_EN, MPU6050_XG_FIFO_EN_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  588. return MPUbuffer[0];
  589. }
  590. /** Set gyroscope X-axis FIFO enabled value.
  591. * @param enabled New gyroscope X-axis FIFO enabled value
  592. * @see getXGyroFIFOEnabled()
  593. * @see MPU6050_RA_FIFO_EN
  594. */
  595. void MPUsetXGyroFIFOEnabled(bool_t enabled) {
  596. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_FIFO_EN, MPU6050_XG_FIFO_EN_BIT, enabled);
  597. }
  598. /** Get gyroscope Y-axis FIFO enabled value.
  599. * When set to 1, this bit enables GYRO_YOUT_H and GYRO_YOUT_L (Registers 69 and
  600. * 70) to be written into the FIFO buffer.
  601. * @return Current gyroscope Y-axis FIFO enabled value
  602. * @see MPU6050_RA_FIFO_EN
  603. */
  604. bool_t MPUgetYGyroFIFOEnabled() {
  605. I2CdevreadBit(MPUdevAddr, MPU6050_RA_FIFO_EN, MPU6050_YG_FIFO_EN_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  606. return MPUbuffer[0];
  607. }
  608. /** Set gyroscope Y-axis FIFO enabled value.
  609. * @param enabled New gyroscope Y-axis FIFO enabled value
  610. * @see getYGyroFIFOEnabled()
  611. * @see MPU6050_RA_FIFO_EN
  612. */
  613. void MPUsetYGyroFIFOEnabled(bool_t enabled) {
  614. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_FIFO_EN, MPU6050_YG_FIFO_EN_BIT, enabled);
  615. }
  616. /** Get gyroscope Z-axis FIFO enabled value.
  617. * When set to 1, this bit enables GYRO_ZOUT_H and GYRO_ZOUT_L (Registers 71 and
  618. * 72) to be written into the FIFO buffer.
  619. * @return Current gyroscope Z-axis FIFO enabled value
  620. * @see MPU6050_RA_FIFO_EN
  621. */
  622. bool_t MPUgetZGyroFIFOEnabled() {
  623. I2CdevreadBit(MPUdevAddr, MPU6050_RA_FIFO_EN, MPU6050_ZG_FIFO_EN_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  624. return MPUbuffer[0];
  625. }
  626. /** Set gyroscope Z-axis FIFO enabled value.
  627. * @param enabled New gyroscope Z-axis FIFO enabled value
  628. * @see getZGyroFIFOEnabled()
  629. * @see MPU6050_RA_FIFO_EN
  630. */
  631. void MPUsetZGyroFIFOEnabled(bool_t enabled) {
  632. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_FIFO_EN, MPU6050_ZG_FIFO_EN_BIT, enabled);
  633. }
  634. /** Get accelerometer FIFO enabled value.
  635. * When set to 1, this bit enables ACCEL_XOUT_H, ACCEL_XOUT_L, ACCEL_YOUT_H,
  636. * ACCEL_YOUT_L, ACCEL_ZOUT_H, and ACCEL_ZOUT_L (Registers 59 to 64) to be
  637. * written into the FIFO buffer.
  638. * @return Current accelerometer FIFO enabled value
  639. * @see MPU6050_RA_FIFO_EN
  640. */
  641. bool_t MPUgetAccelFIFOEnabled() {
  642. I2CdevreadBit(MPUdevAddr, MPU6050_RA_FIFO_EN, MPU6050_ACCEL_FIFO_EN_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  643. return MPUbuffer[0];
  644. }
  645. /** Set accelerometer FIFO enabled value.
  646. * @param enabled New accelerometer FIFO enabled value
  647. * @see getAccelFIFOEnabled()
  648. * @see MPU6050_RA_FIFO_EN
  649. */
  650. void MPUsetAccelFIFOEnabled(bool_t enabled) {
  651. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_FIFO_EN, MPU6050_ACCEL_FIFO_EN_BIT, enabled);
  652. }
  653. /** Get Slave 2 FIFO enabled value.
  654. * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96)
  655. * associated with Slave 2 to be written into the FIFO buffer.
  656. * @return Current Slave 2 FIFO enabled value
  657. * @see MPU6050_RA_FIFO_EN
  658. */
  659. bool_t MPUgetSlave2FIFOEnabled() {
  660. I2CdevreadBit(MPUdevAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV2_FIFO_EN_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  661. return MPUbuffer[0];
  662. }
  663. /** Set Slave 2 FIFO enabled value.
  664. * @param enabled New Slave 2 FIFO enabled value
  665. * @see getSlave2FIFOEnabled()
  666. * @see MPU6050_RA_FIFO_EN
  667. */
  668. void MPUsetSlave2FIFOEnabled(bool_t enabled) {
  669. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV2_FIFO_EN_BIT, enabled);
  670. }
  671. /** Get Slave 1 FIFO enabled value.
  672. * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96)
  673. * associated with Slave 1 to be written into the FIFO buffer.
  674. * @return Current Slave 1 FIFO enabled value
  675. * @see MPU6050_RA_FIFO_EN
  676. */
  677. bool_t MPUgetSlave1FIFOEnabled() {
  678. I2CdevreadBit(MPUdevAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV1_FIFO_EN_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  679. return MPUbuffer[0];
  680. }
  681. /** Set Slave 1 FIFO enabled value.
  682. * @param enabled New Slave 1 FIFO enabled value
  683. * @see getSlave1FIFOEnabled()
  684. * @see MPU6050_RA_FIFO_EN
  685. */
  686. void MPUsetSlave1FIFOEnabled(bool_t enabled) {
  687. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV1_FIFO_EN_BIT, enabled);
  688. }
  689. /** Get Slave 0 FIFO enabled value.
  690. * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96)
  691. * associated with Slave 0 to be written into the FIFO buffer.
  692. * @return Current Slave 0 FIFO enabled value
  693. * @see MPU6050_RA_FIFO_EN
  694. */
  695. bool_t MPUgetSlave0FIFOEnabled() {
  696. I2CdevreadBit(MPUdevAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV0_FIFO_EN_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  697. return MPUbuffer[0];
  698. }
  699. /** Set Slave 0 FIFO enabled value.
  700. * @param enabled New Slave 0 FIFO enabled value
  701. * @see getSlave0FIFOEnabled()
  702. * @see MPU6050_RA_FIFO_EN
  703. */
  704. void MPUsetSlave0FIFOEnabled(bool_t enabled) {
  705. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV0_FIFO_EN_BIT, enabled);
  706. }
  707. // I2C_MST_CTRL register
  708. /** Get multi-master enabled value.
  709. * Multi-master capability allows multiple I2C masters to operate on the same
  710. * bus. In circuits where multi-master capability is required, set MULT_MST_EN
  711. * to 1. This will increase current drawn by approximately 30uA.
  712. *
  713. * In circuits where multi-master capability is required, the state of the I2C
  714. * bus must always be monitored by each separate I2C Master. Before an I2C
  715. * Master can assume arbitration of the bus, it must first confirm that no other
  716. * I2C Master has arbitration of the bus. When MULT_MST_EN is set to 1, the
  717. * MPU-60X0's bus arbitration detection logic is turned on, enabling it to
  718. * detect when the bus is available.
  719. *
  720. * @return Current multi-master enabled value
  721. * @see MPU6050_RA_I2C_MST_CTRL
  722. */
  723. bool_t MPUgetMultiMasterEnabled() {
  724. I2CdevreadBit(MPUdevAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_MULT_MST_EN_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  725. return MPUbuffer[0];
  726. }
  727. /** Set multi-master enabled value.
  728. * @param enabled New multi-master enabled value
  729. * @see getMultiMasterEnabled()
  730. * @see MPU6050_RA_I2C_MST_CTRL
  731. */
  732. void MPUsetMultiMasterEnabled(bool_t enabled) {
  733. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_MULT_MST_EN_BIT, enabled);
  734. }
  735. /** Get wait-for-external-sensor-data enabled value.
  736. * When the WAIT_FOR_ES bit is set to 1, the Data Ready interrupt will be
  737. * delayed until External Sensor data from the Slave Devices are loaded into the
  738. * EXT_SENS_DATA registers. This is used to ensure that both the internal sensor
  739. * data (i.e. from gyro and accel) and external sensor data have been loaded to
  740. * their respective data registers (i.e. the data is synced) when the Data Ready
  741. * interrupt is triggered.
  742. *
  743. * @return Current wait-for-external-sensor-data enabled value
  744. * @see MPU6050_RA_I2C_MST_CTRL
  745. */
  746. bool_t MPUgetWaitForExternalSensorEnabled() {
  747. I2CdevreadBit(MPUdevAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_WAIT_FOR_ES_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  748. return MPUbuffer[0];
  749. }
  750. /** Set wait-for-external-sensor-data enabled value.
  751. * @param enabled New wait-for-external-sensor-data enabled value
  752. * @see getWaitForExternalSensorEnabled()
  753. * @see MPU6050_RA_I2C_MST_CTRL
  754. */
  755. void MPUsetWaitForExternalSensorEnabled(bool_t enabled) {
  756. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_WAIT_FOR_ES_BIT, enabled);
  757. }
  758. /** Get Slave 3 FIFO enabled value.
  759. * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96)
  760. * associated with Slave 3 to be written into the FIFO buffer.
  761. * @return Current Slave 3 FIFO enabled value
  762. * @see MPU6050_RA_MST_CTRL
  763. */
  764. bool_t MPUgetSlave3FIFOEnabled() {
  765. I2CdevreadBit(MPUdevAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_SLV_3_FIFO_EN_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  766. return MPUbuffer[0];
  767. }
  768. /** Set Slave 3 FIFO enabled value.
  769. * @param enabled New Slave 3 FIFO enabled value
  770. * @see getSlave3FIFOEnabled()
  771. * @see MPU6050_RA_MST_CTRL
  772. */
  773. void MPUsetSlave3FIFOEnabled(bool_t enabled) {
  774. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_SLV_3_FIFO_EN_BIT, enabled);
  775. }
  776. /** Get slave read/write transition enabled value.
  777. * The I2C_MST_P_NSR bit configures the I2C Master's transition from one slave
  778. * read to the next slave read. If the bit equals 0, there will be a restart
  779. * between reads. If the bit equals 1, there will be a stop followed by a start
  780. * of the following read. When a write transaction follows a read transaction,
  781. * the stop followed by a start of the successive write will be always used.
  782. *
  783. * @return Current slave read/write transition enabled value
  784. * @see MPU6050_RA_I2C_MST_CTRL
  785. */
  786. bool_t MPUgetSlaveReadWriteTransitionEnabled() {
  787. I2CdevreadBit(MPUdevAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_P_NSR_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  788. return MPUbuffer[0];
  789. }
  790. /** Set slave read/write transition enabled value.
  791. * @param enabled New slave read/write transition enabled value
  792. * @see getSlaveReadWriteTransitionEnabled()
  793. * @see MPU6050_RA_I2C_MST_CTRL
  794. */
  795. void MPUsetSlaveReadWriteTransitionEnabled(bool_t enabled) {
  796. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_P_NSR_BIT, enabled);
  797. }
  798. /** Get I2C master clock speed.
  799. * I2C_MST_CLK is a 4 bit unsigned value which configures a divider on the
  800. * MPU-60X0 internal 8MHz clock. It sets the I2C master clock speed according to
  801. * the following table:
  802. *
  803. * <pre>
  804. * I2C_MST_CLK | I2C Master Clock Speed | 8MHz Clock Divider
  805. * ------------+------------------------+-------------------
  806. * 0 | 348kHz | 23
  807. * 1 | 333kHz | 24
  808. * 2 | 320kHz | 25
  809. * 3 | 308kHz | 26
  810. * 4 | 296kHz | 27
  811. * 5 | 286kHz | 28
  812. * 6 | 276kHz | 29
  813. * 7 | 267kHz | 30
  814. * 8 | 258kHz | 31
  815. * 9 | 500kHz | 16
  816. * 10 | 471kHz | 17
  817. * 11 | 444kHz | 18
  818. * 12 | 421kHz | 19
  819. * 13 | 400kHz | 20
  820. * 14 | 381kHz | 21
  821. * 15 | 364kHz | 22
  822. * </pre>
  823. *
  824. * @return Current I2C master clock speed
  825. * @see MPU6050_RA_I2C_MST_CTRL
  826. */
  827. uint8_t MPUgetMasterClockSpeed() {
  828. I2CdevreadBits(MPUdevAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_CLK_BIT, MPU6050_I2C_MST_CLK_LENGTH, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  829. return MPUbuffer[0];
  830. }
  831. /** Set I2C master clock speed.
  832. * @reparam speed Current I2C master clock speed
  833. * @see MPU6050_RA_I2C_MST_CTRL
  834. */
  835. void MPUsetMasterClockSpeed(uint8_t speed) {
  836. I2CdevwriteBits(MPUdevAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_CLK_BIT, MPU6050_I2C_MST_CLK_LENGTH, speed);
  837. }
  838. // I2C_SLV* registers (Slave 0-3)
  839. /** Get the I2C address of the specified slave (0-3).
  840. * Note that Bit 7 (MSB) controls read/write mode. If Bit 7 is set, it's a read
  841. * operation, and if it is cleared, then it's a write operation. The remaining
  842. * bits (6-0) are the 7-bit device address of the slave device.
  843. *
  844. * In read mode, the result of the read is placed in the lowest available
  845. * EXT_SENS_DATA register. For further information regarding the allocation of
  846. * read results, please refer to the EXT_SENS_DATA register description
  847. * (Registers 73 - 96).
  848. *
  849. * The MPU-6050 supports a total of five slaves, but Slave 4 has unique
  850. * characteristics, and so it has its own functions (getSlave4* and setSlave4*).
  851. *
  852. * I2C data transactions are performed at the Sample Rate, as defined in
  853. * Register 25. The user is responsible for ensuring that I2C data transactions
  854. * to and from each enabled Slave can be completed within a single period of the
  855. * Sample Rate.
  856. *
  857. * The I2C slave access rate can be reduced relative to the Sample Rate. This
  858. * reduced access rate is determined by I2C_MST_DLY (Register 52). Whether a
  859. * slave's access rate is reduced relative to the Sample Rate is determined by
  860. * I2C_MST_DELAY_CTRL (Register 103).
  861. *
  862. * The processing order for the slaves is fixed. The sequence followed for
  863. * processing the slaves is Slave 0, Slave 1, Slave 2, Slave 3 and Slave 4. If a
  864. * particular Slave is disabled it will be skipped.
  865. *
  866. * Each slave can either be accessed at the sample rate or at a reduced sample
  867. * rate. In a case where some slaves are accessed at the Sample Rate and some
  868. * slaves are accessed at the reduced rate, the sequence of accessing the slaves
  869. * (Slave 0 to Slave 4) is still followed. However, the reduced rate slaves will
  870. * be skipped if their access rate dictates that they should not be accessed
  871. * during that particular cycle. For further information regarding the reduced
  872. * access rate, please refer to Register 52. Whether a slave is accessed at the
  873. * Sample Rate or at the reduced rate is determined by the Delay Enable bits in
  874. * Register 103.
  875. *
  876. * @param num Slave number (0-3)
  877. * @return Current address for specified slave
  878. * @see MPU6050_RA_I2C_SLV0_ADDR
  879. */
  880. uint8_t MPUgetSlaveAddress(uint8_t num) {
  881. if (num > 3) return 0;
  882. I2CdevreadByte(MPUdevAddr, MPU6050_RA_I2C_SLV0_ADDR + num*3, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  883. return MPUbuffer[0];
  884. }
  885. /** Set the I2C address of the specified slave (0-3).
  886. * @param num Slave number (0-3)
  887. * @param address New address for specified slave
  888. * @see getSlaveAddress()
  889. * @see MPU6050_RA_I2C_SLV0_ADDR
  890. */
  891. void MPUsetSlaveAddress(uint8_t num, uint8_t address) {
  892. if (num > 3) return;
  893. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_I2C_SLV0_ADDR + num*3, address);
  894. }
  895. /** Get the active internal register for the specified slave (0-3).
  896. * Read/write operations for this slave will be done to whatever internal
  897. * register address is stored in this MPU register.
  898. *
  899. * The MPU-6050 supports a total of five slaves, but Slave 4 has unique
  900. * characteristics, and so it has its own functions.
  901. *
  902. * @param num Slave number (0-3)
  903. * @return Current active register for specified slave
  904. * @see MPU6050_RA_I2C_SLV0_REG
  905. */
  906. uint8_t MPUgetSlaveRegister(uint8_t num) {
  907. if (num > 3) return 0;
  908. I2CdevreadByte(MPUdevAddr, MPU6050_RA_I2C_SLV0_REG + num*3, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  909. return MPUbuffer[0];
  910. }
  911. /** Set the active internal register for the specified slave (0-3).
  912. * @param num Slave number (0-3)
  913. * @param reg New active register for specified slave
  914. * @see getSlaveRegister()
  915. * @see MPU6050_RA_I2C_SLV0_REG
  916. */
  917. void MPUsetSlaveRegister(uint8_t num, uint8_t reg) {
  918. if (num > 3) return;
  919. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_I2C_SLV0_REG + num*3, reg);
  920. }
  921. /** Get the enabled value for the specified slave (0-3).
  922. * When set to 1, this bit enables Slave 0 for data transfer operations. When
  923. * cleared to 0, this bit disables Slave 0 from data transfer operations.
  924. * @param num Slave number (0-3)
  925. * @return Current enabled value for specified slave
  926. * @see MPU6050_RA_I2C_SLV0_CTRL
  927. */
  928. bool_t MPUgetSlaveEnabled(uint8_t num) {
  929. if (num > 3) return 0;
  930. I2CdevreadBit(MPUdevAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_EN_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  931. return MPUbuffer[0];
  932. }
  933. /** Set the enabled value for the specified slave (0-3).
  934. * @param num Slave number (0-3)
  935. * @param enabled New enabled value for specified slave
  936. * @see getSlaveEnabled()
  937. * @see MPU6050_RA_I2C_SLV0_CTRL
  938. */
  939. void MPUsetSlaveEnabled(uint8_t num, bool_t enabled) {
  940. if (num > 3) return;
  941. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_EN_BIT, enabled);
  942. }
  943. /** Get word pair byte-swapping enabled for the specified slave (0-3).
  944. * When set to 1, this bit enables byte swapping. When byte swapping is enabled,
  945. * the high and low bytes of a word pair are swapped. Please refer to
  946. * I2C_SLV0_GRP for the pairing convention of the word pairs. When cleared to 0,
  947. * bytes transferred to and from Slave 0 will be written to EXT_SENS_DATA
  948. * registers in the order they were transferred.
  949. *
  950. * @param num Slave number (0-3)
  951. * @return Current word pair byte-swapping enabled value for specified slave
  952. * @see MPU6050_RA_I2C_SLV0_CTRL
  953. */
  954. bool_t MPUgetSlaveWordByteSwap(uint8_t num) {
  955. if (num > 3) return 0;
  956. I2CdevreadBit(MPUdevAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_BYTE_SW_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  957. return MPUbuffer[0];
  958. }
  959. /** Set word pair byte-swapping enabled for the specified slave (0-3).
  960. * @param num Slave number (0-3)
  961. * @param enabled New word pair byte-swapping enabled value for specified slave
  962. * @see getSlaveWordByteSwap()
  963. * @see MPU6050_RA_I2C_SLV0_CTRL
  964. */
  965. void MPUsetSlaveWordByteSwap(uint8_t num, bool_t enabled) {
  966. if (num > 3) return;
  967. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_BYTE_SW_BIT, enabled);
  968. }
  969. /** Get write mode for the specified slave (0-3).
  970. * When set to 1, the transaction will read or write data only. When cleared to
  971. * 0, the transaction will write a register address prior to reading or writing
  972. * data. This should equal 0 when specifying the register address within the
  973. * Slave device to/from which the ensuing data transaction will take place.
  974. *
  975. * @param num Slave number (0-3)
  976. * @return Current write mode for specified slave (0 = register address + data, 1 = data only)
  977. * @see MPU6050_RA_I2C_SLV0_CTRL
  978. */
  979. bool_t MPUgetSlaveWriteMode(uint8_t num) {
  980. if (num > 3) return 0;
  981. I2CdevreadBit(MPUdevAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_REG_DIS_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  982. return MPUbuffer[0];
  983. }
  984. /** Set write mode for the specified slave (0-3).
  985. * @param num Slave number (0-3)
  986. * @param mode New write mode for specified slave (0 = register address + data, 1 = data only)
  987. * @see getSlaveWriteMode()
  988. * @see MPU6050_RA_I2C_SLV0_CTRL
  989. */
  990. void MPUsetSlaveWriteMode(uint8_t num, bool_t mode) {
  991. if (num > 3) return;
  992. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_REG_DIS_BIT, mode);
  993. }
  994. /** Get word pair grouping order offset for the specified slave (0-3).
  995. * This sets specifies the grouping order of word pairs received from registers.
  996. * When cleared to 0, bytes from register addresses 0 and 1, 2 and 3, etc (even,
  997. * then odd register addresses) are paired to form a word. When set to 1, bytes
  998. * from register addresses are paired 1 and 2, 3 and 4, etc. (odd, then even
  999. * register addresses) are paired to form a word.
  1000. *
  1001. * @param num Slave number (0-3)
  1002. * @return Current word pair grouping order offset for specified slave
  1003. * @see MPU6050_RA_I2C_SLV0_CTRL
  1004. */
  1005. bool_t MPUgetSlaveWordGroupOffset(uint8_t num) {
  1006. if (num > 3) return 0;
  1007. I2CdevreadBit(MPUdevAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_GRP_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1008. return MPUbuffer[0];
  1009. }
  1010. /** Set word pair grouping order offset for the specified slave (0-3).
  1011. * @param num Slave number (0-3)
  1012. * @param enabled New word pair grouping order offset for specified slave
  1013. * @see getSlaveWordGroupOffset()
  1014. * @see MPU6050_RA_I2C_SLV0_CTRL
  1015. */
  1016. void MPUsetSlaveWordGroupOffset(uint8_t num, bool_t enabled) {
  1017. if (num > 3) return;
  1018. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_GRP_BIT, enabled);
  1019. }
  1020. /** Get number of bytes to read for the specified slave (0-3).
  1021. * Specifies the number of bytes transferred to and from Slave 0. Clearing this
  1022. * bit to 0 is equivalent to disabling the register by writing 0 to I2C_SLV0_EN.
  1023. * @param num Slave number (0-3)
  1024. * @return Number of bytes to read for specified slave
  1025. * @see MPU6050_RA_I2C_SLV0_CTRL
  1026. */
  1027. uint8_t MPUgetSlaveDataLength(uint8_t num) {
  1028. if (num > 3) return 0;
  1029. I2CdevreadBits(MPUdevAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_LEN_BIT, MPU6050_I2C_SLV_LEN_LENGTH, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1030. return MPUbuffer[0];
  1031. }
  1032. /** Set number of bytes to read for the specified slave (0-3).
  1033. * @param num Slave number (0-3)
  1034. * @param length Number of bytes to read for specified slave
  1035. * @see getSlaveDataLength()
  1036. * @see MPU6050_RA_I2C_SLV0_CTRL
  1037. */
  1038. void MPUsetSlaveDataLength(uint8_t num, uint8_t length) {
  1039. if (num > 3) return;
  1040. I2CdevwriteBits(MPUdevAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_LEN_BIT, MPU6050_I2C_SLV_LEN_LENGTH, length);
  1041. }
  1042. // I2C_SLV* registers (Slave 4)
  1043. /** Get the I2C address of Slave 4.
  1044. * Note that Bit 7 (MSB) controls read/write mode. If Bit 7 is set, it's a read
  1045. * operation, and if it is cleared, then it's a write operation. The remaining
  1046. * bits (6-0) are the 7-bit device address of the slave device.
  1047. *
  1048. * @return Current address for Slave 4
  1049. * @see getSlaveAddress()
  1050. * @see MPU6050_RA_I2C_SLV4_ADDR
  1051. */
  1052. uint8_t MPUgetSlave4Address() {
  1053. I2CdevreadByte(MPUdevAddr, MPU6050_RA_I2C_SLV4_ADDR, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1054. return MPUbuffer[0];
  1055. }
  1056. /** Set the I2C address of Slave 4.
  1057. * @param address New address for Slave 4
  1058. * @see getSlave4Address()
  1059. * @see MPU6050_RA_I2C_SLV4_ADDR
  1060. */
  1061. void MPUsetSlave4Address(uint8_t address) {
  1062. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_I2C_SLV4_ADDR, address);
  1063. }
  1064. /** Get the active internal register for the Slave 4.
  1065. * Read/write operations for this slave will be done to whatever internal
  1066. * register address is stored in this MPU register.
  1067. *
  1068. * @return Current active register for Slave 4
  1069. * @see MPU6050_RA_I2C_SLV4_REG
  1070. */
  1071. uint8_t MPUgetSlave4Register() {
  1072. I2CdevreadByte(MPUdevAddr, MPU6050_RA_I2C_SLV4_REG, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1073. return MPUbuffer[0];
  1074. }
  1075. /** Set the active internal register for Slave 4.
  1076. * @param reg New active register for Slave 4
  1077. * @see getSlave4Register()
  1078. * @see MPU6050_RA_I2C_SLV4_REG
  1079. */
  1080. void MPUsetSlave4Register(uint8_t reg) {
  1081. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_I2C_SLV4_REG, reg);
  1082. }
  1083. /** Set new byte to write to Slave 4.
  1084. * This register stores the data to be written into the Slave 4. If I2C_SLV4_RW
  1085. * is set 1 (set to read), this register has no effect.
  1086. * @param data New byte to write to Slave 4
  1087. * @see MPU6050_RA_I2C_SLV4_DO
  1088. */
  1089. void MPUsetSlave4OutputByte(uint8_t data) {
  1090. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_I2C_SLV4_DO, data);
  1091. }
  1092. /** Get the enabled value for the Slave 4.
  1093. * When set to 1, this bit enables Slave 4 for data transfer operations. When
  1094. * cleared to 0, this bit disables Slave 4 from data transfer operations.
  1095. * @return Current enabled value for Slave 4
  1096. * @see MPU6050_RA_I2C_SLV4_CTRL
  1097. */
  1098. bool_t MPUgetSlave4Enabled() {
  1099. I2CdevreadBit(MPUdevAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_EN_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1100. return MPUbuffer[0];
  1101. }
  1102. /** Set the enabled value for Slave 4.
  1103. * @param enabled New enabled value for Slave 4
  1104. * @see getSlave4Enabled()
  1105. * @see MPU6050_RA_I2C_SLV4_CTRL
  1106. */
  1107. void MPUsetSlave4Enabled(bool_t enabled) {
  1108. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_EN_BIT, enabled);
  1109. }
  1110. /** Get the enabled value for Slave 4 transaction interrupts.
  1111. * When set to 1, this bit enables the generation of an interrupt signal upon
  1112. * completion of a Slave 4 transaction. When cleared to 0, this bit disables the
  1113. * generation of an interrupt signal upon completion of a Slave 4 transaction.
  1114. * The interrupt status can be observed in Register 54.
  1115. *
  1116. * @return Current enabled value for Slave 4 transaction interrupts.
  1117. * @see MPU6050_RA_I2C_SLV4_CTRL
  1118. */
  1119. bool_t MPUgetSlave4InterruptEnabled() {
  1120. I2CdevreadBit(MPUdevAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_INT_EN_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1121. return MPUbuffer[0];
  1122. }
  1123. /** Set the enabled value for Slave 4 transaction interrupts.
  1124. * @param enabled New enabled value for Slave 4 transaction interrupts.
  1125. * @see getSlave4InterruptEnabled()
  1126. * @see MPU6050_RA_I2C_SLV4_CTRL
  1127. */
  1128. void MPUsetSlave4InterruptEnabled(bool_t enabled) {
  1129. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_INT_EN_BIT, enabled);
  1130. }
  1131. /** Get write mode for Slave 4.
  1132. * When set to 1, the transaction will read or write data only. When cleared to
  1133. * 0, the transaction will write a register address prior to reading or writing
  1134. * data. This should equal 0 when specifying the register address within the
  1135. * Slave device to/from which the ensuing data transaction will take place.
  1136. *
  1137. * @return Current write mode for Slave 4 (0 = register address + data, 1 = data only)
  1138. * @see MPU6050_RA_I2C_SLV4_CTRL
  1139. */
  1140. bool_t MPUgetSlave4WriteMode() {
  1141. I2CdevreadBit(MPUdevAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_REG_DIS_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1142. return MPUbuffer[0];
  1143. }
  1144. /** Set write mode for the Slave 4.
  1145. * @param mode New write mode for Slave 4 (0 = register address + data, 1 = data only)
  1146. * @see getSlave4WriteMode()
  1147. * @see MPU6050_RA_I2C_SLV4_CTRL
  1148. */
  1149. void MPUsetSlave4WriteMode(bool_t mode) {
  1150. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_REG_DIS_BIT, mode);
  1151. }
  1152. /** Get Slave 4 master delay value.
  1153. * This configures the reduced access rate of I2C slaves relative to the Sample
  1154. * Rate. When a slave's access rate is decreased relative to the Sample Rate,
  1155. * the slave is accessed every:
  1156. *
  1157. * 1 / (1 + I2C_MST_DLY) samples
  1158. *
  1159. * This base Sample Rate in turn is determined by SMPLRT_DIV (register 25) and
  1160. * DLPF_CFG (register 26). Whether a slave's access rate is reduced relative to
  1161. * the Sample Rate is determined by I2C_MST_DELAY_CTRL (register 103). For
  1162. * further information regarding the Sample Rate, please refer to register 25.
  1163. *
  1164. * @return Current Slave 4 master delay value
  1165. * @see MPU6050_RA_I2C_SLV4_CTRL
  1166. */
  1167. uint8_t MPUgetSlave4MasterDelay() {
  1168. I2CdevreadBits(MPUdevAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_MST_DLY_BIT, MPU6050_I2C_SLV4_MST_DLY_LENGTH, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1169. return MPUbuffer[0];
  1170. }
  1171. /** Set Slave 4 master delay value.
  1172. * @param delay New Slave 4 master delay value
  1173. * @see getSlave4MasterDelay()
  1174. * @see MPU6050_RA_I2C_SLV4_CTRL
  1175. */
  1176. void MPUsetSlave4MasterDelay(uint8_t delay) {
  1177. I2CdevwriteBits(MPUdevAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_MST_DLY_BIT, MPU6050_I2C_SLV4_MST_DLY_LENGTH, delay);
  1178. }
  1179. /** Get last available byte read from Slave 4.
  1180. * This register stores the data read from Slave 4. This field is populated
  1181. * after a read transaction.
  1182. * @return Last available byte read from to Slave 4
  1183. * @see MPU6050_RA_I2C_SLV4_DI
  1184. */
  1185. uint8_t MPUgetSlate4InputByte() {
  1186. I2CdevreadByte(MPUdevAddr, MPU6050_RA_I2C_SLV4_DI, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1187. return MPUbuffer[0];
  1188. }
  1189. // I2C_MST_STATUS register
  1190. /** Get FSYNC interrupt status.
  1191. * This bit reflects the status of the FSYNC interrupt from an external device
  1192. * into the MPU-60X0. This is used as a way to pass an external interrupt
  1193. * through the MPU-60X0 to the host application processor. When set to 1, this
  1194. * bit will cause an interrupt if FSYNC_INT_EN is asserted in INT_PIN_CFG
  1195. * (Register 55).
  1196. * @return FSYNC interrupt status
  1197. * @see MPU6050_RA_I2C_MST_STATUS
  1198. */
  1199. bool_t MPUgetPassthroughStatus() {
  1200. I2CdevreadBit(MPUdevAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_PASS_THROUGH_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1201. return MPUbuffer[0];
  1202. }
  1203. /** Get Slave 4 transaction done status.
  1204. * Automatically sets to 1 when a Slave 4 transaction has completed. This
  1205. * triggers an interrupt if the I2C_MST_INT_EN bit in the INT_ENABLE register
  1206. * (Register 56) is asserted and if the SLV_4_DONE_INT bit is asserted in the
  1207. * I2C_SLV4_CTRL register (Register 52).
  1208. * @return Slave 4 transaction done status
  1209. * @see MPU6050_RA_I2C_MST_STATUS
  1210. */
  1211. bool_t MPUgetSlave4IsDone() {
  1212. I2CdevreadBit(MPUdevAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV4_DONE_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1213. return MPUbuffer[0];
  1214. }
  1215. /** Get master arbitration lost status.
  1216. * This bit automatically sets to 1 when the I2C Master has lost arbitration of
  1217. * the auxiliary I2C bus (an error condition). This triggers an interrupt if the
  1218. * I2C_MST_INT_EN bit in the INT_ENABLE register (Register 56) is asserted.
  1219. * @return Master arbitration lost status
  1220. * @see MPU6050_RA_I2C_MST_STATUS
  1221. */
  1222. bool_t MPUgetLostArbitration() {
  1223. I2CdevreadBit(MPUdevAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_LOST_ARB_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1224. return MPUbuffer[0];
  1225. }
  1226. /** Get Slave 4 NACK status.
  1227. * This bit automatically sets to 1 when the I2C Master receives a NACK in a
  1228. * transaction with Slave 4. This triggers an interrupt if the I2C_MST_INT_EN
  1229. * bit in the INT_ENABLE register (Register 56) is asserted.
  1230. * @return Slave 4 NACK interrupt status
  1231. * @see MPU6050_RA_I2C_MST_STATUS
  1232. */
  1233. bool_t MPUgetSlave4Nack() {
  1234. I2CdevreadBit(MPUdevAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV4_NACK_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1235. return MPUbuffer[0];
  1236. }
  1237. /** Get Slave 3 NACK status.
  1238. * This bit automatically sets to 1 when the I2C Master receives a NACK in a
  1239. * transaction with Slave 3. This triggers an interrupt if the I2C_MST_INT_EN
  1240. * bit in the INT_ENABLE register (Register 56) is asserted.
  1241. * @return Slave 3 NACK interrupt status
  1242. * @see MPU6050_RA_I2C_MST_STATUS
  1243. */
  1244. bool_t MPUgetSlave3Nack() {
  1245. I2CdevreadBit(MPUdevAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV3_NACK_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1246. return MPUbuffer[0];
  1247. }
  1248. /** Get Slave 2 NACK status.
  1249. * This bit automatically sets to 1 when the I2C Master receives a NACK in a
  1250. * transaction with Slave 2. This triggers an interrupt if the I2C_MST_INT_EN
  1251. * bit in the INT_ENABLE register (Register 56) is asserted.
  1252. * @return Slave 2 NACK interrupt status
  1253. * @see MPU6050_RA_I2C_MST_STATUS
  1254. */
  1255. bool_t MPUgetSlave2Nack() {
  1256. I2CdevreadBit(MPUdevAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV2_NACK_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1257. return MPUbuffer[0];
  1258. }
  1259. /** Get Slave 1 NACK status.
  1260. * This bit automatically sets to 1 when the I2C Master receives a NACK in a
  1261. * transaction with Slave 1. This triggers an interrupt if the I2C_MST_INT_EN
  1262. * bit in the INT_ENABLE register (Register 56) is asserted.
  1263. * @return Slave 1 NACK interrupt status
  1264. * @see MPU6050_RA_I2C_MST_STATUS
  1265. */
  1266. bool_t MPUgetSlave1Nack() {
  1267. I2CdevreadBit(MPUdevAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV1_NACK_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1268. return MPUbuffer[0];
  1269. }
  1270. /** Get Slave 0 NACK status.
  1271. * This bit automatically sets to 1 when the I2C Master receives a NACK in a
  1272. * transaction with Slave 0. This triggers an interrupt if the I2C_MST_INT_EN
  1273. * bit in the INT_ENABLE register (Register 56) is asserted.
  1274. * @return Slave 0 NACK interrupt status
  1275. * @see MPU6050_RA_I2C_MST_STATUS
  1276. */
  1277. bool_t MPUgetSlave0Nack() {
  1278. I2CdevreadBit(MPUdevAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV0_NACK_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1279. return MPUbuffer[0];
  1280. }
  1281. // INT_PIN_CFG register
  1282. /** Get interrupt logic level mode.
  1283. * Will be set 0 for active-high, 1 for active-low.
  1284. * @return Current interrupt mode (0=active-high, 1=active-low)
  1285. * @see MPU6050_RA_INT_PIN_CFG
  1286. * @see MPU6050_INTCFG_INT_LEVEL_BIT
  1287. */
  1288. bool_t MPUgetInterruptMode() {
  1289. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_LEVEL_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1290. return MPUbuffer[0];
  1291. }
  1292. /** Set interrupt logic level mode.
  1293. * @param mode New interrupt mode (0=active-high, 1=active-low)
  1294. * @see getInterruptMode()
  1295. * @see MPU6050_RA_INT_PIN_CFG
  1296. * @see MPU6050_INTCFG_INT_LEVEL_BIT
  1297. */
  1298. void MPUsetInterruptMode(bool_t mode) {
  1299. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_LEVEL_BIT, mode);
  1300. }
  1301. /** Get interrupt drive mode.
  1302. * Will be set 0 for push-pull, 1 for open-drain.
  1303. * @return Current interrupt drive mode (0=push-pull, 1=open-drain)
  1304. * @see MPU6050_RA_INT_PIN_CFG
  1305. * @see MPU6050_INTCFG_INT_OPEN_BIT
  1306. */
  1307. bool_t MPUgetInterruptDrive() {
  1308. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_OPEN_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1309. return MPUbuffer[0];
  1310. }
  1311. /** Set interrupt drive mode.
  1312. * @param drive New interrupt drive mode (0=push-pull, 1=open-drain)
  1313. * @see getInterruptDrive()
  1314. * @see MPU6050_RA_INT_PIN_CFG
  1315. * @see MPU6050_INTCFG_INT_OPEN_BIT
  1316. */
  1317. void MPUsetInterruptDrive(bool_t drive) {
  1318. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_OPEN_BIT, drive);
  1319. }
  1320. /** Get interrupt latch mode.
  1321. * Will be set 0 for 50us-pulse, 1 for latch-until-int-cleared.
  1322. * @return Current latch mode (0=50us-pulse, 1=latch-until-int-cleared)
  1323. * @see MPU6050_RA_INT_PIN_CFG
  1324. * @see MPU6050_INTCFG_LATCH_INT_EN_BIT
  1325. */
  1326. bool_t MPUgetInterruptLatch() {
  1327. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_LATCH_INT_EN_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1328. return MPUbuffer[0];
  1329. }
  1330. /** Set interrupt latch mode.
  1331. * @param latch New latch mode (0=50us-pulse, 1=latch-until-int-cleared)
  1332. * @see getInterruptLatch()
  1333. * @see MPU6050_RA_INT_PIN_CFG
  1334. * @see MPU6050_INTCFG_LATCH_INT_EN_BIT
  1335. */
  1336. void MPUsetInterruptLatch(bool_t latch) {
  1337. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_LATCH_INT_EN_BIT, latch);
  1338. }
  1339. /** Get interrupt latch clear mode.
  1340. * Will be set 0 for status-read-only, 1 for any-register-read.
  1341. * @return Current latch clear mode (0=status-read-only, 1=any-register-read)
  1342. * @see MPU6050_RA_INT_PIN_CFG
  1343. * @see MPU6050_INTCFG_INT_RD_CLEAR_BIT
  1344. */
  1345. bool_t MPUgetInterruptLatchClear() {
  1346. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_RD_CLEAR_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1347. return MPUbuffer[0];
  1348. }
  1349. /** Set interrupt latch clear mode.
  1350. * @param clear New latch clear mode (0=status-read-only, 1=any-register-read)
  1351. * @see getInterruptLatchClear()
  1352. * @see MPU6050_RA_INT_PIN_CFG
  1353. * @see MPU6050_INTCFG_INT_RD_CLEAR_BIT
  1354. */
  1355. void MPUsetInterruptLatchClear(bool_t clear) {
  1356. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_RD_CLEAR_BIT, clear);
  1357. }
  1358. /** Get FSYNC interrupt logic level mode.
  1359. * @return Current FSYNC interrupt mode (0=active-high, 1=active-low)
  1360. * @see getFSyncInterruptMode()
  1361. * @see MPU6050_RA_INT_PIN_CFG
  1362. * @see MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT
  1363. */
  1364. bool_t MPUgetFSyncInterruptLevel() {
  1365. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1366. return MPUbuffer[0];
  1367. }
  1368. /** Set FSYNC interrupt logic level mode.
  1369. * @param mode New FSYNC interrupt mode (0=active-high, 1=active-low)
  1370. * @see getFSyncInterruptMode()
  1371. * @see MPU6050_RA_INT_PIN_CFG
  1372. * @see MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT
  1373. */
  1374. void MPUsetFSyncInterruptLevel(bool_t level) {
  1375. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT, level);
  1376. }
  1377. /** Get FSYNC pin interrupt enabled setting.
  1378. * Will be set 0 for disabled, 1 for enabled.
  1379. * @return Current interrupt enabled setting
  1380. * @see MPU6050_RA_INT_PIN_CFG
  1381. * @see MPU6050_INTCFG_FSYNC_INT_EN_BIT
  1382. */
  1383. bool_t MPUgetFSyncInterruptEnabled() {
  1384. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_EN_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1385. return MPUbuffer[0];
  1386. }
  1387. /** Set FSYNC pin interrupt enabled setting.
  1388. * @param enabled New FSYNC pin interrupt enabled setting
  1389. * @see getFSyncInterruptEnabled()
  1390. * @see MPU6050_RA_INT_PIN_CFG
  1391. * @see MPU6050_INTCFG_FSYNC_INT_EN_BIT
  1392. */
  1393. void MPUsetFSyncInterruptEnabled(bool_t enabled) {
  1394. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_EN_BIT, enabled);
  1395. }
  1396. /** Get I2C bypass enabled status.
  1397. * When this bit is equal to 1 and I2C_MST_EN (Register 106 bit[5]) is equal to
  1398. * 0, the host application processor will be able to directly access the
  1399. * auxiliary I2C bus of the MPU-60X0. When this bit is equal to 0, the host
  1400. * application processor will not be able to directly access the auxiliary I2C
  1401. * bus of the MPU-60X0 regardless of the state of I2C_MST_EN (Register 106
  1402. * bit[5]).
  1403. * @return Current I2C bypass enabled status
  1404. * @see MPU6050_RA_INT_PIN_CFG
  1405. * @see MPU6050_INTCFG_I2C_BYPASS_EN_BIT
  1406. */
  1407. bool_t MPUgetI2CBypassEnabled() {
  1408. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_I2C_BYPASS_EN_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1409. return MPUbuffer[0];
  1410. }
  1411. /** Set I2C bypass enabled status.
  1412. * When this bit is equal to 1 and I2C_MST_EN (Register 106 bit[5]) is equal to
  1413. * 0, the host application processor will be able to directly access the
  1414. * auxiliary I2C bus of the MPU-60X0. When this bit is equal to 0, the host
  1415. * application processor will not be able to directly access the auxiliary I2C
  1416. * bus of the MPU-60X0 regardless of the state of I2C_MST_EN (Register 106
  1417. * bit[5]).
  1418. * @param enabled New I2C bypass enabled status
  1419. * @see MPU6050_RA_INT_PIN_CFG
  1420. * @see MPU6050_INTCFG_I2C_BYPASS_EN_BIT
  1421. */
  1422. void MPUsetI2CBypassEnabled(bool_t enabled) {
  1423. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_I2C_BYPASS_EN_BIT, enabled);
  1424. }
  1425. /** Get reference clock output enabled status.
  1426. * When this bit is equal to 1, a reference clock output is provided at the
  1427. * CLKOUT pin. When this bit is equal to 0, the clock output is disabled. For
  1428. * further information regarding CLKOUT, please refer to the MPU-60X0 Product
  1429. * Specification document.
  1430. * @return Current reference clock output enabled status
  1431. * @see MPU6050_RA_INT_PIN_CFG
  1432. * @see MPU6050_INTCFG_CLKOUT_EN_BIT
  1433. */
  1434. bool_t MPUgetClockOutputEnabled() {
  1435. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_CLKOUT_EN_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1436. return MPUbuffer[0];
  1437. }
  1438. /** Set reference clock output enabled status.
  1439. * When this bit is equal to 1, a reference clock output is provided at the
  1440. * CLKOUT pin. When this bit is equal to 0, the clock output is disabled. For
  1441. * further information regarding CLKOUT, please refer to the MPU-60X0 Product
  1442. * Specification document.
  1443. * @param enabled New reference clock output enabled status
  1444. * @see MPU6050_RA_INT_PIN_CFG
  1445. * @see MPU6050_INTCFG_CLKOUT_EN_BIT
  1446. */
  1447. void MPUsetClockOutputEnabled(bool_t enabled) {
  1448. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_CLKOUT_EN_BIT, enabled);
  1449. }
  1450. // INT_ENABLE register
  1451. /** Get full interrupt enabled status.
  1452. * Full register byte for all interrupts, for quick reading. Each bit will be
  1453. * set 0 for disabled, 1 for enabled.
  1454. * @return Current interrupt enabled status
  1455. * @see MPU6050_RA_INT_ENABLE
  1456. * @see MPU6050_INTERRUPT_FF_BIT
  1457. **/
  1458. uint8_t MPUgetIntEnabled() {
  1459. I2CdevreadByte(MPUdevAddr, MPU6050_RA_INT_ENABLE, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1460. return MPUbuffer[0];
  1461. }
  1462. /** Set full interrupt enabled status.
  1463. * Full register byte for all interrupts, for quick reading. Each bit should be
  1464. * set 0 for disabled, 1 for enabled.
  1465. * @param enabled New interrupt enabled status
  1466. * @see getIntFreefallEnabled()
  1467. * @see MPU6050_RA_INT_ENABLE
  1468. * @see MPU6050_INTERRUPT_FF_BIT
  1469. **/
  1470. void MPUsetIntEnabled(uint8_t enabled) {
  1471. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_INT_ENABLE, enabled);
  1472. }
  1473. /** Get Free Fall interrupt enabled status.
  1474. * Will be set 0 for disabled, 1 for enabled.
  1475. * @return Current interrupt enabled status
  1476. * @see MPU6050_RA_INT_ENABLE
  1477. * @see MPU6050_INTERRUPT_FF_BIT
  1478. **/
  1479. bool_t MPUgetIntFreefallEnabled() {
  1480. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FF_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1481. return MPUbuffer[0];
  1482. }
  1483. /** Set Free Fall interrupt enabled status.
  1484. * @param enabled New interrupt enabled status
  1485. * @see getIntFreefallEnabled()
  1486. * @see MPU6050_RA_INT_ENABLE
  1487. * @see MPU6050_INTERRUPT_FF_BIT
  1488. **/
  1489. void MPUsetIntFreefallEnabled(bool_t enabled) {
  1490. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FF_BIT, enabled);
  1491. }
  1492. /** Get Motion Detection interrupt enabled status.
  1493. * Will be set 0 for disabled, 1 for enabled.
  1494. * @return Current interrupt enabled status
  1495. * @see MPU6050_RA_INT_ENABLE
  1496. * @see MPU6050_INTERRUPT_MOT_BIT
  1497. **/
  1498. bool_t MPUgetIntMotionEnabled() {
  1499. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_MOT_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1500. return MPUbuffer[0];
  1501. }
  1502. /** Set Motion Detection interrupt enabled status.
  1503. * @param enabled New interrupt enabled status
  1504. * @see getIntMotionEnabled()
  1505. * @see MPU6050_RA_INT_ENABLE
  1506. * @see MPU6050_INTERRUPT_MOT_BIT
  1507. **/
  1508. void MPUsetIntMotionEnabled(bool_t enabled) {
  1509. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_MOT_BIT, enabled);
  1510. }
  1511. /** Get Zero Motion Detection interrupt enabled status.
  1512. * Will be set 0 for disabled, 1 for enabled.
  1513. * @return Current interrupt enabled status
  1514. * @see MPU6050_RA_INT_ENABLE
  1515. * @see MPU6050_INTERRUPT_ZMOT_BIT
  1516. **/
  1517. bool_t MPUgetIntZeroMotionEnabled() {
  1518. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_ZMOT_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1519. return MPUbuffer[0];
  1520. }
  1521. /** Set Zero Motion Detection interrupt enabled status.
  1522. * @param enabled New interrupt enabled status
  1523. * @see getIntZeroMotionEnabled()
  1524. * @see MPU6050_RA_INT_ENABLE
  1525. * @see MPU6050_INTERRUPT_ZMOT_BIT
  1526. **/
  1527. void MPUsetIntZeroMotionEnabled(bool_t enabled) {
  1528. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_ZMOT_BIT, enabled);
  1529. }
  1530. /** Get FIFO Buffer Overflow interrupt enabled status.
  1531. * Will be set 0 for disabled, 1 for enabled.
  1532. * @return Current interrupt enabled status
  1533. * @see MPU6050_RA_INT_ENABLE
  1534. * @see MPU6050_INTERRUPT_FIFO_OFLOW_BIT
  1535. **/
  1536. bool_t MPUgetIntFIFOBufferOverflowEnabled() {
  1537. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FIFO_OFLOW_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1538. return MPUbuffer[0];
  1539. }
  1540. /** Set FIFO Buffer Overflow interrupt enabled status.
  1541. * @param enabled New interrupt enabled status
  1542. * @see getIntFIFOBufferOverflowEnabled()
  1543. * @see MPU6050_RA_INT_ENABLE
  1544. * @see MPU6050_INTERRUPT_FIFO_OFLOW_BIT
  1545. **/
  1546. void MPUsetIntFIFOBufferOverflowEnabled(bool_t enabled) {
  1547. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FIFO_OFLOW_BIT, enabled);
  1548. }
  1549. /** Get I2C Master interrupt enabled status.
  1550. * This enables any of the I2C Master interrupt sources to generate an
  1551. * interrupt. Will be set 0 for disabled, 1 for enabled.
  1552. * @return Current interrupt enabled status
  1553. * @see MPU6050_RA_INT_ENABLE
  1554. * @see MPU6050_INTERRUPT_I2C_MST_INT_BIT
  1555. **/
  1556. bool_t MPUgetIntI2CMasterEnabled() {
  1557. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_I2C_MST_INT_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1558. return MPUbuffer[0];
  1559. }
  1560. /** Set I2C Master interrupt enabled status.
  1561. * @param enabled New interrupt enabled status
  1562. * @see getIntI2CMasterEnabled()
  1563. * @see MPU6050_RA_INT_ENABLE
  1564. * @see MPU6050_INTERRUPT_I2C_MST_INT_BIT
  1565. **/
  1566. void MPUsetIntI2CMasterEnabled(bool_t enabled) {
  1567. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_I2C_MST_INT_BIT, enabled);
  1568. }
  1569. /** Get Data Ready interrupt enabled setting.
  1570. * This event occurs each time a write operation to all of the sensor registers
  1571. * has been completed. Will be set 0 for disabled, 1 for enabled.
  1572. * @return Current interrupt enabled status
  1573. * @see MPU6050_RA_INT_ENABLE
  1574. * @see MPU6050_INTERRUPT_DATA_RDY_BIT
  1575. */
  1576. bool_t MPUgetIntDataReadyEnabled() {
  1577. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_DATA_RDY_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1578. return MPUbuffer[0];
  1579. }
  1580. /** Set Data Ready interrupt enabled status.
  1581. * @param enabled New interrupt enabled status
  1582. * @see getIntDataReadyEnabled()
  1583. * @see MPU6050_RA_INT_CFG
  1584. * @see MPU6050_INTERRUPT_DATA_RDY_BIT
  1585. */
  1586. void MPUsetIntDataReadyEnabled(bool_t enabled) {
  1587. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_DATA_RDY_BIT, enabled);
  1588. }
  1589. // INT_STATUS register
  1590. /** Get full set of interrupt status bits.
  1591. * These bits clear to 0 after the register has been read. Very useful
  1592. * for getting multiple INT statuses, since each single bit read clears
  1593. * all of them because it has to read the whole byte.
  1594. * @return Current interrupt status
  1595. * @see MPU6050_RA_INT_STATUS
  1596. */
  1597. uint8_t MPUgetIntStatus() {
  1598. I2CdevreadByte(MPUdevAddr, MPU6050_RA_INT_STATUS, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1599. return MPUbuffer[0];
  1600. }
  1601. /** Get Free Fall interrupt status.
  1602. * This bit automatically sets to 1 when a Free Fall interrupt has been
  1603. * generated. The bit clears to 0 after the register has been read.
  1604. * @return Current interrupt status
  1605. * @see MPU6050_RA_INT_STATUS
  1606. * @see MPU6050_INTERRUPT_FF_BIT
  1607. */
  1608. bool_t MPUgetIntFreefallStatus() {
  1609. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_FF_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1610. return MPUbuffer[0];
  1611. }
  1612. /** Get Motion Detection interrupt status.
  1613. * This bit automatically sets to 1 when a Motion Detection interrupt has been
  1614. * generated. The bit clears to 0 after the register has been read.
  1615. * @return Current interrupt status
  1616. * @see MPU6050_RA_INT_STATUS
  1617. * @see MPU6050_INTERRUPT_MOT_BIT
  1618. */
  1619. bool_t MPUgetIntMotionStatus() {
  1620. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_MOT_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1621. return MPUbuffer[0];
  1622. }
  1623. /** Get Zero Motion Detection interrupt status.
  1624. * This bit automatically sets to 1 when a Zero Motion Detection interrupt has
  1625. * been generated. The bit clears to 0 after the register has been read.
  1626. * @return Current interrupt status
  1627. * @see MPU6050_RA_INT_STATUS
  1628. * @see MPU6050_INTERRUPT_ZMOT_BIT
  1629. */
  1630. bool_t MPUgetIntZeroMotionStatus() {
  1631. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_ZMOT_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1632. return MPUbuffer[0];
  1633. }
  1634. /** Get FIFO Buffer Overflow interrupt status.
  1635. * This bit automatically sets to 1 when a Free Fall interrupt has been
  1636. * generated. The bit clears to 0 after the register has been read.
  1637. * @return Current interrupt status
  1638. * @see MPU6050_RA_INT_STATUS
  1639. * @see MPU6050_INTERRUPT_FIFO_OFLOW_BIT
  1640. */
  1641. bool_t MPUgetIntFIFOBufferOverflowStatus() {
  1642. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_FIFO_OFLOW_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1643. return MPUbuffer[0];
  1644. }
  1645. /** Get I2C Master interrupt status.
  1646. * This bit automatically sets to 1 when an I2C Master interrupt has been
  1647. * generated. For a list of I2C Master interrupts, please refer to Register 54.
  1648. * The bit clears to 0 after the register has been read.
  1649. * @return Current interrupt status
  1650. * @see MPU6050_RA_INT_STATUS
  1651. * @see MPU6050_INTERRUPT_I2C_MST_INT_BIT
  1652. */
  1653. bool_t MPUgetIntI2CMasterStatus() {
  1654. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_I2C_MST_INT_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1655. return MPUbuffer[0];
  1656. }
  1657. /** Get Data Ready interrupt status.
  1658. * This bit automatically sets to 1 when a Data Ready interrupt has been
  1659. * generated. The bit clears to 0 after the register has been read.
  1660. * @return Current interrupt status
  1661. * @see MPU6050_RA_INT_STATUS
  1662. * @see MPU6050_INTERRUPT_DATA_RDY_BIT
  1663. */
  1664. bool_t MPUgetIntDataReadyStatus() {
  1665. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_DATA_RDY_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1666. return MPUbuffer[0];
  1667. }
  1668. // ACCEL_*OUT_* registers
  1669. /** Get raw 9-axis motion sensor readings (accel/gyro/compass).
  1670. * FUNCTION NOT FULLY IMPLEMENTED YET.
  1671. * @param ax 16-bit signed integer container for accelerometer X-axis value
  1672. * @param ay 16-bit signed integer container for accelerometer Y-axis value
  1673. * @param az 16-bit signed integer container for accelerometer Z-axis value
  1674. * @param gx 16-bit signed integer container for gyroscope X-axis value
  1675. * @param gy 16-bit signed integer container for gyroscope Y-axis value
  1676. * @param gz 16-bit signed integer container for gyroscope Z-axis value
  1677. * @param mx 16-bit signed integer container for magnetometer X-axis value
  1678. * @param my 16-bit signed integer container for magnetometer Y-axis value
  1679. * @param mz 16-bit signed integer container for magnetometer Z-axis value
  1680. * @see getMotion6()
  1681. * @see getAcceleration()
  1682. * @see getRotation()
  1683. * @see MPU6050_RA_ACCEL_XOUT_H
  1684. */
  1685. void MPUgetMotion9(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) {
  1686. MPUgetMotion6(ax, ay, az, gx, gy, gz);
  1687. // TODO: magnetometer integration
  1688. }
  1689. /** Get raw 6-axis motion sensor readings (accel/gyro).
  1690. * Retrieves all currently available motion sensor values.
  1691. * @param ax 16-bit signed integer container for accelerometer X-axis value
  1692. * @param ay 16-bit signed integer container for accelerometer Y-axis value
  1693. * @param az 16-bit signed integer container for accelerometer Z-axis value
  1694. * @param gx 16-bit signed integer container for gyroscope X-axis value
  1695. * @param gy 16-bit signed integer container for gyroscope Y-axis value
  1696. * @param gz 16-bit signed integer container for gyroscope Z-axis value
  1697. * @see getAcceleration()
  1698. * @see getRotation()
  1699. * @see MPU6050_RA_ACCEL_XOUT_H
  1700. */
  1701. void MPUgetMotion6(int16_t* ax, int16_t* ay, int16_t* az, int16_t* gx, int16_t* gy, int16_t* gz) {
  1702. I2CdevreadBytes(MPUdevAddr, MPU6050_RA_ACCEL_XOUT_H, 14, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1703. *ax = (((int16_t)MPUbuffer[0]) << 8) | MPUbuffer[1];
  1704. *ay = (((int16_t)MPUbuffer[2]) << 8) | MPUbuffer[3];
  1705. *az = (((int16_t)MPUbuffer[4]) << 8) | MPUbuffer[5];
  1706. *gx = (((int16_t)MPUbuffer[8]) << 8) | MPUbuffer[9];
  1707. *gy = (((int16_t)MPUbuffer[10]) << 8) | MPUbuffer[11];
  1708. *gz = (((int16_t)MPUbuffer[12]) << 8) | MPUbuffer[13];
  1709. }
  1710. /** Get 3-axis accelerometer readings.
  1711. * These registers store the most recent accelerometer measurements.
  1712. * Accelerometer measurements are written to these registers at the Sample Rate
  1713. * as defined in Register 25.
  1714. *
  1715. * The accelerometer measurement registers, along with the temperature
  1716. * measurement registers, gyroscope measurement registers, and external sensor
  1717. * data registers, are composed of two sets of registers: an internal register
  1718. * set and a user-facing read register set.
  1719. *
  1720. * The data within the accelerometer sensors' internal register set is always
  1721. * updated at the Sample Rate. Meanwhile, the user-facing read register set
  1722. * duplicates the internal register set's data values whenever the serial
  1723. * interface is idle. This guarantees that a burst read of sensor registers will
  1724. * read measurements from the same sampling instant. Note that if burst reads
  1725. * are not used, the user is responsible for ensuring a set of single byte reads
  1726. * correspond to a single sampling instant by checking the Data Ready interrupt.
  1727. *
  1728. * Each 16-bit accelerometer measurement has a full scale defined in ACCEL_FS
  1729. * (Register 28). For each full scale setting, the accelerometers' sensitivity
  1730. * per LSB in ACCEL_xOUT is shown in the table below:
  1731. *
  1732. * <pre>
  1733. * AFS_SEL | Full Scale Range | LSB Sensitivity
  1734. * --------+------------------+----------------
  1735. * 0 | +/- 2g | 8192 LSB/mg
  1736. * 1 | +/- 4g | 4096 LSB/mg
  1737. * 2 | +/- 8g | 2048 LSB/mg
  1738. * 3 | +/- 16g | 1024 LSB/mg
  1739. * </pre>
  1740. *
  1741. * @param x 16-bit signed integer container for X-axis acceleration
  1742. * @param y 16-bit signed integer container for Y-axis acceleration
  1743. * @param z 16-bit signed integer container for Z-axis acceleration
  1744. * @see MPU6050_RA_GYRO_XOUT_H
  1745. */
  1746. void MPUgetAcceleration(int16_t* x, int16_t* y, int16_t* z) {
  1747. I2CdevreadBytes(MPUdevAddr, MPU6050_RA_ACCEL_XOUT_H, 6, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1748. *x = (((int16_t)MPUbuffer[0]) << 8) | MPUbuffer[1];
  1749. *y = (((int16_t)MPUbuffer[2]) << 8) | MPUbuffer[3];
  1750. *z = (((int16_t)MPUbuffer[4]) << 8) | MPUbuffer[5];
  1751. }
  1752. /** Get X-axis accelerometer reading.
  1753. * @return X-axis acceleration measurement in 16-bit 2's complement format
  1754. * @see getMotion6()
  1755. * @see MPU6050_RA_ACCEL_XOUT_H
  1756. */
  1757. int16_t MPUgetAccelerationX() {
  1758. I2CdevreadBytes(MPUdevAddr, MPU6050_RA_ACCEL_XOUT_H, 2, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1759. return (((int16_t)MPUbuffer[0]) << 8) | MPUbuffer[1];
  1760. }
  1761. /** Get Y-axis accelerometer reading.
  1762. * @return Y-axis acceleration measurement in 16-bit 2's complement format
  1763. * @see getMotion6()
  1764. * @see MPU6050_RA_ACCEL_YOUT_H
  1765. */
  1766. int16_t MPUgetAccelerationY() {
  1767. I2CdevreadBytes(MPUdevAddr, MPU6050_RA_ACCEL_YOUT_H, 2, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1768. return (((int16_t)MPUbuffer[0]) << 8) | MPUbuffer[1];
  1769. }
  1770. /** Get Z-axis accelerometer reading.
  1771. * @return Z-axis acceleration measurement in 16-bit 2's complement format
  1772. * @see getMotion6()
  1773. * @see MPU6050_RA_ACCEL_ZOUT_H
  1774. */
  1775. int16_t MPUgetAccelerationZ() {
  1776. I2CdevreadBytes(MPUdevAddr, MPU6050_RA_ACCEL_ZOUT_H, 2, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1777. return (((int16_t)MPUbuffer[0]) << 8) | MPUbuffer[1];
  1778. }
  1779. // TEMP_OUT_* registers
  1780. /** Get current internal temperature.
  1781. * @return Temperature reading in 16-bit 2's complement format
  1782. * @see MPU6050_RA_TEMP_OUT_H
  1783. */
  1784. int16_t MPUgetTemperature() {
  1785. I2CdevreadBytes(MPUdevAddr, MPU6050_RA_TEMP_OUT_H, 2, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1786. return (((int16_t)MPUbuffer[0]) << 8) | MPUbuffer[1];
  1787. }
  1788. // GYRO_*OUT_* registers
  1789. /** Get 3-axis gyroscope readings.
  1790. * These gyroscope measurement registers, along with the accelerometer
  1791. * measurement registers, temperature measurement registers, and external sensor
  1792. * data registers, are composed of two sets of registers: an internal register
  1793. * set and a user-facing read register set.
  1794. * The data within the gyroscope sensors' internal register set is always
  1795. * updated at the Sample Rate. Meanwhile, the user-facing read register set
  1796. * duplicates the internal register set's data values whenever the serial
  1797. * interface is idle. This guarantees that a burst read of sensor registers will
  1798. * read measurements from the same sampling instant. Note that if burst reads
  1799. * are not used, the user is responsible for ensuring a set of single byte reads
  1800. * correspond to a single sampling instant by checking the Data Ready interrupt.
  1801. *
  1802. * Each 16-bit gyroscope measurement has a full scale defined in FS_SEL
  1803. * (Register 27). For each full scale setting, the gyroscopes' sensitivity per
  1804. * LSB in GYRO_xOUT is shown in the table below:
  1805. *
  1806. * <pre>
  1807. * FS_SEL | Full Scale Range | LSB Sensitivity
  1808. * -------+--------------------+----------------
  1809. * 0 | +/- 250 degrees/s | 131 LSB/deg/s
  1810. * 1 | +/- 500 degrees/s | 65.5 LSB/deg/s
  1811. * 2 | +/- 1000 degrees/s | 32.8 LSB/deg/s
  1812. * 3 | +/- 2000 degrees/s | 16.4 LSB/deg/s
  1813. * </pre>
  1814. *
  1815. * @param x 16-bit signed integer container for X-axis rotation
  1816. * @param y 16-bit signed integer container for Y-axis rotation
  1817. * @param z 16-bit signed integer container for Z-axis rotation
  1818. * @see getMotion6()
  1819. * @see MPU6050_RA_GYRO_XOUT_H
  1820. */
  1821. void MPUgetRotation(int16_t* x, int16_t* y, int16_t* z) {
  1822. I2CdevreadBytes(MPUdevAddr, MPU6050_RA_GYRO_XOUT_H, 6, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1823. *x = (((int16_t)MPUbuffer[0]) << 8) | MPUbuffer[1];
  1824. *y = (((int16_t)MPUbuffer[2]) << 8) | MPUbuffer[3];
  1825. *z = (((int16_t)MPUbuffer[4]) << 8) | MPUbuffer[5];
  1826. }
  1827. /** Get X-axis gyroscope reading.
  1828. * @return X-axis rotation measurement in 16-bit 2's complement format
  1829. * @see getMotion6()
  1830. * @see MPU6050_RA_GYRO_XOUT_H
  1831. */
  1832. int16_t MPUgetRotationX() {
  1833. I2CdevreadBytes(MPUdevAddr, MPU6050_RA_GYRO_XOUT_H, 2, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1834. return (((int16_t)MPUbuffer[0]) << 8) | MPUbuffer[1];
  1835. }
  1836. /** Get Y-axis gyroscope reading.
  1837. * @return Y-axis rotation measurement in 16-bit 2's complement format
  1838. * @see getMotion6()
  1839. * @see MPU6050_RA_GYRO_YOUT_H
  1840. */
  1841. int16_t MPUgetRotationY() {
  1842. I2CdevreadBytes(MPUdevAddr, MPU6050_RA_GYRO_YOUT_H, 2, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1843. return (((int16_t)MPUbuffer[0]) << 8) | MPUbuffer[1];
  1844. }
  1845. /** Get Z-axis gyroscope reading.
  1846. * @return Z-axis rotation measurement in 16-bit 2's complement format
  1847. * @see getMotion6()
  1848. * @see MPU6050_RA_GYRO_ZOUT_H
  1849. */
  1850. int16_t MPUgetRotationZ() {
  1851. I2CdevreadBytes(MPUdevAddr, MPU6050_RA_GYRO_ZOUT_H, 2, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1852. return (((int16_t)MPUbuffer[0]) << 8) | MPUbuffer[1];
  1853. }
  1854. // EXT_SENS_DATA_* registers
  1855. /** Read single byte from external sensor data register.
  1856. * These registers store data read from external sensors by the Slave 0, 1, 2,
  1857. * and 3 on the auxiliary I2C interface. Data read by Slave 4 is stored in
  1858. * I2C_SLV4_DI (Register 53).
  1859. *
  1860. * External sensor data is written to these registers at the Sample Rate as
  1861. * defined in Register 25. This access rate can be reduced by using the Slave
  1862. * Delay Enable registers (Register 103).
  1863. *
  1864. * External sensor data registers, along with the gyroscope measurement
  1865. * registers, accelerometer measurement registers, and temperature measurement
  1866. * registers, are composed of two sets of registers: an internal register set
  1867. * and a user-facing read register set.
  1868. *
  1869. * The data within the external sensors' internal register set is always updated
  1870. * at the Sample Rate (or the reduced access rate) whenever the serial interface
  1871. * is idle. This guarantees that a burst read of sensor registers will read
  1872. * measurements from the same sampling instant. Note that if burst reads are not
  1873. * used, the user is responsible for ensuring a set of single byte reads
  1874. * correspond to a single sampling instant by checking the Data Ready interrupt.
  1875. *
  1876. * Data is placed in these external sensor data registers according to
  1877. * I2C_SLV0_CTRL, I2C_SLV1_CTRL, I2C_SLV2_CTRL, and I2C_SLV3_CTRL (Registers 39,
  1878. * 42, 45, and 48). When more than zero bytes are read (I2C_SLVx_LEN > 0) from
  1879. * an enabled slave (I2C_SLVx_EN = 1), the slave is read at the Sample Rate (as
  1880. * defined in Register 25) or delayed rate (if specified in Register 52 and
  1881. * 103). During each Sample cycle, slave reads are performed in order of Slave
  1882. * number. If all slaves are enabled with more than zero bytes to be read, the
  1883. * order will be Slave 0, followed by Slave 1, Slave 2, and Slave 3.
  1884. *
  1885. * Each enabled slave will have EXT_SENS_DATA registers associated with it by
  1886. * number of bytes read (I2C_SLVx_LEN) in order of slave number, starting from
  1887. * EXT_SENS_DATA_00. Note that this means enabling or disabling a slave may
  1888. * change the higher numbered slaves' associated registers. Furthermore, if
  1889. * fewer total bytes are being read from the external sensors as a result of
  1890. * such a change, then the data remaining in the registers which no longer have
  1891. * an associated slave device (i.e. high numbered registers) will remain in
  1892. * these previously allocated registers unless reset.
  1893. *
  1894. * If the sum of the read lengths of all SLVx transactions exceed the number of
  1895. * available EXT_SENS_DATA registers, the excess bytes will be dropped. There
  1896. * are 24 EXT_SENS_DATA registers and hence the total read lengths between all
  1897. * the slaves cannot be greater than 24 or some bytes will be lost.
  1898. *
  1899. * Note: Slave 4's behavior is distinct from that of Slaves 0-3. For further
  1900. * information regarding the characteristics of Slave 4, please refer to
  1901. * Registers 49 to 53.
  1902. *
  1903. * EXAMPLE:
  1904. * Suppose that Slave 0 is enabled with 4 bytes to be read (I2C_SLV0_EN = 1 and
  1905. * I2C_SLV0_LEN = 4) while Slave 1 is enabled with 2 bytes to be read so that
  1906. * I2C_SLV1_EN = 1 and I2C_SLV1_LEN = 2. In such a situation, EXT_SENS_DATA _00
  1907. * through _03 will be associated with Slave 0, while EXT_SENS_DATA _04 and 05
  1908. * will be associated with Slave 1. If Slave 2 is enabled as well, registers
  1909. * starting from EXT_SENS_DATA_06 will be allocated to Slave 2.
  1910. *
  1911. * If Slave 2 is disabled while Slave 3 is enabled in this same situation, then
  1912. * registers starting from EXT_SENS_DATA_06 will be allocated to Slave 3
  1913. * instead.
  1914. *
  1915. * REGISTER ALLOCATION FOR DYNAMIC DISABLE VS. NORMAL DISABLE:
  1916. * If a slave is disabled at any time, the space initially allocated to the
  1917. * slave in the EXT_SENS_DATA register, will remain associated with that slave.
  1918. * This is to avoid dynamic adjustment of the register allocation.
  1919. *
  1920. * The allocation of the EXT_SENS_DATA registers is recomputed only when (1) all
  1921. * slaves are disabled, or (2) the I2C_MST_RST bit is set (Register 106).
  1922. *
  1923. * This above is also true if one of the slaves gets NACKed and stops
  1924. * functioning.
  1925. *
  1926. * @param position Starting position (0-23)
  1927. * @return Byte read from register
  1928. */
  1929. uint8_t MPUgetExternalSensorByte(int position) {
  1930. I2CdevreadByte(MPUdevAddr, MPU6050_RA_EXT_SENS_DATA_00 + position, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1931. return MPUbuffer[0];
  1932. }
  1933. /** Read word (2 bytes) from external sensor data registers.
  1934. * @param position Starting position (0-21)
  1935. * @return Word read from register
  1936. * @see getExternalSensorByte()
  1937. */
  1938. uint16_t MPUgetExternalSensorWord(int position) {
  1939. I2CdevreadBytes(MPUdevAddr, MPU6050_RA_EXT_SENS_DATA_00 + position, 2, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1940. return (((uint16_t)MPUbuffer[0]) << 8) | MPUbuffer[1];
  1941. }
  1942. /** Read double word (4 bytes) from external sensor data registers.
  1943. * @param position Starting position (0-20)
  1944. * @return Double word read from registers
  1945. * @see getExternalSensorByte()
  1946. */
  1947. uint32_t MPUgetExternalSensorDWord(int position) {
  1948. I2CdevreadBytes(MPUdevAddr, MPU6050_RA_EXT_SENS_DATA_00 + position, 4, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1949. return (((uint32_t)MPUbuffer[0]) << 24) | (((uint32_t)MPUbuffer[1]) << 16) | (((uint16_t)MPUbuffer[2]) << 8) | MPUbuffer[3];
  1950. }
  1951. // MOT_DETECT_STATUS register
  1952. /** Get X-axis negative motion detection interrupt status.
  1953. * @return Motion detection status
  1954. * @see MPU6050_RA_MOT_DETECT_STATUS
  1955. * @see MPU6050_MOTION_MOT_XNEG_BIT
  1956. */
  1957. bool_t MPUgetXNegMotionDetected() {
  1958. I2CdevreadBit(MPUdevAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_XNEG_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1959. return MPUbuffer[0];
  1960. }
  1961. /** Get X-axis positive motion detection interrupt status.
  1962. * @return Motion detection status
  1963. * @see MPU6050_RA_MOT_DETECT_STATUS
  1964. * @see MPU6050_MOTION_MOT_XPOS_BIT
  1965. */
  1966. bool_t MPUgetXPosMotionDetected() {
  1967. I2CdevreadBit(MPUdevAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_XPOS_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1968. return MPUbuffer[0];
  1969. }
  1970. /** Get Y-axis negative motion detection interrupt status.
  1971. * @return Motion detection status
  1972. * @see MPU6050_RA_MOT_DETECT_STATUS
  1973. * @see MPU6050_MOTION_MOT_YNEG_BIT
  1974. */
  1975. bool_t MPUgetYNegMotionDetected() {
  1976. I2CdevreadBit(MPUdevAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_YNEG_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1977. return MPUbuffer[0];
  1978. }
  1979. /** Get Y-axis positive motion detection interrupt status.
  1980. * @return Motion detection status
  1981. * @see MPU6050_RA_MOT_DETECT_STATUS
  1982. * @see MPU6050_MOTION_MOT_YPOS_BIT
  1983. */
  1984. bool_t MPUgetYPosMotionDetected() {
  1985. I2CdevreadBit(MPUdevAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_YPOS_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1986. return MPUbuffer[0];
  1987. }
  1988. /** Get Z-axis negative motion detection interrupt status.
  1989. * @return Motion detection status
  1990. * @see MPU6050_RA_MOT_DETECT_STATUS
  1991. * @see MPU6050_MOTION_MOT_ZNEG_BIT
  1992. */
  1993. bool_t MPUgetZNegMotionDetected() {
  1994. I2CdevreadBit(MPUdevAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_ZNEG_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  1995. return MPUbuffer[0];
  1996. }
  1997. /** Get Z-axis positive motion detection interrupt status.
  1998. * @return Motion detection status
  1999. * @see MPU6050_RA_MOT_DETECT_STATUS
  2000. * @see MPU6050_MOTION_MOT_ZPOS_BIT
  2001. */
  2002. bool_t MPUgetZPosMotionDetected() {
  2003. I2CdevreadBit(MPUdevAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_ZPOS_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2004. return MPUbuffer[0];
  2005. }
  2006. /** Get zero motion detection interrupt status.
  2007. * @return Motion detection status
  2008. * @see MPU6050_RA_MOT_DETECT_STATUS
  2009. * @see MPU6050_MOTION_MOT_ZRMOT_BIT
  2010. */
  2011. bool_t MPUgetZeroMotionDetected() {
  2012. I2CdevreadBit(MPUdevAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_ZRMOT_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2013. return MPUbuffer[0];
  2014. }
  2015. // I2C_SLV*_DO register
  2016. /** Write byte to Data Output container for specified slave.
  2017. * This register holds the output data written into Slave when Slave is set to
  2018. * write mode. For further information regarding Slave control, please
  2019. * refer to Registers 37 to 39 and immediately following.
  2020. * @param num Slave number (0-3)
  2021. * @param data Byte to write
  2022. * @see MPU6050_RA_I2C_SLV0_DO
  2023. */
  2024. void MPUsetSlaveOutputByte(uint8_t num, uint8_t data) {
  2025. if (num > 3) return;
  2026. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_I2C_SLV0_DO + num, data);
  2027. }
  2028. // I2C_MST_DELAY_CTRL register
  2029. /** Get external data shadow delay enabled status.
  2030. * This register is used to specify the timing of external sensor data
  2031. * shadowing. When DELAY_ES_SHADOW is set to 1, shadowing of external
  2032. * sensor data is delayed until all data has been received.
  2033. * @return Current external data shadow delay enabled status.
  2034. * @see MPU6050_RA_I2C_MST_DELAY_CTRL
  2035. * @see MPU6050_DELAYCTRL_DELAY_ES_SHADOW_BIT
  2036. */
  2037. bool_t MPUgetExternalShadowDelayEnabled() {
  2038. I2CdevreadBit(MPUdevAddr, MPU6050_RA_I2C_MST_DELAY_CTRL, MPU6050_DELAYCTRL_DELAY_ES_SHADOW_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2039. return MPUbuffer[0];
  2040. }
  2041. /** Set external data shadow delay enabled status.
  2042. * @param enabled New external data shadow delay enabled status.
  2043. * @see getExternalShadowDelayEnabled()
  2044. * @see MPU6050_RA_I2C_MST_DELAY_CTRL
  2045. * @see MPU6050_DELAYCTRL_DELAY_ES_SHADOW_BIT
  2046. */
  2047. void MPUsetExternalShadowDelayEnabled(bool_t enabled) {
  2048. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_I2C_MST_DELAY_CTRL, MPU6050_DELAYCTRL_DELAY_ES_SHADOW_BIT, enabled);
  2049. }
  2050. /** Get slave delay enabled status.
  2051. * When a particular slave delay is enabled, the rate of access for the that
  2052. * slave device is reduced. When a slave's access rate is decreased relative to
  2053. * the Sample Rate, the slave is accessed every:
  2054. *
  2055. * 1 / (1 + I2C_MST_DLY) Samples
  2056. *
  2057. * This base Sample Rate in turn is determined by SMPLRT_DIV (register * 25)
  2058. * and DLPF_CFG (register 26).
  2059. *
  2060. * For further information regarding I2C_MST_DLY, please refer to register 52.
  2061. * For further information regarding the Sample Rate, please refer to register 25.
  2062. *
  2063. * @param num Slave number (0-4)
  2064. * @return Current slave delay enabled status.
  2065. * @see MPU6050_RA_I2C_MST_DELAY_CTRL
  2066. * @see MPU6050_DELAYCTRL_I2C_SLV0_DLY_EN_BIT
  2067. */
  2068. bool_t MPUgetSlaveDelayEnabled(uint8_t num) {
  2069. // MPU6050_DELAYCTRL_I2C_SLV4_DLY_EN_BIT is 4, SLV3 is 3, etc.
  2070. if (num > 4) return 0;
  2071. I2CdevreadBit(MPUdevAddr, MPU6050_RA_I2C_MST_DELAY_CTRL, num, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2072. return MPUbuffer[0];
  2073. }
  2074. /** Set slave delay enabled status.
  2075. * @param num Slave number (0-4)
  2076. * @param enabled New slave delay enabled status.
  2077. * @see MPU6050_RA_I2C_MST_DELAY_CTRL
  2078. * @see MPU6050_DELAYCTRL_I2C_SLV0_DLY_EN_BIT
  2079. */
  2080. void MPUsetSlaveDelayEnabled(uint8_t num, bool_t enabled) {
  2081. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_I2C_MST_DELAY_CTRL, num, enabled);
  2082. }
  2083. // SIGNAL_PATH_RESET register
  2084. /** Reset gyroscope signal path.
  2085. * The reset will revert the signal path analog to digital converters and
  2086. * filters to their power up configurations.
  2087. * @see MPU6050_RA_SIGNAL_PATH_RESET
  2088. * @see MPU6050_PATHRESET_GYRO_RESET_BIT
  2089. */
  2090. void MPUresetGyroscopePath() {
  2091. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_SIGNAL_PATH_RESET, MPU6050_PATHRESET_GYRO_RESET_BIT, TRUE);
  2092. }
  2093. /** Reset accelerometer signal path.
  2094. * The reset will revert the signal path analog to digital converters and
  2095. * filters to their power up configurations.
  2096. * @see MPU6050_RA_SIGNAL_PATH_RESET
  2097. * @see MPU6050_PATHRESET_ACCEL_RESET_BIT
  2098. */
  2099. void MPUresetAccelerometerPath() {
  2100. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_SIGNAL_PATH_RESET, MPU6050_PATHRESET_ACCEL_RESET_BIT, TRUE);
  2101. }
  2102. /** Reset temperature sensor signal path.
  2103. * The reset will revert the signal path analog to digital converters and
  2104. * filters to their power up configurations.
  2105. * @see MPU6050_RA_SIGNAL_PATH_RESET
  2106. * @see MPU6050_PATHRESET_TEMP_RESET_BIT
  2107. */
  2108. void MPUresetTemperaturePath() {
  2109. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_SIGNAL_PATH_RESET, MPU6050_PATHRESET_TEMP_RESET_BIT, TRUE);
  2110. }
  2111. // MOT_DETECT_CTRL register
  2112. /** Get accelerometer power-on delay.
  2113. * The accelerometer data path provides samples to the sensor registers, Motion
  2114. * detection, Zero Motion detection, and Free Fall detection modules. The
  2115. * signal path contains filters which must be flushed on wake-up with new
  2116. * samples before the detection modules begin operations. The default wake-up
  2117. * delay, of 4ms can be lengthened by up to 3ms. This additional delay is
  2118. * specified in ACCEL_ON_DELAY in units of 1 LSB = 1 ms. The user may select
  2119. * any value above zero unless instructed otherwise by InvenSense. Please refer
  2120. * to Section 8 of the MPU-6000/MPU-6050 Product Specification document for
  2121. * further information regarding the detection modules.
  2122. * @return Current accelerometer power-on delay
  2123. * @see MPU6050_RA_MOT_DETECT_CTRL
  2124. * @see MPU6050_DETECT_ACCEL_ON_DELAY_BIT
  2125. */
  2126. uint8_t MPUgetAccelerometerPowerOnDelay() {
  2127. I2CdevreadBits(MPUdevAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_ACCEL_ON_DELAY_BIT, MPU6050_DETECT_ACCEL_ON_DELAY_LENGTH, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2128. return MPUbuffer[0];
  2129. }
  2130. /** Set accelerometer power-on delay.
  2131. * @param delay New accelerometer power-on delay (0-3)
  2132. * @see getAccelerometerPowerOnDelay()
  2133. * @see MPU6050_RA_MOT_DETECT_CTRL
  2134. * @see MPU6050_DETECT_ACCEL_ON_DELAY_BIT
  2135. */
  2136. void MPUsetAccelerometerPowerOnDelay(uint8_t delay) {
  2137. I2CdevwriteBits(MPUdevAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_ACCEL_ON_DELAY_BIT, MPU6050_DETECT_ACCEL_ON_DELAY_LENGTH, delay);
  2138. }
  2139. /** Get Free Fall detection counter decrement configuration.
  2140. * Detection is registered by the Free Fall detection module after accelerometer
  2141. * measurements meet their respective threshold conditions over a specified
  2142. * number of samples. When the threshold conditions are met, the corresponding
  2143. * detection counter increments by 1. The user may control the rate at which the
  2144. * detection counter decrements when the threshold condition is not met by
  2145. * configuring FF_COUNT. The decrement rate can be set according to the
  2146. * following table:
  2147. *
  2148. * <pre>
  2149. * FF_COUNT | Counter Decrement
  2150. * ---------+------------------
  2151. * 0 | Reset
  2152. * 1 | 1
  2153. * 2 | 2
  2154. * 3 | 4
  2155. * </pre>
  2156. *
  2157. * When FF_COUNT is configured to 0 (reset), any non-qualifying sample will
  2158. * reset the counter to 0. For further information on Free Fall detection,
  2159. * please refer to Registers 29 to 32.
  2160. *
  2161. * @return Current decrement configuration
  2162. * @see MPU6050_RA_MOT_DETECT_CTRL
  2163. * @see MPU6050_DETECT_FF_COUNT_BIT
  2164. */
  2165. uint8_t MPUgetFreefallDetectionCounterDecrement() {
  2166. I2CdevreadBits(MPUdevAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_FF_COUNT_BIT, MPU6050_DETECT_FF_COUNT_LENGTH, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2167. return MPUbuffer[0];
  2168. }
  2169. /** Set Free Fall detection counter decrement configuration.
  2170. * @param decrement New decrement configuration value
  2171. * @see getFreefallDetectionCounterDecrement()
  2172. * @see MPU6050_RA_MOT_DETECT_CTRL
  2173. * @see MPU6050_DETECT_FF_COUNT_BIT
  2174. */
  2175. void MPUsetFreefallDetectionCounterDecrement(uint8_t decrement) {
  2176. I2CdevwriteBits(MPUdevAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_FF_COUNT_BIT, MPU6050_DETECT_FF_COUNT_LENGTH, decrement);
  2177. }
  2178. /** Get Motion detection counter decrement configuration.
  2179. * Detection is registered by the Motion detection module after accelerometer
  2180. * measurements meet their respective threshold conditions over a specified
  2181. * number of samples. When the threshold conditions are met, the corresponding
  2182. * detection counter increments by 1. The user may control the rate at which the
  2183. * detection counter decrements when the threshold condition is not met by
  2184. * configuring MOT_COUNT. The decrement rate can be set according to the
  2185. * following table:
  2186. *
  2187. * <pre>
  2188. * MOT_COUNT | Counter Decrement
  2189. * ----------+------------------
  2190. * 0 | Reset
  2191. * 1 | 1
  2192. * 2 | 2
  2193. * 3 | 4
  2194. * </pre>
  2195. *
  2196. * When MOT_COUNT is configured to 0 (reset), any non-qualifying sample will
  2197. * reset the counter to 0. For further information on Motion detection,
  2198. * please refer to Registers 29 to 32.
  2199. *
  2200. */
  2201. uint8_t MPUgetMotionDetectionCounterDecrement() {
  2202. I2CdevreadBits(MPUdevAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_MOT_COUNT_BIT, MPU6050_DETECT_MOT_COUNT_LENGTH, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2203. return MPUbuffer[0];
  2204. }
  2205. /** Set Motion detection counter decrement configuration.
  2206. * @param decrement New decrement configuration value
  2207. * @see getMotionDetectionCounterDecrement()
  2208. * @see MPU6050_RA_MOT_DETECT_CTRL
  2209. * @see MPU6050_DETECT_MOT_COUNT_BIT
  2210. */
  2211. void MPUsetMotionDetectionCounterDecrement(uint8_t decrement) {
  2212. I2CdevwriteBits(MPUdevAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_MOT_COUNT_BIT, MPU6050_DETECT_MOT_COUNT_LENGTH, decrement);
  2213. }
  2214. // USER_CTRL register
  2215. /** Get FIFO enabled status.
  2216. * When this bit is set to 0, the FIFO buffer is disabled. The FIFO buffer
  2217. * cannot be written to or read from while disabled. The FIFO buffer's state
  2218. * does not change unless the MPU-60X0 is power cycled.
  2219. * @return Current FIFO enabled status
  2220. * @see MPU6050_RA_USER_CTRL
  2221. * @see MPU6050_USERCTRL_FIFO_EN_BIT
  2222. */
  2223. bool_t MPUgetFIFOEnabled() {
  2224. I2CdevreadBit(MPUdevAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_FIFO_EN_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2225. return MPUbuffer[0];
  2226. }
  2227. /** Set FIFO enabled status.
  2228. * @param enabled New FIFO enabled status
  2229. * @see getFIFOEnabled()
  2230. * @see MPU6050_RA_USER_CTRL
  2231. * @see MPU6050_USERCTRL_FIFO_EN_BIT
  2232. */
  2233. void MPUsetFIFOEnabled(bool_t enabled) {
  2234. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_FIFO_EN_BIT, enabled);
  2235. }
  2236. /** Get I2C Master Mode enabled status.
  2237. * When this mode is enabled, the MPU-60X0 acts as the I2C Master to the
  2238. * external sensor slave devices on the auxiliary I2C bus. When this bit is
  2239. * cleared to 0, the auxiliary I2C bus lines (AUX_DA and AUX_CL) are logically
  2240. * driven by the primary I2C bus (SDA and SCL). This is a precondition to
  2241. * enabling Bypass Mode. For further information regarding Bypass Mode, please
  2242. * refer to Register 55.
  2243. * @return Current I2C Master Mode enabled status
  2244. * @see MPU6050_RA_USER_CTRL
  2245. * @see MPU6050_USERCTRL_I2C_MST_EN_BIT
  2246. */
  2247. bool_t MPUgetI2CMasterModeEnabled() {
  2248. I2CdevreadBit(MPUdevAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_I2C_MST_EN_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2249. return MPUbuffer[0];
  2250. }
  2251. /** Set I2C Master Mode enabled status.
  2252. * @param enabled New I2C Master Mode enabled status
  2253. * @see getI2CMasterModeEnabled()
  2254. * @see MPU6050_RA_USER_CTRL
  2255. * @see MPU6050_USERCTRL_I2C_MST_EN_BIT
  2256. */
  2257. void MPUsetI2CMasterModeEnabled(bool_t enabled) {
  2258. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_I2C_MST_EN_BIT, enabled);
  2259. }
  2260. /** Switch from I2C to SPI mode (MPU-6000 only)
  2261. * If this is set, the primary SPI interface will be enabled in place of the
  2262. * disabled primary I2C interface.
  2263. */
  2264. void MPUswitchSPIEnabled(bool_t enabled) {
  2265. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_I2C_IF_DIS_BIT, enabled);
  2266. }
  2267. /** Reset the FIFO.
  2268. * This bit resets the FIFO buffer when set to 1 while FIFO_EN equals 0. This
  2269. * bit automatically clears to 0 after the reset has been triggered.
  2270. * @see MPU6050_RA_USER_CTRL
  2271. * @see MPU6050_USERCTRL_FIFO_RESET_BIT
  2272. */
  2273. void MPUresetFIFO() {
  2274. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_FIFO_RESET_BIT, TRUE);
  2275. }
  2276. /** Reset the I2C Master.
  2277. * This bit resets the I2C Master when set to 1 while I2C_MST_EN equals 0.
  2278. * This bit automatically clears to 0 after the reset has been triggered.
  2279. * @see MPU6050_RA_USER_CTRL
  2280. * @see MPU6050_USERCTRL_I2C_MST_RESET_BIT
  2281. */
  2282. void MPUresetI2CMaster() {
  2283. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_I2C_MST_RESET_BIT, TRUE);
  2284. }
  2285. /** Reset all sensor registers and signal paths.
  2286. * When set to 1, this bit resets the signal paths for all sensors (gyroscopes,
  2287. * accelerometers, and temperature sensor). This operation will also clear the
  2288. * sensor registers. This bit automatically clears to 0 after the reset has been
  2289. * triggered.
  2290. *
  2291. * When resetting only the signal path (and not the sensor registers), please
  2292. * use Register 104, SIGNAL_PATH_RESET.
  2293. *
  2294. * @see MPU6050_RA_USER_CTRL
  2295. * @see MPU6050_USERCTRL_SIG_COND_RESET_BIT
  2296. */
  2297. void MPUresetSensors() {
  2298. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_SIG_COND_RESET_BIT, TRUE);
  2299. }
  2300. // PWR_MGMT_1 register
  2301. /** Trigger a full device reset.
  2302. * A small delay of ~50ms may be desirable after triggering a reset.
  2303. * @see MPU6050_RA_PWR_MGMT_1
  2304. * @see MPU6050_PWR1_DEVICE_RESET_BIT
  2305. */
  2306. void MPUreset() {
  2307. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_DEVICE_RESET_BIT, TRUE);
  2308. }
  2309. /** Get sleep mode status.
  2310. * Setting the SLEEP bit in the register puts the device into very low power
  2311. * sleep mode. In this mode, only the serial interface and internal registers
  2312. * remain active, allowing for a very low standby current. Clearing this bit
  2313. * puts the device back into normal mode. To save power, the individual standby
  2314. * selections for each of the gyros should be used if any gyro axis is not used
  2315. * by the application.
  2316. * @return Current sleep mode enabled status
  2317. * @see MPU6050_RA_PWR_MGMT_1
  2318. * @see MPU6050_PWR1_SLEEP_BIT
  2319. */
  2320. bool_t MPUgetSleepEnabled() {
  2321. I2CdevreadBit(MPUdevAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_SLEEP_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2322. return MPUbuffer[0];
  2323. }
  2324. /** Set sleep mode status.
  2325. * @param enabled New sleep mode enabled status
  2326. * @see getSleepEnabled()
  2327. * @see MPU6050_RA_PWR_MGMT_1
  2328. * @see MPU6050_PWR1_SLEEP_BIT
  2329. */
  2330. void MPUsetSleepEnabled(bool_t enabled) {
  2331. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_SLEEP_BIT, enabled);
  2332. }
  2333. /** Get wake cycle enabled status.
  2334. * When this bit is set to 1 and SLEEP is disabled, the MPU-60X0 will cycle
  2335. * between sleep mode and waking up to take a single sample of data from active
  2336. * sensors at a rate determined by LP_WAKE_CTRL (register 108).
  2337. * @return Current sleep mode enabled status
  2338. * @see MPU6050_RA_PWR_MGMT_1
  2339. * @see MPU6050_PWR1_CYCLE_BIT
  2340. */
  2341. bool_t MPUgetWakeCycleEnabled() {
  2342. I2CdevreadBit(MPUdevAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_CYCLE_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2343. return MPUbuffer[0];
  2344. }
  2345. /** Set wake cycle enabled status.
  2346. * @param enabled New sleep mode enabled status
  2347. * @see getWakeCycleEnabled()
  2348. * @see MPU6050_RA_PWR_MGMT_1
  2349. * @see MPU6050_PWR1_CYCLE_BIT
  2350. */
  2351. void MPUsetWakeCycleEnabled(bool_t enabled) {
  2352. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_CYCLE_BIT, enabled);
  2353. }
  2354. /** Get temperature sensor enabled status.
  2355. * Control the usage of the internal temperature sensor.
  2356. *
  2357. * Note: this register stores the *disabled* value, but for consistency with the
  2358. * rest of the code, the function is named and used with standard TRUE/FALSE
  2359. * values to indicate whether the sensor is enabled or disabled, respectively.
  2360. *
  2361. * @return Current temperature sensor enabled status
  2362. * @see MPU6050_RA_PWR_MGMT_1
  2363. * @see MPU6050_PWR1_TEMP_DIS_BIT
  2364. */
  2365. bool_t MPUgetTempSensorEnabled() {
  2366. I2CdevreadBit(MPUdevAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_TEMP_DIS_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2367. return MPUbuffer[0] == 0; // 1 is actually disabled here
  2368. }
  2369. /** Set temperature sensor enabled status.
  2370. * Note: this register stores the *disabled* value, but for consistency with the
  2371. * rest of the code, the function is named and used with standard TRUE/FALSE
  2372. * values to indicate whether the sensor is enabled or disabled, respectively.
  2373. *
  2374. * @param enabled New temperature sensor enabled status
  2375. * @see getTempSensorEnabled()
  2376. * @see MPU6050_RA_PWR_MGMT_1
  2377. * @see MPU6050_PWR1_TEMP_DIS_BIT
  2378. */
  2379. void MPUsetTempSensorEnabled(bool_t enabled) {
  2380. // 1 is actually disabled here
  2381. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_TEMP_DIS_BIT, !enabled);
  2382. }
  2383. /** Get clock source setting.
  2384. * @return Current clock source setting
  2385. * @see MPU6050_RA_PWR_MGMT_1
  2386. * @see MPU6050_PWR1_CLKSEL_BIT
  2387. * @see MPU6050_PWR1_CLKSEL_LENGTH
  2388. */
  2389. uint8_t MPUgetClockSource() {
  2390. I2CdevreadBits(MPUdevAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_CLKSEL_BIT, MPU6050_PWR1_CLKSEL_LENGTH, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2391. return MPUbuffer[0];
  2392. }
  2393. /** Set clock source setting.
  2394. * An internal 8MHz oscillator, gyroscope based clock, or external sources can
  2395. * be selected as the MPU-60X0 clock source. When the internal 8 MHz oscillator
  2396. * or an external source is chosen as the clock source, the MPU-60X0 can operate
  2397. * in low power modes with the gyroscopes disabled.
  2398. *
  2399. * Upon power up, the MPU-60X0 clock source defaults to the internal oscillator.
  2400. * However, it is highly recommended that the device be configured to use one of
  2401. * the gyroscopes (or an external clock source) as the clock reference for
  2402. * improved stability. The clock source can be selected according to the following table:
  2403. *
  2404. * <pre>
  2405. * CLK_SEL | Clock Source
  2406. * --------+--------------------------------------
  2407. * 0 | Internal oscillator
  2408. * 1 | PLL with X Gyro reference
  2409. * 2 | PLL with Y Gyro reference
  2410. * 3 | PLL with Z Gyro reference
  2411. * 4 | PLL with external 32.768kHz reference
  2412. * 5 | PLL with external 19.2MHz reference
  2413. * 6 | Reserved
  2414. * 7 | Stops the clock and keeps the timing generator in reset
  2415. * </pre>
  2416. *
  2417. * @param source New clock source setting
  2418. * @see getClockSource()
  2419. * @see MPU6050_RA_PWR_MGMT_1
  2420. * @see MPU6050_PWR1_CLKSEL_BIT
  2421. * @see MPU6050_PWR1_CLKSEL_LENGTH
  2422. */
  2423. void MPUsetClockSource(uint8_t source) {
  2424. I2CdevwriteBits(MPUdevAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_CLKSEL_BIT, MPU6050_PWR1_CLKSEL_LENGTH, source);
  2425. }
  2426. // PWR_MGMT_2 register
  2427. /** Get wake frequency in Accel-Only Low Power Mode.
  2428. * The MPU-60X0 can be put into Accerlerometer Only Low Power Mode by setting
  2429. * PWRSEL to 1 in the Power Management 1 register (Register 107). In this mode,
  2430. * the device will power off all devices except for the primary I2C interface,
  2431. * waking only the accelerometer at fixed intervals to take a single
  2432. * measurement. The frequency of wake-ups can be configured with LP_WAKE_CTRL
  2433. * as shown below:
  2434. *
  2435. * <pre>
  2436. * LP_WAKE_CTRL | Wake-up Frequency
  2437. * -------------+------------------
  2438. * 0 | 1.25 Hz
  2439. * 1 | 2.5 Hz
  2440. * 2 | 5 Hz
  2441. * 3 | 10 Hz
  2442. * <pre>
  2443. *
  2444. * For further information regarding the MPU-60X0's power modes, please refer to
  2445. * Register 107.
  2446. *
  2447. * @return Current wake frequency
  2448. * @see MPU6050_RA_PWR_MGMT_2
  2449. */
  2450. uint8_t MPUgetWakeFrequency() {
  2451. I2CdevreadBits(MPUdevAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_LP_WAKE_CTRL_BIT, MPU6050_PWR2_LP_WAKE_CTRL_LENGTH, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2452. return MPUbuffer[0];
  2453. }
  2454. /** Set wake frequency in Accel-Only Low Power Mode.
  2455. * @param frequency New wake frequency
  2456. * @see MPU6050_RA_PWR_MGMT_2
  2457. */
  2458. void MPUsetWakeFrequency(uint8_t frequency) {
  2459. I2CdevwriteBits(MPUdevAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_LP_WAKE_CTRL_BIT, MPU6050_PWR2_LP_WAKE_CTRL_LENGTH, frequency);
  2460. }
  2461. /** Get X-axis accelerometer standby enabled status.
  2462. * If enabled, the X-axis will not gather or report data (or use power).
  2463. * @return Current X-axis standby enabled status
  2464. * @see MPU6050_RA_PWR_MGMT_2
  2465. * @see MPU6050_PWR2_STBY_XA_BIT
  2466. */
  2467. bool_t MPUgetStandbyXAccelEnabled() {
  2468. I2CdevreadBit(MPUdevAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_XA_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2469. return MPUbuffer[0];
  2470. }
  2471. /** Set X-axis accelerometer standby enabled status.
  2472. * @param New X-axis standby enabled status
  2473. * @see getStandbyXAccelEnabled()
  2474. * @see MPU6050_RA_PWR_MGMT_2
  2475. * @see MPU6050_PWR2_STBY_XA_BIT
  2476. */
  2477. void MPUsetStandbyXAccelEnabled(bool_t enabled) {
  2478. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_XA_BIT, enabled);
  2479. }
  2480. /** Get Y-axis accelerometer standby enabled status.
  2481. * If enabled, the Y-axis will not gather or report data (or use power).
  2482. * @return Current Y-axis standby enabled status
  2483. * @see MPU6050_RA_PWR_MGMT_2
  2484. * @see MPU6050_PWR2_STBY_YA_BIT
  2485. */
  2486. bool_t MPUgetStandbyYAccelEnabled() {
  2487. I2CdevreadBit(MPUdevAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_YA_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2488. return MPUbuffer[0];
  2489. }
  2490. /** Set Y-axis accelerometer standby enabled status.
  2491. * @param New Y-axis standby enabled status
  2492. * @see getStandbyYAccelEnabled()
  2493. * @see MPU6050_RA_PWR_MGMT_2
  2494. * @see MPU6050_PWR2_STBY_YA_BIT
  2495. */
  2496. void MPUsetStandbyYAccelEnabled(bool_t enabled) {
  2497. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_YA_BIT, enabled);
  2498. }
  2499. /** Get Z-axis accelerometer standby enabled status.
  2500. * If enabled, the Z-axis will not gather or report data (or use power).
  2501. * @return Current Z-axis standby enabled status
  2502. * @see MPU6050_RA_PWR_MGMT_2
  2503. * @see MPU6050_PWR2_STBY_ZA_BIT
  2504. */
  2505. bool_t MPUgetStandbyZAccelEnabled() {
  2506. I2CdevreadBit(MPUdevAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_ZA_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2507. return MPUbuffer[0];
  2508. }
  2509. /** Set Z-axis accelerometer standby enabled status.
  2510. * @param New Z-axis standby enabled status
  2511. * @see getStandbyZAccelEnabled()
  2512. * @see MPU6050_RA_PWR_MGMT_2
  2513. * @see MPU6050_PWR2_STBY_ZA_BIT
  2514. */
  2515. void MPUsetStandbyZAccelEnabled(bool_t enabled) {
  2516. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_ZA_BIT, enabled);
  2517. }
  2518. /** Get X-axis gyroscope standby enabled status.
  2519. * If enabled, the X-axis will not gather or report data (or use power).
  2520. * @return Current X-axis standby enabled status
  2521. * @see MPU6050_RA_PWR_MGMT_2
  2522. * @see MPU6050_PWR2_STBY_XG_BIT
  2523. */
  2524. bool_t MPUgetStandbyXGyroEnabled() {
  2525. I2CdevreadBit(MPUdevAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_XG_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2526. return MPUbuffer[0];
  2527. }
  2528. /** Set X-axis gyroscope standby enabled status.
  2529. * @param New X-axis standby enabled status
  2530. * @see getStandbyXGyroEnabled()
  2531. * @see MPU6050_RA_PWR_MGMT_2
  2532. * @see MPU6050_PWR2_STBY_XG_BIT
  2533. */
  2534. void MPUsetStandbyXGyroEnabled(bool_t enabled) {
  2535. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_XG_BIT, enabled);
  2536. }
  2537. /** Get Y-axis gyroscope standby enabled status.
  2538. * If enabled, the Y-axis will not gather or report data (or use power).
  2539. * @return Current Y-axis standby enabled status
  2540. * @see MPU6050_RA_PWR_MGMT_2
  2541. * @see MPU6050_PWR2_STBY_YG_BIT
  2542. */
  2543. bool_t MPUgetStandbyYGyroEnabled() {
  2544. I2CdevreadBit(MPUdevAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_YG_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2545. return MPUbuffer[0];
  2546. }
  2547. /** Set Y-axis gyroscope standby enabled status.
  2548. * @param New Y-axis standby enabled status
  2549. * @see getStandbyYGyroEnabled()
  2550. * @see MPU6050_RA_PWR_MGMT_2
  2551. * @see MPU6050_PWR2_STBY_YG_BIT
  2552. */
  2553. void MPUsetStandbyYGyroEnabled(bool_t enabled) {
  2554. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_YG_BIT, enabled);
  2555. }
  2556. /** Get Z-axis gyroscope standby enabled status.
  2557. * If enabled, the Z-axis will not gather or report data (or use power).
  2558. * @return Current Z-axis standby enabled status
  2559. * @see MPU6050_RA_PWR_MGMT_2
  2560. * @see MPU6050_PWR2_STBY_ZG_BIT
  2561. */
  2562. bool_t MPUgetStandbyZGyroEnabled() {
  2563. I2CdevreadBit(MPUdevAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_ZG_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2564. return MPUbuffer[0];
  2565. }
  2566. /** Set Z-axis gyroscope standby enabled status.
  2567. * @param New Z-axis standby enabled status
  2568. * @see getStandbyZGyroEnabled()
  2569. * @see MPU6050_RA_PWR_MGMT_2
  2570. * @see MPU6050_PWR2_STBY_ZG_BIT
  2571. */
  2572. void MPUsetStandbyZGyroEnabled(bool_t enabled) {
  2573. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_ZG_BIT, enabled);
  2574. }
  2575. // FIFO_COUNT* registers
  2576. /** Get current FIFO buffer size.
  2577. * This value indicates the number of bytes stored in the FIFO buffer. This
  2578. * number is in turn the number of bytes that can be read from the FIFO buffer
  2579. * and it is directly proportional to the number of samples available given the
  2580. * set of sensor data bound to be stored in the FIFO (register 35 and 36).
  2581. * @return Current FIFO buffer size
  2582. */
  2583. uint16_t MPUgetFIFOCount() {
  2584. I2CdevreadBytes(MPUdevAddr, MPU6050_RA_FIFO_COUNTH, 2, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2585. return (((uint16_t)MPUbuffer[0]) << 8) | MPUbuffer[1];
  2586. }
  2587. // FIFO_R_W register
  2588. /** Get byte from FIFO buffer.
  2589. * This register is used to read and write data from the FIFO buffer. Data is
  2590. * written to the FIFO in order of register number (from lowest to highest). If
  2591. * all the FIFO enable flags (see below) are enabled and all External Sensor
  2592. * Data registers (Registers 73 to 96) are associated with a Slave device, the
  2593. * contents of registers 59 through 96 will be written in order at the Sample
  2594. * Rate.
  2595. *
  2596. * The contents of the sensor data registers (Registers 59 to 96) are written
  2597. * into the FIFO buffer when their corresponding FIFO enable flags are set to 1
  2598. * in FIFO_EN (Register 35). An additional flag for the sensor data registers
  2599. * associated with I2C Slave 3 can be found in I2C_MST_CTRL (Register 36).
  2600. *
  2601. * If the FIFO buffer has overflowed, the status bit FIFO_OFLOW_INT is
  2602. * automatically set to 1. This bit is located in INT_STATUS (Register 58).
  2603. * When the FIFO buffer has overflowed, the oldest data will be lost and new
  2604. * data will be written to the FIFO.
  2605. *
  2606. * If the FIFO buffer is empty, reading this register will return the last byte
  2607. * that was previously read from the FIFO until new data is available. The user
  2608. * should check FIFO_COUNT to ensure that the FIFO buffer is not read when
  2609. * empty.
  2610. *
  2611. * @return Byte from FIFO buffer
  2612. */
  2613. uint8_t MPUgetFIFOByte() {
  2614. I2CdevreadByte(MPUdevAddr, MPU6050_RA_FIFO_R_W, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2615. return MPUbuffer[0];
  2616. }
  2617. void MPUgetFIFOBytes(uint8_t *data, uint8_t length) {
  2618. I2CdevreadBytes(MPUdevAddr, MPU6050_RA_FIFO_R_W, length, data, I2CDEV_DEFAULT_READ_TIMEOUT);
  2619. }
  2620. /** Write byte to FIFO buffer.
  2621. * @see getFIFOByte()
  2622. * @see MPU6050_RA_FIFO_R_W
  2623. */
  2624. void MPUsetFIFOByte(uint8_t data) {
  2625. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_FIFO_R_W, data);
  2626. }
  2627. // WHO_AM_I register
  2628. /** Get Device ID.
  2629. * This register is used to verify the identity of the device (0b110100, 0x34).
  2630. * @return Device ID (6 bits only! should be 0x34)
  2631. * @see MPU6050_RA_WHO_AM_I
  2632. * @see MPU6050_WHO_AM_I_BIT
  2633. * @see MPU6050_WHO_AM_I_LENGTH
  2634. */
  2635. uint8_t MPUgetDeviceID() {
  2636. I2CdevreadBits(MPUdevAddr, MPU6050_RA_WHO_AM_I, MPU6050_WHO_AM_I_BIT, MPU6050_WHO_AM_I_LENGTH, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2637. return MPUbuffer[0];
  2638. }
  2639. /** Set Device ID.
  2640. * Write a new ID into the WHO_AM_I register (no idea why this should ever be
  2641. * necessary though).
  2642. * @param id New device ID to set.
  2643. * @see getDeviceID()
  2644. * @see MPU6050_RA_WHO_AM_I
  2645. * @see MPU6050_WHO_AM_I_BIT
  2646. * @see MPU6050_WHO_AM_I_LENGTH
  2647. */
  2648. void MPUsetDeviceID(uint8_t id) {
  2649. I2CdevwriteBits(MPUdevAddr, MPU6050_RA_WHO_AM_I, MPU6050_WHO_AM_I_BIT, MPU6050_WHO_AM_I_LENGTH, id);
  2650. }
  2651. // ======== UNDOCUMENTED/DMP REGISTERS/METHODS ========
  2652. // XG_OFFS_TC register
  2653. uint8_t MPUgetOTPBankValid() {
  2654. I2CdevreadBit(MPUdevAddr, MPU6050_RA_XG_OFFS_TC, MPU6050_TC_OTP_BNK_VLD_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2655. return MPUbuffer[0];
  2656. }
  2657. void MPUsetOTPBankValid(bool_t enabled) {
  2658. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_XG_OFFS_TC, MPU6050_TC_OTP_BNK_VLD_BIT, enabled);
  2659. }
  2660. int8_t MPUgetXGyroOffset() {
  2661. I2CdevreadBits(MPUdevAddr, MPU6050_RA_XG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2662. return MPUbuffer[0];
  2663. }
  2664. void MPUsetXGyroOffset(int8_t offset) {
  2665. I2CdevwriteBits(MPUdevAddr, MPU6050_RA_XG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, offset);
  2666. }
  2667. // YG_OFFS_TC register
  2668. int8_t MPUgetYGyroOffset() {
  2669. I2CdevreadBits(MPUdevAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2670. return MPUbuffer[0];
  2671. }
  2672. void MPUsetYGyroOffset(int8_t offset) {
  2673. I2CdevwriteBits(MPUdevAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, offset);
  2674. }
  2675. // ZG_OFFS_TC register
  2676. int8_t MPUgetZGyroOffset() {
  2677. I2CdevreadBits(MPUdevAddr, MPU6050_RA_ZG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2678. return MPUbuffer[0];
  2679. }
  2680. void MPUsetZGyroOffset(int8_t offset) {
  2681. I2CdevwriteBits(MPUdevAddr, MPU6050_RA_ZG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, offset);
  2682. }
  2683. // X_FINE_GAIN register
  2684. int8_t MPUgetXFineGain() {
  2685. I2CdevreadByte(MPUdevAddr, MPU6050_RA_X_FINE_GAIN, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2686. return MPUbuffer[0];
  2687. }
  2688. void MPUsetXFineGain(int8_t gain) {
  2689. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_X_FINE_GAIN, gain);
  2690. }
  2691. // Y_FINE_GAIN register
  2692. int8_t MPUgetYFineGain() {
  2693. I2CdevreadByte(MPUdevAddr, MPU6050_RA_Y_FINE_GAIN, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2694. return MPUbuffer[0];
  2695. }
  2696. void MPUsetYFineGain(int8_t gain) {
  2697. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_Y_FINE_GAIN, gain);
  2698. }
  2699. // Z_FINE_GAIN register
  2700. int8_t MPUgetZFineGain() {
  2701. I2CdevreadByte(MPUdevAddr, MPU6050_RA_Z_FINE_GAIN, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2702. return MPUbuffer[0];
  2703. }
  2704. void MPUsetZFineGain(int8_t gain) {
  2705. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_Z_FINE_GAIN, gain);
  2706. }
  2707. // XA_OFFS_* registers
  2708. int16_t MPUgetXAccelOffset() {
  2709. I2CdevreadBytes(MPUdevAddr, MPU6050_RA_XA_OFFS_H, 2, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2710. return (((int16_t)MPUbuffer[0]) << 8) | MPUbuffer[1];
  2711. }
  2712. void MPUsetXAccelOffset(int16_t offset) {
  2713. I2CdevwriteWord(MPUdevAddr, MPU6050_RA_XA_OFFS_H, offset);
  2714. }
  2715. // YA_OFFS_* register
  2716. int16_t MPUgetYAccelOffset() {
  2717. I2CdevreadBytes(MPUdevAddr, MPU6050_RA_YA_OFFS_H, 2, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2718. return (((int16_t)MPUbuffer[0]) << 8) | MPUbuffer[1];
  2719. }
  2720. void MPUsetYAccelOffset(int16_t offset) {
  2721. I2CdevwriteWord(MPUdevAddr, MPU6050_RA_YA_OFFS_H, offset);
  2722. }
  2723. // ZA_OFFS_* register
  2724. int16_t MPUgetZAccelOffset() {
  2725. I2CdevreadBytes(MPUdevAddr, MPU6050_RA_ZA_OFFS_H, 2, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2726. return (((int16_t)MPUbuffer[0]) << 8) | MPUbuffer[1];
  2727. }
  2728. void MPUsetZAccelOffset(int16_t offset) {
  2729. I2CdevwriteWord(MPUdevAddr, MPU6050_RA_ZA_OFFS_H, offset);
  2730. }
  2731. // XG_OFFS_USR* registers
  2732. int16_t MPUgetXGyroOffsetUser() {
  2733. I2CdevreadBytes(MPUdevAddr, MPU6050_RA_XG_OFFS_USRH, 2, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2734. return (((int16_t)MPUbuffer[0]) << 8) | MPUbuffer[1];
  2735. }
  2736. void MPUsetXGyroOffsetUser(int16_t offset) {
  2737. I2CdevwriteWord(MPUdevAddr, MPU6050_RA_XG_OFFS_USRH, offset);
  2738. }
  2739. // YG_OFFS_USR* register
  2740. int16_t MPUgetYGyroOffsetUser() {
  2741. I2CdevreadBytes(MPUdevAddr, MPU6050_RA_YG_OFFS_USRH, 2, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2742. return (((int16_t)MPUbuffer[0]) << 8) | MPUbuffer[1];
  2743. }
  2744. void MPUsetYGyroOffsetUser(int16_t offset) {
  2745. I2CdevwriteWord(MPUdevAddr, MPU6050_RA_YG_OFFS_USRH, offset);
  2746. }
  2747. // ZG_OFFS_USR* register
  2748. int16_t MPUgetZGyroOffsetUser() {
  2749. I2CdevreadBytes(MPUdevAddr, MPU6050_RA_ZG_OFFS_USRH, 2, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2750. return (((int16_t)MPUbuffer[0]) << 8) | MPUbuffer[1];
  2751. }
  2752. void MPUsetZGyroOffsetUser(int16_t offset) {
  2753. I2CdevwriteWord(MPUdevAddr, MPU6050_RA_ZG_OFFS_USRH, offset);
  2754. }
  2755. // INT_ENABLE register (DMP functions)
  2756. bool_t MPUgetIntPLLReadyEnabled() {
  2757. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_PLL_RDY_INT_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2758. return MPUbuffer[0];
  2759. }
  2760. void MPUsetIntPLLReadyEnabled(bool_t enabled) {
  2761. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_PLL_RDY_INT_BIT, enabled);
  2762. }
  2763. bool_t MPUgetIntDMPEnabled() {
  2764. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_DMP_INT_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2765. return MPUbuffer[0];
  2766. }
  2767. void MPUsetIntDMPEnabled(bool_t enabled) {
  2768. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_DMP_INT_BIT, enabled);
  2769. }
  2770. // DMP_INT_STATUS
  2771. bool_t MPUgetDMPInt5Status() {
  2772. I2CdevreadBit(MPUdevAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_5_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2773. return MPUbuffer[0];
  2774. }
  2775. bool_t MPUgetDMPInt4Status() {
  2776. I2CdevreadBit(MPUdevAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_4_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2777. return MPUbuffer[0];
  2778. }
  2779. bool_t MPUgetDMPInt3Status() {
  2780. I2CdevreadBit(MPUdevAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_3_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2781. return MPUbuffer[0];
  2782. }
  2783. bool_t MPUgetDMPInt2Status() {
  2784. I2CdevreadBit(MPUdevAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_2_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2785. return MPUbuffer[0];
  2786. }
  2787. bool_t MPUgetDMPInt1Status() {
  2788. I2CdevreadBit(MPUdevAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_1_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2789. return MPUbuffer[0];
  2790. }
  2791. bool_t MPUgetDMPInt0Status() {
  2792. I2CdevreadBit(MPUdevAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_0_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2793. return MPUbuffer[0];
  2794. }
  2795. // INT_STATUS register (DMP functions)
  2796. bool_t MPUgetIntPLLReadyStatus() {
  2797. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_PLL_RDY_INT_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2798. return MPUbuffer[0];
  2799. }
  2800. bool_t MPUgetIntDMPStatus() {
  2801. I2CdevreadBit(MPUdevAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_DMP_INT_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2802. return MPUbuffer[0];
  2803. }
  2804. // USER_CTRL register (DMP functions)
  2805. bool_t MPUgetDMPEnabled() {
  2806. I2CdevreadBit(MPUdevAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_DMP_EN_BIT, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2807. return MPUbuffer[0];
  2808. }
  2809. void MPUsetDMPEnabled(bool_t enabled) {
  2810. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_DMP_EN_BIT, enabled);
  2811. }
  2812. void MPUresetDMP() {
  2813. I2CdevwriteBit(MPUdevAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_DMP_RESET_BIT, TRUE);
  2814. }
  2815. // BANK_SEL register
  2816. void MPUsetMemoryBank(uint8_t bank, bool_t prefetchEnabled, bool_t userBank) {
  2817. bank &= 0x1F;
  2818. if (userBank) bank |= 0x20;
  2819. if (prefetchEnabled) bank |= 0x40;
  2820. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_BANK_SEL, bank);
  2821. }
  2822. // MEM_START_ADDR register
  2823. void MPUsetMemoryStartAddress(uint8_t address) {
  2824. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_MEM_START_ADDR, address);
  2825. }
  2826. // MEM_R_W register
  2827. uint8_t MPUreadMemoryByte() {
  2828. I2CdevreadByte(MPUdevAddr, MPU6050_RA_MEM_R_W, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2829. return MPUbuffer[0];
  2830. }
  2831. void MPUwriteMemoryByte(uint8_t data) {
  2832. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_MEM_R_W, data);
  2833. }
  2834. void MPUreadMemoryBlock(uint8_t *data, uint16_t dataSize, uint8_t bank, uint8_t address) {
  2835. uint8_t chunkSize;
  2836. uint16_t i;
  2837. MPUsetMemoryBank(bank, FALSE, FALSE);
  2838. MPUsetMemoryStartAddress(address);
  2839. for (i = 0; i < dataSize;) {
  2840. // determine correct chunk size according to bank position and data size
  2841. chunkSize = MPU6050_DMP_MEMORY_CHUNK_SIZE;
  2842. // make sure we don't go past the data size
  2843. if (i + chunkSize > dataSize) chunkSize = dataSize - i;
  2844. // make sure this chunk doesn't go past the bank boundary (256 bytes)
  2845. if (chunkSize > 256 - address) chunkSize = 256 - address;
  2846. // read the chunk of data as specified
  2847. I2CdevreadBytes(MPUdevAddr, MPU6050_RA_MEM_R_W, chunkSize, data + i, I2CDEV_DEFAULT_READ_TIMEOUT);
  2848. // increase byte index by [chunkSize]
  2849. i += chunkSize;
  2850. // uint8_t automatically wraps to 0 at 256
  2851. address += chunkSize;
  2852. // if we aren't done, update bank (if necessary) and address
  2853. if (i < dataSize) {
  2854. if (address == 0) bank++;
  2855. MPUsetMemoryBank(bank, FALSE, FALSE);
  2856. MPUsetMemoryStartAddress(address);
  2857. }
  2858. }
  2859. }
  2860. bool_t MPUwriteMemoryBlock(const uint8_t *data, uint16_t dataSize, uint8_t bank, uint8_t address, bool_t verify, bool_t useProgMem) {
  2861. /*
  2862. verifyBuffer and progBuffer malloc/free has been removed, ProgMem support removed
  2863. */
  2864. uint8_t chunkSize;
  2865. uint16_t i;
  2866. MPUsetMemoryBank(bank, FALSE, FALSE);
  2867. MPUsetMemoryStartAddress(address);
  2868. for (i = 0; i < dataSize;) {
  2869. // determine correct chunk size according to bank position and data size
  2870. chunkSize = MPU6050_DMP_MEMORY_CHUNK_SIZE;
  2871. // make sure we don't go past the data size
  2872. if (i + chunkSize > dataSize) chunkSize = dataSize - i;
  2873. // make sure this chunk doesn't go past the bank boundary (256 bytes)
  2874. if (chunkSize > 256 - address) chunkSize = 256 - address;
  2875. // write the chunk of data as specified
  2876. // MPUprogBuffer = (uint8_t *)data + i;
  2877. I2CdevwriteBytes(MPUdevAddr, MPU6050_RA_MEM_R_W, chunkSize, (uint8_t *)data + i);
  2878. // verify data if needed
  2879. if (verify) {
  2880. MPUsetMemoryBank(bank, FALSE, FALSE);
  2881. MPUsetMemoryStartAddress(address);
  2882. I2CdevreadBytes(MPUdevAddr, MPU6050_RA_MEM_R_W, chunkSize, MPUverifyBuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2883. if (memcmp((uint8_t *)data + i, MPUverifyBuffer, chunkSize) != 0) {
  2884. //chprintf((BaseChannel *)&SD2, "******** verify error! ********");
  2885. return FALSE; // uh oh.
  2886. }
  2887. }
  2888. // increase byte index by [chunkSize]
  2889. i += chunkSize;
  2890. // uint8_t automatically wraps to 0 at 256
  2891. address += chunkSize;
  2892. // if we aren't done, update bank (if necessary) and address
  2893. if (i < dataSize) {
  2894. if (address == 0) bank++;
  2895. MPUsetMemoryBank(bank, FALSE, FALSE);
  2896. MPUsetMemoryStartAddress(address);
  2897. }
  2898. }
  2899. return TRUE;
  2900. }
  2901. bool_t MPUwriteProgMemoryBlock(const uint8_t *data, uint16_t dataSize, uint8_t bank, uint8_t address, bool_t verify) {
  2902. return MPUwriteMemoryBlock(data, dataSize, bank, address, verify, TRUE);
  2903. }
  2904. bool_t MPUwriteDMPConfigurationSet(const uint8_t *data, uint16_t dataSize, bool_t useProgMem) {
  2905. uint8_t success, special;
  2906. uint16_t i;
  2907. // config set data is a long string of blocks with the following structure:
  2908. // [bank] [offset] [length] [byte[0], byte[1], ..., byte[length]]
  2909. uint8_t bank, offset, length;
  2910. for (i = 0; i < dataSize;) {
  2911. bank = data[i++];
  2912. offset = data[i++];
  2913. length = data[i++];
  2914. // write data or perform special action
  2915. if (length > 0) {
  2916. // MPUprogBuffer = (uint8_t *)data + i;
  2917. /* too few arguments in function call? added FALSE at the end */
  2918. success = MPUwriteMemoryBlock((uint8_t *)data + i, length, bank, offset, TRUE, FALSE);
  2919. i += length;
  2920. } else {
  2921. // special instruction
  2922. // NOTE: this kind of behavior (what and when to do certain things)
  2923. // is totally undocumented. This code is in here based on observed
  2924. // behavior only, and exactly why (or even whether) it has to be here
  2925. // is anybody's guess for now.
  2926. special = data[i++];
  2927. /*Serial.print("Special command code ");
  2928. Serial.print(special, HEX);
  2929. Serial.println(" found...");*/
  2930. if (special == 0x01) {
  2931. // enable DMP-related interrupts
  2932. //setIntZeroMotionEnabled(TRUE);
  2933. //setIntFIFOBufferOverflowEnabled(TRUE);
  2934. //setIntDMPEnabled(TRUE);
  2935. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_INT_ENABLE, 0x32); // single operation
  2936. success = TRUE;
  2937. } else {
  2938. // unknown special command
  2939. success = FALSE;
  2940. }
  2941. }
  2942. if (!success) {
  2943. return FALSE; // uh oh
  2944. }
  2945. }
  2946. return TRUE;
  2947. }
  2948. bool_t MPUwriteProgDMPConfigurationSet(const uint8_t *data, uint16_t dataSize) {
  2949. return MPUwriteDMPConfigurationSet(data, dataSize, TRUE);
  2950. }
  2951. // DMP_CFG_1 register
  2952. uint8_t MPUgetDMPConfig1() {
  2953. I2CdevreadByte(MPUdevAddr, MPU6050_RA_DMP_CFG_1, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2954. return MPUbuffer[0];
  2955. }
  2956. void MPUsetDMPConfig1(uint8_t config) {
  2957. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_DMP_CFG_1, config);
  2958. }
  2959. // DMP_CFG_2 register
  2960. uint8_t MPUgetDMPConfig2() {
  2961. I2CdevreadByte(MPUdevAddr, MPU6050_RA_DMP_CFG_2, MPUbuffer, I2CDEV_DEFAULT_READ_TIMEOUT);
  2962. return MPUbuffer[0];
  2963. }
  2964. void MPUsetDMPConfig2(uint8_t config) {
  2965. I2CdevwriteByte(MPUdevAddr, MPU6050_RA_DMP_CFG_2, config);
  2966. }