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