5a340c3f3b4526f5700ba487063368dcb5602f9c
[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 #if HAS_ADXL375
66 #include <ao_adxl375.h>
67 #define AO_DATA_ADXL375 (1 << 4)
68 #else
69 #define AO_DATA_ADXL375 0
70 #endif
71
72 #if HAS_MAX6691
73 #include <ao_max6691.h>
74 #define AO_DATA_MAX6691 (1 << 4)
75 #else
76 #define AO_DATA_MAX6691 0
77 #endif
78
79 #ifndef HAS_SENSOR_ERRORS
80 #if HAS_IMU || HAS_MMA655X || HAS_MS5607 || HAS_MS5611
81 #define HAS_SENSOR_ERRORS       1
82 #endif
83 #endif
84
85 #if HAS_SENSOR_ERRORS
86 extern uint8_t                  ao_sensor_errors;
87 #endif
88
89 #ifdef AO_DATA_RING
90
91 #define AO_DATA_ALL     (AO_DATA_ADC|AO_DATA_MS5607|AO_DATA_MPU6000|AO_DATA_HMC5883|AO_DATA_MMA655X|AO_DATA_MPU9250|AO_DATA_ADXL375)
92
93 struct ao_data {
94         uint16_t                        tick;
95 #if HAS_ADC
96         struct ao_adc                   adc;
97 #endif
98 #if HAS_MS5607
99         struct ao_ms5607_sample         ms5607_raw;
100         struct ao_ms5607_value          ms5607_cooked;
101 #endif
102 #if HAS_MPU6000
103         struct ao_mpu6000_sample        mpu6000;
104 #if !HAS_MMA655X
105         int16_t                         z_accel;
106 #endif
107 #endif
108 #if HAS_MPU9250
109         struct ao_mpu9250_sample        mpu9250;
110 #endif
111 #if HAS_HMC5883
112         struct ao_hmc5883_sample        hmc5883;
113 #endif
114 #if HAS_MMA655X
115         uint16_t                        mma655x;
116 #endif
117 #if HAS_ADXL375
118         struct ao_adxl375_sample        adxl375;
119 #endif
120 #if HAS_MAX6691
121         struct ao_max6691_sample        max6691;
122 #endif
123 #if HAS_ADS131A0X
124         struct ao_ads131a0x_sample      ads131a0x;
125 #endif
126 };
127
128 #define ao_data_ring_next(n)    (((n) + 1) & (AO_DATA_RING - 1))
129 #define ao_data_ring_prev(n)    (((n) - 1) & (AO_DATA_RING - 1))
130
131 /* Get a copy of the last complete sample set */
132 void
133 ao_data_get(struct ao_data *packet);
134
135 extern volatile struct ao_data  ao_data_ring[AO_DATA_RING];
136 extern volatile uint8_t         ao_data_head;
137 extern volatile uint8_t         ao_data_present;
138 extern volatile uint8_t         ao_data_count;
139
140 /*
141  * Mark a section of data as ready, check for data complete
142  */
143 #define AO_DATA_PRESENT(bit)    (ao_data_present |= (bit))
144
145 /*
146  * Wait until it is time to write a sensor sample; this is
147  * signaled by the timer tick
148  */
149 #define AO_DATA_WAIT()          ao_sleep((void *) &ao_data_count)
150
151 #endif /* AO_DATA_RING */
152
153 #if !HAS_BARO && HAS_MS5607
154
155 /* Either an MS5607 or an MS5611 hooked to a SPI port
156  */
157
158 #define HAS_BARO        1
159
160 typedef int32_t pres_t;
161
162 #define AO_ALT_TYPE     int32_t
163
164 typedef AO_ALT_TYPE     alt_t;
165
166 #define ao_data_pres_cook(packet)       ao_ms5607_convert(&packet->ms5607_raw, &packet->ms5607_cooked)
167
168 #define ao_data_pres(packet)    ((packet)->ms5607_cooked.pres)
169 #define ao_data_temp(packet)    ((packet)->ms5607_cooked.temp)
170
171 #define pres_to_altitude(p)     ao_pa_to_altitude(p)
172
173 #endif
174
175 #if !HAS_BARO && HAS_ADC
176
177 #define HAS_BARO        1
178
179 typedef int16_t pres_t;
180 typedef int16_t alt_t;
181
182 #define ao_data_pres(packet)    ((packet)->adc.pres)
183 #define ao_data_temp(packet)    ((packet)->adc.temp)
184 #define pres_to_altitude(p)     ao_pres_to_altitude(p)
185 #define ao_data_pres_cook(p)
186
187 #endif
188
189 /*
190  * Need a few macros to pull data from the sensors:
191  *
192  * ao_data_accel_sample - pull raw sensor and convert to normalized values
193  * ao_data_accel        - pull normalized value (lives in the same memory)
194  * ao_data_set_accel    - store normalized value back in the sensor location
195  * ao_data_accel_invert - flip rocket ends for positive acceleration
196  */
197
198 #if HAS_ACCEL
199
200 /* This section is for an analog accelerometer hooked to one of the ADC pins. As
201  * those are 5V parts, this also requires that the 5V supply be hooked to to anothe ADC
202  * pin so that the both can be measured to correct for changes between the 3.3V and 5V rails
203  */
204
205 typedef int16_t accel_t;
206 #define ao_data_accel(packet)                   ((packet)->adc.accel)
207 #define ao_data_set_accel(packet, a)            ((packet)->adc.accel = (a))
208 #define ao_data_accel_invert(a)                 (0x7fff -(a))
209
210 /*
211  * Ok, the math here is a bit tricky.
212  *
213  * ao_sample_accel:  ADC output for acceleration
214  * ao_accel_ref:  ADC output for the 5V reference.
215  * ao_cook_accel: Corrected acceleration value
216  * Vcc:           3.3V supply to the CC1111
217  * Vac:           5V supply to the accelerometer
218  * accel:         input voltage to accelerometer ADC pin
219  * ref:           input voltage to 5V reference ADC pin
220  *
221  *
222  * Measured acceleration is ratiometric to Vcc:
223  *
224  *     ao_sample_accel   accel
225  *     ------------ = -----
226  *        32767        Vcc
227  *
228  * Measured 5v reference is also ratiometric to Vcc:
229  *
230  *     ao_accel_ref    ref
231  *     ------------ = -----
232  *        32767        Vcc
233  *
234  *
235  *      ao_accel_ref = 32767 * (ref / Vcc)
236  *
237  * Acceleration is measured ratiometric to the 5V supply,
238  * so what we want is:
239  *
240  *      ao_cook_accel    accel
241  *      ------------- =  -----
242  *          32767         ref
243  *
244  *
245  *                      accel    Vcc
246  *                    = ----- *  ---
247  *                       Vcc     ref
248  *
249  *                      ao_sample_accel       32767
250  *                    = ------------ *  ------------
251  *                         32767        ao_accel_ref
252  *
253  * Multiply through by 32767:
254  *
255  *                      ao_sample_accel * 32767
256  *      ao_cook_accel = --------------------
257  *                          ao_accel_ref
258  *
259  * Now, the tricky part. Getting this to compile efficiently
260  * and keeping all of the values in-range.
261  *
262  * First off, we need to use a shift of 16 instead of * 32767 as SDCC
263  * does the obvious optimizations for byte-granularity shifts:
264  *
265  *      ao_cook_accel = (ao_sample_accel << 16) / ao_accel_ref
266  *
267  * Next, lets check our input ranges:
268  *
269  *      0 <= ao_sample_accel <= 0x7fff          (singled ended ADC conversion)
270  *      0x7000 <= ao_accel_ref <= 0x7fff        (the 5V ref value is close to 0x7fff)
271  *
272  * Plugging in our input ranges, we get an output range of 0 - 0x12490,
273  * which is 17 bits. That won't work. If we take the accel ref and shift
274  * by a bit, we'll change its range:
275  *
276  *      0xe000 <= ao_accel_ref<<1 <= 0xfffe
277  *
278  *      ao_cook_accel = (ao_sample_accel << 16) / (ao_accel_ref << 1)
279  *
280  * Now the output range is 0 - 0x9248, which nicely fits in 16 bits. It
281  * is, however, one bit too large for our signed computations. So, we
282  * take the result and shift that by a bit:
283  *
284  *      ao_cook_accel = ((ao_sample_accel << 16) / (ao_accel_ref << 1)) >> 1
285  *
286  * This finally creates an output range of 0 - 0x4924. As the ADC only
287  * provides 11 bits of data, we haven't actually lost any precision,
288  * just dropped a bit of noise off the low end.
289  */
290
291 #if HAS_ACCEL_REF
292
293 #define ao_data_accel_cook(packet) \
294         ((uint16_t) ((((uint32_t) (packet)->adc.accel << 16) / ((packet)->adc.accel_ref << 1))) >> 1)
295
296 #else
297
298 #define ao_data_accel_cook(packet) ((packet)->adc.accel)
299
300 #endif /* HAS_ACCEL_REF */
301
302 #endif  /* HAS_ACCEL */
303
304 #if !HAS_ACCEL && HAS_MMA655X
305
306 #define HAS_ACCEL       1
307
308 typedef int16_t accel_t;
309
310 /* MMA655X is hooked up so that positive values represent negative acceleration */
311
312 #define AO_ACCEL_INVERT         4095
313
314 #ifndef AO_MMA655X_INVERT
315 #error AO_MMA655X_INVERT not defined
316 #endif
317
318 #define ao_data_accel(packet)                   ((packet)->mma655x)
319 #if AO_MMA655X_INVERT
320 #define ao_data_accel_cook(packet)              (AO_ACCEL_INVERT - (packet)->mma655x)
321 #else
322 #define ao_data_accel_cook(packet)              ((packet)->mma655x)
323 #endif
324 #define ao_data_set_accel(packet, accel)        ((packet)->mma655x = (accel))
325 #define ao_data_accel_invert(accel)             (AO_ACCEL_INVERT - (accel))
326
327 #endif
328
329 #if !HAS_ACCEL && HAS_ADXL375
330
331 #define HAS_ACCEL       1
332
333 typedef int16_t accel_t;
334
335 #ifndef AO_ADXL375_INVERT
336 #error AO_ADXL375_INVERT not defined
337 #endif
338
339 #define ao_data_accel(packet)                   ((packet)->adxl375.AO_ADXL375_AXIS)
340 #if AO_ADXL375_INVERT
341 #define ao_data_accel_cook(packet)              (-ao_data_accel(packet))
342 #else
343 #define ao_data_accel_cook(packet)              ao_data_accel(packet)
344 #endif
345 #define ao_data_set_accel(packet, accel)        (ao_data_accel(packet) = (accel))
346 #define ao_data_accel_invert(accel)             (-(accel))
347
348 #endif /* HAS_ADXL375 */
349
350 #if !HAS_ACCEL && HAS_MPU6000
351
352 #define HAS_ACCEL       1
353
354 typedef int16_t accel_t;
355
356 /* MPU6000 is hooked up so that positive y is positive acceleration */
357 #define ao_data_accel(packet)                   ((packet)->z_accel)
358 #define ao_data_accel_cook(packet)              (-(packet)->mpu6000.accel_y)
359 #define ao_data_set_accel(packet, accel)        ((packet)->z_accel = (accel))
360 #define ao_data_accel_invert(a)                 (-(a))
361
362 #endif
363
364 #if !HAS_GYRO && HAS_MPU6000
365
366 #define HAS_GYRO        1
367
368 typedef int16_t gyro_t;         /* in raw sample units */
369 typedef int16_t angle_t;        /* in degrees */
370
371 /* Y axis is aligned with the direction of motion (along) */
372 /* X axis is aligned in the other board axis (across) */
373 /* Z axis is aligned perpendicular to the board (through) */
374
375 #define ao_data_along(packet)   ((packet)->mpu6000.accel_y)
376 #define ao_data_across(packet)  ((packet)->mpu6000.accel_x)
377 #define ao_data_through(packet) ((packet)->mpu6000.accel_z)
378
379 #define ao_data_roll(packet)    ((packet)->mpu6000.gyro_y)
380 #define ao_data_pitch(packet)   ((packet)->mpu6000.gyro_x)
381 #define ao_data_yaw(packet)     ((packet)->mpu6000.gyro_z)
382
383 static inline float ao_convert_gyro(float sensor)
384 {
385         return ao_mpu6000_gyro(sensor);
386 }
387
388 static inline float ao_convert_accel(int16_t sensor)
389 {
390         return ao_mpu6000_accel(sensor);
391 }
392
393 #endif
394
395 #if !HAS_GYRO && HAS_MPU9250
396
397 #define HAS_GYRO        1
398
399 typedef int16_t gyro_t;         /* in raw sample units */
400 typedef int16_t angle_t;        /* in degrees */
401
402 /* Y axis is aligned with the direction of motion (along) */
403 /* X axis is aligned in the other board axis (across) */
404 /* Z axis is aligned perpendicular to the board (through) */
405
406 #ifndef ao_data_along
407 #define ao_data_along(packet)   ((packet)->mpu9250.accel_y)
408 #define ao_data_across(packet)  ((packet)->mpu9250.accel_x)
409 #define ao_data_through(packet) ((packet)->mpu9250.accel_z)
410
411 #define ao_data_roll(packet)    ((packet)->mpu9250.gyro_y)
412 #define ao_data_pitch(packet)   ((packet)->mpu9250.gyro_x)
413 #define ao_data_yaw(packet)     ((packet)->mpu9250.gyro_z)
414 #endif
415
416 static inline float ao_convert_gyro(float sensor)
417 {
418         return ao_mpu9250_gyro(sensor);
419 }
420
421 static inline float ao_convert_accel(int16_t sensor)
422 {
423         return ao_mpu9250_accel(sensor);
424 }
425
426 #endif
427
428 #if !HAS_MAG && HAS_HMC5883
429
430 #define HAS_MAG         1
431
432 typedef int16_t ao_mag_t;               /* in raw sample units */
433
434 #define ao_data_mag_along(packet)       ((packet)->hmc5883.x)
435 #define ao_data_mag_across(packet)      ((packet)->hmc5883.y)
436 #define ao_data_mag_through(packet)     ((packet)->hmc5883.z)
437
438 #endif
439
440 #if !HAS_MAG && HAS_MPU9250
441
442 #define HAS_MAG         1
443
444 typedef int16_t ao_mag_t;               /* in raw sample units */
445
446 /* Note that this order is different from the accel and gyro. For some
447  * reason, the mag sensor axes aren't the same as the other two
448  * sensors. Also, the Z axis is flipped in sign.
449  */
450
451 #ifndef ao_data_mag_along
452 #define ao_data_mag_along(packet)       ((packet)->mpu9250.mag_x)
453 #define ao_data_mag_across(packet)      ((packet)->mpu9250.mag_y)
454 #define ao_data_mag_through(packet)     ((packet)->mpu9250.mag_z)
455 #endif
456
457 #endif
458
459 #ifdef AO_DATA_RING
460
461 static inline void
462 ao_data_fill(int head) {
463         if (ao_data_present == AO_DATA_ALL) {
464 #if HAS_MS5607
465                 ao_data_ring[head].ms5607_raw = ao_ms5607_current;
466 #endif
467 #if HAS_MMA655X
468                 ao_data_ring[head].mma655x = ao_mma655x_current;
469 #endif
470 #if HAS_HMC5883
471                 ao_data_ring[head].hmc5883 = ao_hmc5883_current;
472 #endif
473 #if HAS_MPU6000
474                 ao_data_ring[head].mpu6000 = ao_mpu6000_current;
475 #endif
476 #if HAS_MPU9250
477                 ao_data_ring[head].mpu9250 = ao_mpu9250_current;
478 #endif
479 #if HAS_ADXL375
480                 ao_data_ring[head].adxl375 = ao_adxl375_current;
481 #endif
482 #if HAS_MAX6691
483                 ao_data_ring[head].max6691 = ao_max6691_current;
484 #endif
485 #if HAS_ADS131A0X
486                 ao_data_ring[head].ads131a0x = ao_ads131a0x_current;
487 #endif
488                 ao_data_ring[head].tick = ao_tick_count;
489                 ao_data_head = ao_data_ring_next(head);
490                 ao_wakeup((void *) &ao_data_head);
491         }
492 }
493
494 #endif
495
496 #endif /* _AO_DATA_H_ */