2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 #ifndef _AO_MPU9250_H_
20 #define _AO_MPU9250_H_
23 #define M_PI 3.1415926535897832384626433
26 #define MPU9250_ADDR_WRITE 0xd0
27 #define MPU9250_ADDR_READ 0xd1
30 #define MPUREG_XG_OFFS_TC 0x00
31 #define MPUREG_YG_OFFS_TC 0x01
32 #define MPUREG_ZG_OFFS_TC 0x02
33 #define MPUREG_X_FINE_GAIN 0x03
34 #define MPUREG_Y_FINE_GAIN 0x04
35 #define MPUREG_Z_FINE_GAIN 0x05
36 #define MPUREG_XA_OFFS_H 0x06 // X axis accelerometer offset (high byte)
37 #define MPUREG_XA_OFFS_L 0x07 // X axis accelerometer offset (low byte)
38 #define MPUREG_YA_OFFS_H 0x08 // Y axis accelerometer offset (high byte)
39 #define MPUREG_YA_OFFS_L 0x09 // Y axis accelerometer offset (low byte)
40 #define MPUREG_ZA_OFFS_H 0x0A // Z axis accelerometer offset (high byte)
41 #define MPUREG_ZA_OFFS_L 0x0B // Z axis accelerometer offset (low byte)
42 #define MPUREG_PRODUCT_ID 0x0C // Product ID Register
43 #define MPUREG_XG_OFFS_USRH 0x13 // X axis gyro offset (high byte)
44 #define MPUREG_XG_OFFS_USRL 0x14 // X axis gyro offset (low byte)
45 #define MPUREG_YG_OFFS_USRH 0x15 // Y axis gyro offset (high byte)
46 #define MPUREG_YG_OFFS_USRL 0x16 // Y axis gyro offset (low byte)
47 #define MPUREG_ZG_OFFS_USRH 0x17 // Z axis gyro offset (high byte)
48 #define MPUREG_ZG_OFFS_USRL 0x18 // Z axis gyro offset (low byte)
50 #define MPU9250_SMPRT_DIV 0x19
52 #define MPU9250_CONFIG 0x1a
54 #define MPU9250_CONFIG_FIFO_MODE 6
55 # define MPU9250_CONFIG_FIFO_MODE_REPLACE 0
56 # define MPU9250_CONFIG_FIFO_MODE_DROP 1
58 #define MPU9250_CONFIG_EXT_SYNC_SET 3
59 #define MPU9250_CONFIG_EXT_SYNC_SET_DISABLED 0
60 #define MPU9250_CONFIG_EXT_SYNC_SET_TEMP_OUT_L 1
61 #define MPU9250_CONFIG_EXT_SYNC_SET_GYRO_XOUT_L 2
62 #define MPU9250_CONFIG_EXT_SYNC_SET_GYRO_YOUT_L 3
63 #define MPU9250_CONFIG_EXT_SYNC_SET_GYRO_ZOUT_L 4
64 #define MPU9250_CONFIG_EXT_SYNC_SET_ACCEL_XOUT_L 5
65 #define MPU9250_CONFIG_EXT_SYNC_SET_ACCEL_YOUT_L 6
66 #define MPU9250_CONFIG_EXT_SYNC_SET_ACCEL_ZOUT_L 7
67 #define MPU9250_CONFIG_EXT_SYNC_SET_MASK 7
69 #define MPU9250_CONFIG_DLPF_CFG 0
70 #define MPU9250_CONFIG_DLPF_CFG_250 0
71 #define MPU9250_CONFIG_DLPF_CFG_184 1
72 #define MPU9250_CONFIG_DLPF_CFG_92 2
73 #define MPU9250_CONFIG_DLPF_CFG_41 3
74 #define MPU9250_CONFIG_DLPF_CFG_20 4
75 #define MPU9250_CONFIG_DLPF_CFG_10 5
76 #define MPU9250_CONFIG_DLPF_CFG_5 6
77 #define MPU9250_CONFIG_DLPF_CFG_MASK 7
79 #define MPU9250_GYRO_CONFIG 0x1b
80 # define MPU9250_GYRO_CONFIG_XG_ST 7
81 # define MPU9250_GYRO_CONFIG_YG_ST 6
82 # define MPU9250_GYRO_CONFIG_ZG_ST 5
83 # define MPU9250_GYRO_CONFIG_FS_SEL 3
84 # define MPU9250_GYRO_CONFIG_FS_SEL_250 0
85 # define MPU9250_GYRO_CONFIG_FS_SEL_500 1
86 # define MPU9250_GYRO_CONFIG_FS_SEL_1000 2
87 # define MPU9250_GYRO_CONFIG_FS_SEL_2000 3
88 # define MPU9250_GYRO_CONFIG_FS_SEL_MASK 3
89 # define MPU9250_GYRO_CONFIG_FCHOICE_B 0
90 # define MPU9250_GYRO_CONFIG_FCHOICE_B_8800 3
91 # define MPU9250_GYRO_CONFIG_FCHOICE_B_3600 2
92 # define MPU9250_GYRO_CONFIG_FCHOICE_B_LOW 0
94 #define MPU9250_ACCEL_CONFIG 0x1c
95 # define MPU9250_ACCEL_CONFIG_XA_ST 7
96 # define MPU9250_ACCEL_CONFIG_YA_ST 6
97 # define MPU9250_ACCEL_CONFIG_ZA_ST 5
98 # define MPU9250_ACCEL_CONFIG_AFS_SEL 3
99 # define MPU9250_ACCEL_CONFIG_AFS_SEL_2G 0
100 # define MPU9250_ACCEL_CONFIG_AFS_SEL_4G 1
101 # define MPU9250_ACCEL_CONFIG_AFS_SEL_8G 2
102 # define MPU9250_ACCEL_CONFIG_AFS_SEL_16G 3
103 # define MPU9250_ACCEL_CONFIG_AFS_SEL_MASK 3
105 #define MPU9250_MST_CTRL 0x24
106 #define MPU9250_MST_CTRL_MULT_MST_EN 7
107 #define MPU9250_MST_CTRL_WAIT_FOR_ES 6
108 #define MPU9250_MST_CTRL_SLV_3_FIFO_EN 5
109 #define MPU9250_MST_CTRL_I2C_MST_P_NSR 4
110 #define MPU9250_MST_CTRL_I2C_MST_CLK 0
111 #define MPU9250_MST_CTRL_I2C_MST_CLK_348 0
112 #define MPU9250_MST_CTRL_I2C_MST_CLK_333 1
113 #define MPU9250_MST_CTRL_I2C_MST_CLK_320 2
114 #define MPU9250_MST_CTRL_I2C_MST_CLK_308 3
115 #define MPU9250_MST_CTRL_I2C_MST_CLK_296 4
116 #define MPU9250_MST_CTRL_I2C_MST_CLK_286 5
117 #define MPU9250_MST_CTRL_I2C_MST_CLK_276 6
118 #define MPU9250_MST_CTRL_I2C_MST_CLK_267 7
119 #define MPU9250_MST_CTRL_I2C_MST_CLK_258 8
120 #define MPU9250_MST_CTRL_I2C_MST_CLK_500 9
121 #define MPU9250_MST_CTRL_I2C_MST_CLK_471 10
122 #define MPU9250_MST_CTRL_I2C_MST_CLK_444 11
123 #define MPU9250_MST_CTRL_I2C_MST_CLK_421 12
124 #define MPU9250_MST_CTRL_I2C_MST_CLK_400 13
125 #define MPU9250_MST_CTRL_I2C_MST_CLK_381 14
126 #define MPU9250_MST_CTRL_I2C_MST_CLK_364 15
127 #define MPU9250_MST_CTRL_I2C_MST_CLK_MASK 15
129 #define MPU9250_I2C_SLV0_ADDR 0x25
130 #define MPU9250_I2C_SLV0_REG 0x26
131 #define MPU9250_I2C_SLV0_CTRL 0x27
133 #define MPU9250_I2C_SLV0_CTRL_I2C_SLV0_EN 7
134 #define MPU9250_I2C_SLV0_CTRL_I2C_SLV0_BYTE_SW 6
135 #define MPU9250_I2C_SLV0_CTRL_I2C_SLV0_REG_DIS 5
136 #define MPU9250_I2C_SLV0_CTRL_I2C_SLV0_GRP 4
137 #define MPU9250_I2C_SLV0_CTRL_I2C_SLV0_LENG 0
139 #define MPU9250_I2C_SLV1_ADDR 0x28
140 #define MPU9250_I2C_SLV1_REG 0x29
141 #define MPU9250_I2C_SLV1_CTRL 0x2a
143 #define MPU9250_I2C_SLV2_ADDR 0x2b
144 #define MPU9250_I2C_SLV2_REG 0x2c
145 #define MPU9250_I2C_SLV2_CTRL 0x2d
147 #define MPU9250_I2C_SLV3_ADDR 0x2e
148 #define MPU9250_I2C_SLV3_REG 0x2f
149 #define MPU9250_I2C_SLV3_CTRL 0x30
151 #define MPU9250_I2C_SLV4_ADDR 0x31
152 #define MPU9250_I2C_SLV4_REG 0x32
153 #define MPU9250_I2C_SLV4_DO 0x33
154 #define MPU9250_I2C_SLV4_CTRL 0x34
155 #define MPU9250_I2C_SLV4_CTRL_I2C_SLV4_EN 7
156 #define MPU9250_I2C_SLV4_CTRL_SLV4_DONE_INT_EN 6
157 #define MPU9250_I2C_SLV4_CTRL_I2C_SLV4_REG_DIS 5
158 #define MPU9250_I2C_SLV4_CTRL_I2C_MST_DLY 0
160 #define MPU9250_I2C_SLV4_DI 0x35
162 #define MPU9250_I2C_MST_STATUS 0x36
164 #define MPU9250_INT_PIN_CFG 0x37
166 #define MPU9250_INT_ENABLE 0x38
167 #define MPU9250_INT_ENABLE_WOM_EN 6
168 #define MPU9250_INT_ENABLE_FIFO_OFLOW_EN 4
169 #define MPU9250_INT_ENABLE_FSYNC_INT_EN 3
170 #define MPU9250_INT_ENABLE_RAW_RDY_EN 0
172 #define MPU9250_INT_STATUS 0x3a
173 #define MPU9250_INT_STATUS_WOM_INT 6
174 #define MPU9250_INT_STATUS_FIFO_OFLOW_INT 4
175 #define MPU9250_INT_STATUS_FSYNC_INT 3
176 #define MPU9250_INT_STATUS_RAW_RDY_INT 0
178 #define MPU9250_ACCEL_XOUT_H 0x3b
179 #define MPU9250_ACCEL_XOUT_L 0x3c
180 #define MPU9250_ACCEL_YOUT_H 0x3d
181 #define MPU9250_ACCEL_YOUT_L 0x3e
182 #define MPU9250_ACCEL_ZOUT_H 0x3f
183 #define MPU9250_ACCEL_ZOUT_L 0x40
184 #define MPU9250_TEMP_H 0x41
185 #define MPU9250_TEMP_L 0x42
186 #define MPU9250_GYRO_XOUT_H 0x43
187 #define MPU9250_GYRO_XOUT_L 0x44
188 #define MPU9250_GYRO_YOUT_H 0x45
189 #define MPU9250_GYRO_YOUT_L 0x46
190 #define MPU9250_GYRO_ZOUT_H 0x47
191 #define MPU9250_GYRO_ZOUT_L 0x48
193 #define MPU9250_I2C_MST_DELAY_CTRL 0x67
195 #define MPU9250_I2C_MST_DELAY_CTRL_DELAY_ES_SHADOW 7
196 #define MPU9250_I2C_MST_DELAY_CTRL_I2C_SLV4_DLY_EN 4
197 #define MPU9250_I2C_MST_DELAY_CTRL_I2C_SLV3_DLY_EN 3
198 #define MPU9250_I2C_MST_DELAY_CTRL_I2C_SLV2_DLY_EN 2
199 #define MPU9250_I2C_MST_DELAY_CTRL_I2C_SLV1_DLY_EN 1
200 #define MPU9250_I2C_MST_DELAY_CTRL_I2C_SLV0_DLY_EN 0
202 #define MPU9250_SIGNAL_PATH_RESET 0x68
203 #define MPU9250_SIGNAL_PATH_RESET_GYRO_RESET 2
204 #define MPU9250_SIGNAL_PATH_RESET_ACCEL_RESET 1
205 #define MPU9250_SIGNAL_PATH_RESET_TEMP_RESET 0
207 #define MPU9250_USER_CTRL 0x6a
208 #define MPU9250_USER_CTRL_FIFO_EN 6
209 #define MPU9250_USER_CTRL_I2C_MST_EN 5
210 #define MPU9250_USER_CTRL_I2C_IF_DIS 4
211 #define MPU9250_USER_CTRL_FIFO_RESET 2
212 #define MPU9250_USER_CTRL_I2C_MST_RESET 1
213 #define MPU9250_USER_CTRL_SIG_COND_RESET 0
215 #define MPU9250_PWR_MGMT_1 0x6b
216 #define MPU9250_PWR_MGMT_1_DEVICE_RESET 7
217 #define MPU9250_PWR_MGMT_1_SLEEP 6
218 #define MPU9250_PWR_MGMT_1_CYCLE 5
219 #define MPU9250_PWR_MGMT_1_TEMP_DIS 3
220 #define MPU9250_PWR_MGMT_1_CLKSEL 0
221 #define MPU9250_PWR_MGMT_1_CLKSEL_INTERNAL 0
222 #define MPU9250_PWR_MGMT_1_CLKSEL_PLL_X_AXIS 1
223 #define MPU9250_PWR_MGMT_1_CLKSEL_PLL_Y_AXIS 2
224 #define MPU9250_PWR_MGMT_1_CLKSEL_PLL_Z_AXIS 3
225 #define MPU9250_PWR_MGMT_1_CLKSEL_PLL_EXTERNAL_32K 4
226 #define MPU9250_PWR_MGMT_1_CLKSEL_PLL_EXTERNAL_19M 5
227 #define MPU9250_PWR_MGMT_1_CLKSEL_STOP 7
228 #define MPU9250_PWR_MGMT_1_CLKSEL_MASK 7
230 #define MPU9250_PWR_MGMT_2 0x6c
232 #define MPU9250_WHO_AM_I 0x75
233 #define MPU9250_I_AM_9250 0x71
235 /* AK8963 mag sensor on the I2C bus */
237 #define MPU9250_MAG_ADDR 0x0c
239 #define MPU9250_MAG_WIA 0x00
240 #define MPU9250_MAG_WIA_VALUE 0x48
242 #define MPU9250_MAG_INFO 0x01
243 #define MPU9250_MAG_ST1 0x02
244 #define MPU9250_MAG_ST1_DOR 1
245 #define MPU9250_MAG_ST1_DRDY 0
247 #define MPU9250_MAG_HXL 0x03
248 #define MPU9250_MAG_HXH 0x04
249 #define MPU9250_MAG_HYL 0x05
250 #define MPU9250_MAG_HYH 0x06
251 #define MPU9250_MAG_HZL 0x07
252 #define MPU9250_MAG_HZH 0x08
253 #define MPU9250_MAG_ST2 0x09
254 #define MPU9250_MAG_ST2_BITM 4
255 #define MPU9250_MAG_ST2_HOFL 3
257 #define MPU9250_MAG_CNTL1 0x0a
258 #define MPU9250_MAG_CNTL1_MODE 0
259 #define MPU9250_MAG_CNTL1_MODE_POWER_DOWN 0x0
260 #define MPU9250_MAG_CNTL1_MODE_SINGLE 0x1
261 #define MPU9250_MAG_CNTL1_MODE_CONT_1 0x2 /* 8Hz */
262 #define MPU9250_MAG_CNTL1_MODE_CONT_2 0x6 /* 100Hz */
263 #define MPU9250_MAG_CNTL1_MODE_EXTERNAL 0x4
264 #define MPU9250_MAG_CNTL1_MODE_SELF_TEST 0x8
265 #define MPU9250_MAG_CNTL1_MODE_FUSE_ACCESS 0xf
267 #define MPU9250_MAG_CNTL1_BIT 4
268 #define MPU9250_MAG_CNTL1_BIT_14 0
269 #define MPU9250_MAG_CNTL1_BIT_16 1
271 #define MPU9250_MAG_CNTL2 0x0b
272 #define MPU9250_MAG_CNTL2_SRST 0
274 #define MPU9250_MAG_ASTC 0x0c
275 #define MPU9250_MAG_ASTC_SELF 6
277 #define MPU9250_MAG_TS1 0x0d
278 #define MPU9250_MAG_TS2 0x0e
279 #define MPU9250_MAG_I2CDIS 0x0f
280 #define MPU9250_MAG_I2CDIS_VALUE 0x1d
282 #define MPU9250_MAG_ASAX 0x10
283 #define MPU9250_MAG_ASAY 0x11
284 #define MPU9250_MAG_ASAZ 0x12
286 /* Self test acceleration is approximately 0.5g */
287 #define MPU9250_ST_ACCEL(full_scale) (32767 / ((full_scale) * 2))
289 /* Self test gyro is approximately 50°/s */
290 #define MPU9250_ST_GYRO(full_scale) ((int16_t) (((int32_t) 32767 * (int32_t) 50) / (full_scale)))
292 #define MPU9250_GYRO_FULLSCALE ((float) 2000 * M_PI/180.0)
295 ao_mpu9250_gyro(float sensor) {
296 return sensor * ((float) (MPU9250_GYRO_FULLSCALE / 32767.0));
299 #define MPU9250_ACCEL_FULLSCALE 16
302 ao_mpu9250_accel(int16_t sensor) {
303 return (float) sensor * ((float) (MPU9250_ACCEL_FULLSCALE * GRAVITY / 32767.0));
306 struct ao_mpu9250_sample {
319 extern struct ao_mpu9250_sample ao_mpu9250_current;
322 ao_mpu9250_init(void);
324 /* Product ID Description for MPU9250
325 * high 4 bits low 4 bits
326 * Product Name Product Revision
328 #define MPU9250ES_REV_C4 0x14 /* 0001 0100 */
329 #define MPU9250ES_REV_C5 0x15 /* 0001 0101 */
330 #define MPU9250ES_REV_D6 0x16 /* 0001 0110 */
331 #define MPU9250ES_REV_D7 0x17 /* 0001 0111 */
332 #define MPU9250ES_REV_D8 0x18 /* 0001 1000 */
333 #define MPU9250_REV_C4 0x54 /* 0101 0100 */
334 #define MPU9250_REV_C5 0x55 /* 0101 0101 */
335 #define MPU9250_REV_D6 0x56 /* 0101 0110 */
336 #define MPU9250_REV_D7 0x57 /* 0101 0111 */
337 #define MPU9250_REV_D8 0x58 /* 0101 1000 */
338 #define MPU9250_REV_D9 0x59 /* 0101 1001 */
340 #endif /* _AO_MPU9250_H_ */