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