inv_mpu.c 85 KB

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