MPU6050.cpp 124 KB

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