dd3da8dee3837f504a6df05d4633a0d5e03bf9cf
[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; 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_mpu6000.h>
21 #include <ao_exti.h>
22
23 #if HAS_MPU6000
24
25 static uint8_t  ao_mpu6000_configured;
26
27 #ifndef AO_MPU6000_I2C_INDEX
28 #define AO_MPU6000_SPI  1
29 #else
30 #define AO_MPU6000_SPI  0
31 #endif
32
33 #if AO_MPU6000_SPI
34
35 #define AO_MPU6000_SPI_SPEED    ao_spi_speed(1000000)   /* 1Mhz for all register access */
36
37 #define ao_mpu6000_spi_get()    ao_spi_get(AO_MPU6000_SPI_BUS, AO_MPU6000_SPI_SPEED)
38 #define ao_mpu6000_spi_put()    ao_spi_put(AO_MPU6000_SPI_BUS)
39
40 #define ao_mpu6000_spi_start()  ao_spi_set_cs(AO_MPU6000_SPI_CS_PORT,   \
41                                               (1 << AO_MPU6000_SPI_CS_PIN))
42
43 #define ao_mpu6000_spi_end()    ao_spi_clr_cs(AO_MPU6000_SPI_CS_PORT,   \
44                                               (1 << AO_MPU6000_SPI_CS_PIN))
45
46 #endif
47
48
49 static void
50 _ao_mpu6000_reg_write(uint8_t addr, uint8_t value)
51 {
52         uint8_t d[2] = { addr, value };
53 #if AO_MPU6000_SPI
54         ao_mpu6000_spi_start();
55         ao_spi_send(d, 2, AO_MPU6000_SPI_BUS);
56         ao_mpu6000_spi_end();
57 #else
58         ao_i2c_get(AO_MPU6000_I2C_INDEX);
59         ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE);
60         ao_i2c_send(d, 2, AO_MPU6000_I2C_INDEX, true);
61         ao_i2c_put(AO_MPU6000_I2C_INDEX);
62 #endif
63 }
64
65 static void
66 _ao_mpu6000_read(uint8_t addr, void *data, uint8_t len)
67 {
68 #if AO_MPU6000_SPI
69         addr |= 0x80;
70         ao_mpu6000_spi_start();
71         ao_spi_send(&addr, 1, AO_MPU6000_SPI_BUS);
72         ao_spi_recv(data, len, AO_MPU6000_SPI_BUS);
73         ao_mpu6000_spi_end();
74 #else
75         ao_i2c_get(AO_MPU6000_I2C_INDEX);
76         ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE);
77         ao_i2c_send(&addr, 1, AO_MPU6000_I2C_INDEX, false);
78         ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_READ);
79         ao_i2c_recv(data, len, AO_MPU6000_I2C_INDEX, true);
80         ao_i2c_put(AO_MPU6000_I2C_INDEX);
81 #endif
82 }
83
84 static uint8_t
85 _ao_mpu6000_reg_read(uint8_t addr)
86 {
87         uint8_t value;
88 #if AO_MPU6000_SPI
89         addr |= 0x80;
90         ao_mpu6000_spi_start();
91         ao_spi_send(&addr, 1, AO_MPU6000_SPI_BUS);
92         ao_spi_recv(&value, 1, AO_MPU6000_SPI_BUS);
93         ao_mpu6000_spi_end();
94 #else
95         ao_i2c_get(AO_MPU6000_I2C_INDEX);
96         ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE);
97         ao_i2c_send(&addr, 1, AO_MPU6000_I2C_INDEX, false);
98         ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_READ);
99         ao_i2c_recv(&value, 1, AO_MPU6000_I2C_INDEX, true);
100         ao_i2c_put(AO_MPU6000_I2C_INDEX);
101 #endif
102         return value;
103 }
104
105 static void
106 _ao_mpu6000_sample(struct ao_mpu6000_sample *sample)
107 {
108         uint16_t        *d = (uint16_t *) sample;
109         int             i = sizeof (*sample) / 2;
110
111         _ao_mpu6000_read(MPU6000_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_mpu6000_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_mpu6000_gyro(int16_t v)
132 {
133         return (int16_t) ((v * (int32_t) 20000) / 32767);
134 }
135 #endif
136
137 static uint8_t
138 ao_mpu6000_accel_check(int16_t normal, int16_t test)
139 {
140         int16_t diff = test - normal;
141
142         if (diff < MPU6000_ST_ACCEL(16) / 4) {
143                 return 1;
144         }
145         if (diff > MPU6000_ST_ACCEL(16) * 4) {
146                 return 1;
147         }
148         return 0;
149 }
150
151 static uint8_t
152 ao_mpu6000_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 < MPU6000_ST_GYRO(2000) / 4) {
159                 return 1;
160         }
161         if (diff > MPU6000_ST_GYRO(2000) * 4) {
162                 return 1;
163         }
164         return 0;
165 }
166
167 static uint8_t  mpu_id;
168
169 static void
170 _ao_mpu6000_wait_alive(void)
171 {
172         uint8_t i;
173
174         /* Wait for the chip to wake up */
175         for (i = 0; i < 30; i++) {
176                 ao_delay(AO_MS_TO_TICKS(100));
177                 mpu_id = _ao_mpu6000_reg_read(MPU6000_WHO_AM_I);
178                 if (mpu_id == 0x68)
179                         return;
180         }
181         AO_SENSOR_ERROR(AO_DATA_MPU6000);
182 }
183
184 #define ST_TRIES        10
185
186 static void
187 _ao_mpu6000_setup(void)
188 {
189         struct ao_mpu6000_sample        normal_mode, test_mode;
190         int                             errors;
191         int                             st_tries;
192
193         if (ao_mpu6000_configured)
194                 return;
195
196         _ao_mpu6000_wait_alive();
197
198         /* Reset the whole chip */
199
200         _ao_mpu6000_reg_write(MPU6000_PWR_MGMT_1,
201                               (1 << MPU6000_PWR_MGMT_1_DEVICE_RESET));
202
203         /* Wait for it to reset. If we talk too quickly, it appears to get confused */
204
205         _ao_mpu6000_wait_alive();
206
207         /* Reset signal conditioning, disabling I2C on SPI systems */
208         _ao_mpu6000_reg_write(MPU6000_USER_CTRL,
209                               (0 << MPU6000_USER_CTRL_FIFO_EN) |
210                               (0 << MPU6000_USER_CTRL_I2C_MST_EN) |
211                               (AO_MPU6000_SPI << MPU6000_USER_CTRL_I2C_IF_DIS) |
212                               (0 << MPU6000_USER_CTRL_FIFO_RESET) |
213                               (0 << MPU6000_USER_CTRL_I2C_MST_RESET) |
214                               (1 << MPU6000_USER_CTRL_SIG_COND_RESET));
215
216         while (_ao_mpu6000_reg_read(MPU6000_USER_CTRL) & (1 << MPU6000_USER_CTRL_SIG_COND_RESET))
217                 ao_delay(AO_MS_TO_TICKS(10));
218
219         /* Reset signal paths */
220         _ao_mpu6000_reg_write(MPU6000_SIGNAL_PATH_RESET,
221                               (1 << MPU6000_SIGNAL_PATH_RESET_GYRO_RESET) |
222                               (1 << MPU6000_SIGNAL_PATH_RESET_ACCEL_RESET) |
223                               (1 << MPU6000_SIGNAL_PATH_RESET_TEMP_RESET));
224
225         _ao_mpu6000_reg_write(MPU6000_SIGNAL_PATH_RESET,
226                               (0 << MPU6000_SIGNAL_PATH_RESET_GYRO_RESET) |
227                               (0 << MPU6000_SIGNAL_PATH_RESET_ACCEL_RESET) |
228                               (0 << MPU6000_SIGNAL_PATH_RESET_TEMP_RESET));
229
230         /* Select clocks, disable sleep */
231         _ao_mpu6000_reg_write(MPU6000_PWR_MGMT_1,
232                               (0 << MPU6000_PWR_MGMT_1_DEVICE_RESET) |
233                               (0 << MPU6000_PWR_MGMT_1_SLEEP) |
234                               (0 << MPU6000_PWR_MGMT_1_CYCLE) |
235                               (0 << MPU6000_PWR_MGMT_1_TEMP_DIS) |
236                               (MPU6000_PWR_MGMT_1_CLKSEL_PLL_X_AXIS << MPU6000_PWR_MGMT_1_CLKSEL));
237
238         /* Set sample rate divider to sample at full speed */
239         _ao_mpu6000_reg_write(MPU6000_SMPRT_DIV, 0);
240
241         /* Disable filtering */
242         _ao_mpu6000_reg_write(MPU6000_CONFIG,
243                               (MPU6000_CONFIG_EXT_SYNC_SET_DISABLED << MPU6000_CONFIG_EXT_SYNC_SET) |
244                               (MPU6000_CONFIG_DLPF_CFG_260_256 << MPU6000_CONFIG_DLPF_CFG));
245
246 #if TRIDGE
247         // read the product ID rev c has 1/2 the sensitivity of rev d
248         _mpu6000_product_id = _register_read(MPUREG_PRODUCT_ID);
249         //Serial.printf("Product_ID= 0x%x\n", (unsigned) _mpu6000_product_id);
250
251         if ((_mpu6000_product_id == MPU6000ES_REV_C4) || (_mpu6000_product_id == MPU6000ES_REV_C5) ||
252             (_mpu6000_product_id == MPU6000_REV_C4) || (_mpu6000_product_id == MPU6000_REV_C5)) {
253                 // Accel scale 8g (4096 LSB/g)
254                 // Rev C has different scaling than rev D
255                 register_write(MPUREG_ACCEL_CONFIG,1<<3);
256         } else {
257                 // Accel scale 8g (4096 LSB/g)
258                 register_write(MPUREG_ACCEL_CONFIG,2<<3);
259         }
260         hal.scheduler->delay(1);
261 #endif
262
263         for (st_tries = 0; st_tries < ST_TRIES; st_tries++) {
264                 errors = 0;
265
266                 /* Configure accelerometer to +/-16G in self-test mode */
267                 _ao_mpu6000_reg_write(MPU6000_ACCEL_CONFIG,
268                                       (1 << MPU600_ACCEL_CONFIG_XA_ST) |
269                                       (1 << MPU600_ACCEL_CONFIG_YA_ST) |
270                                       (1 << MPU600_ACCEL_CONFIG_ZA_ST) |
271                                       (MPU600_ACCEL_CONFIG_AFS_SEL_16G << MPU600_ACCEL_CONFIG_AFS_SEL));
272
273                 /* Configure gyro to +/- 2000°/s in self-test mode */
274                 _ao_mpu6000_reg_write(MPU6000_GYRO_CONFIG,
275                                       (1 << MPU600_GYRO_CONFIG_XG_ST) |
276                                       (1 << MPU600_GYRO_CONFIG_YG_ST) |
277                                       (1 << MPU600_GYRO_CONFIG_ZG_ST) |
278                                       (MPU600_GYRO_CONFIG_FS_SEL_2000 << MPU600_GYRO_CONFIG_FS_SEL));
279
280                 ao_delay(AO_MS_TO_TICKS(200));
281                 _ao_mpu6000_sample(&test_mode);
282
283                 /* Configure accelerometer to +/-16G */
284                 _ao_mpu6000_reg_write(MPU6000_ACCEL_CONFIG,
285                                       (0 << MPU600_ACCEL_CONFIG_XA_ST) |
286                                       (0 << MPU600_ACCEL_CONFIG_YA_ST) |
287                                       (0 << MPU600_ACCEL_CONFIG_ZA_ST) |
288                                       (MPU600_ACCEL_CONFIG_AFS_SEL_16G << MPU600_ACCEL_CONFIG_AFS_SEL));
289
290                 /* Configure gyro to +/- 2000°/s */
291                 _ao_mpu6000_reg_write(MPU6000_GYRO_CONFIG,
292                                       (0 << MPU600_GYRO_CONFIG_XG_ST) |
293                                       (0 << MPU600_GYRO_CONFIG_YG_ST) |
294                                       (0 << MPU600_GYRO_CONFIG_ZG_ST) |
295                                       (MPU600_GYRO_CONFIG_FS_SEL_2000 << MPU600_GYRO_CONFIG_FS_SEL));
296
297                 ao_delay(AO_MS_TO_TICKS(200));
298                 _ao_mpu6000_sample(&normal_mode);
299
300                 errors += ao_mpu6000_accel_check(normal_mode.accel_x, test_mode.accel_x);
301                 errors += ao_mpu6000_accel_check(normal_mode.accel_y, test_mode.accel_y);
302                 errors += ao_mpu6000_accel_check(normal_mode.accel_z, test_mode.accel_z);
303
304                 errors += ao_mpu6000_gyro_check(normal_mode.gyro_x, test_mode.gyro_x);
305                 errors += ao_mpu6000_gyro_check(normal_mode.gyro_y, test_mode.gyro_y);
306                 errors += ao_mpu6000_gyro_check(normal_mode.gyro_z, test_mode.gyro_z);
307                 if (!errors)
308                         break;
309         }
310
311         if (st_tries == ST_TRIES)
312                 AO_SENSOR_ERROR(AO_DATA_MPU6000);
313
314         /* Filter to about 100Hz, which also sets the gyro rate to 1000Hz */
315         _ao_mpu6000_reg_write(MPU6000_CONFIG,
316                               (MPU6000_CONFIG_EXT_SYNC_SET_DISABLED << MPU6000_CONFIG_EXT_SYNC_SET) |
317                               (MPU6000_CONFIG_DLPF_CFG_94_98 << MPU6000_CONFIG_DLPF_CFG));
318
319         /* Set sample rate divider to sample at 200Hz (v = gyro/rate - 1) */
320         _ao_mpu6000_reg_write(MPU6000_SMPRT_DIV,
321                               1000 / 200 - 1);
322
323         ao_delay(AO_MS_TO_TICKS(100));
324         ao_mpu6000_configured = 1;
325 }
326
327 struct ao_mpu6000_sample        ao_mpu6000_current;
328
329 static void
330 ao_mpu6000(void)
331 {
332         struct ao_mpu6000_sample        sample;
333         /* ao_mpu6000_init already grabbed the SPI bus and mutex */
334         _ao_mpu6000_setup();
335 #if AO_MPU6000_SPI
336         ao_mpu6000_spi_put();
337 #endif
338         for (;;)
339         {
340 #if AO_MPU6000_SPI
341                 ao_mpu6000_spi_get();
342 #endif
343                 _ao_mpu6000_sample(&sample);
344 #if AO_MPU6000_SPI
345                 ao_mpu6000_spi_put();
346 #endif
347                 ao_arch_block_interrupts();
348                 ao_mpu6000_current = sample;
349                 AO_DATA_PRESENT(AO_DATA_MPU6000);
350                 AO_DATA_WAIT();
351                 ao_arch_release_interrupts();
352         }
353 }
354
355 static struct ao_task ao_mpu6000_task;
356
357 static void
358 ao_mpu6000_show(void)
359 {
360         printf ("Accel: %7d %7d %7d Gyro: %7d %7d %7d id %02x\n",
361                 ao_mpu6000_current.accel_x,
362                 ao_mpu6000_current.accel_y,
363                 ao_mpu6000_current.accel_z,
364                 ao_mpu6000_current.gyro_x,
365                 ao_mpu6000_current.gyro_y,
366                 ao_mpu6000_current.gyro_z,
367                 mpu_id);
368 }
369
370 static const struct ao_cmds ao_mpu6000_cmds[] = {
371         { ao_mpu6000_show,      "I\0Show MPU6000 status" },
372         { 0, NULL }
373 };
374
375 void
376 ao_mpu6000_init(void)
377 {
378         ao_mpu6000_configured = 0;
379
380         ao_add_task(&ao_mpu6000_task, ao_mpu6000, "mpu6000");
381
382 #if AO_MPU6000_SPI
383         ao_spi_init_cs(AO_MPU6000_SPI_CS_PORT, (1 << AO_MPU6000_SPI_CS_PIN));
384
385         /* Pretend to be the mpu6000 task. Grab the SPI bus right away and
386          * hold it for the task so that nothing else uses the SPI bus before
387          * we get the I2C mode disabled in the chip
388          */
389
390         ao_cur_task = &ao_mpu6000_task;
391         ao_mpu6000_spi_get();
392         ao_cur_task = NULL;
393 #endif
394
395         ao_cmd_register(&ao_mpu6000_cmds[0]);
396 }
397 #endif