Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / src / drivers / ao_mpu6000.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include <ao.h>
19 #include <ao_mpu6000.h>
20 #include <ao_exti.h>
21
22 static uint8_t  ao_mpu6000_wake;
23 static uint8_t  ao_mpu6000_configured;
24
25 static void
26 ao_mpu6000_write(uint8_t addr, uint8_t *data, uint8_t len)
27 {
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);
33 }
34
35 static void
36 ao_mpu6000_reg_write(uint8_t addr, uint8_t value)
37 {
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);
43 }
44
45 static void
46 ao_mpu6000_read(uint8_t addr, void *data, uint8_t len)
47 {
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);
54 }
55
56 static uint8_t
57 ao_mpu6000_reg_read(uint8_t addr)
58 {
59         uint8_t value;
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);
66         return value;
67 }
68
69 static void
70 ao_mpu6000_sample(struct ao_mpu6000_sample *sample)
71 {
72         uint16_t        *d = (uint16_t *) sample;
73         int             i = sizeof (*sample) / 2;
74
75         ao_mpu6000_read(MPU6000_ACCEL_XOUT_H, sample, sizeof (*sample));
76         /* byte swap (sigh) */
77         while (i--) {
78                 uint16_t        t = *d;
79                 *d++ = (t >> 8) | (t << 8);
80         }
81 }
82
83 #define G       981     /* in cm/s² */
84
85 static int16_t /* cm/s² */
86 ao_mpu6000_accel(int16_t v)
87 {
88         return (int16_t) ((v * (int32_t) (16.0 * 980.665 + 0.5)) / 32767);
89 }
90
91 static int16_t  /* deg*10/s */
92 ao_mpu6000_gyro(int16_t v)
93 {
94         return (int16_t) ((v * (int32_t) 20000) / 32767);
95 }
96
97 static uint8_t
98 ao_mpu6000_accel_check(int16_t normal, int16_t test, char *which)
99 {
100         int16_t diff = test - normal;
101
102         if (diff < MPU6000_ST_ACCEL(16) / 2) {
103                 printf ("%s accel self test value too small (normal %d, test %d)\n",
104                         which, normal, test);
105                 return FALSE;
106         }
107         if (diff > MPU6000_ST_ACCEL(16) * 2) {
108                 printf ("%s accel self test value too large (normal %d, test %d)\n",
109                         which, normal, test);
110                 return FALSE;
111         }
112         return TRUE;
113 }
114
115 static uint8_t
116 ao_mpu6000_gyro_check(int16_t normal, int16_t test, char *which)
117 {
118         int16_t diff = test - normal;
119
120         if (diff < 0)
121                 diff = -diff;
122         if (diff < MPU6000_ST_GYRO(2000) / 2) {
123                 printf ("%s gyro self test value too small (normal %d, test %d)\n",
124                         which, normal, test);
125                 return FALSE;
126         }
127         if (diff > MPU6000_ST_GYRO(2000) * 2) {
128                 printf ("%s gyro self test value too large (normal %d, test %d)\n",
129                         which, normal, test);
130                 return FALSE;
131         }
132         return TRUE;
133 }
134
135 static void
136 ao_mpu6000_setup(void)
137 {
138         struct ao_mpu6000_sample        normal_mode, test_mode;
139         int                             t;
140
141         if (ao_mpu6000_configured)
142                 return;
143
144         /* Reset the whole chip */
145         
146         ao_mpu6000_reg_write(MPU6000_PWR_MGMT_1,
147                              (1 << MPU6000_PWR_MGMT_1_DEVICE_RESET));
148         while (ao_mpu6000_reg_read(MPU6000_PWR_MGMT_1) &
149                (1 << MPU6000_PWR_MGMT_1_DEVICE_RESET))
150                 ao_yield();
151
152         /* Reset signal conditioning */
153         ao_mpu6000_reg_write(MPU6000_USER_CONTROL,
154                              (0 << MPU6000_USER_CONTROL_FIFO_EN) |
155                              (0 << MPU6000_USER_CONTROL_I2C_MST_EN) |
156                              (0 << MPU6000_USER_CONTROL_I2C_IF_DIS) |
157                              (0 << MPU6000_USER_CONTROL_FIFO_RESET) |
158                              (0 << MPU6000_USER_CONTROL_I2C_MST_RESET) |
159                              (1 << MPU6000_USER_CONTROL_SIG_COND_RESET));
160
161         while (ao_mpu6000_reg_read(MPU6000_USER_CONTROL) & (1 << MPU6000_USER_CONTROL_SIG_COND_RESET))
162                 ao_yield();
163
164         /* Reset signal paths */
165         ao_mpu6000_reg_write(MPU6000_SIGNAL_PATH_RESET,
166                              (1 << MPU6000_SIGNAL_PATH_RESET_GYRO_RESET) |
167                              (1 << MPU6000_SIGNAL_PATH_RESET_ACCEL_RESET) |
168                              (1 << MPU6000_SIGNAL_PATH_RESET_TEMP_RESET));
169
170         ao_mpu6000_reg_write(MPU6000_SIGNAL_PATH_RESET,
171                              (0 << MPU6000_SIGNAL_PATH_RESET_GYRO_RESET) |
172                              (0 << MPU6000_SIGNAL_PATH_RESET_ACCEL_RESET) |
173                              (0 << MPU6000_SIGNAL_PATH_RESET_TEMP_RESET));
174
175         /* Select clocks, disable sleep */
176         ao_mpu6000_reg_write(MPU6000_PWR_MGMT_1,
177                              (0 << MPU6000_PWR_MGMT_1_DEVICE_RESET) |
178                              (0 << MPU6000_PWR_MGMT_1_SLEEP) |
179                              (0 << MPU6000_PWR_MGMT_1_CYCLE) |
180                              (0 << MPU6000_PWR_MGMT_1_TEMP_DIS) |
181                              (MPU6000_PWR_MGMT_1_CLKSEL_PLL_X_AXIS << MPU6000_PWR_MGMT_1_CLKSEL));
182
183         /* Set sample rate divider to sample at full speed
184         ao_mpu6000_reg_write(MPU6000_SMPRT_DIV, 0);
185
186         /* Disable filtering */
187         ao_mpu6000_reg_write(MPU6000_CONFIG,
188                              (MPU6000_CONFIG_EXT_SYNC_SET_DISABLED << MPU6000_CONFIG_EXT_SYNC_SET) |
189                              (MPU6000_CONFIG_DLPF_CFG_260_256 << MPU6000_CONFIG_DLPF_CFG));
190
191         /* Configure accelerometer to +/-16G in self-test mode */
192         ao_mpu6000_reg_write(MPU6000_ACCEL_CONFIG,
193                              (1 << MPU600_ACCEL_CONFIG_XA_ST) |
194                              (1 << MPU600_ACCEL_CONFIG_YA_ST) |
195                              (1 << MPU600_ACCEL_CONFIG_ZA_ST) |
196                              (MPU600_ACCEL_CONFIG_AFS_SEL_16G << MPU600_ACCEL_CONFIG_AFS_SEL));
197
198         /* Configure gyro to +/- 2000°/s in self-test mode */
199         ao_mpu6000_reg_write(MPU6000_GYRO_CONFIG,
200                              (1 << MPU600_GYRO_CONFIG_XG_ST) |
201                              (1 << MPU600_GYRO_CONFIG_YG_ST) |
202                              (1 << MPU600_GYRO_CONFIG_ZG_ST) |
203                              (MPU600_GYRO_CONFIG_FS_SEL_2000 << MPU600_GYRO_CONFIG_FS_SEL));
204
205         ao_delay(AO_MS_TO_TICKS(200));
206         ao_mpu6000_sample(&test_mode);
207
208         /* Configure accelerometer to +/-16G */
209         ao_mpu6000_reg_write(MPU6000_ACCEL_CONFIG,
210                              (0 << MPU600_ACCEL_CONFIG_XA_ST) |
211                              (0 << MPU600_ACCEL_CONFIG_YA_ST) |
212                              (0 << MPU600_ACCEL_CONFIG_ZA_ST) |
213                              (MPU600_ACCEL_CONFIG_AFS_SEL_16G << MPU600_ACCEL_CONFIG_AFS_SEL));
214
215         /* Configure gyro to +/- 2000°/s */
216         ao_mpu6000_reg_write(MPU6000_GYRO_CONFIG,
217                              (0 << MPU600_GYRO_CONFIG_XG_ST) |
218                              (0 << MPU600_GYRO_CONFIG_YG_ST) |
219                              (0 << MPU600_GYRO_CONFIG_ZG_ST) |
220                              (MPU600_GYRO_CONFIG_FS_SEL_2000 << MPU600_GYRO_CONFIG_FS_SEL));
221
222         ao_delay(AO_MS_TO_TICKS(10));
223         ao_mpu6000_sample(&normal_mode);
224         
225         ao_mpu6000_accel_check(normal_mode.accel_x, test_mode.accel_x, "x");
226         ao_mpu6000_accel_check(normal_mode.accel_y, test_mode.accel_y, "y");
227         ao_mpu6000_accel_check(normal_mode.accel_z, test_mode.accel_z, "z");
228
229         ao_mpu6000_gyro_check(normal_mode.gyro_x, test_mode.gyro_x, "x");
230         ao_mpu6000_gyro_check(normal_mode.gyro_y, test_mode.gyro_y, "y");
231         ao_mpu6000_gyro_check(normal_mode.gyro_z, test_mode.gyro_z, "z");
232
233         /* Filter to about 100Hz, which also sets the gyro rate to 1000Hz */
234         ao_mpu6000_reg_write(MPU6000_CONFIG,
235                              (MPU6000_CONFIG_EXT_SYNC_SET_DISABLED << MPU6000_CONFIG_EXT_SYNC_SET) |
236                              (MPU6000_CONFIG_DLPF_CFG_94_98 << MPU6000_CONFIG_DLPF_CFG));
237
238         /* Set sample rate divider to sample at 200Hz (v = gyro/rate - 1) */
239         ao_mpu6000_reg_write(MPU6000_SMPRT_DIV,
240                              1000 / 200 - 1);
241         
242         ao_delay(AO_MS_TO_TICKS(100));
243         ao_mpu6000_configured = 1;
244 }
245
246
247 static void
248 ao_mpu6000_show(void)
249 {
250         struct ao_mpu6000_sample        sample;
251
252         ao_mpu6000_setup();
253         ao_mpu6000_sample(&sample);
254         printf ("Accel: %7d %7d %7d Gyro: %7d %7d %7d\n",
255                 ao_mpu6000_accel(sample.accel_x),
256                 ao_mpu6000_accel(sample.accel_y),
257                 ao_mpu6000_accel(sample.accel_z),
258                 ao_mpu6000_gyro(sample.gyro_x),
259                 ao_mpu6000_gyro(sample.gyro_y),
260                 ao_mpu6000_gyro(sample.gyro_z));
261 }
262
263 static const struct ao_cmds ao_mpu6000_cmds[] = {
264         { ao_mpu6000_show,      "I\0Show MPU6000 status" },
265         { 0, NULL }
266 };
267
268 void
269 ao_mpu6000_init(void)
270 {
271         ao_mpu6000_configured = 0;
272
273         ao_cmd_register(&ao_mpu6000_cmds[0]);
274 }