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++ = (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) {
168 _ao_mpu6000_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_mpu6000_reg_read(MPU6000_WHO_AM_I) == 0x68)
179 ao_panic(AO_PANIC_SELF_TEST_MPU6000);
185 _ao_mpu6000_setup(void)
187 struct ao_mpu6000_sample normal_mode, test_mode;
191 if (ao_mpu6000_configured)
194 _ao_mpu6000_wait_alive();
196 /* Reset the whole chip */
198 _ao_mpu6000_reg_write(MPU6000_PWR_MGMT_1,
199 (1 << MPU6000_PWR_MGMT_1_DEVICE_RESET));
201 /* Wait for it to reset. If we talk too quickly, it appears to get confused */
203 _ao_mpu6000_wait_alive();
205 /* Reset signal conditioning, disabling I2C on SPI systems */
206 _ao_mpu6000_reg_write(MPU6000_USER_CTRL,
207 (0 << MPU6000_USER_CTRL_FIFO_EN) |
208 (0 << MPU6000_USER_CTRL_I2C_MST_EN) |
209 (AO_MPU6000_SPI << MPU6000_USER_CTRL_I2C_IF_DIS) |
210 (0 << MPU6000_USER_CTRL_FIFO_RESET) |
211 (0 << MPU6000_USER_CTRL_I2C_MST_RESET) |
212 (1 << MPU6000_USER_CTRL_SIG_COND_RESET));
214 while (_ao_mpu6000_reg_read(MPU6000_USER_CTRL) & (1 << MPU6000_USER_CTRL_SIG_COND_RESET))
215 ao_delay(AO_MS_TO_TICKS(10));
217 /* Reset signal paths */
218 _ao_mpu6000_reg_write(MPU6000_SIGNAL_PATH_RESET,
219 (1 << MPU6000_SIGNAL_PATH_RESET_GYRO_RESET) |
220 (1 << MPU6000_SIGNAL_PATH_RESET_ACCEL_RESET) |
221 (1 << MPU6000_SIGNAL_PATH_RESET_TEMP_RESET));
223 _ao_mpu6000_reg_write(MPU6000_SIGNAL_PATH_RESET,
224 (0 << MPU6000_SIGNAL_PATH_RESET_GYRO_RESET) |
225 (0 << MPU6000_SIGNAL_PATH_RESET_ACCEL_RESET) |
226 (0 << MPU6000_SIGNAL_PATH_RESET_TEMP_RESET));
228 /* Select clocks, disable sleep */
229 _ao_mpu6000_reg_write(MPU6000_PWR_MGMT_1,
230 (0 << MPU6000_PWR_MGMT_1_DEVICE_RESET) |
231 (0 << MPU6000_PWR_MGMT_1_SLEEP) |
232 (0 << MPU6000_PWR_MGMT_1_CYCLE) |
233 (0 << MPU6000_PWR_MGMT_1_TEMP_DIS) |
234 (MPU6000_PWR_MGMT_1_CLKSEL_PLL_X_AXIS << MPU6000_PWR_MGMT_1_CLKSEL));
236 /* Set sample rate divider to sample at full speed */
237 _ao_mpu6000_reg_write(MPU6000_SMPRT_DIV, 0);
239 /* Disable filtering */
240 _ao_mpu6000_reg_write(MPU6000_CONFIG,
241 (MPU6000_CONFIG_EXT_SYNC_SET_DISABLED << MPU6000_CONFIG_EXT_SYNC_SET) |
242 (MPU6000_CONFIG_DLPF_CFG_260_256 << MPU6000_CONFIG_DLPF_CFG));
245 // read the product ID rev c has 1/2 the sensitivity of rev d
246 _mpu6000_product_id = _register_read(MPUREG_PRODUCT_ID);
247 //Serial.printf("Product_ID= 0x%x\n", (unsigned) _mpu6000_product_id);
249 if ((_mpu6000_product_id == MPU6000ES_REV_C4) || (_mpu6000_product_id == MPU6000ES_REV_C5) ||
250 (_mpu6000_product_id == MPU6000_REV_C4) || (_mpu6000_product_id == MPU6000_REV_C5)) {
251 // Accel scale 8g (4096 LSB/g)
252 // Rev C has different scaling than rev D
253 register_write(MPUREG_ACCEL_CONFIG,1<<3);
255 // Accel scale 8g (4096 LSB/g)
256 register_write(MPUREG_ACCEL_CONFIG,2<<3);
258 hal.scheduler->delay(1);
261 for (st_tries = 0; st_tries < ST_TRIES; st_tries++) {
264 /* Configure accelerometer to +/-16G in self-test mode */
265 _ao_mpu6000_reg_write(MPU6000_ACCEL_CONFIG,
266 (1 << MPU600_ACCEL_CONFIG_XA_ST) |
267 (1 << MPU600_ACCEL_CONFIG_YA_ST) |
268 (1 << MPU600_ACCEL_CONFIG_ZA_ST) |
269 (MPU600_ACCEL_CONFIG_AFS_SEL_16G << MPU600_ACCEL_CONFIG_AFS_SEL));
271 /* Configure gyro to +/- 2000°/s in self-test mode */
272 _ao_mpu6000_reg_write(MPU6000_GYRO_CONFIG,
273 (1 << MPU600_GYRO_CONFIG_XG_ST) |
274 (1 << MPU600_GYRO_CONFIG_YG_ST) |
275 (1 << MPU600_GYRO_CONFIG_ZG_ST) |
276 (MPU600_GYRO_CONFIG_FS_SEL_2000 << MPU600_GYRO_CONFIG_FS_SEL));
278 ao_delay(AO_MS_TO_TICKS(200));
279 _ao_mpu6000_sample(&test_mode);
281 /* Configure accelerometer to +/-16G */
282 _ao_mpu6000_reg_write(MPU6000_ACCEL_CONFIG,
283 (0 << MPU600_ACCEL_CONFIG_XA_ST) |
284 (0 << MPU600_ACCEL_CONFIG_YA_ST) |
285 (0 << MPU600_ACCEL_CONFIG_ZA_ST) |
286 (MPU600_ACCEL_CONFIG_AFS_SEL_16G << MPU600_ACCEL_CONFIG_AFS_SEL));
288 /* Configure gyro to +/- 2000°/s */
289 _ao_mpu6000_reg_write(MPU6000_GYRO_CONFIG,
290 (0 << MPU600_GYRO_CONFIG_XG_ST) |
291 (0 << MPU600_GYRO_CONFIG_YG_ST) |
292 (0 << MPU600_GYRO_CONFIG_ZG_ST) |
293 (MPU600_GYRO_CONFIG_FS_SEL_2000 << MPU600_GYRO_CONFIG_FS_SEL));
295 ao_delay(AO_MS_TO_TICKS(200));
296 _ao_mpu6000_sample(&normal_mode);
298 errors += ao_mpu6000_accel_check(normal_mode.accel_x, test_mode.accel_x);
299 errors += ao_mpu6000_accel_check(normal_mode.accel_y, test_mode.accel_y);
300 errors += ao_mpu6000_accel_check(normal_mode.accel_z, test_mode.accel_z);
302 errors += ao_mpu6000_gyro_check(normal_mode.gyro_x, test_mode.gyro_x);
303 errors += ao_mpu6000_gyro_check(normal_mode.gyro_y, test_mode.gyro_y);
304 errors += ao_mpu6000_gyro_check(normal_mode.gyro_z, test_mode.gyro_z);
309 if (st_tries == ST_TRIES)
310 AO_SENSOR_ERROR(AO_DATA_MPU6000);
312 /* Filter to about 100Hz, which also sets the gyro rate to 1000Hz */
313 _ao_mpu6000_reg_write(MPU6000_CONFIG,
314 (MPU6000_CONFIG_EXT_SYNC_SET_DISABLED << MPU6000_CONFIG_EXT_SYNC_SET) |
315 (MPU6000_CONFIG_DLPF_CFG_94_98 << MPU6000_CONFIG_DLPF_CFG));
317 /* Set sample rate divider to sample at 200Hz (v = gyro/rate - 1) */
318 _ao_mpu6000_reg_write(MPU6000_SMPRT_DIV,
321 ao_delay(AO_MS_TO_TICKS(100));
322 ao_mpu6000_configured = 1;
325 struct ao_mpu6000_sample ao_mpu6000_current;
330 struct ao_mpu6000_sample sample;
331 /* ao_mpu6000_init already grabbed the SPI bus and mutex */
334 ao_mpu6000_spi_put();
339 ao_mpu6000_spi_get();
341 _ao_mpu6000_sample(&sample);
343 ao_mpu6000_spi_put();
345 ao_arch_block_interrupts();
346 ao_mpu6000_current = sample;
347 AO_DATA_PRESENT(AO_DATA_MPU6000);
349 ao_arch_release_interrupts();
353 static struct ao_task ao_mpu6000_task;
356 ao_mpu6000_show(void)
358 printf ("Accel: %7d %7d %7d Gyro: %7d %7d %7d\n",
359 ao_mpu6000_current.accel_x,
360 ao_mpu6000_current.accel_y,
361 ao_mpu6000_current.accel_z,
362 ao_mpu6000_current.gyro_x,
363 ao_mpu6000_current.gyro_y,
364 ao_mpu6000_current.gyro_z);
367 static const struct ao_cmds ao_mpu6000_cmds[] = {
368 { ao_mpu6000_show, "I\0Show MPU6000 status" },
373 ao_mpu6000_init(void)
375 ao_mpu6000_configured = 0;
377 ao_add_task(&ao_mpu6000_task, ao_mpu6000, "mpu6000");
380 ao_spi_init_cs(AO_MPU6000_SPI_CS_PORT, (1 << AO_MPU6000_SPI_CS_PIN));
382 /* Pretend to be the mpu6000 task. Grab the SPI bus right away and
383 * hold it for the task so that nothing else uses the SPI bus before
384 * we get the I2C mode disabled in the chip
387 ao_cur_task = &ao_mpu6000_task;
388 ao_mpu6000_spi_get();
392 ao_cmd_register(&ao_mpu6000_cmds[0]);