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