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