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