altos: Add MPU9250 support to self test and data
[fw/altos] / src / kernel / ao_data.h
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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #ifndef _AO_DATA_H_
20 #define _AO_DATA_H_
21
22 #define GRAVITY 9.80665
23
24 #if HAS_ADC
25 #define AO_DATA_ADC     (1 << 0)
26 #else
27 #define AO_DATA_ADC     0
28 #endif
29
30 #if HAS_MS5607
31 #include <ao_ms5607.h>
32 #define AO_DATA_MS5607  (1 << 1)
33 #else
34 #define AO_DATA_MS5607  0
35 #endif
36
37 #if HAS_MPU6000
38 #include <ao_mpu6000.h>
39 #define AO_DATA_MPU6000 (1 << 2)
40 #else
41 #define AO_DATA_MPU6000 0
42 #endif
43
44 #if HAS_MPU9250
45 #include <ao_mpu9250.h>
46 #define AO_DATA_MPU9250 (1 << 2)
47 #else
48 #define AO_DATA_MPU9250 0
49 #endif
50
51 #if HAS_HMC5883
52 #include <ao_hmc5883.h>
53 #define AO_DATA_HMC5883 (1 << 3)
54 #else
55 #define AO_DATA_HMC5883 0
56 #endif
57
58 #if HAS_MMA655X
59 #include <ao_mma655x.h>
60 #define AO_DATA_MMA655X (1 << 4)
61 #else
62 #define AO_DATA_MMA655X 0
63 #endif
64
65 #ifdef AO_DATA_RING
66
67 #define AO_DATA_ALL     (AO_DATA_ADC|AO_DATA_MS5607|AO_DATA_MPU6000|AO_DATA_HMC5883|AO_DATA_MMA655X|AO_DATA_MPU9250)
68
69 struct ao_data {
70         uint16_t                        tick;
71 #if HAS_ADC
72         struct ao_adc                   adc;
73 #endif
74 #if HAS_MS5607
75         struct ao_ms5607_sample         ms5607_raw;
76         struct ao_ms5607_value          ms5607_cooked;
77 #endif
78 #if HAS_MPU6000
79         struct ao_mpu6000_sample        mpu6000;
80 #if !HAS_MMA655X
81         int16_t                         z_accel;
82 #endif
83 #endif
84 #if HAS_MPU9250
85         struct ao_mpu9250_sample        mpu9250;
86 #endif
87 #if HAS_HMC5883
88         struct ao_hmc5883_sample        hmc5883;
89 #endif
90 #if HAS_MMA655X
91         uint16_t                        mma655x;
92 #endif
93 };
94
95 #define ao_data_ring_next(n)    (((n) + 1) & (AO_DATA_RING - 1))
96 #define ao_data_ring_prev(n)    (((n) - 1) & (AO_DATA_RING - 1))
97
98 /* Get a copy of the last complete sample set */
99 void
100 ao_data_get(__xdata struct ao_data *packet);
101
102 extern volatile __xdata struct ao_data  ao_data_ring[AO_DATA_RING];
103 extern volatile __data uint8_t          ao_data_head;
104 extern volatile __data uint8_t          ao_data_present;
105 extern volatile __data uint8_t          ao_data_count;
106
107 /*
108  * Mark a section of data as ready, check for data complete
109  */
110 #define AO_DATA_PRESENT(bit)    (ao_data_present |= (bit))
111
112 /*
113  * Wait until it is time to write a sensor sample; this is
114  * signaled by the timer tick
115  */
116 #define AO_DATA_WAIT() do {                             \
117                 ao_sleep(DATA_TO_XDATA ((void *) &ao_data_count));      \
118         } while (0)
119
120 #endif /* AO_DATA_RING */
121
122 #if !HAS_BARO && HAS_MS5607
123
124 /* Either an MS5607 or an MS5611 hooked to a SPI port
125  */
126
127 #define HAS_BARO        1
128
129 typedef int32_t pres_t;
130
131 #define AO_ALT_TYPE     int32_t
132
133 typedef AO_ALT_TYPE     alt_t;
134
135 #define ao_data_pres_cook(packet)       ao_ms5607_convert(&packet->ms5607_raw, &packet->ms5607_cooked)
136
137 #define ao_data_pres(packet)    ((packet)->ms5607_cooked.pres)
138 #define ao_data_temp(packet)    ((packet)->ms5607_cooked.temp)
139
140 #define pres_to_altitude(p)     ao_pa_to_altitude(p)
141
142 #endif
143
144 #if !HAS_BARO && HAS_ADC
145
146 #define HAS_BARO        1
147
148 typedef int16_t pres_t;
149 typedef int16_t alt_t;
150
151 #define ao_data_pres(packet)    ((packet)->adc.pres)
152 #define ao_data_temp(packet)    ((packet)->adc.temp)
153 #define pres_to_altitude(p)     ao_pres_to_altitude(p)
154 #define ao_data_pres_cook(p)
155
156 #endif
157
158 /*
159  * Need a few macros to pull data from the sensors:
160  *
161  * ao_data_accel_sample - pull raw sensor and convert to normalized values
162  * ao_data_accel        - pull normalized value (lives in the same memory)
163  * ao_data_set_accel    - store normalized value back in the sensor location
164  * ao_data_accel_invert - flip rocket ends for positive acceleration
165  */
166
167 #if HAS_ACCEL
168
169 /* This section is for an analog accelerometer hooked to one of the ADC pins. As
170  * those are 5V parts, this also requires that the 5V supply be hooked to to anothe ADC
171  * pin so that the both can be measured to correct for changes between the 3.3V and 5V rails
172  */
173
174 typedef int16_t accel_t;
175 #define ao_data_accel(packet)                   ((packet)->adc.accel)
176 #define ao_data_set_accel(packet, a)            ((packet)->adc.accel = (a))
177 #define ao_data_accel_invert(a)                 (0x7fff -(a))
178
179 /*
180  * Ok, the math here is a bit tricky.
181  *
182  * ao_sample_accel:  ADC output for acceleration
183  * ao_accel_ref:  ADC output for the 5V reference.
184  * ao_cook_accel: Corrected acceleration value
185  * Vcc:           3.3V supply to the CC1111
186  * Vac:           5V supply to the accelerometer
187  * accel:         input voltage to accelerometer ADC pin
188  * ref:           input voltage to 5V reference ADC pin
189  *
190  *
191  * Measured acceleration is ratiometric to Vcc:
192  *
193  *     ao_sample_accel   accel
194  *     ------------ = -----
195  *        32767        Vcc
196  *
197  * Measured 5v reference is also ratiometric to Vcc:
198  *
199  *     ao_accel_ref    ref
200  *     ------------ = -----
201  *        32767        Vcc
202  *
203  *
204  *      ao_accel_ref = 32767 * (ref / Vcc)
205  *
206  * Acceleration is measured ratiometric to the 5V supply,
207  * so what we want is:
208  *
209  *      ao_cook_accel    accel
210  *      ------------- =  -----
211  *          32767         ref
212  *
213  *
214  *                      accel    Vcc
215  *                    = ----- *  ---
216  *                       Vcc     ref
217  *
218  *                      ao_sample_accel       32767
219  *                    = ------------ *  ------------
220  *                         32767        ao_accel_ref
221  *
222  * Multiply through by 32767:
223  *
224  *                      ao_sample_accel * 32767
225  *      ao_cook_accel = --------------------
226  *                          ao_accel_ref
227  *
228  * Now, the tricky part. Getting this to compile efficiently
229  * and keeping all of the values in-range.
230  *
231  * First off, we need to use a shift of 16 instead of * 32767 as SDCC
232  * does the obvious optimizations for byte-granularity shifts:
233  *
234  *      ao_cook_accel = (ao_sample_accel << 16) / ao_accel_ref
235  *
236  * Next, lets check our input ranges:
237  *
238  *      0 <= ao_sample_accel <= 0x7fff          (singled ended ADC conversion)
239  *      0x7000 <= ao_accel_ref <= 0x7fff        (the 5V ref value is close to 0x7fff)
240  *
241  * Plugging in our input ranges, we get an output range of 0 - 0x12490,
242  * which is 17 bits. That won't work. If we take the accel ref and shift
243  * by a bit, we'll change its range:
244  *
245  *      0xe000 <= ao_accel_ref<<1 <= 0xfffe
246  *
247  *      ao_cook_accel = (ao_sample_accel << 16) / (ao_accel_ref << 1)
248  *
249  * Now the output range is 0 - 0x9248, which nicely fits in 16 bits. It
250  * is, however, one bit too large for our signed computations. So, we
251  * take the result and shift that by a bit:
252  *
253  *      ao_cook_accel = ((ao_sample_accel << 16) / (ao_accel_ref << 1)) >> 1
254  *
255  * This finally creates an output range of 0 - 0x4924. As the ADC only
256  * provides 11 bits of data, we haven't actually lost any precision,
257  * just dropped a bit of noise off the low end.
258  */
259
260 #if HAS_ACCEL_REF
261
262 #define ao_data_accel_cook(packet) \
263         ((uint16_t) ((((uint32_t) (packet)->adc.accel << 16) / ((packet)->adc.accel_ref << 1))) >> 1)
264
265 #else
266
267 #define ao_data_accel_cook(packet) ((packet)->adc.accel)
268
269 #endif /* HAS_ACCEL_REF */
270
271 #endif  /* HAS_ACCEL */
272
273 #if !HAS_ACCEL && HAS_MMA655X
274
275 #define HAS_ACCEL       1
276
277 typedef int16_t accel_t;
278
279 /* MMA655X is hooked up so that positive values represent negative acceleration */
280
281 #define AO_ACCEL_INVERT         4095
282
283 #ifndef AO_MMA655X_INVERT
284 #error AO_MMA655X_INVERT not defined
285 #endif
286
287 #define ao_data_accel(packet)                   ((packet)->mma655x)
288 #if AO_MMA655X_INVERT
289 #define ao_data_accel_cook(packet)              (AO_ACCEL_INVERT - (packet)->mma655x)
290 #else
291 #define ao_data_accel_cook(packet)              ((packet)->mma655x)
292 #endif
293 #define ao_data_set_accel(packet, accel)        ((packet)->mma655x = (accel))
294 #define ao_data_accel_invert(accel)             (AO_ACCEL_INVERT - (accel))
295
296 #endif
297
298 #if !HAS_ACCEL && HAS_MPU6000
299
300 #define HAS_ACCEL       1
301
302 #define AO_ACCEL_INVERT         0
303
304 typedef int16_t accel_t;
305
306 /* MPU6000 is hooked up so that positive y is positive acceleration */
307 #define ao_data_accel(packet)                   ((packet)->z_accel)
308 #define ao_data_accel_cook(packet)              (-(packet)->mpu6000.accel_y)
309 #define ao_data_set_accel(packet, accel)        ((packet)->z_accel = (accel))
310 #define ao_data_accel_invert(a)                 (-(a))
311
312 #endif
313
314 #if !HAS_GYRO && HAS_MPU6000
315
316 #define HAS_GYRO        1
317
318 typedef int16_t gyro_t;         /* in raw sample units */
319 typedef int16_t angle_t;        /* in degrees */
320
321 /* Y axis is aligned with the direction of motion (along) */
322 /* X axis is aligned in the other board axis (across) */
323 /* Z axis is aligned perpendicular to the board (through) */
324
325 #define ao_data_along(packet)   ((packet)->mpu6000.accel_y)
326 #define ao_data_across(packet)  ((packet)->mpu6000.accel_x)
327 #define ao_data_through(packet) ((packet)->mpu6000.accel_z)
328
329 #define ao_data_roll(packet)    ((packet)->mpu6000.gyro_y)
330 #define ao_data_pitch(packet)   ((packet)->mpu6000.gyro_x)
331 #define ao_data_yaw(packet)     ((packet)->mpu6000.gyro_z)
332
333 #endif
334
335 #if !HAS_MAG && HAS_HMC5883
336
337 #define HAS_MAG         1
338
339 typedef int16_t ao_mag_t;               /* in raw sample units */
340
341 #define ao_data_mag_along(packet)       ((packet)->hmc5883.x)
342 #define ao_data_mag_across(packet)      ((packet)->hmc5883.y)
343 #define ao_data_mag_through(packet)     ((packet)->hmc5883.z)
344
345 #endif
346
347 #endif /* _AO_DATA_H_ */