74e444a9d0950ba21a4439d19a5adcc5999a0f3c
[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 #define MPU9250_TEST    0
26
27 static uint8_t  ao_mpu9250_configured;
28
29 extern uint8_t ao_sensor_errors;
30
31 #ifndef AO_MPU9250_I2C_INDEX
32 #define AO_MPU9250_SPI  1
33 #else
34 #define AO_MPU9250_SPI  0
35 #endif
36
37 #if AO_MPU9250_SPI
38
39 #define ao_mpu9250_spi_get()    ao_spi_get(AO_MPU9250_SPI_BUS, AO_SPI_SPEED_1MHz)
40 #define ao_mpu9250_spi_put()    ao_spi_put(AO_MPU9250_SPI_BUS)
41
42 #define ao_mpu9250_spi_start()  ao_spi_set_cs(AO_MPU9250_SPI_CS_PORT,   \
43                                               (1 << AO_MPU9250_SPI_CS_PIN))
44
45 #define ao_mpu9250_spi_end()    ao_spi_clr_cs(AO_MPU9250_SPI_CS_PORT,   \
46                                               (1 << AO_MPU9250_SPI_CS_PIN))
47
48 #else
49
50 #define ao_mpu9250_spi_get()
51 #define ao_mpu9250_spi_put()
52
53 #endif
54
55 static void
56 _ao_mpu9250_reg_write(uint8_t addr, uint8_t value)
57 {
58         uint8_t d[2] = { addr, value };
59 #if AO_MPU9250_SPI
60         ao_mpu9250_spi_start();
61         ao_spi_send(d, 2, AO_MPU9250_SPI_BUS);
62         ao_mpu9250_spi_end();
63 #else
64         ao_i2c_get(AO_MPU9250_I2C_INDEX);
65         ao_i2c_start(AO_MPU9250_I2C_INDEX, MPU9250_ADDR_WRITE);
66         ao_i2c_send(d, 2, AO_MPU9250_I2C_INDEX, true);
67         ao_i2c_put(AO_MPU9250_I2C_INDEX);
68 #endif
69 }
70
71 static void
72 _ao_mpu9250_read(uint8_t addr, void *data, uint8_t len)
73 {
74 #if AO_MPU9250_SPI
75         addr |= 0x80;
76         ao_mpu9250_spi_start();
77         ao_spi_send(&addr, 1, AO_MPU9250_SPI_BUS);
78         ao_spi_recv(data, len, AO_MPU9250_SPI_BUS);
79         ao_mpu9250_spi_end();
80 #else
81         ao_i2c_get(AO_MPU9250_I2C_INDEX);
82         ao_i2c_start(AO_MPU9250_I2C_INDEX, MPU9250_ADDR_WRITE);
83         ao_i2c_send(&addr, 1, AO_MPU9250_I2C_INDEX, false);
84         ao_i2c_start(AO_MPU9250_I2C_INDEX, MPU9250_ADDR_READ);
85         ao_i2c_recv(data, len, AO_MPU9250_I2C_INDEX, true);
86         ao_i2c_put(AO_MPU9250_I2C_INDEX);
87 #endif
88 }
89
90 static uint8_t
91 _ao_mpu9250_reg_read(uint8_t addr)
92 {
93         uint8_t value;
94 #if AO_MPU9250_SPI
95         addr |= 0x80;
96         ao_mpu9250_spi_start();
97         ao_spi_send(&addr, 1, AO_MPU9250_SPI_BUS);
98         ao_spi_recv(&value, 1, AO_MPU9250_SPI_BUS);
99         ao_mpu9250_spi_end();
100 #else
101         ao_i2c_get(AO_MPU9250_I2C_INDEX);
102         ao_i2c_start(AO_MPU9250_I2C_INDEX, MPU9250_ADDR_WRITE);
103         ao_i2c_send(&addr, 1, AO_MPU9250_I2C_INDEX, false);
104         ao_i2c_start(AO_MPU9250_I2C_INDEX, MPU9250_ADDR_READ);
105         ao_i2c_recv(&value, 1, AO_MPU9250_I2C_INDEX, true);
106         ao_i2c_put(AO_MPU9250_I2C_INDEX);
107 #endif
108         return value;
109 }
110
111 static void
112 _ao_mpu9250_slv4_setup(uint8_t addr, uint8_t reg)
113 {
114         /* Set i2c slave address */
115         _ao_mpu9250_reg_write(MPU9250_I2C_SLV4_ADDR,
116                               addr);
117
118         /* Set i2c register address */
119         _ao_mpu9250_reg_write(MPU9250_I2C_SLV4_REG,
120                               reg);
121 }
122
123 static void
124 _ao_mpu9250_slv4_run(void)
125 {
126         uint8_t ctrl;
127
128         /* Start the transfer */
129         _ao_mpu9250_reg_write(MPU9250_I2C_SLV4_CTRL,
130                               (1 << MPU9250_I2C_SLV4_CTRL_I2C_SLV4_EN) |
131                               (0 << MPU9250_I2C_SLV4_CTRL_SLV4_DONE_INT_EN) |
132                               (0 << MPU9250_I2C_SLV4_CTRL_I2C_SLV4_REG_DIS) |
133                               (0 << MPU9250_I2C_SLV4_CTRL_I2C_MST_DLY));
134
135         /* Poll for completion */
136         for (;;) {
137                 ctrl = _ao_mpu9250_reg_read(MPU9250_I2C_SLV4_CTRL);
138                 if ((ctrl & (1 << MPU9250_I2C_SLV4_CTRL_I2C_SLV4_EN)) == 0)
139                         break;
140                 ao_delay(0);
141         }
142 }
143
144 static uint8_t
145 _ao_mpu9250_mag_reg_read(uint8_t reg)
146 {
147         _ao_mpu9250_slv4_setup((1 << 7) | MPU9250_MAG_ADDR, reg);
148
149         _ao_mpu9250_slv4_run();
150
151         return _ao_mpu9250_reg_read(MPU9250_I2C_SLV4_DI);
152 }
153
154 static void
155 _ao_mpu9250_mag_reg_write(uint8_t reg, uint8_t value)
156 {
157         _ao_mpu9250_slv4_setup((0 << 7) | MPU9250_MAG_ADDR, reg);
158
159         /* Set the data */
160         _ao_mpu9250_reg_write(MPU9250_I2C_SLV4_DO,
161                               value);
162
163         _ao_mpu9250_slv4_run();
164 }
165
166 static void
167 _ao_mpu9250_sample(struct ao_mpu9250_sample *sample)
168 {
169         uint16_t        *d = (uint16_t *) sample;
170         int             i = sizeof (*sample) / 2;
171
172         _ao_mpu9250_read(MPU9250_ACCEL_XOUT_H, sample, sizeof (*sample));
173 #if __BYTE_ORDER == __LITTLE_ENDIAN
174         /* byte swap */
175         while (i--) {
176                 uint16_t        t = *d;
177                 *d++ = (t >> 8) | (t << 8);
178         }
179 #endif
180 }
181
182 #define G       981     /* in cm/s² */
183
184 #if 0
185 static int16_t /* cm/s² */
186 ao_mpu9250_accel(int16_t v)
187 {
188         return (int16_t) ((v * (int32_t) (16.0 * 980.665 + 0.5)) / 32767);
189 }
190
191 static int16_t  /* deg*10/s */
192 ao_mpu9250_gyro(int16_t v)
193 {
194         return (int16_t) ((v * (int32_t) 20000) / 32767);
195 }
196 #endif
197
198 static uint8_t
199 ao_mpu9250_accel_check(int16_t normal, int16_t test)
200 {
201         int16_t diff = test - normal;
202
203         if (diff < MPU9250_ST_ACCEL(16) / 4) {
204                 return 1;
205         }
206         if (diff > MPU9250_ST_ACCEL(16) * 4) {
207                 return 1;
208         }
209         return 0;
210 }
211
212 static uint8_t
213 ao_mpu9250_gyro_check(int16_t normal, int16_t test)
214 {
215         int16_t diff = test - normal;
216
217         if (diff < 0)
218                 diff = -diff;
219         if (diff < MPU9250_ST_GYRO(2000) / 4) {
220                 return 1;
221         }
222         if (diff > MPU9250_ST_GYRO(2000) * 4) {
223                 return 1;
224         }
225         return 0;
226 }
227
228 static void
229 _ao_mpu9250_wait_alive(void)
230 {
231         uint8_t i;
232
233         /* Wait for the chip to wake up */
234         for (i = 0; i < 30; i++) {
235                 ao_delay(AO_MS_TO_TICKS(100));
236                 if (_ao_mpu9250_reg_read(MPU9250_WHO_AM_I) == MPU9250_I_AM_9250)
237                         break;
238         }
239         if (i == 30)
240                 ao_panic(AO_PANIC_SELF_TEST_MPU9250);
241 }
242
243 #define ST_TRIES        10
244 #define MAG_TRIES       10
245
246 static void
247 _ao_mpu9250_setup(void)
248 {
249         struct ao_mpu9250_sample        normal_mode, test_mode;
250         int                             errors;
251         int                             st_tries;
252         int                             mag_tries;
253
254         if (ao_mpu9250_configured)
255                 return;
256
257         _ao_mpu9250_wait_alive();
258
259         /* Reset the whole chip */
260
261         _ao_mpu9250_reg_write(MPU9250_PWR_MGMT_1,
262                               (1 << MPU9250_PWR_MGMT_1_DEVICE_RESET));
263
264         /* Wait for it to reset. If we talk too quickly, it appears to get confused */
265
266         _ao_mpu9250_wait_alive();
267
268         /* Reset signal conditioning, disabling I2C on SPI systems */
269         _ao_mpu9250_reg_write(MPU9250_USER_CTRL,
270                               (0 << MPU9250_USER_CTRL_FIFO_EN) |
271                               (1 << MPU9250_USER_CTRL_I2C_MST_EN) |
272                               (AO_MPU9250_SPI << MPU9250_USER_CTRL_I2C_IF_DIS) |
273                               (0 << MPU9250_USER_CTRL_FIFO_RESET) |
274                               (0 << MPU9250_USER_CTRL_I2C_MST_RESET) |
275                               (1 << MPU9250_USER_CTRL_SIG_COND_RESET));
276
277         while (_ao_mpu9250_reg_read(MPU9250_USER_CTRL) & (1 << MPU9250_USER_CTRL_SIG_COND_RESET))
278                 ao_delay(AO_MS_TO_TICKS(10));
279
280         /* Reset signal paths */
281         _ao_mpu9250_reg_write(MPU9250_SIGNAL_PATH_RESET,
282                               (1 << MPU9250_SIGNAL_PATH_RESET_GYRO_RESET) |
283                               (1 << MPU9250_SIGNAL_PATH_RESET_ACCEL_RESET) |
284                               (1 << MPU9250_SIGNAL_PATH_RESET_TEMP_RESET));
285
286         _ao_mpu9250_reg_write(MPU9250_SIGNAL_PATH_RESET,
287                               (0 << MPU9250_SIGNAL_PATH_RESET_GYRO_RESET) |
288                               (0 << MPU9250_SIGNAL_PATH_RESET_ACCEL_RESET) |
289                               (0 << MPU9250_SIGNAL_PATH_RESET_TEMP_RESET));
290
291         /* Select clocks, disable sleep */
292         _ao_mpu9250_reg_write(MPU9250_PWR_MGMT_1,
293                               (0 << MPU9250_PWR_MGMT_1_DEVICE_RESET) |
294                               (0 << MPU9250_PWR_MGMT_1_SLEEP) |
295                               (0 << MPU9250_PWR_MGMT_1_CYCLE) |
296                               (0 << MPU9250_PWR_MGMT_1_TEMP_DIS) |
297                               (MPU9250_PWR_MGMT_1_CLKSEL_PLL_X_AXIS << MPU9250_PWR_MGMT_1_CLKSEL));
298
299         /* Set I2C clock and options */
300         _ao_mpu9250_reg_write(MPU9250_MST_CTRL,
301                               (0 << MPU9250_MST_CTRL_MULT_MST_EN) |
302                               (0 << MPU9250_MST_CTRL_WAIT_FOR_ES) |
303                               (0 << MPU9250_MST_CTRL_SLV_3_FIFO_EN) |
304                               (0 << MPU9250_MST_CTRL_I2C_MST_P_NSR) |
305                               (MPU9250_MST_CTRL_I2C_MST_CLK_400 << MPU9250_MST_CTRL_I2C_MST_CLK));
306
307         /* Set sample rate divider to sample at full speed */
308         _ao_mpu9250_reg_write(MPU9250_SMPRT_DIV, 0);
309
310         /* Disable filtering */
311         _ao_mpu9250_reg_write(MPU9250_CONFIG,
312                               (MPU9250_CONFIG_EXT_SYNC_SET_DISABLED << MPU9250_CONFIG_EXT_SYNC_SET) |
313                               (MPU9250_CONFIG_DLPF_CFG_250 << MPU9250_CONFIG_DLPF_CFG));
314
315         for (st_tries = 0; st_tries < ST_TRIES; st_tries++) {
316                 errors = 0;
317
318                 /* Configure accelerometer to +/-16G in self-test mode */
319                 _ao_mpu9250_reg_write(MPU9250_ACCEL_CONFIG,
320                                       (1 << MPU9250_ACCEL_CONFIG_XA_ST) |
321                                       (1 << MPU9250_ACCEL_CONFIG_YA_ST) |
322                                       (1 << MPU9250_ACCEL_CONFIG_ZA_ST) |
323                                       (MPU9250_ACCEL_CONFIG_AFS_SEL_16G << MPU9250_ACCEL_CONFIG_AFS_SEL));
324
325                 /* Configure gyro to +/- 2000°/s in self-test mode */
326                 _ao_mpu9250_reg_write(MPU9250_GYRO_CONFIG,
327                                       (1 << MPU9250_GYRO_CONFIG_XG_ST) |
328                                       (1 << MPU9250_GYRO_CONFIG_YG_ST) |
329                                       (1 << MPU9250_GYRO_CONFIG_ZG_ST) |
330                                       (MPU9250_GYRO_CONFIG_FS_SEL_2000 << MPU9250_GYRO_CONFIG_FS_SEL));
331
332                 ao_delay(AO_MS_TO_TICKS(200));
333                 _ao_mpu9250_sample(&test_mode);
334
335                 /* Configure accelerometer to +/-16G */
336                 _ao_mpu9250_reg_write(MPU9250_ACCEL_CONFIG,
337                                       (0 << MPU9250_ACCEL_CONFIG_XA_ST) |
338                                       (0 << MPU9250_ACCEL_CONFIG_YA_ST) |
339                                       (0 << MPU9250_ACCEL_CONFIG_ZA_ST) |
340                                       (MPU9250_ACCEL_CONFIG_AFS_SEL_16G << MPU9250_ACCEL_CONFIG_AFS_SEL));
341
342                 /* Configure gyro to +/- 2000°/s */
343                 _ao_mpu9250_reg_write(MPU9250_GYRO_CONFIG,
344                                       (0 << MPU9250_GYRO_CONFIG_XG_ST) |
345                                       (0 << MPU9250_GYRO_CONFIG_YG_ST) |
346                                       (0 << MPU9250_GYRO_CONFIG_ZG_ST) |
347                                       (MPU9250_GYRO_CONFIG_FS_SEL_2000 << MPU9250_GYRO_CONFIG_FS_SEL));
348
349                 ao_delay(AO_MS_TO_TICKS(200));
350                 _ao_mpu9250_sample(&normal_mode);
351
352                 errors += ao_mpu9250_accel_check(normal_mode.accel_x, test_mode.accel_x);
353                 errors += ao_mpu9250_accel_check(normal_mode.accel_y, test_mode.accel_y);
354                 errors += ao_mpu9250_accel_check(normal_mode.accel_z, test_mode.accel_z);
355
356                 errors += ao_mpu9250_gyro_check(normal_mode.gyro_x, test_mode.gyro_x);
357                 errors += ao_mpu9250_gyro_check(normal_mode.gyro_y, test_mode.gyro_y);
358                 errors += ao_mpu9250_gyro_check(normal_mode.gyro_z, test_mode.gyro_z);
359                 if (!errors)
360                         break;
361         }
362
363         if (st_tries == ST_TRIES)
364                 ao_sensor_errors = 1;
365
366         /* Set up the mag sensor */
367
368         /* make sure it's alive */
369         for (mag_tries = 0; mag_tries < MAG_TRIES; mag_tries++) {
370                 if (_ao_mpu9250_mag_reg_read(MPU9250_MAG_WIA) == MPU9250_MAG_WIA_VALUE)
371                         break;
372         }
373
374         if (mag_tries == MAG_TRIES)
375                 ao_sensor_errors = 1;
376
377         /* Select continuous mode 2 (100Hz), 16 bit samples */
378
379         _ao_mpu9250_mag_reg_write(MPU9250_MAG_CNTL1,
380                                   (MPU9250_MAG_CNTL1_BIT_16 << MPU9250_MAG_CNTL1_BIT) |
381                                   (MPU9250_MAG_CNTL1_MODE_CONT_2 << MPU9250_MAG_CNTL1_MODE));
382
383         /* Set i2c master to delay shadowing data until read is
384          * complete (avoids tearing the data) */
385
386         _ao_mpu9250_reg_write(MPU9250_I2C_MST_DELAY_CTRL,
387                               (1 << MPU9250_I2C_MST_DELAY_CTRL_DELAY_ES_SHADOW) |
388                               (0 << MPU9250_I2C_MST_DELAY_CTRL_I2C_SLV4_DLY_EN) |
389                               (0 << MPU9250_I2C_MST_DELAY_CTRL_I2C_SLV3_DLY_EN) |
390                               (0 << MPU9250_I2C_MST_DELAY_CTRL_I2C_SLV2_DLY_EN) |
391                               (0 << MPU9250_I2C_MST_DELAY_CTRL_I2C_SLV1_DLY_EN) |
392                               (0 << MPU9250_I2C_MST_DELAY_CTRL_I2C_SLV0_DLY_EN));
393
394         /* Set up i2c slave 0 to read the mag registers starting at HXL (3) */
395
396         _ao_mpu9250_reg_write(MPU9250_I2C_SLV0_ADDR,
397                               (1 << 7) | MPU9250_MAG_ADDR);
398
399         _ao_mpu9250_reg_write(MPU9250_I2C_SLV0_REG,
400                               MPU9250_MAG_HXL);
401
402         /* Byte swap so the mag values match the gyro/accel. Read 7 bytes
403          * to include the status register
404          */
405
406         _ao_mpu9250_reg_write(MPU9250_I2C_SLV0_CTRL,
407                               (1 << MPU9250_I2C_SLV0_CTRL_I2C_SLV0_EN) |
408                               (1 << MPU9250_I2C_SLV0_CTRL_I2C_SLV0_BYTE_SW) |
409                               (0 << MPU9250_I2C_SLV0_CTRL_I2C_SLV0_REG_DIS) |
410                               (1 << MPU9250_I2C_SLV0_CTRL_I2C_SLV0_GRP) |
411                               (MPU9250_MAG_ST2 - MPU9250_MAG_HXL + 1) << MPU9250_I2C_SLV0_CTRL_I2C_SLV0_LENG);
412
413         /* Filter to about 100Hz, which also sets the gyro rate to 1000Hz */
414         _ao_mpu9250_reg_write(MPU9250_CONFIG,
415                               (MPU9250_CONFIG_FIFO_MODE_REPLACE << MPU9250_CONFIG_FIFO_MODE) |
416                               (MPU9250_CONFIG_EXT_SYNC_SET_DISABLED << MPU9250_CONFIG_EXT_SYNC_SET) |
417                               (MPU9250_CONFIG_DLPF_CFG_92 << MPU9250_CONFIG_DLPF_CFG));
418
419         /* Set sample rate divider to sample at 200Hz (v = gyro/rate - 1) */
420         _ao_mpu9250_reg_write(MPU9250_SMPRT_DIV,
421                               1000 / 200 - 1);
422
423         ao_delay(AO_MS_TO_TICKS(100));
424         ao_mpu9250_configured = 1;
425 }
426
427 struct ao_mpu9250_sample        ao_mpu9250_current;
428
429 static void
430 ao_mpu9250(void)
431 {
432         struct ao_mpu9250_sample        sample;
433
434         /* ao_mpu9250_init already grabbed the SPI bus and mutex */
435         _ao_mpu9250_setup();
436         ao_mpu9250_spi_put();
437         for (;;)
438         {
439                 ao_mpu9250_spi_get();
440                 _ao_mpu9250_sample(&sample);
441                 ao_mpu9250_spi_put();
442                 ao_arch_block_interrupts();
443                 ao_mpu9250_current = sample;
444                 AO_DATA_PRESENT(AO_DATA_MPU9250);
445                 AO_DATA_WAIT();
446                 ao_arch_release_interrupts();
447         }
448 }
449
450 static struct ao_task ao_mpu9250_task;
451
452 static void
453 ao_mpu9250_show(void)
454 {
455         printf ("Accel: %7d %7d %7d Gyro: %7d %7d %7d Mag: %7d %7d %7d\n",
456                 ao_mpu9250_current.accel_x,
457                 ao_mpu9250_current.accel_y,
458                 ao_mpu9250_current.accel_z,
459                 ao_mpu9250_current.gyro_x,
460                 ao_mpu9250_current.gyro_y,
461                 ao_mpu9250_current.gyro_z,
462                 ao_mpu9250_current.mag_x,
463                 ao_mpu9250_current.mag_y,
464                 ao_mpu9250_current.mag_z);
465 }
466
467 #if MPU9250_TEST
468
469 static void
470 ao_mpu9250_read(void)
471 {
472         uint8_t addr;
473         uint8_t val;
474
475         addr = ao_cmd_hex();
476         if (ao_cmd_status != ao_cmd_success)
477                 return;
478         ao_mpu9250_spi_get();
479         val = _ao_mpu9250_reg_read(addr);
480         ao_mpu9250_spi_put();
481         printf("Addr %02x val %02x\n", addr, val);
482 }
483
484 static void
485 ao_mpu9250_write(void)
486 {
487         uint8_t addr;
488         uint8_t val;
489
490         addr = ao_cmd_hex();
491         if (ao_cmd_status != ao_cmd_success)
492                 return;
493         val = ao_cmd_hex();
494         if (ao_cmd_status != ao_cmd_success)
495                 return;
496         printf("Addr %02x val %02x\n", addr, val);
497         ao_mpu9250_spi_get();
498         _ao_mpu9250_reg_write(addr, val);
499         ao_mpu9250_spi_put();
500 }
501
502 static void
503 ao_mpu9250_mag_read(void)
504 {
505         uint8_t addr;
506         uint8_t val;
507
508         addr = ao_cmd_hex();
509         if (ao_cmd_status != ao_cmd_success)
510                 return;
511         ao_mpu9250_spi_get();
512         val = _ao_mpu9250_mag_reg_read(addr);
513         ao_mpu9250_spi_put();
514         printf("Addr %02x val %02x\n", addr, val);
515 }
516
517 static void
518 ao_mpu9250_mag_write(void)
519 {
520         uint8_t addr;
521         uint8_t val;
522
523         addr = ao_cmd_hex();
524         if (ao_cmd_status != ao_cmd_success)
525                 return;
526         val = ao_cmd_hex();
527         if (ao_cmd_status != ao_cmd_success)
528                 return;
529         printf("Addr %02x val %02x\n", addr, val);
530         ao_mpu9250_spi_get();
531         _ao_mpu9250_mag_reg_write(addr, val);
532         ao_mpu9250_spi_put();
533 }
534
535 #endif /* MPU9250_TEST */
536
537 static const struct ao_cmds ao_mpu9250_cmds[] = {
538         { ao_mpu9250_show,      "I\0Show MPU9250 status" },
539 #if MPU9250_TEST
540         { ao_mpu9250_read,      "R <addr>\0Read MPU9250 register" },
541         { ao_mpu9250_write,     "W <addr> <val>\0Write MPU9250 register" },
542         { ao_mpu9250_mag_read,  "G <addr>\0Read MPU9250 Mag register" },
543         { ao_mpu9250_mag_write, "P <addr> <val>\0Write MPU9250 Mag register" },
544 #endif
545         { 0, NULL }
546 };
547
548 void
549 ao_mpu9250_init(void)
550 {
551         ao_mpu9250_configured = 0;
552
553         ao_add_task(&ao_mpu9250_task, ao_mpu9250, "mpu9250");
554
555 #if AO_MPU9250_SPI
556         ao_spi_init_cs(AO_MPU9250_SPI_CS_PORT, (1 << AO_MPU9250_SPI_CS_PIN));
557
558         /* Pretend to be the mpu9250 task. Grab the SPI bus right away and
559          * hold it for the task so that nothing else uses the SPI bus before
560          * we get the I2C mode disabled in the chip
561          */
562
563         ao_cur_task = &ao_mpu9250_task;
564         ao_spi_get(AO_MPU9250_SPI_BUS, AO_SPI_SPEED_1MHz);
565         ao_cur_task = NULL;
566 #endif
567         ao_cmd_register(&ao_mpu9250_cmds[0]);
568 }
569 #endif