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