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