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 #define MPU9250_TEST 0
27 static uint8_t ao_mpu9250_configured;
29 #ifndef AO_MPU9250_I2C_INDEX
30 #define AO_MPU9250_SPI 1
32 #define AO_MPU9250_SPI 0
37 #ifndef AO_MPU9250_SPI_SPEED
38 #define AO_MPU9250_SPI_SPEED ao_spi_speed(1000000) /* 1MHz max SCLK */
41 #define ao_mpu9250_spi_get() ao_spi_get(AO_MPU9250_SPI_BUS, AO_MPU9250_SPI_SPEED)
42 #define ao_mpu9250_spi_put() ao_spi_put(AO_MPU9250_SPI_BUS)
44 #define ao_mpu9250_spi_start() ao_spi_set_cs(AO_MPU9250_SPI_CS_PORT, \
45 (1 << AO_MPU9250_SPI_CS_PIN))
47 #define ao_mpu9250_spi_end() ao_spi_clr_cs(AO_MPU9250_SPI_CS_PORT, \
48 (1 << AO_MPU9250_SPI_CS_PIN))
52 #define ao_mpu9250_spi_get()
53 #define ao_mpu9250_spi_put()
58 _ao_mpu9250_reg_write(uint8_t addr, uint8_t value)
60 uint8_t d[2] = { addr, value };
62 ao_mpu9250_spi_start();
63 ao_spi_send(d, 2, AO_MPU9250_SPI_BUS);
66 ao_i2c_get(AO_MPU9250_I2C_INDEX);
67 ao_i2c_start(AO_MPU9250_I2C_INDEX, MPU9250_ADDR_WRITE);
68 ao_i2c_send(d, 2, AO_MPU9250_I2C_INDEX, true);
69 ao_i2c_put(AO_MPU9250_I2C_INDEX);
74 _ao_mpu9250_read(uint8_t addr, void *data, uint8_t len)
78 ao_mpu9250_spi_start();
79 ao_spi_send(&addr, 1, AO_MPU9250_SPI_BUS);
80 ao_spi_recv(data, len, AO_MPU9250_SPI_BUS);
83 ao_i2c_get(AO_MPU9250_I2C_INDEX);
84 ao_i2c_start(AO_MPU9250_I2C_INDEX, MPU9250_ADDR_WRITE);
85 ao_i2c_send(&addr, 1, AO_MPU9250_I2C_INDEX, false);
86 ao_i2c_start(AO_MPU9250_I2C_INDEX, MPU9250_ADDR_READ);
87 ao_i2c_recv(data, len, AO_MPU9250_I2C_INDEX, true);
88 ao_i2c_put(AO_MPU9250_I2C_INDEX);
93 _ao_mpu9250_reg_read(uint8_t addr)
98 ao_mpu9250_spi_start();
99 ao_spi_send(&addr, 1, AO_MPU9250_SPI_BUS);
100 ao_spi_recv(&value, 1, AO_MPU9250_SPI_BUS);
101 ao_mpu9250_spi_end();
103 ao_i2c_get(AO_MPU9250_I2C_INDEX);
104 ao_i2c_start(AO_MPU9250_I2C_INDEX, MPU9250_ADDR_WRITE);
105 ao_i2c_send(&addr, 1, AO_MPU9250_I2C_INDEX, false);
106 ao_i2c_start(AO_MPU9250_I2C_INDEX, MPU9250_ADDR_READ);
107 ao_i2c_recv(&value, 1, AO_MPU9250_I2C_INDEX, true);
108 ao_i2c_put(AO_MPU9250_I2C_INDEX);
114 _ao_mpu9250_slv4_setup(uint8_t addr, uint8_t reg)
116 /* Set i2c slave address */
117 _ao_mpu9250_reg_write(MPU9250_I2C_SLV4_ADDR,
120 /* Set i2c register address */
121 _ao_mpu9250_reg_write(MPU9250_I2C_SLV4_REG,
126 _ao_mpu9250_slv4_run(void)
130 /* Start the transfer */
131 _ao_mpu9250_reg_write(MPU9250_I2C_SLV4_CTRL,
132 (1 << MPU9250_I2C_SLV4_CTRL_I2C_SLV4_EN) |
133 (0 << MPU9250_I2C_SLV4_CTRL_SLV4_DONE_INT_EN) |
134 (0 << MPU9250_I2C_SLV4_CTRL_I2C_SLV4_REG_DIS) |
135 (0 << MPU9250_I2C_SLV4_CTRL_I2C_MST_DLY));
137 /* Poll for completion */
139 ctrl = _ao_mpu9250_reg_read(MPU9250_I2C_SLV4_CTRL);
140 if ((ctrl & (1 << MPU9250_I2C_SLV4_CTRL_I2C_SLV4_EN)) == 0)
147 _ao_mpu9250_mag_reg_read(uint8_t reg)
149 _ao_mpu9250_slv4_setup((1 << 7) | MPU9250_MAG_ADDR, reg);
151 _ao_mpu9250_slv4_run();
153 return _ao_mpu9250_reg_read(MPU9250_I2C_SLV4_DI);
157 _ao_mpu9250_mag_reg_write(uint8_t reg, uint8_t value)
159 _ao_mpu9250_slv4_setup((0 << 7) | MPU9250_MAG_ADDR, reg);
162 _ao_mpu9250_reg_write(MPU9250_I2C_SLV4_DO,
165 _ao_mpu9250_slv4_run();
169 _ao_mpu9250_sample(struct ao_mpu9250_sample *sample)
171 uint16_t *d = (uint16_t *) sample;
172 int i = sizeof (*sample) / 2;
174 _ao_mpu9250_read(MPU9250_ACCEL_XOUT_H, sample, sizeof (*sample));
175 #if __BYTE_ORDER == __LITTLE_ENDIAN
179 *d++ = (uint16_t) ((uint16_t) (t >> 8) | (uint16_t) (t << 8));
184 #define G 981 /* in cm/s² */
187 static int16_t /* cm/s² */
188 ao_mpu9250_accel(int16_t v)
190 return (int16_t) ((v * (int32_t) (16.0 * 980.665 + 0.5)) / 32767);
193 static int16_t /* deg*10/s */
194 ao_mpu9250_gyro(int16_t v)
196 return (int16_t) ((v * (int32_t) 20000) / 32767);
201 ao_mpu9250_accel_check(int16_t normal, int16_t test)
203 int16_t diff = test - normal;
205 if (diff < MPU9250_ST_ACCEL(16) / 4) {
208 if (diff > MPU9250_ST_ACCEL(16) * 4) {
215 ao_mpu9250_gyro_check(int16_t normal, int16_t test)
217 int16_t diff = test - normal;
221 if (diff < MPU9250_ST_GYRO(2000) / 4) {
224 if (diff > MPU9250_ST_GYRO(2000) * 4) {
231 _ao_mpu9250_wait_alive(void)
235 /* Wait for the chip to wake up */
236 for (i = 0; i < 30; i++) {
237 ao_delay(AO_MS_TO_TICKS(100));
238 if (_ao_mpu9250_reg_read(MPU9250_WHO_AM_I) == MPU9250_I_AM_9250)
242 ao_panic(AO_PANIC_SELF_TEST_MPU9250);
249 _ao_mpu9250_setup(void)
251 struct ao_mpu9250_sample normal_mode, test_mode;
256 if (ao_mpu9250_configured)
259 _ao_mpu9250_wait_alive();
261 /* Reset the whole chip */
263 _ao_mpu9250_reg_write(MPU9250_PWR_MGMT_1,
264 (1 << MPU9250_PWR_MGMT_1_DEVICE_RESET));
266 /* Wait for it to reset. If we talk too quickly, it appears to get confused */
268 _ao_mpu9250_wait_alive();
270 /* Reset signal conditioning, disabling I2C on SPI systems */
271 _ao_mpu9250_reg_write(MPU9250_USER_CTRL,
272 (0 << MPU9250_USER_CTRL_FIFO_EN) |
273 (1 << MPU9250_USER_CTRL_I2C_MST_EN) |
274 (AO_MPU9250_SPI << MPU9250_USER_CTRL_I2C_IF_DIS) |
275 (0 << MPU9250_USER_CTRL_FIFO_RESET) |
276 (0 << MPU9250_USER_CTRL_I2C_MST_RESET) |
277 (1 << MPU9250_USER_CTRL_SIG_COND_RESET));
279 while (_ao_mpu9250_reg_read(MPU9250_USER_CTRL) & (1 << MPU9250_USER_CTRL_SIG_COND_RESET))
280 ao_delay(AO_MS_TO_TICKS(10));
282 /* Reset signal paths */
283 _ao_mpu9250_reg_write(MPU9250_SIGNAL_PATH_RESET,
284 (1 << MPU9250_SIGNAL_PATH_RESET_GYRO_RESET) |
285 (1 << MPU9250_SIGNAL_PATH_RESET_ACCEL_RESET) |
286 (1 << MPU9250_SIGNAL_PATH_RESET_TEMP_RESET));
288 _ao_mpu9250_reg_write(MPU9250_SIGNAL_PATH_RESET,
289 (0 << MPU9250_SIGNAL_PATH_RESET_GYRO_RESET) |
290 (0 << MPU9250_SIGNAL_PATH_RESET_ACCEL_RESET) |
291 (0 << MPU9250_SIGNAL_PATH_RESET_TEMP_RESET));
293 /* Select clocks, disable sleep */
294 _ao_mpu9250_reg_write(MPU9250_PWR_MGMT_1,
295 (0 << MPU9250_PWR_MGMT_1_DEVICE_RESET) |
296 (0 << MPU9250_PWR_MGMT_1_SLEEP) |
297 (0 << MPU9250_PWR_MGMT_1_CYCLE) |
298 (0 << MPU9250_PWR_MGMT_1_TEMP_DIS) |
299 (MPU9250_PWR_MGMT_1_CLKSEL_PLL_X_AXIS << MPU9250_PWR_MGMT_1_CLKSEL));
301 /* Set I2C clock and options */
302 _ao_mpu9250_reg_write(MPU9250_MST_CTRL,
303 (0 << MPU9250_MST_CTRL_MULT_MST_EN) |
304 (0 << MPU9250_MST_CTRL_WAIT_FOR_ES) |
305 (0 << MPU9250_MST_CTRL_SLV_3_FIFO_EN) |
306 (0 << MPU9250_MST_CTRL_I2C_MST_P_NSR) |
307 (MPU9250_MST_CTRL_I2C_MST_CLK_400 << MPU9250_MST_CTRL_I2C_MST_CLK));
309 /* Set sample rate divider to sample at full speed */
310 _ao_mpu9250_reg_write(MPU9250_SMPRT_DIV, 0);
312 /* Disable filtering */
313 _ao_mpu9250_reg_write(MPU9250_CONFIG,
314 (MPU9250_CONFIG_EXT_SYNC_SET_DISABLED << MPU9250_CONFIG_EXT_SYNC_SET) |
315 (MPU9250_CONFIG_DLPF_CFG_250 << MPU9250_CONFIG_DLPF_CFG));
317 for (st_tries = 0; st_tries < ST_TRIES; st_tries++) {
320 /* Configure accelerometer to +/-16G in self-test mode */
321 _ao_mpu9250_reg_write(MPU9250_ACCEL_CONFIG,
322 (1 << MPU9250_ACCEL_CONFIG_XA_ST) |
323 (1 << MPU9250_ACCEL_CONFIG_YA_ST) |
324 (1 << MPU9250_ACCEL_CONFIG_ZA_ST) |
325 (MPU9250_ACCEL_CONFIG_AFS_SEL_16G << MPU9250_ACCEL_CONFIG_AFS_SEL));
327 /* Configure gyro to +/- 2000°/s in self-test mode */
328 _ao_mpu9250_reg_write(MPU9250_GYRO_CONFIG,
329 (1 << MPU9250_GYRO_CONFIG_XG_ST) |
330 (1 << MPU9250_GYRO_CONFIG_YG_ST) |
331 (1 << MPU9250_GYRO_CONFIG_ZG_ST) |
332 (MPU9250_GYRO_CONFIG_FS_SEL_2000 << MPU9250_GYRO_CONFIG_FS_SEL));
334 ao_delay(AO_MS_TO_TICKS(200));
335 _ao_mpu9250_sample(&test_mode);
337 /* Configure accelerometer to +/-16G */
338 _ao_mpu9250_reg_write(MPU9250_ACCEL_CONFIG,
339 (0 << MPU9250_ACCEL_CONFIG_XA_ST) |
340 (0 << MPU9250_ACCEL_CONFIG_YA_ST) |
341 (0 << MPU9250_ACCEL_CONFIG_ZA_ST) |
342 (MPU9250_ACCEL_CONFIG_AFS_SEL_16G << MPU9250_ACCEL_CONFIG_AFS_SEL));
344 /* Configure gyro to +/- 2000°/s */
345 _ao_mpu9250_reg_write(MPU9250_GYRO_CONFIG,
346 (0 << MPU9250_GYRO_CONFIG_XG_ST) |
347 (0 << MPU9250_GYRO_CONFIG_YG_ST) |
348 (0 << MPU9250_GYRO_CONFIG_ZG_ST) |
349 (MPU9250_GYRO_CONFIG_FS_SEL_2000 << MPU9250_GYRO_CONFIG_FS_SEL));
351 ao_delay(AO_MS_TO_TICKS(200));
352 _ao_mpu9250_sample(&normal_mode);
354 errors += ao_mpu9250_accel_check(normal_mode.accel_x, test_mode.accel_x);
355 errors += ao_mpu9250_accel_check(normal_mode.accel_y, test_mode.accel_y);
356 errors += ao_mpu9250_accel_check(normal_mode.accel_z, test_mode.accel_z);
358 errors += ao_mpu9250_gyro_check(normal_mode.gyro_x, test_mode.gyro_x);
359 errors += ao_mpu9250_gyro_check(normal_mode.gyro_y, test_mode.gyro_y);
360 errors += ao_mpu9250_gyro_check(normal_mode.gyro_z, test_mode.gyro_z);
365 if (st_tries == ST_TRIES)
366 AO_SENSOR_ERROR(AO_DATA_MPU9250);
368 /* Set up the mag sensor */
370 /* make sure it's alive */
371 for (mag_tries = 0; mag_tries < MAG_TRIES; mag_tries++) {
372 if (_ao_mpu9250_mag_reg_read(MPU9250_MAG_WIA) == MPU9250_MAG_WIA_VALUE)
376 if (mag_tries == MAG_TRIES)
377 AO_SENSOR_ERROR(AO_DATA_MPU9250);
379 /* Select continuous mode 2 (100Hz), 16 bit samples */
381 _ao_mpu9250_mag_reg_write(MPU9250_MAG_CNTL1,
382 (MPU9250_MAG_CNTL1_BIT_16 << MPU9250_MAG_CNTL1_BIT) |
383 (MPU9250_MAG_CNTL1_MODE_CONT_2 << MPU9250_MAG_CNTL1_MODE));
385 /* Set i2c master to delay shadowing data until read is
386 * complete (avoids tearing the data) */
388 _ao_mpu9250_reg_write(MPU9250_I2C_MST_DELAY_CTRL,
389 (1 << MPU9250_I2C_MST_DELAY_CTRL_DELAY_ES_SHADOW) |
390 (0 << MPU9250_I2C_MST_DELAY_CTRL_I2C_SLV4_DLY_EN) |
391 (0 << MPU9250_I2C_MST_DELAY_CTRL_I2C_SLV3_DLY_EN) |
392 (0 << MPU9250_I2C_MST_DELAY_CTRL_I2C_SLV2_DLY_EN) |
393 (0 << MPU9250_I2C_MST_DELAY_CTRL_I2C_SLV1_DLY_EN) |
394 (0 << MPU9250_I2C_MST_DELAY_CTRL_I2C_SLV0_DLY_EN));
396 /* Set up i2c slave 0 to read the mag registers starting at HXL (3) */
398 _ao_mpu9250_reg_write(MPU9250_I2C_SLV0_ADDR,
399 (1 << 7) | MPU9250_MAG_ADDR);
401 _ao_mpu9250_reg_write(MPU9250_I2C_SLV0_REG,
404 /* Byte swap so the mag values match the gyro/accel. Read 7 bytes
405 * to include the status register
408 _ao_mpu9250_reg_write(MPU9250_I2C_SLV0_CTRL,
409 (1 << MPU9250_I2C_SLV0_CTRL_I2C_SLV0_EN) |
410 (1 << MPU9250_I2C_SLV0_CTRL_I2C_SLV0_BYTE_SW) |
411 (0 << MPU9250_I2C_SLV0_CTRL_I2C_SLV0_REG_DIS) |
412 (1 << MPU9250_I2C_SLV0_CTRL_I2C_SLV0_GRP) |
413 (MPU9250_MAG_ST2 - MPU9250_MAG_HXL + 1) << MPU9250_I2C_SLV0_CTRL_I2C_SLV0_LENG);
415 /* Filter to about 100Hz, which also sets the gyro rate to 1000Hz */
416 _ao_mpu9250_reg_write(MPU9250_CONFIG,
417 (MPU9250_CONFIG_FIFO_MODE_REPLACE << MPU9250_CONFIG_FIFO_MODE) |
418 (MPU9250_CONFIG_EXT_SYNC_SET_DISABLED << MPU9250_CONFIG_EXT_SYNC_SET) |
419 (MPU9250_CONFIG_DLPF_CFG_92 << MPU9250_CONFIG_DLPF_CFG));
421 /* Set sample rate divider to sample at 200Hz (v = gyro/rate - 1) */
422 _ao_mpu9250_reg_write(MPU9250_SMPRT_DIV,
425 ao_delay(AO_MS_TO_TICKS(100));
426 ao_mpu9250_configured = 1;
429 struct ao_mpu9250_sample ao_mpu9250_current;
434 struct ao_mpu9250_sample sample;
436 /* ao_mpu9250_init already grabbed the SPI bus and mutex */
438 ao_mpu9250_spi_put();
441 ao_mpu9250_spi_get();
442 _ao_mpu9250_sample(&sample);
443 ao_mpu9250_spi_put();
444 ao_arch_block_interrupts();
445 ao_mpu9250_current = sample;
446 AO_DATA_PRESENT(AO_DATA_MPU9250);
448 ao_arch_release_interrupts();
452 static struct ao_task ao_mpu9250_task;
455 ao_mpu9250_show(void)
457 printf ("Accel: %7d %7d %7d Gyro: %7d %7d %7d Mag: %7d %7d %7d\n",
458 ao_mpu9250_current.accel_x,
459 ao_mpu9250_current.accel_y,
460 ao_mpu9250_current.accel_z,
461 ao_mpu9250_current.gyro_x,
462 ao_mpu9250_current.gyro_y,
463 ao_mpu9250_current.gyro_z,
464 ao_mpu9250_current.mag_x,
465 ao_mpu9250_current.mag_y,
466 ao_mpu9250_current.mag_z);
472 ao_mpu9250_read(void)
478 if (ao_cmd_status != ao_cmd_success)
480 ao_mpu9250_spi_get();
481 val = _ao_mpu9250_reg_read(addr);
482 ao_mpu9250_spi_put();
483 printf("Addr %02x val %02x\n", addr, val);
487 ao_mpu9250_write(void)
493 if (ao_cmd_status != ao_cmd_success)
496 if (ao_cmd_status != ao_cmd_success)
498 printf("Addr %02x val %02x\n", addr, val);
499 ao_mpu9250_spi_get();
500 _ao_mpu9250_reg_write(addr, val);
501 ao_mpu9250_spi_put();
505 ao_mpu9250_mag_read(void)
511 if (ao_cmd_status != ao_cmd_success)
513 ao_mpu9250_spi_get();
514 val = _ao_mpu9250_mag_reg_read(addr);
515 ao_mpu9250_spi_put();
516 printf("Addr %02x val %02x\n", addr, val);
520 ao_mpu9250_mag_write(void)
526 if (ao_cmd_status != ao_cmd_success)
529 if (ao_cmd_status != ao_cmd_success)
531 printf("Addr %02x val %02x\n", addr, val);
532 ao_mpu9250_spi_get();
533 _ao_mpu9250_mag_reg_write(addr, val);
534 ao_mpu9250_spi_put();
537 #endif /* MPU9250_TEST */
539 static const struct ao_cmds ao_mpu9250_cmds[] = {
540 { ao_mpu9250_show, "I\0Show MPU9250 status" },
542 { ao_mpu9250_read, "R <addr>\0Read MPU9250 register" },
543 { ao_mpu9250_write, "W <addr> <val>\0Write MPU9250 register" },
544 { ao_mpu9250_mag_read, "G <addr>\0Read MPU9250 Mag register" },
545 { ao_mpu9250_mag_write, "P <addr> <val>\0Write MPU9250 Mag register" },
551 ao_mpu9250_init(void)
553 ao_mpu9250_configured = 0;
555 ao_add_task(&ao_mpu9250_task, ao_mpu9250, "mpu9250");
558 ao_spi_init_cs(AO_MPU9250_SPI_CS_PORT, (1 << AO_MPU9250_SPI_CS_PIN));
560 /* Pretend to be the mpu9250 task. Grab the SPI bus right away and
561 * hold it for the task so that nothing else uses the SPI bus before
562 * we get the I2C mode disabled in the chip
565 ao_cur_task = &ao_mpu9250_task;
566 ao_mpu9250_spi_get();
569 ao_cmd_register(&ao_mpu9250_cmds[0]);