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