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_mpu6000.h>
25 static uint8_t ao_mpu6000_configured;
27 #ifndef AO_MPU6000_I2C_INDEX
28 #define AO_MPU6000_SPI 1
30 #define AO_MPU6000_SPI 0
35 #define AO_MPU6000_SPI_SPEED ao_spi_speed(1000000) /* 1Mhz for all register access */
37 #define ao_mpu6000_spi_get() ao_spi_get(AO_MPU6000_SPI_BUS, AO_MPU6000_SPI_SPEED)
38 #define ao_mpu6000_spi_put() ao_spi_put(AO_MPU6000_SPI_BUS)
40 #define ao_mpu6000_spi_start() ao_spi_set_cs(AO_MPU6000_SPI_CS_PORT, \
41 (1 << AO_MPU6000_SPI_CS_PIN))
43 #define ao_mpu6000_spi_end() ao_spi_clr_cs(AO_MPU6000_SPI_CS_PORT, \
44 (1 << AO_MPU6000_SPI_CS_PIN))
50 _ao_mpu6000_reg_write(uint8_t addr, uint8_t value)
52 uint8_t d[2] = { addr, value };
54 ao_mpu6000_spi_start();
55 ao_spi_send(d, 2, AO_MPU6000_SPI_BUS);
58 ao_i2c_get(AO_MPU6000_I2C_INDEX);
59 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE);
60 ao_i2c_send(d, 2, AO_MPU6000_I2C_INDEX, true);
61 ao_i2c_put(AO_MPU6000_I2C_INDEX);
66 _ao_mpu6000_read(uint8_t addr, void *data, uint8_t len)
70 ao_mpu6000_spi_start();
71 ao_spi_send(&addr, 1, AO_MPU6000_SPI_BUS);
72 ao_spi_recv(data, len, AO_MPU6000_SPI_BUS);
75 ao_i2c_get(AO_MPU6000_I2C_INDEX);
76 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE);
77 ao_i2c_send(&addr, 1, AO_MPU6000_I2C_INDEX, false);
78 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_READ);
79 ao_i2c_recv(data, len, AO_MPU6000_I2C_INDEX, true);
80 ao_i2c_put(AO_MPU6000_I2C_INDEX);
85 _ao_mpu6000_reg_read(uint8_t addr)
90 ao_mpu6000_spi_start();
91 ao_spi_send(&addr, 1, AO_MPU6000_SPI_BUS);
92 ao_spi_recv(&value, 1, AO_MPU6000_SPI_BUS);
95 ao_i2c_get(AO_MPU6000_I2C_INDEX);
96 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE);
97 ao_i2c_send(&addr, 1, AO_MPU6000_I2C_INDEX, false);
98 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_READ);
99 ao_i2c_recv(&value, 1, AO_MPU6000_I2C_INDEX, true);
100 ao_i2c_put(AO_MPU6000_I2C_INDEX);
106 _ao_mpu6000_sample(struct ao_mpu6000_sample *sample)
108 uint16_t *d = (uint16_t *) sample;
109 int i = sizeof (*sample) / 2;
111 _ao_mpu6000_read(MPU6000_ACCEL_XOUT_H, sample, sizeof (*sample));
112 #if __BYTE_ORDER == __LITTLE_ENDIAN
116 *d++ = (uint16_t) ((t >> 8) | (t << 8));
121 #define G 981 /* in cm/s² */
124 static int16_t /* cm/s² */
125 ao_mpu6000_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_mpu6000_gyro(int16_t v)
133 return (int16_t) ((v * (int32_t) 20000) / 32767);
138 ao_mpu6000_accel_check(int16_t normal, int16_t test)
140 int16_t diff = test - normal;
142 if (diff < MPU6000_ST_ACCEL(16) / 4) {
145 if (diff > MPU6000_ST_ACCEL(16) * 4) {
152 ao_mpu6000_gyro_check(int16_t normal, int16_t test)
154 int16_t diff = test - normal;
158 if (diff < MPU6000_ST_GYRO(2000) / 4) {
161 if (diff > MPU6000_ST_GYRO(2000) * 4) {
167 static uint8_t mpu_id;
170 _ao_mpu6000_wait_alive(void)
174 /* Wait for the chip to wake up */
175 for (i = 0; i < 30; i++) {
176 ao_delay(AO_MS_TO_TICKS(100));
177 mpu_id = _ao_mpu6000_reg_read(MPU6000_WHO_AM_I);
181 AO_SENSOR_ERROR(AO_DATA_MPU6000);
187 _ao_mpu6000_setup(void)
189 struct ao_mpu6000_sample normal_mode, test_mode;
193 if (ao_mpu6000_configured)
196 _ao_mpu6000_wait_alive();
198 /* Reset the whole chip */
200 _ao_mpu6000_reg_write(MPU6000_PWR_MGMT_1,
201 (1 << MPU6000_PWR_MGMT_1_DEVICE_RESET));
203 /* Wait for it to reset. If we talk too quickly, it appears to get confused */
205 _ao_mpu6000_wait_alive();
207 /* Reset signal conditioning, disabling I2C on SPI systems */
208 _ao_mpu6000_reg_write(MPU6000_USER_CTRL,
209 (0 << MPU6000_USER_CTRL_FIFO_EN) |
210 (0 << MPU6000_USER_CTRL_I2C_MST_EN) |
211 (AO_MPU6000_SPI << MPU6000_USER_CTRL_I2C_IF_DIS) |
212 (0 << MPU6000_USER_CTRL_FIFO_RESET) |
213 (0 << MPU6000_USER_CTRL_I2C_MST_RESET) |
214 (1 << MPU6000_USER_CTRL_SIG_COND_RESET));
216 while (_ao_mpu6000_reg_read(MPU6000_USER_CTRL) & (1 << MPU6000_USER_CTRL_SIG_COND_RESET))
217 ao_delay(AO_MS_TO_TICKS(10));
219 /* Reset signal paths */
220 _ao_mpu6000_reg_write(MPU6000_SIGNAL_PATH_RESET,
221 (1 << MPU6000_SIGNAL_PATH_RESET_GYRO_RESET) |
222 (1 << MPU6000_SIGNAL_PATH_RESET_ACCEL_RESET) |
223 (1 << MPU6000_SIGNAL_PATH_RESET_TEMP_RESET));
225 _ao_mpu6000_reg_write(MPU6000_SIGNAL_PATH_RESET,
226 (0 << MPU6000_SIGNAL_PATH_RESET_GYRO_RESET) |
227 (0 << MPU6000_SIGNAL_PATH_RESET_ACCEL_RESET) |
228 (0 << MPU6000_SIGNAL_PATH_RESET_TEMP_RESET));
230 /* Select clocks, disable sleep */
231 _ao_mpu6000_reg_write(MPU6000_PWR_MGMT_1,
232 (0 << MPU6000_PWR_MGMT_1_DEVICE_RESET) |
233 (0 << MPU6000_PWR_MGMT_1_SLEEP) |
234 (0 << MPU6000_PWR_MGMT_1_CYCLE) |
235 (0 << MPU6000_PWR_MGMT_1_TEMP_DIS) |
236 (MPU6000_PWR_MGMT_1_CLKSEL_PLL_X_AXIS << MPU6000_PWR_MGMT_1_CLKSEL));
238 /* Set sample rate divider to sample at full speed */
239 _ao_mpu6000_reg_write(MPU6000_SMPRT_DIV, 0);
241 /* Disable filtering */
242 _ao_mpu6000_reg_write(MPU6000_CONFIG,
243 (MPU6000_CONFIG_EXT_SYNC_SET_DISABLED << MPU6000_CONFIG_EXT_SYNC_SET) |
244 (MPU6000_CONFIG_DLPF_CFG_260_256 << MPU6000_CONFIG_DLPF_CFG));
247 // read the product ID rev c has 1/2 the sensitivity of rev d
248 _mpu6000_product_id = _register_read(MPUREG_PRODUCT_ID);
249 //Serial.printf("Product_ID= 0x%x\n", (unsigned) _mpu6000_product_id);
251 if ((_mpu6000_product_id == MPU6000ES_REV_C4) || (_mpu6000_product_id == MPU6000ES_REV_C5) ||
252 (_mpu6000_product_id == MPU6000_REV_C4) || (_mpu6000_product_id == MPU6000_REV_C5)) {
253 // Accel scale 8g (4096 LSB/g)
254 // Rev C has different scaling than rev D
255 register_write(MPUREG_ACCEL_CONFIG,1<<3);
257 // Accel scale 8g (4096 LSB/g)
258 register_write(MPUREG_ACCEL_CONFIG,2<<3);
260 hal.scheduler->delay(1);
263 for (st_tries = 0; st_tries < ST_TRIES; st_tries++) {
266 /* Configure accelerometer to +/-16G in self-test mode */
267 _ao_mpu6000_reg_write(MPU6000_ACCEL_CONFIG,
268 (1 << MPU600_ACCEL_CONFIG_XA_ST) |
269 (1 << MPU600_ACCEL_CONFIG_YA_ST) |
270 (1 << MPU600_ACCEL_CONFIG_ZA_ST) |
271 (MPU600_ACCEL_CONFIG_AFS_SEL_16G << MPU600_ACCEL_CONFIG_AFS_SEL));
273 /* Configure gyro to +/- 2000°/s in self-test mode */
274 _ao_mpu6000_reg_write(MPU6000_GYRO_CONFIG,
275 (1 << MPU600_GYRO_CONFIG_XG_ST) |
276 (1 << MPU600_GYRO_CONFIG_YG_ST) |
277 (1 << MPU600_GYRO_CONFIG_ZG_ST) |
278 (MPU600_GYRO_CONFIG_FS_SEL_2000 << MPU600_GYRO_CONFIG_FS_SEL));
280 ao_delay(AO_MS_TO_TICKS(200));
281 _ao_mpu6000_sample(&test_mode);
283 /* Configure accelerometer to +/-16G */
284 _ao_mpu6000_reg_write(MPU6000_ACCEL_CONFIG,
285 (0 << MPU600_ACCEL_CONFIG_XA_ST) |
286 (0 << MPU600_ACCEL_CONFIG_YA_ST) |
287 (0 << MPU600_ACCEL_CONFIG_ZA_ST) |
288 (MPU600_ACCEL_CONFIG_AFS_SEL_16G << MPU600_ACCEL_CONFIG_AFS_SEL));
290 /* Configure gyro to +/- 2000°/s */
291 _ao_mpu6000_reg_write(MPU6000_GYRO_CONFIG,
292 (0 << MPU600_GYRO_CONFIG_XG_ST) |
293 (0 << MPU600_GYRO_CONFIG_YG_ST) |
294 (0 << MPU600_GYRO_CONFIG_ZG_ST) |
295 (MPU600_GYRO_CONFIG_FS_SEL_2000 << MPU600_GYRO_CONFIG_FS_SEL));
297 ao_delay(AO_MS_TO_TICKS(200));
298 _ao_mpu6000_sample(&normal_mode);
300 errors += ao_mpu6000_accel_check(normal_mode.accel_x, test_mode.accel_x);
301 errors += ao_mpu6000_accel_check(normal_mode.accel_y, test_mode.accel_y);
302 errors += ao_mpu6000_accel_check(normal_mode.accel_z, test_mode.accel_z);
304 errors += ao_mpu6000_gyro_check(normal_mode.gyro_x, test_mode.gyro_x);
305 errors += ao_mpu6000_gyro_check(normal_mode.gyro_y, test_mode.gyro_y);
306 errors += ao_mpu6000_gyro_check(normal_mode.gyro_z, test_mode.gyro_z);
311 if (st_tries == ST_TRIES)
312 AO_SENSOR_ERROR(AO_DATA_MPU6000);
314 /* Filter to about 100Hz, which also sets the gyro rate to 1000Hz */
315 _ao_mpu6000_reg_write(MPU6000_CONFIG,
316 (MPU6000_CONFIG_EXT_SYNC_SET_DISABLED << MPU6000_CONFIG_EXT_SYNC_SET) |
317 (MPU6000_CONFIG_DLPF_CFG_94_98 << MPU6000_CONFIG_DLPF_CFG));
319 /* Set sample rate divider to sample at 200Hz (v = gyro/rate - 1) */
320 _ao_mpu6000_reg_write(MPU6000_SMPRT_DIV,
323 ao_delay(AO_MS_TO_TICKS(100));
324 ao_mpu6000_configured = 1;
327 struct ao_mpu6000_sample ao_mpu6000_current;
332 struct ao_mpu6000_sample sample;
333 /* ao_mpu6000_init already grabbed the SPI bus and mutex */
336 ao_mpu6000_spi_put();
341 ao_mpu6000_spi_get();
343 _ao_mpu6000_sample(&sample);
345 ao_mpu6000_spi_put();
347 ao_arch_block_interrupts();
348 ao_mpu6000_current = sample;
349 AO_DATA_PRESENT(AO_DATA_MPU6000);
351 ao_arch_release_interrupts();
355 static struct ao_task ao_mpu6000_task;
358 ao_mpu6000_show(void)
360 #ifdef AO_LOG_NORMALIZED
361 printf ("MPU6000: %7d %7d %7d %7d %7d %7d\n",
362 ao_mpu6000_along(&ao_mpu6000_current),
363 ao_mpu6000_across(&ao_mpu6000_current),
364 ao_mpu6000_through(&ao_mpu6000_current),
365 ao_mpu6000_roll(&ao_mpu6000_current),
366 ao_mpu6000_pitch(&ao_mpu6000_current),
367 ao_mpu6000_yaw(&ao_mpu6000_current));
369 printf ("Accel: %7d %7d %7d Gyro: %7d %7d %7d\n",
370 ao_mpu6000_current.accel_x,
371 ao_mpu6000_current.accel_y,
372 ao_mpu6000_current.accel_z,
373 ao_mpu6000_current.gyro_x,
374 ao_mpu6000_current.gyro_y,
375 ao_mpu6000_current.gyro_z);
379 static const struct ao_cmds ao_mpu6000_cmds[] = {
380 { ao_mpu6000_show, "I\0Show MPU6000 status" },
385 ao_mpu6000_init(void)
387 ao_mpu6000_configured = 0;
389 ao_add_task(&ao_mpu6000_task, ao_mpu6000, "mpu6000");
392 ao_spi_init_cs(AO_MPU6000_SPI_CS_PORT, (1 << AO_MPU6000_SPI_CS_PIN));
394 /* Pretend to be the mpu6000 task. Grab the SPI bus right away and
395 * hold it for the task so that nothing else uses the SPI bus before
396 * we get the I2C mode disabled in the chip
399 ao_cur_task = &ao_mpu6000_task;
400 ao_mpu6000_spi_get();
404 ao_cmd_register(&ao_mpu6000_cmds[0]);