drv_mpu9250.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #ifndef __drv_mpu9250_H
  2. #define __drv_mpu9250_H
  3. #include <stdbool.h>
  4. #include <stdint.h>
  5. #include <string.h>
  6. #include "sdk_common.h"
  7. #include "SEGGER_RTT.h"
  8. #include "usr_config.h"
  9. /* ---- MPU9250 Reg In MPU9250 ---------------------------------------------- */
  10. //#define MPU9250_I2C_ADDR ((uint8_t)0xD0)
  11. #define MPU9250_I2C_ADDR ((uint8_t)0xD0)
  12. #define MPU9250_Device_ID ((uint8_t)0x71) // In MPU9250
  13. #define MPU9250_SELF_TEST_XG ((uint8_t)0x00)
  14. #define MPU9250_SELF_TEST_YG ((uint8_t)0x01)
  15. #define MPU9250_SELF_TEST_ZG ((uint8_t)0x02)
  16. #define MPU9250_SELF_TEST_XA ((uint8_t)0x0D)
  17. #define MPU9250_SELF_TEST_YA ((uint8_t)0x0E)
  18. #define MPU9250_SELF_TEST_ZA ((uint8_t)0x0F)
  19. #define MPU9250_XG_OFFSET_H ((uint8_t)0x13)
  20. #define MPU9250_XG_OFFSET_L ((uint8_t)0x14)
  21. #define MPU9250_YG_OFFSET_H ((uint8_t)0x15)
  22. #define MPU9250_YG_OFFSET_L ((uint8_t)0x16)
  23. #define MPU9250_ZG_OFFSET_H ((uint8_t)0x17)
  24. #define MPU9250_ZG_OFFSET_L ((uint8_t)0x18)
  25. #define MPU9250_SMPLRT_DIV ((uint8_t)0x19)
  26. #define MPU9250_CONFIG ((uint8_t)0x1A)
  27. #define MPU9250_GYRO_CONFIG ((uint8_t)0x1B)
  28. #define MPU9250_ACCEL_CONFIG ((uint8_t)0x1C)
  29. #define MPU9250_ACCEL_CONFIG_2 ((uint8_t)0x1D)
  30. #define MPU9250_LP_ACCEL_ODR ((uint8_t)0x1E)
  31. #define MPU9250_MOT_THR ((uint8_t)0x1F)
  32. #define MPU9250_FIFO_EN ((uint8_t)0x23)
  33. #define MPU9250_I2C_MST_CTRL ((uint8_t)0x24)
  34. #define MPU9250_I2C_SLV0_ADDR ((uint8_t)0x25)
  35. #define MPU9250_I2C_SLV0_REG ((uint8_t)0x26)
  36. #define MPU9250_I2C_SLV0_CTRL ((uint8_t)0x27)
  37. #define MPU9250_I2C_SLV1_ADDR ((uint8_t)0x28)
  38. #define MPU9250_I2C_SLV1_REG ((uint8_t)0x29)
  39. #define MPU9250_I2C_SLV1_CTRL ((uint8_t)0x2A)
  40. #define MPU9250_I2C_SLV2_ADDR ((uint8_t)0x2B)
  41. #define MPU9250_I2C_SLV2_REG ((uint8_t)0x2C)
  42. #define MPU9250_I2C_SLV2_CTRL ((uint8_t)0x2D)
  43. #define MPU9250_I2C_SLV3_ADDR ((uint8_t)0x2E)
  44. #define MPU9250_I2C_SLV3_REG ((uint8_t)0x2F)
  45. #define MPU9250_I2C_SLV3_CTRL ((uint8_t)0x30)
  46. #define MPU9250_I2C_SLV4_ADDR ((uint8_t)0x31)
  47. #define MPU9250_I2C_SLV4_REG ((uint8_t)0x32)
  48. #define MPU9250_I2C_SLV4_DO ((uint8_t)0x33)
  49. #define MPU9250_I2C_SLV4_CTRL ((uint8_t)0x34)
  50. #define MPU9250_I2C_SLV4_DI ((uint8_t)0x35)
  51. #define MPU9250_I2C_MST_STATUS ((uint8_t)0x36)
  52. #define MPU9250_INT_PIN_CFG ((uint8_t)0x37)
  53. #define MPU9250_INT_ENABLE ((uint8_t)0x38)
  54. #define MPU9250_INT_STATUS ((uint8_t)0x3A)
  55. #define MPU9250_ACCEL_XOUT_H ((uint8_t)0x3B)
  56. #define MPU9250_ACCEL_XOUT_L ((uint8_t)0x3C)
  57. #define MPU9250_ACCEL_YOUT_H ((uint8_t)0x3D)
  58. #define MPU9250_ACCEL_YOUT_L ((uint8_t)0x3E)
  59. #define MPU9250_ACCEL_ZOUT_H ((uint8_t)0x3F)
  60. #define MPU9250_ACCEL_ZOUT_L ((uint8_t)0x40)
  61. #define MPU9250_TEMP_OUT_H ((uint8_t)0x41)
  62. #define MPU9250_TEMP_OUT_L ((uint8_t)0x42)
  63. #define MPU9250_GYRO_XOUT_H ((uint8_t)0x43)
  64. #define MPU9250_GYRO_XOUT_L ((uint8_t)0x44)
  65. #define MPU9250_GYRO_YOUT_H ((uint8_t)0x45)
  66. #define MPU9250_GYRO_YOUT_L ((uint8_t)0x46)
  67. #define MPU9250_GYRO_ZOUT_H ((uint8_t)0x47)
  68. #define MPU9250_GYRO_ZOUT_L ((uint8_t)0x48)
  69. #define MPU9250_EXT_SENS_DATA_00 ((uint8_t)0x49)
  70. #define MPU9250_EXT_SENS_DATA_01 ((uint8_t)0x4A)
  71. #define MPU9250_EXT_SENS_DATA_02 ((uint8_t)0x4B)
  72. #define MPU9250_EXT_SENS_DATA_03 ((uint8_t)0x4C)
  73. #define MPU9250_EXT_SENS_DATA_04 ((uint8_t)0x4D)
  74. #define MPU9250_EXT_SENS_DATA_05 ((uint8_t)0x4E)
  75. #define MPU9250_EXT_SENS_DATA_06 ((uint8_t)0x4F)
  76. #define MPU9250_EXT_SENS_DATA_07 ((uint8_t)0x50)
  77. #define MPU9250_EXT_SENS_DATA_08 ((uint8_t)0x51)
  78. #define MPU9250_EXT_SENS_DATA_09 ((uint8_t)0x52)
  79. #define MPU9250_EXT_SENS_DATA_10 ((uint8_t)0x53)
  80. #define MPU9250_EXT_SENS_DATA_11 ((uint8_t)0x54)
  81. #define MPU9250_EXT_SENS_DATA_12 ((uint8_t)0x55)
  82. #define MPU9250_EXT_SENS_DATA_13 ((uint8_t)0x56)
  83. #define MPU9250_EXT_SENS_DATA_14 ((uint8_t)0x57)
  84. #define MPU9250_EXT_SENS_DATA_15 ((uint8_t)0x58)
  85. #define MPU9250_EXT_SENS_DATA_16 ((uint8_t)0x59)
  86. #define MPU9250_EXT_SENS_DATA_17 ((uint8_t)0x5A)
  87. #define MPU9250_EXT_SENS_DATA_18 ((uint8_t)0x5B)
  88. #define MPU9250_EXT_SENS_DATA_19 ((uint8_t)0x5C)
  89. #define MPU9250_EXT_SENS_DATA_20 ((uint8_t)0x5D)
  90. #define MPU9250_EXT_SENS_DATA_21 ((uint8_t)0x5E)
  91. #define MPU9250_EXT_SENS_DATA_22 ((uint8_t)0x5F)
  92. #define MPU9250_EXT_SENS_DATA_23 ((uint8_t)0x60)
  93. #define MPU9250_I2C_SLV0_DO ((uint8_t)0x63)
  94. #define MPU9250_I2C_SLV1_DO ((uint8_t)0x64)
  95. #define MPU9250_I2C_SLV2_DO ((uint8_t)0x65)
  96. #define MPU9250_I2C_SLV3_DO ((uint8_t)0x66)
  97. #define MPU9250_I2C_MST_DELAY_CTRL ((uint8_t)0x67)
  98. #define MPU9250_SIGNAL_PATH_RESET ((uint8_t)0x68)
  99. #define MPU9250_MOT_DETECT_CTRL ((uint8_t)0x69)
  100. #define MPU9250_USER_CTRL ((uint8_t)0x6A)
  101. #define MPU9250_PWR_MGMT_1 ((uint8_t)0x6B)
  102. #define MPU9250_PWR_MGMT_2 ((uint8_t)0x6C)
  103. #define MPU9250_FIFO_COUNTH ((uint8_t)0x72)
  104. #define MPU9250_FIFO_COUNTL ((uint8_t)0x73)
  105. #define MPU9250_FIFO_R_W ((uint8_t)0x74)
  106. #define MPU9250_WHO_AM_I ((uint8_t)0x75) // ID = 0x71 In MPU9250
  107. #define MPU9250_XA_OFFSET_H ((uint8_t)0x77)
  108. #define MPU9250_XA_OFFSET_L ((uint8_t)0x78)
  109. #define MPU9250_YA_OFFSET_H ((uint8_t)0x7A)
  110. #define MPU9250_YA_OFFSET_L ((uint8_t)0x7B)
  111. #define MPU9250_ZA_OFFSET_H ((uint8_t)0x7D)
  112. #define MPU9250_ZA_OFFSET_L ((uint8_t)0x7E)
  113. /* ---- AK8963 Reg In MPU9250 ----------------------------------------------- */
  114. #define AK8963_I2C_ADDR ((uint8_t)0x18)
  115. #define AK8963_Device_ID ((uint8_t)0x48)
  116. // Read-only Reg
  117. #define AK8963_WIA ((uint8_t)0x00)
  118. #define AK8963_INFO ((uint8_t)0x01)
  119. #define AK8963_ST1 ((uint8_t)0x02)
  120. #define AK8963_HXL ((uint8_t)0x03)
  121. #define AK8963_HXH ((uint8_t)0x04)
  122. #define AK8963_HYL ((uint8_t)0x05)
  123. #define AK8963_HYH ((uint8_t)0x06)
  124. #define AK8963_HZL ((uint8_t)0x07)
  125. #define AK8963_HZH ((uint8_t)0x08)
  126. #define AK8963_ST2 ((uint8_t)0x09)
  127. // Write/Read Reg
  128. #define AK8963_CNTL1 ((uint8_t)0x0A)
  129. #define AK8963_CNTL2 ((uint8_t)0x0B)
  130. #define AK8963_ASTC ((uint8_t)0x0C)
  131. #define AK8963_TS1 ((uint8_t)0x0D)
  132. #define AK8963_TS2 ((uint8_t)0x0E)
  133. #define AK8963_I2CDIS ((uint8_t)0x0F)
  134. // Read-only Reg ( ROM )
  135. #define AK8963_ASAX ((uint8_t)0x10)
  136. #define AK8963_ASAY ((uint8_t)0x11)
  137. #define AK8963_ASAZ ((uint8_t)0x12)
  138. //(0x00 +-2g;) ( 0x08 +-4g;) (0x10 +-8g;) (0x18 +-16g)
  139. #define MPU9250_ACC_2G 0x00
  140. #define MPU9250_ACC_4G 0x08
  141. #define MPU9250_ACC_8G 0x10
  142. #define MPU9250_ACC_16G 0x18
  143. //#define MPU9250_ACC_SEL MPU9250_ACC_2G
  144. //#define MPU9250_ACC_SEL MPU9250_ACC_4G
  145. //#define MPU9250_ACC_SEL MPU9250_ACC_8G
  146. #define MPU9250_ACC_SEL MPU9250_ACC_16G
  147. #if MPU9250_ACC_SEL == MPU9250_ACC_2G
  148. #ifndef MPU9250_ACC_SSF
  149. #define MPU9250_ACC_SSF 16384
  150. #endif
  151. #endif
  152. #if MPU9250_ACC_SEL == MPU9250_ACC_4G
  153. #ifndef MPU9250_ACC_SSF
  154. #define MPU9250_ACC_SSF 8192
  155. #endif
  156. #endif
  157. #if MPU9250_ACC_SEL == MPU9250_ACC_8G
  158. #ifndef MPU9250_ACC_SSF
  159. #define MPU9250_ACC_SSF 4096
  160. #endif
  161. #endif
  162. #if MPU9250_ACC_SEL == MPU9250_ACC_16G
  163. #ifndef MPU9250_ACC_SSF
  164. #define MPU9250_ACC_SSF 2048
  165. #endif
  166. #endif
  167. //(0x00 +-250deg/s;) ( 0x08 +-500deg/s;) (0x10 +-1000deg/s;) (0x18 +-2000deg/s)
  168. #define MPU9250_GYR_250 0x00
  169. #define MPU9250_GYR_500 0x08
  170. #define MPU9250_GYR_1000 0x10
  171. #define MPU9250_GYR_2000 0x18
  172. //#define MPU9250_GYR_SEL MPU9250_GYR_250
  173. //#define MPU9250_GYR_SEL MPU9250_GYR_500
  174. //#define MPU9250_GYR_SEL MPU9250_GYR_1000
  175. #define MPU9250_GYR_SEL MPU9250_GYR_2000
  176. #if MPU9250_GYR_SEL == MPU9250_GYR_250
  177. #ifndef MPU9250_GYR_SSF
  178. #define MPU9250_GYR_SSF 131
  179. #endif
  180. #endif
  181. #if MPU9250_GYR_SEL == MPU9250_GYR_500
  182. #ifndef MPU9250_GYR_SSF
  183. #define MPU9250_GYR_SSF 65.5
  184. #endif
  185. #endif
  186. #if MPU9250_GYR_SEL == MPU9250_GYR_1000
  187. #ifndef MPU9250_GYR_SSF
  188. #define MPU9250_GYR_SSF 32.8
  189. #endif
  190. #endif
  191. #if MPU9250_GYR_SEL == MPU9250_GYR_2000
  192. #ifndef MPU9250_GYR_SSF
  193. #define MPU9250_GYR_SSF 16.4
  194. #endif
  195. #endif
  196. #define MUP9250_DLPF_260Hz 0x00
  197. #define MUP9250_DLPF_184Hz 0x01
  198. #define MUP9250_DLPF_94Hz 0x02
  199. #define MUP9250_DLPF_44Hz 0x03
  200. #define MUP9250_DLPF_21Hz 0x04
  201. #define MUP9250_DLPF_10Hz 0x05
  202. #define MUP9250_DLPF_5Hz 0x06
  203. //#define MUP9250_ACC_DLPF_260Hz 0x00 //0ms
  204. //#define MUP9250_ACC_DLPF_184Hz 0x01 //2ms
  205. //#define MUP9250_ACC_DLPF_94Hz 0x02 //3ms
  206. //#define MUP9250_ACC_DLPF_44Hz 0x03 //4.9ms
  207. //#define MUP9250_ACC_DLPF_21Hz 0x04 //8.5ms
  208. //#define MUP9250_ACC_DLPF_10Hz 0x05 //13.8ms
  209. //#define MUP9250_ACC_DLPF_5Hz 0x06 //19ms
  210. //#define MUP9250_GYR_DLPF_256Hz 0x00 //0.98ms
  211. //#define MUP9250_GYR_DLPF_188Hz 0x01 //1.9ms
  212. //#define MUP9250_GYR_DLPF_98Hz 0x02 //2.8ms
  213. //#define MUP9250_GYR_DLPF_42Hz 0x03 //4.8ms
  214. //#define MUP9250_GYR_DLPF_20Hz 0x04 //8.3ms
  215. //#define MUP9250_GYR_DLPF_10Hz 0x05 //13.4ms
  216. //#define MUP9250_GYR_DLPF_5Hz 0x06 //18.6ms
  217. bool MPU9250_Reset(void);
  218. bool MPU9250_sleep(void);
  219. bool MPU9250_Init(void);
  220. bool MPU9250_Acc_Init(void);
  221. bool MPU9250_MageInit(void);
  222. bool MPU9250_Read_Acc(int16_t* acc);
  223. bool MPU9250_Read(int16_t* acc,int16_t* gyr,int16_t* mag,int16_t* temperature);
  224. #endif