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