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>
22 static uint8_t ao_mpu6000_wake;
23 static uint8_t ao_mpu6000_configured;
26 ao_mpu6000_write(uint8_t addr, uint8_t *data, uint8_t len)
28 ao_i2c_get(AO_MPU6000_I2C_INDEX);
29 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE);
30 ao_i2c_send(&addr, 1, AO_MPU6000_I2C_INDEX, FALSE);
31 ao_i2c_send(data, len, AO_MPU6000_I2C_INDEX, TRUE);
32 ao_i2c_put(AO_MPU6000_I2C_INDEX);
36 ao_mpu6000_reg_write(uint8_t addr, uint8_t value)
38 uint8_t d[2] = { addr, value };
39 ao_i2c_get(AO_MPU6000_I2C_INDEX);
40 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE);
41 ao_i2c_send(d, 2, AO_MPU6000_I2C_INDEX, TRUE);
42 ao_i2c_put(AO_MPU6000_I2C_INDEX);
46 ao_mpu6000_read(uint8_t addr, void *data, uint8_t len)
48 ao_i2c_get(AO_MPU6000_I2C_INDEX);
49 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE);
50 ao_i2c_send(&addr, 1, AO_MPU6000_I2C_INDEX, FALSE);
51 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_READ);
52 ao_i2c_recv(data, len, AO_MPU6000_I2C_INDEX, TRUE);
53 ao_i2c_put(AO_MPU6000_I2C_INDEX);
57 ao_mpu6000_reg_read(uint8_t addr)
60 ao_i2c_get(AO_MPU6000_I2C_INDEX);
61 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE);
62 ao_i2c_send(&addr, 1, AO_MPU6000_I2C_INDEX, FALSE);
63 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_READ);
64 ao_i2c_recv(&value, 1, AO_MPU6000_I2C_INDEX, TRUE);
65 ao_i2c_put(AO_MPU6000_I2C_INDEX);
70 ao_mpu6000_sample(struct ao_mpu6000_sample *sample)
72 uint16_t *d = (uint16_t *) sample;
73 int i = sizeof (*sample) / 2;
75 ao_mpu6000_read(MPU6000_ACCEL_XOUT_H, sample, sizeof (*sample));
76 #if __BYTE_ORDER == __LITTLE_ENDIAN
80 *d++ = (t >> 8) | (t << 8);
85 #define G 981 /* in cm/s² */
87 static int16_t /* cm/s² */
88 ao_mpu6000_accel(int16_t v)
90 return (int16_t) ((v * (int32_t) (16.0 * 980.665 + 0.5)) / 32767);
93 static int16_t /* deg*10/s */
94 ao_mpu6000_gyro(int16_t v)
96 return (int16_t) ((v * (int32_t) 20000) / 32767);
100 ao_mpu6000_accel_check(int16_t normal, int16_t test, char *which)
102 int16_t diff = test - normal;
104 if (diff < MPU6000_ST_ACCEL(16) / 2) {
107 if (diff > MPU6000_ST_ACCEL(16) * 2) {
114 ao_mpu6000_gyro_check(int16_t normal, int16_t test, char *which)
116 int16_t diff = test - normal;
120 if (diff < MPU6000_ST_GYRO(2000) / 2) {
123 if (diff > MPU6000_ST_GYRO(2000) * 2) {
130 ao_mpu6000_setup(void)
132 struct ao_mpu6000_sample normal_mode, test_mode;
135 if (ao_mpu6000_configured)
138 /* Reset the whole chip */
140 ao_mpu6000_reg_write(MPU6000_PWR_MGMT_1,
141 (1 << MPU6000_PWR_MGMT_1_DEVICE_RESET));
143 /* Wait for it to reset. If we talk too quickly, it appears to get confused */
144 ao_delay(AO_MS_TO_TICKS(100));
146 /* Reset signal conditioning */
147 ao_mpu6000_reg_write(MPU6000_USER_CONTROL,
148 (0 << MPU6000_USER_CONTROL_FIFO_EN) |
149 (0 << MPU6000_USER_CONTROL_I2C_MST_EN) |
150 (0 << MPU6000_USER_CONTROL_I2C_IF_DIS) |
151 (0 << MPU6000_USER_CONTROL_FIFO_RESET) |
152 (0 << MPU6000_USER_CONTROL_I2C_MST_RESET) |
153 (1 << MPU6000_USER_CONTROL_SIG_COND_RESET));
155 while (ao_mpu6000_reg_read(MPU6000_USER_CONTROL) & (1 << MPU6000_USER_CONTROL_SIG_COND_RESET))
158 /* Reset signal paths */
159 ao_mpu6000_reg_write(MPU6000_SIGNAL_PATH_RESET,
160 (1 << MPU6000_SIGNAL_PATH_RESET_GYRO_RESET) |
161 (1 << MPU6000_SIGNAL_PATH_RESET_ACCEL_RESET) |
162 (1 << MPU6000_SIGNAL_PATH_RESET_TEMP_RESET));
164 ao_mpu6000_reg_write(MPU6000_SIGNAL_PATH_RESET,
165 (0 << MPU6000_SIGNAL_PATH_RESET_GYRO_RESET) |
166 (0 << MPU6000_SIGNAL_PATH_RESET_ACCEL_RESET) |
167 (0 << MPU6000_SIGNAL_PATH_RESET_TEMP_RESET));
169 /* Select clocks, disable sleep */
170 ao_mpu6000_reg_write(MPU6000_PWR_MGMT_1,
171 (0 << MPU6000_PWR_MGMT_1_DEVICE_RESET) |
172 (0 << MPU6000_PWR_MGMT_1_SLEEP) |
173 (0 << MPU6000_PWR_MGMT_1_CYCLE) |
174 (0 << MPU6000_PWR_MGMT_1_TEMP_DIS) |
175 (MPU6000_PWR_MGMT_1_CLKSEL_PLL_X_AXIS << MPU6000_PWR_MGMT_1_CLKSEL));
177 /* Set sample rate divider to sample at full speed
178 ao_mpu6000_reg_write(MPU6000_SMPRT_DIV, 0);
180 /* Disable filtering */
181 ao_mpu6000_reg_write(MPU6000_CONFIG,
182 (MPU6000_CONFIG_EXT_SYNC_SET_DISABLED << MPU6000_CONFIG_EXT_SYNC_SET) |
183 (MPU6000_CONFIG_DLPF_CFG_260_256 << MPU6000_CONFIG_DLPF_CFG));
185 /* Configure accelerometer to +/-16G in self-test mode */
186 ao_mpu6000_reg_write(MPU6000_ACCEL_CONFIG,
187 (1 << MPU600_ACCEL_CONFIG_XA_ST) |
188 (1 << MPU600_ACCEL_CONFIG_YA_ST) |
189 (1 << MPU600_ACCEL_CONFIG_ZA_ST) |
190 (MPU600_ACCEL_CONFIG_AFS_SEL_16G << MPU600_ACCEL_CONFIG_AFS_SEL));
192 /* Configure gyro to +/- 2000°/s in self-test mode */
193 ao_mpu6000_reg_write(MPU6000_GYRO_CONFIG,
194 (1 << MPU600_GYRO_CONFIG_XG_ST) |
195 (1 << MPU600_GYRO_CONFIG_YG_ST) |
196 (1 << MPU600_GYRO_CONFIG_ZG_ST) |
197 (MPU600_GYRO_CONFIG_FS_SEL_2000 << MPU600_GYRO_CONFIG_FS_SEL));
199 ao_delay(AO_MS_TO_TICKS(200));
200 ao_mpu6000_sample(&test_mode);
202 /* Configure accelerometer to +/-16G */
203 ao_mpu6000_reg_write(MPU6000_ACCEL_CONFIG,
204 (0 << MPU600_ACCEL_CONFIG_XA_ST) |
205 (0 << MPU600_ACCEL_CONFIG_YA_ST) |
206 (0 << MPU600_ACCEL_CONFIG_ZA_ST) |
207 (MPU600_ACCEL_CONFIG_AFS_SEL_16G << MPU600_ACCEL_CONFIG_AFS_SEL));
209 /* Configure gyro to +/- 2000°/s */
210 ao_mpu6000_reg_write(MPU6000_GYRO_CONFIG,
211 (0 << MPU600_GYRO_CONFIG_XG_ST) |
212 (0 << MPU600_GYRO_CONFIG_YG_ST) |
213 (0 << MPU600_GYRO_CONFIG_ZG_ST) |
214 (MPU600_GYRO_CONFIG_FS_SEL_2000 << MPU600_GYRO_CONFIG_FS_SEL));
216 ao_delay(AO_MS_TO_TICKS(10));
217 ao_mpu6000_sample(&normal_mode);
219 errors += ao_mpu6000_accel_check(normal_mode.accel_x, test_mode.accel_x, "x");
220 errors += ao_mpu6000_accel_check(normal_mode.accel_y, test_mode.accel_y, "y");
221 errors += ao_mpu6000_accel_check(normal_mode.accel_z, test_mode.accel_z, "z");
223 errors += ao_mpu6000_gyro_check(normal_mode.gyro_x, test_mode.gyro_x, "x");
224 errors += ao_mpu6000_gyro_check(normal_mode.gyro_y, test_mode.gyro_y, "y");
225 errors += ao_mpu6000_gyro_check(normal_mode.gyro_z, test_mode.gyro_z, "z");
228 ao_panic(AO_PANIC_SELF_TEST_MPU6000);
230 /* Filter to about 100Hz, which also sets the gyro rate to 1000Hz */
231 ao_mpu6000_reg_write(MPU6000_CONFIG,
232 (MPU6000_CONFIG_EXT_SYNC_SET_DISABLED << MPU6000_CONFIG_EXT_SYNC_SET) |
233 (MPU6000_CONFIG_DLPF_CFG_94_98 << MPU6000_CONFIG_DLPF_CFG));
235 /* Set sample rate divider to sample at 200Hz (v = gyro/rate - 1) */
236 ao_mpu6000_reg_write(MPU6000_SMPRT_DIV,
239 ao_delay(AO_MS_TO_TICKS(100));
240 ao_mpu6000_configured = 1;
243 struct ao_mpu6000_sample ao_mpu6000_current;
251 ao_mpu6000_sample(&ao_mpu6000_current);
253 AO_DATA_PRESENT(AO_DATA_MPU6000);
259 static struct ao_task ao_mpu6000_task;
262 ao_mpu6000_show(void)
264 struct ao_data sample;
266 ao_data_get(&sample);
267 printf ("Accel: %7d %7d %7d Gyro: %7d %7d %7d\n",
268 sample.mpu6000.accel_x,
269 sample.mpu6000.accel_y,
270 sample.mpu6000.accel_z,
271 sample.mpu6000.gyro_x,
272 sample.mpu6000.gyro_y,
273 sample.mpu6000.gyro_z);
276 static const struct ao_cmds ao_mpu6000_cmds[] = {
277 { ao_mpu6000_show, "I\0Show MPU6000 status" },
282 ao_mpu6000_init(void)
284 ao_mpu6000_configured = 0;
286 ao_add_task(&ao_mpu6000_task, ao_mpu6000, "mpu6000");
287 ao_cmd_register(&ao_mpu6000_cmds[0]);