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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 #include <ao_mpu6000.h>
24 static uint8_t ao_mpu6000_wake;
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_get() ao_spi_get(AO_MPU6000_SPI_BUS, AO_SPI_SPEED_1MHz)
36 #define ao_mpu6000_spi_put() ao_spi_put(AO_MPU6000_SPI_BUS)
38 #define ao_mpu6000_spi_start() ao_spi_get_bit(AO_MPU6000_SPI_CS_PORT, \
39 AO_MPU6000_SPI_CS_PIN, \
44 #define ao_mpu6000_spi_end() ao_spi_put_bit(AO_MPU6000_SPI_CS_PORT, \
45 AO_MPU6000_SPI_CS_PIN, \
52 _ao_mpu6000_reg_write(uint8_t addr, uint8_t value)
54 uint8_t d[2] = { addr, value };
56 ao_mpu6000_spi_start();
57 ao_spi_send(d, 2, AO_MPU6000_SPI_BUS);
60 ao_i2c_get(AO_MPU6000_I2C_INDEX);
61 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE);
62 ao_i2c_send(d, 2, AO_MPU6000_I2C_INDEX, TRUE);
63 ao_i2c_put(AO_MPU6000_I2C_INDEX);
68 _ao_mpu6000_read(uint8_t addr, void *data, uint8_t len)
72 ao_mpu6000_spi_start();
73 ao_spi_send(&addr, 1, AO_MPU6000_SPI_BUS);
74 ao_spi_recv(data, len, AO_MPU6000_SPI_BUS);
77 ao_i2c_get(AO_MPU6000_I2C_INDEX);
78 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE);
79 ao_i2c_send(&addr, 1, AO_MPU6000_I2C_INDEX, FALSE);
80 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_READ);
81 ao_i2c_recv(data, len, AO_MPU6000_I2C_INDEX, TRUE);
82 ao_i2c_put(AO_MPU6000_I2C_INDEX);
87 _ao_mpu6000_reg_read(uint8_t addr)
92 ao_mpu6000_spi_start();
93 ao_spi_send(&addr, 1, AO_MPU6000_SPI_BUS);
94 ao_spi_recv(&value, 1, AO_MPU6000_SPI_BUS);
97 ao_i2c_get(AO_MPU6000_I2C_INDEX);
98 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE);
99 ao_i2c_send(&addr, 1, AO_MPU6000_I2C_INDEX, FALSE);
100 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_READ);
101 ao_i2c_recv(&value, 1, AO_MPU6000_I2C_INDEX, TRUE);
102 ao_i2c_put(AO_MPU6000_I2C_INDEX);
108 _ao_mpu6000_sample(struct ao_mpu6000_sample *sample)
110 uint16_t *d = (uint16_t *) sample;
111 int i = sizeof (*sample) / 2;
113 _ao_mpu6000_read(MPU6000_ACCEL_XOUT_H, sample, sizeof (*sample));
114 #if __BYTE_ORDER == __LITTLE_ENDIAN
118 *d++ = (t >> 8) | (t << 8);
123 #define G 981 /* in cm/s² */
125 static int16_t /* cm/s² */
126 ao_mpu6000_accel(int16_t v)
128 return (int16_t) ((v * (int32_t) (16.0 * 980.665 + 0.5)) / 32767);
131 static int16_t /* deg*10/s */
132 ao_mpu6000_gyro(int16_t v)
134 return (int16_t) ((v * (int32_t) 20000) / 32767);
138 ao_mpu6000_accel_check(int16_t normal, int16_t test, char *which)
140 int16_t diff = test - normal;
142 if (diff < MPU6000_ST_ACCEL(16) / 2) {
145 if (diff > MPU6000_ST_ACCEL(16) * 2) {
152 ao_mpu6000_gyro_check(int16_t normal, int16_t test, char *which)
154 int16_t diff = test - normal;
158 if (diff < MPU6000_ST_GYRO(2000) / 2) {
161 if (diff > MPU6000_ST_GYRO(2000) * 2) {
168 _ao_mpu6000_setup(void)
170 struct ao_mpu6000_sample normal_mode, test_mode;
174 if (ao_mpu6000_configured)
177 /* Wait for the chip to wake up */
178 for (i = 0; i < 30; i++) {
179 ao_delay(AO_MS_TO_TICKS(100));
180 if (_ao_mpu6000_reg_read(MPU6000_WHO_AM_I) == 0x68)
184 ao_panic(AO_PANIC_SELF_TEST_MPU6000);
186 /* Reset the whole chip */
188 _ao_mpu6000_reg_write(MPU6000_PWR_MGMT_1,
189 (1 << MPU6000_PWR_MGMT_1_DEVICE_RESET));
191 /* Wait for it to reset. If we talk too quickly, it appears to get confused */
193 ao_delay(AO_MS_TO_TICKS(100));
195 /* Reset signal conditioning, disabling I2C on SPI systems */
196 _ao_mpu6000_reg_write(MPU6000_USER_CTRL,
197 (0 << MPU6000_USER_CTRL_FIFO_EN) |
198 (0 << MPU6000_USER_CTRL_I2C_MST_EN) |
199 (AO_MPU6000_SPI << MPU6000_USER_CTRL_I2C_IF_DIS) |
200 (0 << MPU6000_USER_CTRL_FIFO_RESET) |
201 (0 << MPU6000_USER_CTRL_I2C_MST_RESET) |
202 (1 << MPU6000_USER_CTRL_SIG_COND_RESET));
204 while (_ao_mpu6000_reg_read(MPU6000_USER_CTRL) & (1 << MPU6000_USER_CTRL_SIG_COND_RESET))
207 /* Reset signal paths */
208 _ao_mpu6000_reg_write(MPU6000_SIGNAL_PATH_RESET,
209 (1 << MPU6000_SIGNAL_PATH_RESET_GYRO_RESET) |
210 (1 << MPU6000_SIGNAL_PATH_RESET_ACCEL_RESET) |
211 (1 << MPU6000_SIGNAL_PATH_RESET_TEMP_RESET));
213 _ao_mpu6000_reg_write(MPU6000_SIGNAL_PATH_RESET,
214 (0 << MPU6000_SIGNAL_PATH_RESET_GYRO_RESET) |
215 (0 << MPU6000_SIGNAL_PATH_RESET_ACCEL_RESET) |
216 (0 << MPU6000_SIGNAL_PATH_RESET_TEMP_RESET));
218 /* Select clocks, disable sleep */
219 _ao_mpu6000_reg_write(MPU6000_PWR_MGMT_1,
220 (0 << MPU6000_PWR_MGMT_1_DEVICE_RESET) |
221 (0 << MPU6000_PWR_MGMT_1_SLEEP) |
222 (0 << MPU6000_PWR_MGMT_1_CYCLE) |
223 (0 << MPU6000_PWR_MGMT_1_TEMP_DIS) |
224 (MPU6000_PWR_MGMT_1_CLKSEL_PLL_X_AXIS << MPU6000_PWR_MGMT_1_CLKSEL));
226 /* Set sample rate divider to sample at full speed
227 _ao_mpu6000_reg_write(MPU6000_SMPRT_DIV, 0);
229 /* Disable filtering */
230 _ao_mpu6000_reg_write(MPU6000_CONFIG,
231 (MPU6000_CONFIG_EXT_SYNC_SET_DISABLED << MPU6000_CONFIG_EXT_SYNC_SET) |
232 (MPU6000_CONFIG_DLPF_CFG_260_256 << MPU6000_CONFIG_DLPF_CFG));
234 /* Configure accelerometer to +/-16G in self-test mode */
235 _ao_mpu6000_reg_write(MPU6000_ACCEL_CONFIG,
236 (1 << MPU600_ACCEL_CONFIG_XA_ST) |
237 (1 << MPU600_ACCEL_CONFIG_YA_ST) |
238 (1 << MPU600_ACCEL_CONFIG_ZA_ST) |
239 (MPU600_ACCEL_CONFIG_AFS_SEL_16G << MPU600_ACCEL_CONFIG_AFS_SEL));
241 /* Configure gyro to +/- 2000°/s in self-test mode */
242 _ao_mpu6000_reg_write(MPU6000_GYRO_CONFIG,
243 (1 << MPU600_GYRO_CONFIG_XG_ST) |
244 (1 << MPU600_GYRO_CONFIG_YG_ST) |
245 (1 << MPU600_GYRO_CONFIG_ZG_ST) |
246 (MPU600_GYRO_CONFIG_FS_SEL_2000 << MPU600_GYRO_CONFIG_FS_SEL));
248 ao_delay(AO_MS_TO_TICKS(200));
249 _ao_mpu6000_sample(&test_mode);
252 // read the product ID rev c has 1/2 the sensitivity of rev d
253 _mpu6000_product_id = _register_read(MPUREG_PRODUCT_ID);
254 //Serial.printf("Product_ID= 0x%x\n", (unsigned) _mpu6000_product_id);
256 if ((_mpu6000_product_id == MPU6000ES_REV_C4) || (_mpu6000_product_id == MPU6000ES_REV_C5) ||
257 (_mpu6000_product_id == MPU6000_REV_C4) || (_mpu6000_product_id == MPU6000_REV_C5)) {
258 // Accel scale 8g (4096 LSB/g)
259 // Rev C has different scaling than rev D
260 register_write(MPUREG_ACCEL_CONFIG,1<<3);
262 // Accel scale 8g (4096 LSB/g)
263 register_write(MPUREG_ACCEL_CONFIG,2<<3);
265 hal.scheduler->delay(1);
269 /* Configure accelerometer to +/-16G */
270 _ao_mpu6000_reg_write(MPU6000_ACCEL_CONFIG,
271 (0 << MPU600_ACCEL_CONFIG_XA_ST) |
272 (0 << MPU600_ACCEL_CONFIG_YA_ST) |
273 (0 << MPU600_ACCEL_CONFIG_ZA_ST) |
274 (MPU600_ACCEL_CONFIG_AFS_SEL_16G << MPU600_ACCEL_CONFIG_AFS_SEL));
276 /* Configure gyro to +/- 2000°/s */
277 _ao_mpu6000_reg_write(MPU6000_GYRO_CONFIG,
278 (0 << MPU600_GYRO_CONFIG_XG_ST) |
279 (0 << MPU600_GYRO_CONFIG_YG_ST) |
280 (0 << MPU600_GYRO_CONFIG_ZG_ST) |
281 (MPU600_GYRO_CONFIG_FS_SEL_2000 << MPU600_GYRO_CONFIG_FS_SEL));
283 ao_delay(AO_MS_TO_TICKS(10));
284 _ao_mpu6000_sample(&normal_mode);
286 errors += ao_mpu6000_accel_check(normal_mode.accel_x, test_mode.accel_x, "x");
287 errors += ao_mpu6000_accel_check(normal_mode.accel_y, test_mode.accel_y, "y");
288 errors += ao_mpu6000_accel_check(normal_mode.accel_z, test_mode.accel_z, "z");
290 errors += ao_mpu6000_gyro_check(normal_mode.gyro_x, test_mode.gyro_x, "x");
291 errors += ao_mpu6000_gyro_check(normal_mode.gyro_y, test_mode.gyro_y, "y");
292 errors += ao_mpu6000_gyro_check(normal_mode.gyro_z, test_mode.gyro_z, "z");
295 ao_panic(AO_PANIC_SELF_TEST_MPU6000);
297 /* Filter to about 100Hz, which also sets the gyro rate to 1000Hz */
298 _ao_mpu6000_reg_write(MPU6000_CONFIG,
299 (MPU6000_CONFIG_EXT_SYNC_SET_DISABLED << MPU6000_CONFIG_EXT_SYNC_SET) |
300 (MPU6000_CONFIG_DLPF_CFG_94_98 << MPU6000_CONFIG_DLPF_CFG));
302 /* Set sample rate divider to sample at 200Hz (v = gyro/rate - 1) */
303 _ao_mpu6000_reg_write(MPU6000_SMPRT_DIV,
306 ao_delay(AO_MS_TO_TICKS(100));
307 ao_mpu6000_configured = 1;
310 struct ao_mpu6000_sample ao_mpu6000_current;
315 /* ao_mpu6000_init already grabbed the SPI bus and mutex */
318 ao_mpu6000_spi_put();
323 ao_mpu6000_spi_get();
325 _ao_mpu6000_sample(&ao_mpu6000_current);
327 ao_mpu6000_spi_put();
330 AO_DATA_PRESENT(AO_DATA_MPU6000);
336 static struct ao_task ao_mpu6000_task;
339 ao_mpu6000_show(void)
341 struct ao_data sample;
343 ao_data_get(&sample);
344 printf ("Accel: %7d %7d %7d Gyro: %7d %7d %7d\n",
345 sample.mpu6000.accel_x,
346 sample.mpu6000.accel_y,
347 sample.mpu6000.accel_z,
348 sample.mpu6000.gyro_x,
349 sample.mpu6000.gyro_y,
350 sample.mpu6000.gyro_z);
353 static const struct ao_cmds ao_mpu6000_cmds[] = {
354 { ao_mpu6000_show, "I\0Show MPU6000 status" },
359 ao_mpu6000_init(void)
361 ao_mpu6000_configured = 0;
363 ao_add_task(&ao_mpu6000_task, ao_mpu6000, "mpu6000");
366 ao_spi_init_cs(AO_MPU6000_SPI_CS_PORT, (1 << AO_MPU6000_SPI_CS_PIN));
368 /* Pretend to be the mpu6000 task. Grab the SPI bus right away and
369 * hold it for the task so that nothing else uses the SPI bus before
370 * we get the I2C mode disabled in the chip
373 ao_cur_task = &ao_mpu6000_task;
374 ao_spi_get(AO_MPU6000_SPI_BUS, AO_SPI_SPEED_1MHz);
378 ao_cmd_register(&ao_mpu6000_cmds[0]);