b79f27ca868d7bb849b22128eebd28810067e81a
[fw/altos] / src / drivers / ao_mpu9250.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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <ao.h>
20 #include <ao_mpu9250.h>
21 #include <ao_exti.h>
22
23 #if HAS_MPU9250
24
25 static uint8_t  ao_mpu9250_configured;
26
27 extern uint8_t ao_sensor_errors;
28
29 #ifndef AO_MPU9250_I2C_INDEX
30 #define AO_MPU9250_SPI  1
31 #else
32 #define AO_MPU9250_SPI  0
33 #endif
34
35 #if AO_MPU9250_SPI
36
37 #define ao_mpu9250_spi_get()    ao_spi_get(AO_MPU9250_SPI_BUS, AO_SPI_SPEED_1MHz)
38 #define ao_mpu9250_spi_put()    ao_spi_put(AO_MPU9250_SPI_BUS)
39
40 #define ao_mpu9250_spi_start()  ao_spi_set_cs(AO_MPU9250_SPI_CS_PORT,   \
41                                               (1 << AO_MPU9250_SPI_CS_PIN))
42
43 #define ao_mpu9250_spi_end()    ao_spi_clr_cs(AO_MPU9250_SPI_CS_PORT,   \
44                                               (1 << AO_MPU9250_SPI_CS_PIN))
45
46 #endif
47
48
49 static void
50 _ao_mpu9250_reg_write(uint8_t addr, uint8_t value)
51 {
52         uint8_t d[2] = { addr, value };
53 #if AO_MPU9250_SPI
54         ao_mpu9250_spi_start();
55         ao_spi_send(d, 2, AO_MPU9250_SPI_BUS);
56         ao_mpu9250_spi_end();
57 #else
58         ao_i2c_get(AO_MPU9250_I2C_INDEX);
59         ao_i2c_start(AO_MPU9250_I2C_INDEX, MPU9250_ADDR_WRITE);
60         ao_i2c_send(d, 2, AO_MPU9250_I2C_INDEX, TRUE);
61         ao_i2c_put(AO_MPU9250_I2C_INDEX);
62 #endif
63 }
64
65 static void
66 _ao_mpu9250_read(uint8_t addr, void *data, uint8_t len)
67 {
68 #if AO_MPU9250_SPI
69         addr |= 0x80;
70         ao_mpu9250_spi_start();
71         ao_spi_send(&addr, 1, AO_MPU9250_SPI_BUS);
72         ao_spi_recv(data, len, AO_MPU9250_SPI_BUS);
73         ao_mpu9250_spi_end();
74 #else
75         ao_i2c_get(AO_MPU9250_I2C_INDEX);
76         ao_i2c_start(AO_MPU9250_I2C_INDEX, MPU9250_ADDR_WRITE);
77         ao_i2c_send(&addr, 1, AO_MPU9250_I2C_INDEX, FALSE);
78         ao_i2c_start(AO_MPU9250_I2C_INDEX, MPU9250_ADDR_READ);
79         ao_i2c_recv(data, len, AO_MPU9250_I2C_INDEX, TRUE);
80         ao_i2c_put(AO_MPU9250_I2C_INDEX);
81 #endif
82 }
83
84 static uint8_t
85 _ao_mpu9250_reg_read(uint8_t addr)
86 {
87         uint8_t value;
88 #if AO_MPU9250_SPI
89         addr |= 0x80;
90         ao_mpu9250_spi_start();
91         ao_spi_send(&addr, 1, AO_MPU9250_SPI_BUS);
92         ao_spi_recv(&value, 1, AO_MPU9250_SPI_BUS);
93         ao_mpu9250_spi_end();
94 #else
95         ao_i2c_get(AO_MPU9250_I2C_INDEX);
96         ao_i2c_start(AO_MPU9250_I2C_INDEX, MPU9250_ADDR_WRITE);
97         ao_i2c_send(&addr, 1, AO_MPU9250_I2C_INDEX, FALSE);
98         ao_i2c_start(AO_MPU9250_I2C_INDEX, MPU9250_ADDR_READ);
99         ao_i2c_recv(&value, 1, AO_MPU9250_I2C_INDEX, TRUE);
100         ao_i2c_put(AO_MPU9250_I2C_INDEX);
101 #endif
102         return value;
103 }
104
105 static void
106 _ao_mpu9250_sample(struct ao_mpu9250_sample *sample)
107 {
108         uint16_t        *d = (uint16_t *) sample;
109         int             i = sizeof (*sample) / 2;
110
111         _ao_mpu9250_read(MPU9250_ACCEL_XOUT_H, sample, sizeof (*sample));
112 #if __BYTE_ORDER == __LITTLE_ENDIAN
113         /* byte swap */
114         while (i--) {
115                 uint16_t        t = *d;
116                 *d++ = (t >> 8) | (t << 8);
117         }
118 #endif
119 }
120
121 #define G       981     /* in cm/s² */
122
123 #if 0
124 static int16_t /* cm/s² */
125 ao_mpu9250_accel(int16_t v)
126 {
127         return (int16_t) ((v * (int32_t) (16.0 * 980.665 + 0.5)) / 32767);
128 }
129
130 static int16_t  /* deg*10/s */
131 ao_mpu9250_gyro(int16_t v)
132 {
133         return (int16_t) ((v * (int32_t) 20000) / 32767);
134 }
135 #endif
136
137 static uint8_t
138 ao_mpu9250_accel_check(int16_t normal, int16_t test)
139 {
140         int16_t diff = test - normal;
141
142         if (diff < MPU9250_ST_ACCEL(16) / 4) {
143                 return 1;
144         }
145         if (diff > MPU9250_ST_ACCEL(16) * 4) {
146                 return 1;
147         }
148         return 0;
149 }
150
151 static uint8_t
152 ao_mpu9250_gyro_check(int16_t normal, int16_t test)
153 {
154         int16_t diff = test - normal;
155
156         if (diff < 0)
157                 diff = -diff;
158         if (diff < MPU9250_ST_GYRO(2000) / 4) {
159                 return 1;
160         }
161         if (diff > MPU9250_ST_GYRO(2000) * 4) {
162                 return 1;
163         }
164         return 0;
165 }
166
167 static void
168 _ao_mpu9250_wait_alive(void)
169 {
170         uint8_t i;
171
172         /* Wait for the chip to wake up */
173         for (i = 0; i < 30; i++) {
174                 ao_delay(AO_MS_TO_TICKS(100));
175                 if (_ao_mpu9250_reg_read(MPU9250_WHO_AM_I) == MPU9250_I_AM_9250)
176                         break;
177         }
178         if (i == 30)
179                 ao_panic(AO_PANIC_SELF_TEST_MPU9250);
180 }
181
182 #define ST_TRIES        10
183
184 static void
185 _ao_mpu9250_setup(void)
186 {
187         struct ao_mpu9250_sample        normal_mode, test_mode;
188         int                             errors;
189         int                             st_tries;
190
191         if (ao_mpu9250_configured)
192                 return;
193
194         _ao_mpu9250_wait_alive();
195
196         /* Reset the whole chip */
197
198         _ao_mpu9250_reg_write(MPU9250_PWR_MGMT_1,
199                               (1 << MPU9250_PWR_MGMT_1_DEVICE_RESET));
200
201         /* Wait for it to reset. If we talk too quickly, it appears to get confused */
202
203         _ao_mpu9250_wait_alive();
204
205         /* Reset signal conditioning, disabling I2C on SPI systems */
206         _ao_mpu9250_reg_write(MPU9250_USER_CTRL,
207                               (0 << MPU9250_USER_CTRL_FIFO_EN) |
208                               (0 << MPU9250_USER_CTRL_I2C_MST_EN) |
209                               (AO_MPU9250_SPI << MPU9250_USER_CTRL_I2C_IF_DIS) |
210                               (0 << MPU9250_USER_CTRL_FIFO_RESET) |
211                               (0 << MPU9250_USER_CTRL_I2C_MST_RESET) |
212                               (1 << MPU9250_USER_CTRL_SIG_COND_RESET));
213
214         while (_ao_mpu9250_reg_read(MPU9250_USER_CTRL) & (1 << MPU9250_USER_CTRL_SIG_COND_RESET))
215                 ao_delay(AO_MS_TO_TICKS(10));
216
217         /* Reset signal paths */
218         _ao_mpu9250_reg_write(MPU9250_SIGNAL_PATH_RESET,
219                               (1 << MPU9250_SIGNAL_PATH_RESET_GYRO_RESET) |
220                               (1 << MPU9250_SIGNAL_PATH_RESET_ACCEL_RESET) |
221                               (1 << MPU9250_SIGNAL_PATH_RESET_TEMP_RESET));
222
223         _ao_mpu9250_reg_write(MPU9250_SIGNAL_PATH_RESET,
224                               (0 << MPU9250_SIGNAL_PATH_RESET_GYRO_RESET) |
225                               (0 << MPU9250_SIGNAL_PATH_RESET_ACCEL_RESET) |
226                               (0 << MPU9250_SIGNAL_PATH_RESET_TEMP_RESET));
227
228         /* Select clocks, disable sleep */
229         _ao_mpu9250_reg_write(MPU9250_PWR_MGMT_1,
230                               (0 << MPU9250_PWR_MGMT_1_DEVICE_RESET) |
231                               (0 << MPU9250_PWR_MGMT_1_SLEEP) |
232                               (0 << MPU9250_PWR_MGMT_1_CYCLE) |
233                               (0 << MPU9250_PWR_MGMT_1_TEMP_DIS) |
234                               (MPU9250_PWR_MGMT_1_CLKSEL_PLL_X_AXIS << MPU9250_PWR_MGMT_1_CLKSEL));
235
236         /* Set sample rate divider to sample at full speed */
237         _ao_mpu9250_reg_write(MPU9250_SMPRT_DIV, 0);
238
239         /* Disable filtering */
240         _ao_mpu9250_reg_write(MPU9250_CONFIG,
241                               (MPU9250_CONFIG_EXT_SYNC_SET_DISABLED << MPU9250_CONFIG_EXT_SYNC_SET) |
242                               (MPU9250_CONFIG_DLPF_CFG_250 << MPU9250_CONFIG_DLPF_CFG));
243
244         for (st_tries = 0; st_tries < ST_TRIES; st_tries++) {
245                 errors = 0;
246
247                 /* Configure accelerometer to +/-16G in self-test mode */
248                 _ao_mpu9250_reg_write(MPU9250_ACCEL_CONFIG,
249                                       (1 << MPU9250_ACCEL_CONFIG_XA_ST) |
250                                       (1 << MPU9250_ACCEL_CONFIG_YA_ST) |
251                                       (1 << MPU9250_ACCEL_CONFIG_ZA_ST) |
252                                       (MPU9250_ACCEL_CONFIG_AFS_SEL_16G << MPU9250_ACCEL_CONFIG_AFS_SEL));
253
254                 /* Configure gyro to +/- 2000°/s in self-test mode */
255                 _ao_mpu9250_reg_write(MPU9250_GYRO_CONFIG,
256                                       (1 << MPU9250_GYRO_CONFIG_XG_ST) |
257                                       (1 << MPU9250_GYRO_CONFIG_YG_ST) |
258                                       (1 << MPU9250_GYRO_CONFIG_ZG_ST) |
259                                       (MPU9250_GYRO_CONFIG_FS_SEL_2000 << MPU9250_GYRO_CONFIG_FS_SEL));
260
261                 ao_delay(AO_MS_TO_TICKS(200));
262                 _ao_mpu9250_sample(&test_mode);
263
264                 /* Configure accelerometer to +/-16G */
265                 _ao_mpu9250_reg_write(MPU9250_ACCEL_CONFIG,
266                                       (0 << MPU9250_ACCEL_CONFIG_XA_ST) |
267                                       (0 << MPU9250_ACCEL_CONFIG_YA_ST) |
268                                       (0 << MPU9250_ACCEL_CONFIG_ZA_ST) |
269                                       (MPU9250_ACCEL_CONFIG_AFS_SEL_16G << MPU9250_ACCEL_CONFIG_AFS_SEL));
270
271                 /* Configure gyro to +/- 2000°/s */
272                 _ao_mpu9250_reg_write(MPU9250_GYRO_CONFIG,
273                                       (0 << MPU9250_GYRO_CONFIG_XG_ST) |
274                                       (0 << MPU9250_GYRO_CONFIG_YG_ST) |
275                                       (0 << MPU9250_GYRO_CONFIG_ZG_ST) |
276                                       (MPU9250_GYRO_CONFIG_FS_SEL_2000 << MPU9250_GYRO_CONFIG_FS_SEL));
277
278                 ao_delay(AO_MS_TO_TICKS(200));
279                 _ao_mpu9250_sample(&normal_mode);
280
281                 errors += ao_mpu9250_accel_check(normal_mode.accel_x, test_mode.accel_x);
282                 errors += ao_mpu9250_accel_check(normal_mode.accel_y, test_mode.accel_y);
283                 errors += ao_mpu9250_accel_check(normal_mode.accel_z, test_mode.accel_z);
284
285                 errors += ao_mpu9250_gyro_check(normal_mode.gyro_x, test_mode.gyro_x);
286                 errors += ao_mpu9250_gyro_check(normal_mode.gyro_y, test_mode.gyro_y);
287                 errors += ao_mpu9250_gyro_check(normal_mode.gyro_z, test_mode.gyro_z);
288                 if (!errors)
289                         break;
290         }
291
292         if (st_tries == ST_TRIES)
293                 ao_sensor_errors = 1;
294
295         /* Filter to about 100Hz, which also sets the gyro rate to 1000Hz */
296         _ao_mpu9250_reg_write(MPU9250_CONFIG,
297                               (MPU9250_CONFIG_FIFO_MODE_REPLACE << MPU9250_CONFIG_FIFO_MODE) |
298                               (MPU9250_CONFIG_EXT_SYNC_SET_DISABLED << MPU9250_CONFIG_EXT_SYNC_SET) |
299                               (MPU9250_CONFIG_DLPF_CFG_92 << MPU9250_CONFIG_DLPF_CFG));
300
301         /* Set sample rate divider to sample at 200Hz (v = gyro/rate - 1) */
302         _ao_mpu9250_reg_write(MPU9250_SMPRT_DIV,
303                               1000 / 200 - 1);
304
305         ao_delay(AO_MS_TO_TICKS(100));
306         ao_mpu9250_configured = 1;
307 }
308
309 struct ao_mpu9250_sample        ao_mpu9250_current;
310
311 static void
312 ao_mpu9250(void)
313 {
314         struct ao_mpu9250_sample        sample;
315         /* ao_mpu9250_init already grabbed the SPI bus and mutex */
316         _ao_mpu9250_setup();
317 #if AO_MPU9250_SPI
318         ao_mpu9250_spi_put();
319 #endif
320         for (;;)
321         {
322 #if AO_MPU9250_SPI
323                 ao_mpu9250_spi_get();
324 #endif
325                 _ao_mpu9250_sample(&sample);
326 #if AO_MPU9250_SPI
327                 ao_mpu9250_spi_put();
328 #endif
329                 ao_arch_block_interrupts();
330                 ao_mpu9250_current = sample;
331                 AO_DATA_PRESENT(AO_DATA_MPU9250);
332                 AO_DATA_WAIT();
333                 ao_arch_release_interrupts();
334         }
335 }
336
337 static struct ao_task ao_mpu9250_task;
338
339 static void
340 ao_mpu9250_show(void)
341 {
342         printf ("Accel: %7d %7d %7d Gyro: %7d %7d %7d\n",
343                 ao_mpu9250_current.accel_x,
344                 ao_mpu9250_current.accel_y,
345                 ao_mpu9250_current.accel_z,
346                 ao_mpu9250_current.gyro_x,
347                 ao_mpu9250_current.gyro_y,
348                 ao_mpu9250_current.gyro_z);
349 }
350
351 static void
352 ao_mpu9250_read(void)
353 {
354         uint8_t addr;
355         uint8_t val;
356
357         ao_cmd_hex();
358         if (ao_cmd_status != ao_cmd_success)
359                 return;
360         addr = ao_cmd_lex_i;
361         ao_mpu9250_spi_get();
362         val = _ao_mpu9250_reg_read(addr);
363         ao_mpu9250_spi_put();
364         printf("Addr %02x val %02x\n", addr, val);
365 }
366
367 static void
368 ao_mpu9250_write(void)
369 {
370         uint8_t addr;
371         uint8_t val;
372
373         ao_cmd_hex();
374         if (ao_cmd_status != ao_cmd_success)
375                 return;
376         addr = ao_cmd_lex_i;
377         ao_cmd_hex();
378         if (ao_cmd_status != ao_cmd_success)
379                 return;
380         val = ao_cmd_lex_i;
381         printf("Addr %02x val %02x\n", addr, val);
382         ao_mpu9250_spi_get();
383         _ao_mpu9250_reg_write(addr, val);
384         ao_mpu9250_spi_put();
385 }
386
387 static const struct ao_cmds ao_mpu9250_cmds[] = {
388         { ao_mpu9250_show,      "I\0Show MPU9250 status" },
389         { ao_mpu9250_read,      "R <addr>\0Read MPU9250 register" },
390         { ao_mpu9250_write,     "W <addr> <val>\0Write MPU9250 register" },
391         { 0, NULL }
392 };
393
394 void
395 ao_mpu9250_init(void)
396 {
397         ao_mpu9250_configured = 0;
398
399         ao_add_task(&ao_mpu9250_task, ao_mpu9250, "mpu9250");
400
401 #if AO_MPU9250_SPI
402         ao_spi_init_cs(AO_MPU9250_SPI_CS_PORT, (1 << AO_MPU9250_SPI_CS_PIN));
403
404         /* Pretend to be the mpu9250 task. Grab the SPI bus right away and
405          * hold it for the task so that nothing else uses the SPI bus before
406          * we get the I2C mode disabled in the chip
407          */
408
409         ao_cur_task = &ao_mpu9250_task;
410         ao_spi_get(AO_MPU9250_SPI_BUS, AO_SPI_SPEED_1MHz);
411         ao_cur_task = NULL;
412 #endif
413         ao_cmd_register(&ao_mpu9250_cmds[0]);
414 }
415 #endif