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.
20 #include <ao_mpu9250.h>
25 static uint8_t ao_mpu9250_configured;
27 extern uint8_t ao_sensor_errors;
29 #ifndef AO_MPU9250_I2C_INDEX
30 #define AO_MPU9250_SPI 1
32 #define AO_MPU9250_SPI 0
37 #define ao_mpu9250_spi_get() ao_spi_get(AO_MPU9250_SPI_BUS, AO_SPI_SPEED_1MHz)
38 #define ao_mpu9250_spi_put() ao_spi_put(AO_MPU9250_SPI_BUS)
40 #define ao_mpu9250_spi_start() ao_spi_set_cs(AO_MPU9250_SPI_CS_PORT, \
41 (1 << AO_MPU9250_SPI_CS_PIN))
43 #define ao_mpu9250_spi_end() ao_spi_clr_cs(AO_MPU9250_SPI_CS_PORT, \
44 (1 << AO_MPU9250_SPI_CS_PIN))
50 _ao_mpu9250_reg_write(uint8_t addr, uint8_t value)
52 uint8_t d[2] = { addr, value };
54 ao_mpu9250_spi_start();
55 ao_spi_send(d, 2, AO_MPU9250_SPI_BUS);
58 ao_i2c_get(AO_MPU9250_I2C_INDEX);
59 ao_i2c_start(AO_MPU9250_I2C_INDEX, MPU9250_ADDR_WRITE);
60 ao_i2c_send(d, 2, AO_MPU9250_I2C_INDEX, TRUE);
61 ao_i2c_put(AO_MPU9250_I2C_INDEX);
66 _ao_mpu9250_read(uint8_t addr, void *data, uint8_t len)
70 ao_mpu9250_spi_start();
71 ao_spi_send(&addr, 1, AO_MPU9250_SPI_BUS);
72 ao_spi_recv(data, len, AO_MPU9250_SPI_BUS);
75 ao_i2c_get(AO_MPU9250_I2C_INDEX);
76 ao_i2c_start(AO_MPU9250_I2C_INDEX, MPU9250_ADDR_WRITE);
77 ao_i2c_send(&addr, 1, AO_MPU9250_I2C_INDEX, FALSE);
78 ao_i2c_start(AO_MPU9250_I2C_INDEX, MPU9250_ADDR_READ);
79 ao_i2c_recv(data, len, AO_MPU9250_I2C_INDEX, TRUE);
80 ao_i2c_put(AO_MPU9250_I2C_INDEX);
85 _ao_mpu9250_reg_read(uint8_t addr)
90 ao_mpu9250_spi_start();
91 ao_spi_send(&addr, 1, AO_MPU9250_SPI_BUS);
92 ao_spi_recv(&value, 1, AO_MPU9250_SPI_BUS);
95 ao_i2c_get(AO_MPU9250_I2C_INDEX);
96 ao_i2c_start(AO_MPU9250_I2C_INDEX, MPU9250_ADDR_WRITE);
97 ao_i2c_send(&addr, 1, AO_MPU9250_I2C_INDEX, FALSE);
98 ao_i2c_start(AO_MPU9250_I2C_INDEX, MPU9250_ADDR_READ);
99 ao_i2c_recv(&value, 1, AO_MPU9250_I2C_INDEX, TRUE);
100 ao_i2c_put(AO_MPU9250_I2C_INDEX);
106 _ao_mpu9250_sample(struct ao_mpu9250_sample *sample)
108 uint16_t *d = (uint16_t *) sample;
109 int i = sizeof (*sample) / 2;
111 _ao_mpu9250_read(MPU9250_ACCEL_XOUT_H, sample, sizeof (*sample));
112 #if __BYTE_ORDER == __LITTLE_ENDIAN
116 *d++ = (t >> 8) | (t << 8);
121 #define G 981 /* in cm/s² */
124 static int16_t /* cm/s² */
125 ao_mpu9250_accel(int16_t v)
127 return (int16_t) ((v * (int32_t) (16.0 * 980.665 + 0.5)) / 32767);
130 static int16_t /* deg*10/s */
131 ao_mpu9250_gyro(int16_t v)
133 return (int16_t) ((v * (int32_t) 20000) / 32767);
138 ao_mpu9250_accel_check(int16_t normal, int16_t test)
140 int16_t diff = test - normal;
142 if (diff < MPU9250_ST_ACCEL(16) / 4) {
145 if (diff > MPU9250_ST_ACCEL(16) * 4) {
152 ao_mpu9250_gyro_check(int16_t normal, int16_t test)
154 int16_t diff = test - normal;
158 if (diff < MPU9250_ST_GYRO(2000) / 4) {
161 if (diff > MPU9250_ST_GYRO(2000) * 4) {
168 _ao_mpu9250_wait_alive(void)
172 /* Wait for the chip to wake up */
173 for (i = 0; i < 30; i++) {
174 ao_delay(AO_MS_TO_TICKS(100));
175 if (_ao_mpu9250_reg_read(MPU9250_WHO_AM_I) == MPU9250_I_AM_9250)
179 ao_panic(AO_PANIC_SELF_TEST_MPU9250);
185 _ao_mpu9250_setup(void)
187 struct ao_mpu9250_sample normal_mode, test_mode;
191 if (ao_mpu9250_configured)
194 _ao_mpu9250_wait_alive();
196 /* Reset the whole chip */
198 _ao_mpu9250_reg_write(MPU9250_PWR_MGMT_1,
199 (1 << MPU9250_PWR_MGMT_1_DEVICE_RESET));
201 /* Wait for it to reset. If we talk too quickly, it appears to get confused */
203 _ao_mpu9250_wait_alive();
205 /* Reset signal conditioning, disabling I2C on SPI systems */
206 _ao_mpu9250_reg_write(MPU9250_USER_CTRL,
207 (0 << MPU9250_USER_CTRL_FIFO_EN) |
208 (0 << MPU9250_USER_CTRL_I2C_MST_EN) |
209 (AO_MPU9250_SPI << MPU9250_USER_CTRL_I2C_IF_DIS) |
210 (0 << MPU9250_USER_CTRL_FIFO_RESET) |
211 (0 << MPU9250_USER_CTRL_I2C_MST_RESET) |
212 (1 << MPU9250_USER_CTRL_SIG_COND_RESET));
214 while (_ao_mpu9250_reg_read(MPU9250_USER_CTRL) & (1 << MPU9250_USER_CTRL_SIG_COND_RESET))
215 ao_delay(AO_MS_TO_TICKS(10));
217 /* Reset signal paths */
218 _ao_mpu9250_reg_write(MPU9250_SIGNAL_PATH_RESET,
219 (1 << MPU9250_SIGNAL_PATH_RESET_GYRO_RESET) |
220 (1 << MPU9250_SIGNAL_PATH_RESET_ACCEL_RESET) |
221 (1 << MPU9250_SIGNAL_PATH_RESET_TEMP_RESET));
223 _ao_mpu9250_reg_write(MPU9250_SIGNAL_PATH_RESET,
224 (0 << MPU9250_SIGNAL_PATH_RESET_GYRO_RESET) |
225 (0 << MPU9250_SIGNAL_PATH_RESET_ACCEL_RESET) |
226 (0 << MPU9250_SIGNAL_PATH_RESET_TEMP_RESET));
228 /* Select clocks, disable sleep */
229 _ao_mpu9250_reg_write(MPU9250_PWR_MGMT_1,
230 (0 << MPU9250_PWR_MGMT_1_DEVICE_RESET) |
231 (0 << MPU9250_PWR_MGMT_1_SLEEP) |
232 (0 << MPU9250_PWR_MGMT_1_CYCLE) |
233 (0 << MPU9250_PWR_MGMT_1_TEMP_DIS) |
234 (MPU9250_PWR_MGMT_1_CLKSEL_PLL_X_AXIS << MPU9250_PWR_MGMT_1_CLKSEL));
236 /* Set sample rate divider to sample at full speed */
237 _ao_mpu9250_reg_write(MPU9250_SMPRT_DIV, 0);
239 /* Disable filtering */
240 _ao_mpu9250_reg_write(MPU9250_CONFIG,
241 (MPU9250_CONFIG_EXT_SYNC_SET_DISABLED << MPU9250_CONFIG_EXT_SYNC_SET) |
242 (MPU9250_CONFIG_DLPF_CFG_250 << MPU9250_CONFIG_DLPF_CFG));
244 for (st_tries = 0; st_tries < ST_TRIES; st_tries++) {
247 /* Configure accelerometer to +/-16G in self-test mode */
248 _ao_mpu9250_reg_write(MPU9250_ACCEL_CONFIG,
249 (1 << MPU9250_ACCEL_CONFIG_XA_ST) |
250 (1 << MPU9250_ACCEL_CONFIG_YA_ST) |
251 (1 << MPU9250_ACCEL_CONFIG_ZA_ST) |
252 (MPU9250_ACCEL_CONFIG_AFS_SEL_16G << MPU9250_ACCEL_CONFIG_AFS_SEL));
254 /* Configure gyro to +/- 2000°/s in self-test mode */
255 _ao_mpu9250_reg_write(MPU9250_GYRO_CONFIG,
256 (1 << MPU9250_GYRO_CONFIG_XG_ST) |
257 (1 << MPU9250_GYRO_CONFIG_YG_ST) |
258 (1 << MPU9250_GYRO_CONFIG_ZG_ST) |
259 (MPU9250_GYRO_CONFIG_FS_SEL_2000 << MPU9250_GYRO_CONFIG_FS_SEL));
261 ao_delay(AO_MS_TO_TICKS(200));
262 _ao_mpu9250_sample(&test_mode);
264 /* Configure accelerometer to +/-16G */
265 _ao_mpu9250_reg_write(MPU9250_ACCEL_CONFIG,
266 (0 << MPU9250_ACCEL_CONFIG_XA_ST) |
267 (0 << MPU9250_ACCEL_CONFIG_YA_ST) |
268 (0 << MPU9250_ACCEL_CONFIG_ZA_ST) |
269 (MPU9250_ACCEL_CONFIG_AFS_SEL_16G << MPU9250_ACCEL_CONFIG_AFS_SEL));
271 /* Configure gyro to +/- 2000°/s */
272 _ao_mpu9250_reg_write(MPU9250_GYRO_CONFIG,
273 (0 << MPU9250_GYRO_CONFIG_XG_ST) |
274 (0 << MPU9250_GYRO_CONFIG_YG_ST) |
275 (0 << MPU9250_GYRO_CONFIG_ZG_ST) |
276 (MPU9250_GYRO_CONFIG_FS_SEL_2000 << MPU9250_GYRO_CONFIG_FS_SEL));
278 ao_delay(AO_MS_TO_TICKS(200));
279 _ao_mpu9250_sample(&normal_mode);
281 errors += ao_mpu9250_accel_check(normal_mode.accel_x, test_mode.accel_x);
282 errors += ao_mpu9250_accel_check(normal_mode.accel_y, test_mode.accel_y);
283 errors += ao_mpu9250_accel_check(normal_mode.accel_z, test_mode.accel_z);
285 errors += ao_mpu9250_gyro_check(normal_mode.gyro_x, test_mode.gyro_x);
286 errors += ao_mpu9250_gyro_check(normal_mode.gyro_y, test_mode.gyro_y);
287 errors += ao_mpu9250_gyro_check(normal_mode.gyro_z, test_mode.gyro_z);
292 if (st_tries == ST_TRIES)
293 ao_sensor_errors = 1;
295 /* Filter to about 100Hz, which also sets the gyro rate to 1000Hz */
296 _ao_mpu9250_reg_write(MPU9250_CONFIG,
297 (MPU9250_CONFIG_FIFO_MODE_REPLACE << MPU9250_CONFIG_FIFO_MODE) |
298 (MPU9250_CONFIG_EXT_SYNC_SET_DISABLED << MPU9250_CONFIG_EXT_SYNC_SET) |
299 (MPU9250_CONFIG_DLPF_CFG_92 << MPU9250_CONFIG_DLPF_CFG));
301 /* Set sample rate divider to sample at 200Hz (v = gyro/rate - 1) */
302 _ao_mpu9250_reg_write(MPU9250_SMPRT_DIV,
305 ao_delay(AO_MS_TO_TICKS(100));
306 ao_mpu9250_configured = 1;
309 struct ao_mpu9250_sample ao_mpu9250_current;
314 struct ao_mpu9250_sample sample;
315 /* ao_mpu9250_init already grabbed the SPI bus and mutex */
318 ao_mpu9250_spi_put();
323 ao_mpu9250_spi_get();
325 _ao_mpu9250_sample(&sample);
327 ao_mpu9250_spi_put();
329 ao_arch_block_interrupts();
330 ao_mpu9250_current = sample;
331 AO_DATA_PRESENT(AO_DATA_MPU9250);
333 ao_arch_release_interrupts();
337 static struct ao_task ao_mpu9250_task;
340 ao_mpu9250_show(void)
342 printf ("Accel: %7d %7d %7d Gyro: %7d %7d %7d\n",
343 ao_mpu9250_current.accel_x,
344 ao_mpu9250_current.accel_y,
345 ao_mpu9250_current.accel_z,
346 ao_mpu9250_current.gyro_x,
347 ao_mpu9250_current.gyro_y,
348 ao_mpu9250_current.gyro_z);
352 ao_mpu9250_read(void)
358 if (ao_cmd_status != ao_cmd_success)
361 ao_mpu9250_spi_get();
362 val = _ao_mpu9250_reg_read(addr);
363 ao_mpu9250_spi_put();
364 printf("Addr %02x val %02x\n", addr, val);
368 ao_mpu9250_write(void)
374 if (ao_cmd_status != ao_cmd_success)
378 if (ao_cmd_status != ao_cmd_success)
381 printf("Addr %02x val %02x\n", addr, val);
382 ao_mpu9250_spi_get();
383 _ao_mpu9250_reg_write(addr, val);
384 ao_mpu9250_spi_put();
387 static const struct ao_cmds ao_mpu9250_cmds[] = {
388 { ao_mpu9250_show, "I\0Show MPU9250 status" },
389 { ao_mpu9250_read, "R <addr>\0Read MPU9250 register" },
390 { ao_mpu9250_write, "W <addr> <val>\0Write MPU9250 register" },
395 ao_mpu9250_init(void)
397 ao_mpu9250_configured = 0;
399 ao_add_task(&ao_mpu9250_task, ao_mpu9250, "mpu9250");
402 ao_spi_init_cs(AO_MPU9250_SPI_CS_PORT, (1 << AO_MPU9250_SPI_CS_PIN));
404 /* Pretend to be the mpu9250 task. Grab the SPI bus right away and
405 * hold it for the task so that nothing else uses the SPI bus before
406 * we get the I2C mode disabled in the chip
409 ao_cur_task = &ao_mpu9250_task;
410 ao_spi_get(AO_MPU9250_SPI_BUS, AO_SPI_SPEED_1MHz);
413 ao_cmd_register(&ao_mpu9250_cmds[0]);