PageRenderTime 84ms CodeModel.GetById 24ms 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

Large files files are truncated, but you can click here to view the full file

  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. * …

Large files files are truncated, but you can click here to view the full file