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