当前位置:   article > 正文

六轴加速陀螺仪MPU6500/MPU6050使用及DMP库移植,含记步器功能

mpu6500

本篇博客为博主学习使用MPU6500完成后的学习记录,故在这只做主要讲解,如有博友看完后仍不知如何使用,可在下方留言问我,或发邮件问我(dayou1024@qq.com),因邮件有QQ和微信提示,故我能更快看到.

工程代码及开发资料下载连接https://download.csdn.net/download/dayou1024/12589762

1、MPU6500/MPU6050是一个6轴的传感器,及加速度和角速度各有 x,y,z轴

相关数据手册可在网上找到,如立创商城

2、我这里使用I2C总线驱动MPU6500,要想后面的程序能调通,首先要确保I2C连续读写都正确,能读写MPU6500一两个寄存器正确并不能说明你的I2C一定是可靠了的,因为后面要用到官方给的函数库,这些库要大量连续读写数据。检验你的I2C是否可靠,可能这样操作:

    MPU6500中有以下四个寄存器,可读可写,且它自己不会往里写数据,我们可以连续往这写入四个字节,再读出,读出写入一样说明I2C可靠。

 

DMP库自带的获取6500温度函数是不对的,我们可以参考数据手册自己写

 

3、我们从MPU6500的寄存器中出来的加速度和角速度值,意义是不大的,主要是想得到欧拉角,但我们自己通过加速度和角速度来计算欧拉角是很麻烦的,官方给了一个DMP解算例程,例程中的core文件夹包含了库函数文件

我们通过DMP函数得能这些角度数据后,用串口发送数据到“匿名四轴”2.6版上位机,可得到3D姿态图

 

 

 

 

 

      5.1版本解算库包含以下几个文件,比6.12版轻量,适合小工程

我们需要修改两个C文件和inv_mpu.h文件,改动很小,只改到一些宏定义

为了尽量减少改动,inv_mpu_dmp_motion_driver.c 文件只需在头部加上自己的宏定义

inv_mpu_dmp_motion_driver.c 改动:

这段宏定义也是照搬文件下面的,Delay_ms函数提供给DMP库用于毫秒级延时,Get_Timer2_ms函数是毫秒计时,这两个函数要我们写出,printf是DMP库在使用串口输出一些提示信息

 

inv_mpu.c改动:

 

这里也是照着文件下面添加的,MPU6500_Write_Len 和 MPU6500_Read_Len函数是DMP库在操作MPU6500时要用的,由我们写一个函数提供给它,函数的形式在文件头部有说明,  fsbsf是C编译器的函数库,不用我们写,把math.h头文件包含进去就可以。

 

inv_mpu.h改动

这里只用加上41和42这两行,其实在函数中实际上也用不到

 

4,在配置MDK开发环境时,要定义STM32F10X_HD和MPU6500,当然地,也可以在文件中定义,因我为用的平台是STM32F103,所以我定义成这个,也可以定义成你自己的开发平台。

同时还要勾选使用C99标准,因为DMP库中有些语法C99才支持

 

以下是主要的驱动代码,关于STM32的引脚配置就不放上来了

1,首先是官方给的5.1版本DMP函数库,

(1)inv_mpu_dmp_motion_driver.c

  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_dmp_motion_driver.c
  13. * @brief DMP image and interface functions.
  14. * @details All functions are preceded by the dmp_ prefix to
  15. * differentiate among MPL and general driver function calls.
  16. */
  17. #include <stdio.h>
  18. #include <stdint.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #include "inv_mpu.h"
  23. #include "inv_mpu_dmp_motion_driver.h"
  24. #include "dmpKey.h"
  25. #include "dmpmap.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. */
  34. /***********以下为自己定义的系统平台,在STM32F103ZE上运行***************************/
  35. #if defined STM32F10X_HD //在编译器中定义 STM32F10X_HD 则使用这段,以下的系统平台将不会用到
  36. #include "delay.h"
  37. #include "timer.h"
  38. #include "stdio.h"
  39. #define delay_ms Delay_ms
  40. #define get_ms Get_Timer2_ms
  41. #define log_i printf //打印信息
  42. #define log_e printf //打印信息
  43. /*********************************************************/
  44. #elif defined MOTION_DRIVER_TARGET_MSP430
  45. #include "msp430.h"
  46. #include "msp430_clock.h"
  47. #define delay_ms msp430_delay_ms
  48. #define get_ms msp430_get_clock_ms
  49. #define log_i(...) do {} while (0)
  50. #define log_e(...) do {} while (0)
  51. #elif defined EMPL_TARGET_MSP430
  52. #include "msp430.h"
  53. #include "msp430_clock.h"
  54. #include "log.h"
  55. #define delay_ms msp430_delay_ms
  56. #define get_ms msp430_get_clock_ms
  57. #define log_i MPL_LOGI
  58. #define log_e MPL_LOGE
  59. #elif defined EMPL_TARGET_UC3L0
  60. /* Instead of using the standard TWI driver from the ASF library, we're using
  61. * a TWI driver that follows the slave address + register address convention.
  62. */
  63. #include "delay.h"
  64. #include "sysclk.h"
  65. #include "log.h"
  66. #include "uc3l0_clock.h"
  67. /* delay_ms is a function already defined in ASF. */
  68. #define get_ms uc3l0_get_clock_ms
  69. #define log_i MPL_LOGI
  70. #define log_e MPL_LOGE
  71. #else
  72. #error Gyro driver is missing the system layer implementations.
  73. #endif
  74. /* These defines are copied from dmpDefaultMPU6050.c in the general MPL
  75. * releases. These defines may change for each DMP image, so be sure to modify
  76. * these values when switching to a new image.
  77. */
  78. #define CFG_LP_QUAT (2712)
  79. #define END_ORIENT_TEMP (1866)
  80. #define CFG_27 (2742)
  81. #define CFG_20 (2224)
  82. #define CFG_23 (2745)
  83. #define CFG_FIFO_ON_EVENT (2690)
  84. #define END_PREDICTION_UPDATE (1761)
  85. #define CGNOTICE_INTR (2620)
  86. #define X_GRT_Y_TMP (1358)
  87. #define CFG_DR_INT (1029)
  88. #define CFG_AUTH (1035)
  89. #define UPDATE_PROP_ROT (1835)
  90. #define END_COMPARE_Y_X_TMP2 (1455)
  91. #define SKIP_X_GRT_Y_TMP (1359)
  92. #define SKIP_END_COMPARE (1435)
  93. #define FCFG_3 (1088)
  94. #define FCFG_2 (1066)
  95. #define FCFG_1 (1062)
  96. #define END_COMPARE_Y_X_TMP3 (1434)
  97. #define FCFG_7 (1073)
  98. #define FCFG_6 (1106)
  99. #define FLAT_STATE_END (1713)
  100. #define SWING_END_4 (1616)
  101. #define SWING_END_2 (1565)
  102. #define SWING_END_3 (1587)
  103. #define SWING_END_1 (1550)
  104. #define CFG_8 (2718)
  105. #define CFG_15 (2727)
  106. #define CFG_16 (2746)
  107. #define CFG_EXT_GYRO_BIAS (1189)
  108. #define END_COMPARE_Y_X_TMP (1407)
  109. #define DO_NOT_UPDATE_PROP_ROT (1839)
  110. #define CFG_7 (1205)
  111. #define FLAT_STATE_END_TEMP (1683)
  112. #define END_COMPARE_Y_X (1484)
  113. #define SKIP_SWING_END_1 (1551)
  114. #define SKIP_SWING_END_3 (1588)
  115. #define SKIP_SWING_END_2 (1566)
  116. #define TILTG75_START (1672)
  117. #define CFG_6 (2753)
  118. #define TILTL75_END (1669)
  119. #define END_ORIENT (1884)
  120. #define CFG_FLICK_IN (2573)
  121. #define TILTL75_START (1643)
  122. #define CFG_MOTION_BIAS (1208)
  123. #define X_GRT_Y (1408)
  124. #define TEMPLABEL (2324)
  125. #define CFG_ANDROID_ORIENT_INT (1853)
  126. #define CFG_GYRO_RAW_DATA (2722)
  127. #define X_GRT_Y_TMP2 (1379)
  128. #define D_0_22 (22+512)
  129. #define D_0_24 (24+512)
  130. #define D_0_36 (36)
  131. #define D_0_52 (52)
  132. #define D_0_96 (96)
  133. #define D_0_104 (104)
  134. #define D_0_108 (108)
  135. #define D_0_163 (163)
  136. #define D_0_188 (188)
  137. #define D_0_192 (192)
  138. #define D_0_224 (224)
  139. #define D_0_228 (228)
  140. #define D_0_232 (232)
  141. #define D_0_236 (236)
  142. #define D_1_2 (256 + 2)
  143. #define D_1_4 (256 + 4)
  144. #define D_1_8 (256 + 8)
  145. #define D_1_10 (256 + 10)
  146. #define D_1_24 (256 + 24)
  147. #define D_1_28 (256 + 28)
  148. #define D_1_36 (256 + 36)
  149. #define D_1_40 (256 + 40)
  150. #define D_1_44 (256 + 44)
  151. #define D_1_72 (256 + 72)
  152. #define D_1_74 (256 + 74)
  153. #define D_1_79 (256 + 79)
  154. #define D_1_88 (256 + 88)
  155. #define D_1_90 (256 + 90)
  156. #define D_1_92 (256 + 92)
  157. #define D_1_96 (256 + 96)
  158. #define D_1_98 (256 + 98)
  159. #define D_1_106 (256 + 106)
  160. #define D_1_108 (256 + 108)
  161. #define D_1_112 (256 + 112)
  162. #define D_1_128 (256 + 144)
  163. #define D_1_152 (256 + 12)
  164. #define D_1_160 (256 + 160)
  165. #define D_1_176 (256 + 176)
  166. #define D_1_178 (256 + 178)
  167. #define D_1_218 (256 + 218)
  168. #define D_1_232 (256 + 232)
  169. #define D_1_236 (256 + 236)
  170. #define D_1_240 (256 + 240)
  171. #define D_1_244 (256 + 244)
  172. #define D_1_250 (256 + 250)
  173. #define D_1_252 (256 + 252)
  174. #define D_2_12 (512 + 12)
  175. #define D_2_96 (512 + 96)
  176. #define D_2_108 (512 + 108)
  177. #define D_2_208 (512 + 208)
  178. #define D_2_224 (512 + 224)
  179. #define D_2_236 (512 + 236)
  180. #define D_2_244 (512 + 244)
  181. #define D_2_248 (512 + 248)
  182. #define D_2_252 (512 + 252)
  183. #define CPASS_BIAS_X (35 * 16 + 4)
  184. #define CPASS_BIAS_Y (35 * 16 + 8)
  185. #define CPASS_BIAS_Z (35 * 16 + 12)
  186. #define CPASS_MTX_00 (36 * 16)
  187. #define CPASS_MTX_01 (36 * 16 + 4)
  188. #define CPASS_MTX_02 (36 * 16 + 8)
  189. #define CPASS_MTX_10 (36 * 16 + 12)
  190. #define CPASS_MTX_11 (37 * 16)
  191. #define CPASS_MTX_12 (37 * 16 + 4)
  192. #define CPASS_MTX_20 (37 * 16 + 8)
  193. #define CPASS_MTX_21 (37 * 16 + 12)
  194. #define CPASS_MTX_22 (43 * 16 + 12)
  195. #define D_EXT_GYRO_BIAS_X (61 * 16)
  196. #define D_EXT_GYRO_BIAS_Y (61 * 16) + 4
  197. #define D_EXT_GYRO_BIAS_Z (61 * 16) + 8
  198. #define D_ACT0 (40 * 16)
  199. #define D_ACSX (40 * 16 + 4)
  200. #define D_ACSY (40 * 16 + 8)
  201. #define D_ACSZ (40 * 16 + 12)
  202. #define FLICK_MSG (45 * 16 + 4)
  203. #define FLICK_COUNTER (45 * 16 + 8)
  204. #define FLICK_LOWER (45 * 16 + 12)
  205. #define FLICK_UPPER (46 * 16 + 12)
  206. #define D_AUTH_OUT (992)
  207. #define D_AUTH_IN (996)
  208. #define D_AUTH_A (1000)
  209. #define D_AUTH_B (1004)
  210. #define D_PEDSTD_BP_B (768 + 0x1C)
  211. #define D_PEDSTD_HP_A (768 + 0x78)
  212. #define D_PEDSTD_HP_B (768 + 0x7C)
  213. #define D_PEDSTD_BP_A4 (768 + 0x40)
  214. #define D_PEDSTD_BP_A3 (768 + 0x44)
  215. #define D_PEDSTD_BP_A2 (768 + 0x48)
  216. #define D_PEDSTD_BP_A1 (768 + 0x4C)
  217. #define D_PEDSTD_INT_THRSH (768 + 0x68)
  218. #define D_PEDSTD_CLIP (768 + 0x6C)
  219. #define D_PEDSTD_SB (768 + 0x28)
  220. #define D_PEDSTD_SB_TIME (768 + 0x2C)
  221. #define D_PEDSTD_PEAKTHRSH (768 + 0x98)
  222. #define D_PEDSTD_TIML (768 + 0x2A)
  223. #define D_PEDSTD_TIMH (768 + 0x2E)
  224. #define D_PEDSTD_PEAK (768 + 0X94)
  225. #define D_PEDSTD_STEPCTR (768 + 0x60)
  226. #define D_PEDSTD_TIMECTR (964)
  227. #define D_PEDSTD_DECI (768 + 0xA0)
  228. #define D_HOST_NO_MOT (976)
  229. #define D_ACCEL_BIAS (660)
  230. #define D_ORIENT_GAP (76)
  231. #define D_TILT0_H (48)
  232. #define D_TILT0_L (50)
  233. #define D_TILT1_H (52)
  234. #define D_TILT1_L (54)
  235. #define D_TILT2_H (56)
  236. #define D_TILT2_L (58)
  237. #define D_TILT3_H (60)
  238. #define D_TILT3_L (62)
  239. #define DMP_CODE_SIZE (3062)
  240. static const unsigned char dmp_memory[DMP_CODE_SIZE] = {
  241. /* bank # 0 */
  242. 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00,
  243. 0x00, 0x65, 0x00, 0x54, 0xff, 0xef, 0x00, 0x00, 0xfa, 0x80, 0x00, 0x0b, 0x12, 0x82, 0x00, 0x01,
  244. 0x03, 0x0c, 0x30, 0xc3, 0x0e, 0x8c, 0x8c, 0xe9, 0x14, 0xd5, 0x40, 0x02, 0x13, 0x71, 0x0f, 0x8e,
  245. 0x38, 0x83, 0xf8, 0x83, 0x30, 0x00, 0xf8, 0x83, 0x25, 0x8e, 0xf8, 0x83, 0x30, 0x00, 0xf8, 0x83,
  246. 0xff, 0xff, 0xff, 0xff, 0x0f, 0xfe, 0xa9, 0xd6, 0x24, 0x00, 0x04, 0x00, 0x1a, 0x82, 0x79, 0xa1,
  247. 0x00, 0x00, 0x00, 0x3c, 0xff, 0xff, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x38, 0x83, 0x6f, 0xa2,
  248. 0x00, 0x3e, 0x03, 0x30, 0x40, 0x00, 0x00, 0x00, 0x02, 0xca, 0xe3, 0x09, 0x3e, 0x80, 0x00, 0x00,
  249. 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00,
  250. 0x00, 0x0c, 0x00, 0x00, 0x00, 0x0c, 0x18, 0x6e, 0x00, 0x00, 0x06, 0x92, 0x0a, 0x16, 0xc0, 0xdf,
  251. 0xff, 0xff, 0x02, 0x56, 0xfd, 0x8c, 0xd3, 0x77, 0xff, 0xe1, 0xc4, 0x96, 0xe0, 0xc5, 0xbe, 0xaa,
  252. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x0b, 0x2b, 0x00, 0x00, 0x16, 0x57, 0x00, 0x00, 0x03, 0x59,
  253. 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1d, 0xfa, 0x00, 0x02, 0x6c, 0x1d, 0x00, 0x00, 0x00, 0x00,
  254. 0x3f, 0xff, 0xdf, 0xeb, 0x00, 0x3e, 0xb3, 0xb6, 0x00, 0x0d, 0x22, 0x78, 0x00, 0x00, 0x2f, 0x3c,
  255. 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x42, 0xb5, 0x00, 0x00, 0x39, 0xa2, 0x00, 0x00, 0xb3, 0x65,
  256. 0xd9, 0x0e, 0x9f, 0xc9, 0x1d, 0xcf, 0x4c, 0x34, 0x30, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  257. 0x3b, 0xb6, 0x7a, 0xe8, 0x00, 0x64, 0x00, 0x00, 0x00, 0xc8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  258. /* bank # 1 */
  259. 0x10, 0x00, 0x00, 0x00, 0x10, 0x00, 0xfa, 0x92, 0x10, 0x00, 0x22, 0x5e, 0x00, 0x0d, 0x22, 0x9f,
  260. 0x00, 0x01, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0xff, 0x46, 0x00, 0x00, 0x63, 0xd4, 0x00, 0x00,
  261. 0x10, 0x00, 0x00, 0x00, 0x04, 0xd6, 0x00, 0x00, 0x04, 0xcc, 0x00, 0x00, 0x04, 0xcc, 0x00, 0x00,
  262. 0x00, 0x00, 0x10, 0x72, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  263. 0x00, 0x06, 0x00, 0x02, 0x00, 0x05, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00,
  264. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x00, 0x64, 0x00, 0x20, 0x00, 0x00,
  265. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x03, 0x00,
  266. 0x00, 0x00, 0x00, 0x32, 0xf8, 0x98, 0x00, 0x00, 0xff, 0x65, 0x00, 0x00, 0x83, 0x0f, 0x00, 0x00,
  267. 0xff, 0x9b, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  268. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  269. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
  270. 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0xb2, 0x6a, 0x00, 0x02, 0x00, 0x00,
  271. 0x00, 0x01, 0xfb, 0x83, 0x00, 0x68, 0x00, 0x00, 0x00, 0xd9, 0xfc, 0x00, 0x7c, 0xf1, 0xff, 0x83,
  272. 0x00, 0x00, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x64, 0x03, 0xe8, 0x00, 0x64, 0x00, 0x28,
  273. 0x00, 0x00, 0x00, 0x25, 0x00, 0x00, 0x00, 0x00, 0x16, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
  274. 0x00, 0x00, 0x10, 0x00, 0x00, 0x2f, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf4, 0x00, 0x00, 0x10, 0x00,
  275. /* bank # 2 */
  276. 0x00, 0x28, 0x00, 0x00, 0xff, 0xff, 0x45, 0x81, 0xff, 0xff, 0xfa, 0x72, 0x00, 0x00, 0x00, 0x00,
  277. 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x00, 0x05, 0x00, 0x05, 0xba, 0xc6, 0x00, 0x47, 0x78, 0xa2,
  278. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x14,
  279. 0x00, 0x00, 0x25, 0x4d, 0x00, 0x2f, 0x70, 0x6d, 0x00, 0x00, 0x05, 0xae, 0x00, 0x0c, 0x02, 0xd0,
  280. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  281. 0x00, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  282. 0x00, 0x64, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  283. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  284. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  285. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  286. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  287. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  288. 0x00, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x0e,
  289. 0x00, 0x00, 0x0a, 0xc7, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x32, 0xff, 0xff, 0xff, 0x9c,
  290. 0x00, 0x00, 0x0b, 0x2b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x64,
  291. 0xff, 0xe5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  292. /* bank # 3 */
  293. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  294. 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x24, 0x26, 0xd3,
  295. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x10, 0x00, 0x96, 0x00, 0x3c,
  296. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  297. 0x0c, 0x0a, 0x4e, 0x68, 0xcd, 0xcf, 0x77, 0x09, 0x50, 0x16, 0x67, 0x59, 0xc6, 0x19, 0xce, 0x82,
  298. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  299. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0xd7, 0x84, 0x00, 0x03, 0x00, 0x00, 0x00,
  300. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc7, 0x93, 0x8f, 0x9d, 0x1e, 0x1b, 0x1c, 0x19,
  301. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  302. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x18, 0x85, 0x00, 0x00, 0x40, 0x00,
  303. 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  304. 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  305. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  306. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  307. 0x00, 0x00, 0x00, 0x00, 0x67, 0x7d, 0xdf, 0x7e, 0x72, 0x90, 0x2e, 0x55, 0x4c, 0xf6, 0xe6, 0x88,
  308. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  309. /* bank # 4 */
  310. 0xd8, 0xdc, 0xb4, 0xb8, 0xb0, 0xd8, 0xb9, 0xab, 0xf3, 0xf8, 0xfa, 0xb3, 0xb7, 0xbb, 0x8e, 0x9e,
  311. 0xae, 0xf1, 0x32, 0xf5, 0x1b, 0xf1, 0xb4, 0xb8, 0xb0, 0x80, 0x97, 0xf1, 0xa9, 0xdf, 0xdf, 0xdf,
  312. 0xaa, 0xdf, 0xdf, 0xdf, 0xf2, 0xaa, 0xc5, 0xcd, 0xc7, 0xa9, 0x0c, 0xc9, 0x2c, 0x97, 0xf1, 0xa9,
  313. 0x89, 0x26, 0x46, 0x66, 0xb2, 0x89, 0x99, 0xa9, 0x2d, 0x55, 0x7d, 0xb0, 0xb0, 0x8a, 0xa8, 0x96,
  314. 0x36, 0x56, 0x76, 0xf1, 0xba, 0xa3, 0xb4, 0xb2, 0x80, 0xc0, 0xb8, 0xa8, 0x97, 0x11, 0xb2, 0x83,
  315. 0x98, 0xba, 0xa3, 0xf0, 0x24, 0x08, 0x44, 0x10, 0x64, 0x18, 0xb2, 0xb9, 0xb4, 0x98, 0x83, 0xf1,
  316. 0xa3, 0x29, 0x55, 0x7d, 0xba, 0xb5, 0xb1, 0xa3, 0x83, 0x93, 0xf0, 0x00, 0x28, 0x50, 0xf5, 0xb2,
  317. 0xb6, 0xaa, 0x83, 0x93, 0x28, 0x54, 0x7c, 0xf1, 0xb9, 0xa3, 0x82, 0x93, 0x61, 0xba, 0xa2, 0xda,
  318. 0xde, 0xdf, 0xdb, 0x81, 0x9a, 0xb9, 0xae, 0xf5, 0x60, 0x68, 0x70, 0xf1, 0xda, 0xba, 0xa2, 0xdf,
  319. 0xd9, 0xba, 0xa2, 0xfa, 0xb9, 0xa3, 0x82, 0x92, 0xdb, 0x31, 0xba, 0xa2, 0xd9, 0xba, 0xa2, 0xf8,
  320. 0xdf, 0x85, 0xa4, 0xd0, 0xc1, 0xbb, 0xad, 0x83, 0xc2, 0xc5, 0xc7, 0xb8, 0xa2, 0xdf, 0xdf, 0xdf,
  321. 0xba, 0xa0, 0xdf, 0xdf, 0xdf, 0xd8, 0xd8, 0xf1, 0xb8, 0xaa, 0xb3, 0x8d, 0xb4, 0x98, 0x0d, 0x35,
  322. 0x5d, 0xb2, 0xb6, 0xba, 0xaf, 0x8c, 0x96, 0x19, 0x8f, 0x9f, 0xa7, 0x0e, 0x16, 0x1e, 0xb4, 0x9a,
  323. 0xb8, 0xaa, 0x87, 0x2c, 0x54, 0x7c, 0xba, 0xa4, 0xb0, 0x8a, 0xb6, 0x91, 0x32, 0x56, 0x76, 0xb2,
  324. 0x84, 0x94, 0xa4, 0xc8, 0x08, 0xcd, 0xd8, 0xb8, 0xb4, 0xb0, 0xf1, 0x99, 0x82, 0xa8, 0x2d, 0x55,
  325. 0x7d, 0x98, 0xa8, 0x0e, 0x16, 0x1e, 0xa2, 0x2c, 0x54, 0x7c, 0x92, 0xa4, 0xf0, 0x2c, 0x50, 0x78,
  326. /* bank # 5 */
  327. 0xf1, 0x84, 0xa8, 0x98, 0xc4, 0xcd, 0xfc, 0xd8, 0x0d, 0xdb, 0xa8, 0xfc, 0x2d, 0xf3, 0xd9, 0xba,
  328. 0xa6, 0xf8, 0xda, 0xba, 0xa6, 0xde, 0xd8, 0xba, 0xb2, 0xb6, 0x86, 0x96, 0xa6, 0xd0, 0xf3, 0xc8,
  329. 0x41, 0xda, 0xa6, 0xc8, 0xf8, 0xd8, 0xb0, 0xb4, 0xb8, 0x82, 0xa8, 0x92, 0xf5, 0x2c, 0x54, 0x88,
  330. 0x98, 0xf1, 0x35, 0xd9, 0xf4, 0x18, 0xd8, 0xf1, 0xa2, 0xd0, 0xf8, 0xf9, 0xa8, 0x84, 0xd9, 0xc7,
  331. 0xdf, 0xf8, 0xf8, 0x83, 0xc5, 0xda, 0xdf, 0x69, 0xdf, 0x83, 0xc1, 0xd8, 0xf4, 0x01, 0x14, 0xf1,
  332. 0xa8, 0x82, 0x4e, 0xa8, 0x84, 0xf3, 0x11, 0xd1, 0x82, 0xf5, 0xd9, 0x92, 0x28, 0x97, 0x88, 0xf1,
  333. 0x09, 0xf4, 0x1c, 0x1c, 0xd8, 0x84, 0xa8, 0xf3, 0xc0, 0xf9, 0xd1, 0xd9, 0x97, 0x82, 0xf1, 0x29,
  334. 0xf4, 0x0d, 0xd8, 0xf3, 0xf9, 0xf9, 0xd1, 0xd9, 0x82, 0xf4, 0xc2, 0x03, 0xd8, 0xde, 0xdf, 0x1a,
  335. 0xd8, 0xf1, 0xa2, 0xfa, 0xf9, 0xa8, 0x84, 0x98, 0xd9, 0xc7, 0xdf, 0xf8, 0xf8, 0xf8, 0x83, 0xc7,
  336. 0xda, 0xdf, 0x69, 0xdf, 0xf8, 0x83, 0xc3, 0xd8, 0xf4, 0x01, 0x14, 0xf1, 0x98, 0xa8, 0x82, 0x2e,
  337. 0xa8, 0x84, 0xf3, 0x11, 0xd1, 0x82, 0xf5, 0xd9, 0x92, 0x50, 0x97, 0x88, 0xf1, 0x09, 0xf4, 0x1c,
  338. 0xd8, 0x84, 0xa8, 0xf3, 0xc0, 0xf8, 0xf9, 0xd1, 0xd9, 0x97, 0x82, 0xf1, 0x49, 0xf4, 0x0d, 0xd8,
  339. 0xf3, 0xf9, 0xf9, 0xd1, 0xd9, 0x82, 0xf4, 0xc4, 0x03, 0xd8, 0xde, 0xdf, 0xd8, 0xf1, 0xad, 0x88,
  340. 0x98, 0xcc, 0xa8, 0x09, 0xf9, 0xd9, 0x82, 0x92, 0xa8, 0xf5, 0x7c, 0xf1, 0x88, 0x3a, 0xcf, 0x94,
  341. 0x4a, 0x6e, 0x98, 0xdb, 0x69, 0x31, 0xda, 0xad, 0xf2, 0xde, 0xf9, 0xd8, 0x87, 0x95, 0xa8, 0xf2,
  342. 0x21, 0xd1, 0xda, 0xa5, 0xf9, 0xf4, 0x17, 0xd9, 0xf1, 0xae, 0x8e, 0xd0, 0xc0, 0xc3, 0xae, 0x82,
  343. /* bank # 6 */
  344. 0xc6, 0x84, 0xc3, 0xa8, 0x85, 0x95, 0xc8, 0xa5, 0x88, 0xf2, 0xc0, 0xf1, 0xf4, 0x01, 0x0e, 0xf1,
  345. 0x8e, 0x9e, 0xa8, 0xc6, 0x3e, 0x56, 0xf5, 0x54, 0xf1, 0x88, 0x72, 0xf4, 0x01, 0x15, 0xf1, 0x98,
  346. 0x45, 0x85, 0x6e, 0xf5, 0x8e, 0x9e, 0x04, 0x88, 0xf1, 0x42, 0x98, 0x5a, 0x8e, 0x9e, 0x06, 0x88,
  347. 0x69, 0xf4, 0x01, 0x1c, 0xf1, 0x98, 0x1e, 0x11, 0x08, 0xd0, 0xf5, 0x04, 0xf1, 0x1e, 0x97, 0x02,
  348. 0x02, 0x98, 0x36, 0x25, 0xdb, 0xf9, 0xd9, 0x85, 0xa5, 0xf3, 0xc1, 0xda, 0x85, 0xa5, 0xf3, 0xdf,
  349. 0xd8, 0x85, 0x95, 0xa8, 0xf3, 0x09, 0xda, 0xa5, 0xfa, 0xd8, 0x82, 0x92, 0xa8, 0xf5, 0x78, 0xf1,
  350. 0x88, 0x1a, 0x84, 0x9f, 0x26, 0x88, 0x98, 0x21, 0xda, 0xf4, 0x1d, 0xf3, 0xd8, 0x87, 0x9f, 0x39,
  351. 0xd1, 0xaf, 0xd9, 0xdf, 0xdf, 0xfb, 0xf9, 0xf4, 0x0c, 0xf3, 0xd8, 0xfa, 0xd0, 0xf8, 0xda, 0xf9,
  352. 0xf9, 0xd0, 0xdf, 0xd9, 0xf9, 0xd8, 0xf4, 0x0b, 0xd8, 0xf3, 0x87, 0x9f, 0x39, 0xd1, 0xaf, 0xd9,
  353. 0xdf, 0xdf, 0xf4, 0x1d, 0xf3, 0xd8, 0xfa, 0xfc, 0xa8, 0x69, 0xf9, 0xf9, 0xaf, 0xd0, 0xda, 0xde,
  354. 0xfa, 0xd9, 0xf8, 0x8f, 0x9f, 0xa8, 0xf1, 0xcc, 0xf3, 0x98, 0xdb, 0x45, 0xd9, 0xaf, 0xdf, 0xd0,
  355. 0xf8, 0xd8, 0xf1, 0x8f, 0x9f, 0xa8, 0xca, 0xf3, 0x88, 0x09, 0xda, 0xaf, 0x8f, 0xcb, 0xf8, 0xd8,
  356. 0xf2, 0xad, 0x97, 0x8d, 0x0c, 0xd9, 0xa5, 0xdf, 0xf9, 0xba, 0xa6, 0xf3, 0xfa, 0xf4, 0x12, 0xf2,
  357. 0xd8, 0x95, 0x0d, 0xd1, 0xd9, 0xba, 0xa6, 0xf3, 0xfa, 0xda, 0xa5, 0xf2, 0xc1, 0xba, 0xa6, 0xf3,
  358. 0xdf, 0xd8, 0xf1, 0xba, 0xb2, 0xb6, 0x86, 0x96, 0xa6, 0xd0, 0xca, 0xf3, 0x49, 0xda, 0xa6, 0xcb,
  359. 0xf8, 0xd8, 0xb0, 0xb4, 0xb8, 0xd8, 0xad, 0x84, 0xf2, 0xc0, 0xdf, 0xf1, 0x8f, 0xcb, 0xc3, 0xa8,
  360. /* bank # 7 */
  361. 0xb2, 0xb6, 0x86, 0x96, 0xc8, 0xc1, 0xcb, 0xc3, 0xf3, 0xb0, 0xb4, 0x88, 0x98, 0xa8, 0x21, 0xdb,
  362. 0x71, 0x8d, 0x9d, 0x71, 0x85, 0x95, 0x21, 0xd9, 0xad, 0xf2, 0xfa, 0xd8, 0x85, 0x97, 0xa8, 0x28,
  363. 0xd9, 0xf4, 0x08, 0xd8, 0xf2, 0x8d, 0x29, 0xda, 0xf4, 0x05, 0xd9, 0xf2, 0x85, 0xa4, 0xc2, 0xf2,
  364. 0xd8, 0xa8, 0x8d, 0x94, 0x01, 0xd1, 0xd9, 0xf4, 0x11, 0xf2, 0xd8, 0x87, 0x21, 0xd8, 0xf4, 0x0a,
  365. 0xd8, 0xf2, 0x84, 0x98, 0xa8, 0xc8, 0x01, 0xd1, 0xd9, 0xf4, 0x11, 0xd8, 0xf3, 0xa4, 0xc8, 0xbb,
  366. 0xaf, 0xd0, 0xf2, 0xde, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xd8, 0xf1, 0xb8, 0xf6,
  367. 0xb5, 0xb9, 0xb0, 0x8a, 0x95, 0xa3, 0xde, 0x3c, 0xa3, 0xd9, 0xf8, 0xd8, 0x5c, 0xa3, 0xd9, 0xf8,
  368. 0xd8, 0x7c, 0xa3, 0xd9, 0xf8, 0xd8, 0xf8, 0xf9, 0xd1, 0xa5, 0xd9, 0xdf, 0xda, 0xfa, 0xd8, 0xb1,
  369. 0x85, 0x30, 0xf7, 0xd9, 0xde, 0xd8, 0xf8, 0x30, 0xad, 0xda, 0xde, 0xd8, 0xf2, 0xb4, 0x8c, 0x99,
  370. 0xa3, 0x2d, 0x55, 0x7d, 0xa0, 0x83, 0xdf, 0xdf, 0xdf, 0xb5, 0x91, 0xa0, 0xf6, 0x29, 0xd9, 0xfb,
  371. 0xd8, 0xa0, 0xfc, 0x29, 0xd9, 0xfa, 0xd8, 0xa0, 0xd0, 0x51, 0xd9, 0xf8, 0xd8, 0xfc, 0x51, 0xd9,
  372. 0xf9, 0xd8, 0x79, 0xd9, 0xfb, 0xd8, 0xa0, 0xd0, 0xfc, 0x79, 0xd9, 0xfa, 0xd8, 0xa1, 0xf9, 0xf9,
  373. 0xf9, 0xf9, 0xf9, 0xa0, 0xda, 0xdf, 0xdf, 0xdf, 0xd8, 0xa1, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xac,
  374. 0xde, 0xf8, 0xad, 0xde, 0x83, 0x93, 0xac, 0x2c, 0x54, 0x7c, 0xf1, 0xa8, 0xdf, 0xdf, 0xdf, 0xf6,
  375. 0x9d, 0x2c, 0xda, 0xa0, 0xdf, 0xd9, 0xfa, 0xdb, 0x2d, 0xf8, 0xd8, 0xa8, 0x50, 0xda, 0xa0, 0xd0,
  376. 0xde, 0xd9, 0xd0, 0xf8, 0xf8, 0xf8, 0xdb, 0x55, 0xf8, 0xd8, 0xa8, 0x78, 0xda, 0xa0, 0xd0, 0xdf,
  377. /* bank # 8 */
  378. 0xd9, 0xd0, 0xfa, 0xf8, 0xf8, 0xf8, 0xf8, 0xdb, 0x7d, 0xf8, 0xd8, 0x9c, 0xa8, 0x8c, 0xf5, 0x30,
  379. 0xdb, 0x38, 0xd9, 0xd0, 0xde, 0xdf, 0xa0, 0xd0, 0xde, 0xdf, 0xd8, 0xa8, 0x48, 0xdb, 0x58, 0xd9,
  380. 0xdf, 0xd0, 0xde, 0xa0, 0xdf, 0xd0, 0xde, 0xd8, 0xa8, 0x68, 0xdb, 0x70, 0xd9, 0xdf, 0xdf, 0xa0,
  381. 0xdf, 0xdf, 0xd8, 0xf1, 0xa8, 0x88, 0x90, 0x2c, 0x54, 0x7c, 0x98, 0xa8, 0xd0, 0x5c, 0x38, 0xd1,
  382. 0xda, 0xf2, 0xae, 0x8c, 0xdf, 0xf9, 0xd8, 0xb0, 0x87, 0xa8, 0xc1, 0xc1, 0xb1, 0x88, 0xa8, 0xc6,
  383. 0xf9, 0xf9, 0xda, 0x36, 0xd8, 0xa8, 0xf9, 0xda, 0x36, 0xd8, 0xa8, 0xf9, 0xda, 0x36, 0xd8, 0xa8,
  384. 0xf9, 0xda, 0x36, 0xd8, 0xa8, 0xf9, 0xda, 0x36, 0xd8, 0xf7, 0x8d, 0x9d, 0xad, 0xf8, 0x18, 0xda,
  385. 0xf2, 0xae, 0xdf, 0xd8, 0xf7, 0xad, 0xfa, 0x30, 0xd9, 0xa4, 0xde, 0xf9, 0xd8, 0xf2, 0xae, 0xde,
  386. 0xfa, 0xf9, 0x83, 0xa7, 0xd9, 0xc3, 0xc5, 0xc7, 0xf1, 0x88, 0x9b, 0xa7, 0x7a, 0xad, 0xf7, 0xde,
  387. 0xdf, 0xa4, 0xf8, 0x84, 0x94, 0x08, 0xa7, 0x97, 0xf3, 0x00, 0xae, 0xf2, 0x98, 0x19, 0xa4, 0x88,
  388. 0xc6, 0xa3, 0x94, 0x88, 0xf6, 0x32, 0xdf, 0xf2, 0x83, 0x93, 0xdb, 0x09, 0xd9, 0xf2, 0xaa, 0xdf,
  389. 0xd8, 0xd8, 0xae, 0xf8, 0xf9, 0xd1, 0xda, 0xf3, 0xa4, 0xde, 0xa7, 0xf1, 0x88, 0x9b, 0x7a, 0xd8,
  390. 0xf3, 0x84, 0x94, 0xae, 0x19, 0xf9, 0xda, 0xaa, 0xf1, 0xdf, 0xd8, 0xa8, 0x81, 0xc0, 0xc3, 0xc5,
  391. 0xc7, 0xa3, 0x92, 0x83, 0xf6, 0x28, 0xad, 0xde, 0xd9, 0xf8, 0xd8, 0xa3, 0x50, 0xad, 0xd9, 0xf8,
  392. 0xd8, 0xa3, 0x78, 0xad, 0xd9, 0xf8, 0xd8, 0xf8, 0xf9, 0xd1, 0xa1, 0xda, 0xde, 0xc3, 0xc5, 0xc7,
  393. 0xd8, 0xa1, 0x81, 0x94, 0xf8, 0x18, 0xf2, 0xb0, 0x89, 0xac, 0xc3, 0xc5, 0xc7, 0xf1, 0xd8, 0xb8,
  394. /* bank # 9 */
  395. 0xb4, 0xb0, 0x97, 0x86, 0xa8, 0x31, 0x9b, 0x06, 0x99, 0x07, 0xab, 0x97, 0x28, 0x88, 0x9b, 0xf0,
  396. 0x0c, 0x20, 0x14, 0x40, 0xb0, 0xb4, 0xb8, 0xf0, 0xa8, 0x8a, 0x9a, 0x28, 0x50, 0x78, 0xb7, 0x9b,
  397. 0xa8, 0x29, 0x51, 0x79, 0x24, 0x70, 0x59, 0x44, 0x69, 0x38, 0x64, 0x48, 0x31, 0xf1, 0xbb, 0xab,
  398. 0x88, 0x00, 0x2c, 0x54, 0x7c, 0xf0, 0xb3, 0x8b, 0xb8, 0xa8, 0x04, 0x28, 0x50, 0x78, 0xf1, 0xb0,
  399. 0x88, 0xb4, 0x97, 0x26, 0xa8, 0x59, 0x98, 0xbb, 0xab, 0xb3, 0x8b, 0x02, 0x26, 0x46, 0x66, 0xb0,
  400. 0xb8, 0xf0, 0x8a, 0x9c, 0xa8, 0x29, 0x51, 0x79, 0x8b, 0x29, 0x51, 0x79, 0x8a, 0x24, 0x70, 0x59,
  401. 0x8b, 0x20, 0x58, 0x71, 0x8a, 0x44, 0x69, 0x38, 0x8b, 0x39, 0x40, 0x68, 0x8a, 0x64, 0x48, 0x31,
  402. 0x8b, 0x30, 0x49, 0x60, 0x88, 0xf1, 0xac, 0x00, 0x2c, 0x54, 0x7c, 0xf0, 0x8c, 0xa8, 0x04, 0x28,
  403. 0x50, 0x78, 0xf1, 0x88, 0x97, 0x26, 0xa8, 0x59, 0x98, 0xac, 0x8c, 0x02, 0x26, 0x46, 0x66, 0xf0,
  404. 0x89, 0x9c, 0xa8, 0x29, 0x51, 0x79, 0x24, 0x70, 0x59, 0x44, 0x69, 0x38, 0x64, 0x48, 0x31, 0xa9,
  405. 0x88, 0x09, 0x20, 0x59, 0x70, 0xab, 0x11, 0x38, 0x40, 0x69, 0xa8, 0x19, 0x31, 0x48, 0x60, 0x8c,
  406. 0xa8, 0x3c, 0x41, 0x5c, 0x20, 0x7c, 0x00, 0xf1, 0x87, 0x98, 0x19, 0x86, 0xa8, 0x6e, 0x76, 0x7e,
  407. 0xa9, 0x99, 0x88, 0x2d, 0x55, 0x7d, 0xd8, 0xb1, 0xb5, 0xb9, 0xa3, 0xdf, 0xdf, 0xdf, 0xae, 0xd0,
  408. 0xdf, 0xaa, 0xd0, 0xde, 0xf2, 0xab, 0xf8, 0xf9, 0xd9, 0xb0, 0x87, 0xc4, 0xaa, 0xf1, 0xdf, 0xdf,
  409. 0xbb, 0xaf, 0xdf, 0xdf, 0xb9, 0xd8, 0xb1, 0xf1, 0xa3, 0x97, 0x8e, 0x60, 0xdf, 0xb0, 0x84, 0xf2,
  410. 0xc8, 0xf8, 0xf9, 0xd9, 0xde, 0xd8, 0x93, 0x85, 0xf1, 0x4a, 0xb1, 0x83, 0xa3, 0x08, 0xb5, 0x83,
  411. /* bank # 10 */
  412. 0x9a, 0x08, 0x10, 0xb7, 0x9f, 0x10, 0xd8, 0xf1, 0xb0, 0xba, 0xae, 0xb0, 0x8a, 0xc2, 0xb2, 0xb6,
  413. 0x8e, 0x9e, 0xf1, 0xfb, 0xd9, 0xf4, 0x1d, 0xd8, 0xf9, 0xd9, 0x0c, 0xf1, 0xd8, 0xf8, 0xf8, 0xad,
  414. 0x61, 0xd9, 0xae, 0xfb, 0xd8, 0xf4, 0x0c, 0xf1, 0xd8, 0xf8, 0xf8, 0xad, 0x19, 0xd9, 0xae, 0xfb,
  415. 0xdf, 0xd8, 0xf4, 0x16, 0xf1, 0xd8, 0xf8, 0xad, 0x8d, 0x61, 0xd9, 0xf4, 0xf4, 0xac, 0xf5, 0x9c,
  416. 0x9c, 0x8d, 0xdf, 0x2b, 0xba, 0xb6, 0xae, 0xfa, 0xf8, 0xf4, 0x0b, 0xd8, 0xf1, 0xae, 0xd0, 0xf8,
  417. 0xad, 0x51, 0xda, 0xae, 0xfa, 0xf8, 0xf1, 0xd8, 0xb9, 0xb1, 0xb6, 0xa3, 0x83, 0x9c, 0x08, 0xb9,
  418. 0xb1, 0x83, 0x9a, 0xb5, 0xaa, 0xc0, 0xfd, 0x30, 0x83, 0xb7, 0x9f, 0x10, 0xb5, 0x8b, 0x93, 0xf2,
  419. 0x02, 0x02, 0xd1, 0xab, 0xda, 0xde, 0xd8, 0xf1, 0xb0, 0x80, 0xba, 0xab, 0xc0, 0xc3, 0xb2, 0x84,
  420. 0xc1, 0xc3, 0xd8, 0xb1, 0xb9, 0xf3, 0x8b, 0xa3, 0x91, 0xb6, 0x09, 0xb4, 0xd9, 0xab, 0xde, 0xb0,
  421. 0x87, 0x9c, 0xb9, 0xa3, 0xdd, 0xf1, 0xb3, 0x8b, 0x8b, 0x8b, 0x8b, 0x8b, 0xb0, 0x87, 0xa3, 0xa3,
  422. 0xa3, 0xa3, 0xb2, 0x8b, 0xb6, 0x9b, 0xf2, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
  423. 0xa3, 0xf1, 0xb0, 0x87, 0xb5, 0x9a, 0xa3, 0xf3, 0x9b, 0xa3, 0xa3, 0xdc, 0xba, 0xac, 0xdf, 0xb9,
  424. 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
  425. 0xd8, 0xd8, 0xd8, 0xbb, 0xb3, 0xb7, 0xf1, 0xaa, 0xf9, 0xda, 0xff, 0xd9, 0x80, 0x9a, 0xaa, 0x28,
  426. 0xb4, 0x80, 0x98, 0xa7, 0x20, 0xb7, 0x97, 0x87, 0xa8, 0x66, 0x88, 0xf0, 0x79, 0x51, 0xf1, 0x90,
  427. 0x2c, 0x87, 0x0c, 0xa7, 0x81, 0x97, 0x62, 0x93, 0xf0, 0x71, 0x71, 0x60, 0x85, 0x94, 0x01, 0x29,
  428. /* bank # 11 */
  429. 0x51, 0x79, 0x90, 0xa5, 0xf1, 0x28, 0x4c, 0x6c, 0x87, 0x0c, 0x95, 0x18, 0x85, 0x78, 0xa3, 0x83,
  430. 0x90, 0x28, 0x4c, 0x6c, 0x88, 0x6c, 0xd8, 0xf3, 0xa2, 0x82, 0x00, 0xf2, 0x10, 0xa8, 0x92, 0x19,
  431. 0x80, 0xa2, 0xf2, 0xd9, 0x26, 0xd8, 0xf1, 0x88, 0xa8, 0x4d, 0xd9, 0x48, 0xd8, 0x96, 0xa8, 0x39,
  432. 0x80, 0xd9, 0x3c, 0xd8, 0x95, 0x80, 0xa8, 0x39, 0xa6, 0x86, 0x98, 0xd9, 0x2c, 0xda, 0x87, 0xa7,
  433. 0x2c, 0xd8, 0xa8, 0x89, 0x95, 0x19, 0xa9, 0x80, 0xd9, 0x38, 0xd8, 0xa8, 0x89, 0x39, 0xa9, 0x80,
  434. 0xda, 0x3c, 0xd8, 0xa8, 0x2e, 0xa8, 0x39, 0x90, 0xd9, 0x0c, 0xd8, 0xa8, 0x95, 0x31, 0x98, 0xd9,
  435. 0x0c, 0xd8, 0xa8, 0x09, 0xd9, 0xff, 0xd8, 0x01, 0xda, 0xff, 0xd8, 0x95, 0x39, 0xa9, 0xda, 0x26,
  436. 0xff, 0xd8, 0x90, 0xa8, 0x0d, 0x89, 0x99, 0xa8, 0x10, 0x80, 0x98, 0x21, 0xda, 0x2e, 0xd8, 0x89,
  437. 0x99, 0xa8, 0x31, 0x80, 0xda, 0x2e, 0xd8, 0xa8, 0x86, 0x96, 0x31, 0x80, 0xda, 0x2e, 0xd8, 0xa8,
  438. 0x87, 0x31, 0x80, 0xda, 0x2e, 0xd8, 0xa8, 0x82, 0x92, 0xf3, 0x41, 0x80, 0xf1, 0xd9, 0x2e, 0xd8,
  439. 0xa8, 0x82, 0xf3, 0x19, 0x80, 0xf1, 0xd9, 0x2e, 0xd8, 0x82, 0xac, 0xf3, 0xc0, 0xa2, 0x80, 0x22,
  440. 0xf1, 0xa6, 0x2e, 0xa7, 0x2e, 0xa9, 0x22, 0x98, 0xa8, 0x29, 0xda, 0xac, 0xde, 0xff, 0xd8, 0xa2,
  441. 0xf2, 0x2a, 0xf1, 0xa9, 0x2e, 0x82, 0x92, 0xa8, 0xf2, 0x31, 0x80, 0xa6, 0x96, 0xf1, 0xd9, 0x00,
  442. 0xac, 0x8c, 0x9c, 0x0c, 0x30, 0xac, 0xde, 0xd0, 0xde, 0xff, 0xd8, 0x8c, 0x9c, 0xac, 0xd0, 0x10,
  443. 0xac, 0xde, 0x80, 0x92, 0xa2, 0xf2, 0x4c, 0x82, 0xa8, 0xf1, 0xca, 0xf2, 0x35, 0xf1, 0x96, 0x88,
  444. 0xa6, 0xd9, 0x00, 0xd8, 0xf1, 0xff
  445. };
  446. static const unsigned short sStartAddress = 0x0400;
  447. /* END OF SECTION COPIED FROM dmpDefaultMPU6050.c */
  448. #define INT_SRC_TAP (0x01)
  449. #define INT_SRC_ANDROID_ORIENT (0x08)
  450. #define DMP_FEATURE_SEND_ANY_GYRO (DMP_FEATURE_SEND_RAW_GYRO | \
  451. DMP_FEATURE_SEND_CAL_GYRO)
  452. #define MAX_PACKET_LENGTH (32)
  453. #define DMP_SAMPLE_RATE (200)
  454. #define GYRO_SF (46850825LL * 200 / DMP_SAMPLE_RATE)
  455. #define FIFO_CORRUPTION_CHECK
  456. #ifdef FIFO_CORRUPTION_CHECK
  457. #define QUAT_ERROR_THRESH (1L<<24)
  458. #define QUAT_MAG_SQ_NORMALIZED (1L<<28)
  459. #define QUAT_MAG_SQ_MIN (QUAT_MAG_SQ_NORMALIZED - QUAT_ERROR_THRESH)
  460. #define QUAT_MAG_SQ_MAX (QUAT_MAG_SQ_NORMALIZED + QUAT_ERROR_THRESH)
  461. #endif
  462. struct dmp_s {
  463. void (*tap_cb)(unsigned char count, unsigned char direction);
  464. void (*android_orient_cb)(unsigned char orientation);
  465. unsigned short orient;
  466. unsigned short feature_mask;
  467. unsigned short fifo_rate;
  468. unsigned char packet_length;
  469. };
  470. static struct dmp_s dmp = {
  471. .tap_cb = NULL,
  472. .android_orient_cb = NULL,
  473. .orient = 0,
  474. .feature_mask = 0,
  475. .fifo_rate = 0,
  476. .packet_length = 0
  477. };
  478. /**
  479. * @brief Load the DMP with this image.
  480. * @return 0 if successful.
  481. */
  482. int dmp_load_motion_driver_firmware(void)
  483. {
  484. return mpu_load_firmware(DMP_CODE_SIZE, dmp_memory, sStartAddress,
  485. DMP_SAMPLE_RATE);
  486. }
  487. /**
  488. * @brief Push gyro and accel orientation to the DMP.
  489. * The orientation is represented here as the output of
  490. * @e inv_orientation_matrix_to_scalar.
  491. * @param[in] orient Gyro and accel orientation in body frame.
  492. * @return 0 if successful.
  493. */
  494. int dmp_set_orientation(unsigned short orient)
  495. {
  496. unsigned char gyro_regs[3], accel_regs[3];
  497. const unsigned char gyro_axes[3] = {DINA4C, DINACD, DINA6C};
  498. const unsigned char accel_axes[3] = {DINA0C, DINAC9, DINA2C};
  499. const unsigned char gyro_sign[3] = {DINA36, DINA56, DINA76};
  500. const unsigned char accel_sign[3] = {DINA26, DINA46, DINA66};
  501. gyro_regs[0] = gyro_axes[orient & 3];
  502. gyro_regs[1] = gyro_axes[(orient >> 3) & 3];
  503. gyro_regs[2] = gyro_axes[(orient >> 6) & 3];
  504. accel_regs[0] = accel_axes[orient & 3];
  505. accel_regs[1] = accel_axes[(orient >> 3) & 3];
  506. accel_regs[2] = accel_axes[(orient >> 6) & 3];
  507. /* Chip-to-body, axes only. */
  508. if (mpu_write_mem(FCFG_1, 3, gyro_regs))
  509. return -1;
  510. if (mpu_write_mem(FCFG_2, 3, accel_regs))
  511. return -1;
  512. memcpy(gyro_regs, gyro_sign, 3);
  513. memcpy(accel_regs, accel_sign, 3);
  514. if (orient & 4) {
  515. gyro_regs[0] |= 1;
  516. accel_regs[0] |= 1;
  517. }
  518. if (orient & 0x20) {
  519. gyro_regs[1] |= 1;
  520. accel_regs[1] |= 1;
  521. }
  522. if (orient & 0x100) {
  523. gyro_regs[2] |= 1;
  524. accel_regs[2] |= 1;
  525. }
  526. /* Chip-to-body, sign only. */
  527. if (mpu_write_mem(FCFG_3, 3, gyro_regs))
  528. return -1;
  529. if (mpu_write_mem(FCFG_7, 3, accel_regs))
  530. return -1;
  531. dmp.orient = orient;
  532. return 0;
  533. }
  534. /**
  535. * @brief Push gyro biases to the DMP.
  536. * Because the gyro integration is handled in the DMP, any gyro biases
  537. * calculated by the MPL should be pushed down to DMP memory to remove
  538. * 3-axis quaternion drift.
  539. * \n NOTE: If the DMP-based gyro calibration is enabled, the DMP will
  540. * overwrite the biases written to this location once a new one is computed.
  541. * @param[in] bias Gyro biases in q16.
  542. * @return 0 if successful.
  543. */
  544. int dmp_set_gyro_bias(long *bias)
  545. {
  546. long gyro_bias_body[3];
  547. unsigned char regs[4];
  548. gyro_bias_body[0] = bias[dmp.orient & 3];
  549. if (dmp.orient & 4)
  550. gyro_bias_body[0] *= -1;
  551. gyro_bias_body[1] = bias[(dmp.orient >> 3) & 3];
  552. if (dmp.orient & 0x20)
  553. gyro_bias_body[1] *= -1;
  554. gyro_bias_body[2] = bias[(dmp.orient >> 6) & 3];
  555. if (dmp.orient & 0x100)
  556. gyro_bias_body[2] *= -1;
  557. #ifdef EMPL_NO_64BIT
  558. gyro_bias_body[0] = (long)(((float)gyro_bias_body[0] * GYRO_SF) / 1073741824.f);
  559. gyro_bias_body[1] = (long)(((float)gyro_bias_body[1] * GYRO_SF) / 1073741824.f);
  560. gyro_bias_body[2] = (long)(((float)gyro_bias_body[2] * GYRO_SF) / 1073741824.f);
  561. #else
  562. gyro_bias_body[0] = (long)(((long long)gyro_bias_body[0] * GYRO_SF) >> 30);
  563. gyro_bias_body[1] = (long)(((long long)gyro_bias_body[1] * GYRO_SF) >> 30);
  564. gyro_bias_body[2] = (long)(((long long)gyro_bias_body[2] * GYRO_SF) >> 30);
  565. #endif
  566. regs[0] = (unsigned char)((gyro_bias_body[0] >> 24) & 0xFF);
  567. regs[1] = (unsigned char)((gyro_bias_body[0] >> 16) & 0xFF);
  568. regs[2] = (unsigned char)((gyro_bias_body[0] >> 8) & 0xFF);
  569. regs[3] = (unsigned char)(gyro_bias_body[0] & 0xFF);
  570. if (mpu_write_mem(D_EXT_GYRO_BIAS_X, 4, regs))
  571. return -1;
  572. regs[0] = (unsigned char)((gyro_bias_body[1] >> 24) & 0xFF);
  573. regs[1] = (unsigned char)((gyro_bias_body[1] >> 16) & 0xFF);
  574. regs[2] = (unsigned char)((gyro_bias_body[1] >> 8) & 0xFF);
  575. regs[3] = (unsigned char)(gyro_bias_body[1] & 0xFF);
  576. if (mpu_write_mem(D_EXT_GYRO_BIAS_Y, 4, regs))
  577. return -1;
  578. regs[0] = (unsigned char)((gyro_bias_body[2] >> 24) & 0xFF);
  579. regs[1] = (unsigned char)((gyro_bias_body[2] >> 16) & 0xFF);
  580. regs[2] = (unsigned char)((gyro_bias_body[2] >> 8) & 0xFF);
  581. regs[3] = (unsigned char)(gyro_bias_body[2] & 0xFF);
  582. return mpu_write_mem(D_EXT_GYRO_BIAS_Z, 4, regs);
  583. }
  584. /**
  585. * @brief Push accel biases to the DMP.
  586. * These biases will be removed from the DMP 6-axis quaternion.
  587. * @param[in] bias Accel biases in q16.
  588. * @return 0 if successful.
  589. */
  590. int dmp_set_accel_bias(long *bias)
  591. {
  592. long accel_bias_body[3];
  593. unsigned char regs[12];
  594. long long accel_sf;
  595. unsigned short accel_sens;
  596. mpu_get_accel_sens(&accel_sens);
  597. accel_sf = (long long)accel_sens << 15;
  598. // __no_operation();
  599. accel_bias_body[0] = bias[dmp.orient & 3];
  600. if (dmp.orient & 4)
  601. accel_bias_body[0] *= -1;
  602. accel_bias_body[1] = bias[(dmp.orient >> 3) & 3];
  603. if (dmp.orient & 0x20)
  604. accel_bias_body[1] *= -1;
  605. accel_bias_body[2] = bias[(dmp.orient >> 6) & 3];
  606. if (dmp.orient & 0x100)
  607. accel_bias_body[2] *= -1;
  608. #ifdef EMPL_NO_64BIT
  609. accel_bias_body[0] = (long)(((float)accel_bias_body[0] * accel_sf) / 1073741824.f);
  610. accel_bias_body[1] = (long)(((float)accel_bias_body[1] * accel_sf) / 1073741824.f);
  611. accel_bias_body[2] = (long)(((float)accel_bias_body[2] * accel_sf) / 1073741824.f);
  612. #else
  613. accel_bias_body[0] = (long)(((long long)accel_bias_body[0] * accel_sf) >> 30);
  614. accel_bias_body[1] = (long)(((long long)accel_bias_body[1] * accel_sf) >> 30);
  615. accel_bias_body[2] = (long)(((long long)accel_bias_body[2] * accel_sf) >> 30);
  616. #endif
  617. regs[0] = (unsigned char)((accel_bias_body[0] >> 24) & 0xFF);
  618. regs[1] = (unsigned char)((accel_bias_body[0] >> 16) & 0xFF);
  619. regs[2] = (unsigned char)((accel_bias_body[0] >> 8) & 0xFF);
  620. regs[3] = (unsigned char)(accel_bias_body[0] & 0xFF);
  621. regs[4] = (unsigned char)((accel_bias_body[1] >> 24) & 0xFF);
  622. regs[5] = (unsigned char)((accel_bias_body[1] >> 16) & 0xFF);
  623. regs[6] = (unsigned char)((accel_bias_body[1] >> 8) & 0xFF);
  624. regs[7] = (unsigned char)(accel_bias_body[1] & 0xFF);
  625. regs[8] = (unsigned char)((accel_bias_body[2] >> 24) & 0xFF);
  626. regs[9] = (unsigned char)((accel_bias_body[2] >> 16) & 0xFF);
  627. regs[10] = (unsigned char)((accel_bias_body[2] >> 8) & 0xFF);
  628. regs[11] = (unsigned char)(accel_bias_body[2] & 0xFF);
  629. return mpu_write_mem(D_ACCEL_BIAS, 12, regs);
  630. }
  631. /**
  632. * @brief Set DMP output rate.
  633. * Only used when DMP is on.
  634. * @param[in] rate Desired fifo rate (Hz).
  635. * @return 0 if successful.
  636. */
  637. int dmp_set_fifo_rate(unsigned short rate)
  638. {
  639. const unsigned char regs_end[12] = {DINAFE, DINAF2, DINAAB,
  640. 0xc4, DINAAA, DINAF1, DINADF, DINADF, 0xBB, 0xAF, DINADF, DINADF};
  641. unsigned short div;
  642. unsigned char tmp[8];
  643. if (rate > DMP_SAMPLE_RATE)
  644. return -1;
  645. div = DMP_SAMPLE_RATE / rate - 1;
  646. tmp[0] = (unsigned char)((div >> 8) & 0xFF);
  647. tmp[1] = (unsigned char)(div & 0xFF);
  648. if (mpu_write_mem(D_0_22, 2, tmp))
  649. return -1;
  650. if (mpu_write_mem(CFG_6, 12, (unsigned char*)regs_end))
  651. return -1;
  652. dmp.fifo_rate = rate;
  653. return 0;
  654. }
  655. /**
  656. * @brief Get DMP output rate.
  657. * @param[out] rate Current fifo rate (Hz).
  658. * @return 0 if successful.
  659. */
  660. int dmp_get_fifo_rate(unsigned short *rate)
  661. {
  662. rate[0] = dmp.fifo_rate;
  663. return 0;
  664. }
  665. /**
  666. * @brief Set tap threshold for a specific axis.
  667. * @param[in] axis 1, 2, and 4 for XYZ accel, respectively.
  668. * @param[in] thresh Tap threshold, in mg/ms.
  669. * @return 0 if successful.
  670. */
  671. int dmp_set_tap_thresh(unsigned char axis, unsigned short thresh)
  672. {
  673. unsigned char tmp[4], accel_fsr;
  674. float scaled_thresh;
  675. unsigned short dmp_thresh, dmp_thresh_2;
  676. if (!(axis & TAP_XYZ) || thresh > 1600)
  677. return -1;
  678. scaled_thresh = (float)thresh / DMP_SAMPLE_RATE;
  679. mpu_get_accel_fsr(&accel_fsr);
  680. switch (accel_fsr) {
  681. case 2:
  682. dmp_thresh = (unsigned short)(scaled_thresh * 16384);
  683. /* dmp_thresh * 0.75 */
  684. dmp_thresh_2 = (unsigned short)(scaled_thresh * 12288);
  685. break;
  686. case 4:
  687. dmp_thresh = (unsigned short)(scaled_thresh * 8192);
  688. /* dmp_thresh * 0.75 */
  689. dmp_thresh_2 = (unsigned short)(scaled_thresh * 6144);
  690. break;
  691. case 8:
  692. dmp_thresh = (unsigned short)(scaled_thresh * 4096);
  693. /* dmp_thresh * 0.75 */
  694. dmp_thresh_2 = (unsigned short)(scaled_thresh * 3072);
  695. break;
  696. case 16:
  697. dmp_thresh = (unsigned short)(scaled_thresh * 2048);
  698. /* dmp_thresh * 0.75 */
  699. dmp_thresh_2 = (unsigned short)(scaled_thresh * 1536);
  700. break;
  701. default:
  702. return -1;
  703. }
  704. tmp[0] = (unsigned char)(dmp_thresh >> 8);
  705. tmp[1] = (unsigned char)(dmp_thresh & 0xFF);
  706. tmp[2] = (unsigned char)(dmp_thresh_2 >> 8);
  707. tmp[3] = (unsigned char)(dmp_thresh_2 & 0xFF);
  708. if (axis & TAP_X) {
  709. if (mpu_write_mem(DMP_TAP_THX, 2, tmp))
  710. return -1;
  711. if (mpu_write_mem(D_1_36, 2, tmp+2))
  712. return -1;
  713. }
  714. if (axis & TAP_Y) {
  715. if (mpu_write_mem(DMP_TAP_THY, 2, tmp))
  716. return -1;
  717. if (mpu_write_mem(D_1_40, 2, tmp+2))
  718. return -1;
  719. }
  720. if (axis & TAP_Z) {
  721. if (mpu_write_mem(DMP_TAP_THZ, 2, tmp))
  722. return -1;
  723. if (mpu_write_mem(D_1_44, 2, tmp+2))
  724. return -1;
  725. }
  726. return 0;
  727. }
  728. /**
  729. * @brief Set which axes will register a tap.
  730. * @param[in] axis 1, 2, and 4 for XYZ, respectively.
  731. * @return 0 if successful.
  732. */
  733. int dmp_set_tap_axes(unsigned char axis)
  734. {
  735. unsigned char tmp = 0;
  736. if (axis & TAP_X)
  737. tmp |= 0x30;
  738. if (axis & TAP_Y)
  739. tmp |= 0x0C;
  740. if (axis & TAP_Z)
  741. tmp |= 0x03;
  742. return mpu_write_mem(D_1_72, 1, &tmp);
  743. }
  744. /**
  745. * @brief Set minimum number of taps needed for an interrupt.
  746. * @param[in] min_taps Minimum consecutive taps (1-4).
  747. * @return 0 if successful.
  748. */
  749. int dmp_set_tap_count(unsigned char min_taps)
  750. {
  751. unsigned char tmp;
  752. if (min_taps < 1)
  753. min_taps = 1;
  754. else if (min_taps > 4)
  755. min_taps = 4;
  756. tmp = min_taps - 1;
  757. return mpu_write_mem(D_1_79, 1, &tmp);
  758. }
  759. /**
  760. * @brief Set length between valid taps.
  761. * @param[in] time Milliseconds between taps.
  762. * @return 0 if successful.
  763. */
  764. int dmp_set_tap_time(unsigned short time)
  765. {
  766. unsigned short dmp_time;
  767. unsigned char tmp[2];
  768. dmp_time = time / (1000 / DMP_SAMPLE_RATE);
  769. tmp[0] = (unsigned char)(dmp_time >> 8);
  770. tmp[1] = (unsigned char)(dmp_time & 0xFF);
  771. return mpu_write_mem(DMP_TAPW_MIN, 2, tmp);
  772. }
  773. /**
  774. * @brief Set max time between taps to register as a multi-tap.
  775. * @param[in] time Max milliseconds between taps.
  776. * @return 0 if successful.
  777. */
  778. int dmp_set_tap_time_multi(unsigned short time)
  779. {
  780. unsigned short dmp_time;
  781. unsigned char tmp[2];
  782. dmp_time = time / (1000 / DMP_SAMPLE_RATE);
  783. tmp[0] = (unsigned char)(dmp_time >> 8);
  784. tmp[1] = (unsigned char)(dmp_time & 0xFF);
  785. return mpu_write_mem(D_1_218, 2, tmp);
  786. }
  787. /**
  788. * @brief Set shake rejection threshold.
  789. * If the DMP detects a gyro sample larger than @e thresh, taps are rejected.
  790. * @param[in] sf Gyro scale factor.
  791. * @param[in] thresh Gyro threshold in dps.
  792. * @return 0 if successful.
  793. */
  794. int dmp_set_shake_reject_thresh(long sf, unsigned short thresh)
  795. {
  796. unsigned char tmp[4];
  797. long thresh_scaled = sf / 1000 * thresh;
  798. tmp[0] = (unsigned char)(((long)thresh_scaled >> 24) & 0xFF);
  799. tmp[1] = (unsigned char)(((long)thresh_scaled >> 16) & 0xFF);
  800. tmp[2] = (unsigned char)(((long)thresh_scaled >> 8) & 0xFF);
  801. tmp[3] = (unsigned char)((long)thresh_scaled & 0xFF);
  802. return mpu_write_mem(D_1_92, 4, tmp);
  803. }
  804. /**
  805. * @brief Set shake rejection time.
  806. * Sets the length of time that the gyro must be outside of the threshold set
  807. * by @e gyro_set_shake_reject_thresh before taps are rejected. A mandatory
  808. * 60 ms is added to this parameter.
  809. * @param[in] time Time in milliseconds.
  810. * @return 0 if successful.
  811. */
  812. int dmp_set_shake_reject_time(unsigned short time)
  813. {
  814. unsigned char tmp[2];
  815. time /= (1000 / DMP_SAMPLE_RATE);
  816. tmp[0] = time >> 8;
  817. tmp[1] = time & 0xFF;
  818. return mpu_write_mem(D_1_90,2,tmp);
  819. }
  820. /**
  821. * @brief Set shake rejection timeout.
  822. * Sets the length of time after a shake rejection that the gyro must stay
  823. * inside of the threshold before taps can be detected again. A mandatory
  824. * 60 ms is added to this parameter.
  825. * @param[in] time Time in milliseconds.
  826. * @return 0 if successful.
  827. */
  828. int dmp_set_shake_reject_timeout(unsigned short time)
  829. {
  830. unsigned char tmp[2];
  831. time /= (1000 / DMP_SAMPLE_RATE);
  832. tmp[0] = time >> 8;
  833. tmp[1] = time & 0xFF;
  834. return mpu_write_mem(D_1_88,2,tmp);
  835. }
  836. /**
  837. * @brief Get current step count.
  838. * @param[out] count Number of steps detected.
  839. * @return 0 if successful.
  840. */
  841. int dmp_get_pedometer_step_count(unsigned long *count)
  842. {
  843. unsigned char tmp[4];
  844. if (!count)
  845. return -1;
  846. if (mpu_read_mem(D_PEDSTD_STEPCTR, 4, tmp))
  847. return -1;
  848. count[0] = ((unsigned long)tmp[0] << 24) | ((unsigned long)tmp[1] << 16) |
  849. ((unsigned long)tmp[2] << 8) | tmp[3];
  850. return 0;
  851. }
  852. /**
  853. * @brief Overwrite current step count.
  854. * WARNING: This function writes to DMP memory and could potentially encounter
  855. * a race condition if called while the pedometer is enabled.
  856. * @param[in] count New step count.
  857. * @return 0 if successful.
  858. */
  859. int dmp_set_pedometer_step_count(unsigned long count)
  860. {
  861. unsigned char tmp[4];
  862. tmp[0] = (unsigned char)((count >> 24) & 0xFF);
  863. tmp[1] = (unsigned char)((count >> 16) & 0xFF);
  864. tmp[2] = (unsigned char)((count >> 8) & 0xFF);
  865. tmp[3] = (unsigned char)(count & 0xFF);
  866. return mpu_write_mem(D_PEDSTD_STEPCTR, 4, tmp);
  867. }
  868. /**
  869. * @brief Get duration of walking time.
  870. * @param[in] time Walk time in milliseconds.
  871. * @return 0 if successful.
  872. */
  873. int dmp_get_pedometer_walk_time(unsigned long *time)
  874. {
  875. unsigned char tmp[4];
  876. if (!time)
  877. return -1;
  878. if (mpu_read_mem(D_PEDSTD_TIMECTR, 4, tmp))
  879. return -1;
  880. time[0] = (((unsigned long)tmp[0] << 24) | ((unsigned long)tmp[1] << 16) |
  881. ((unsigned long)tmp[2] << 8) | tmp[3]) * 20;
  882. return 0;
  883. }
  884. /**
  885. * @brief Overwrite current walk time.
  886. * WARNING: This function writes to DMP memory and could potentially encounter
  887. * a race condition if called while the pedometer is enabled.
  888. * @param[in] time New walk time in milliseconds.
  889. */
  890. int dmp_set_pedometer_walk_time(unsigned long time)
  891. {
  892. unsigned char tmp[4];
  893. time /= 20;
  894. tmp[0] = (unsigned char)((time >> 24) & 0xFF);
  895. tmp[1] = (unsigned char)((time >> 16) & 0xFF);
  896. tmp[2] = (unsigned char)((time >> 8) & 0xFF);
  897. tmp[3] = (unsigned char)(time & 0xFF);
  898. return mpu_write_mem(D_PEDSTD_TIMECTR, 4, tmp);
  899. }
  900. /**
  901. * @brief Enable DMP features.
  902. * The following \#define's are used in the input mask:
  903. * \n DMP_FEATURE_TAP
  904. * \n DMP_FEATURE_ANDROID_ORIENT
  905. * \n DMP_FEATURE_LP_QUAT
  906. * \n DMP_FEATURE_6X_LP_QUAT
  907. * \n DMP_FEATURE_GYRO_CAL
  908. * \n DMP_FEATURE_SEND_RAW_ACCEL
  909. * \n DMP_FEATURE_SEND_RAW_GYRO
  910. * \n NOTE: DMP_FEATURE_LP_QUAT and DMP_FEATURE_6X_LP_QUAT are mutually
  911. * exclusive.
  912. * \n NOTE: DMP_FEATURE_SEND_RAW_GYRO and DMP_FEATURE_SEND_CAL_GYRO are also
  913. * mutually exclusive.
  914. * @param[in] mask Mask of features to enable.
  915. * @return 0 if successful.
  916. */
  917. int dmp_enable_feature(unsigned short mask)
  918. {
  919. unsigned char tmp[10];
  920. /* TODO: All of these settings can probably be integrated into the default
  921. * DMP image.
  922. */
  923. /* Set integration scale factor. */
  924. tmp[0] = (unsigned char)((GYRO_SF >> 24) & 0xFF);
  925. tmp[1] = (unsigned char)((GYRO_SF >> 16) & 0xFF);
  926. tmp[2] = (unsigned char)((GYRO_SF >> 8) & 0xFF);
  927. tmp[3] = (unsigned char)(GYRO_SF & 0xFF);
  928. mpu_write_mem(D_0_104, 4, tmp);
  929. /* Send sensor data to the FIFO. */
  930. tmp[0] = 0xA3;
  931. if (mask & DMP_FEATURE_SEND_RAW_ACCEL) {
  932. tmp[1] = 0xC0;
  933. tmp[2] = 0xC8;
  934. tmp[3] = 0xC2;
  935. } else {
  936. tmp[1] = 0xA3;
  937. tmp[2] = 0xA3;
  938. tmp[3] = 0xA3;
  939. }
  940. if (mask & DMP_FEATURE_SEND_ANY_GYRO) {
  941. tmp[4] = 0xC4;
  942. tmp[5] = 0xCC;
  943. tmp[6] = 0xC6;
  944. } else {
  945. tmp[4] = 0xA3;
  946. tmp[5] = 0xA3;
  947. tmp[6] = 0xA3;
  948. }
  949. tmp[7] = 0xA3;
  950. tmp[8] = 0xA3;
  951. tmp[9] = 0xA3;
  952. mpu_write_mem(CFG_15,10,tmp);
  953. /* Send gesture data to the FIFO. */
  954. if (mask & (DMP_FEATURE_TAP | DMP_FEATURE_ANDROID_ORIENT))
  955. tmp[0] = DINA20;
  956. else
  957. tmp[0] = 0xD8;
  958. mpu_write_mem(CFG_27,1,tmp);
  959. if (mask & DMP_FEATURE_GYRO_CAL)
  960. dmp_enable_gyro_cal(1);
  961. else
  962. dmp_enable_gyro_cal(0);
  963. if (mask & DMP_FEATURE_SEND_ANY_GYRO) {
  964. if (mask & DMP_FEATURE_SEND_CAL_GYRO) {
  965. tmp[0] = 0xB2;
  966. tmp[1] = 0x8B;
  967. tmp[2] = 0xB6;
  968. tmp[3] = 0x9B;
  969. } else {
  970. tmp[0] = DINAC0;
  971. tmp[1] = DINA80;
  972. tmp[2] = DINAC2;
  973. tmp[3] = DINA90;
  974. }
  975. mpu_write_mem(CFG_GYRO_RAW_DATA, 4, tmp);
  976. }
  977. if (mask & DMP_FEATURE_TAP) {
  978. /* Enable tap. */
  979. tmp[0] = 0xF8;
  980. mpu_write_mem(CFG_20, 1, tmp);
  981. dmp_set_tap_thresh(TAP_XYZ, 250);
  982. dmp_set_tap_axes(TAP_XYZ);
  983. dmp_set_tap_count(1);
  984. dmp_set_tap_time(100);
  985. dmp_set_tap_time_multi(500);
  986. dmp_set_shake_reject_thresh(GYRO_SF, 200);
  987. dmp_set_shake_reject_time(40);
  988. dmp_set_shake_reject_timeout(10);
  989. } else {
  990. tmp[0] = 0xD8;
  991. mpu_write_mem(CFG_20, 1, tmp);
  992. }
  993. if (mask & DMP_FEATURE_ANDROID_ORIENT) {
  994. tmp[0] = 0xD9;
  995. } else
  996. tmp[0] = 0xD8;
  997. mpu_write_mem(CFG_ANDROID_ORIENT_INT, 1, tmp);
  998. if (mask & DMP_FEATURE_LP_QUAT)
  999. dmp_enable_lp_quat(1);
  1000. else
  1001. dmp_enable_lp_quat(0);
  1002. if (mask & DMP_FEATURE_6X_LP_QUAT)
  1003. dmp_enable_6x_lp_quat(1);
  1004. else
  1005. dmp_enable_6x_lp_quat(0);
  1006. /* Pedometer is always enabled. */
  1007. dmp.feature_mask = mask | DMP_FEATURE_PEDOMETER;
  1008. mpu_reset_fifo();
  1009. dmp.packet_length = 0;
  1010. if (mask & DMP_FEATURE_SEND_RAW_ACCEL)
  1011. dmp.packet_length += 6;
  1012. if (mask & DMP_FEATURE_SEND_ANY_GYRO)
  1013. dmp.packet_length += 6;
  1014. if (mask & (DMP_FEATURE_LP_QUAT | DMP_FEATURE_6X_LP_QUAT))
  1015. dmp.packet_length += 16;
  1016. if (mask & (DMP_FEATURE_TAP | DMP_FEATURE_ANDROID_ORIENT))
  1017. dmp.packet_length += 4;
  1018. return 0;
  1019. }
  1020. /**
  1021. * @brief Get list of currently enabled DMP features.
  1022. * @param[out] Mask of enabled features.
  1023. * @return 0 if successful.
  1024. */
  1025. int dmp_get_enabled_features(unsigned short *mask)
  1026. {
  1027. mask[0] = dmp.feature_mask;
  1028. return 0;
  1029. }
  1030. /**
  1031. * @brief Calibrate the gyro data in the DMP.
  1032. * After eight seconds of no motion, the DMP will compute gyro biases and
  1033. * subtract them from the quaternion output. If @e dmp_enable_feature is
  1034. * called with @e DMP_FEATURE_SEND_CAL_GYRO, the biases will also be
  1035. * subtracted from the gyro output.
  1036. * @param[in] enable 1 to enable gyro calibration.
  1037. * @return 0 if successful.
  1038. */
  1039. int dmp_enable_gyro_cal(unsigned char enable)
  1040. {
  1041. if (enable) {
  1042. unsigned char regs[9] = {0xb8, 0xaa, 0xb3, 0x8d, 0xb4, 0x98, 0x0d, 0x35, 0x5d};
  1043. return mpu_write_mem(CFG_MOTION_BIAS, 9, regs);
  1044. } else {
  1045. unsigned char regs[9] = {0xb8, 0xaa, 0xaa, 0xaa, 0xb0, 0x88, 0xc3, 0xc5, 0xc7};
  1046. return mpu_write_mem(CFG_MOTION_BIAS, 9, regs);
  1047. }
  1048. }
  1049. /**
  1050. * @brief Generate 3-axis quaternions from the DMP.
  1051. * In this driver, the 3-axis and 6-axis DMP quaternion features are mutually
  1052. * exclusive.
  1053. * @param[in] enable 1 to enable 3-axis quaternion.
  1054. * @return 0 if successful.
  1055. */
  1056. int dmp_enable_lp_quat(unsigned char enable)
  1057. {
  1058. unsigned char regs[4];
  1059. if (enable) {
  1060. regs[0] = DINBC0;
  1061. regs[1] = DINBC2;
  1062. regs[2] = DINBC4;
  1063. regs[3] = DINBC6;
  1064. }
  1065. else
  1066. memset(regs, 0x8B, 4);
  1067. mpu_write_mem(CFG_LP_QUAT, 4, regs);
  1068. return mpu_reset_fifo();
  1069. }
  1070. /**
  1071. * @brief Generate 6-axis quaternions from the DMP.
  1072. * In this driver, the 3-axis and 6-axis DMP quaternion features are mutually
  1073. * exclusive.
  1074. * @param[in] enable 1 to enable 6-axis quaternion.
  1075. * @return 0 if successful.
  1076. */
  1077. int dmp_enable_6x_lp_quat(unsigned char enable)
  1078. {
  1079. unsigned char regs[4];
  1080. if (enable) {
  1081. regs[0] = DINA20;
  1082. regs[1] = DINA28;
  1083. regs[2] = DINA30;
  1084. regs[3] = DINA38;
  1085. } else
  1086. memset(regs, 0xA3, 4);
  1087. mpu_write_mem(CFG_8, 4, regs);
  1088. return mpu_reset_fifo();
  1089. }
  1090. /**
  1091. * @brief Decode the four-byte gesture data and execute any callbacks.
  1092. * @param[in] gesture Gesture data from DMP packet.
  1093. * @return 0 if successful.
  1094. */
  1095. static int decode_gesture(unsigned char *gesture)
  1096. {
  1097. unsigned char tap, android_orient;
  1098. android_orient = gesture[3] & 0xC0;
  1099. tap = 0x3F & gesture[3];
  1100. if (gesture[1] & INT_SRC_TAP) {
  1101. unsigned char direction, count;
  1102. direction = tap >> 3;
  1103. count = (tap % 8) + 1;
  1104. if (dmp.tap_cb)
  1105. dmp.tap_cb(direction, count);
  1106. }
  1107. if (gesture[1] & INT_SRC_ANDROID_ORIENT) {
  1108. if (dmp.android_orient_cb)
  1109. dmp.android_orient_cb(android_orient >> 6);
  1110. }
  1111. return 0;
  1112. }
  1113. /**
  1114. * @brief Specify when a DMP interrupt should occur.
  1115. * A DMP interrupt can be configured to trigger on either of the two
  1116. * conditions below:
  1117. * \n a. One FIFO period has elapsed (set by @e mpu_set_sample_rate).
  1118. * \n b. A tap event has been detected.
  1119. * @param[in] mode DMP_INT_GESTURE or DMP_INT_CONTINUOUS.
  1120. * @return 0 if successful.
  1121. */
  1122. int dmp_set_interrupt_mode(unsigned char mode)
  1123. {
  1124. const unsigned char regs_continuous[11] =
  1125. {0xd8, 0xb1, 0xb9, 0xf3, 0x8b, 0xa3, 0x91, 0xb6, 0x09, 0xb4, 0xd9};
  1126. const unsigned char regs_gesture[11] =
  1127. {0xda, 0xb1, 0xb9, 0xf3, 0x8b, 0xa3, 0x91, 0xb6, 0xda, 0xb4, 0xda};
  1128. switch (mode) {
  1129. case DMP_INT_CONTINUOUS:
  1130. return mpu_write_mem(CFG_FIFO_ON_EVENT, 11,
  1131. (unsigned char*)regs_continuous);
  1132. case DMP_INT_GESTURE:
  1133. return mpu_write_mem(CFG_FIFO_ON_EVENT, 11,
  1134. (unsigned char*)regs_gesture);
  1135. default:
  1136. return -1;
  1137. }
  1138. }
  1139. /**
  1140. * @brief Get one packet from the FIFO.
  1141. * If @e sensors does not contain a particular sensor, disregard the data
  1142. * returned to that pointer.
  1143. * \n @e sensors can contain a combination of the following flags:
  1144. * \n INV_X_GYRO, INV_Y_GYRO, INV_Z_GYRO
  1145. * \n INV_XYZ_GYRO
  1146. * \n INV_XYZ_ACCEL
  1147. * \n INV_WXYZ_QUAT
  1148. * \n If the FIFO has no new data, @e sensors will be zero.
  1149. * \n If the FIFO is disabled, @e sensors will be zero and this function will
  1150. * return a non-zero error code.
  1151. * @param[out] gyro Gyro data in hardware units.
  1152. * @param[out] accel Accel data in hardware units.
  1153. * @param[out] quat 3-axis quaternion data in hardware units.
  1154. * @param[out] timestamp Timestamp in milliseconds.
  1155. * @param[out] sensors Mask of sensors read from FIFO.
  1156. * @param[out] more Number of remaining packets.
  1157. * @return 0 if successful.
  1158. */
  1159. int dmp_read_fifo(short *gyro, short *accel, long *quat,
  1160. unsigned long *timestamp, short *sensors, unsigned char *more)
  1161. {
  1162. unsigned char fifo_data[MAX_PACKET_LENGTH];
  1163. unsigned char ii = 0;
  1164. /* TODO: sensors[0] only changes when dmp_enable_feature is called. We can
  1165. * cache this value and save some cycles.
  1166. */
  1167. sensors[0] = 0;
  1168. /* Get a packet. */
  1169. if (mpu_read_fifo_stream(dmp.packet_length, fifo_data, more))
  1170. return -1;
  1171. /* Parse DMP packet. */
  1172. if (dmp.feature_mask & (DMP_FEATURE_LP_QUAT | DMP_FEATURE_6X_LP_QUAT)) {
  1173. #ifdef FIFO_CORRUPTION_CHECK
  1174. long quat_q14[4], quat_mag_sq;
  1175. #endif
  1176. quat[0] = ((long)fifo_data[0] << 24) | ((long)fifo_data[1] << 16) |
  1177. ((long)fifo_data[2] << 8) | fifo_data[3];
  1178. quat[1] = ((long)fifo_data[4] << 24) | ((long)fifo_data[5] << 16) |
  1179. ((long)fifo_data[6] << 8) | fifo_data[7];
  1180. quat[2] = ((long)fifo_data[8] << 24) | ((long)fifo_data[9] << 16) |
  1181. ((long)fifo_data[10] << 8) | fifo_data[11];
  1182. quat[3] = ((long)fifo_data[12] << 24) | ((long)fifo_data[13] << 16) |
  1183. ((long)fifo_data[14] << 8) | fifo_data[15];
  1184. ii += 16;
  1185. #ifdef FIFO_CORRUPTION_CHECK
  1186. /* We can detect a corrupted FIFO by monitoring the quaternion data and
  1187. * ensuring that the magnitude is always normalized to one. This
  1188. * shouldn't happen in normal operation, but if an I2C error occurs,
  1189. * the FIFO reads might become misaligned.
  1190. *
  1191. * Let's start by scaling down the quaternion data to avoid long long
  1192. * math.
  1193. */
  1194. quat_q14[0] = quat[0] >> 16;
  1195. quat_q14[1] = quat[1] >> 16;
  1196. quat_q14[2] = quat[2] >> 16;
  1197. quat_q14[3] = quat[3] >> 16;
  1198. quat_mag_sq = quat_q14[0] * quat_q14[0] + quat_q14[1] * quat_q14[1] +
  1199. quat_q14[2] * quat_q14[2] + quat_q14[3] * quat_q14[3];
  1200. if ((quat_mag_sq < QUAT_MAG_SQ_MIN) ||
  1201. (quat_mag_sq > QUAT_MAG_SQ_MAX)) {
  1202. /* Quaternion is outside of the acceptable threshold. */
  1203. mpu_reset_fifo();
  1204. sensors[0] = 0;
  1205. return -1;
  1206. }
  1207. sensors[0] |= INV_WXYZ_QUAT;
  1208. #endif
  1209. }
  1210. if (dmp.feature_mask & DMP_FEATURE_SEND_RAW_ACCEL) {
  1211. accel[0] = ((short)fifo_data[ii+0] << 8) | fifo_data[ii+1];
  1212. accel[1] = ((short)fifo_data[ii+2] << 8) | fifo_data[ii+3];
  1213. accel[2] = ((short)fifo_data[ii+4] << 8) | fifo_data[ii+5];
  1214. ii += 6;
  1215. sensors[0] |= INV_XYZ_ACCEL;
  1216. }
  1217. if (dmp.feature_mask & DMP_FEATURE_SEND_ANY_GYRO) {
  1218. gyro[0] = ((short)fifo_data[ii+0] << 8) | fifo_data[ii+1];
  1219. gyro[1] = ((short)fifo_data[ii+2] << 8) | fifo_data[ii+3];
  1220. gyro[2] = ((short)fifo_data[ii+4] << 8) | fifo_data[ii+5];
  1221. ii += 6;
  1222. sensors[0] |= INV_XYZ_GYRO;
  1223. }
  1224. /* Gesture data is at the end of the DMP packet. Parse it and call
  1225. * the gesture callbacks (if registered).
  1226. */
  1227. if (dmp.feature_mask & (DMP_FEATURE_TAP | DMP_FEATURE_ANDROID_ORIENT))
  1228. decode_gesture(fifo_data + ii);
  1229. get_ms(timestamp);
  1230. return 0;
  1231. }
  1232. /**
  1233. * @brief Register a function to be executed on a tap event.
  1234. * The tap direction is represented by one of the following:
  1235. * \n TAP_X_UP
  1236. * \n TAP_X_DOWN
  1237. * \n TAP_Y_UP
  1238. * \n TAP_Y_DOWN
  1239. * \n TAP_Z_UP
  1240. * \n TAP_Z_DOWN
  1241. * @param[in] func Callback function.
  1242. * @return 0 if successful.
  1243. */
  1244. int dmp_register_tap_cb(void (*func)(unsigned char, unsigned char))
  1245. {
  1246. dmp.tap_cb = func;
  1247. return 0;
  1248. }
  1249. /**
  1250. * @brief Register a function to be executed on a android orientation event.
  1251. * @param[in] func Callback function.
  1252. * @return 0 if successful.
  1253. */
  1254. int dmp_register_android_orient_cb(void (*func)(unsigned char))
  1255. {
  1256. dmp.android_orient_cb = func;
  1257. return 0;
  1258. }
  1259. /**
  1260. * @}
  1261. */

(2)inv_mpu_dmp_motion_driver.h

  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_dmp_motion_driver.h
  13. * @brief DMP image and interface functions.
  14. * @details All functions are preceded by the dmp_ prefix to
  15. * differentiate among MPL and general driver function calls.
  16. */
  17. #ifndef _INV_MPU_DMP_MOTION_DRIVER_H_
  18. #define _INV_MPU_DMP_MOTION_DRIVER_H_
  19. #define TAP_X (0x01)
  20. #define TAP_Y (0x02)
  21. #define TAP_Z (0x04)
  22. #define TAP_XYZ (0x07)
  23. #define TAP_X_UP (0x01)
  24. #define TAP_X_DOWN (0x02)
  25. #define TAP_Y_UP (0x03)
  26. #define TAP_Y_DOWN (0x04)
  27. #define TAP_Z_UP (0x05)
  28. #define TAP_Z_DOWN (0x06)
  29. #define ANDROID_ORIENT_PORTRAIT (0x00)
  30. #define ANDROID_ORIENT_LANDSCAPE (0x01)
  31. #define ANDROID_ORIENT_REVERSE_PORTRAIT (0x02)
  32. #define ANDROID_ORIENT_REVERSE_LANDSCAPE (0x03)
  33. #define DMP_INT_GESTURE (0x01)
  34. #define DMP_INT_CONTINUOUS (0x02)
  35. #define DMP_FEATURE_TAP (0x001)
  36. #define DMP_FEATURE_ANDROID_ORIENT (0x002)
  37. #define DMP_FEATURE_LP_QUAT (0x004)
  38. #define DMP_FEATURE_PEDOMETER (0x008)
  39. #define DMP_FEATURE_6X_LP_QUAT (0x010)
  40. #define DMP_FEATURE_GYRO_CAL (0x020)
  41. #define DMP_FEATURE_SEND_RAW_ACCEL (0x040)
  42. #define DMP_FEATURE_SEND_RAW_GYRO (0x080)
  43. #define DMP_FEATURE_SEND_CAL_GYRO (0x100)
  44. #define INV_WXYZ_QUAT (0x100)
  45. /* Set up functions. */
  46. int dmp_load_motion_driver_firmware(void);
  47. int dmp_set_fifo_rate(unsigned short rate);
  48. int dmp_get_fifo_rate(unsigned short *rate);
  49. int dmp_enable_feature(unsigned short mask);
  50. int dmp_get_enabled_features(unsigned short *mask);
  51. int dmp_set_interrupt_mode(unsigned char mode);
  52. int dmp_set_orientation(unsigned short orient);
  53. int dmp_set_gyro_bias(long *bias);
  54. int dmp_set_accel_bias(long *bias);
  55. /* Tap functions. */
  56. int dmp_register_tap_cb(void (*func)(unsigned char, unsigned char));
  57. int dmp_set_tap_thresh(unsigned char axis, unsigned short thresh);
  58. int dmp_set_tap_axes(unsigned char axis);
  59. int dmp_set_tap_count(unsigned char min_taps);
  60. int dmp_set_tap_time(unsigned short time);
  61. int dmp_set_tap_time_multi(unsigned short time);
  62. int dmp_set_shake_reject_thresh(long sf, unsigned short thresh);
  63. int dmp_set_shake_reject_time(unsigned short time);
  64. int dmp_set_shake_reject_timeout(unsigned short time);
  65. /* Android orientation functions. */
  66. int dmp_register_android_orient_cb(void (*func)(unsigned char));
  67. /* LP quaternion functions. */
  68. int dmp_enable_lp_quat(unsigned char enable);
  69. int dmp_enable_6x_lp_quat(unsigned char enable);
  70. /* Pedometer functions. */
  71. int dmp_get_pedometer_step_count(unsigned long *count);
  72. int dmp_set_pedometer_step_count(unsigned long count);
  73. int dmp_get_pedometer_walk_time(unsigned long *time);
  74. int dmp_set_pedometer_walk_time(unsigned long time);
  75. /* DMP gyro calibration functions. */
  76. int dmp_enable_gyro_cal(unsigned char enable);
  77. /* Read function. This function should be called whenever the MPU interrupt is
  78. * detected.
  79. */
  80. int dmp_read_fifo(short *gyro, short *accel, long *quat,
  81. unsigned long *timestamp, short *sensors, unsigned char *more);
  82. #endif /* #ifndef _INV_MPU_DMP_MOTION_DRIVER_H_ */

(3) inv_mpu.c

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

(4) inv_mpu.h

  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.h
  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. #ifndef _INV_MPU_H_
  21. #define _INV_MPU_H_
  22. #define INV_X_GYRO (0x40)
  23. #define INV_Y_GYRO (0x20)
  24. #define INV_Z_GYRO (0x10)
  25. #define INV_XYZ_GYRO (INV_X_GYRO | INV_Y_GYRO | INV_Z_GYRO)
  26. #define INV_XYZ_ACCEL (0x08)
  27. #define INV_XYZ_COMPASS (0x01)
  28. struct int_param_s {
  29. #if defined EMPL_TARGET_MSP430 || defined MOTION_DRIVER_TARGET_MSP430
  30. void (*cb)(void);
  31. unsigned short pin;
  32. unsigned char lp_exit;
  33. unsigned char active_low;
  34. #elif defined EMPL_TARGET_UC3L0
  35. unsigned long pin;
  36. void (*cb)(volatile void*);
  37. void *arg;
  38. #elif defined STM32F10X_HD //自行添加的一行,避免编译错误,实际没用到
  39. void (*cb)(void);
  40. #endif
  41. };
  42. #define MPU_INT_STATUS_DATA_READY (0x0001)
  43. #define MPU_INT_STATUS_DMP (0x0002)
  44. #define MPU_INT_STATUS_PLL_READY (0x0004)
  45. #define MPU_INT_STATUS_I2C_MST (0x0008)
  46. #define MPU_INT_STATUS_FIFO_OVERFLOW (0x0010)
  47. #define MPU_INT_STATUS_ZMOT (0x0020)
  48. #define MPU_INT_STATUS_MOT (0x0040)
  49. #define MPU_INT_STATUS_FREE_FALL (0x0080)
  50. #define MPU_INT_STATUS_DMP_0 (0x0100)
  51. #define MPU_INT_STATUS_DMP_1 (0x0200)
  52. #define MPU_INT_STATUS_DMP_2 (0x0400)
  53. #define MPU_INT_STATUS_DMP_3 (0x0800)
  54. #define MPU_INT_STATUS_DMP_4 (0x1000)
  55. #define MPU_INT_STATUS_DMP_5 (0x2000)
  56. /* Set up APIs */
  57. int mpu_init(struct int_param_s *int_param);
  58. int mpu_init_slave(void);
  59. int mpu_set_bypass(unsigned char bypass_on);
  60. /* Configuration APIs */
  61. int mpu_lp_accel_mode(unsigned char rate);
  62. int mpu_lp_motion_interrupt(unsigned short thresh, unsigned char time,
  63. unsigned char lpa_freq);
  64. int mpu_set_int_level(unsigned char active_low);
  65. int mpu_set_int_latched(unsigned char enable);
  66. int mpu_set_dmp_state(unsigned char enable);
  67. int mpu_get_dmp_state(unsigned char *enabled);
  68. int mpu_get_lpf(unsigned short *lpf);
  69. int mpu_set_lpf(unsigned short lpf);
  70. int mpu_get_gyro_fsr(unsigned short *fsr);
  71. int mpu_set_gyro_fsr(unsigned short fsr);
  72. int mpu_get_accel_fsr(unsigned char *fsr);
  73. int mpu_set_accel_fsr(unsigned char fsr);
  74. int mpu_get_compass_fsr(unsigned short *fsr);
  75. int mpu_get_gyro_sens(float *sens);
  76. int mpu_get_accel_sens(unsigned short *sens);
  77. int mpu_get_sample_rate(unsigned short *rate);
  78. int mpu_set_sample_rate(unsigned short rate);
  79. int mpu_get_compass_sample_rate(unsigned short *rate);
  80. int mpu_set_compass_sample_rate(unsigned short rate);
  81. int mpu_get_fifo_config(unsigned char *sensors);
  82. int mpu_configure_fifo(unsigned char sensors);
  83. int mpu_get_power_state(unsigned char *power_on);
  84. int mpu_set_sensors(unsigned char sensors);
  85. int mpu_set_accel_bias(const long *accel_bias);
  86. /* Data getter/setter APIs */
  87. int mpu_get_gyro_reg(short *data, unsigned long *timestamp);
  88. int mpu_get_accel_reg(short *data, unsigned long *timestamp);
  89. int mpu_get_compass_reg(short *data, unsigned long *timestamp);
  90. int mpu_get_temperature(long *data, unsigned long *timestamp);
  91. int mpu_get_int_status(short *status);
  92. int mpu_read_fifo(short *gyro, short *accel, unsigned long *timestamp,
  93. unsigned char *sensors, unsigned char *more);
  94. int mpu_read_fifo_stream(unsigned short length, unsigned char *data,
  95. unsigned char *more);
  96. int mpu_reset_fifo(void);
  97. int mpu_write_mem(unsigned short mem_addr, unsigned short length,
  98. unsigned char *data);
  99. int mpu_read_mem(unsigned short mem_addr, unsigned short length,
  100. unsigned char *data);
  101. int mpu_load_firmware(unsigned short length, const unsigned char *firmware,
  102. unsigned short start_addr, unsigned short sample_rate);
  103. int mpu_reg_dump(void);
  104. int mpu_read_reg(unsigned char reg, unsigned char *data);
  105. int mpu_run_self_test(long *gyro, long *accel);
  106. int mpu_register_tap_cb(void (*func)(unsigned char, unsigned char));
  107. #endif /* #ifndef _INV_MPU_H_ */

(5) dmpKey.h

  1. /*
  2. $License:
  3. Copyright (C) 2011 InvenSense Corporation, All Rights Reserved.
  4. $
  5. */
  6. #ifndef DMPKEY_H__
  7. #define DMPKEY_H__
  8. #define KEY_CFG_25 (0)
  9. #define KEY_CFG_24 (KEY_CFG_25 + 1)
  10. #define KEY_CFG_26 (KEY_CFG_24 + 1)
  11. #define KEY_CFG_27 (KEY_CFG_26 + 1)
  12. #define KEY_CFG_21 (KEY_CFG_27 + 1)
  13. #define KEY_CFG_20 (KEY_CFG_21 + 1)
  14. #define KEY_CFG_TAP4 (KEY_CFG_20 + 1)
  15. #define KEY_CFG_TAP5 (KEY_CFG_TAP4 + 1)
  16. #define KEY_CFG_TAP6 (KEY_CFG_TAP5 + 1)
  17. #define KEY_CFG_TAP7 (KEY_CFG_TAP6 + 1)
  18. #define KEY_CFG_TAP0 (KEY_CFG_TAP7 + 1)
  19. #define KEY_CFG_TAP1 (KEY_CFG_TAP0 + 1)
  20. #define KEY_CFG_TAP2 (KEY_CFG_TAP1 + 1)
  21. #define KEY_CFG_TAP3 (KEY_CFG_TAP2 + 1)
  22. #define KEY_CFG_TAP_QUANTIZE (KEY_CFG_TAP3 + 1)
  23. #define KEY_CFG_TAP_JERK (KEY_CFG_TAP_QUANTIZE + 1)
  24. #define KEY_CFG_DR_INT (KEY_CFG_TAP_JERK + 1)
  25. #define KEY_CFG_AUTH (KEY_CFG_DR_INT + 1)
  26. #define KEY_CFG_TAP_SAVE_ACCB (KEY_CFG_AUTH + 1)
  27. #define KEY_CFG_TAP_CLEAR_STICKY (KEY_CFG_TAP_SAVE_ACCB + 1)
  28. #define KEY_CFG_FIFO_ON_EVENT (KEY_CFG_TAP_CLEAR_STICKY + 1)
  29. #define KEY_FCFG_ACCEL_INPUT (KEY_CFG_FIFO_ON_EVENT + 1)
  30. #define KEY_FCFG_ACCEL_INIT (KEY_FCFG_ACCEL_INPUT + 1)
  31. #define KEY_CFG_23 (KEY_FCFG_ACCEL_INIT + 1)
  32. #define KEY_FCFG_1 (KEY_CFG_23 + 1)
  33. #define KEY_FCFG_3 (KEY_FCFG_1 + 1)
  34. #define KEY_FCFG_2 (KEY_FCFG_3 + 1)
  35. #define KEY_CFG_3D (KEY_FCFG_2 + 1)
  36. #define KEY_CFG_3B (KEY_CFG_3D + 1)
  37. #define KEY_CFG_3C (KEY_CFG_3B + 1)
  38. #define KEY_FCFG_5 (KEY_CFG_3C + 1)
  39. #define KEY_FCFG_4 (KEY_FCFG_5 + 1)
  40. #define KEY_FCFG_7 (KEY_FCFG_4 + 1)
  41. #define KEY_FCFG_FSCALE (KEY_FCFG_7 + 1)
  42. #define KEY_FCFG_AZ (KEY_FCFG_FSCALE + 1)
  43. #define KEY_FCFG_6 (KEY_FCFG_AZ + 1)
  44. #define KEY_FCFG_LSB4 (KEY_FCFG_6 + 1)
  45. #define KEY_CFG_12 (KEY_FCFG_LSB4 + 1)
  46. #define KEY_CFG_14 (KEY_CFG_12 + 1)
  47. #define KEY_CFG_15 (KEY_CFG_14 + 1)
  48. #define KEY_CFG_16 (KEY_CFG_15 + 1)
  49. #define KEY_CFG_18 (KEY_CFG_16 + 1)
  50. #define KEY_CFG_6 (KEY_CFG_18 + 1)
  51. #define KEY_CFG_7 (KEY_CFG_6 + 1)
  52. #define KEY_CFG_4 (KEY_CFG_7 + 1)
  53. #define KEY_CFG_5 (KEY_CFG_4 + 1)
  54. #define KEY_CFG_2 (KEY_CFG_5 + 1)
  55. #define KEY_CFG_3 (KEY_CFG_2 + 1)
  56. #define KEY_CFG_1 (KEY_CFG_3 + 1)
  57. #define KEY_CFG_EXTERNAL (KEY_CFG_1 + 1)
  58. #define KEY_CFG_8 (KEY_CFG_EXTERNAL + 1)
  59. #define KEY_CFG_9 (KEY_CFG_8 + 1)
  60. #define KEY_CFG_ORIENT_3 (KEY_CFG_9 + 1)
  61. #define KEY_CFG_ORIENT_2 (KEY_CFG_ORIENT_3 + 1)
  62. #define KEY_CFG_ORIENT_1 (KEY_CFG_ORIENT_2 + 1)
  63. #define KEY_CFG_GYRO_SOURCE (KEY_CFG_ORIENT_1 + 1)
  64. #define KEY_CFG_ORIENT_IRQ_1 (KEY_CFG_GYRO_SOURCE + 1)
  65. #define KEY_CFG_ORIENT_IRQ_2 (KEY_CFG_ORIENT_IRQ_1 + 1)
  66. #define KEY_CFG_ORIENT_IRQ_3 (KEY_CFG_ORIENT_IRQ_2 + 1)
  67. #define KEY_FCFG_MAG_VAL (KEY_CFG_ORIENT_IRQ_3 + 1)
  68. #define KEY_FCFG_MAG_MOV (KEY_FCFG_MAG_VAL + 1)
  69. #define KEY_CFG_LP_QUAT (KEY_FCFG_MAG_MOV + 1)
  70. /* MPU6050 keys */
  71. #define KEY_CFG_ACCEL_FILTER (KEY_CFG_LP_QUAT + 1)
  72. #define KEY_CFG_MOTION_BIAS (KEY_CFG_ACCEL_FILTER + 1)
  73. #define KEY_TEMPLABEL (KEY_CFG_MOTION_BIAS + 1)
  74. #define KEY_D_0_22 (KEY_TEMPLABEL + 1)
  75. #define KEY_D_0_24 (KEY_D_0_22 + 1)
  76. #define KEY_D_0_36 (KEY_D_0_24 + 1)
  77. #define KEY_D_0_52 (KEY_D_0_36 + 1)
  78. #define KEY_D_0_96 (KEY_D_0_52 + 1)
  79. #define KEY_D_0_104 (KEY_D_0_96 + 1)
  80. #define KEY_D_0_108 (KEY_D_0_104 + 1)
  81. #define KEY_D_0_163 (KEY_D_0_108 + 1)
  82. #define KEY_D_0_188 (KEY_D_0_163 + 1)
  83. #define KEY_D_0_192 (KEY_D_0_188 + 1)
  84. #define KEY_D_0_224 (KEY_D_0_192 + 1)
  85. #define KEY_D_0_228 (KEY_D_0_224 + 1)
  86. #define KEY_D_0_232 (KEY_D_0_228 + 1)
  87. #define KEY_D_0_236 (KEY_D_0_232 + 1)
  88. #define KEY_DMP_PREVPTAT (KEY_D_0_236 + 1)
  89. #define KEY_D_1_2 (KEY_DMP_PREVPTAT + 1)
  90. #define KEY_D_1_4 (KEY_D_1_2 + 1)
  91. #define KEY_D_1_8 (KEY_D_1_4 + 1)
  92. #define KEY_D_1_10 (KEY_D_1_8 + 1)
  93. #define KEY_D_1_24 (KEY_D_1_10 + 1)
  94. #define KEY_D_1_28 (KEY_D_1_24 + 1)
  95. #define KEY_D_1_36 (KEY_D_1_28 + 1)
  96. #define KEY_D_1_40 (KEY_D_1_36 + 1)
  97. #define KEY_D_1_44 (KEY_D_1_40 + 1)
  98. #define KEY_D_1_72 (KEY_D_1_44 + 1)
  99. #define KEY_D_1_74 (KEY_D_1_72 + 1)
  100. #define KEY_D_1_79 (KEY_D_1_74 + 1)
  101. #define KEY_D_1_88 (KEY_D_1_79 + 1)
  102. #define KEY_D_1_90 (KEY_D_1_88 + 1)
  103. #define KEY_D_1_92 (KEY_D_1_90 + 1)
  104. #define KEY_D_1_96 (KEY_D_1_92 + 1)
  105. #define KEY_D_1_98 (KEY_D_1_96 + 1)
  106. #define KEY_D_1_100 (KEY_D_1_98 + 1)
  107. #define KEY_D_1_106 (KEY_D_1_100 + 1)
  108. #define KEY_D_1_108 (KEY_D_1_106 + 1)
  109. #define KEY_D_1_112 (KEY_D_1_108 + 1)
  110. #define KEY_D_1_128 (KEY_D_1_112 + 1)
  111. #define KEY_D_1_152 (KEY_D_1_128 + 1)
  112. #define KEY_D_1_160 (KEY_D_1_152 + 1)
  113. #define KEY_D_1_168 (KEY_D_1_160 + 1)
  114. #define KEY_D_1_175 (KEY_D_1_168 + 1)
  115. #define KEY_D_1_176 (KEY_D_1_175 + 1)
  116. #define KEY_D_1_178 (KEY_D_1_176 + 1)
  117. #define KEY_D_1_179 (KEY_D_1_178 + 1)
  118. #define KEY_D_1_218 (KEY_D_1_179 + 1)
  119. #define KEY_D_1_232 (KEY_D_1_218 + 1)
  120. #define KEY_D_1_236 (KEY_D_1_232 + 1)
  121. #define KEY_D_1_240 (KEY_D_1_236 + 1)
  122. #define KEY_D_1_244 (KEY_D_1_240 + 1)
  123. #define KEY_D_1_250 (KEY_D_1_244 + 1)
  124. #define KEY_D_1_252 (KEY_D_1_250 + 1)
  125. #define KEY_D_2_12 (KEY_D_1_252 + 1)
  126. #define KEY_D_2_96 (KEY_D_2_12 + 1)
  127. #define KEY_D_2_108 (KEY_D_2_96 + 1)
  128. #define KEY_D_2_208 (KEY_D_2_108 + 1)
  129. #define KEY_FLICK_MSG (KEY_D_2_208 + 1)
  130. #define KEY_FLICK_COUNTER (KEY_FLICK_MSG + 1)
  131. #define KEY_FLICK_LOWER (KEY_FLICK_COUNTER + 1)
  132. #define KEY_CFG_FLICK_IN (KEY_FLICK_LOWER + 1)
  133. #define KEY_FLICK_UPPER (KEY_CFG_FLICK_IN + 1)
  134. #define KEY_CGNOTICE_INTR (KEY_FLICK_UPPER + 1)
  135. #define KEY_D_2_224 (KEY_CGNOTICE_INTR + 1)
  136. #define KEY_D_2_244 (KEY_D_2_224 + 1)
  137. #define KEY_D_2_248 (KEY_D_2_244 + 1)
  138. #define KEY_D_2_252 (KEY_D_2_248 + 1)
  139. #define KEY_D_GYRO_BIAS_X (KEY_D_2_252 + 1)
  140. #define KEY_D_GYRO_BIAS_Y (KEY_D_GYRO_BIAS_X + 1)
  141. #define KEY_D_GYRO_BIAS_Z (KEY_D_GYRO_BIAS_Y + 1)
  142. #define KEY_D_ACC_BIAS_X (KEY_D_GYRO_BIAS_Z + 1)
  143. #define KEY_D_ACC_BIAS_Y (KEY_D_ACC_BIAS_X + 1)
  144. #define KEY_D_ACC_BIAS_Z (KEY_D_ACC_BIAS_Y + 1)
  145. #define KEY_D_GYRO_ENABLE (KEY_D_ACC_BIAS_Z + 1)
  146. #define KEY_D_ACCEL_ENABLE (KEY_D_GYRO_ENABLE + 1)
  147. #define KEY_D_QUAT_ENABLE (KEY_D_ACCEL_ENABLE +1)
  148. #define KEY_D_OUTPUT_ENABLE (KEY_D_QUAT_ENABLE + 1)
  149. #define KEY_D_CR_TIME_G (KEY_D_OUTPUT_ENABLE + 1)
  150. #define KEY_D_CR_TIME_A (KEY_D_CR_TIME_G + 1)
  151. #define KEY_D_CR_TIME_Q (KEY_D_CR_TIME_A + 1)
  152. #define KEY_D_CS_TAX (KEY_D_CR_TIME_Q + 1)
  153. #define KEY_D_CS_TAY (KEY_D_CS_TAX + 1)
  154. #define KEY_D_CS_TAZ (KEY_D_CS_TAY + 1)
  155. #define KEY_D_CS_TGX (KEY_D_CS_TAZ + 1)
  156. #define KEY_D_CS_TGY (KEY_D_CS_TGX + 1)
  157. #define KEY_D_CS_TGZ (KEY_D_CS_TGY + 1)
  158. #define KEY_D_CS_TQ0 (KEY_D_CS_TGZ + 1)
  159. #define KEY_D_CS_TQ1 (KEY_D_CS_TQ0 + 1)
  160. #define KEY_D_CS_TQ2 (KEY_D_CS_TQ1 + 1)
  161. #define KEY_D_CS_TQ3 (KEY_D_CS_TQ2 + 1)
  162. /* Compass keys */
  163. #define KEY_CPASS_BIAS_X (KEY_D_CS_TQ3 + 1)
  164. #define KEY_CPASS_BIAS_Y (KEY_CPASS_BIAS_X + 1)
  165. #define KEY_CPASS_BIAS_Z (KEY_CPASS_BIAS_Y + 1)
  166. #define KEY_CPASS_MTX_00 (KEY_CPASS_BIAS_Z + 1)
  167. #define KEY_CPASS_MTX_01 (KEY_CPASS_MTX_00 + 1)
  168. #define KEY_CPASS_MTX_02 (KEY_CPASS_MTX_01 + 1)
  169. #define KEY_CPASS_MTX_10 (KEY_CPASS_MTX_02 + 1)
  170. #define KEY_CPASS_MTX_11 (KEY_CPASS_MTX_10 + 1)
  171. #define KEY_CPASS_MTX_12 (KEY_CPASS_MTX_11 + 1)
  172. #define KEY_CPASS_MTX_20 (KEY_CPASS_MTX_12 + 1)
  173. #define KEY_CPASS_MTX_21 (KEY_CPASS_MTX_20 + 1)
  174. #define KEY_CPASS_MTX_22 (KEY_CPASS_MTX_21 + 1)
  175. /* Gesture Keys */
  176. #define KEY_DMP_TAPW_MIN (KEY_CPASS_MTX_22 + 1)
  177. #define KEY_DMP_TAP_THR_X (KEY_DMP_TAPW_MIN + 1)
  178. #define KEY_DMP_TAP_THR_Y (KEY_DMP_TAP_THR_X + 1)
  179. #define KEY_DMP_TAP_THR_Z (KEY_DMP_TAP_THR_Y + 1)
  180. #define KEY_DMP_SH_TH_Y (KEY_DMP_TAP_THR_Z + 1)
  181. #define KEY_DMP_SH_TH_X (KEY_DMP_SH_TH_Y + 1)
  182. #define KEY_DMP_SH_TH_Z (KEY_DMP_SH_TH_X + 1)
  183. #define KEY_DMP_ORIENT (KEY_DMP_SH_TH_Z + 1)
  184. #define KEY_D_ACT0 (KEY_DMP_ORIENT + 1)
  185. #define KEY_D_ACSX (KEY_D_ACT0 + 1)
  186. #define KEY_D_ACSY (KEY_D_ACSX + 1)
  187. #define KEY_D_ACSZ (KEY_D_ACSY + 1)
  188. #define KEY_X_GRT_Y_TMP (KEY_D_ACSZ + 1)
  189. #define KEY_SKIP_X_GRT_Y_TMP (KEY_X_GRT_Y_TMP + 1)
  190. #define KEY_SKIP_END_COMPARE (KEY_SKIP_X_GRT_Y_TMP + 1)
  191. #define KEY_END_COMPARE_Y_X_TMP2 (KEY_SKIP_END_COMPARE + 1)
  192. #define KEY_CFG_ANDROID_ORIENT_INT (KEY_END_COMPARE_Y_X_TMP2 + 1)
  193. #define KEY_NO_ORIENT_INTERRUPT (KEY_CFG_ANDROID_ORIENT_INT + 1)
  194. #define KEY_END_COMPARE_Y_X_TMP (KEY_NO_ORIENT_INTERRUPT + 1)
  195. #define KEY_END_ORIENT_1 (KEY_END_COMPARE_Y_X_TMP + 1)
  196. #define KEY_END_COMPARE_Y_X (KEY_END_ORIENT_1 + 1)
  197. #define KEY_END_ORIENT (KEY_END_COMPARE_Y_X + 1)
  198. #define KEY_X_GRT_Y (KEY_END_ORIENT + 1)
  199. #define KEY_NOT_TIME_MINUS_1 (KEY_X_GRT_Y + 1)
  200. #define KEY_END_COMPARE_Y_X_TMP3 (KEY_NOT_TIME_MINUS_1 + 1)
  201. #define KEY_X_GRT_Y_TMP2 (KEY_END_COMPARE_Y_X_TMP3 + 1)
  202. /* Authenticate Keys */
  203. #define KEY_D_AUTH_OUT (KEY_X_GRT_Y_TMP2 + 1)
  204. #define KEY_D_AUTH_IN (KEY_D_AUTH_OUT + 1)
  205. #define KEY_D_AUTH_A (KEY_D_AUTH_IN + 1)
  206. #define KEY_D_AUTH_B (KEY_D_AUTH_A + 1)
  207. /* Pedometer standalone only keys */
  208. #define KEY_D_PEDSTD_BP_B (KEY_D_AUTH_B + 1)
  209. #define KEY_D_PEDSTD_HP_A (KEY_D_PEDSTD_BP_B + 1)
  210. #define KEY_D_PEDSTD_HP_B (KEY_D_PEDSTD_HP_A + 1)
  211. #define KEY_D_PEDSTD_BP_A4 (KEY_D_PEDSTD_HP_B + 1)
  212. #define KEY_D_PEDSTD_BP_A3 (KEY_D_PEDSTD_BP_A4 + 1)
  213. #define KEY_D_PEDSTD_BP_A2 (KEY_D_PEDSTD_BP_A3 + 1)
  214. #define KEY_D_PEDSTD_BP_A1 (KEY_D_PEDSTD_BP_A2 + 1)
  215. #define KEY_D_PEDSTD_INT_THRSH (KEY_D_PEDSTD_BP_A1 + 1)
  216. #define KEY_D_PEDSTD_CLIP (KEY_D_PEDSTD_INT_THRSH + 1)
  217. #define KEY_D_PEDSTD_SB (KEY_D_PEDSTD_CLIP + 1)
  218. #define KEY_D_PEDSTD_SB_TIME (KEY_D_PEDSTD_SB + 1)
  219. #define KEY_D_PEDSTD_PEAKTHRSH (KEY_D_PEDSTD_SB_TIME + 1)
  220. #define KEY_D_PEDSTD_TIML (KEY_D_PEDSTD_PEAKTHRSH + 1)
  221. #define KEY_D_PEDSTD_TIMH (KEY_D_PEDSTD_TIML + 1)
  222. #define KEY_D_PEDSTD_PEAK (KEY_D_PEDSTD_TIMH + 1)
  223. #define KEY_D_PEDSTD_TIMECTR (KEY_D_PEDSTD_PEAK + 1)
  224. #define KEY_D_PEDSTD_STEPCTR (KEY_D_PEDSTD_TIMECTR + 1)
  225. #define KEY_D_PEDSTD_WALKTIME (KEY_D_PEDSTD_STEPCTR + 1)
  226. #define KEY_D_PEDSTD_DECI (KEY_D_PEDSTD_WALKTIME + 1)
  227. /*Host Based No Motion*/
  228. #define KEY_D_HOST_NO_MOT (KEY_D_PEDSTD_DECI + 1)
  229. /* EIS keys */
  230. #define KEY_P_EIS_FIFO_FOOTER (KEY_D_HOST_NO_MOT + 1)
  231. #define KEY_P_EIS_FIFO_YSHIFT (KEY_P_EIS_FIFO_FOOTER + 1)
  232. #define KEY_P_EIS_DATA_RATE (KEY_P_EIS_FIFO_YSHIFT + 1)
  233. #define KEY_P_EIS_FIFO_XSHIFT (KEY_P_EIS_DATA_RATE + 1)
  234. #define KEY_P_EIS_FIFO_SYNC (KEY_P_EIS_FIFO_XSHIFT + 1)
  235. #define KEY_P_EIS_FIFO_ZSHIFT (KEY_P_EIS_FIFO_SYNC + 1)
  236. #define KEY_P_EIS_FIFO_READY (KEY_P_EIS_FIFO_ZSHIFT + 1)
  237. #define KEY_DMP_FOOTER (KEY_P_EIS_FIFO_READY + 1)
  238. #define KEY_DMP_INTX_HC (KEY_DMP_FOOTER + 1)
  239. #define KEY_DMP_INTX_PH (KEY_DMP_INTX_HC + 1)
  240. #define KEY_DMP_INTX_SH (KEY_DMP_INTX_PH + 1)
  241. #define KEY_DMP_AINV_SH (KEY_DMP_INTX_SH + 1)
  242. #define KEY_DMP_A_INV_XH (KEY_DMP_AINV_SH + 1)
  243. #define KEY_DMP_AINV_PH (KEY_DMP_A_INV_XH + 1)
  244. #define KEY_DMP_CTHX_H (KEY_DMP_AINV_PH + 1)
  245. #define KEY_DMP_CTHY_H (KEY_DMP_CTHX_H + 1)
  246. #define KEY_DMP_CTHZ_H (KEY_DMP_CTHY_H + 1)
  247. #define KEY_DMP_NCTHX_H (KEY_DMP_CTHZ_H + 1)
  248. #define KEY_DMP_NCTHY_H (KEY_DMP_NCTHX_H + 1)
  249. #define KEY_DMP_NCTHZ_H (KEY_DMP_NCTHY_H + 1)
  250. #define KEY_DMP_CTSQ_XH (KEY_DMP_NCTHZ_H + 1)
  251. #define KEY_DMP_CTSQ_YH (KEY_DMP_CTSQ_XH + 1)
  252. #define KEY_DMP_CTSQ_ZH (KEY_DMP_CTSQ_YH + 1)
  253. #define KEY_DMP_INTX_H (KEY_DMP_CTSQ_ZH + 1)
  254. #define KEY_DMP_INTY_H (KEY_DMP_INTX_H + 1)
  255. #define KEY_DMP_INTZ_H (KEY_DMP_INTY_H + 1)
  256. //#define KEY_DMP_HPX_H (KEY_DMP_INTZ_H + 1)
  257. //#define KEY_DMP_HPY_H (KEY_DMP_HPX_H + 1)
  258. //#define KEY_DMP_HPZ_H (KEY_DMP_HPY_H + 1)
  259. /* Stream keys */
  260. #define KEY_STREAM_P_GYRO_Z (KEY_DMP_INTZ_H + 1)
  261. #define KEY_STREAM_P_GYRO_Y (KEY_STREAM_P_GYRO_Z + 1)
  262. #define KEY_STREAM_P_GYRO_X (KEY_STREAM_P_GYRO_Y + 1)
  263. #define KEY_STREAM_P_TEMP (KEY_STREAM_P_GYRO_X + 1)
  264. #define KEY_STREAM_P_AUX_Y (KEY_STREAM_P_TEMP + 1)
  265. #define KEY_STREAM_P_AUX_X (KEY_STREAM_P_AUX_Y + 1)
  266. #define KEY_STREAM_P_AUX_Z (KEY_STREAM_P_AUX_X + 1)
  267. #define KEY_STREAM_P_ACCEL_Y (KEY_STREAM_P_AUX_Z + 1)
  268. #define KEY_STREAM_P_ACCEL_X (KEY_STREAM_P_ACCEL_Y + 1)
  269. #define KEY_STREAM_P_FOOTER (KEY_STREAM_P_ACCEL_X + 1)
  270. #define KEY_STREAM_P_ACCEL_Z (KEY_STREAM_P_FOOTER + 1)
  271. #define NUM_KEYS (KEY_STREAM_P_ACCEL_Z + 1)
  272. typedef struct {
  273. unsigned short key;
  274. unsigned short addr;
  275. } tKeyLabel;
  276. #define DINA0A 0x0a
  277. #define DINA22 0x22
  278. #define DINA42 0x42
  279. #define DINA5A 0x5a
  280. #define DINA06 0x06
  281. #define DINA0E 0x0e
  282. #define DINA16 0x16
  283. #define DINA1E 0x1e
  284. #define DINA26 0x26
  285. #define DINA2E 0x2e
  286. #define DINA36 0x36
  287. #define DINA3E 0x3e
  288. #define DINA46 0x46
  289. #define DINA4E 0x4e
  290. #define DINA56 0x56
  291. #define DINA5E 0x5e
  292. #define DINA66 0x66
  293. #define DINA6E 0x6e
  294. #define DINA76 0x76
  295. #define DINA7E 0x7e
  296. #define DINA00 0x00
  297. #define DINA08 0x08
  298. #define DINA10 0x10
  299. #define DINA18 0x18
  300. #define DINA20 0x20
  301. #define DINA28 0x28
  302. #define DINA30 0x30
  303. #define DINA38 0x38
  304. #define DINA40 0x40
  305. #define DINA48 0x48
  306. #define DINA50 0x50
  307. #define DINA58 0x58
  308. #define DINA60 0x60
  309. #define DINA68 0x68
  310. #define DINA70 0x70
  311. #define DINA78 0x78
  312. #define DINA04 0x04
  313. #define DINA0C 0x0c
  314. #define DINA14 0x14
  315. #define DINA1C 0x1C
  316. #define DINA24 0x24
  317. #define DINA2C 0x2c
  318. #define DINA34 0x34
  319. #define DINA3C 0x3c
  320. #define DINA44 0x44
  321. #define DINA4C 0x4c
  322. #define DINA54 0x54
  323. #define DINA5C 0x5c
  324. #define DINA64 0x64
  325. #define DINA6C 0x6c
  326. #define DINA74 0x74
  327. #define DINA7C 0x7c
  328. #define DINA01 0x01
  329. #define DINA09 0x09
  330. #define DINA11 0x11
  331. #define DINA19 0x19
  332. #define DINA21 0x21
  333. #define DINA29 0x29
  334. #define DINA31 0x31
  335. #define DINA39 0x39
  336. #define DINA41 0x41
  337. #define DINA49 0x49
  338. #define DINA51 0x51
  339. #define DINA59 0x59
  340. #define DINA61 0x61
  341. #define DINA69 0x69
  342. #define DINA71 0x71
  343. #define DINA79 0x79
  344. #define DINA25 0x25
  345. #define DINA2D 0x2d
  346. #define DINA35 0x35
  347. #define DINA3D 0x3d
  348. #define DINA4D 0x4d
  349. #define DINA55 0x55
  350. #define DINA5D 0x5D
  351. #define DINA6D 0x6d
  352. #define DINA75 0x75
  353. #define DINA7D 0x7d
  354. #define DINADC 0xdc
  355. #define DINAF2 0xf2
  356. #define DINAAB 0xab
  357. #define DINAAA 0xaa
  358. #define DINAF1 0xf1
  359. #define DINADF 0xdf
  360. #define DINADA 0xda
  361. #define DINAB1 0xb1
  362. #define DINAB9 0xb9
  363. #define DINAF3 0xf3
  364. #define DINA8B 0x8b
  365. #define DINAA3 0xa3
  366. #define DINA91 0x91
  367. #define DINAB6 0xb6
  368. #define DINAB4 0xb4
  369. #define DINC00 0x00
  370. #define DINC01 0x01
  371. #define DINC02 0x02
  372. #define DINC03 0x03
  373. #define DINC08 0x08
  374. #define DINC09 0x09
  375. #define DINC0A 0x0a
  376. #define DINC0B 0x0b
  377. #define DINC10 0x10
  378. #define DINC11 0x11
  379. #define DINC12 0x12
  380. #define DINC13 0x13
  381. #define DINC18 0x18
  382. #define DINC19 0x19
  383. #define DINC1A 0x1a
  384. #define DINC1B 0x1b
  385. #define DINC20 0x20
  386. #define DINC21 0x21
  387. #define DINC22 0x22
  388. #define DINC23 0x23
  389. #define DINC28 0x28
  390. #define DINC29 0x29
  391. #define DINC2A 0x2a
  392. #define DINC2B 0x2b
  393. #define DINC30 0x30
  394. #define DINC31 0x31
  395. #define DINC32 0x32
  396. #define DINC33 0x33
  397. #define DINC38 0x38
  398. #define DINC39 0x39
  399. #define DINC3A 0x3a
  400. #define DINC3B 0x3b
  401. #define DINC40 0x40
  402. #define DINC41 0x41
  403. #define DINC42 0x42
  404. #define DINC43 0x43
  405. #define DINC48 0x48
  406. #define DINC49 0x49
  407. #define DINC4A 0x4a
  408. #define DINC4B 0x4b
  409. #define DINC50 0x50
  410. #define DINC51 0x51
  411. #define DINC52 0x52
  412. #define DINC53 0x53
  413. #define DINC58 0x58
  414. #define DINC59 0x59
  415. #define DINC5A 0x5a
  416. #define DINC5B 0x5b
  417. #define DINC60 0x60
  418. #define DINC61 0x61
  419. #define DINC62 0x62
  420. #define DINC63 0x63
  421. #define DINC68 0x68
  422. #define DINC69 0x69
  423. #define DINC6A 0x6a
  424. #define DINC6B 0x6b
  425. #define DINC70 0x70
  426. #define DINC71 0x71
  427. #define DINC72 0x72
  428. #define DINC73 0x73
  429. #define DINC78 0x78
  430. #define DINC79 0x79
  431. #define DINC7A 0x7a
  432. #define DINC7B 0x7b
  433. #define DIND40 0x40
  434. #define DINA80 0x80
  435. #define DINA90 0x90
  436. #define DINAA0 0xa0
  437. #define DINAC9 0xc9
  438. #define DINACB 0xcb
  439. #define DINACD 0xcd
  440. #define DINACF 0xcf
  441. #define DINAC8 0xc8
  442. #define DINACA 0xca
  443. #define DINACC 0xcc
  444. #define DINACE 0xce
  445. #define DINAD8 0xd8
  446. #define DINADD 0xdd
  447. #define DINAF8 0xf0
  448. #define DINAFE 0xfe
  449. #define DINBF8 0xf8
  450. #define DINAC0 0xb0
  451. #define DINAC1 0xb1
  452. #define DINAC2 0xb4
  453. #define DINAC3 0xb5
  454. #define DINAC4 0xb8
  455. #define DINAC5 0xb9
  456. #define DINBC0 0xc0
  457. #define DINBC2 0xc2
  458. #define DINBC4 0xc4
  459. #define DINBC6 0xc6
  460. #endif // DMPKEY_H__

(6) dmpmap.h

  1. /*
  2. $License:
  3. Copyright (C) 2011 InvenSense Corporation, All Rights Reserved.
  4. $
  5. */
  6. #ifndef DMPMAP_H
  7. #define DMPMAP_H
  8. #ifdef __cplusplus
  9. extern "C"
  10. {
  11. #endif
  12. #define DMP_PTAT 0
  13. #define DMP_XGYR 2
  14. #define DMP_YGYR 4
  15. #define DMP_ZGYR 6
  16. #define DMP_XACC 8
  17. #define DMP_YACC 10
  18. #define DMP_ZACC 12
  19. #define DMP_ADC1 14
  20. #define DMP_ADC2 16
  21. #define DMP_ADC3 18
  22. #define DMP_BIASUNC 20
  23. #define DMP_FIFORT 22
  24. #define DMP_INVGSFH 24
  25. #define DMP_INVGSFL 26
  26. #define DMP_1H 28
  27. #define DMP_1L 30
  28. #define DMP_BLPFSTCH 32
  29. #define DMP_BLPFSTCL 34
  30. #define DMP_BLPFSXH 36
  31. #define DMP_BLPFSXL 38
  32. #define DMP_BLPFSYH 40
  33. #define DMP_BLPFSYL 42
  34. #define DMP_BLPFSZH 44
  35. #define DMP_BLPFSZL 46
  36. #define DMP_BLPFMTC 48
  37. #define DMP_SMC 50
  38. #define DMP_BLPFMXH 52
  39. #define DMP_BLPFMXL 54
  40. #define DMP_BLPFMYH 56
  41. #define DMP_BLPFMYL 58
  42. #define DMP_BLPFMZH 60
  43. #define DMP_BLPFMZL 62
  44. #define DMP_BLPFC 64
  45. #define DMP_SMCTH 66
  46. #define DMP_0H2 68
  47. #define DMP_0L2 70
  48. #define DMP_BERR2H 72
  49. #define DMP_BERR2L 74
  50. #define DMP_BERR2NH 76
  51. #define DMP_SMCINC 78
  52. #define DMP_ANGVBXH 80
  53. #define DMP_ANGVBXL 82
  54. #define DMP_ANGVBYH 84
  55. #define DMP_ANGVBYL 86
  56. #define DMP_ANGVBZH 88
  57. #define DMP_ANGVBZL 90
  58. #define DMP_BERR1H 92
  59. #define DMP_BERR1L 94
  60. #define DMP_ATCH 96
  61. #define DMP_BIASUNCSF 98
  62. #define DMP_ACT2H 100
  63. #define DMP_ACT2L 102
  64. #define DMP_GSFH 104
  65. #define DMP_GSFL 106
  66. #define DMP_GH 108
  67. #define DMP_GL 110
  68. #define DMP_0_5H 112
  69. #define DMP_0_5L 114
  70. #define DMP_0_0H 116
  71. #define DMP_0_0L 118
  72. #define DMP_1_0H 120
  73. #define DMP_1_0L 122
  74. #define DMP_1_5H 124
  75. #define DMP_1_5L 126
  76. #define DMP_TMP1AH 128
  77. #define DMP_TMP1AL 130
  78. #define DMP_TMP2AH 132
  79. #define DMP_TMP2AL 134
  80. #define DMP_TMP3AH 136
  81. #define DMP_TMP3AL 138
  82. #define DMP_TMP4AH 140
  83. #define DMP_TMP4AL 142
  84. #define DMP_XACCW 144
  85. #define DMP_TMP5 146
  86. #define DMP_XACCB 148
  87. #define DMP_TMP8 150
  88. #define DMP_YACCB 152
  89. #define DMP_TMP9 154
  90. #define DMP_ZACCB 156
  91. #define DMP_TMP10 158
  92. #define DMP_DZH 160
  93. #define DMP_DZL 162
  94. #define DMP_XGCH 164
  95. #define DMP_XGCL 166
  96. #define DMP_YGCH 168
  97. #define DMP_YGCL 170
  98. #define DMP_ZGCH 172
  99. #define DMP_ZGCL 174
  100. #define DMP_YACCW 176
  101. #define DMP_TMP7 178
  102. #define DMP_AFB1H 180
  103. #define DMP_AFB1L 182
  104. #define DMP_AFB2H 184
  105. #define DMP_AFB2L 186
  106. #define DMP_MAGFBH 188
  107. #define DMP_MAGFBL 190
  108. #define DMP_QT1H 192
  109. #define DMP_QT1L 194
  110. #define DMP_QT2H 196
  111. #define DMP_QT2L 198
  112. #define DMP_QT3H 200
  113. #define DMP_QT3L 202
  114. #define DMP_QT4H 204
  115. #define DMP_QT4L 206
  116. #define DMP_CTRL1H 208
  117. #define DMP_CTRL1L 210
  118. #define DMP_CTRL2H 212
  119. #define DMP_CTRL2L 214
  120. #define DMP_CTRL3H 216
  121. #define DMP_CTRL3L 218
  122. #define DMP_CTRL4H 220
  123. #define DMP_CTRL4L 222
  124. #define DMP_CTRLS1 224
  125. #define DMP_CTRLSF1 226
  126. #define DMP_CTRLS2 228
  127. #define DMP_CTRLSF2 230
  128. #define DMP_CTRLS3 232
  129. #define DMP_CTRLSFNLL 234
  130. #define DMP_CTRLS4 236
  131. #define DMP_CTRLSFNL2 238
  132. #define DMP_CTRLSFNL 240
  133. #define DMP_TMP30 242
  134. #define DMP_CTRLSFJT 244
  135. #define DMP_TMP31 246
  136. #define DMP_TMP11 248
  137. #define DMP_CTRLSF2_2 250
  138. #define DMP_TMP12 252
  139. #define DMP_CTRLSF1_2 254
  140. #define DMP_PREVPTAT 256
  141. #define DMP_ACCZB 258
  142. #define DMP_ACCXB 264
  143. #define DMP_ACCYB 266
  144. #define DMP_1HB 272
  145. #define DMP_1LB 274
  146. #define DMP_0H 276
  147. #define DMP_0L 278
  148. #define DMP_ASR22H 280
  149. #define DMP_ASR22L 282
  150. #define DMP_ASR6H 284
  151. #define DMP_ASR6L 286
  152. #define DMP_TMP13 288
  153. #define DMP_TMP14 290
  154. #define DMP_FINTXH 292
  155. #define DMP_FINTXL 294
  156. #define DMP_FINTYH 296
  157. #define DMP_FINTYL 298
  158. #define DMP_FINTZH 300
  159. #define DMP_FINTZL 302
  160. #define DMP_TMP1BH 304
  161. #define DMP_TMP1BL 306
  162. #define DMP_TMP2BH 308
  163. #define DMP_TMP2BL 310
  164. #define DMP_TMP3BH 312
  165. #define DMP_TMP3BL 314
  166. #define DMP_TMP4BH 316
  167. #define DMP_TMP4BL 318
  168. #define DMP_STXG 320
  169. #define DMP_ZCTXG 322
  170. #define DMP_STYG 324
  171. #define DMP_ZCTYG 326
  172. #define DMP_STZG 328
  173. #define DMP_ZCTZG 330
  174. #define DMP_CTRLSFJT2 332
  175. #define DMP_CTRLSFJTCNT 334
  176. #define DMP_PVXG 336
  177. #define DMP_TMP15 338
  178. #define DMP_PVYG 340
  179. #define DMP_TMP16 342
  180. #define DMP_PVZG 344
  181. #define DMP_TMP17 346
  182. #define DMP_MNMFLAGH 352
  183. #define DMP_MNMFLAGL 354
  184. #define DMP_MNMTMH 356
  185. #define DMP_MNMTML 358
  186. #define DMP_MNMTMTHRH 360
  187. #define DMP_MNMTMTHRL 362
  188. #define DMP_MNMTHRH 364
  189. #define DMP_MNMTHRL 366
  190. #define DMP_ACCQD4H 368
  191. #define DMP_ACCQD4L 370
  192. #define DMP_ACCQD5H 372
  193. #define DMP_ACCQD5L 374
  194. #define DMP_ACCQD6H 376
  195. #define DMP_ACCQD6L 378
  196. #define DMP_ACCQD7H 380
  197. #define DMP_ACCQD7L 382
  198. #define DMP_ACCQD0H 384
  199. #define DMP_ACCQD0L 386
  200. #define DMP_ACCQD1H 388
  201. #define DMP_ACCQD1L 390
  202. #define DMP_ACCQD2H 392
  203. #define DMP_ACCQD2L 394
  204. #define DMP_ACCQD3H 396
  205. #define DMP_ACCQD3L 398
  206. #define DMP_XN2H 400
  207. #define DMP_XN2L 402
  208. #define DMP_XN1H 404
  209. #define DMP_XN1L 406
  210. #define DMP_YN2H 408
  211. #define DMP_YN2L 410
  212. #define DMP_YN1H 412
  213. #define DMP_YN1L 414
  214. #define DMP_YH 416
  215. #define DMP_YL 418
  216. #define DMP_B0H 420
  217. #define DMP_B0L 422
  218. #define DMP_A1H 424
  219. #define DMP_A1L 426
  220. #define DMP_A2H 428
  221. #define DMP_A2L 430
  222. #define DMP_SEM1 432
  223. #define DMP_FIFOCNT 434
  224. #define DMP_SH_TH_X 436
  225. #define DMP_PACKET 438
  226. #define DMP_SH_TH_Y 440
  227. #define DMP_FOOTER 442
  228. #define DMP_SH_TH_Z 444
  229. #define DMP_TEMP29 448
  230. #define DMP_TEMP30 450
  231. #define DMP_XACCB_PRE 452
  232. #define DMP_XACCB_PREL 454
  233. #define DMP_YACCB_PRE 456
  234. #define DMP_YACCB_PREL 458
  235. #define DMP_ZACCB_PRE 460
  236. #define DMP_ZACCB_PREL 462
  237. #define DMP_TMP22 464
  238. #define DMP_TAP_TIMER 466
  239. #define DMP_TAP_THX 468
  240. #define DMP_TAP_THY 472
  241. #define DMP_TAP_THZ 476
  242. #define DMP_TAPW_MIN 478
  243. #define DMP_TMP25 480
  244. #define DMP_TMP26 482
  245. #define DMP_TMP27 484
  246. #define DMP_TMP28 486
  247. #define DMP_ORIENT 488
  248. #define DMP_THRSH 490
  249. #define DMP_ENDIANH 492
  250. #define DMP_ENDIANL 494
  251. #define DMP_BLPFNMTCH 496
  252. #define DMP_BLPFNMTCL 498
  253. #define DMP_BLPFNMXH 500
  254. #define DMP_BLPFNMXL 502
  255. #define DMP_BLPFNMYH 504
  256. #define DMP_BLPFNMYL 506
  257. #define DMP_BLPFNMZH 508
  258. #define DMP_BLPFNMZL 510
  259. #ifdef __cplusplus
  260. }
  261. #endif
  262. #endif // DMPMAP_H

2、下面是MPU6500低层通信函数,如I2C读写,直接读写MPU6500寄存器,还有初始化函数

(1) mpu6500_driver.c

  1. #include "mpu6500_driver.h"
  2. #include "delay.h"
  3. #include "inv_mpu.h"
  4. #include "inv_mpu_dmp_motion_driver.h"
  5. #include "math_fun.h"
  6. #include "math.h"
  7. u8 MPU_EXTI_flag=0; //MPU6500引脚中断
  8. /* Platform-specific information. Kinda like a boardfile. */
  9. struct platform_data_s {
  10. signed char orientation[9];
  11. };
  12. /* The sensors can be mounted onto the board in any orientation. The mounting
  13. * matrix seen below tells the MPL how to rotate the raw data from the
  14. * driver(s).
  15. * TODO: The following matrices refer to the configuration on internal test
  16. * boards at Invensense. If needed, please modify the matrices to match the
  17. * chip-to-body matrix for your particular set up.
  18. */
  19. static struct platform_data_s gyro_pdata = {
  20. .orientation = {0, -1, 0,
  21. 1, 0, 0,
  22. 0, 0, 1}
  23. };
  24. /* Axis Transformation matrix
  25. |r11 r12 r13| |vx| |v'x|
  26. |r21 r22 r23| |vy| = |v'y|
  27. |r31 r32 r33| |vz| |v'z|
  28. v'x = {(r11 * vx) +( r12 * vy) +( r13 * vz)}
  29. v'y = {(r21 * vx) +( r22 * vy) +( r23 * vz)}
  30. v'z = {(r31 * vx) +( r32 * vy) +( r33 * vz)}
  31. */
  32. void MPU6500_Port_EXIT_Init(void)
  33. {
  34. GPIO_InitTypeDef GPIO_InitStrue;
  35. EXTI_InitTypeDef EXTI_InitStrue;
  36. NVIC_InitTypeDef NVIC_InitStrue;
  37. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOB,ENABLE);
  38. GPIO_InitStrue.GPIO_Mode=GPIO_Mode_IPU;
  39. GPIO_InitStrue.GPIO_Pin=GPIO_Pin_14;
  40. GPIO_Init(GPIOB,&GPIO_InitStrue);
  41. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource14);
  42. EXTI_InitStrue.EXTI_Line=EXTI_Line14;
  43. EXTI_InitStrue.EXTI_LineCmd=ENABLE;
  44. EXTI_InitStrue.EXTI_Mode=EXTI_Mode_Interrupt;
  45. EXTI_InitStrue.EXTI_Trigger=EXTI_Trigger_Falling;
  46. EXTI_Init(&EXTI_InitStrue);
  47. NVIC_InitStrue.NVIC_IRQChannel=EXTI15_10_IRQn;
  48. NVIC_InitStrue.NVIC_IRQChannelCmd=ENABLE;
  49. NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority=1;
  50. NVIC_InitStrue.NVIC_IRQChannelSubPriority=2;
  51. NVIC_Init(&NVIC_InitStrue);
  52. }
  53. void MPU6500_I2C_PORT_Init(void)
  54. {
  55. GPIO_InitTypeDef GPIO_InitStructure;
  56. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  57. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  58. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12|GPIO_Pin_13;
  59. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  60. GPIO_Init(GPIOB,&GPIO_InitStructure);
  61. GPIO_SetBits(GPIOB,GPIO_Pin_12|GPIO_Pin_13);
  62. }
  63. void MPU6500_I2C_delay(void)
  64. {
  65. u16 i;
  66. /* 
  67. 下面的时间是通过逻辑分析仪测试得到的。
  68. 工作条件:CPU主频72MHz ,MDK编译环境,1级优化
  69. 循环次数为10时,SCL频率 = 205KHz
  70. 循环次数为7时,SCL频率 = 347KHz, SCL高电平时间1.5us,SCL低电平时间2.87us
  71. 循环次数为5时,SCL频率 = 421KHz, SCL高电平时间1.25us,SCL低电平时间2.375us
  72. */
  73. for(i=0;i<5;i++);
  74. }
  75. void MPU6500_I2C_start(void)
  76. {
  77. MPU6500_I2C_SCL=0;
  78. MPU6500_I2C_SetOut_Mode();
  79. MPU6500_I2C_SDA_WR=1;
  80. MPU6500_I2C_SCL=1;
  81. MPU6500_I2C_delay();
  82. MPU6500_I2C_SDA_WR=0;
  83. MPU6500_I2C_SCL=0;
  84. MPU6500_I2C_delay();
  85. }
  86. void MPU6500_I2C_stop(void)
  87. {
  88. MPU6500_I2C_SCL=0;
  89. MPU6500_I2C_SetOut_Mode();
  90. MPU6500_I2C_SDA_WR=0;
  91. MPU6500_I2C_SCL=1;
  92. MPU6500_I2C_delay();
  93. MPU6500_I2C_SDA_WR=1;
  94. MPU6500_I2C_delay();
  95. }
  96. u8 MPU6500_I2C_check_ack(void)
  97. {
  98. u16 delay_count=0;
  99. MPU6500_I2C_SCL=0;
  100. MPU6500_I2C_SetIn_Mode();
  101. MPU6500_I2C_SDA_WR=1;
  102. MPU6500_I2C_SCL=1;
  103. MPU6500_I2C_delay();
  104. while( MPU6500_I2C_SDA_RE )
  105. {
  106. delay_count++;
  107. if(delay_count>0x0fff)
  108. {
  109. MPU6500_I2C_stop();
  110. return 1;
  111. }
  112. }
  113. MPU6500_I2C_SCL=0;
  114. MPU6500_I2C_delay();
  115. return 0;
  116. }
  117. void MPU6500_I2C_ack(void)
  118. {
  119. MPU6500_I2C_SCL=0;
  120. MPU6500_I2C_SetOut_Mode();
  121. MPU6500_I2C_SDA_WR=0;
  122. MPU6500_I2C_SCL=1;
  123. MPU6500_I2C_delay();
  124. MPU6500_I2C_SCL=0;
  125. MPU6500_I2C_delay();
  126. MPU6500_I2C_SDA_WR=1;
  127. }
  128. void MPU6500_I2C_NoAck(void)
  129. {
  130. MPU6500_I2C_SCL=0;
  131. MPU6500_I2C_SetOut_Mode();
  132. MPU6500_I2C_SDA_WR=1;
  133. MPU6500_I2C_delay();
  134. MPU6500_I2C_SCL=1;
  135. MPU6500_I2C_delay();
  136. MPU6500_I2C_SCL=0;
  137. MPU6500_I2C_delay();
  138. }
  139. void MPU6500_I2C_write_char(u8 dat)
  140. {
  141. u8 i=0;
  142. MPU6500_I2C_SCL=0;
  143. MPU6500_I2C_SetOut_Mode();
  144. for(i=0;i<8;i++)
  145. {
  146. if(dat&0x80) MPU6500_I2C_SDA_WR=1;
  147. else MPU6500_I2C_SDA_WR=0;
  148. MPU6500_I2C_delay();
  149. MPU6500_I2C_SCL=1;
  150. MPU6500_I2C_delay();
  151. MPU6500_I2C_SCL=0;
  152. dat<<=1;
  153. }
  154. }
  155. u8 MPU6500_I2C_read_char(void)
  156. {
  157. u8 i=0,dat=0;
  158. MPU6500_I2C_SCL=0;
  159. MPU6500_I2C_SetIn_Mode();
  160. for(i=0;i<8;i++)
  161. {
  162. dat<<=1;
  163. MPU6500_I2C_SCL=1;
  164. MPU6500_I2C_delay();
  165. if(MPU6500_I2C_SDA_RE) dat|=0x01;
  166. MPU6500_I2C_SCL=0;
  167. MPU6500_I2C_delay();
  168. }
  169. return dat;
  170. }
  171. u8 MPU6500_write_byte(u8 reg,u8 data)
  172. {
  173. MPU6500_I2C_start();
  174. MPU6500_I2C_write_char(MPU6500_device_addr<<1 | 0x00);
  175. if(MPU6500_I2C_check_ack())
  176. {
  177. MPU6500_I2C_stop();
  178. return 2;
  179. }
  180. MPU6500_I2C_write_char(reg);
  181. MPU6500_I2C_check_ack();
  182. MPU6500_I2C_write_char(data);
  183. MPU6500_I2C_check_ack();
  184. MPU6500_I2C_stop();
  185. return 0;
  186. }
  187. u8 MPU6500_read_byte(u8 reg)
  188. {
  189. u8 data=0;
  190. MPU6500_I2C_start();
  191. MPU6500_I2C_write_char(MPU6500_device_addr<<1 | 0x00);
  192. if(MPU6500_I2C_check_ack())
  193. {
  194. MPU6500_I2C_stop();
  195. return 2;
  196. }
  197. MPU6500_I2C_write_char(reg);
  198. MPU6500_I2C_check_ack();
  199. MPU6500_I2C_start();
  200. MPU6500_I2C_write_char(MPU6500_device_addr<<1 | 0x01);
  201. MPU6500_I2C_check_ack();
  202. data=MPU6500_I2C_read_char();
  203. MPU6500_I2C_ack();
  204. MPU6500_I2C_stop();
  205. return data;
  206. }
  207. u8 MPU6500_Read_Len(u8 DeviceAddr,u8 RegAddr,u8 len,u8 *pbuff)
  208. {
  209. u8 i;
  210. MPU6500_I2C_start();
  211. MPU6500_I2C_write_char(DeviceAddr<<1 | 0x00 );
  212. if(MPU6500_I2C_check_ack())
  213. {
  214. MPU6500_I2C_stop();
  215. return 2;
  216. }
  217. MPU6500_I2C_write_char(RegAddr);
  218. MPU6500_I2C_check_ack();
  219. MPU6500_I2C_start();
  220. MPU6500_I2C_write_char(DeviceAddr<<1 | 0x01);
  221. MPU6500_I2C_check_ack();
  222. for(i=0;i<len;i++)
  223. {
  224. *pbuff++ =MPU6500_I2C_read_char();
  225. if(i+1>=len) {MPU6500_I2C_NoAck(); break;}
  226. MPU6500_I2C_ack();
  227. }
  228. MPU6500_I2C_stop();
  229. return 0;
  230. }
  231. u8 MPU6500_Write_Len(u8 DeviceAddr,u8 RegAddr,u8 len,u8 *pbuff)
  232. {
  233. u8 i;
  234. MPU6500_I2C_start();
  235. MPU6500_I2C_write_char(DeviceAddr<<1 | 0x00);
  236. if(MPU6500_I2C_check_ack())
  237. {
  238. MPU6500_I2C_stop();
  239. return 2;
  240. }
  241. MPU6500_I2C_write_char(RegAddr);
  242. MPU6500_I2C_check_ack();
  243. for(i=0;i<len;i++)
  244. {
  245. MPU6500_I2C_write_char(*pbuff++);
  246. if(MPU6500_I2C_check_ack())
  247. {
  248. MPU6500_I2C_stop();
  249. return 3;
  250. }
  251. }
  252. MPU6500_I2C_stop();
  253. return 0;
  254. }
  255. u8 InitMPU6050(void)
  256. {
  257. int i=0,j=0;
  258. u8 res=0;
  259. MPU6500_I2C_PORT_Init();
  260. //在初始化之前要延时一段时间,若没有延时,则断电后再上电数据可能会出错
  261. for(i=0;i<1000;i++)
  262. {
  263. for(j=0;j<1000;j++)
  264. {
  265. ;
  266. }
  267. }
  268. MPU6500_write_byte(PWR_MGMT_1,0x80);//复位MPU6050
  269. Delay_ms(100);
  270. MPU6500_write_byte(PWR_MGMT_1,0);唤醒MPU6050
  271. MPU6500_Set_Gyro_Fsr(3); //陀螺仪传感器,±2000dps
  272. MPU6500_Set_Accel_Fsr(0); //加速度传感器,±2g
  273. MPU6500_Set_Rate(50); //设置采样率50Hz
  274. MPU6500_write_byte(INT_EN_REG,0X00); //关闭所有中断
  275. MPU6500_write_byte(USER_CTRL_REG,0X00); //I2C主模式关闭
  276. MPU6500_write_byte(FIFO_EN_REG,0X00); //关闭FIFO
  277. MPU6500_write_byte(INTBP_CFG_REG,0X80); //INT引脚低电平有效
  278. res=MPU6500_read_byte(WHO_AM_I);
  279. if(res==MPU6500_ID)
  280. {
  281. MPU6500_write_byte(PWR_MGMT_1,0X01); //设置CLKSEL,PLL X轴为参考
  282. MPU6500_write_byte(PWR_MGMT_2,0X00); //加速度与陀螺仪都工作
  283. MPU6500_Set_Rate(50); //设置采样率为50Hz
  284. }
  285. else return 1;
  286. /*
  287. temp=0x080;
  288. MPU6500_Write_Len(MPU6500_device_addr,PWR_MGMT_1,1, &temp); //解除休眠状态
  289. temp=0x00;
  290. MPU6500_Write_Len(MPU6500_device_addr,PWR_MGMT_1,1, &temp); //解除休眠状态
  291. MPU6500_Write_Len(MPU6500_device_addr,SMPLRT_DIV,1, &temp);
  292. temp=0x06;
  293. MPU6500_Write_Len(MPU6500_device_addr,CONFIG, 1,&temp);
  294. temp=0x00;
  295. MPU6500_Write_Len(MPU6500_device_addr,ACCEL_CONFIG,1, &temp);
  296. temp=0x18;
  297. MPU6500_Write_Len(MPU6500_device_addr,GYRO_CONFIG,1, &temp);
  298. */
  299. return 0;
  300. }
  301. short GetData(u8 REG_Address)
  302. {
  303. u8 buff[2];
  304. MPU6500_Read_Len(MPU6500_device_addr,REG_Address,2,buff);
  305. return ((buff[0]<<8)|buff[1]); //合成数据
  306. }
  307. //得到加速度值(原始值)
  308. //gx,gy,gz:陀螺仪x,y,z轴的原始读数(带符号)
  309. //返回值:0,成功
  310. // 其他,错误代码
  311. u8 MPU6500_Get_Accelerometer(short *ax,short *ay,short *az)
  312. {
  313. u8 result;
  314. u8 acecel[6];
  315. result=MPU6500_Read_Len(MPU6500_device_addr,ACCEL_XOUT_H,6,acecel);
  316. if(result==0)
  317. {
  318. *ax=((u16)acecel[0]<<8)|acecel[1];
  319. *ay=((u16)acecel[2]<<8)|acecel[3];
  320. *az=((u16)acecel[4]<<8)|acecel[5];
  321. }
  322. return result;
  323. }
  324. //得到陀螺仪值(原始值)
  325. //gx,gy,gz:陀螺仪x,y,z轴的原始读数(带符号)
  326. //返回值:0,成功
  327. // 其他,错误代码
  328. u8 MPU6500_Get_Gyroscope(short *gx,short *gy,short *gz)
  329. {
  330. u8 result;
  331. u8 gyro[6];
  332. result=MPU6500_Read_Len(MPU6500_device_addr,GYRO_XOUT_H,6,gyro);
  333. if(result==0)
  334. {
  335. *gx=((u16)gyro[0]<<8)|gyro[1];
  336. *gy=((u16)gyro[2]<<8)|gyro[3];
  337. *gz=((u16)gyro[4]<<8)|gyro[5];
  338. }
  339. return result;
  340. }
  341. float MPU6500_GetTemperature(void)
  342. {
  343. short temp;
  344. float dat;
  345. temp=GetData(TEMP_OUT_H);
  346. dat=((double) (temp-21)/333.87)+21;
  347. return dat;
  348. }
  349. //设置MPU6050陀螺仪传感器满量程范围
  350. //fsr:0250dps;1500dps;21000dps;32000dps
  351. //返回值:0,设置成功
  352. // 其他,设置失败
  353. u8 MPU6500_Set_Gyro_Fsr(u8 fsr)
  354. {
  355. return MPU6500_write_byte(GYRO_CONFIG,fsr<<3);//设置陀螺仪满量程范围
  356. }
  357. //设置MPU6050加速度传感器满量程范围
  358. //fsr:02g;14g;28g;316g
  359. //返回值:0,设置成功
  360. // 其他,设置失败
  361. u8 MPU6500_Set_Accel_Fsr(u8 fsr)
  362. {
  363. return MPU6500_write_byte(ACCEL_CONFIG,fsr<<3);//设置加速度传感器满量程范围
  364. }
  365. //设置MPU6050的采样率(假定Fs=1KHz)
  366. //rate:4~1000(Hz)
  367. //返回值:0,设置成功
  368. // 其他,设置失败
  369. u8 MPU6500_Set_Rate(u16 rate)
  370. {
  371. u8 data;
  372. if(rate>1000)rate=1000;
  373. if(rate<4)rate=4;
  374. data=1000/rate-1;
  375. data=MPU6500_write_byte(SMPLRT_DIV,data); //设置数字低通滤波器
  376. return MPU6500_Set_LPF(rate/2); //自动设置LPF为采样率的一半
  377. }
  378. //设置MPU6050的数字低通滤波器
  379. //lpf:数字低通滤波频率(Hz)
  380. //返回值:0,设置成功
  381. // 其他,设置失败
  382. u8 MPU6500_Set_LPF(u16 lpf)
  383. {
  384. u8 data=0;
  385. if(lpf>=188)data=1;
  386. else if(lpf>=98)data=2;
  387. else if(lpf>=42)data=3;
  388. else if(lpf>=20)data=4;
  389. else if(lpf>=10)data=5;
  390. else data=6;
  391. return MPU6500_write_byte(CONFIG,data);//设置数字低通滤波器
  392. }
  393. //MPU6050自测试
  394. //返回值:0,正常
  395. // 其他,失败
  396. u8 MPU6500_run_self_test(void)
  397. {
  398. int result;
  399. //char test_packet[4] = {0};
  400. long gyro[3], accel[3];
  401. result = mpu_run_self_test(gyro, accel);
  402. if (result == 0x07)
  403. {
  404. /* Test passed. We can trust the gyro data here, so let's push it down
  405. * to the DMP.
  406. */
  407. float gyro_sens;
  408. unsigned short accel_sens;
  409. mpu_get_gyro_sens(&gyro_sens);
  410. gyro[0] = (long)(gyro[0] * gyro_sens);
  411. gyro[1] = (long)(gyro[1] * gyro_sens);
  412. gyro[2] = (long)(gyro[2] * gyro_sens);
  413. dmp_set_gyro_bias(gyro);
  414. mpu_get_accel_sens(&accel_sens);
  415. accel[0] *= accel_sens;
  416. accel[1] *= accel_sens;
  417. accel[2] *= accel_sens;
  418. dmp_set_accel_bias(accel);
  419. return 0;
  420. }else return 1;
  421. }
  422. u8 MPU6500_DMP_Init(void)
  423. {
  424. struct int_param_s int_param;
  425. int result;
  426. result=mpu_init(&int_param);
  427. if(result) return 1;
  428. result=mpu_set_sensors(INV_XYZ_GYRO|INV_XYZ_ACCEL);//设置所需要的传感器
  429. if(result) return 2;
  430. result=mpu_configure_fifo(INV_XYZ_GYRO|INV_XYZ_ACCEL);//设置FIFO
  431. if(result) return 3;
  432. result=mpu_set_sample_rate(DEFAULT_MPU_HZ);//设置采样率
  433. if(result) return 4;
  434. result=dmp_load_motion_driver_firmware();//加载dmp固件
  435. if(result) return 5;
  436. result=dmp_set_orientation(inv_orientation_matrix_to_scalar(gyro_pdata.orientation));//设置陀螺仪方向
  437. if(result) return 6;
  438. result=dmp_enable_feature(DMP_FEATURE_TAP|DMP_FEATURE_ANDROID_ORIENT|
  439. DMP_FEATURE_6X_LP_QUAT|DMP_FEATURE_GYRO_CAL|
  440. DMP_FEATURE_SEND_RAW_ACCEL|DMP_FEATURE_SEND_RAW_GYRO);//设置dmp功能
  441. if(result) return 7;
  442. result=dmp_set_fifo_rate(DEFAULT_MPU_HZ);//设置DMP输出速率(最大不超过200Hz)
  443. if(result) return 8;
  444. result=MPU6500_run_self_test(); //自检
  445. if(result) return 9;
  446. result=mpu_set_dmp_state(1);//使能DMP
  447. if(result) return 10;
  448. result=dmp_set_interrupt_mode(DMP_INT_CONTINUOUS);//设置中断产生方式
  449. if(result) return 11;
  450. return 0;
  451. }
  452. //得到dmp处理后的数据(注意,本函数需要比较多堆栈,局部变量有点多)
  453. //pitch:俯仰角 精度:0.1° 范围:-90.0° <---> +90.0°
  454. //roll:横滚角 精度:0.1° 范围:-180.0°<---> +180.0°
  455. //yaw:航向角 精度:0.1° 范围:-180.0°<---> +180.0°
  456. //返回值:0,正常
  457. // 其他,失败
  458. u8 MPU6500_dmp_get_euler_angle(short *accel,short *gyro,float *pitch,float *roll,float *yaw)
  459. {
  460. //q30格式,long转float时的除数.
  461. #define Q30 ((1<<30)*1.0f)
  462. float q0=1.0f,q1=0.0f,q2=0.0f,q3=0.0f;
  463. unsigned long sensor_timestamp;
  464. short sensors;
  465. unsigned char more;
  466. long quat[4];
  467. u8 result=0;
  468. result=dmp_read_fifo(gyro, accel, quat, &sensor_timestamp, &sensors,&more);
  469. if(result)return 1;
  470. /* Gyro and accel data are written to the FIFO by the DMP in chip frame and hardware units.
  471. * This behavior is convenient because it keeps the gyro and accel outputs of dmp_read_fifo and mpu_read_fifo consistent.
  472. **/
  473. /*if (sensors & INV_XYZ_GYRO )
  474. send_packet(PACKET_TYPE_GYRO, gyro);
  475. if (sensors & INV_XYZ_ACCEL)
  476. send_packet(PACKET_TYPE_ACCEL, accel); */
  477. /* Unlike gyro and accel, quaternions are written to the FIFO in the body frame, q30.
  478. * The orientation is set by the scalar passed to dmp_set_orientation during initialization.
  479. **/
  480. if(sensors&INV_WXYZ_QUAT)
  481. {
  482. q0 = quat[0] / Q30; //q30格式转换为浮点数
  483. q1 = quat[1] / Q30;
  484. q2 = quat[2] / Q30;
  485. q3 = quat[3] / Q30;
  486. //计算得到俯仰角/横滚角/航向角
  487. *pitch = asin(-2 * q1 * q3 + 2 * q0* q2)* 57.3; // pitch
  488. *roll = atan2(2 * q2 * q3 + 2 * q0 * q1, -2 * q1 * q1 - 2 * q2* q2 + 1)* 57.3; // roll
  489. *yaw = atan2(2*(q1*q2 + q0*q3),q0*q0+q1*q1-q2*q2-q3*q3) * 57.3; //yaw
  490. }else return 2;
  491. return 0;
  492. }

(2) mpu6500_driver.h

  1. #ifndef __MPU6500_DRIVER_H
  2. #define __MPU6500_DRIVER_H
  3. #include "stm32f10x.h"
  4. #include "sys.h"
  5. #define MPU6500_I2C_SDA_RE PBin(13)
  6. #define MPU6500_I2C_SDA_WR PBout(13)
  7. #define MPU6500_I2C_SCL PBout(12)
  8. #define MPU6500_I2C_SetOut_Mode() {GPIOB->CRH&=0XFF0FFFFF;GPIOB->CRH|=0X00300000;}
  9. #define MPU6500_I2C_SetIn_Mode() {GPIOB->CRH&=0XFF0FFFFF;GPIOB->CRH|=0X00800000;GPIOB->ODR|=0X01<13;}
  10. #define MPU6500_device_addr 0x68
  11. #define MPU6500_ID 0x70
  12. #define DEFAULT_MPU_HZ 100 //100Hz
  13. //****************************************
  14. // 定义MPU6050内部地址
  15. //****************************************
  16. #define SMPLRT_DIV 0x19 //陀螺仪采样率,典型值:0x07(125Hz)
  17. #define CONFIG 0x1A //低通滤波频率,典型值:0x06(5Hz)
  18. #define GYRO_CONFIG 0x1B //陀螺仪自检及测量范围,典型值:0x18(不自检,2000deg/s)
  19. #define ACCEL_CONFIG 0x1C //加速计自检、测量范围及高通滤波频率,典型值:0x01(不自检,2G,5Hz)
  20. #define FIFO_EN_REG 0X23 //FIFO使能寄存器
  21. #define INTBP_CFG_REG 0X37 //中断/旁路设置寄存器
  22. #define INT_EN_REG 0X38 //中断使能寄存器
  23. #define ACCEL_XOUT_H 0x3B
  24. #define ACCEL_XOUT_L 0x3C
  25. #define ACCEL_YOUT_H 0x3D
  26. #define ACCEL_YOUT_L 0x3E
  27. #define ACCEL_ZOUT_H 0x3F
  28. #define ACCEL_ZOUT_L 0x40
  29. #define TEMP_OUT_H 0x41
  30. #define TEMP_OUT_L 0x42
  31. #define GYRO_XOUT_H 0x43
  32. #define GYRO_XOUT_L 0x44
  33. #define GYRO_YOUT_H 0x45
  34. #define GYRO_YOUT_L 0x46
  35. #define GYRO_ZOUT_H 0x47
  36. #define GYRO_ZOUT_L 0x48
  37. #define USER_CTRL_REG 0X6A //用户控制寄存器
  38. #define PWR_MGMT_1 0x6B //电源管理,典型值:0x00(正常启用)
  39. #define PWR_MGMT_2 0X6C //电源管理寄存器2
  40. #define WHO_AM_I 0x75 //IIC地址寄存器(默认数值0x68,只读)
  41. #define SlaveAddress 0xD0 //IIC写入时的地址字节数据,+1为读取
  42. extern u8 MPU_EXTI_flag;
  43. void MPU6500_Port_EXIT_Init(void);
  44. void MPU6500_I2C_PORT_Init(void);
  45. void MPU6500_I2C_delay(void);
  46. void MPU6500_I2C_start(void);
  47. void MPU6500_I2C_stop(void);
  48. u8 MPU6500_I2C_check_ack(void);
  49. void MPU6500_I2C_ack(void);
  50. void MPU6500_I2C_NoAck(void);
  51. void MPU6500_I2C_write_char(u8 dat);
  52. u8 MPU6500_I2C_read_char(void);
  53. u8 MPU6500_write_byte(u8 reg,u8 data);
  54. u8 MPU6500_read_byte(u8 reg);
  55. u8 MPU6500_Read_Len(u8 DeviceAddr,u8 RegAddr,u8 len,u8 *pbuff);
  56. u8 MPU6500_Write_Len(u8 DeviceAddr,u8 RegAddr,u8 len,u8 *pbuff);
  57. u8 InitMPU6050(void);
  58. short GetData(u8 REG_Address);
  59. u8 MPU6500_Get_Accelerometer(short *ax,short *ay,short *az);
  60. u8 MPU6500_Get_Gyroscope(short *gx,short *gy,short *gz);
  61. float MPU6500_GetTemperature(void);
  62. u8 MPU6500_Set_Gyro_Fsr(u8 fsr);
  63. u8 MPU6500_Set_Accel_Fsr(u8 fsr);
  64. u8 MPU6500_Set_Rate(u16 rate);
  65. u8 MPU6500_Set_LPF(u16 lpf);
  66. u8 MPU6500_run_self_test(void);
  67. u8 MPU6500_DMP_Init(void);
  68. u8 MPU6500_dmp_get_euler_angle(short *accel,short *gyro,float *pitch,float *roll,float *yaw);
  69. #endif

3、接下来是两个关于计算方向矩阵的函数,是从DMP6.12版复制过来的,在DMP库设置设计方向矩阵时调用

(1) math_fun.c

  1. #include "math_fun.h"
  2. /**
  3. *此文件包含计算MPU6XXX方向矩阵的数学计算函数
  4. *
  5. *
  6. *
  7. *
  8. **/
  9. unsigned short inv_row_2_scale(const signed char *row)
  10. {
  11. unsigned short b;
  12. if (row[0] > 0)
  13. b = 0;
  14. else if (row[0] < 0)
  15. b = 4;
  16. else if (row[1] > 0)
  17. b = 1;
  18. else if (row[1] < 0)
  19. b = 5;
  20. else if (row[2] > 0)
  21. b = 2;
  22. else if (row[2] < 0)
  23. b = 6;
  24. else
  25. b = 7; // error
  26. return b;
  27. }
  28. /** Converts an orientation matrix made up of 0,+1,and -1 to a scalar representation.
  29. * @param[in] mtx Orientation matrix to convert to a scalar.
  30. * @return Description of orientation matrix. The lowest 2 bits (0 and 1) represent the column the one is on for the
  31. * first row, with the bit number 2 being the sign. The next 2 bits (3 and 4) represent
  32. * the column the one is on for the second row with bit number 5 being the sign.
  33. * The next 2 bits (6 and 7) represent the column the one is on for the third row with
  34. * bit number 8 being the sign. In binary the identity matrix would therefor be:
  35. * 010_001_000 or 0x88 in hex.
  36. */
  37. unsigned short inv_orientation_matrix_to_scalar(const signed char *mtx)
  38. {
  39. unsigned short scalar;
  40. /*
  41. XYZ 010_001_000 Identity Matrix
  42. XZY 001_010_000
  43. YXZ 010_000_001
  44. YZX 000_010_001
  45. ZXY 001_000_010
  46. ZYX 000_001_010
  47. */
  48. scalar = inv_row_2_scale(mtx);
  49. scalar |= inv_row_2_scale(mtx + 3) << 3;
  50. scalar |= inv_row_2_scale(mtx + 6) << 6;
  51. return scalar;
  52. }

(2) math_fun.h

  1. #ifndef __MATH_FUN_H
  2. #define __MATH_FUN_H
  3. unsigned short inv_row_2_scale(const signed char *row);
  4. unsigned short inv_orientation_matrix_to_scalar(const signed char *mtx);
  5. #endif

4、向上位机“匿名四轴”2.6版发送数据的函数,这些函数的功能是,将加速度,角速度,航向角,俯仰角,横滚角,按照上位机的格式要求,将数据组装后通过串口发送到上位机,格式要求可打开“匿名四轴”2.6版,按F12查看帮助。

(1) send_to_client.c

  1. #include "send_to_client.h"
  2. //串口1发送1个字符
  3. //c:要发送的字符
  4. void usart1_send_char(u8 c)
  5. {
  6. while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET); //循环发送,直到发送完毕
  7. USART_SendData(USART1,c);
  8. }
  9. //传送数据给匿名四轴上位机软件(V2.6版本)
  10. //fun:功能字. 0XA0~0XAF
  11. //data:数据缓存区,最多28字节!!
  12. //len:data区有效数据个数
  13. void usart1_niming_report(u8 fun,u8*data,u8 len)
  14. {
  15. u8 send_buf[32];
  16. u8 i;
  17. if(len>28)return; //最多28字节数据
  18. send_buf[len+3]=0; //校验数置零
  19. send_buf[0]=0X88; //帧头
  20. send_buf[1]=fun; //功能字
  21. send_buf[2]=len; //数据长度
  22. for(i=0;i<len;i++)send_buf[3+i]=data[i]; //复制数据
  23. for(i=0;i<len+3;i++)send_buf[len+3]+=send_buf[i]; //计算校验和
  24. for(i=0;i<len+4;i++)usart1_send_char(send_buf[i]); //发送数据到串口1
  25. }
  26. //发送加速度传感器数据和陀螺仪数据
  27. //aacx,aacy,aacz:x,y,z三个方向上面的加速度值
  28. //gyrox,gyroy,gyroz:x,y,z三个方向上面的陀螺仪值
  29. void mpu6050_send_data(short aacx,short aacy,short aacz,short gyrox,short gyroy,short gyroz)
  30. {
  31. u8 tbuf[12];
  32. tbuf[0]=(aacx>>8)&0XFF;
  33. tbuf[1]=aacx&0XFF;
  34. tbuf[2]=(aacy>>8)&0XFF;
  35. tbuf[3]=aacy&0XFF;
  36. tbuf[4]=(aacz>>8)&0XFF;
  37. tbuf[5]=aacz&0XFF;
  38. tbuf[6]=(gyrox>>8)&0XFF;
  39. tbuf[7]=gyrox&0XFF;
  40. tbuf[8]=(gyroy>>8)&0XFF;
  41. tbuf[9]=gyroy&0XFF;
  42. tbuf[10]=(gyroz>>8)&0XFF;
  43. tbuf[11]=gyroz&0XFF;
  44. usart1_niming_report(0XA1,tbuf,12);//自定义帧,0XA1
  45. }
  46. //通过串口1上报结算后的姿态数据给电脑
  47. //aacx,aacy,aacz:x,y,z三个方向上面的加速度值
  48. //gyrox,gyroy,gyroz:x,y,z三个方向上面的陀螺仪值
  49. //roll:横滚角.单位0.01度。 -18000 -> 18000 对应 -180.00 -> 180.00
  50. //pitch:俯仰角.单位 0.01度。-9000 - 9000 对应 -90.00 -> 90.00
  51. //yaw:航向角.单位为0.10 -> 3600 对应 0 -> 360.0
  52. void usart1_report_imu(short aacx,short aacy,short aacz,short gyrox,short gyroy,short gyroz,float roll_f,float pitch_f,float yaw_f)
  53. {
  54. u8 i;
  55. short roll,pitch,yaw;
  56. u8 tbuf[28];
  57. for(i=0;i<28;i++)tbuf[i]=0;//0
  58. //按上位机格式要求整理数据
  59. roll = (int)(roll_f*100);
  60. pitch = (int)(pitch_f*100);
  61. yaw = (int)(yaw_f*10);
  62. tbuf[0]=(aacx>>8)&0XFF;
  63. tbuf[1]=aacx&0XFF;
  64. tbuf[2]=(aacy>>8)&0XFF;
  65. tbuf[3]=aacy&0XFF;
  66. tbuf[4]=(aacz>>8)&0XFF;
  67. tbuf[5]=aacz&0XFF;
  68. tbuf[6]=(gyrox>>8)&0XFF;
  69. tbuf[7]=gyrox&0XFF;
  70. tbuf[8]=(gyroy>>8)&0XFF;
  71. tbuf[9]=gyroy&0XFF;
  72. tbuf[10]=(gyroz>>8)&0XFF;
  73. tbuf[11]=gyroz&0XFF;
  74. tbuf[18]=(roll>>8)&0XFF;
  75. tbuf[19]=roll&0XFF;
  76. tbuf[20]=(pitch>>8)&0XFF;
  77. tbuf[21]=pitch&0XFF;
  78. tbuf[22]=(yaw>>8)&0XFF;
  79. tbuf[23]=yaw&0XFF;
  80. usart1_niming_report(0XAF,tbuf,28);//飞控显示帧,0XAF
  81. }

(2) send_to_client.h

  1. #ifndef __SEND_TO_CLIENT_H
  2. #define __SEND_TO_CLIENT_H
  3. #include "stm32f10x.h"
  4. #include "sys.h"
  5. void usart1_send_char(u8 c);
  6. void usart1_niming_report(u8 fun,u8*data,u8 len);
  7. void mpu6050_send_data(short aacx,short aacy,short aacz,short gyrox,short gyroy,short gyroz);
  8. void usart1_report_imu(short aacx,short aacy,short aacz,short gyrox,short gyroy,short gyroz,float roll_f,float pitch_f,float yaw_f);
  9. #endif

 

5、最后是主函数调用 main.c

  1. #include "stm32f10x.h"
  2. #include "variable.h"
  3. #include "delay.h"
  4. #include "led.h"
  5. #include "key.h"
  6. #include "usart.h"
  7. #include "timer.h"
  8. #include "mpu6500_driver.h"
  9. #include "send_to_client.h"
  10. #include "inv_mpu_dmp_motion_driver.h"
  11. #include "inv_mpu.h"
  12. #include "lcd_driver.h"
  13. #define USART1_Bound 115200
  14. #define save_mpu6500_key 0xaa55
  15. #define save_mpu6500_addr 98
  16. // unsigned long sensor_timestamp;
  17. // short gyro[3], accel[3], sensors;
  18. // unsigned char more;
  19. // long quat[4];
  20. u8 buffer[100];
  21. long MPU_buffer[100];
  22. int main(void)
  23. {
  24. u8 temp,result;
  25. float pitch,roll,yaw;
  26. short accel[3],gyro[3];
  27. float temperature;
  28. // unsigned long timestamp;
  29. unsigned long walk_count;
  30. unsigned long walk_time;
  31. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  32. Delay_Init();
  33. LED_Init();
  34. Key_Init();
  35. MY_USART1_Init(USART1_Bound);
  36. Timer2_Init(1000-1,72-1);//set timer frequent:1000KHz T=1us
  37. LCD_Init();
  38. LCD_Clear(WHITE);
  39. MPU6500_Port_EXIT_Init();
  40. if(InitMPU6050()) printf("InitMPU6050 error\r\n");
  41. if(MPU6500_DMP_Init()) printf("MPU6500_DMP_Init error :%d \r\n",result);
  42. LCD_ShowString(10,20,100,16,16,(u8*)"MPU6500 TEST");
  43. MPU6500_Read_Len(MPU6500_device_addr,WHO_AM_I,1,&temp);
  44. sprintf((char*)buffer,"MPU6500 ID:0X%x",temp);
  45. LCD_ShowString(10,50,200,16,16,buffer);
  46. while(1)
  47. {
  48. if(MPU_EXTI_flag)
  49. {
  50. MPU_EXTI_flag=0;
  51. LED_D5=!LED_D5;
  52. result=MPU6500_dmp_get_euler_angle(accel,gyro,&pitch,&roll,&yaw);//获得各角数据
  53. if(result==0)
  54. {
  55. mpu6050_send_data(accel[0],accel[1],accel[2],gyro[0],gyro[1],gyro[2]);//将数据发到上位机
  56. usart1_report_imu(accel[0],accel[1],accel[2],gyro[0],gyro[1],gyro[2],(roll),(pitch),(yaw));
  57. }
  58. }
  59. if(Timer2_flag_100ms)
  60. {
  61. Timer2_flag_100ms=0;
  62. sprintf((char*)buffer,"yaw:%5.1f",yaw);
  63. LCD_ShowString(10,70,100,16,16,buffer);
  64. sprintf((char*)buffer,"pitch:%5.1f",pitch);
  65. LCD_ShowString(10,90,100,16,16,buffer);
  66. sprintf((char*)buffer,"roll:%5.1f",roll);
  67. LCD_ShowString(10,110,100,16,16,buffer);
  68. }
  69. if(Timer2_flag_1s&LED_Time_flag_MaskBit)
  70. {
  71. Timer2_flag_1s&=~LED_Time_flag_MaskBit;
  72. LED_D4=!LED_D4;
  73. dmp_get_pedometer_step_count(&walk_count);//计步器
  74. dmp_get_pedometer_walk_time(&walk_time);
  75. temperature=MPU6500_GetTemperature();//内部温度
  76. sprintf((char*)buffer,"step:%d walk_time:%d",walk_count,walk_time);
  77. LCD_ShowString(10,130,240,16,16,buffer);
  78. sprintf((char*)buffer,"T:%5.1f",temperature);
  79. LCD_ShowString(10,150,240,16,16,buffer);
  80. }
  81. /*
  82. if(USART1_Receive_T_flag&0x01)
  83. {
  84. USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);
  85. DMA_Cmd(DMA1_Channel4,ENABLE);
  86. USART1_Receive_T_flag&=~0x01;
  87. }
  88. if(DMA_TransferComplete_Flag&0x01)
  89. {
  90. LED_D4=0;
  91. DMA_TransferComplete_Flag&=~0x01;
  92. }
  93. */
  94. }
  95. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/467194
推荐阅读
相关标签
  

闽ICP备14008679号