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 #define ao_mpu6000_spi_get() ao_spi_get_bit(AO_MPU6000_SPI_CS_PORT, \
28 AO_MPU6000_SPI_CS_PIN, \
33 #define ao_mpu6000_spi_put() ao_spi_put_bit(AO_MPU6000_SPI_CS_PORT, \
34 AO_MPU6000_SPI_CS_PIN, \
40 ao_mpu6000_reg_write(uint8_t addr, uint8_t value)
42 uint8_t d[2] = { addr, value };
43 #ifdef AO_MPU6000_I2C_INDEX
44 ao_i2c_get(AO_MPU6000_I2C_INDEX);
45 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE);
46 ao_i2c_send(d, 2, AO_MPU6000_I2C_INDEX, TRUE);
47 ao_i2c_put(AO_MPU6000_I2C_INDEX);
50 ao_spi_send(d, 2, AO_MPU6000_SPI_BUS);
56 ao_mpu6000_read(uint8_t addr, void *data, uint8_t len)
58 #ifdef AO_MPU6000_I2C_INDEX
59 ao_i2c_get(AO_MPU6000_I2C_INDEX);
60 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE);
61 ao_i2c_send(&addr, 1, AO_MPU6000_I2C_INDEX, FALSE);
62 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_READ);
63 ao_i2c_recv(data, len, AO_MPU6000_I2C_INDEX, TRUE);
64 ao_i2c_put(AO_MPU6000_I2C_INDEX);
68 ao_spi_send(&addr, 1, AO_MPU6000_SPI_BUS);
69 ao_spi_recv(data, len, AO_MPU6000_SPI_BUS);
75 ao_mpu6000_reg_read(uint8_t addr)
78 #ifdef AO_MPU6000_I2C_INDEX
79 ao_i2c_get(AO_MPU6000_I2C_INDEX);
80 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE);
81 ao_i2c_send(&addr, 1, AO_MPU6000_I2C_INDEX, FALSE);
82 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_READ);
83 ao_i2c_recv(&value, 1, AO_MPU6000_I2C_INDEX, TRUE);
84 ao_i2c_put(AO_MPU6000_I2C_INDEX);
88 ao_spi_send(&addr, 1, AO_MPU6000_SPI_BUS);
89 ao_spi_recv(&value, 1, AO_MPU6000_SPI_BUS);
96 ao_mpu6000_sample(struct ao_mpu6000_sample *sample)
98 uint16_t *d = (uint16_t *) sample;
99 int i = sizeof (*sample) / 2;
101 ao_mpu6000_read(MPU6000_ACCEL_XOUT_H, sample, sizeof (*sample));
102 #if __BYTE_ORDER == __LITTLE_ENDIAN
106 *d++ = (t >> 8) | (t << 8);
111 #define G 981 /* in cm/s² */
113 static int16_t /* cm/s² */
114 ao_mpu6000_accel(int16_t v)
116 return (int16_t) ((v * (int32_t) (16.0 * 980.665 + 0.5)) / 32767);
119 static int16_t /* deg*10/s */
120 ao_mpu6000_gyro(int16_t v)
122 return (int16_t) ((v * (int32_t) 20000) / 32767);
126 ao_mpu6000_accel_check(int16_t normal, int16_t test, char *which)
128 int16_t diff = test - normal;
130 if (diff < MPU6000_ST_ACCEL(16) / 2) {
133 if (diff > MPU6000_ST_ACCEL(16) * 2) {
140 ao_mpu6000_gyro_check(int16_t normal, int16_t test, char *which)
142 int16_t diff = test - normal;
146 if (diff < MPU6000_ST_GYRO(2000) / 2) {
149 if (diff > MPU6000_ST_GYRO(2000) * 2) {
156 ao_mpu6000_setup(void)
158 struct ao_mpu6000_sample normal_mode, test_mode;
161 if (ao_mpu6000_configured)
164 /* Reset the whole chip */
166 ao_mpu6000_reg_write(MPU6000_PWR_MGMT_1,
167 (1 << MPU6000_PWR_MGMT_1_DEVICE_RESET));
169 /* Wait for it to reset. If we talk too quickly, it appears to get confused */
170 ao_delay(AO_MS_TO_TICKS(100));
172 /* Reset signal conditioning */
173 ao_mpu6000_reg_write(MPU6000_USER_CONTROL,
174 (0 << MPU6000_USER_CONTROL_FIFO_EN) |
175 (0 << MPU6000_USER_CONTROL_I2C_MST_EN) |
176 (0 << MPU6000_USER_CONTROL_I2C_IF_DIS) |
177 (0 << MPU6000_USER_CONTROL_FIFO_RESET) |
178 (0 << MPU6000_USER_CONTROL_I2C_MST_RESET) |
179 (1 << MPU6000_USER_CONTROL_SIG_COND_RESET));
181 while (ao_mpu6000_reg_read(MPU6000_USER_CONTROL) & (1 << MPU6000_USER_CONTROL_SIG_COND_RESET))
184 /* Reset signal paths */
185 ao_mpu6000_reg_write(MPU6000_SIGNAL_PATH_RESET,
186 (1 << MPU6000_SIGNAL_PATH_RESET_GYRO_RESET) |
187 (1 << MPU6000_SIGNAL_PATH_RESET_ACCEL_RESET) |
188 (1 << MPU6000_SIGNAL_PATH_RESET_TEMP_RESET));
190 ao_mpu6000_reg_write(MPU6000_SIGNAL_PATH_RESET,
191 (0 << MPU6000_SIGNAL_PATH_RESET_GYRO_RESET) |
192 (0 << MPU6000_SIGNAL_PATH_RESET_ACCEL_RESET) |
193 (0 << MPU6000_SIGNAL_PATH_RESET_TEMP_RESET));
195 /* Select clocks, disable sleep */
196 ao_mpu6000_reg_write(MPU6000_PWR_MGMT_1,
197 (0 << MPU6000_PWR_MGMT_1_DEVICE_RESET) |
198 (0 << MPU6000_PWR_MGMT_1_SLEEP) |
199 (0 << MPU6000_PWR_MGMT_1_CYCLE) |
200 (0 << MPU6000_PWR_MGMT_1_TEMP_DIS) |
201 (MPU6000_PWR_MGMT_1_CLKSEL_PLL_X_AXIS << MPU6000_PWR_MGMT_1_CLKSEL));
203 /* Set sample rate divider to sample at full speed
204 ao_mpu6000_reg_write(MPU6000_SMPRT_DIV, 0);
206 /* Disable filtering */
207 ao_mpu6000_reg_write(MPU6000_CONFIG,
208 (MPU6000_CONFIG_EXT_SYNC_SET_DISABLED << MPU6000_CONFIG_EXT_SYNC_SET) |
209 (MPU6000_CONFIG_DLPF_CFG_260_256 << MPU6000_CONFIG_DLPF_CFG));
211 /* Configure accelerometer to +/-16G in self-test mode */
212 ao_mpu6000_reg_write(MPU6000_ACCEL_CONFIG,
213 (1 << MPU600_ACCEL_CONFIG_XA_ST) |
214 (1 << MPU600_ACCEL_CONFIG_YA_ST) |
215 (1 << MPU600_ACCEL_CONFIG_ZA_ST) |
216 (MPU600_ACCEL_CONFIG_AFS_SEL_16G << MPU600_ACCEL_CONFIG_AFS_SEL));
218 /* Configure gyro to +/- 2000°/s in self-test mode */
219 ao_mpu6000_reg_write(MPU6000_GYRO_CONFIG,
220 (1 << MPU600_GYRO_CONFIG_XG_ST) |
221 (1 << MPU600_GYRO_CONFIG_YG_ST) |
222 (1 << MPU600_GYRO_CONFIG_ZG_ST) |
223 (MPU600_GYRO_CONFIG_FS_SEL_2000 << MPU600_GYRO_CONFIG_FS_SEL));
225 ao_delay(AO_MS_TO_TICKS(200));
226 ao_mpu6000_sample(&test_mode);
229 // read the product ID rev c has 1/2 the sensitivity of rev d
230 _mpu6000_product_id = _register_read(MPUREG_PRODUCT_ID);
231 //Serial.printf("Product_ID= 0x%x\n", (unsigned) _mpu6000_product_id);
233 if ((_mpu6000_product_id == MPU6000ES_REV_C4) || (_mpu6000_product_id == MPU6000ES_REV_C5) ||
234 (_mpu6000_product_id == MPU6000_REV_C4) || (_mpu6000_product_id == MPU6000_REV_C5)) {
235 // Accel scale 8g (4096 LSB/g)
236 // Rev C has different scaling than rev D
237 register_write(MPUREG_ACCEL_CONFIG,1<<3);
239 // Accel scale 8g (4096 LSB/g)
240 register_write(MPUREG_ACCEL_CONFIG,2<<3);
242 hal.scheduler->delay(1);
246 /* Configure accelerometer to +/-16G */
247 ao_mpu6000_reg_write(MPU6000_ACCEL_CONFIG,
248 (0 << MPU600_ACCEL_CONFIG_XA_ST) |
249 (0 << MPU600_ACCEL_CONFIG_YA_ST) |
250 (0 << MPU600_ACCEL_CONFIG_ZA_ST) |
251 (MPU600_ACCEL_CONFIG_AFS_SEL_16G << MPU600_ACCEL_CONFIG_AFS_SEL));
253 /* Configure gyro to +/- 2000°/s */
254 ao_mpu6000_reg_write(MPU6000_GYRO_CONFIG,
255 (0 << MPU600_GYRO_CONFIG_XG_ST) |
256 (0 << MPU600_GYRO_CONFIG_YG_ST) |
257 (0 << MPU600_GYRO_CONFIG_ZG_ST) |
258 (MPU600_GYRO_CONFIG_FS_SEL_2000 << MPU600_GYRO_CONFIG_FS_SEL));
260 ao_delay(AO_MS_TO_TICKS(10));
261 ao_mpu6000_sample(&normal_mode);
263 errors += ao_mpu6000_accel_check(normal_mode.accel_x, test_mode.accel_x, "x");
264 errors += ao_mpu6000_accel_check(normal_mode.accel_y, test_mode.accel_y, "y");
265 errors += ao_mpu6000_accel_check(normal_mode.accel_z, test_mode.accel_z, "z");
267 errors += ao_mpu6000_gyro_check(normal_mode.gyro_x, test_mode.gyro_x, "x");
268 errors += ao_mpu6000_gyro_check(normal_mode.gyro_y, test_mode.gyro_y, "y");
269 errors += ao_mpu6000_gyro_check(normal_mode.gyro_z, test_mode.gyro_z, "z");
272 ao_panic(AO_PANIC_SELF_TEST_MPU6000);
274 /* Filter to about 100Hz, which also sets the gyro rate to 1000Hz */
275 ao_mpu6000_reg_write(MPU6000_CONFIG,
276 (MPU6000_CONFIG_EXT_SYNC_SET_DISABLED << MPU6000_CONFIG_EXT_SYNC_SET) |
277 (MPU6000_CONFIG_DLPF_CFG_94_98 << MPU6000_CONFIG_DLPF_CFG));
279 /* Set sample rate divider to sample at 200Hz (v = gyro/rate - 1) */
280 ao_mpu6000_reg_write(MPU6000_SMPRT_DIV,
283 ao_delay(AO_MS_TO_TICKS(100));
284 ao_mpu6000_configured = 1;
287 struct ao_mpu6000_sample ao_mpu6000_current;
295 ao_mpu6000_sample(&ao_mpu6000_current);
297 AO_DATA_PRESENT(AO_DATA_MPU6000);
303 static struct ao_task ao_mpu6000_task;
306 ao_mpu6000_show(void)
308 struct ao_data sample;
310 ao_data_get(&sample);
311 printf ("Accel: %7d %7d %7d Gyro: %7d %7d %7d\n",
312 sample.mpu6000.accel_x,
313 sample.mpu6000.accel_y,
314 sample.mpu6000.accel_z,
315 sample.mpu6000.gyro_x,
316 sample.mpu6000.gyro_y,
317 sample.mpu6000.gyro_z);
320 static const struct ao_cmds ao_mpu6000_cmds[] = {
321 { ao_mpu6000_show, "I\0Show MPU6000 status" },
326 ao_mpu6000_init(void)
328 ao_mpu6000_configured = 0;
330 ao_add_task(&ao_mpu6000_task, ao_mpu6000, "mpu6000");
331 #ifndef AO_MPU6000_I2C_INDEX
332 ao_spi_init_cs(AO_MPU6000_SPI_CS_PORT, (1 << AO_MPU6000_SPI_CS_PIN));
334 ao_cmd_register(&ao_mpu6000_cmds[0]);