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