altos: Don't start ADC ring until the other sensors have a valid value
[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 #if __BYTE_ORDER == __LITTLE_ENDIAN
77         /* byte swap */
78         while (i--) {
79                 uint16_t        t = *d;
80                 *d++ = (t >> 8) | (t << 8);
81         }
82 #endif
83 }
84
85 #define G       981     /* in cm/s² */
86
87 static int16_t /* cm/s² */
88 ao_mpu6000_accel(int16_t v)
89 {
90         return (int16_t) ((v * (int32_t) (16.0 * 980.665 + 0.5)) / 32767);
91 }
92
93 static int16_t  /* deg*10/s */
94 ao_mpu6000_gyro(int16_t v)
95 {
96         return (int16_t) ((v * (int32_t) 20000) / 32767);
97 }
98
99 static uint8_t
100 ao_mpu6000_accel_check(int16_t normal, int16_t test, char *which)
101 {
102         int16_t diff = test - normal;
103
104         if (diff < MPU6000_ST_ACCEL(16) / 2) {
105                 printf ("%s accel self test value too small (normal %d, test %d)\n",
106                         which, normal, test);
107                 return FALSE;
108         }
109         if (diff > MPU6000_ST_ACCEL(16) * 2) {
110                 printf ("%s accel self test value too large (normal %d, test %d)\n",
111                         which, normal, test);
112                 return FALSE;
113         }
114         return TRUE;
115 }
116
117 static uint8_t
118 ao_mpu6000_gyro_check(int16_t normal, int16_t test, char *which)
119 {
120         int16_t diff = test - normal;
121
122         if (diff < 0)
123                 diff = -diff;
124         if (diff < MPU6000_ST_GYRO(2000) / 2) {
125                 printf ("%s gyro self test value too small (normal %d, test %d)\n",
126                         which, normal, test);
127                 return FALSE;
128         }
129         if (diff > MPU6000_ST_GYRO(2000) * 2) {
130                 printf ("%s gyro self test value too large (normal %d, test %d)\n",
131                         which, normal, test);
132                 return FALSE;
133         }
134         return TRUE;
135 }
136
137 static void
138 ao_mpu6000_setup(void)
139 {
140         struct ao_mpu6000_sample        normal_mode, test_mode;
141         int                             t;
142
143         if (ao_mpu6000_configured)
144                 return;
145
146         /* Reset the whole chip */
147         
148         ao_mpu6000_reg_write(MPU6000_PWR_MGMT_1,
149                              (1 << MPU6000_PWR_MGMT_1_DEVICE_RESET));
150
151         /* Wait for it to reset. If we talk too quickly, it appears to get confused */
152         ao_delay(AO_MS_TO_TICKS(100));
153
154         /* Reset signal conditioning */
155         ao_mpu6000_reg_write(MPU6000_USER_CONTROL,
156                              (0 << MPU6000_USER_CONTROL_FIFO_EN) |
157                              (0 << MPU6000_USER_CONTROL_I2C_MST_EN) |
158                              (0 << MPU6000_USER_CONTROL_I2C_IF_DIS) |
159                              (0 << MPU6000_USER_CONTROL_FIFO_RESET) |
160                              (0 << MPU6000_USER_CONTROL_I2C_MST_RESET) |
161                              (1 << MPU6000_USER_CONTROL_SIG_COND_RESET));
162
163         while (ao_mpu6000_reg_read(MPU6000_USER_CONTROL) & (1 << MPU6000_USER_CONTROL_SIG_COND_RESET))
164                 ao_yield();
165
166         /* Reset signal paths */
167         ao_mpu6000_reg_write(MPU6000_SIGNAL_PATH_RESET,
168                              (1 << MPU6000_SIGNAL_PATH_RESET_GYRO_RESET) |
169                              (1 << MPU6000_SIGNAL_PATH_RESET_ACCEL_RESET) |
170                              (1 << MPU6000_SIGNAL_PATH_RESET_TEMP_RESET));
171
172         ao_mpu6000_reg_write(MPU6000_SIGNAL_PATH_RESET,
173                              (0 << MPU6000_SIGNAL_PATH_RESET_GYRO_RESET) |
174                              (0 << MPU6000_SIGNAL_PATH_RESET_ACCEL_RESET) |
175                              (0 << MPU6000_SIGNAL_PATH_RESET_TEMP_RESET));
176
177         /* Select clocks, disable sleep */
178         ao_mpu6000_reg_write(MPU6000_PWR_MGMT_1,
179                              (0 << MPU6000_PWR_MGMT_1_DEVICE_RESET) |
180                              (0 << MPU6000_PWR_MGMT_1_SLEEP) |
181                              (0 << MPU6000_PWR_MGMT_1_CYCLE) |
182                              (0 << MPU6000_PWR_MGMT_1_TEMP_DIS) |
183                              (MPU6000_PWR_MGMT_1_CLKSEL_PLL_X_AXIS << MPU6000_PWR_MGMT_1_CLKSEL));
184
185         /* Set sample rate divider to sample at full speed
186         ao_mpu6000_reg_write(MPU6000_SMPRT_DIV, 0);
187
188         /* Disable filtering */
189         ao_mpu6000_reg_write(MPU6000_CONFIG,
190                              (MPU6000_CONFIG_EXT_SYNC_SET_DISABLED << MPU6000_CONFIG_EXT_SYNC_SET) |
191                              (MPU6000_CONFIG_DLPF_CFG_260_256 << MPU6000_CONFIG_DLPF_CFG));
192
193         /* Configure accelerometer to +/-16G in self-test mode */
194         ao_mpu6000_reg_write(MPU6000_ACCEL_CONFIG,
195                              (1 << MPU600_ACCEL_CONFIG_XA_ST) |
196                              (1 << MPU600_ACCEL_CONFIG_YA_ST) |
197                              (1 << MPU600_ACCEL_CONFIG_ZA_ST) |
198                              (MPU600_ACCEL_CONFIG_AFS_SEL_16G << MPU600_ACCEL_CONFIG_AFS_SEL));
199
200         /* Configure gyro to +/- 2000°/s in self-test mode */
201         ao_mpu6000_reg_write(MPU6000_GYRO_CONFIG,
202                              (1 << MPU600_GYRO_CONFIG_XG_ST) |
203                              (1 << MPU600_GYRO_CONFIG_YG_ST) |
204                              (1 << MPU600_GYRO_CONFIG_ZG_ST) |
205                              (MPU600_GYRO_CONFIG_FS_SEL_2000 << MPU600_GYRO_CONFIG_FS_SEL));
206
207         ao_delay(AO_MS_TO_TICKS(200));
208         ao_mpu6000_sample(&test_mode);
209
210         /* Configure accelerometer to +/-16G */
211         ao_mpu6000_reg_write(MPU6000_ACCEL_CONFIG,
212                              (0 << MPU600_ACCEL_CONFIG_XA_ST) |
213                              (0 << MPU600_ACCEL_CONFIG_YA_ST) |
214                              (0 << MPU600_ACCEL_CONFIG_ZA_ST) |
215                              (MPU600_ACCEL_CONFIG_AFS_SEL_16G << MPU600_ACCEL_CONFIG_AFS_SEL));
216
217         /* Configure gyro to +/- 2000°/s */
218         ao_mpu6000_reg_write(MPU6000_GYRO_CONFIG,
219                              (0 << MPU600_GYRO_CONFIG_XG_ST) |
220                              (0 << MPU600_GYRO_CONFIG_YG_ST) |
221                              (0 << MPU600_GYRO_CONFIG_ZG_ST) |
222                              (MPU600_GYRO_CONFIG_FS_SEL_2000 << MPU600_GYRO_CONFIG_FS_SEL));
223
224         ao_delay(AO_MS_TO_TICKS(10));
225         ao_mpu6000_sample(&normal_mode);
226         
227         ao_mpu6000_accel_check(normal_mode.accel_x, test_mode.accel_x, "x");
228         ao_mpu6000_accel_check(normal_mode.accel_y, test_mode.accel_y, "y");
229         ao_mpu6000_accel_check(normal_mode.accel_z, test_mode.accel_z, "z");
230
231         ao_mpu6000_gyro_check(normal_mode.gyro_x, test_mode.gyro_x, "x");
232         ao_mpu6000_gyro_check(normal_mode.gyro_y, test_mode.gyro_y, "y");
233         ao_mpu6000_gyro_check(normal_mode.gyro_z, test_mode.gyro_z, "z");
234
235         /* Filter to about 100Hz, which also sets the gyro rate to 1000Hz */
236         ao_mpu6000_reg_write(MPU6000_CONFIG,
237                              (MPU6000_CONFIG_EXT_SYNC_SET_DISABLED << MPU6000_CONFIG_EXT_SYNC_SET) |
238                              (MPU6000_CONFIG_DLPF_CFG_94_98 << MPU6000_CONFIG_DLPF_CFG));
239
240         /* Set sample rate divider to sample at 200Hz (v = gyro/rate - 1) */
241         ao_mpu6000_reg_write(MPU6000_SMPRT_DIV,
242                              1000 / 200 - 1);
243         
244         ao_delay(AO_MS_TO_TICKS(100));
245         ao_mpu6000_configured = 1;
246 }
247
248 struct ao_mpu6000_sample ao_mpu6000_current;
249 uint8_t ao_mpu6000_valid;
250
251 static void
252 ao_mpu6000(void)
253 {
254         ao_mpu6000_setup();
255         for (;;)
256         {
257                 struct ao_mpu6000_sample ao_mpu6000_next;
258                 ao_mpu6000_sample(&ao_mpu6000_next);
259                 ao_arch_critical(
260                         ao_mpu6000_current = ao_mpu6000_next;
261                         ao_mpu6000_valid = 1;
262                         );
263                 ao_delay(0);
264         }
265 }
266
267 static struct ao_task ao_mpu6000_task;
268
269 static void
270 ao_mpu6000_show(void)
271 {
272         struct ao_mpu6000_sample        sample;
273
274         sample = ao_mpu6000_current;
275         printf ("Accel: %7d %7d %7d Gyro: %7d %7d %7d\n",
276                 ao_mpu6000_accel(sample.accel_x),
277                 ao_mpu6000_accel(sample.accel_y),
278                 ao_mpu6000_accel(sample.accel_z),
279                 ao_mpu6000_gyro(sample.gyro_x),
280                 ao_mpu6000_gyro(sample.gyro_y),
281                 ao_mpu6000_gyro(sample.gyro_z));
282 }
283
284 static const struct ao_cmds ao_mpu6000_cmds[] = {
285         { ao_mpu6000_show,      "I\0Show MPU6000 status" },
286         { 0, NULL }
287 };
288
289 void
290 ao_mpu6000_init(void)
291 {
292         ao_mpu6000_configured = 0;
293         ao_mpu6000_valid = 0;
294
295         ao_add_task(&ao_mpu6000_task, ao_mpu6000, "mpu6000");
296         ao_cmd_register(&ao_mpu6000_cmds[0]);
297 }