altos: Make telemetrum-v1.0 build with new ao_data structure
[fw/altos] / src / core / 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; 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 #ifndef _AO_DATA_H_
19 #define _AO_DATA_H_
20
21 #if HAS_MS5607
22 #include <ao_ms5607.h>
23 #endif
24
25 #if HAS_MPU6000
26 #include <ao_mpu6000.h>
27 #endif
28
29 struct ao_data {
30         uint16_t                        tick;
31 #if HAS_ADC
32         struct ao_adc                   adc;
33 #endif
34 #if HAS_ACCEL_REF
35         uint16_t                        accel_ref;
36 #endif
37 #if HAS_MS5607
38         struct ao_ms5607_sample         ms5607;
39 #endif
40 #if HAS_MPU6000
41         struct ao_mpu6000_sample        mpu6000;
42 #endif
43 };
44
45 #define ao_data_ring_next(n)    (((n) + 1) & (AO_DATA_RING - 1))
46 #define ao_data_ring_prev(n)    (((n) - 1) & (AO_DATA_RING - 1))
47
48 extern volatile __xdata struct ao_data  ao_data_ring[AO_DATA_RING];
49 extern volatile __data uint8_t          ao_data_head;
50
51 #if HAS_MS5607
52
53 typedef int32_t pres_t;
54 typedef int32_t alt_t;
55
56 static inline pres_t ao_data_pres(struct ao_data *packet)
57 {
58         struct ao_ms5607_value  value;
59
60         ao_ms5607_convert(&packet->ms5607, &value);
61         return value.pres;
62 }
63
64 #define pres_to_altitude(p)     ao_pa_to_altitude(p)
65
66 #else
67
68 typedef int16_t pres_t;
69 typedef int16_t alt_t;
70
71 #define ao_data_pres(packet)    ((packet)->adc.pres)
72 #define pres_to_altitude(p)     ao_pres_to_altitude(p)
73
74 #endif
75
76 /*
77  * Need a few macros to pull data from the sensors:
78  *
79  * ao_data_accel_sample - pull raw sensor and convert to normalized values
80  * ao_data_accel        - pull normalized value (lives in the same memory)
81  * ao_data_set_accel    - store normalized value back in the sensor location
82  * ao_data_accel_invert - flip rocket ends for positive acceleration
83  */
84
85 #if HAS_MPU6000
86
87 typedef int16_t accel_t;
88
89 /* MPU6000 is hooked up so that positive y is positive acceleration */
90 #define ao_data_accel(packet)                   ((packet)->mpu6000.accel_y)
91 #define ao_data_accel_sample(packet)            (-ao_data_accel(packet))
92 #define ao_data_set_accel(packet, accel)        ((packet)->mpu6000.accel_y = (accel))
93 #define ao_data_accel_invert(a)                 (-(a))
94
95 #else
96
97 typedef int16_t accel_t;
98 #define ao_data_accel(packet)                   ((packet)->adc.accel)
99 #define ao_data_set_accel(packet, a)            ((packet)->adc.accel = (a))
100 #define ao_data_accel_invert(a)                 (0x7fff -(a))
101
102 /*
103  * Ok, the math here is a bit tricky.
104  *
105  * ao_sample_accel:  ADC output for acceleration
106  * ao_accel_ref:  ADC output for the 5V reference.
107  * ao_cook_accel: Corrected acceleration value
108  * Vcc:           3.3V supply to the CC1111
109  * Vac:           5V supply to the accelerometer
110  * accel:         input voltage to accelerometer ADC pin
111  * ref:           input voltage to 5V reference ADC pin
112  *
113  *
114  * Measured acceleration is ratiometric to Vcc:
115  *
116  *     ao_sample_accel   accel
117  *     ------------ = -----
118  *        32767        Vcc
119  *
120  * Measured 5v reference is also ratiometric to Vcc:
121  *
122  *     ao_accel_ref    ref
123  *     ------------ = -----
124  *        32767        Vcc
125  *
126  *
127  *      ao_accel_ref = 32767 * (ref / Vcc)
128  *
129  * Acceleration is measured ratiometric to the 5V supply,
130  * so what we want is:
131  *
132  *      ao_cook_accel    accel
133  *      ------------- =  -----
134  *          32767         ref
135  *
136  *
137  *                      accel    Vcc
138  *                    = ----- *  ---
139  *                       Vcc     ref
140  *
141  *                      ao_sample_accel       32767
142  *                    = ------------ *  ------------
143  *                         32767        ao_accel_ref
144  *
145  * Multiply through by 32767:
146  *
147  *                      ao_sample_accel * 32767
148  *      ao_cook_accel = --------------------
149  *                          ao_accel_ref
150  *
151  * Now, the tricky part. Getting this to compile efficiently
152  * and keeping all of the values in-range.
153  *
154  * First off, we need to use a shift of 16 instead of * 32767 as SDCC
155  * does the obvious optimizations for byte-granularity shifts:
156  *
157  *      ao_cook_accel = (ao_sample_accel << 16) / ao_accel_ref
158  *
159  * Next, lets check our input ranges:
160  *
161  *      0 <= ao_sample_accel <= 0x7fff          (singled ended ADC conversion)
162  *      0x7000 <= ao_accel_ref <= 0x7fff        (the 5V ref value is close to 0x7fff)
163  *
164  * Plugging in our input ranges, we get an output range of 0 - 0x12490,
165  * which is 17 bits. That won't work. If we take the accel ref and shift
166  * by a bit, we'll change its range:
167  *
168  *      0xe000 <= ao_accel_ref<<1 <= 0xfffe
169  *
170  *      ao_cook_accel = (ao_sample_accel << 16) / (ao_accel_ref << 1)
171  *
172  * Now the output range is 0 - 0x9248, which nicely fits in 16 bits. It
173  * is, however, one bit too large for our signed computations. So, we
174  * take the result and shift that by a bit:
175  *
176  *      ao_cook_accel = ((ao_sample_accel << 16) / (ao_accel_ref << 1)) >> 1
177  *
178  * This finally creates an output range of 0 - 0x4924. As the ADC only
179  * provides 11 bits of data, we haven't actually lost any precision,
180  * just dropped a bit of noise off the low end.
181  */
182 #if HAS_ACCEL_REF
183 #define ao_data_accel_sample(packet) \
184         ((uint16_t) ((((uint32_t) (packet)->adc.accel << 16) / ((packet)->accel_ref << 1))) >> 1)
185 #else
186 #define ao_data_accel_sample(packet) ((packet)->adc.accel)
187 #endif /* HAS_ACCEL_REF */
188
189 #endif  /* else some other pressure sensor */
190
191 #endif /* _AO_DATA_H_ */