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_set_cs(AO_MPU6000_SPI_CS_PORT, \
39 (1 << AO_MPU6000_SPI_CS_PIN))
41 #define ao_mpu6000_spi_end() ao_spi_clr_cs(AO_MPU6000_SPI_CS_PORT, \
42 (1 << AO_MPU6000_SPI_CS_PIN))
48 _ao_mpu6000_reg_write(uint8_t addr, uint8_t value)
50 uint8_t d[2] = { addr, value };
52 ao_mpu6000_spi_start();
53 ao_spi_send(d, 2, AO_MPU6000_SPI_BUS);
56 ao_i2c_get(AO_MPU6000_I2C_INDEX);
57 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE);
58 ao_i2c_send(d, 2, AO_MPU6000_I2C_INDEX, TRUE);
59 ao_i2c_put(AO_MPU6000_I2C_INDEX);
64 _ao_mpu6000_read(uint8_t addr, void *data, uint8_t len)
68 ao_mpu6000_spi_start();
69 ao_spi_send(&addr, 1, AO_MPU6000_SPI_BUS);
70 ao_spi_recv(data, len, AO_MPU6000_SPI_BUS);
73 ao_i2c_get(AO_MPU6000_I2C_INDEX);
74 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE);
75 ao_i2c_send(&addr, 1, AO_MPU6000_I2C_INDEX, FALSE);
76 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_READ);
77 ao_i2c_recv(data, len, AO_MPU6000_I2C_INDEX, TRUE);
78 ao_i2c_put(AO_MPU6000_I2C_INDEX);
83 _ao_mpu6000_reg_read(uint8_t addr)
88 ao_mpu6000_spi_start();
89 ao_spi_send(&addr, 1, AO_MPU6000_SPI_BUS);
90 ao_spi_recv(&value, 1, AO_MPU6000_SPI_BUS);
93 ao_i2c_get(AO_MPU6000_I2C_INDEX);
94 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE);
95 ao_i2c_send(&addr, 1, AO_MPU6000_I2C_INDEX, FALSE);
96 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_READ);
97 ao_i2c_recv(&value, 1, AO_MPU6000_I2C_INDEX, TRUE);
98 ao_i2c_put(AO_MPU6000_I2C_INDEX);
104 _ao_mpu6000_sample(struct ao_mpu6000_sample *sample)
106 uint16_t *d = (uint16_t *) sample;
107 int i = sizeof (*sample) / 2;
109 _ao_mpu6000_read(MPU6000_ACCEL_XOUT_H, sample, sizeof (*sample));
110 #if __BYTE_ORDER == __LITTLE_ENDIAN
114 *d++ = (t >> 8) | (t << 8);
119 #define G 981 /* in cm/s² */
122 static int16_t /* cm/s² */
123 ao_mpu6000_accel(int16_t v)
125 return (int16_t) ((v * (int32_t) (16.0 * 980.665 + 0.5)) / 32767);
128 static int16_t /* deg*10/s */
129 ao_mpu6000_gyro(int16_t v)
131 return (int16_t) ((v * (int32_t) 20000) / 32767);
136 ao_mpu6000_accel_check(int16_t normal, int16_t test, char *which)
138 int16_t diff = test - normal;
140 if (diff < MPU6000_ST_ACCEL(16) / 2) {
143 if (diff > MPU6000_ST_ACCEL(16) * 2) {
150 ao_mpu6000_gyro_check(int16_t normal, int16_t test, char *which)
152 int16_t diff = test - normal;
156 if (diff < MPU6000_ST_GYRO(2000) / 2) {
159 if (diff > MPU6000_ST_GYRO(2000) * 2) {
166 _ao_mpu6000_wait_alive(void)
170 /* Wait for the chip to wake up */
171 for (i = 0; i < 30; i++) {
172 ao_delay(AO_MS_TO_TICKS(100));
173 if (_ao_mpu6000_reg_read(MPU6000_WHO_AM_I) == 0x68)
177 ao_panic(AO_PANIC_SELF_TEST_MPU6000);
181 _ao_mpu6000_setup(void)
183 struct ao_mpu6000_sample normal_mode, test_mode;
186 if (ao_mpu6000_configured)
189 _ao_mpu6000_wait_alive();
191 /* Reset the whole chip */
193 _ao_mpu6000_reg_write(MPU6000_PWR_MGMT_1,
194 (1 << MPU6000_PWR_MGMT_1_DEVICE_RESET));
196 /* Wait for it to reset. If we talk too quickly, it appears to get confused */
198 _ao_mpu6000_wait_alive();
200 /* Reset signal conditioning, disabling I2C on SPI systems */
201 _ao_mpu6000_reg_write(MPU6000_USER_CTRL,
202 (0 << MPU6000_USER_CTRL_FIFO_EN) |
203 (0 << MPU6000_USER_CTRL_I2C_MST_EN) |
204 (AO_MPU6000_SPI << MPU6000_USER_CTRL_I2C_IF_DIS) |
205 (0 << MPU6000_USER_CTRL_FIFO_RESET) |
206 (0 << MPU6000_USER_CTRL_I2C_MST_RESET) |
207 (1 << MPU6000_USER_CTRL_SIG_COND_RESET));
209 while (_ao_mpu6000_reg_read(MPU6000_USER_CTRL) & (1 << MPU6000_USER_CTRL_SIG_COND_RESET))
210 ao_delay(AO_MS_TO_TICKS(10));
212 /* Reset signal paths */
213 _ao_mpu6000_reg_write(MPU6000_SIGNAL_PATH_RESET,
214 (1 << MPU6000_SIGNAL_PATH_RESET_GYRO_RESET) |
215 (1 << MPU6000_SIGNAL_PATH_RESET_ACCEL_RESET) |
216 (1 << MPU6000_SIGNAL_PATH_RESET_TEMP_RESET));
218 _ao_mpu6000_reg_write(MPU6000_SIGNAL_PATH_RESET,
219 (0 << MPU6000_SIGNAL_PATH_RESET_GYRO_RESET) |
220 (0 << MPU6000_SIGNAL_PATH_RESET_ACCEL_RESET) |
221 (0 << MPU6000_SIGNAL_PATH_RESET_TEMP_RESET));
223 /* Select clocks, disable sleep */
224 _ao_mpu6000_reg_write(MPU6000_PWR_MGMT_1,
225 (0 << MPU6000_PWR_MGMT_1_DEVICE_RESET) |
226 (0 << MPU6000_PWR_MGMT_1_SLEEP) |
227 (0 << MPU6000_PWR_MGMT_1_CYCLE) |
228 (0 << MPU6000_PWR_MGMT_1_TEMP_DIS) |
229 (MPU6000_PWR_MGMT_1_CLKSEL_PLL_X_AXIS << MPU6000_PWR_MGMT_1_CLKSEL));
231 /* Set sample rate divider to sample at full speed */
232 _ao_mpu6000_reg_write(MPU6000_SMPRT_DIV, 0);
234 /* Disable filtering */
235 _ao_mpu6000_reg_write(MPU6000_CONFIG,
236 (MPU6000_CONFIG_EXT_SYNC_SET_DISABLED << MPU6000_CONFIG_EXT_SYNC_SET) |
237 (MPU6000_CONFIG_DLPF_CFG_260_256 << MPU6000_CONFIG_DLPF_CFG));
239 /* Configure accelerometer to +/-16G in self-test mode */
240 _ao_mpu6000_reg_write(MPU6000_ACCEL_CONFIG,
241 (1 << MPU600_ACCEL_CONFIG_XA_ST) |
242 (1 << MPU600_ACCEL_CONFIG_YA_ST) |
243 (1 << MPU600_ACCEL_CONFIG_ZA_ST) |
244 (MPU600_ACCEL_CONFIG_AFS_SEL_16G << MPU600_ACCEL_CONFIG_AFS_SEL));
246 /* Configure gyro to +/- 2000°/s in self-test mode */
247 _ao_mpu6000_reg_write(MPU6000_GYRO_CONFIG,
248 (1 << MPU600_GYRO_CONFIG_XG_ST) |
249 (1 << MPU600_GYRO_CONFIG_YG_ST) |
250 (1 << MPU600_GYRO_CONFIG_ZG_ST) |
251 (MPU600_GYRO_CONFIG_FS_SEL_2000 << MPU600_GYRO_CONFIG_FS_SEL));
253 ao_delay(AO_MS_TO_TICKS(200));
254 _ao_mpu6000_sample(&test_mode);
257 // read the product ID rev c has 1/2 the sensitivity of rev d
258 _mpu6000_product_id = _register_read(MPUREG_PRODUCT_ID);
259 //Serial.printf("Product_ID= 0x%x\n", (unsigned) _mpu6000_product_id);
261 if ((_mpu6000_product_id == MPU6000ES_REV_C4) || (_mpu6000_product_id == MPU6000ES_REV_C5) ||
262 (_mpu6000_product_id == MPU6000_REV_C4) || (_mpu6000_product_id == MPU6000_REV_C5)) {
263 // Accel scale 8g (4096 LSB/g)
264 // Rev C has different scaling than rev D
265 register_write(MPUREG_ACCEL_CONFIG,1<<3);
267 // Accel scale 8g (4096 LSB/g)
268 register_write(MPUREG_ACCEL_CONFIG,2<<3);
270 hal.scheduler->delay(1);
274 /* Configure accelerometer to +/-16G */
275 _ao_mpu6000_reg_write(MPU6000_ACCEL_CONFIG,
276 (0 << MPU600_ACCEL_CONFIG_XA_ST) |
277 (0 << MPU600_ACCEL_CONFIG_YA_ST) |
278 (0 << MPU600_ACCEL_CONFIG_ZA_ST) |
279 (MPU600_ACCEL_CONFIG_AFS_SEL_16G << MPU600_ACCEL_CONFIG_AFS_SEL));
281 /* Configure gyro to +/- 2000°/s */
282 _ao_mpu6000_reg_write(MPU6000_GYRO_CONFIG,
283 (0 << MPU600_GYRO_CONFIG_XG_ST) |
284 (0 << MPU600_GYRO_CONFIG_YG_ST) |
285 (0 << MPU600_GYRO_CONFIG_ZG_ST) |
286 (MPU600_GYRO_CONFIG_FS_SEL_2000 << MPU600_GYRO_CONFIG_FS_SEL));
288 ao_delay(AO_MS_TO_TICKS(10));
289 _ao_mpu6000_sample(&normal_mode);
291 errors += ao_mpu6000_accel_check(normal_mode.accel_x, test_mode.accel_x, "x");
292 errors += ao_mpu6000_accel_check(normal_mode.accel_y, test_mode.accel_y, "y");
293 errors += ao_mpu6000_accel_check(normal_mode.accel_z, test_mode.accel_z, "z");
295 errors += ao_mpu6000_gyro_check(normal_mode.gyro_x, test_mode.gyro_x, "x");
296 errors += ao_mpu6000_gyro_check(normal_mode.gyro_y, test_mode.gyro_y, "y");
297 errors += ao_mpu6000_gyro_check(normal_mode.gyro_z, test_mode.gyro_z, "z");
300 ao_panic(AO_PANIC_SELF_TEST_MPU6000);
302 /* Filter to about 100Hz, which also sets the gyro rate to 1000Hz */
303 _ao_mpu6000_reg_write(MPU6000_CONFIG,
304 (MPU6000_CONFIG_EXT_SYNC_SET_DISABLED << MPU6000_CONFIG_EXT_SYNC_SET) |
305 (MPU6000_CONFIG_DLPF_CFG_94_98 << MPU6000_CONFIG_DLPF_CFG));
307 /* Set sample rate divider to sample at 200Hz (v = gyro/rate - 1) */
308 _ao_mpu6000_reg_write(MPU6000_SMPRT_DIV,
311 ao_delay(AO_MS_TO_TICKS(100));
312 ao_mpu6000_configured = 1;
315 struct ao_mpu6000_sample ao_mpu6000_current;
320 /* ao_mpu6000_init already grabbed the SPI bus and mutex */
323 ao_mpu6000_spi_put();
328 ao_mpu6000_spi_get();
330 _ao_mpu6000_sample(&ao_mpu6000_current);
332 ao_mpu6000_spi_put();
335 AO_DATA_PRESENT(AO_DATA_MPU6000);
341 static struct ao_task ao_mpu6000_task;
344 ao_mpu6000_show(void)
346 struct ao_data sample;
348 ao_data_get(&sample);
349 printf ("Accel: %7d %7d %7d Gyro: %7d %7d %7d\n",
350 sample.mpu6000.accel_x,
351 sample.mpu6000.accel_y,
352 sample.mpu6000.accel_z,
353 sample.mpu6000.gyro_x,
354 sample.mpu6000.gyro_y,
355 sample.mpu6000.gyro_z);
358 static const struct ao_cmds ao_mpu6000_cmds[] = {
359 { ao_mpu6000_show, "I\0Show MPU6000 status" },
364 ao_mpu6000_init(void)
366 ao_mpu6000_configured = 0;
368 ao_add_task(&ao_mpu6000_task, ao_mpu6000, "mpu6000");
371 ao_spi_init_cs(AO_MPU6000_SPI_CS_PORT, (1 << AO_MPU6000_SPI_CS_PIN));
373 /* Pretend to be the mpu6000 task. Grab the SPI bus right away and
374 * hold it for the task so that nothing else uses the SPI bus before
375 * we get the I2C mode disabled in the chip
378 ao_cur_task = &ao_mpu6000_task;
379 ao_spi_get(AO_MPU6000_SPI_BUS, AO_SPI_SPEED_1MHz);
383 ao_cmd_register(&ao_mpu6000_cmds[0]);