inv_mpu.c 77 KB

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