inv_mpu.c 80 KB

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