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