inv_mpu.c 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298
  1. /*
  2. $License:
  3. Copyright (C) 2011-2012 InvenSense Corporation, All Rights Reserved.
  4. See included License.txt for License information.
  5. $
  6. */
  7. /**
  8. * @addtogroup DRIVERS Sensor Driver Layer
  9. * @brief Hardware drivers to communicate with sensors via I2C.
  10. *
  11. * @{
  12. * @file inv_mpu.c
  13. * @brief An I2C-based driver for Invensense gyroscopes.
  14. * @details This driver currently works for the following devices:
  15. * MPU6050
  16. * MPU6500
  17. * MPU9150 (or MPU6050 w/ AK8975 on the auxiliary bus)
  18. * MPU9250 (or MPU6500 w/ AK8963 on the auxiliary bus)
  19. */
  20. #include <stdio.h>
  21. #include <stdint.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <math.h>
  25. #include "inv_mpu.h"
  26. // #define MD_DEBUG
  27. #define LOG_TAG "md.inv"
  28. #include "md_log.h"
  29. #include "MD_Ported_to_RTT.h"
  30. // #define MPU6050
  31. /* The following functions must be defined for this platform:
  32. * i2c_write(unsigned char slave_addr, unsigned char reg_addr,
  33. * unsigned char length, unsigned char const *data)
  34. * i2c_read(unsigned char slave_addr, unsigned char reg_addr,
  35. * unsigned char length, unsigned char *data)
  36. * delay_ms(unsigned long num_ms)
  37. * get_ms(unsigned long *count)
  38. * reg_int_cb(void (*cb)(void), unsigned char port, unsigned char pin)
  39. * labs(long x)
  40. * fabsf(float x)
  41. * min(int a, int b)
  42. */
  43. #define EMPL_TARGET_STM32F4
  44. #if defined EMPL_TARGET_STM32F4
  45. // #include "i2c.h"
  46. // #include "main.h"
  47. // #include "log.h"
  48. // #include "board-st_discovery.h"
  49. // #define i2c_write Sensors_I2C_WriteRegister
  50. // #define i2c_read Sensors_I2C_ReadRegister
  51. // #define delay_ms mdelay
  52. // #define get_ms get_tick_count
  53. // #define log_i MPL_LOGI
  54. // #define log_e MPL_LOGE
  55. // #define min(a,b) ((a<b)?a:b)
  56. #elif defined MOTION_DRIVER_TARGET_MSP430
  57. #include "msp430.h"
  58. #include "msp430_i2c.h"
  59. #include "msp430_clock.h"
  60. #include "msp430_interrupt.h"
  61. #define i2c_write msp430_i2c_write
  62. #define i2c_read msp430_i2c_read
  63. #define delay_ms msp430_delay_ms
  64. #define get_ms msp430_get_clock_ms
  65. static inline int reg_int_cb(struct int_param_s *int_param)
  66. {
  67. return msp430_reg_int_cb(int_param->cb, int_param->pin, int_param->lp_exit,
  68. int_param->active_low);
  69. }
  70. #define log_i(...) do {} while (0)
  71. #define log_e(...) do {} while (0)
  72. /* labs is already defined by TI's toolchain. */
  73. /* fabs is for doubles. fabsf is for floats. */
  74. #define fabs fabsf
  75. #define min(a,b) ((a<b)?a:b)
  76. #elif defined EMPL_TARGET_MSP430
  77. #include "msp430.h"
  78. #include "msp430_i2c.h"
  79. #include "msp430_clock.h"
  80. #include "msp430_interrupt.h"
  81. #include "log.h"
  82. #define i2c_write msp430_i2c_write
  83. #define i2c_read msp430_i2c_read
  84. #define delay_ms msp430_delay_ms
  85. #define get_ms msp430_get_clock_ms
  86. static inline int reg_int_cb(struct int_param_s *int_param)
  87. {
  88. return msp430_reg_int_cb(int_param->cb, int_param->pin, int_param->lp_exit,
  89. int_param->active_low);
  90. }
  91. #define log_i MPL_LOGI
  92. #define log_e MPL_LOGE
  93. /* labs is already defined by TI's toolchain. */
  94. /* fabs is for doubles. fabsf is for floats. */
  95. #define fabs fabsf
  96. #define min(a,b) ((a<b)?a:b)
  97. #elif defined EMPL_TARGET_UC3L0
  98. /* Instead of using the standard TWI driver from the ASF library, we're using
  99. * a TWI driver that follows the slave address + register address convention.
  100. */
  101. #include "twi.h"
  102. #include "delay.h"
  103. #include "sysclk.h"
  104. #include "log.h"
  105. #include "sensors_xplained.h"
  106. #include "uc3l0_clock.h"
  107. #define i2c_write(a, b, c, d) twi_write(a, b, d, c)
  108. #define i2c_read(a, b, c, d) twi_read(a, b, d, c)
  109. /* delay_ms is a function already defined in ASF. */
  110. #define get_ms uc3l0_get_clock_ms
  111. static inline int reg_int_cb(struct int_param_s *int_param)
  112. {
  113. sensor_board_irq_connect(int_param->pin, int_param->cb, int_param->arg);
  114. return 0;
  115. }
  116. #define log_i MPL_LOGI
  117. #define log_e MPL_LOGE
  118. /* UC3 is a 32-bit processor, so abs and labs are equivalent. */
  119. #define labs abs
  120. #define fabs(x) (((x)>0)?(x):-(x))
  121. #else
  122. #error Gyro driver is missing the system layer implementations.
  123. #endif
  124. #if !defined MPU6050 && !defined MPU9150 && !defined MPU6500 && !defined MPU9250
  125. #error Which gyro are you using? Define MPUxxxx in your compiler options.
  126. #endif
  127. /* Time for some messy macro work. =]
  128. * #define MPU9150
  129. * is equivalent to..
  130. * #define MPU6050
  131. * #define AK8975_SECONDARY
  132. *
  133. * #define MPU9250
  134. * is equivalent to..
  135. * #define MPU6500
  136. * #define AK8963_SECONDARY
  137. */
  138. #if defined MPU9150
  139. #ifndef MPU6050
  140. #define MPU6050
  141. #endif /* #ifndef MPU6050 */
  142. #if defined AK8963_SECONDARY
  143. #error "MPU9150 and AK8963_SECONDARY cannot both be defined."
  144. #elif !defined AK8975_SECONDARY /* #if defined AK8963_SECONDARY */
  145. #define AK8975_SECONDARY
  146. #endif /* #if defined AK8963_SECONDARY */
  147. #elif defined MPU9250 /* #if defined MPU9150 */
  148. #ifndef MPU6500
  149. #define MPU6500
  150. #endif /* #ifndef MPU6500 */
  151. #if defined AK8975_SECONDARY
  152. #error "MPU9250 and AK8975_SECONDARY cannot both be defined."
  153. #elif !defined AK8963_SECONDARY /* #if defined AK8975_SECONDARY */
  154. #define AK8963_SECONDARY
  155. #endif /* #if defined AK8975_SECONDARY */
  156. #endif /* #if defined MPU9150 */
  157. #if defined AK8975_SECONDARY || defined AK8963_SECONDARY
  158. #define AK89xx_SECONDARY
  159. #else
  160. /* #warning "No compass = less profit for Invensense. Lame." */
  161. #endif
  162. static int set_int_enable(unsigned char enable);
  163. /* Hardware registers needed by driver. */
  164. struct gyro_reg_s {
  165. unsigned char who_am_i;
  166. unsigned char rate_div;
  167. unsigned char lpf;
  168. unsigned char prod_id;
  169. unsigned char user_ctrl;
  170. unsigned char fifo_en;
  171. unsigned char gyro_cfg;
  172. unsigned char accel_cfg;
  173. unsigned char accel_cfg2;
  174. unsigned char lp_accel_odr;
  175. unsigned char motion_thr;
  176. unsigned char motion_dur;
  177. unsigned char fifo_count_h;
  178. unsigned char fifo_r_w;
  179. unsigned char raw_gyro;
  180. unsigned char raw_accel;
  181. unsigned char temp;
  182. unsigned char int_enable;
  183. unsigned char dmp_int_status;
  184. unsigned char int_status;
  185. unsigned char accel_intel;
  186. unsigned char pwr_mgmt_1;
  187. unsigned char pwr_mgmt_2;
  188. unsigned char int_pin_cfg;
  189. unsigned char mem_r_w;
  190. unsigned char accel_offs;
  191. unsigned char i2c_mst;
  192. unsigned char bank_sel;
  193. unsigned char mem_start_addr;
  194. unsigned char prgm_start_h;
  195. #if defined AK89xx_SECONDARY
  196. unsigned char s0_addr;
  197. unsigned char s0_reg;
  198. unsigned char s0_ctrl;
  199. unsigned char s1_addr;
  200. unsigned char s1_reg;
  201. unsigned char s1_ctrl;
  202. unsigned char s4_ctrl;
  203. unsigned char s0_do;
  204. unsigned char s1_do;
  205. unsigned char i2c_delay_ctrl;
  206. unsigned char raw_compass;
  207. /* The I2C_MST_VDDIO bit is in this register. */
  208. unsigned char yg_offs_tc;
  209. #endif
  210. };
  211. /* Information specific to a particular device. */
  212. struct hw_s {
  213. unsigned char addr;
  214. unsigned short max_fifo;
  215. unsigned char num_reg;
  216. unsigned short temp_sens;
  217. short temp_offset;
  218. unsigned short bank_size;
  219. #if defined AK89xx_SECONDARY
  220. unsigned short compass_fsr;
  221. #endif
  222. };
  223. /* When entering motion interrupt mode, the driver keeps track of the
  224. * previous state so that it can be restored at a later time.
  225. * TODO: This is tacky. Fix it.
  226. */
  227. struct motion_int_cache_s {
  228. unsigned short gyro_fsr;
  229. unsigned char accel_fsr;
  230. unsigned short lpf;
  231. unsigned short sample_rate;
  232. unsigned char sensors_on;
  233. unsigned char fifo_sensors;
  234. unsigned char dmp_on;
  235. };
  236. /* Cached chip configuration data.
  237. * TODO: A lot of these can be handled with a bitmask.
  238. */
  239. struct chip_cfg_s {
  240. /* Matches gyro_cfg >> 3 & 0x03 */
  241. unsigned char gyro_fsr;
  242. /* Matches accel_cfg >> 3 & 0x03 */
  243. unsigned char accel_fsr;
  244. /* Enabled sensors. Uses same masks as fifo_en, NOT pwr_mgmt_2. */
  245. unsigned char sensors;
  246. /* Matches config register. */
  247. unsigned char lpf;
  248. unsigned char clk_src;
  249. /* Sample rate, NOT rate divider. */
  250. unsigned short sample_rate;
  251. /* Matches fifo_en register. */
  252. unsigned char fifo_enable;
  253. /* Matches int enable register. */
  254. unsigned char int_enable;
  255. /* 1 if devices on auxiliary I2C bus appear on the primary. */
  256. unsigned char bypass_mode;
  257. /* 1 if half-sensitivity.
  258. * NOTE: This doesn't belong here, but everything else in hw_s is const,
  259. * and this allows us to save some precious RAM.
  260. */
  261. unsigned char accel_half;
  262. /* 1 if device in low-power accel-only mode. */
  263. unsigned char lp_accel_mode;
  264. /* 1 if interrupts are only triggered on motion events. */
  265. unsigned char int_motion_only;
  266. struct motion_int_cache_s cache;
  267. /* 1 for active low interrupts. */
  268. unsigned char active_low_int;
  269. /* 1 for latched interrupts. */
  270. unsigned char latched_int;
  271. /* 1 if DMP is enabled. */
  272. unsigned char dmp_on;
  273. /* Ensures that DMP will only be loaded once. */
  274. unsigned char dmp_loaded;
  275. /* Sampling rate used when DMP is enabled. */
  276. unsigned short dmp_sample_rate;
  277. #ifdef AK89xx_SECONDARY
  278. /* Compass sample rate. */
  279. unsigned short compass_sample_rate;
  280. unsigned char compass_addr;
  281. short mag_sens_adj[3];
  282. #endif
  283. };
  284. /* Information for self-test. */
  285. struct test_s {
  286. unsigned long gyro_sens;
  287. unsigned long accel_sens;
  288. unsigned char reg_rate_div;
  289. unsigned char reg_lpf;
  290. unsigned char reg_gyro_fsr;
  291. unsigned char reg_accel_fsr;
  292. unsigned short wait_ms;
  293. unsigned char packet_thresh;
  294. float min_dps;
  295. float max_dps;
  296. float max_gyro_var;
  297. float min_g;
  298. float max_g;
  299. float max_accel_var;
  300. #ifdef MPU6500
  301. float max_g_offset;
  302. unsigned short sample_wait_ms;
  303. #endif
  304. };
  305. /* Gyro driver state variables. */
  306. struct gyro_state_s {
  307. const struct gyro_reg_s *reg;
  308. const struct hw_s *hw;
  309. struct chip_cfg_s chip_cfg;
  310. const struct test_s *test;
  311. };
  312. /* Filter configurations. */
  313. enum lpf_e {
  314. INV_FILTER_256HZ_NOLPF2 = 0,
  315. INV_FILTER_188HZ,
  316. INV_FILTER_98HZ,
  317. INV_FILTER_42HZ,
  318. INV_FILTER_20HZ,
  319. INV_FILTER_10HZ,
  320. INV_FILTER_5HZ,
  321. INV_FILTER_2100HZ_NOLPF,
  322. NUM_FILTER
  323. };
  324. /* Full scale ranges. */
  325. enum gyro_fsr_e {
  326. INV_FSR_250DPS = 0,
  327. INV_FSR_500DPS,
  328. INV_FSR_1000DPS,
  329. INV_FSR_2000DPS,
  330. NUM_GYRO_FSR
  331. };
  332. /* Full scale ranges. */
  333. enum accel_fsr_e {
  334. INV_FSR_2G = 0,
  335. INV_FSR_4G,
  336. INV_FSR_8G,
  337. INV_FSR_16G,
  338. NUM_ACCEL_FSR
  339. };
  340. /* Clock sources. */
  341. enum clock_sel_e {
  342. INV_CLK_INTERNAL = 0,
  343. INV_CLK_PLL,
  344. NUM_CLK
  345. };
  346. /* Low-power accel wakeup rates. */
  347. enum lp_accel_rate_e {
  348. #if defined MPU6050
  349. INV_LPA_1_25HZ,
  350. INV_LPA_5HZ,
  351. INV_LPA_20HZ,
  352. INV_LPA_40HZ
  353. #elif defined MPU6500
  354. INV_LPA_0_3125HZ,
  355. INV_LPA_0_625HZ,
  356. INV_LPA_1_25HZ,
  357. INV_LPA_2_5HZ,
  358. INV_LPA_5HZ,
  359. INV_LPA_10HZ,
  360. INV_LPA_20HZ,
  361. INV_LPA_40HZ,
  362. INV_LPA_80HZ,
  363. INV_LPA_160HZ,
  364. INV_LPA_320HZ,
  365. INV_LPA_640HZ
  366. #endif
  367. };
  368. #define BIT_I2C_MST_VDDIO (0x80)
  369. #define BIT_FIFO_EN (0x40)
  370. #define BIT_DMP_EN (0x80)
  371. #define BIT_FIFO_RST (0x04)
  372. #define BIT_DMP_RST (0x08)
  373. #define BIT_FIFO_OVERFLOW (0x10)
  374. #define BIT_DATA_RDY_EN (0x01)
  375. #define BIT_DMP_INT_EN (0x02)
  376. #define BIT_MOT_INT_EN (0x40)
  377. #define BITS_FSR (0x18)
  378. #define BITS_LPF (0x07)
  379. #define BITS_HPF (0x07)
  380. #define BITS_CLK (0x07)
  381. #define BIT_FIFO_SIZE_1024 (0x40)
  382. #define BIT_FIFO_SIZE_2048 (0x80)
  383. #define BIT_FIFO_SIZE_4096 (0xC0)
  384. #define BIT_RESET (0x80)
  385. #define BIT_SLEEP (0x40)
  386. #define BIT_S0_DELAY_EN (0x01)
  387. #define BIT_S2_DELAY_EN (0x04)
  388. #define BITS_SLAVE_LENGTH (0x0F)
  389. #define BIT_SLAVE_BYTE_SW (0x40)
  390. #define BIT_SLAVE_GROUP (0x10)
  391. #define BIT_SLAVE_EN (0x80)
  392. #define BIT_I2C_READ (0x80)
  393. #define BITS_I2C_MASTER_DLY (0x1F)
  394. #define BIT_AUX_IF_EN (0x20)
  395. #define BIT_ACTL (0x80)
  396. #define BIT_LATCH_EN (0x20)
  397. #define BIT_ANY_RD_CLR (0x10)
  398. #define BIT_BYPASS_EN (0x02)
  399. #define BITS_WOM_EN (0xC0)
  400. #define BIT_LPA_CYCLE (0x20)
  401. #define BIT_STBY_XA (0x20)
  402. #define BIT_STBY_YA (0x10)
  403. #define BIT_STBY_ZA (0x08)
  404. #define BIT_STBY_XG (0x04)
  405. #define BIT_STBY_YG (0x02)
  406. #define BIT_STBY_ZG (0x01)
  407. #define BIT_STBY_XYZA (BIT_STBY_XA | BIT_STBY_YA | BIT_STBY_ZA)
  408. #define BIT_STBY_XYZG (BIT_STBY_XG | BIT_STBY_YG | BIT_STBY_ZG)
  409. #if defined AK8975_SECONDARY
  410. #define SUPPORTS_AK89xx_HIGH_SENS (0x00)
  411. #define AK89xx_FSR (9830)
  412. #elif defined AK8963_SECONDARY
  413. #define SUPPORTS_AK89xx_HIGH_SENS (0x10)
  414. #define AK89xx_FSR (4915)
  415. #endif
  416. #ifdef AK89xx_SECONDARY
  417. #define AKM_REG_WHOAMI (0x00)
  418. #define AKM_REG_ST1 (0x02)
  419. #define AKM_REG_HXL (0x03)
  420. #define AKM_REG_ST2 (0x09)
  421. #define AKM_REG_CNTL (0x0A)
  422. #define AKM_REG_ASTC (0x0C)
  423. #define AKM_REG_ASAX (0x10)
  424. #define AKM_REG_ASAY (0x11)
  425. #define AKM_REG_ASAZ (0x12)
  426. #define AKM_DATA_READY (0x01)
  427. #define AKM_DATA_OVERRUN (0x02)
  428. #define AKM_OVERFLOW (0x80)
  429. #define AKM_DATA_ERROR (0x40)
  430. #define AKM_BIT_SELF_TEST (0x40)
  431. #define AKM_POWER_DOWN (0x00 | SUPPORTS_AK89xx_HIGH_SENS)
  432. #define AKM_SINGLE_MEASUREMENT (0x01 | SUPPORTS_AK89xx_HIGH_SENS)
  433. #define AKM_FUSE_ROM_ACCESS (0x0F | SUPPORTS_AK89xx_HIGH_SENS)
  434. #define AKM_MODE_SELF_TEST (0x08 | SUPPORTS_AK89xx_HIGH_SENS)
  435. #define AKM_WHOAMI (0x48)
  436. #endif
  437. #if defined MPU6050
  438. const struct gyro_reg_s reg = {
  439. .who_am_i = 0x75,
  440. .rate_div = 0x19,
  441. .lpf = 0x1A,
  442. .prod_id = 0x0C,
  443. .user_ctrl = 0x6A,
  444. .fifo_en = 0x23,
  445. .gyro_cfg = 0x1B,
  446. .accel_cfg = 0x1C,
  447. .motion_thr = 0x1F,
  448. .motion_dur = 0x20,
  449. .fifo_count_h = 0x72,
  450. .fifo_r_w = 0x74,
  451. .raw_gyro = 0x43,
  452. .raw_accel = 0x3B,
  453. .temp = 0x41,
  454. .int_enable = 0x38,
  455. .dmp_int_status = 0x39,
  456. .int_status = 0x3A,
  457. .pwr_mgmt_1 = 0x6B,
  458. .pwr_mgmt_2 = 0x6C,
  459. .int_pin_cfg = 0x37,
  460. .mem_r_w = 0x6F,
  461. .accel_offs = 0x06,
  462. .i2c_mst = 0x24,
  463. .bank_sel = 0x6D,
  464. .mem_start_addr = 0x6E,
  465. .prgm_start_h = 0x70
  466. #ifdef AK89xx_SECONDARY
  467. ,.raw_compass = 0x49,
  468. .yg_offs_tc = 0x01,
  469. .s0_addr = 0x25,
  470. .s0_reg = 0x26,
  471. .s0_ctrl = 0x27,
  472. .s1_addr = 0x28,
  473. .s1_reg = 0x29,
  474. .s1_ctrl = 0x2A,
  475. .s4_ctrl = 0x34,
  476. .s0_do = 0x63,
  477. .s1_do = 0x64,
  478. .i2c_delay_ctrl = 0x67
  479. #endif
  480. };
  481. const struct hw_s hw = {
  482. .addr = 0x68,
  483. .max_fifo = 2048,
  484. .num_reg = 118,
  485. .temp_sens = 340,
  486. .temp_offset = -521,
  487. .bank_size = 256
  488. #if defined AK89xx_SECONDARY
  489. ,.compass_fsr = AK89xx_FSR
  490. #endif
  491. };
  492. const struct test_s test = {
  493. .gyro_sens = 32768/250,
  494. .accel_sens = 32768/16,
  495. .reg_rate_div = 0, /* 1kHz. */
  496. .reg_lpf = 1, /* 188Hz. */
  497. .reg_gyro_fsr = 0, /* 250dps. */
  498. .reg_accel_fsr = 0x18, /* 16g. */
  499. .wait_ms = 50,
  500. .packet_thresh = 5, /* 5% */
  501. .min_dps = 10.f,
  502. .max_dps = 105.f,
  503. .max_gyro_var = 0.14f,
  504. .min_g = 0.3f,
  505. .max_g = 0.95f,
  506. .max_accel_var = 0.14f
  507. };
  508. static struct gyro_state_s st = {
  509. .reg = &reg,
  510. .hw = &hw,
  511. .test = &test
  512. };
  513. #elif defined MPU6500
  514. const struct gyro_reg_s reg = {
  515. .who_am_i = 0x75,
  516. .rate_div = 0x19,
  517. .lpf = 0x1A,
  518. .prod_id = 0x0C,
  519. .user_ctrl = 0x6A,
  520. .fifo_en = 0x23,
  521. .gyro_cfg = 0x1B,
  522. .accel_cfg = 0x1C,
  523. .accel_cfg2 = 0x1D,
  524. .lp_accel_odr = 0x1E,
  525. .motion_thr = 0x1F,
  526. .motion_dur = 0x20,
  527. .fifo_count_h = 0x72,
  528. .fifo_r_w = 0x74,
  529. .raw_gyro = 0x43,
  530. .raw_accel = 0x3B,
  531. .temp = 0x41,
  532. .int_enable = 0x38,
  533. .dmp_int_status = 0x39,
  534. .int_status = 0x3A,
  535. .accel_intel = 0x69,
  536. .pwr_mgmt_1 = 0x6B,
  537. .pwr_mgmt_2 = 0x6C,
  538. .int_pin_cfg = 0x37,
  539. .mem_r_w = 0x6F,
  540. .accel_offs = 0x77,
  541. .i2c_mst = 0x24,
  542. .bank_sel = 0x6D,
  543. .mem_start_addr = 0x6E,
  544. .prgm_start_h = 0x70
  545. #ifdef AK89xx_SECONDARY
  546. ,.raw_compass = 0x49,
  547. .s0_addr = 0x25,
  548. .s0_reg = 0x26,
  549. .s0_ctrl = 0x27,
  550. .s1_addr = 0x28,
  551. .s1_reg = 0x29,
  552. .s1_ctrl = 0x2A,
  553. .s4_ctrl = 0x34,
  554. .s0_do = 0x63,
  555. .s1_do = 0x64,
  556. .i2c_delay_ctrl = 0x67
  557. #endif
  558. };
  559. const struct hw_s hw = {
  560. .addr = 0x68,
  561. .max_fifo = 1024,
  562. .num_reg = 128,
  563. .temp_sens = 321,
  564. .temp_offset = 0,
  565. .bank_size = 256
  566. #if defined AK89xx_SECONDARY
  567. ,.compass_fsr = AK89xx_FSR
  568. #endif
  569. };
  570. const struct test_s test = {
  571. .gyro_sens = 32768/250,
  572. .accel_sens = 32768/2, //FSR = +-2G = 16384 LSB/G
  573. .reg_rate_div = 0, /* 1kHz. */
  574. .reg_lpf = 2, /* 92Hz low pass filter*/
  575. .reg_gyro_fsr = 0, /* 250dps. */
  576. .reg_accel_fsr = 0x0, /* Accel FSR setting = 2g. */
  577. .wait_ms = 200, //200ms stabilization time
  578. .packet_thresh = 200, /* 200 samples */
  579. .min_dps = 20.f, //20 dps for Gyro Criteria C
  580. .max_dps = 60.f, //Must exceed 60 dps threshold for Gyro Criteria B
  581. .max_gyro_var = .5f, //Must exceed +50% variation for Gyro Criteria A
  582. .min_g = .225f, //Accel must exceed Min 225 mg for Criteria B
  583. .max_g = .675f, //Accel cannot exceed Max 675 mg for Criteria B
  584. .max_accel_var = .5f, //Accel must be within 50% variation for Criteria A
  585. .max_g_offset = .5f, //500 mg for Accel Criteria C
  586. .sample_wait_ms = 10 //10ms sample time wait
  587. };
  588. static struct gyro_state_s st = {
  589. .reg = &reg,
  590. .hw = &hw,
  591. .test = &test
  592. };
  593. #endif
  594. #define MAX_PACKET_LENGTH (12)
  595. #ifdef MPU6500
  596. #define HWST_MAX_PACKET_LENGTH (512)
  597. #endif
  598. #ifdef AK89xx_SECONDARY
  599. static int setup_compass(void);
  600. #define MAX_COMPASS_SAMPLE_RATE (100)
  601. #endif
  602. /**
  603. * @brief Enable/disable data ready interrupt.
  604. * If the DMP is on, the DMP interrupt is enabled. Otherwise, the data ready
  605. * interrupt is used.
  606. * @param[in] enable 1 to enable interrupt.
  607. * @return 0 if successful.
  608. */
  609. static int set_int_enable(unsigned char enable)
  610. {
  611. unsigned char tmp;
  612. if (st.chip_cfg.dmp_on) {
  613. if (enable)
  614. tmp = BIT_DMP_INT_EN;
  615. else
  616. tmp = 0x00;
  617. if (i2c_write(st.hw->addr, st.reg->int_enable, 1, &tmp))
  618. return -1;
  619. st.chip_cfg.int_enable = tmp;
  620. } else {
  621. if (!st.chip_cfg.sensors)
  622. return -1;
  623. if (enable && st.chip_cfg.int_enable)
  624. return 0;
  625. if (enable)
  626. tmp = BIT_DATA_RDY_EN;
  627. else
  628. tmp = 0x00;
  629. if (i2c_write(st.hw->addr, st.reg->int_enable, 1, &tmp))
  630. return -1;
  631. st.chip_cfg.int_enable = tmp;
  632. }
  633. return 0;
  634. }
  635. /**
  636. * @brief Register dump for testing.
  637. * @return 0 if successful.
  638. */
  639. int mpu_reg_dump(void)
  640. {
  641. unsigned char ii;
  642. unsigned char data;
  643. for (ii = 0; ii < st.hw->num_reg; ii++) {
  644. if (ii == st.reg->fifo_r_w || ii == st.reg->mem_r_w)
  645. continue;
  646. if (i2c_read(st.hw->addr, ii, 1, &data))
  647. return -1;
  648. log_i("%#5x: %#5x\r\n", ii, data);
  649. }
  650. return 0;
  651. }
  652. /**
  653. * @brief Read from a single register.
  654. * NOTE: The memory and FIFO read/write registers cannot be accessed.
  655. * @param[in] reg Register address.
  656. * @param[out] data Register data.
  657. * @return 0 if successful.
  658. */
  659. int mpu_read_reg(unsigned char reg, unsigned char *data)
  660. {
  661. if (reg == st.reg->fifo_r_w || reg == st.reg->mem_r_w)
  662. return -1;
  663. if (reg >= st.hw->num_reg)
  664. return -1;
  665. return i2c_read(st.hw->addr, reg, 1, data);
  666. }
  667. /**
  668. * @brief Initialize hardware.
  669. * Initial configuration:\n
  670. * Gyro FSR: +/- 2000DPS\n
  671. * Accel FSR +/- 2G\n
  672. * DLPF: 42Hz\n
  673. * FIFO rate: 50Hz\n
  674. * Clock source: Gyro PLL\n
  675. * FIFO: Disabled.\n
  676. * Data ready interrupt: Disabled, active low, unlatched.
  677. * @param[in] int_param Platform-specific parameters to interrupt API.
  678. * @return 0 if successful.
  679. */
  680. int mpu_init(struct int_param_s *int_param)
  681. {
  682. unsigned char data[6];
  683. /* Reset device. */
  684. data[0] = BIT_RESET;
  685. if (i2c_write(st.hw->addr, st.reg->pwr_mgmt_1, 1, data))
  686. return -1;
  687. delay_ms(100);
  688. /* Wake up chip. */
  689. data[0] = 0x00;
  690. if (i2c_write(st.hw->addr, st.reg->pwr_mgmt_1, 1, data))
  691. return -1;
  692. st.chip_cfg.accel_half = 0;
  693. #ifdef MPU6500
  694. /* MPU6500 shares 4kB of memory between the DMP and the FIFO. Since the
  695. * first 3kB are needed by the DMP, we'll use the last 1kB for the FIFO.
  696. */
  697. data[0] = BIT_FIFO_SIZE_1024 | 0x8;
  698. if (i2c_write(st.hw->addr, st.reg->accel_cfg2, 1, data))
  699. return -1;
  700. #endif
  701. /* Set to invalid values to ensure no I2C writes are skipped. */
  702. st.chip_cfg.sensors = 0xFF;
  703. st.chip_cfg.gyro_fsr = 0xFF;
  704. st.chip_cfg.accel_fsr = 0xFF;
  705. st.chip_cfg.lpf = 0xFF;
  706. st.chip_cfg.sample_rate = 0xFFFF;
  707. st.chip_cfg.fifo_enable = 0xFF;
  708. st.chip_cfg.bypass_mode = 0xFF;
  709. #ifdef AK89xx_SECONDARY
  710. st.chip_cfg.compass_sample_rate = 0xFFFF;
  711. #endif
  712. /* mpu_set_sensors always preserves this setting. */
  713. st.chip_cfg.clk_src = INV_CLK_PLL;
  714. /* Handled in next call to mpu_set_bypass. */
  715. st.chip_cfg.active_low_int = 1;
  716. st.chip_cfg.latched_int = 0;
  717. st.chip_cfg.int_motion_only = 0;
  718. st.chip_cfg.lp_accel_mode = 0;
  719. memset(&st.chip_cfg.cache, 0, sizeof(st.chip_cfg.cache));
  720. st.chip_cfg.dmp_on = 0;
  721. st.chip_cfg.dmp_loaded = 0;
  722. st.chip_cfg.dmp_sample_rate = 0;
  723. if (mpu_set_gyro_fsr(2000))
  724. return -1;
  725. if (mpu_set_accel_fsr(2))
  726. return -1;
  727. if (mpu_set_lpf(42))
  728. return -1;
  729. if (mpu_set_sample_rate(50))
  730. return -1;
  731. if (mpu_configure_fifo(0))
  732. return -1;
  733. #ifndef EMPL_TARGET_STM32F4
  734. // if (int_param)
  735. // reg_int_cb(int_param);
  736. #endif
  737. #ifdef AK89xx_SECONDARY
  738. setup_compass();
  739. if (mpu_set_compass_sample_rate(10))
  740. return -1;
  741. #else
  742. /* Already disabled by setup_compass. */
  743. if (mpu_set_bypass(0))
  744. return -1;
  745. #endif
  746. mpu_set_sensors(0);
  747. return 0;
  748. }
  749. /**
  750. * @brief Enter low-power accel-only mode.
  751. * In low-power accel mode, the chip goes to sleep and only wakes up to sample
  752. * the accelerometer at one of the following frequencies:
  753. * \n MPU6050: 1.25Hz, 5Hz, 20Hz, 40Hz
  754. * \n MPU6500: 1.25Hz, 2.5Hz, 5Hz, 10Hz, 20Hz, 40Hz, 80Hz, 160Hz, 320Hz, 640Hz
  755. * \n If the requested rate is not one listed above, the device will be set to
  756. * the next highest rate. Requesting a rate above the maximum supported
  757. * frequency will result in an error.
  758. * \n To select a fractional wake-up frequency, round down the value passed to
  759. * @e rate.
  760. * @param[in] rate Minimum sampling rate, or zero to disable LP
  761. * accel mode.
  762. * @return 0 if successful.
  763. */
  764. int mpu_lp_accel_mode(unsigned short rate)
  765. {
  766. unsigned char tmp[2];
  767. if (rate > 40)
  768. return -1;
  769. if (!rate) {
  770. mpu_set_int_latched(0);
  771. tmp[0] = 0;
  772. tmp[1] = BIT_STBY_XYZG;
  773. if (i2c_write(st.hw->addr, st.reg->pwr_mgmt_1, 2, tmp))
  774. return -1;
  775. st.chip_cfg.lp_accel_mode = 0;
  776. return 0;
  777. }
  778. /* For LP accel, we automatically configure the hardware to produce latched
  779. * interrupts. In LP accel mode, the hardware cycles into sleep mode before
  780. * it gets a chance to deassert the interrupt pin; therefore, we shift this
  781. * responsibility over to the MCU.
  782. *
  783. * Any register read will clear the interrupt.
  784. */
  785. mpu_set_int_latched(1);
  786. #if defined MPU6050
  787. tmp[0] = BIT_LPA_CYCLE;
  788. if (rate == 1) {
  789. tmp[1] = INV_LPA_1_25HZ;
  790. mpu_set_lpf(5);
  791. } else if (rate <= 5) {
  792. tmp[1] = INV_LPA_5HZ;
  793. mpu_set_lpf(5);
  794. } else if (rate <= 20) {
  795. tmp[1] = INV_LPA_20HZ;
  796. mpu_set_lpf(10);
  797. } else {
  798. tmp[1] = INV_LPA_40HZ;
  799. mpu_set_lpf(20);
  800. }
  801. tmp[1] = (tmp[1] << 6) | BIT_STBY_XYZG;
  802. if (i2c_write(st.hw->addr, st.reg->pwr_mgmt_1, 2, tmp))
  803. return -1;
  804. #elif defined MPU6500
  805. /* Set wake frequency. */
  806. if (rate == 1)
  807. tmp[0] = INV_LPA_1_25HZ;
  808. else if (rate == 2)
  809. tmp[0] = INV_LPA_2_5HZ;
  810. else if (rate <= 5)
  811. tmp[0] = INV_LPA_5HZ;
  812. else if (rate <= 10)
  813. tmp[0] = INV_LPA_10HZ;
  814. else if (rate <= 20)
  815. tmp[0] = INV_LPA_20HZ;
  816. else if (rate <= 40)
  817. tmp[0] = INV_LPA_40HZ;
  818. else if (rate <= 80)
  819. tmp[0] = INV_LPA_80HZ;
  820. else if (rate <= 160)
  821. tmp[0] = INV_LPA_160HZ;
  822. else if (rate <= 320)
  823. tmp[0] = INV_LPA_320HZ;
  824. else
  825. tmp[0] = INV_LPA_640HZ;
  826. if (i2c_write(st.hw->addr, st.reg->lp_accel_odr, 1, tmp))
  827. return -1;
  828. tmp[0] = BIT_LPA_CYCLE;
  829. if (i2c_write(st.hw->addr, st.reg->pwr_mgmt_1, 1, tmp))
  830. return -1;
  831. #endif
  832. st.chip_cfg.sensors = INV_XYZ_ACCEL;
  833. st.chip_cfg.clk_src = 0;
  834. st.chip_cfg.lp_accel_mode = 1;
  835. mpu_configure_fifo(0);
  836. return 0;
  837. }
  838. /**
  839. * @brief Read raw gyro data directly from the registers.
  840. * @param[out] data Raw data in hardware units.
  841. * @param[out] timestamp Timestamp in milliseconds. Null if not needed.
  842. * @return 0 if successful.
  843. */
  844. int mpu_get_gyro_reg(short *data, unsigned long *timestamp)
  845. {
  846. unsigned char tmp[6];
  847. if (!(st.chip_cfg.sensors & INV_XYZ_GYRO))
  848. return -1;
  849. if (i2c_read(st.hw->addr, st.reg->raw_gyro, 6, tmp))
  850. return -1;
  851. data[0] = (tmp[0] << 8) | tmp[1];
  852. data[1] = (tmp[2] << 8) | tmp[3];
  853. data[2] = (tmp[4] << 8) | tmp[5];
  854. if (timestamp)
  855. get_ms(timestamp);
  856. return 0;
  857. }
  858. /**
  859. * @brief Read raw accel data directly from the registers.
  860. * @param[out] data Raw data in hardware units.
  861. * @param[out] timestamp Timestamp in milliseconds. Null if not needed.
  862. * @return 0 if successful.
  863. */
  864. int mpu_get_accel_reg(short *data, unsigned long *timestamp)
  865. {
  866. unsigned char tmp[6];
  867. if (!(st.chip_cfg.sensors & INV_XYZ_ACCEL))
  868. return -1;
  869. if (i2c_read(st.hw->addr, st.reg->raw_accel, 6, tmp))
  870. return -1;
  871. data[0] = (tmp[0] << 8) | tmp[1];
  872. data[1] = (tmp[2] << 8) | tmp[3];
  873. data[2] = (tmp[4] << 8) | tmp[5];
  874. if (timestamp)
  875. get_ms(timestamp);
  876. return 0;
  877. }
  878. /**
  879. * @brief Read temperature data directly from the registers.
  880. * @param[out] data Data in q16 format.
  881. * @param[out] timestamp Timestamp in milliseconds. Null if not needed.
  882. * @return 0 if successful.
  883. */
  884. int mpu_get_temperature(long *data, unsigned long *timestamp)
  885. {
  886. unsigned char tmp[2];
  887. short raw;
  888. if (!(st.chip_cfg.sensors))
  889. return -1;
  890. if (i2c_read(st.hw->addr, st.reg->temp, 2, tmp))
  891. return -1;
  892. raw = (tmp[0] << 8) | tmp[1];
  893. if (timestamp)
  894. get_ms(timestamp);
  895. data[0] = (long)((35 + ((raw - (float)st.hw->temp_offset) / st.hw->temp_sens)) * 65536L);
  896. return 0;
  897. }
  898. /**
  899. * @brief Read biases to the accel bias 6500 registers.
  900. * This function reads from the MPU6500 accel offset cancellations registers.
  901. * The format are G in +-8G format. The register is initialized with OTP
  902. * factory trim values.
  903. * @param[in] accel_bias returned structure with the accel bias
  904. * @return 0 if successful.
  905. */
  906. int mpu_read_6500_accel_bias(long *accel_bias) {
  907. unsigned char data[6];
  908. if (i2c_read(st.hw->addr, 0x77, 2, &data[0]))
  909. return -1;
  910. if (i2c_read(st.hw->addr, 0x7A, 2, &data[2]))
  911. return -1;
  912. if (i2c_read(st.hw->addr, 0x7D, 2, &data[4]))
  913. return -1;
  914. accel_bias[0] = ((long)data[0]<<8) | data[1];
  915. accel_bias[1] = ((long)data[2]<<8) | data[3];
  916. accel_bias[2] = ((long)data[4]<<8) | data[5];
  917. return 0;
  918. }
  919. /**
  920. * @brief Read biases to the accel bias 6050 registers.
  921. * This function reads from the MPU6050 accel offset cancellations registers.
  922. * The format are G in +-8G format. The register is initialized with OTP
  923. * factory trim values.
  924. * @param[in] accel_bias returned structure with the accel bias
  925. * @return 0 if successful.
  926. */
  927. int mpu_read_6050_accel_bias(long *accel_bias) {
  928. unsigned char data[6];
  929. if (i2c_read(st.hw->addr, 0x06, 2, &data[0]))
  930. return -1;
  931. if (i2c_read(st.hw->addr, 0x08, 2, &data[2]))
  932. return -1;
  933. if (i2c_read(st.hw->addr, 0x0A, 2, &data[4]))
  934. return -1;
  935. accel_bias[0] = ((long)data[0]<<8) | data[1];
  936. accel_bias[1] = ((long)data[2]<<8) | data[3];
  937. accel_bias[2] = ((long)data[4]<<8) | data[5];
  938. return 0;
  939. }
  940. int mpu_read_6500_gyro_bias(long *gyro_bias) {
  941. unsigned char data[6];
  942. if (i2c_read(st.hw->addr, 0x13, 2, &data[0]))
  943. return -1;
  944. if (i2c_read(st.hw->addr, 0x15, 2, &data[2]))
  945. return -1;
  946. if (i2c_read(st.hw->addr, 0x17, 2, &data[4]))
  947. return -1;
  948. gyro_bias[0] = ((long)data[0]<<8) | data[1];
  949. gyro_bias[1] = ((long)data[2]<<8) | data[3];
  950. gyro_bias[2] = ((long)data[4]<<8) | data[5];
  951. return 0;
  952. }
  953. /**
  954. * @brief Push biases to the gyro bias 6500/6050 registers.
  955. * This function expects biases relative to the current sensor output, and
  956. * these biases will be added to the factory-supplied values. Bias inputs are LSB
  957. * in +-1000dps format.
  958. * @param[in] gyro_bias New biases.
  959. * @return 0 if successful.
  960. */
  961. int mpu_set_gyro_bias_reg(long *gyro_bias)
  962. {
  963. unsigned char data[6] = {0, 0, 0, 0, 0, 0};
  964. int i=0;
  965. for(i=0;i<3;i++) {
  966. gyro_bias[i]= (-gyro_bias[i]);
  967. }
  968. data[0] = (gyro_bias[0] >> 8) & 0xff;
  969. data[1] = (gyro_bias[0]) & 0xff;
  970. data[2] = (gyro_bias[1] >> 8) & 0xff;
  971. data[3] = (gyro_bias[1]) & 0xff;
  972. data[4] = (gyro_bias[2] >> 8) & 0xff;
  973. data[5] = (gyro_bias[2]) & 0xff;
  974. if (i2c_write(st.hw->addr, 0x13, 2, &data[0]))
  975. return -1;
  976. if (i2c_write(st.hw->addr, 0x15, 2, &data[2]))
  977. return -1;
  978. if (i2c_write(st.hw->addr, 0x17, 2, &data[4]))
  979. return -1;
  980. return 0;
  981. }
  982. /**
  983. * @brief Push biases to the accel bias 6050 registers.
  984. * This function expects biases relative to the current sensor output, and
  985. * these biases will be added to the factory-supplied values. Bias inputs are LSB
  986. * in +-16G format.
  987. * @param[in] accel_bias New biases.
  988. * @return 0 if successful.
  989. */
  990. int mpu_set_accel_bias_6050_reg(const long *accel_bias) {
  991. unsigned char data[6] = {0, 0, 0, 0, 0, 0};
  992. long accel_reg_bias[3] = {0, 0, 0};
  993. if(mpu_read_6050_accel_bias(accel_reg_bias))
  994. return -1;
  995. accel_reg_bias[0] -= (accel_bias[0] & ~1);
  996. accel_reg_bias[1] -= (accel_bias[1] & ~1);
  997. accel_reg_bias[2] -= (accel_bias[2] & ~1);
  998. data[0] = (accel_reg_bias[0] >> 8) & 0xff;
  999. data[1] = (accel_reg_bias[0]) & 0xff;
  1000. data[2] = (accel_reg_bias[1] >> 8) & 0xff;
  1001. data[3] = (accel_reg_bias[1]) & 0xff;
  1002. data[4] = (accel_reg_bias[2] >> 8) & 0xff;
  1003. data[5] = (accel_reg_bias[2]) & 0xff;
  1004. if (i2c_write(st.hw->addr, 0x06, 2, &data[0]))
  1005. return -1;
  1006. if (i2c_write(st.hw->addr, 0x08, 2, &data[2]))
  1007. return -1;
  1008. if (i2c_write(st.hw->addr, 0x0A, 2, &data[4]))
  1009. return -1;
  1010. return 0;
  1011. }
  1012. /**
  1013. * @brief Push biases to the accel bias 6500 registers.
  1014. * This function expects biases relative to the current sensor output, and
  1015. * these biases will be added to the factory-supplied values. Bias inputs are LSB
  1016. * in +-16G format.
  1017. * @param[in] accel_bias New biases.
  1018. * @return 0 if successful.
  1019. */
  1020. int mpu_set_accel_bias_6500_reg(const long *accel_bias) {
  1021. unsigned char data[6] = {0, 0, 0, 0, 0, 0};
  1022. long accel_reg_bias[3] = {0, 0, 0};
  1023. if(mpu_read_6500_accel_bias(accel_reg_bias))
  1024. return -1;
  1025. // Preserve bit 0 of factory value (for temperature compensation)
  1026. accel_reg_bias[0] -= (accel_bias[0] & ~1);
  1027. accel_reg_bias[1] -= (accel_bias[1] & ~1);
  1028. accel_reg_bias[2] -= (accel_bias[2] & ~1);
  1029. data[0] = (accel_reg_bias[0] >> 8) & 0xff;
  1030. data[1] = (accel_reg_bias[0]) & 0xff;
  1031. data[2] = (accel_reg_bias[1] >> 8) & 0xff;
  1032. data[3] = (accel_reg_bias[1]) & 0xff;
  1033. data[4] = (accel_reg_bias[2] >> 8) & 0xff;
  1034. data[5] = (accel_reg_bias[2]) & 0xff;
  1035. if (i2c_write(st.hw->addr, 0x77, 2, &data[0]))
  1036. return -1;
  1037. if (i2c_write(st.hw->addr, 0x7A, 2, &data[2]))
  1038. return -1;
  1039. if (i2c_write(st.hw->addr, 0x7D, 2, &data[4]))
  1040. return -1;
  1041. return 0;
  1042. }
  1043. /**
  1044. * @brief Reset FIFO read/write pointers.
  1045. * @return 0 if successful.
  1046. */
  1047. int mpu_reset_fifo(void)
  1048. {
  1049. unsigned char data;
  1050. if (!(st.chip_cfg.sensors))
  1051. return -1;
  1052. data = 0;
  1053. if (i2c_write(st.hw->addr, st.reg->int_enable, 1, &data))
  1054. return -1;
  1055. if (i2c_write(st.hw->addr, st.reg->fifo_en, 1, &data))
  1056. return -1;
  1057. if (i2c_write(st.hw->addr, st.reg->user_ctrl, 1, &data))
  1058. return -1;
  1059. if (st.chip_cfg.dmp_on) {
  1060. data = BIT_FIFO_RST | BIT_DMP_RST;
  1061. if (i2c_write(st.hw->addr, st.reg->user_ctrl, 1, &data))
  1062. return -1;
  1063. delay_ms(50);
  1064. data = BIT_DMP_EN | BIT_FIFO_EN;
  1065. if (st.chip_cfg.sensors & INV_XYZ_COMPASS)
  1066. data |= BIT_AUX_IF_EN;
  1067. if (i2c_write(st.hw->addr, st.reg->user_ctrl, 1, &data))
  1068. return -1;
  1069. if (st.chip_cfg.int_enable)
  1070. data = BIT_DMP_INT_EN;
  1071. else
  1072. data = 0;
  1073. if (i2c_write(st.hw->addr, st.reg->int_enable, 1, &data))
  1074. return -1;
  1075. data = 0;
  1076. if (i2c_write(st.hw->addr, st.reg->fifo_en, 1, &data))
  1077. return -1;
  1078. } else {
  1079. data = BIT_FIFO_RST;
  1080. if (i2c_write(st.hw->addr, st.reg->user_ctrl, 1, &data))
  1081. return -1;
  1082. if (st.chip_cfg.bypass_mode || !(st.chip_cfg.sensors & INV_XYZ_COMPASS))
  1083. data = BIT_FIFO_EN;
  1084. else
  1085. data = BIT_FIFO_EN | BIT_AUX_IF_EN;
  1086. if (i2c_write(st.hw->addr, st.reg->user_ctrl, 1, &data))
  1087. return -1;
  1088. delay_ms(50);
  1089. if (st.chip_cfg.int_enable)
  1090. data = BIT_DATA_RDY_EN;
  1091. else
  1092. data = 0;
  1093. if (i2c_write(st.hw->addr, st.reg->int_enable, 1, &data))
  1094. return -1;
  1095. if (i2c_write(st.hw->addr, st.reg->fifo_en, 1, &st.chip_cfg.fifo_enable))
  1096. return -1;
  1097. }
  1098. return 0;
  1099. }
  1100. /**
  1101. * @brief Get the gyro full-scale range.
  1102. * @param[out] fsr Current full-scale range.
  1103. * @return 0 if successful.
  1104. */
  1105. int mpu_get_gyro_fsr(unsigned short *fsr)
  1106. {
  1107. switch (st.chip_cfg.gyro_fsr) {
  1108. case INV_FSR_250DPS:
  1109. fsr[0] = 250;
  1110. break;
  1111. case INV_FSR_500DPS:
  1112. fsr[0] = 500;
  1113. break;
  1114. case INV_FSR_1000DPS:
  1115. fsr[0] = 1000;
  1116. break;
  1117. case INV_FSR_2000DPS:
  1118. fsr[0] = 2000;
  1119. break;
  1120. default:
  1121. fsr[0] = 0;
  1122. break;
  1123. }
  1124. return 0;
  1125. }
  1126. /**
  1127. * @brief Set the gyro full-scale range.
  1128. * @param[in] fsr Desired full-scale range.
  1129. * @return 0 if successful.
  1130. */
  1131. int mpu_set_gyro_fsr(unsigned short fsr)
  1132. {
  1133. unsigned char data;
  1134. if (!(st.chip_cfg.sensors))
  1135. return -1;
  1136. switch (fsr) {
  1137. case 250:
  1138. data = INV_FSR_250DPS << 3;
  1139. break;
  1140. case 500:
  1141. data = INV_FSR_500DPS << 3;
  1142. break;
  1143. case 1000:
  1144. data = INV_FSR_1000DPS << 3;
  1145. break;
  1146. case 2000:
  1147. data = INV_FSR_2000DPS << 3;
  1148. break;
  1149. default:
  1150. return -1;
  1151. }
  1152. if (st.chip_cfg.gyro_fsr == (data >> 3))
  1153. return 0;
  1154. if (i2c_write(st.hw->addr, st.reg->gyro_cfg, 1, &data))
  1155. return -1;
  1156. st.chip_cfg.gyro_fsr = data >> 3;
  1157. return 0;
  1158. }
  1159. /**
  1160. * @brief Get the accel full-scale range.
  1161. * @param[out] fsr Current full-scale range.
  1162. * @return 0 if successful.
  1163. */
  1164. int mpu_get_accel_fsr(unsigned char *fsr)
  1165. {
  1166. switch (st.chip_cfg.accel_fsr) {
  1167. case INV_FSR_2G:
  1168. fsr[0] = 2;
  1169. break;
  1170. case INV_FSR_4G:
  1171. fsr[0] = 4;
  1172. break;
  1173. case INV_FSR_8G:
  1174. fsr[0] = 8;
  1175. break;
  1176. case INV_FSR_16G:
  1177. fsr[0] = 16;
  1178. break;
  1179. default:
  1180. return -1;
  1181. }
  1182. if (st.chip_cfg.accel_half)
  1183. fsr[0] <<= 1;
  1184. return 0;
  1185. }
  1186. /**
  1187. * @brief Set the accel full-scale range.
  1188. * @param[in] fsr Desired full-scale range.
  1189. * @return 0 if successful.
  1190. */
  1191. int mpu_set_accel_fsr(unsigned char fsr)
  1192. {
  1193. unsigned char data;
  1194. if (!(st.chip_cfg.sensors))
  1195. return -1;
  1196. switch (fsr) {
  1197. case 2:
  1198. data = INV_FSR_2G << 3;
  1199. break;
  1200. case 4:
  1201. data = INV_FSR_4G << 3;
  1202. break;
  1203. case 8:
  1204. data = INV_FSR_8G << 3;
  1205. break;
  1206. case 16:
  1207. data = INV_FSR_16G << 3;
  1208. break;
  1209. default:
  1210. return -1;
  1211. }
  1212. if (st.chip_cfg.accel_fsr == (data >> 3))
  1213. return 0;
  1214. if (i2c_write(st.hw->addr, st.reg->accel_cfg, 1, &data))
  1215. return -1;
  1216. st.chip_cfg.accel_fsr = data >> 3;
  1217. return 0;
  1218. }
  1219. /**
  1220. * @brief Get the current DLPF setting.
  1221. * @param[out] lpf Current LPF setting.
  1222. * 0 if successful.
  1223. */
  1224. int mpu_get_lpf(unsigned short *lpf)
  1225. {
  1226. switch (st.chip_cfg.lpf) {
  1227. case INV_FILTER_188HZ:
  1228. lpf[0] = 188;
  1229. break;
  1230. case INV_FILTER_98HZ:
  1231. lpf[0] = 98;
  1232. break;
  1233. case INV_FILTER_42HZ:
  1234. lpf[0] = 42;
  1235. break;
  1236. case INV_FILTER_20HZ:
  1237. lpf[0] = 20;
  1238. break;
  1239. case INV_FILTER_10HZ:
  1240. lpf[0] = 10;
  1241. break;
  1242. case INV_FILTER_5HZ:
  1243. lpf[0] = 5;
  1244. break;
  1245. case INV_FILTER_256HZ_NOLPF2:
  1246. case INV_FILTER_2100HZ_NOLPF:
  1247. default:
  1248. lpf[0] = 0;
  1249. break;
  1250. }
  1251. return 0;
  1252. }
  1253. /**
  1254. * @brief Set digital low pass filter.
  1255. * The following LPF settings are supported: 188, 98, 42, 20, 10, 5.
  1256. * @param[in] lpf Desired LPF setting.
  1257. * @return 0 if successful.
  1258. */
  1259. int mpu_set_lpf(unsigned short lpf)
  1260. {
  1261. unsigned char data;
  1262. if (!(st.chip_cfg.sensors))
  1263. return -1;
  1264. if (lpf >= 188)
  1265. data = INV_FILTER_188HZ;
  1266. else if (lpf >= 98)
  1267. data = INV_FILTER_98HZ;
  1268. else if (lpf >= 42)
  1269. data = INV_FILTER_42HZ;
  1270. else if (lpf >= 20)
  1271. data = INV_FILTER_20HZ;
  1272. else if (lpf >= 10)
  1273. data = INV_FILTER_10HZ;
  1274. else
  1275. data = INV_FILTER_5HZ;
  1276. if (st.chip_cfg.lpf == data)
  1277. return 0;
  1278. if (i2c_write(st.hw->addr, st.reg->lpf, 1, &data))
  1279. return -1;
  1280. st.chip_cfg.lpf = data;
  1281. return 0;
  1282. }
  1283. /**
  1284. * @brief Get sampling rate.
  1285. * @param[out] rate Current sampling rate (Hz).
  1286. * @return 0 if successful.
  1287. */
  1288. int mpu_get_sample_rate(unsigned short *rate)
  1289. {
  1290. if (st.chip_cfg.dmp_on)
  1291. return -1;
  1292. else
  1293. rate[0] = st.chip_cfg.sample_rate;
  1294. return 0;
  1295. }
  1296. /**
  1297. * @brief Set sampling rate.
  1298. * Sampling rate must be between 4Hz and 1kHz.
  1299. * @param[in] rate Desired sampling rate (Hz).
  1300. * @return 0 if successful.
  1301. */
  1302. int mpu_set_sample_rate(unsigned short rate)
  1303. {
  1304. unsigned char data;
  1305. if (!(st.chip_cfg.sensors))
  1306. return -1;
  1307. if (st.chip_cfg.dmp_on)
  1308. return -1;
  1309. else {
  1310. if (st.chip_cfg.lp_accel_mode) {
  1311. if (rate && (rate <= 40)) {
  1312. /* Just stay in low-power accel mode. */
  1313. mpu_lp_accel_mode(rate);
  1314. return 0;
  1315. }
  1316. /* Requested rate exceeds the allowed frequencies in LP accel mode,
  1317. * switch back to full-power mode.
  1318. */
  1319. mpu_lp_accel_mode(0);
  1320. }
  1321. if (rate < 4)
  1322. rate = 4;
  1323. else if (rate > 1000)
  1324. rate = 1000;
  1325. data = 1000 / rate - 1;
  1326. if (i2c_write(st.hw->addr, st.reg->rate_div, 1, &data))
  1327. return -1;
  1328. st.chip_cfg.sample_rate = 1000 / (1 + data);
  1329. #ifdef AK89xx_SECONDARY
  1330. mpu_set_compass_sample_rate(min(st.chip_cfg.compass_sample_rate, MAX_COMPASS_SAMPLE_RATE));
  1331. #endif
  1332. /* Automatically set LPF to 1/2 sampling rate. */
  1333. mpu_set_lpf(st.chip_cfg.sample_rate >> 1);
  1334. return 0;
  1335. }
  1336. }
  1337. /**
  1338. * @brief Get compass sampling rate.
  1339. * @param[out] rate Current compass sampling rate (Hz).
  1340. * @return 0 if successful.
  1341. */
  1342. int mpu_get_compass_sample_rate(unsigned short *rate)
  1343. {
  1344. #ifdef AK89xx_SECONDARY
  1345. rate[0] = st.chip_cfg.compass_sample_rate;
  1346. return 0;
  1347. #else
  1348. rate[0] = 0;
  1349. return -1;
  1350. #endif
  1351. }
  1352. /**
  1353. * @brief Set compass sampling rate.
  1354. * The compass on the auxiliary I2C bus is read by the MPU hardware at a
  1355. * maximum of 100Hz. The actual rate can be set to a fraction of the gyro
  1356. * sampling rate.
  1357. *
  1358. * \n WARNING: The new rate may be different than what was requested. Call
  1359. * mpu_get_compass_sample_rate to check the actual setting.
  1360. * @param[in] rate Desired compass sampling rate (Hz).
  1361. * @return 0 if successful.
  1362. */
  1363. int mpu_set_compass_sample_rate(unsigned short rate)
  1364. {
  1365. #ifdef AK89xx_SECONDARY
  1366. unsigned char div;
  1367. if (!rate || rate > st.chip_cfg.sample_rate || rate > MAX_COMPASS_SAMPLE_RATE)
  1368. return -1;
  1369. div = st.chip_cfg.sample_rate / rate - 1;
  1370. if (i2c_write(st.hw->addr, st.reg->s4_ctrl, 1, &div))
  1371. return -1;
  1372. st.chip_cfg.compass_sample_rate = st.chip_cfg.sample_rate / (div + 1);
  1373. return 0;
  1374. #else
  1375. return -1;
  1376. #endif
  1377. }
  1378. /**
  1379. * @brief Get gyro sensitivity scale factor.
  1380. * @param[out] sens Conversion from hardware units to dps.
  1381. * @return 0 if successful.
  1382. */
  1383. int mpu_get_gyro_sens(float *sens)
  1384. {
  1385. switch (st.chip_cfg.gyro_fsr) {
  1386. case INV_FSR_250DPS:
  1387. sens[0] = 131.f;
  1388. break;
  1389. case INV_FSR_500DPS:
  1390. sens[0] = 65.5f;
  1391. break;
  1392. case INV_FSR_1000DPS:
  1393. sens[0] = 32.8f;
  1394. break;
  1395. case INV_FSR_2000DPS:
  1396. sens[0] = 16.4f;
  1397. break;
  1398. default:
  1399. return -1;
  1400. }
  1401. return 0;
  1402. }
  1403. /**
  1404. * @brief Get accel sensitivity scale factor.
  1405. * @param[out] sens Conversion from hardware units to g's.
  1406. * @return 0 if successful.
  1407. */
  1408. int mpu_get_accel_sens(unsigned short *sens)
  1409. {
  1410. switch (st.chip_cfg.accel_fsr) {
  1411. case INV_FSR_2G:
  1412. sens[0] = 16384;
  1413. break;
  1414. case INV_FSR_4G:
  1415. sens[0] = 8192;
  1416. break;
  1417. case INV_FSR_8G:
  1418. sens[0] = 4096;
  1419. break;
  1420. case INV_FSR_16G:
  1421. sens[0] = 2048;
  1422. break;
  1423. default:
  1424. return -1;
  1425. }
  1426. if (st.chip_cfg.accel_half)
  1427. sens[0] >>= 1;
  1428. return 0;
  1429. }
  1430. /**
  1431. * @brief Get current FIFO configuration.
  1432. * @e sensors can contain a combination of the following flags:
  1433. * \n INV_X_GYRO, INV_Y_GYRO, INV_Z_GYRO
  1434. * \n INV_XYZ_GYRO
  1435. * \n INV_XYZ_ACCEL
  1436. * @param[out] sensors Mask of sensors in FIFO.
  1437. * @return 0 if successful.
  1438. */
  1439. int mpu_get_fifo_config(unsigned char *sensors)
  1440. {
  1441. sensors[0] = st.chip_cfg.fifo_enable;
  1442. return 0;
  1443. }
  1444. /**
  1445. * @brief Select which sensors are pushed to FIFO.
  1446. * @e sensors can contain a combination of the following flags:
  1447. * \n INV_X_GYRO, INV_Y_GYRO, INV_Z_GYRO
  1448. * \n INV_XYZ_GYRO
  1449. * \n INV_XYZ_ACCEL
  1450. * @param[in] sensors Mask of sensors to push to FIFO.
  1451. * @return 0 if successful.
  1452. */
  1453. int mpu_configure_fifo(unsigned char sensors)
  1454. {
  1455. unsigned char prev;
  1456. int result = 0;
  1457. /* Compass data isn't going into the FIFO. Stop trying. */
  1458. sensors &= ~INV_XYZ_COMPASS;
  1459. if (st.chip_cfg.dmp_on)
  1460. return 0;
  1461. else {
  1462. if (!(st.chip_cfg.sensors))
  1463. return -1;
  1464. prev = st.chip_cfg.fifo_enable;
  1465. st.chip_cfg.fifo_enable = sensors & st.chip_cfg.sensors;
  1466. if (st.chip_cfg.fifo_enable != sensors)
  1467. /* You're not getting what you asked for. Some sensors are
  1468. * asleep.
  1469. */
  1470. result = -1;
  1471. else
  1472. result = 0;
  1473. if (sensors || st.chip_cfg.lp_accel_mode)
  1474. set_int_enable(1);
  1475. else
  1476. set_int_enable(0);
  1477. if (sensors) {
  1478. if (mpu_reset_fifo()) {
  1479. st.chip_cfg.fifo_enable = prev;
  1480. return -1;
  1481. }
  1482. }
  1483. }
  1484. return result;
  1485. }
  1486. /**
  1487. * @brief Get current power state.
  1488. * @param[in] power_on 1 if turned on, 0 if suspended.
  1489. * @return 0 if successful.
  1490. */
  1491. int mpu_get_power_state(unsigned char *power_on)
  1492. {
  1493. if (st.chip_cfg.sensors)
  1494. power_on[0] = 1;
  1495. else
  1496. power_on[0] = 0;
  1497. return 0;
  1498. }
  1499. /**
  1500. * @brief Turn specific sensors on/off.
  1501. * @e sensors can contain a combination of the following flags:
  1502. * \n INV_X_GYRO, INV_Y_GYRO, INV_Z_GYRO
  1503. * \n INV_XYZ_GYRO
  1504. * \n INV_XYZ_ACCEL
  1505. * \n INV_XYZ_COMPASS
  1506. * @param[in] sensors Mask of sensors to wake.
  1507. * @return 0 if successful.
  1508. */
  1509. int mpu_set_sensors(unsigned char sensors)
  1510. {
  1511. unsigned char data;
  1512. #ifdef AK89xx_SECONDARY
  1513. unsigned char user_ctrl;
  1514. #endif
  1515. if (sensors & INV_XYZ_GYRO)
  1516. data = INV_CLK_PLL;
  1517. else if (sensors)
  1518. data = 0;
  1519. else
  1520. data = BIT_SLEEP;
  1521. if (i2c_write(st.hw->addr, st.reg->pwr_mgmt_1, 1, &data)) {
  1522. st.chip_cfg.sensors = 0;
  1523. return -1;
  1524. }
  1525. st.chip_cfg.clk_src = data & ~BIT_SLEEP;
  1526. data = 0;
  1527. if (!(sensors & INV_X_GYRO))
  1528. data |= BIT_STBY_XG;
  1529. if (!(sensors & INV_Y_GYRO))
  1530. data |= BIT_STBY_YG;
  1531. if (!(sensors & INV_Z_GYRO))
  1532. data |= BIT_STBY_ZG;
  1533. if (!(sensors & INV_XYZ_ACCEL))
  1534. data |= BIT_STBY_XYZA;
  1535. if (i2c_write(st.hw->addr, st.reg->pwr_mgmt_2, 1, &data)) {
  1536. st.chip_cfg.sensors = 0;
  1537. return -1;
  1538. }
  1539. if (sensors && (sensors != INV_XYZ_ACCEL))
  1540. /* Latched interrupts only used in LP accel mode. */
  1541. mpu_set_int_latched(0);
  1542. #ifdef AK89xx_SECONDARY
  1543. #ifdef AK89xx_BYPASS
  1544. if (sensors & INV_XYZ_COMPASS)
  1545. mpu_set_bypass(1);
  1546. else
  1547. mpu_set_bypass(0);
  1548. #else
  1549. if (i2c_read(st.hw->addr, st.reg->user_ctrl, 1, &user_ctrl))
  1550. return -1;
  1551. /* Handle AKM power management. */
  1552. if (sensors & INV_XYZ_COMPASS) {
  1553. data = AKM_SINGLE_MEASUREMENT;
  1554. user_ctrl |= BIT_AUX_IF_EN;
  1555. } else {
  1556. data = AKM_POWER_DOWN;
  1557. user_ctrl &= ~BIT_AUX_IF_EN;
  1558. }
  1559. if (st.chip_cfg.dmp_on)
  1560. user_ctrl |= BIT_DMP_EN;
  1561. else
  1562. user_ctrl &= ~BIT_DMP_EN;
  1563. if (i2c_write(st.hw->addr, st.reg->s1_do, 1, &data))
  1564. return -1;
  1565. /* Enable/disable I2C master mode. */
  1566. if (i2c_write(st.hw->addr, st.reg->user_ctrl, 1, &user_ctrl))
  1567. return -1;
  1568. #endif
  1569. #endif
  1570. st.chip_cfg.sensors = sensors;
  1571. st.chip_cfg.lp_accel_mode = 0;
  1572. delay_ms(50);
  1573. return 0;
  1574. }
  1575. /**
  1576. * @brief Read the MPU interrupt status registers.
  1577. * @param[out] status Mask of interrupt bits.
  1578. * @return 0 if successful.
  1579. */
  1580. int mpu_get_int_status(short *status)
  1581. {
  1582. unsigned char tmp[2];
  1583. if (!st.chip_cfg.sensors)
  1584. return -1;
  1585. if (i2c_read(st.hw->addr, st.reg->dmp_int_status, 2, tmp))
  1586. return -1;
  1587. status[0] = (tmp[0] << 8) | tmp[1];
  1588. return 0;
  1589. }
  1590. /**
  1591. * @brief Get one packet from the FIFO.
  1592. * If @e sensors does not contain a particular sensor, disregard the data
  1593. * returned to that pointer.
  1594. * \n @e sensors can contain a combination of the following flags:
  1595. * \n INV_X_GYRO, INV_Y_GYRO, INV_Z_GYRO
  1596. * \n INV_XYZ_GYRO
  1597. * \n INV_XYZ_ACCEL
  1598. * \n If the FIFO has no new data, @e sensors will be zero.
  1599. * \n If the FIFO is disabled, @e sensors will be zero and this function will
  1600. * return a non-zero error code.
  1601. * @param[out] gyro Gyro data in hardware units.
  1602. * @param[out] accel Accel data in hardware units.
  1603. * @param[out] timestamp Timestamp in milliseconds.
  1604. * @param[out] sensors Mask of sensors read from FIFO.
  1605. * @param[out] more Number of remaining packets.
  1606. * @return 0 if successful.
  1607. */
  1608. int mpu_read_fifo(short *gyro, short *accel, unsigned long *timestamp,
  1609. unsigned char *sensors, unsigned char *more)
  1610. {
  1611. /* Assumes maximum packet size is gyro (6) + accel (6). */
  1612. unsigned char data[MAX_PACKET_LENGTH];
  1613. unsigned char packet_size = 0;
  1614. unsigned short fifo_count, index = 0;
  1615. if (st.chip_cfg.dmp_on)
  1616. return -1;
  1617. sensors[0] = 0;
  1618. if (!st.chip_cfg.sensors)
  1619. return -1;
  1620. if (!st.chip_cfg.fifo_enable)
  1621. return -1;
  1622. if (st.chip_cfg.fifo_enable & INV_X_GYRO)
  1623. packet_size += 2;
  1624. if (st.chip_cfg.fifo_enable & INV_Y_GYRO)
  1625. packet_size += 2;
  1626. if (st.chip_cfg.fifo_enable & INV_Z_GYRO)
  1627. packet_size += 2;
  1628. if (st.chip_cfg.fifo_enable & INV_XYZ_ACCEL)
  1629. packet_size += 6;
  1630. if (i2c_read(st.hw->addr, st.reg->fifo_count_h, 2, data))
  1631. return -1;
  1632. fifo_count = (data[0] << 8) | data[1];
  1633. if (fifo_count < packet_size)
  1634. return 0;
  1635. // log_i("FIFO count: %hd\n", fifo_count);
  1636. if (fifo_count > (st.hw->max_fifo >> 1)) {
  1637. /* FIFO is 50% full, better check overflow bit. */
  1638. if (i2c_read(st.hw->addr, st.reg->int_status, 1, data))
  1639. return -1;
  1640. if (data[0] & BIT_FIFO_OVERFLOW) {
  1641. mpu_reset_fifo();
  1642. return -2;
  1643. }
  1644. }
  1645. get_ms((unsigned long*)timestamp);
  1646. if (i2c_read(st.hw->addr, st.reg->fifo_r_w, packet_size, data))
  1647. return -1;
  1648. more[0] = fifo_count / packet_size - 1;
  1649. sensors[0] = 0;
  1650. if ((index != packet_size) && st.chip_cfg.fifo_enable & INV_XYZ_ACCEL) {
  1651. accel[0] = (data[index+0] << 8) | data[index+1];
  1652. accel[1] = (data[index+2] << 8) | data[index+3];
  1653. accel[2] = (data[index+4] << 8) | data[index+5];
  1654. sensors[0] |= INV_XYZ_ACCEL;
  1655. index += 6;
  1656. }
  1657. if ((index != packet_size) && st.chip_cfg.fifo_enable & INV_X_GYRO) {
  1658. gyro[0] = (data[index+0] << 8) | data[index+1];
  1659. sensors[0] |= INV_X_GYRO;
  1660. index += 2;
  1661. }
  1662. if ((index != packet_size) && st.chip_cfg.fifo_enable & INV_Y_GYRO) {
  1663. gyro[1] = (data[index+0] << 8) | data[index+1];
  1664. sensors[0] |= INV_Y_GYRO;
  1665. index += 2;
  1666. }
  1667. if ((index != packet_size) && st.chip_cfg.fifo_enable & INV_Z_GYRO) {
  1668. gyro[2] = (data[index+0] << 8) | data[index+1];
  1669. sensors[0] |= INV_Z_GYRO;
  1670. index += 2;
  1671. }
  1672. return 0;
  1673. }
  1674. /**
  1675. * @brief Get one unparsed packet from the FIFO.
  1676. * This function should be used if the packet is to be parsed elsewhere.
  1677. * @param[in] length Length of one FIFO packet.
  1678. * @param[in] data FIFO packet.
  1679. * @param[in] more Number of remaining packets.
  1680. */
  1681. int mpu_read_fifo_stream(unsigned short length, unsigned char *data,
  1682. unsigned char *more)
  1683. {
  1684. unsigned char tmp[2];
  1685. unsigned short fifo_count;
  1686. if (!st.chip_cfg.dmp_on)
  1687. return -1;
  1688. if (!st.chip_cfg.sensors)
  1689. return -1;
  1690. if (i2c_read(st.hw->addr, st.reg->fifo_count_h, 2, tmp))
  1691. return -1;
  1692. fifo_count = (tmp[0] << 8) | tmp[1];
  1693. if (fifo_count < length) {
  1694. more[0] = 0;
  1695. return -1;
  1696. }
  1697. if (fifo_count > (st.hw->max_fifo >> 1)) {
  1698. /* FIFO is 50% full, better check overflow bit. */
  1699. if (i2c_read(st.hw->addr, st.reg->int_status, 1, tmp))
  1700. return -1;
  1701. if (tmp[0] & BIT_FIFO_OVERFLOW) {
  1702. LOG_E("BIT_FIFO_OVERFLOW mpu_reset_fifo..");
  1703. mpu_reset_fifo();
  1704. return -2;
  1705. }
  1706. }
  1707. if (i2c_read(st.hw->addr, st.reg->fifo_r_w, length, data))
  1708. return -1;
  1709. more[0] = fifo_count / length - 1;
  1710. return 0;
  1711. }
  1712. /**
  1713. * @brief Set device to bypass mode.
  1714. * @param[in] bypass_on 1 to enable bypass mode.
  1715. * @return 0 if successful.
  1716. */
  1717. int mpu_set_bypass(unsigned char bypass_on)
  1718. {
  1719. unsigned char tmp;
  1720. if (st.chip_cfg.bypass_mode == bypass_on)
  1721. return 0;
  1722. if (bypass_on) {
  1723. if (i2c_read(st.hw->addr, st.reg->user_ctrl, 1, &tmp))
  1724. return -1;
  1725. tmp &= ~BIT_AUX_IF_EN;
  1726. if (i2c_write(st.hw->addr, st.reg->user_ctrl, 1, &tmp))
  1727. return -1;
  1728. delay_ms(3);
  1729. tmp = BIT_BYPASS_EN;
  1730. if (st.chip_cfg.active_low_int)
  1731. tmp |= BIT_ACTL;
  1732. if (st.chip_cfg.latched_int)
  1733. tmp |= BIT_LATCH_EN | BIT_ANY_RD_CLR;
  1734. if (i2c_write(st.hw->addr, st.reg->int_pin_cfg, 1, &tmp))
  1735. return -1;
  1736. } else {
  1737. /* Enable I2C master mode if compass is being used. */
  1738. if (i2c_read(st.hw->addr, st.reg->user_ctrl, 1, &tmp))
  1739. return -1;
  1740. if (st.chip_cfg.sensors & INV_XYZ_COMPASS)
  1741. tmp |= BIT_AUX_IF_EN;
  1742. else
  1743. tmp &= ~BIT_AUX_IF_EN;
  1744. if (i2c_write(st.hw->addr, st.reg->user_ctrl, 1, &tmp))
  1745. return -1;
  1746. delay_ms(3);
  1747. if (st.chip_cfg.active_low_int)
  1748. tmp = BIT_ACTL;
  1749. else
  1750. tmp = 0;
  1751. if (st.chip_cfg.latched_int)
  1752. tmp |= BIT_LATCH_EN | BIT_ANY_RD_CLR;
  1753. if (i2c_write(st.hw->addr, st.reg->int_pin_cfg, 1, &tmp))
  1754. return -1;
  1755. }
  1756. st.chip_cfg.bypass_mode = bypass_on;
  1757. return 0;
  1758. }
  1759. /**
  1760. * @brief Set interrupt level.
  1761. * @param[in] active_low 1 for active low, 0 for active high.
  1762. * @return 0 if successful.
  1763. */
  1764. int mpu_set_int_level(unsigned char active_low)
  1765. {
  1766. st.chip_cfg.active_low_int = active_low;
  1767. return 0;
  1768. }
  1769. /**
  1770. * @brief Enable latched interrupts.
  1771. * Any MPU register will clear the interrupt.
  1772. * @param[in] enable 1 to enable, 0 to disable.
  1773. * @return 0 if successful.
  1774. */
  1775. int mpu_set_int_latched(unsigned char enable)
  1776. {
  1777. unsigned char tmp;
  1778. if (st.chip_cfg.latched_int == enable)
  1779. return 0;
  1780. if (enable)
  1781. tmp = BIT_LATCH_EN | BIT_ANY_RD_CLR;
  1782. else
  1783. tmp = 0;
  1784. if (st.chip_cfg.bypass_mode)
  1785. tmp |= BIT_BYPASS_EN;
  1786. if (st.chip_cfg.active_low_int)
  1787. tmp |= BIT_ACTL;
  1788. if (i2c_write(st.hw->addr, st.reg->int_pin_cfg, 1, &tmp))
  1789. return -1;
  1790. st.chip_cfg.latched_int = enable;
  1791. return 0;
  1792. }
  1793. #ifdef MPU6050
  1794. static int get_accel_prod_shift(float *st_shift)
  1795. {
  1796. unsigned char tmp[4], shift_code[3], ii;
  1797. if (i2c_read(st.hw->addr, 0x0D, 4, tmp))
  1798. return 0x07;
  1799. shift_code[0] = ((tmp[0] & 0xE0) >> 3) | ((tmp[3] & 0x30) >> 4);
  1800. shift_code[1] = ((tmp[1] & 0xE0) >> 3) | ((tmp[3] & 0x0C) >> 2);
  1801. shift_code[2] = ((tmp[2] & 0xE0) >> 3) | (tmp[3] & 0x03);
  1802. for (ii = 0; ii < 3; ii++) {
  1803. if (!shift_code[ii]) {
  1804. st_shift[ii] = 0.f;
  1805. continue;
  1806. }
  1807. /* Equivalent to..
  1808. * st_shift[ii] = 0.34f * powf(0.92f/0.34f, (shift_code[ii]-1) / 30.f)
  1809. */
  1810. st_shift[ii] = 0.34f;
  1811. while (--shift_code[ii])
  1812. st_shift[ii] *= 1.034f;
  1813. }
  1814. return 0;
  1815. }
  1816. static int accel_self_test(long *bias_regular, long *bias_st)
  1817. {
  1818. int jj, result = 0;
  1819. float st_shift[3], st_shift_cust, st_shift_var;
  1820. get_accel_prod_shift(st_shift);
  1821. for(jj = 0; jj < 3; jj++) {
  1822. st_shift_cust = labs(bias_regular[jj] - bias_st[jj]) / 65536.f;
  1823. if (st_shift[jj]) {
  1824. st_shift_var = st_shift_cust / st_shift[jj] - 1.f;
  1825. if (fabs(st_shift_var) > test.max_accel_var)
  1826. result |= 1 << jj;
  1827. } else if ((st_shift_cust < test.min_g) ||
  1828. (st_shift_cust > test.max_g))
  1829. result |= 1 << jj;
  1830. }
  1831. return result;
  1832. }
  1833. static int gyro_self_test(long *bias_regular, long *bias_st)
  1834. {
  1835. int jj, result = 0;
  1836. unsigned char tmp[3];
  1837. float st_shift, st_shift_cust, st_shift_var;
  1838. if (i2c_read(st.hw->addr, 0x0D, 3, tmp))
  1839. return 0x07;
  1840. tmp[0] &= 0x1F;
  1841. tmp[1] &= 0x1F;
  1842. tmp[2] &= 0x1F;
  1843. for (jj = 0; jj < 3; jj++) {
  1844. st_shift_cust = labs(bias_regular[jj] - bias_st[jj]) / 65536.f;
  1845. if (tmp[jj]) {
  1846. st_shift = 3275.f / test.gyro_sens;
  1847. while (--tmp[jj])
  1848. st_shift *= 1.046f;
  1849. st_shift_var = st_shift_cust / st_shift - 1.f;
  1850. if (fabs(st_shift_var) > test.max_gyro_var)
  1851. result |= 1 << jj;
  1852. } else if ((st_shift_cust < test.min_dps) ||
  1853. (st_shift_cust > test.max_dps))
  1854. result |= 1 << jj;
  1855. }
  1856. return result;
  1857. }
  1858. #endif
  1859. #ifdef AK89xx_SECONDARY
  1860. static int compass_self_test(void)
  1861. {
  1862. unsigned char tmp[6];
  1863. unsigned char tries = 10;
  1864. int result = 0x07;
  1865. short data;
  1866. mpu_set_bypass(1);
  1867. tmp[0] = AKM_POWER_DOWN;
  1868. if (i2c_write(st.chip_cfg.compass_addr, AKM_REG_CNTL, 1, tmp))
  1869. return 0x07;
  1870. tmp[0] = AKM_BIT_SELF_TEST;
  1871. if (i2c_write(st.chip_cfg.compass_addr, AKM_REG_ASTC, 1, tmp))
  1872. goto AKM_restore;
  1873. tmp[0] = AKM_MODE_SELF_TEST;
  1874. if (i2c_write(st.chip_cfg.compass_addr, AKM_REG_CNTL, 1, tmp))
  1875. goto AKM_restore;
  1876. do {
  1877. delay_ms(10);
  1878. if (i2c_read(st.chip_cfg.compass_addr, AKM_REG_ST1, 1, tmp))
  1879. goto AKM_restore;
  1880. if (tmp[0] & AKM_DATA_READY)
  1881. break;
  1882. } while (tries--);
  1883. if (!(tmp[0] & AKM_DATA_READY))
  1884. goto AKM_restore;
  1885. if (i2c_read(st.chip_cfg.compass_addr, AKM_REG_HXL, 6, tmp))
  1886. goto AKM_restore;
  1887. result = 0;
  1888. #if defined MPU9150
  1889. data = (short)(tmp[1] << 8) | tmp[0];
  1890. if ((data > 100) || (data < -100))
  1891. result |= 0x01;
  1892. data = (short)(tmp[3] << 8) | tmp[2];
  1893. if ((data > 100) || (data < -100))
  1894. result |= 0x02;
  1895. data = (short)(tmp[5] << 8) | tmp[4];
  1896. if ((data > -300) || (data < -1000))
  1897. result |= 0x04;
  1898. #elif defined MPU9250
  1899. data = (short)(tmp[1] << 8) | tmp[0];
  1900. if ((data > 200) || (data < -200))
  1901. result |= 0x01;
  1902. data = (short)(tmp[3] << 8) | tmp[2];
  1903. if ((data > 200) || (data < -200))
  1904. result |= 0x02;
  1905. data = (short)(tmp[5] << 8) | tmp[4];
  1906. if ((data > -800) || (data < -3200))
  1907. result |= 0x04;
  1908. #endif
  1909. AKM_restore:
  1910. tmp[0] = 0 | SUPPORTS_AK89xx_HIGH_SENS;
  1911. i2c_write(st.chip_cfg.compass_addr, AKM_REG_ASTC, 1, tmp);
  1912. tmp[0] = SUPPORTS_AK89xx_HIGH_SENS;
  1913. i2c_write(st.chip_cfg.compass_addr, AKM_REG_CNTL, 1, tmp);
  1914. mpu_set_bypass(0);
  1915. return result;
  1916. }
  1917. #endif
  1918. static int get_st_biases(long *gyro, long *accel, unsigned char hw_test)
  1919. {
  1920. unsigned char data[MAX_PACKET_LENGTH];
  1921. unsigned char packet_count, ii;
  1922. unsigned short fifo_count;
  1923. data[0] = 0x01;
  1924. data[1] = 0;
  1925. if (i2c_write(st.hw->addr, st.reg->pwr_mgmt_1, 2, data))
  1926. return -1;
  1927. delay_ms(200);
  1928. data[0] = 0;
  1929. if (i2c_write(st.hw->addr, st.reg->int_enable, 1, data))
  1930. return -1;
  1931. if (i2c_write(st.hw->addr, st.reg->fifo_en, 1, data))
  1932. return -1;
  1933. if (i2c_write(st.hw->addr, st.reg->pwr_mgmt_1, 1, data))
  1934. return -1;
  1935. if (i2c_write(st.hw->addr, st.reg->i2c_mst, 1, data))
  1936. return -1;
  1937. if (i2c_write(st.hw->addr, st.reg->user_ctrl, 1, data))
  1938. return -1;
  1939. data[0] = BIT_FIFO_RST | BIT_DMP_RST;
  1940. if (i2c_write(st.hw->addr, st.reg->user_ctrl, 1, data))
  1941. return -1;
  1942. delay_ms(15);
  1943. data[0] = st.test->reg_lpf;
  1944. if (i2c_write(st.hw->addr, st.reg->lpf, 1, data))
  1945. return -1;
  1946. data[0] = st.test->reg_rate_div;
  1947. if (i2c_write(st.hw->addr, st.reg->rate_div, 1, data))
  1948. return -1;
  1949. if (hw_test)
  1950. data[0] = st.test->reg_gyro_fsr | 0xE0;
  1951. else
  1952. data[0] = st.test->reg_gyro_fsr;
  1953. if (i2c_write(st.hw->addr, st.reg->gyro_cfg, 1, data))
  1954. return -1;
  1955. if (hw_test)
  1956. data[0] = st.test->reg_accel_fsr | 0xE0;
  1957. else
  1958. data[0] = test.reg_accel_fsr;
  1959. if (i2c_write(st.hw->addr, st.reg->accel_cfg, 1, data))
  1960. return -1;
  1961. if (hw_test)
  1962. delay_ms(200);
  1963. /* Fill FIFO for test.wait_ms milliseconds. */
  1964. data[0] = BIT_FIFO_EN;
  1965. if (i2c_write(st.hw->addr, st.reg->user_ctrl, 1, data))
  1966. return -1;
  1967. data[0] = INV_XYZ_GYRO | INV_XYZ_ACCEL;
  1968. if (i2c_write(st.hw->addr, st.reg->fifo_en, 1, data))
  1969. return -1;
  1970. delay_ms(test.wait_ms);
  1971. data[0] = 0;
  1972. if (i2c_write(st.hw->addr, st.reg->fifo_en, 1, data))
  1973. return -1;
  1974. if (i2c_read(st.hw->addr, st.reg->fifo_count_h, 2, data))
  1975. return -1;
  1976. fifo_count = (data[0] << 8) | data[1];
  1977. packet_count = fifo_count / MAX_PACKET_LENGTH;
  1978. gyro[0] = gyro[1] = gyro[2] = 0;
  1979. accel[0] = accel[1] = accel[2] = 0;
  1980. for (ii = 0; ii < packet_count; ii++) {
  1981. short accel_cur[3], gyro_cur[3];
  1982. if (i2c_read(st.hw->addr, st.reg->fifo_r_w, MAX_PACKET_LENGTH, data))
  1983. return -1;
  1984. accel_cur[0] = ((short)data[0] << 8) | data[1];
  1985. accel_cur[1] = ((short)data[2] << 8) | data[3];
  1986. accel_cur[2] = ((short)data[4] << 8) | data[5];
  1987. accel[0] += (long)accel_cur[0];
  1988. accel[1] += (long)accel_cur[1];
  1989. accel[2] += (long)accel_cur[2];
  1990. gyro_cur[0] = (((short)data[6] << 8) | data[7]);
  1991. gyro_cur[1] = (((short)data[8] << 8) | data[9]);
  1992. gyro_cur[2] = (((short)data[10] << 8) | data[11]);
  1993. gyro[0] += (long)gyro_cur[0];
  1994. gyro[1] += (long)gyro_cur[1];
  1995. gyro[2] += (long)gyro_cur[2];
  1996. }
  1997. #ifdef EMPL_NO_64BIT
  1998. gyro[0] = (long)(((float)gyro[0]*65536.f) / test.gyro_sens / packet_count);
  1999. gyro[1] = (long)(((float)gyro[1]*65536.f) / test.gyro_sens / packet_count);
  2000. gyro[2] = (long)(((float)gyro[2]*65536.f) / test.gyro_sens / packet_count);
  2001. if (has_accel) {
  2002. accel[0] = (long)(((float)accel[0]*65536.f) / test.accel_sens /
  2003. packet_count);
  2004. accel[1] = (long)(((float)accel[1]*65536.f) / test.accel_sens /
  2005. packet_count);
  2006. accel[2] = (long)(((float)accel[2]*65536.f) / test.accel_sens /
  2007. packet_count);
  2008. /* Don't remove gravity! */
  2009. accel[2] -= 65536L;
  2010. }
  2011. #else
  2012. gyro[0] = (long)(((long long)gyro[0]<<16) / test.gyro_sens / packet_count);
  2013. gyro[1] = (long)(((long long)gyro[1]<<16) / test.gyro_sens / packet_count);
  2014. gyro[2] = (long)(((long long)gyro[2]<<16) / test.gyro_sens / packet_count);
  2015. accel[0] = (long)(((long long)accel[0]<<16) / test.accel_sens /
  2016. packet_count);
  2017. accel[1] = (long)(((long long)accel[1]<<16) / test.accel_sens /
  2018. packet_count);
  2019. accel[2] = (long)(((long long)accel[2]<<16) / test.accel_sens /
  2020. packet_count);
  2021. /* Don't remove gravity! */
  2022. if (accel[2] > 0L)
  2023. accel[2] -= 65536L;
  2024. else
  2025. accel[2] += 65536L;
  2026. #endif
  2027. return 0;
  2028. }
  2029. #ifdef MPU6500
  2030. #define REG_6500_XG_ST_DATA 0x0
  2031. #define REG_6500_XA_ST_DATA 0xD
  2032. static const unsigned short mpu_6500_st_tb[256] = {
  2033. 2620,2646,2672,2699,2726,2753,2781,2808, //7
  2034. 2837,2865,2894,2923,2952,2981,3011,3041, //15
  2035. 3072,3102,3133,3165,3196,3228,3261,3293, //23
  2036. 3326,3359,3393,3427,3461,3496,3531,3566, //31
  2037. 3602,3638,3674,3711,3748,3786,3823,3862, //39
  2038. 3900,3939,3979,4019,4059,4099,4140,4182, //47
  2039. 4224,4266,4308,4352,4395,4439,4483,4528, //55
  2040. 4574,4619,4665,4712,4759,4807,4855,4903, //63
  2041. 4953,5002,5052,5103,5154,5205,5257,5310, //71
  2042. 5363,5417,5471,5525,5581,5636,5693,5750, //79
  2043. 5807,5865,5924,5983,6043,6104,6165,6226, //87
  2044. 6289,6351,6415,6479,6544,6609,6675,6742, //95
  2045. 6810,6878,6946,7016,7086,7157,7229,7301, //103
  2046. 7374,7448,7522,7597,7673,7750,7828,7906, //111
  2047. 7985,8065,8145,8227,8309,8392,8476,8561, //119
  2048. 8647,8733,8820,8909,8998,9088,9178,9270,
  2049. 9363,9457,9551,9647,9743,9841,9939,10038,
  2050. 10139,10240,10343,10446,10550,10656,10763,10870,
  2051. 10979,11089,11200,11312,11425,11539,11654,11771,
  2052. 11889,12008,12128,12249,12371,12495,12620,12746,
  2053. 12874,13002,13132,13264,13396,13530,13666,13802,
  2054. 13940,14080,14221,14363,14506,14652,14798,14946,
  2055. 15096,15247,15399,15553,15709,15866,16024,16184,
  2056. 16346,16510,16675,16842,17010,17180,17352,17526,
  2057. 17701,17878,18057,18237,18420,18604,18790,18978,
  2058. 19167,19359,19553,19748,19946,20145,20347,20550,
  2059. 20756,20963,21173,21385,21598,21814,22033,22253,
  2060. 22475,22700,22927,23156,23388,23622,23858,24097,
  2061. 24338,24581,24827,25075,25326,25579,25835,26093,
  2062. 26354,26618,26884,27153,27424,27699,27976,28255,
  2063. 28538,28823,29112,29403,29697,29994,30294,30597,
  2064. 30903,31212,31524,31839,32157,32479,32804,33132
  2065. };
  2066. static int accel_6500_self_test(long *bias_regular, long *bias_st, int debug)
  2067. {
  2068. int i, result = 0, otp_value_zero = 0;
  2069. float accel_st_al_min, accel_st_al_max;
  2070. float st_shift_cust[3], st_shift_ratio[3], ct_shift_prod[3], accel_offset_max;
  2071. unsigned char regs[3];
  2072. if (i2c_read(st.hw->addr, REG_6500_XA_ST_DATA, 3, regs)) {
  2073. if(debug)
  2074. log_i("Reading OTP Register Error.\n");
  2075. return 0x07;
  2076. }
  2077. if(debug)
  2078. log_i("Accel OTP:%d, %d, %d\n", regs[0], regs[1], regs[2]);
  2079. for (i = 0; i < 3; i++) {
  2080. if (regs[i] != 0) {
  2081. ct_shift_prod[i] = mpu_6500_st_tb[regs[i] - 1];
  2082. ct_shift_prod[i] *= 65536.f;
  2083. ct_shift_prod[i] /= test.accel_sens;
  2084. }
  2085. else {
  2086. ct_shift_prod[i] = 0;
  2087. otp_value_zero = 1;
  2088. }
  2089. }
  2090. if(otp_value_zero == 0) {
  2091. if(debug)
  2092. log_i("ACCEL:CRITERIA A\n");
  2093. for (i = 0; i < 3; i++) {
  2094. st_shift_cust[i] = bias_st[i] - bias_regular[i];
  2095. if(debug) {
  2096. log_i("Bias_Shift=%7.4f, Bias_Reg=%7.4f, Bias_HWST=%7.4f\r\n",
  2097. st_shift_cust[i]/1.f, bias_regular[i]/1.f,
  2098. bias_st[i]/1.f);
  2099. log_i("OTP value: %7.4f\r\n", ct_shift_prod[i]/1.f);
  2100. }
  2101. st_shift_ratio[i] = st_shift_cust[i] / ct_shift_prod[i] - 1.f;
  2102. if(debug)
  2103. log_i("ratio=%7.4f, threshold=%7.4f\r\n", st_shift_ratio[i]/1.f,
  2104. test.max_accel_var/1.f);
  2105. if (fabs(st_shift_ratio[i]) > test.max_accel_var) {
  2106. if(debug)
  2107. log_i("ACCEL Fail Axis = %d\n", i);
  2108. result |= 1 << i; //Error condition
  2109. }
  2110. }
  2111. }
  2112. else {
  2113. /* Self Test Pass/Fail Criteria B */
  2114. accel_st_al_min = test.min_g * 65536.f;
  2115. accel_st_al_max = test.max_g * 65536.f;
  2116. if(debug) {
  2117. log_i("ACCEL:CRITERIA B\r\n");
  2118. log_i("Min MG: %7.4f\r\n", accel_st_al_min/1.f);
  2119. log_i("Max MG: %7.4f\r\n", accel_st_al_max/1.f);
  2120. }
  2121. for (i = 0; i < 3; i++) {
  2122. st_shift_cust[i] = bias_st[i] - bias_regular[i];
  2123. if(debug)
  2124. log_i("Bias_shift=%7.4f, st=%7.4f, reg=%7.4f\n", st_shift_cust[i]/1.f, bias_st[i]/1.f, bias_regular[i]/1.f);
  2125. if(st_shift_cust[i] < accel_st_al_min || st_shift_cust[i] > accel_st_al_max) {
  2126. if(debug)
  2127. log_i("Accel FAIL axis:%d <= 225mg or >= 675mg\n", i);
  2128. result |= 1 << i; //Error condition
  2129. }
  2130. }
  2131. }
  2132. if(result == 0) {
  2133. /* Self Test Pass/Fail Criteria C */
  2134. accel_offset_max = test.max_g_offset * 65536.f;
  2135. if(debug)
  2136. log_i("Accel:CRITERIA C: bias less than %7.4f\n", accel_offset_max/1.f);
  2137. for (i = 0; i < 3; i++) {
  2138. if(fabs(bias_regular[i]) > accel_offset_max) {
  2139. if(debug)
  2140. log_i("FAILED: Accel axis:%d = %ld > 500mg\n", i, bias_regular[i]);
  2141. result |= 1 << i; //Error condition
  2142. }
  2143. }
  2144. }
  2145. return result;
  2146. }
  2147. static int gyro_6500_self_test(long *bias_regular, long *bias_st, int debug)
  2148. {
  2149. int i, result = 0, otp_value_zero = 0;
  2150. float gyro_st_al_max;
  2151. float st_shift_cust[3], st_shift_ratio[3], ct_shift_prod[3], gyro_offset_max;
  2152. unsigned char regs[3];
  2153. if (i2c_read(st.hw->addr, REG_6500_XG_ST_DATA, 3, regs)) {
  2154. if(debug)
  2155. log_i("Reading OTP Register Error.\n");
  2156. return 0x07;
  2157. }
  2158. if(debug)
  2159. log_i("Gyro OTP:%d, %d, %d\r\n", regs[0], regs[1], regs[2]);
  2160. for (i = 0; i < 3; i++) {
  2161. if (regs[i] != 0) {
  2162. ct_shift_prod[i] = mpu_6500_st_tb[regs[i] - 1];
  2163. ct_shift_prod[i] *= 65536.f;
  2164. ct_shift_prod[i] /= test.gyro_sens;
  2165. }
  2166. else {
  2167. ct_shift_prod[i] = 0;
  2168. otp_value_zero = 1;
  2169. }
  2170. }
  2171. if(otp_value_zero == 0) {
  2172. if(debug)
  2173. log_i("GYRO:CRITERIA A\n");
  2174. /* Self Test Pass/Fail Criteria A */
  2175. for (i = 0; i < 3; i++) {
  2176. st_shift_cust[i] = bias_st[i] - bias_regular[i];
  2177. if(debug) {
  2178. log_i("Bias_Shift=%7.4f, Bias_Reg=%7.4f, Bias_HWST=%7.4f\r\n",
  2179. st_shift_cust[i]/1.f, bias_regular[i]/1.f,
  2180. bias_st[i]/1.f);
  2181. log_i("OTP value: %7.4f\r\n", ct_shift_prod[i]/1.f);
  2182. }
  2183. st_shift_ratio[i] = st_shift_cust[i] / ct_shift_prod[i];
  2184. if(debug)
  2185. log_i("ratio=%7.4f, threshold=%7.4f\r\n", st_shift_ratio[i]/1.f,
  2186. test.max_gyro_var/1.f);
  2187. if (fabs(st_shift_ratio[i]) < test.max_gyro_var) {
  2188. if(debug)
  2189. log_i("Gyro Fail Axis = %d\n", i);
  2190. result |= 1 << i; //Error condition
  2191. }
  2192. }
  2193. }
  2194. else {
  2195. /* Self Test Pass/Fail Criteria B */
  2196. gyro_st_al_max = test.max_dps * 65536.f;
  2197. if(debug) {
  2198. log_i("GYRO:CRITERIA B\r\n");
  2199. log_i("Max DPS: %7.4f\r\n", gyro_st_al_max/1.f);
  2200. }
  2201. for (i = 0; i < 3; i++) {
  2202. st_shift_cust[i] = bias_st[i] - bias_regular[i];
  2203. if(debug)
  2204. log_i("Bias_shift=%7.4f, st=%7.4f, reg=%7.4f\n", st_shift_cust[i]/1.f, bias_st[i]/1.f, bias_regular[i]/1.f);
  2205. if(st_shift_cust[i] < gyro_st_al_max) {
  2206. if(debug)
  2207. log_i("GYRO FAIL axis:%d greater than 60dps\n", i);
  2208. result |= 1 << i; //Error condition
  2209. }
  2210. }
  2211. }
  2212. if(result == 0) {
  2213. /* Self Test Pass/Fail Criteria C */
  2214. gyro_offset_max = test.min_dps * 65536.f;
  2215. if(debug)
  2216. log_i("Gyro:CRITERIA C: bias less than %7.4f\n", gyro_offset_max/1.f);
  2217. for (i = 0; i < 3; i++) {
  2218. if(fabs(bias_regular[i]) > gyro_offset_max) {
  2219. if(debug)
  2220. log_i("FAILED: Gyro axis:%d = %ld > 20dps\n", i, bias_regular[i]);
  2221. result |= 1 << i; //Error condition
  2222. }
  2223. }
  2224. }
  2225. return result;
  2226. }
  2227. static int get_st_6500_biases(long *gyro, long *accel, unsigned char hw_test, int debug)
  2228. {
  2229. unsigned char data[HWST_MAX_PACKET_LENGTH];
  2230. unsigned char packet_count, ii;
  2231. unsigned short fifo_count;
  2232. int s = 0, read_size = 0, ind;
  2233. data[0] = 0x01;
  2234. data[1] = 0;
  2235. if (i2c_write(st.hw->addr, st.reg->pwr_mgmt_1, 2, data))
  2236. return -1;
  2237. delay_ms(200);
  2238. data[0] = 0;
  2239. if (i2c_write(st.hw->addr, st.reg->int_enable, 1, data))
  2240. return -1;
  2241. if (i2c_write(st.hw->addr, st.reg->fifo_en, 1, data))
  2242. return -1;
  2243. if (i2c_write(st.hw->addr, st.reg->pwr_mgmt_1, 1, data))
  2244. return -1;
  2245. if (i2c_write(st.hw->addr, st.reg->i2c_mst, 1, data))
  2246. return -1;
  2247. if (i2c_write(st.hw->addr, st.reg->user_ctrl, 1, data))
  2248. return -1;
  2249. data[0] = BIT_FIFO_RST | BIT_DMP_RST;
  2250. if (i2c_write(st.hw->addr, st.reg->user_ctrl, 1, data))
  2251. return -1;
  2252. delay_ms(15);
  2253. data[0] = st.test->reg_lpf;
  2254. if (i2c_write(st.hw->addr, st.reg->lpf, 1, data))
  2255. return -1;
  2256. data[0] = st.test->reg_rate_div;
  2257. if (i2c_write(st.hw->addr, st.reg->rate_div, 1, data))
  2258. return -1;
  2259. if (hw_test)
  2260. data[0] = st.test->reg_gyro_fsr | 0xE0;
  2261. else
  2262. data[0] = st.test->reg_gyro_fsr;
  2263. if (i2c_write(st.hw->addr, st.reg->gyro_cfg, 1, data))
  2264. return -1;
  2265. if (hw_test)
  2266. data[0] = st.test->reg_accel_fsr | 0xE0;
  2267. else
  2268. data[0] = test.reg_accel_fsr;
  2269. if (i2c_write(st.hw->addr, st.reg->accel_cfg, 1, data))
  2270. return -1;
  2271. delay_ms(test.wait_ms); //wait 200ms for sensors to stabilize
  2272. /* Enable FIFO */
  2273. data[0] = BIT_FIFO_EN;
  2274. if (i2c_write(st.hw->addr, st.reg->user_ctrl, 1, data))
  2275. return -1;
  2276. data[0] = INV_XYZ_GYRO | INV_XYZ_ACCEL;
  2277. if (i2c_write(st.hw->addr, st.reg->fifo_en, 1, data))
  2278. return -1;
  2279. //initialize the bias return values
  2280. gyro[0] = gyro[1] = gyro[2] = 0;
  2281. accel[0] = accel[1] = accel[2] = 0;
  2282. if(debug)
  2283. log_i("Starting Bias Loop Reads\n");
  2284. //start reading samples
  2285. while (s < test.packet_thresh) {
  2286. delay_ms(test.sample_wait_ms); //wait 10ms to fill FIFO
  2287. if (i2c_read(st.hw->addr, st.reg->fifo_count_h, 2, data))
  2288. return -1;
  2289. fifo_count = (data[0] << 8) | data[1];
  2290. packet_count = fifo_count / MAX_PACKET_LENGTH;
  2291. if ((test.packet_thresh - s) < packet_count)
  2292. packet_count = test.packet_thresh - s;
  2293. read_size = packet_count * MAX_PACKET_LENGTH;
  2294. //burst read from FIFO
  2295. if (i2c_read(st.hw->addr, st.reg->fifo_r_w, read_size, data))
  2296. return -1;
  2297. ind = 0;
  2298. for (ii = 0; ii < packet_count; ii++) {
  2299. short accel_cur[3], gyro_cur[3];
  2300. accel_cur[0] = ((short)data[ind + 0] << 8) | data[ind + 1];
  2301. accel_cur[1] = ((short)data[ind + 2] << 8) | data[ind + 3];
  2302. accel_cur[2] = ((short)data[ind + 4] << 8) | data[ind + 5];
  2303. accel[0] += (long)accel_cur[0];
  2304. accel[1] += (long)accel_cur[1];
  2305. accel[2] += (long)accel_cur[2];
  2306. gyro_cur[0] = (((short)data[ind + 6] << 8) | data[ind + 7]);
  2307. gyro_cur[1] = (((short)data[ind + 8] << 8) | data[ind + 9]);
  2308. gyro_cur[2] = (((short)data[ind + 10] << 8) | data[ind + 11]);
  2309. gyro[0] += (long)gyro_cur[0];
  2310. gyro[1] += (long)gyro_cur[1];
  2311. gyro[2] += (long)gyro_cur[2];
  2312. ind += MAX_PACKET_LENGTH;
  2313. }
  2314. s += packet_count;
  2315. }
  2316. if(debug)
  2317. log_i("Samples: %d\n", s);
  2318. //stop FIFO
  2319. data[0] = 0;
  2320. if (i2c_write(st.hw->addr, st.reg->fifo_en, 1, data))
  2321. return -1;
  2322. gyro[0] = (long)(((long long)gyro[0]<<16) / test.gyro_sens / s);
  2323. gyro[1] = (long)(((long long)gyro[1]<<16) / test.gyro_sens / s);
  2324. gyro[2] = (long)(((long long)gyro[2]<<16) / test.gyro_sens / s);
  2325. accel[0] = (long)(((long long)accel[0]<<16) / test.accel_sens / s);
  2326. accel[1] = (long)(((long long)accel[1]<<16) / test.accel_sens / s);
  2327. accel[2] = (long)(((long long)accel[2]<<16) / test.accel_sens / s);
  2328. /* remove gravity from bias calculation */
  2329. if (accel[2] > 0L)
  2330. accel[2] -= 65536L;
  2331. else
  2332. accel[2] += 65536L;
  2333. if(debug) {
  2334. log_i("Accel offset data HWST bit=%d: %7.4f %7.4f %7.4f\r\n", hw_test, accel[0]/65536.f, accel[1]/65536.f, accel[2]/65536.f);
  2335. log_i("Gyro offset data HWST bit=%d: %7.4f %7.4f %7.4f\r\n", hw_test, gyro[0]/65536.f, gyro[1]/65536.f, gyro[2]/65536.f);
  2336. }
  2337. return 0;
  2338. }
  2339. /**
  2340. * @brief Trigger gyro/accel/compass self-test for MPU6500/MPU9250
  2341. * On success/error, the self-test returns a mask representing the sensor(s)
  2342. * that failed. For each bit, a one (1) represents a "pass" case; conversely,
  2343. * a zero (0) indicates a failure.
  2344. *
  2345. * \n The mask is defined as follows:
  2346. * \n Bit 0: Gyro.
  2347. * \n Bit 1: Accel.
  2348. * \n Bit 2: Compass.
  2349. *
  2350. * @param[out] gyro Gyro biases in q16 format.
  2351. * @param[out] accel Accel biases (if applicable) in q16 format.
  2352. * @param[in] debug Debug flag used to print out more detailed logs. Must first set up logging in Motion Driver.
  2353. * @return Result mask (see above).
  2354. */
  2355. int mpu_run_6500_self_test(long *gyro, long *accel, unsigned char debug)
  2356. {
  2357. const unsigned char tries = 2;
  2358. long gyro_st[3], accel_st[3];
  2359. unsigned char accel_result, gyro_result;
  2360. #ifdef AK89xx_SECONDARY
  2361. unsigned char compass_result;
  2362. #endif
  2363. int ii;
  2364. int result;
  2365. unsigned char accel_fsr, fifo_sensors, sensors_on;
  2366. unsigned short gyro_fsr, sample_rate, lpf;
  2367. unsigned char dmp_was_on;
  2368. if(debug)
  2369. log_i("Starting MPU6500 HWST!\r\n");
  2370. if (st.chip_cfg.dmp_on) {
  2371. mpu_set_dmp_state(0);
  2372. dmp_was_on = 1;
  2373. } else
  2374. dmp_was_on = 0;
  2375. /* Get initial settings. */
  2376. mpu_get_gyro_fsr(&gyro_fsr);
  2377. mpu_get_accel_fsr(&accel_fsr);
  2378. mpu_get_lpf(&lpf);
  2379. mpu_get_sample_rate(&sample_rate);
  2380. sensors_on = st.chip_cfg.sensors;
  2381. mpu_get_fifo_config(&fifo_sensors);
  2382. if(debug)
  2383. log_i("Retrieving Biases\r\n");
  2384. for (ii = 0; ii < tries; ii++)
  2385. if (!get_st_6500_biases(gyro, accel, 0, debug))
  2386. break;
  2387. if (ii == tries) {
  2388. /* If we reach this point, we most likely encountered an I2C error.
  2389. * We'll just report an error for all three sensors.
  2390. */
  2391. if(debug)
  2392. log_i("Retrieving Biases Error - possible I2C error\n");
  2393. result = 0;
  2394. goto restore;
  2395. }
  2396. if(debug)
  2397. log_i("Retrieving ST Biases\n");
  2398. for (ii = 0; ii < tries; ii++)
  2399. if (!get_st_6500_biases(gyro_st, accel_st, 1, debug))
  2400. break;
  2401. if (ii == tries) {
  2402. if(debug)
  2403. log_i("Retrieving ST Biases Error - possible I2C error\n");
  2404. /* Again, probably an I2C error. */
  2405. result = 0;
  2406. goto restore;
  2407. }
  2408. accel_result = accel_6500_self_test(accel, accel_st, debug);
  2409. if(debug)
  2410. log_i("Accel Self Test Results: %d\n", accel_result);
  2411. gyro_result = gyro_6500_self_test(gyro, gyro_st, debug);
  2412. if(debug)
  2413. log_i("Gyro Self Test Results: %d\n", gyro_result);
  2414. result = 0;
  2415. if (!gyro_result)
  2416. result |= 0x01;
  2417. if (!accel_result)
  2418. result |= 0x02;
  2419. #ifdef AK89xx_SECONDARY
  2420. compass_result = compass_self_test();
  2421. if(debug)
  2422. log_i("Compass Self Test Results: %d\n", compass_result);
  2423. if (!compass_result)
  2424. result |= 0x04;
  2425. #else
  2426. result |= 0x04;
  2427. #endif
  2428. restore:
  2429. if(debug)
  2430. log_i("Exiting HWST\n");
  2431. /* Set to invalid values to ensure no I2C writes are skipped. */
  2432. st.chip_cfg.gyro_fsr = 0xFF;
  2433. st.chip_cfg.accel_fsr = 0xFF;
  2434. st.chip_cfg.lpf = 0xFF;
  2435. st.chip_cfg.sample_rate = 0xFFFF;
  2436. st.chip_cfg.sensors = 0xFF;
  2437. st.chip_cfg.fifo_enable = 0xFF;
  2438. st.chip_cfg.clk_src = INV_CLK_PLL;
  2439. mpu_set_gyro_fsr(gyro_fsr);
  2440. mpu_set_accel_fsr(accel_fsr);
  2441. mpu_set_lpf(lpf);
  2442. mpu_set_sample_rate(sample_rate);
  2443. mpu_set_sensors(sensors_on);
  2444. mpu_configure_fifo(fifo_sensors);
  2445. if (dmp_was_on)
  2446. mpu_set_dmp_state(1);
  2447. return result;
  2448. }
  2449. #endif
  2450. /*
  2451. * \n This function must be called with the device either face-up or face-down
  2452. * (z-axis is parallel to gravity).
  2453. * @param[out] gyro Gyro biases in q16 format.
  2454. * @param[out] accel Accel biases (if applicable) in q16 format.
  2455. * @return Result mask (see above).
  2456. */
  2457. int mpu_run_self_test(long *gyro, long *accel)
  2458. {
  2459. #ifdef MPU6050
  2460. const unsigned char tries = 2;
  2461. long gyro_st[3], accel_st[3];
  2462. unsigned char accel_result, gyro_result;
  2463. #ifdef AK89xx_SECONDARY
  2464. unsigned char compass_result;
  2465. #endif
  2466. int ii;
  2467. #endif
  2468. int result;
  2469. unsigned char accel_fsr, fifo_sensors, sensors_on;
  2470. unsigned short gyro_fsr, sample_rate, lpf;
  2471. unsigned char dmp_was_on;
  2472. if (st.chip_cfg.dmp_on) {
  2473. mpu_set_dmp_state(0);
  2474. dmp_was_on = 1;
  2475. } else
  2476. dmp_was_on = 0;
  2477. /* Get initial settings. */
  2478. mpu_get_gyro_fsr(&gyro_fsr);
  2479. mpu_get_accel_fsr(&accel_fsr);
  2480. mpu_get_lpf(&lpf);
  2481. mpu_get_sample_rate(&sample_rate);
  2482. sensors_on = st.chip_cfg.sensors;
  2483. mpu_get_fifo_config(&fifo_sensors);
  2484. /* For older chips, the self-test will be different. */
  2485. #if defined MPU6050
  2486. for (ii = 0; ii < tries; ii++)
  2487. if (!get_st_biases(gyro, accel, 0))
  2488. break;
  2489. if (ii == tries) {
  2490. /* If we reach this point, we most likely encountered an I2C error.
  2491. * We'll just report an error for all three sensors.
  2492. */
  2493. result = 0;
  2494. goto restore;
  2495. }
  2496. for (ii = 0; ii < tries; ii++)
  2497. if (!get_st_biases(gyro_st, accel_st, 1))
  2498. break;
  2499. if (ii == tries) {
  2500. /* Again, probably an I2C error. */
  2501. result = 0;
  2502. goto restore;
  2503. }
  2504. accel_result = accel_self_test(accel, accel_st);
  2505. gyro_result = gyro_self_test(gyro, gyro_st);
  2506. result = 0;
  2507. if (!gyro_result)
  2508. result |= 0x01;
  2509. if (!accel_result)
  2510. result |= 0x02;
  2511. #ifdef AK89xx_SECONDARY
  2512. compass_result = compass_self_test();
  2513. if (!compass_result)
  2514. result |= 0x04;
  2515. #else
  2516. result |= 0x04;
  2517. #endif
  2518. restore:
  2519. #elif defined MPU6500
  2520. /* For now, this function will return a "pass" result for all three sensors
  2521. * for compatibility with current test applications.
  2522. */
  2523. get_st_biases(gyro, accel, 0);
  2524. result = 0x7;
  2525. #endif
  2526. /* Set to invalid values to ensure no I2C writes are skipped. */
  2527. st.chip_cfg.gyro_fsr = 0xFF;
  2528. st.chip_cfg.accel_fsr = 0xFF;
  2529. st.chip_cfg.lpf = 0xFF;
  2530. st.chip_cfg.sample_rate = 0xFFFF;
  2531. st.chip_cfg.sensors = 0xFF;
  2532. st.chip_cfg.fifo_enable = 0xFF;
  2533. st.chip_cfg.clk_src = INV_CLK_PLL;
  2534. mpu_set_gyro_fsr(gyro_fsr);
  2535. mpu_set_accel_fsr(accel_fsr);
  2536. mpu_set_lpf(lpf);
  2537. mpu_set_sample_rate(sample_rate);
  2538. mpu_set_sensors(sensors_on);
  2539. mpu_configure_fifo(fifo_sensors);
  2540. if (dmp_was_on)
  2541. mpu_set_dmp_state(1);
  2542. return result;
  2543. }
  2544. /**
  2545. * @brief Write to the DMP memory.
  2546. * This function prevents I2C writes past the bank boundaries. The DMP memory
  2547. * is only accessible when the chip is awake.
  2548. * @param[in] mem_addr Memory location (bank << 8 | start address)
  2549. * @param[in] length Number of bytes to write.
  2550. * @param[in] data Bytes to write to memory.
  2551. * @return 0 if successful.
  2552. */
  2553. int mpu_write_mem(unsigned short mem_addr, unsigned short length,
  2554. unsigned char *data)
  2555. {
  2556. unsigned char tmp[2];
  2557. if (!data)
  2558. return -1;
  2559. if (!st.chip_cfg.sensors)
  2560. return -1;
  2561. tmp[0] = (unsigned char)(mem_addr >> 8);
  2562. tmp[1] = (unsigned char)(mem_addr & 0xFF);
  2563. /* Check bank boundaries. */
  2564. if (tmp[1] + length > st.hw->bank_size)
  2565. return -1;
  2566. if (i2c_write(st.hw->addr, st.reg->bank_sel, 2, tmp))
  2567. return -1;
  2568. if (i2c_write(st.hw->addr, st.reg->mem_r_w, length, data))
  2569. return -1;
  2570. return 0;
  2571. }
  2572. /**
  2573. * @brief Read from the DMP memory.
  2574. * This function prevents I2C reads past the bank boundaries. The DMP memory
  2575. * is only accessible when the chip is awake.
  2576. * @param[in] mem_addr Memory location (bank << 8 | start address)
  2577. * @param[in] length Number of bytes to read.
  2578. * @param[out] data Bytes read from memory.
  2579. * @return 0 if successful.
  2580. */
  2581. int mpu_read_mem(unsigned short mem_addr, unsigned short length,
  2582. unsigned char *data)
  2583. {
  2584. unsigned char tmp[2];
  2585. if (!data)
  2586. return -1;
  2587. if (!st.chip_cfg.sensors)
  2588. return -1;
  2589. tmp[0] = (unsigned char)(mem_addr >> 8);
  2590. tmp[1] = (unsigned char)(mem_addr & 0xFF);
  2591. /* Check bank boundaries. */
  2592. if (tmp[1] + length > st.hw->bank_size)
  2593. return -1;
  2594. if (i2c_write(st.hw->addr, st.reg->bank_sel, 2, tmp))
  2595. return -1;
  2596. if (i2c_read(st.hw->addr, st.reg->mem_r_w, length, data))
  2597. return -1;
  2598. return 0;
  2599. }
  2600. /**
  2601. * @brief Load and verify DMP image.
  2602. * @param[in] length Length of DMP image.
  2603. * @param[in] firmware DMP code.
  2604. * @param[in] start_addr Starting address of DMP code memory.
  2605. * @param[in] sample_rate Fixed sampling rate used when DMP is enabled.
  2606. * @return 0 if successful.
  2607. */
  2608. int mpu_load_firmware(unsigned short length, const unsigned char *firmware,
  2609. unsigned short start_addr, unsigned short sample_rate)
  2610. {
  2611. unsigned short ii;
  2612. unsigned short this_write;
  2613. /* Must divide evenly into st.hw->bank_size to avoid bank crossings. */
  2614. #define LOAD_CHUNK (16)
  2615. unsigned char cur[LOAD_CHUNK], tmp[2];
  2616. if (st.chip_cfg.dmp_loaded)
  2617. /* DMP should only be loaded once. */
  2618. return -1;
  2619. if (!firmware)
  2620. return -1;
  2621. for (ii = 0; ii < length; ii += this_write) {
  2622. this_write = min(LOAD_CHUNK, length - ii);
  2623. // rt_kprintf("this_write:%d,length-ii:%d\n",this_write,length - ii);
  2624. if (mpu_write_mem(ii, this_write, (unsigned char*)&firmware[ii]))
  2625. return -1;
  2626. if (mpu_read_mem(ii, this_write, cur))
  2627. return -1;
  2628. if (memcmp(firmware+ii, cur, this_write))
  2629. return -2;
  2630. }
  2631. /* Set program start address. */
  2632. tmp[0] = start_addr >> 8;
  2633. tmp[1] = start_addr & 0xFF;
  2634. if (i2c_write(st.hw->addr, st.reg->prgm_start_h, 2, tmp))
  2635. return -1;
  2636. st.chip_cfg.dmp_loaded = 1;
  2637. st.chip_cfg.dmp_sample_rate = sample_rate;
  2638. return 0;
  2639. }
  2640. /**
  2641. * @brief Enable/disable DMP support.
  2642. * @param[in] enable 1 to turn on the DMP.
  2643. * @return 0 if successful.
  2644. */
  2645. int mpu_set_dmp_state(unsigned char enable)
  2646. {
  2647. unsigned char tmp;
  2648. if (st.chip_cfg.dmp_on == enable)
  2649. return 0;
  2650. if (enable) {
  2651. if (!st.chip_cfg.dmp_loaded)
  2652. return -1;
  2653. /* Disable data ready interrupt. */
  2654. set_int_enable(0);
  2655. /* Disable bypass mode. */
  2656. mpu_set_bypass(0);
  2657. /* Keep constant sample rate, FIFO rate controlled by DMP. */
  2658. mpu_set_sample_rate(st.chip_cfg.dmp_sample_rate);
  2659. /* Remove FIFO elements. */
  2660. tmp = 0;
  2661. i2c_write(st.hw->addr, 0x23, 1, &tmp);
  2662. st.chip_cfg.dmp_on = 1;
  2663. /* Enable DMP interrupt. */
  2664. set_int_enable(1);
  2665. mpu_reset_fifo();
  2666. } else {
  2667. /* Disable DMP interrupt. */
  2668. set_int_enable(0);
  2669. /* Restore FIFO settings. */
  2670. tmp = st.chip_cfg.fifo_enable;
  2671. i2c_write(st.hw->addr, 0x23, 1, &tmp);
  2672. st.chip_cfg.dmp_on = 0;
  2673. mpu_reset_fifo();
  2674. }
  2675. return 0;
  2676. }
  2677. /**
  2678. * @brief Get DMP state.
  2679. * @param[out] enabled 1 if enabled.
  2680. * @return 0 if successful.
  2681. */
  2682. int mpu_get_dmp_state(unsigned char *enabled)
  2683. {
  2684. enabled[0] = st.chip_cfg.dmp_on;
  2685. return 0;
  2686. }
  2687. #ifdef AK89xx_SECONDARY
  2688. /* This initialization is similar to the one in ak8975.c. */
  2689. static int setup_compass(void)
  2690. {
  2691. unsigned char data[4], akm_addr;
  2692. mpu_set_bypass(1);
  2693. /* Find compass. Possible addresses range from 0x0C to 0x0F. */
  2694. for (akm_addr = 0x0C; akm_addr <= 0x0F; akm_addr++) {
  2695. int result;
  2696. result = i2c_read(akm_addr, AKM_REG_WHOAMI, 1, data);
  2697. if (!result && (data[0] == AKM_WHOAMI))
  2698. break;
  2699. }
  2700. if (akm_addr > 0x0F) {
  2701. /* TODO: Handle this case in all compass-related functions. */
  2702. log_e("Compass not found.\n");
  2703. return -1;
  2704. }
  2705. st.chip_cfg.compass_addr = akm_addr;
  2706. data[0] = AKM_POWER_DOWN;
  2707. if (i2c_write(st.chip_cfg.compass_addr, AKM_REG_CNTL, 1, data))
  2708. return -1;
  2709. delay_ms(1);
  2710. data[0] = AKM_FUSE_ROM_ACCESS;
  2711. if (i2c_write(st.chip_cfg.compass_addr, AKM_REG_CNTL, 1, data))
  2712. return -1;
  2713. delay_ms(1);
  2714. /* Get sensitivity adjustment data from fuse ROM. */
  2715. if (i2c_read(st.chip_cfg.compass_addr, AKM_REG_ASAX, 3, data))
  2716. return -1;
  2717. st.chip_cfg.mag_sens_adj[0] = (long)data[0] + 128;
  2718. st.chip_cfg.mag_sens_adj[1] = (long)data[1] + 128;
  2719. st.chip_cfg.mag_sens_adj[2] = (long)data[2] + 128;
  2720. data[0] = AKM_POWER_DOWN;
  2721. if (i2c_write(st.chip_cfg.compass_addr, AKM_REG_CNTL, 1, data))
  2722. return -1;
  2723. delay_ms(1);
  2724. mpu_set_bypass(0);
  2725. /* Set up master mode, master clock, and ES bit. */
  2726. data[0] = 0x40;
  2727. if (i2c_write(st.hw->addr, st.reg->i2c_mst, 1, data))
  2728. return -1;
  2729. /* Slave 0 reads from AKM data registers. */
  2730. data[0] = BIT_I2C_READ | st.chip_cfg.compass_addr;
  2731. if (i2c_write(st.hw->addr, st.reg->s0_addr, 1, data))
  2732. return -1;
  2733. /* Compass reads start at this register. */
  2734. data[0] = AKM_REG_ST1;
  2735. if (i2c_write(st.hw->addr, st.reg->s0_reg, 1, data))
  2736. return -1;
  2737. /* Enable slave 0, 8-byte reads. */
  2738. data[0] = BIT_SLAVE_EN | 8;
  2739. if (i2c_write(st.hw->addr, st.reg->s0_ctrl, 1, data))
  2740. return -1;
  2741. /* Slave 1 changes AKM measurement mode. */
  2742. data[0] = st.chip_cfg.compass_addr;
  2743. if (i2c_write(st.hw->addr, st.reg->s1_addr, 1, data))
  2744. return -1;
  2745. /* AKM measurement mode register. */
  2746. data[0] = AKM_REG_CNTL;
  2747. if (i2c_write(st.hw->addr, st.reg->s1_reg, 1, data))
  2748. return -1;
  2749. /* Enable slave 1, 1-byte writes. */
  2750. data[0] = BIT_SLAVE_EN | 1;
  2751. if (i2c_write(st.hw->addr, st.reg->s1_ctrl, 1, data))
  2752. return -1;
  2753. /* Set slave 1 data. */
  2754. data[0] = AKM_SINGLE_MEASUREMENT;
  2755. if (i2c_write(st.hw->addr, st.reg->s1_do, 1, data))
  2756. return -1;
  2757. /* Trigger slave 0 and slave 1 actions at each sample. */
  2758. data[0] = 0x03;
  2759. if (i2c_write(st.hw->addr, st.reg->i2c_delay_ctrl, 1, data))
  2760. return -1;
  2761. #ifdef MPU9150
  2762. /* For the MPU9150, the auxiliary I2C bus needs to be set to VDD. */
  2763. data[0] = BIT_I2C_MST_VDDIO;
  2764. if (i2c_write(st.hw->addr, st.reg->yg_offs_tc, 1, data))
  2765. return -1;
  2766. #endif
  2767. return 0;
  2768. }
  2769. #endif
  2770. /**
  2771. * @brief Read raw compass data.
  2772. * @param[out] data Raw data in hardware units.
  2773. * @param[out] timestamp Timestamp in milliseconds. Null if not needed.
  2774. * @return 0 if successful.
  2775. */
  2776. int mpu_get_compass_reg(short *data, unsigned long *timestamp)
  2777. {
  2778. #ifdef AK89xx_SECONDARY
  2779. unsigned char tmp[9];
  2780. if (!(st.chip_cfg.sensors & INV_XYZ_COMPASS))
  2781. return -1;
  2782. #ifdef AK89xx_BYPASS
  2783. if (i2c_read(st.chip_cfg.compass_addr, AKM_REG_ST1, 8, tmp))
  2784. return -1;
  2785. tmp[8] = AKM_SINGLE_MEASUREMENT;
  2786. if (i2c_write(st.chip_cfg.compass_addr, AKM_REG_CNTL, 1, tmp+8))
  2787. return -1;
  2788. #else
  2789. if (i2c_read(st.hw->addr, st.reg->raw_compass, 8, tmp))
  2790. return -1;
  2791. #endif
  2792. #if defined AK8975_SECONDARY
  2793. /* AK8975 doesn't have the overrun error bit. */
  2794. if (!(tmp[0] & AKM_DATA_READY))
  2795. return -2;
  2796. if ((tmp[7] & AKM_OVERFLOW) || (tmp[7] & AKM_DATA_ERROR))
  2797. return -3;
  2798. #elif defined AK8963_SECONDARY
  2799. /* AK8963 doesn't have the data read error bit. */
  2800. if (!(tmp[0] & AKM_DATA_READY) || (tmp[0] & AKM_DATA_OVERRUN))
  2801. return -2;
  2802. if (tmp[7] & AKM_OVERFLOW)
  2803. return -3;
  2804. #endif
  2805. data[0] = (tmp[2] << 8) | tmp[1];
  2806. data[1] = (tmp[4] << 8) | tmp[3];
  2807. data[2] = (tmp[6] << 8) | tmp[5];
  2808. data[0] = ((long)data[0] * st.chip_cfg.mag_sens_adj[0]) >> 8;
  2809. data[1] = ((long)data[1] * st.chip_cfg.mag_sens_adj[1]) >> 8;
  2810. data[2] = ((long)data[2] * st.chip_cfg.mag_sens_adj[2]) >> 8;
  2811. if (timestamp)
  2812. get_ms(timestamp);
  2813. return 0;
  2814. #else
  2815. return -1;
  2816. #endif
  2817. }
  2818. /**
  2819. * @brief Get the compass full-scale range.
  2820. * @param[out] fsr Current full-scale range.
  2821. * @return 0 if successful.
  2822. */
  2823. int mpu_get_compass_fsr(unsigned short *fsr)
  2824. {
  2825. #ifdef AK89xx_SECONDARY
  2826. fsr[0] = st.hw->compass_fsr;
  2827. return 0;
  2828. #else
  2829. return -1;
  2830. #endif
  2831. }
  2832. /**
  2833. * @brief Enters LP accel motion interrupt mode.
  2834. * The behaviour of this feature is very different between the MPU6050 and the
  2835. * MPU6500. Each chip's version of this feature is explained below.
  2836. *
  2837. * \n The hardware motion threshold can be between 32mg and 8160mg in 32mg
  2838. * increments.
  2839. *
  2840. * \n Low-power accel mode supports the following frequencies:
  2841. * \n 1.25Hz, 5Hz, 20Hz, 40Hz
  2842. *
  2843. * \n MPU6500:
  2844. * \n Unlike the MPU6050 version, the hardware does not "lock in" a reference
  2845. * sample. The hardware monitors the accel data and detects any large change
  2846. * over a short period of time.
  2847. *
  2848. * \n The hardware motion threshold can be between 4mg and 1020mg in 4mg
  2849. * increments.
  2850. *
  2851. * \n MPU6500 Low-power accel mode supports the following frequencies:
  2852. * \n 1.25Hz, 2.5Hz, 5Hz, 10Hz, 20Hz, 40Hz, 80Hz, 160Hz, 320Hz, 640Hz
  2853. *
  2854. * \n\n NOTES:
  2855. * \n The driver will round down @e thresh to the nearest supported value if
  2856. * an unsupported threshold is selected.
  2857. * \n To select a fractional wake-up frequency, round down the value passed to
  2858. * @e lpa_freq.
  2859. * \n The MPU6500 does not support a delay parameter. If this function is used
  2860. * for the MPU6500, the value passed to @e time will be ignored.
  2861. * \n To disable this mode, set @e lpa_freq to zero. The driver will restore
  2862. * the previous configuration.
  2863. *
  2864. * @param[in] thresh Motion threshold in mg.
  2865. * @param[in] time Duration in milliseconds that the accel data must
  2866. * exceed @e thresh before motion is reported.
  2867. * @param[in] lpa_freq Minimum sampling rate, or zero to disable.
  2868. * @return 0 if successful.
  2869. */
  2870. int mpu_lp_motion_interrupt(unsigned short thresh, unsigned char time,
  2871. unsigned short lpa_freq)
  2872. {
  2873. #if defined MPU6500
  2874. unsigned char data[3];
  2875. #endif
  2876. if (lpa_freq) {
  2877. #if defined MPU6500
  2878. unsigned char thresh_hw;
  2879. /* 1LSb = 4mg. */
  2880. if (thresh > 1020)
  2881. thresh_hw = 255;
  2882. else if (thresh < 4)
  2883. thresh_hw = 1;
  2884. else
  2885. thresh_hw = thresh >> 2;
  2886. #endif
  2887. if (!time)
  2888. /* Minimum duration must be 1ms. */
  2889. time = 1;
  2890. #if defined MPU6500
  2891. if (lpa_freq > 640)
  2892. /* At this point, the chip has not been re-configured, so the
  2893. * function can safely exit.
  2894. */
  2895. return -1;
  2896. #endif
  2897. if (!st.chip_cfg.int_motion_only) {
  2898. /* Store current settings for later. */
  2899. if (st.chip_cfg.dmp_on) {
  2900. mpu_set_dmp_state(0);
  2901. st.chip_cfg.cache.dmp_on = 1;
  2902. } else
  2903. st.chip_cfg.cache.dmp_on = 0;
  2904. mpu_get_gyro_fsr(&st.chip_cfg.cache.gyro_fsr);
  2905. mpu_get_accel_fsr(&st.chip_cfg.cache.accel_fsr);
  2906. mpu_get_lpf(&st.chip_cfg.cache.lpf);
  2907. mpu_get_sample_rate(&st.chip_cfg.cache.sample_rate);
  2908. st.chip_cfg.cache.sensors_on = st.chip_cfg.sensors;
  2909. mpu_get_fifo_config(&st.chip_cfg.cache.fifo_sensors);
  2910. }
  2911. #if defined MPU6500
  2912. /* Disable hardware interrupts. */
  2913. set_int_enable(0);
  2914. /* Enter full-power accel-only mode, no FIFO/DMP. */
  2915. data[0] = 0;
  2916. data[1] = 0;
  2917. data[2] = BIT_STBY_XYZG;
  2918. if (i2c_write(st.hw->addr, st.reg->user_ctrl, 3, data))
  2919. goto lp_int_restore;
  2920. /* Set motion threshold. */
  2921. data[0] = thresh_hw;
  2922. if (i2c_write(st.hw->addr, st.reg->motion_thr, 1, data))
  2923. goto lp_int_restore;
  2924. /* Set wake frequency. */
  2925. if (lpa_freq == 1)
  2926. data[0] = INV_LPA_1_25HZ;
  2927. else if (lpa_freq == 2)
  2928. data[0] = INV_LPA_2_5HZ;
  2929. else if (lpa_freq <= 5)
  2930. data[0] = INV_LPA_5HZ;
  2931. else if (lpa_freq <= 10)
  2932. data[0] = INV_LPA_10HZ;
  2933. else if (lpa_freq <= 20)
  2934. data[0] = INV_LPA_20HZ;
  2935. else if (lpa_freq <= 40)
  2936. data[0] = INV_LPA_40HZ;
  2937. else if (lpa_freq <= 80)
  2938. data[0] = INV_LPA_80HZ;
  2939. else if (lpa_freq <= 160)
  2940. data[0] = INV_LPA_160HZ;
  2941. else if (lpa_freq <= 320)
  2942. data[0] = INV_LPA_320HZ;
  2943. else
  2944. data[0] = INV_LPA_640HZ;
  2945. if (i2c_write(st.hw->addr, st.reg->lp_accel_odr, 1, data))
  2946. goto lp_int_restore;
  2947. /* Enable motion interrupt (MPU6500 version). */
  2948. data[0] = BITS_WOM_EN;
  2949. if (i2c_write(st.hw->addr, st.reg->accel_intel, 1, data))
  2950. goto lp_int_restore;
  2951. /* Enable cycle mode. */
  2952. data[0] = BIT_LPA_CYCLE;
  2953. if (i2c_write(st.hw->addr, st.reg->pwr_mgmt_1, 1, data))
  2954. goto lp_int_restore;
  2955. /* Enable interrupt. */
  2956. data[0] = BIT_MOT_INT_EN;
  2957. if (i2c_write(st.hw->addr, st.reg->int_enable, 1, data))
  2958. goto lp_int_restore;
  2959. st.chip_cfg.int_motion_only = 1;
  2960. return 0;
  2961. #endif
  2962. } else {
  2963. /* Don't "restore" the previous state if no state has been saved. */
  2964. unsigned int ii;
  2965. char *cache_ptr = (char*)&st.chip_cfg.cache;
  2966. for (ii = 0; ii < sizeof(st.chip_cfg.cache); ii++) {
  2967. if (cache_ptr[ii] != 0)
  2968. goto lp_int_restore;
  2969. }
  2970. /* If we reach this point, motion interrupt mode hasn't been used yet. */
  2971. return -1;
  2972. }
  2973. lp_int_restore:
  2974. /* Set to invalid values to ensure no I2C writes are skipped. */
  2975. st.chip_cfg.gyro_fsr = 0xFF;
  2976. st.chip_cfg.accel_fsr = 0xFF;
  2977. st.chip_cfg.lpf = 0xFF;
  2978. st.chip_cfg.sample_rate = 0xFFFF;
  2979. st.chip_cfg.sensors = 0xFF;
  2980. st.chip_cfg.fifo_enable = 0xFF;
  2981. st.chip_cfg.clk_src = INV_CLK_PLL;
  2982. mpu_set_sensors(st.chip_cfg.cache.sensors_on);
  2983. mpu_set_gyro_fsr(st.chip_cfg.cache.gyro_fsr);
  2984. mpu_set_accel_fsr(st.chip_cfg.cache.accel_fsr);
  2985. mpu_set_lpf(st.chip_cfg.cache.lpf);
  2986. mpu_set_sample_rate(st.chip_cfg.cache.sample_rate);
  2987. mpu_configure_fifo(st.chip_cfg.cache.fifo_sensors);
  2988. if (st.chip_cfg.cache.dmp_on)
  2989. mpu_set_dmp_state(1);
  2990. #ifdef MPU6500
  2991. /* Disable motion interrupt (MPU6500 version). */
  2992. data[0] = 0;
  2993. if (i2c_write(st.hw->addr, st.reg->accel_intel, 1, data))
  2994. goto lp_int_restore;
  2995. #endif
  2996. st.chip_cfg.int_motion_only = 0;
  2997. return 0;
  2998. }
  2999. /**
  3000. * @}
  3001. */