altos/test: Adjust CRC error rate after FEC fix
[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_MMC5983
59 #include <ao_mmc5983.h>
60 #define AO_DATA_MMC5983 (1 << 3)
61 #else
62 #define AO_DATA_MMC5983 0
63 #endif
64
65 #if HAS_MMA655X
66 #include <ao_mma655x.h>
67 #define AO_DATA_MMA655X (1 << 4)
68 #else
69 #define AO_DATA_MMA655X 0
70 #endif
71
72 #if HAS_ADXL375
73 #include <ao_adxl375.h>
74 #define AO_DATA_ADXL375 (1 << 4)
75 #else
76 #define AO_DATA_ADXL375 0
77 #endif
78
79 #if HAS_MAX6691
80 #include <ao_max6691.h>
81 #define AO_DATA_MAX6691 (1 << 4)
82 #else
83 #define AO_DATA_MAX6691 0
84 #endif
85
86 #if HAS_BMX160
87 #include <ao_bmx160.h>
88 #define AO_DATA_BMX160  (1 << 2)
89 #else
90 #define AO_DATA_BMX160  0
91 #endif
92
93 #if HAS_BMI088
94 #include <ao_bmi088.h>
95 #define AO_DATA_BMI088 (1 << 2)
96 #else
97 #define AO_DATA_BMI088  0
98 #endif
99
100 #ifndef HAS_SENSOR_ERRORS
101 #if HAS_IMU || HAS_MMA655X || HAS_MS5607 || HAS_MS5611
102 #define HAS_SENSOR_ERRORS       1
103 #endif
104 #endif
105
106 #if HAS_SENSOR_ERRORS
107 extern uint8_t                  ao_sensor_errors;
108 #endif
109
110 #ifdef AO_DATA_RING
111
112 #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|AO_DATA_BMX160|AO_DATA_MMC5983|AO_DATA_BMI088)
113
114 struct ao_data {
115         AO_TICK_TYPE                    tick;
116 #if HAS_ADC
117         struct ao_adc                   adc;
118 #endif
119 #if HAS_MS5607
120         struct ao_ms5607_sample         ms5607_raw;
121         struct ao_ms5607_value          ms5607_cooked;
122 #endif
123 #if HAS_MPU6000
124         struct ao_mpu6000_sample        mpu6000;
125 #if !HAS_MMA655X
126         int16_t                         z_accel;
127 #endif
128 #endif
129 #if HAS_MPU9250
130         struct ao_mpu9250_sample        mpu9250;
131 #if !HAS_MMA655X
132         int16_t                         z_accel;
133 #endif
134 #endif
135 #if HAS_HMC5883
136         struct ao_hmc5883_sample        hmc5883;
137 #endif
138 #if HAS_MMC5983
139         struct ao_mmc5983_sample        mmc5983;
140 #endif
141 #if HAS_MMA655X
142         uint16_t                        mma655x;
143 #endif
144 #if HAS_ADXL375
145         struct ao_adxl375_sample        adxl375;
146 #endif
147 #if HAS_MAX6691
148         struct ao_max6691_sample        max6691;
149 #endif
150 #if HAS_ADS131A0X
151         struct ao_ads131a0x_sample      ads131a0x;
152 #endif
153 #if HAS_BMX160
154         struct ao_bmx160_sample         bmx160;
155 #if !HAS_ADXL375
156         int16_t z_accel;
157 #endif
158 #endif
159 #if HAS_BMI088
160         struct ao_bmi088_sample         bmi088;
161 #if !HAS_ADXL375
162         int16_t z_accel;
163 #endif
164 #endif
165 };
166
167 #define ao_data_ring_next(n)    (((n) + 1) & (AO_DATA_RING - 1))
168 #define ao_data_ring_prev(n)    (((n) - 1) & (AO_DATA_RING - 1))
169
170 /* Get a copy of the last complete sample set */
171 void
172 ao_data_get(struct ao_data *packet);
173
174 extern volatile struct ao_data  ao_data_ring[AO_DATA_RING];
175 extern volatile uint8_t         ao_data_head;
176 extern volatile uint8_t         ao_data_present;
177 extern volatile uint8_t         ao_data_count;
178
179 /*
180  * Mark a section of data as ready, check for data complete
181  */
182 #define AO_DATA_PRESENT(bit)    (ao_data_present |= (bit))
183
184 /*
185  * Mark sensor failed, and unblock the sample collection code by
186  * marking the data as present
187  */
188 #define AO_SENSOR_ERROR(bit)    (ao_data_present |= (ao_sensor_errors |= (bit)))
189
190 /*
191  * Wait until it is time to write a sensor sample; this is
192  * signaled by the timer tick
193  */
194 #define AO_DATA_WAIT()          ao_sleep((void *) &ao_data_count)
195
196 #endif /* AO_DATA_RING */
197
198 #define AO_ALT_TYPE     int32_t
199
200 typedef AO_ALT_TYPE     alt_t;
201
202 #if !HAS_BARO && HAS_MS5607
203
204 /* Either an MS5607 or an MS5611 hooked to a SPI port
205  */
206
207 #define HAS_BARO        1
208
209 typedef int32_t pres_t;
210
211 #define ao_data_pres_cook(packet)       ao_ms5607_convert(&packet->ms5607_raw, &packet->ms5607_cooked)
212
213 #define ao_data_pres(packet)    ((packet)->ms5607_cooked.pres)
214 #define ao_data_temp(packet)    ((int16_t) (packet)->ms5607_cooked.temp)
215
216 #define pres_to_altitude(p)     ao_pa_to_altitude(p)
217
218 #endif
219
220 /*
221  * Need a few macros to pull data from the sensors:
222  *
223  * ao_data_accel_raw    - pull raw sensor
224  * ao_data_accel_invert - flip rocket ends for positive acceleration
225  */
226
227 #if HAS_ACCEL
228
229 /* This section is for an analog accelerometer hooked to one of the ADC pins. As
230  * those are 5V parts, this also requires that the 5V supply be hooked to to anothe ADC
231  * pin so that the both can be measured to correct for changes between the 3.3V and 5V rails
232  */
233
234 typedef int16_t accel_t;
235 #define ao_data_accel_raw(packet)               ((packet)->adc.accel)
236 #define ao_data_accel_invert(a)                 (0x7fff -(a))
237
238 /*
239  * Ok, the math here is a bit tricky.
240  *
241  * ao_sample_accel:  ADC output for acceleration
242  * ao_accel_ref:  ADC output for the 5V reference.
243  * ao_cook_accel: Corrected acceleration value
244  * Vcc:           3.3V supply to the CC1111
245  * Vac:           5V supply to the accelerometer
246  * accel:         input voltage to accelerometer ADC pin
247  * ref:           input voltage to 5V reference ADC pin
248  *
249  *
250  * Measured acceleration is ratiometric to Vcc:
251  *
252  *     ao_sample_accel   accel
253  *     ------------ = -----
254  *        32767        Vcc
255  *
256  * Measured 5v reference is also ratiometric to Vcc:
257  *
258  *     ao_accel_ref    ref
259  *     ------------ = -----
260  *        32767        Vcc
261  *
262  *
263  *      ao_accel_ref = 32767 * (ref / Vcc)
264  *
265  * Acceleration is measured ratiometric to the 5V supply,
266  * so what we want is:
267  *
268  *      ao_cook_accel    accel
269  *      ------------- =  -----
270  *          32767         ref
271  *
272  *
273  *                      accel    Vcc
274  *                    = ----- *  ---
275  *                       Vcc     ref
276  *
277  *                      ao_sample_accel       32767
278  *                    = ------------ *  ------------
279  *                         32767        ao_accel_ref
280  *
281  * Multiply through by 32767:
282  *
283  *                      ao_sample_accel * 32767
284  *      ao_cook_accel = --------------------
285  *                          ao_accel_ref
286  *
287  * Now, the tricky part. Getting this to compile efficiently
288  * and keeping all of the values in-range.
289  *
290  * First off, we need to use a shift of 16 instead of * 32767 as SDCC
291  * does the obvious optimizations for byte-granularity shifts:
292  *
293  *      ao_cook_accel = (ao_sample_accel << 16) / ao_accel_ref
294  *
295  * Next, lets check our input ranges:
296  *
297  *      0 <= ao_sample_accel <= 0x7fff          (singled ended ADC conversion)
298  *      0x7000 <= ao_accel_ref <= 0x7fff        (the 5V ref value is close to 0x7fff)
299  *
300  * Plugging in our input ranges, we get an output range of 0 - 0x12490,
301  * which is 17 bits. That won't work. If we take the accel ref and shift
302  * by a bit, we'll change its range:
303  *
304  *      0xe000 <= ao_accel_ref<<1 <= 0xfffe
305  *
306  *      ao_cook_accel = (ao_sample_accel << 16) / (ao_accel_ref << 1)
307  *
308  * Now the output range is 0 - 0x9248, which nicely fits in 16 bits. It
309  * is, however, one bit too large for our signed computations. So, we
310  * take the result and shift that by a bit:
311  *
312  *      ao_cook_accel = ((ao_sample_accel << 16) / (ao_accel_ref << 1)) >> 1
313  *
314  * This finally creates an output range of 0 - 0x4924. As the ADC only
315  * provides 11 bits of data, we haven't actually lost any precision,
316  * just dropped a bit of noise off the low end.
317  */
318
319 #if HAS_ACCEL_REF
320
321 #define ao_data_accel_raw(packet) \
322         ((uint16_t) ((((uint32_t) (packet)->adc.accel << 16) / ((packet)->adc.accel_ref << 1))) >> 1)
323
324 #else
325
326 #define ao_data_accel_raw(packet) ((packet)->adc.accel)
327
328 #endif /* HAS_ACCEL_REF */
329
330 #endif  /* HAS_ACCEL */
331
332 #if !HAS_ACCEL && HAS_MMA655X
333
334 #define HAS_ACCEL       1
335
336 typedef int16_t accel_t;
337
338 /* MMA655X is hooked up so that positive values represent negative acceleration */
339
340 #define AO_ACCEL_INVERT         4095
341
342 #ifndef AO_MMA655X_INVERT
343 #error AO_MMA655X_INVERT not defined
344 #endif
345
346 #if AO_MMA655X_INVERT
347 #define ao_data_accel_raw(packet)               ((accel_t) (AO_ACCEL_INVERT - (packet)->mma655x))
348 #else
349 #define ao_data_accel_raw(packet)               ((accel_t) (packet)->mma655x)
350 #endif
351 #define ao_data_accel_invert(accel)             (AO_ACCEL_INVERT - (accel))
352
353 #endif
354
355 #if !HAS_ACCEL && HAS_ADXL375
356
357 #define HAS_ACCEL       1
358
359 typedef int16_t accel_t;
360
361 #ifndef AO_ADXL375_INVERT
362 #error AO_ADXL375_INVERT not defined
363 #endif
364
365 #if AO_ADXL375_INVERT
366 #define ao_data_accel_raw(packet)               (-(packet)->adxl375.AO_ADXL375_AXIS)
367 #else
368 #define ao_data_accel_raw(packet)               ((packet)->adxl375.AO_ADXL375_AXIS)
369 #endif
370 #define ao_data_accel_invert(accel)             (-(accel))
371
372 #if USE_ADXL375_IMU
373 #define ao_data_along(packet)                   ((packet)->adxl375.AO_ADXL375_AXIS)
374 #define ao_data_across(packet)                  ((packet)->adxl375.AO_ADXL375_ACROSS_AXIS)
375 #define ao_data_through(packet)                 ((packet)->adxl375.z)
376 #define ao_data_accel_to_sample(accel)          ao_adxl375_accel_to_sample(accel)
377 #endif
378
379 #endif /* HAS_ADXL375 */
380
381 #if !HAS_ACCEL && HAS_MPU6000
382
383 #define HAS_ACCEL       1
384
385 typedef int16_t accel_t;
386
387 /* MPU6000 is hooked up so that positive y is positive acceleration */
388 #define ao_data_accel_raw(packet)               (-(packet)->mpu6000.accel_y)
389 #define ao_data_accel_invert(a)                 (-(a))
390
391 #endif
392
393 #if !HAS_GYRO && HAS_MPU6000
394
395 #define HAS_GYRO        1
396
397 typedef int16_t gyro_t;         /* in raw sample units */
398 typedef int16_t angle_t;        /* in degrees */
399
400 /* Y axis is aligned with the direction of motion (along) */
401 /* X axis is aligned in the other board axis (across) */
402 /* Z axis is aligned perpendicular to the board (through) */
403
404 #ifndef ao_data_along
405 #define ao_data_along(packet)   ((packet)->mpu6000.accel_y)
406 #define ao_data_across(packet)  ((packet)->mpu6000.accel_x)
407 #define ao_data_through(packet) ((packet)->mpu6000.accel_z)
408
409 #define ao_data_roll(packet)    ((packet)->mpu6000.gyro_y)
410 #define ao_data_pitch(packet)   ((packet)->mpu6000.gyro_x)
411 #define ao_data_yaw(packet)     ((packet)->mpu6000.gyro_z)
412 #endif
413
414 static inline float ao_convert_gyro(float sensor)
415 {
416         return ao_mpu6000_gyro(sensor);
417 }
418
419 static inline float ao_convert_accel(int16_t sensor)
420 {
421         return ao_mpu6000_accel(sensor);
422 }
423
424 #endif
425
426 #if !HAS_ACCEL && HAS_MPU9250
427
428 #define HAS_ACCEL       1
429
430 typedef int16_t accel_t;
431
432 /* MPU9250 is hooked up so that positive y is positive acceleration */
433 #define ao_data_accel_raw(packet)               (-(packet)->mpu9250.accel_y)
434 #define ao_data_accel_invert(a)                 (-(a))
435
436 #endif
437
438 #if !HAS_GYRO && HAS_MPU9250
439
440 #define HAS_GYRO        1
441
442 typedef int16_t gyro_t;         /* in raw sample units */
443 typedef int16_t angle_t;        /* in degrees */
444
445 /* Y axis is aligned with the direction of motion (along) */
446 /* X axis is aligned in the other board axis (across) */
447 /* Z axis is aligned perpendicular to the board (through) */
448
449 #ifndef ao_data_along
450 #define ao_data_along(packet)   ((packet)->mpu9250.accel_y)
451 #define ao_data_across(packet)  ((packet)->mpu9250.accel_x)
452 #define ao_data_through(packet) ((packet)->mpu9250.accel_z)
453
454 #define ao_data_roll(packet)    ((packet)->mpu9250.gyro_y)
455 #define ao_data_pitch(packet)   ((packet)->mpu9250.gyro_x)
456 #define ao_data_yaw(packet)     ((packet)->mpu9250.gyro_z)
457 #endif
458
459 static inline float ao_convert_gyro(float sensor)
460 {
461         return ao_mpu9250_gyro(sensor);
462 }
463
464 static inline float ao_convert_accel(int16_t sensor)
465 {
466         return ao_mpu9250_accel(sensor);
467 }
468
469 #endif
470
471 #if !HAS_ACCEL && HAS_BMX160
472
473 #define HAS_ACCEL       1
474
475 typedef int16_t accel_t;
476
477 #define ao_data_accel_raw(packet)               -ao_data_along(packet)
478 #define ao_data_accel_invert(a)                 (-(a))
479 #define ao_data_accel_to_sample(accel)          ao_bmx_accel_to_sample(accel)
480
481 #endif
482
483 #if !HAS_GYRO && HAS_BMX160
484
485 #define HAS_GYRO        1
486
487 typedef int16_t gyro_t;         /* in raw sample units */
488 typedef int16_t angle_t;        /* in degrees */
489
490 /* X axis is aligned with the direction of motion (along) */
491 /* Y axis is aligned in the other board axis (across) */
492 /* Z axis is aligned perpendicular to the board (through) */
493
494 static inline float ao_convert_gyro(float sensor)
495 {
496         return ao_bmx160_gyro(sensor);
497 }
498
499 static inline float ao_convert_accel(int16_t sensor)
500 {
501         return ao_bmx160_accel(sensor);
502 }
503
504 #endif
505
506 #if !HAS_ACCEL && HAS_BMI088
507
508 #define HAS_ACCEL       1
509
510 typedef int16_t accel_t;
511
512 #define ao_data_accel_raw(packet)               -ao_data_along(packet)
513 #define ao_data_accel_invert(a)                 (-(a))
514 #define ao_data_accel_to_sample(accel)          ao_bmi_accel_to_sample(accel)
515
516 #endif
517
518 #if !HAS_GYRO && HAS_BMI088
519
520 #define HAS_GYRO        1
521
522 typedef int16_t gyro_t;         /* in raw sample units */
523 typedef int16_t angle_t;        /* in degrees */
524
525 /* X axis is aligned with the direction of motion (along) */
526 /* Y axis is aligned in the other board axis (across) */
527 /* Z axis is aligned perpendicular to the board (through) */
528
529 static inline float ao_convert_gyro(float sensor)
530 {
531         return ao_bmi088_gyro(sensor);
532 }
533
534 static inline float ao_convert_accel(int16_t sensor)
535 {
536         return ao_bmi088_accel(sensor);
537 }
538
539 #endif
540
541 #if !HAS_MAG && HAS_HMC5883
542
543 #define HAS_MAG         1
544
545 typedef int16_t ao_mag_t;               /* in raw sample units */
546
547 #define ao_data_mag_along(packet)       ((packet)->hmc5883.x)
548 #define ao_data_mag_across(packet)      ((packet)->hmc5883.y)
549 #define ao_data_mag_through(packet)     ((packet)->hmc5883.z)
550
551 #endif
552
553 #if !HAS_MAG && HAS_MMC5983
554
555 #define HAS_MAG         1
556
557 typedef int16_t ao_mag_t;               /* in raw sample units */
558
559 #endif
560
561 #if !HAS_MAG && HAS_MPU9250
562
563 #define HAS_MAG         1
564
565 typedef int16_t ao_mag_t;               /* in raw sample units */
566
567 /* Note that this order is different from the accel and gyro. For some
568  * reason, the mag sensor axes aren't the same as the other two
569  * sensors. Also, the Z axis is flipped in sign.
570  */
571
572 #ifndef ao_data_mag_along
573 #define ao_data_mag_along(packet)       ((packet)->mpu9250.mag_x)
574 #define ao_data_mag_across(packet)      ((packet)->mpu9250.mag_y)
575 #define ao_data_mag_through(packet)     ((packet)->mpu9250.mag_z)
576 #endif
577
578 #endif
579
580 #ifdef AO_DATA_RING
581
582 static inline void
583 ao_data_fill(int head) {
584         if (ao_data_present == AO_DATA_ALL) {
585 #if HAS_MS5607
586                 ao_data_ring[head].ms5607_raw = ao_ms5607_current;
587 #endif
588 #if HAS_MMA655X
589                 ao_data_ring[head].mma655x = ao_mma655x_current;
590 #endif
591 #if HAS_HMC5883
592                 ao_data_ring[head].hmc5883 = ao_hmc5883_current;
593 #endif
594 #if HAS_MMC5983
595                 ao_data_ring[head].mmc5983 = ao_mmc5983_current;
596 #endif
597 #if HAS_MPU6000
598                 ao_data_ring[head].mpu6000 = ao_mpu6000_current;
599 #endif
600 #if HAS_MPU9250
601                 ao_data_ring[head].mpu9250 = ao_mpu9250_current;
602 #endif
603 #if HAS_ADXL375
604                 ao_data_ring[head].adxl375 = ao_adxl375_current;
605 #endif
606 #if HAS_MAX6691
607                 ao_data_ring[head].max6691 = ao_max6691_current;
608 #endif
609 #if HAS_ADS131A0X
610                 ao_data_ring[head].ads131a0x = ao_ads131a0x_current;
611 #endif
612 #if HAS_BMX160
613                 ao_data_ring[head].bmx160 = ao_bmx160_current;
614 #endif
615 #if HAS_BMI088
616                 ao_data_ring[head].bmi088 = ao_bmi088_current;
617 #endif
618                 ao_data_ring[head].tick = ao_tick_count;
619                 ao_data_head = ao_data_ring_next(head);
620                 ao_wakeup((void *) &ao_data_head);
621         }
622 }
623
624 #endif
625
626 #if HAS_ACCEL
627 accel_t
628 ao_data_accel(volatile struct ao_data *packet);
629 #endif
630
631 #endif /* _AO_DATA_H_ */