2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
22 #include <ao_ms5607.h>
26 #include <ao_mpu6000.h>
35 struct ao_ms5607_sample ms5607;
38 struct ao_mpu6000_sample mpu6000;
42 #define ao_data_ring_next(n) (((n) + 1) & (AO_DATA_RING - 1))
43 #define ao_data_ring_prev(n) (((n) - 1) & (AO_DATA_RING - 1))
45 extern volatile __xdata struct ao_data ao_data_ring[AO_DATA_RING];
46 extern volatile __data uint8_t ao_data_head;
50 typedef int32_t pres_t;
51 typedef int32_t alt_t;
53 static inline pres_t ao_data_pres(struct ao_data *packet)
55 struct ao_ms5607_value value;
57 ao_ms5607_convert(&packet->ms5607, &value);
61 #define pres_to_altitude(p) ao_pa_to_altitude(p)
65 typedef int16_t pres_t;
66 typedef int16_t alt_t;
68 #define ao_data_pres(packet) ((packet)->adc.pres)
69 #define pres_to_altitude(p) ao_pres_to_altitude(p)
74 * Need a few macros to pull data from the sensors:
76 * ao_data_accel_sample - pull raw sensor and convert to normalized values
77 * ao_data_accel - pull normalized value (lives in the same memory)
78 * ao_data_set_accel - store normalized value back in the sensor location
79 * ao_data_accel_invert - flip rocket ends for positive acceleration
82 #if HAS_MPU6000 && !HAS_HIGHG_ACCEL
84 typedef int16_t accel_t;
86 /* MPU6000 is hooked up so that positive y is positive acceleration */
87 #define ao_data_accel(packet) ((packet)->mpu6000.accel_y)
88 #define ao_data_accel_sample(packet) (-ao_data_accel(packet))
89 #define ao_data_set_accel(packet, accel) ((packet)->mpu6000.accel_y = (accel))
90 #define ao_data_accel_invert(a) (-(a))
94 typedef int16_t accel_t;
95 #define ao_data_accel(packet) ((packet)->adc.accel)
96 #define ao_data_set_accel(packet, a) ((packet)->adc.accel = (a))
97 #define ao_data_accel_invert(a) (0x7fff -(a))
100 * Ok, the math here is a bit tricky.
102 * ao_sample_accel: ADC output for acceleration
103 * ao_accel_ref: ADC output for the 5V reference.
104 * ao_cook_accel: Corrected acceleration value
105 * Vcc: 3.3V supply to the CC1111
106 * Vac: 5V supply to the accelerometer
107 * accel: input voltage to accelerometer ADC pin
108 * ref: input voltage to 5V reference ADC pin
111 * Measured acceleration is ratiometric to Vcc:
113 * ao_sample_accel accel
114 * ------------ = -----
117 * Measured 5v reference is also ratiometric to Vcc:
120 * ------------ = -----
124 * ao_accel_ref = 32767 * (ref / Vcc)
126 * Acceleration is measured ratiometric to the 5V supply,
127 * so what we want is:
129 * ao_cook_accel accel
130 * ------------- = -----
138 * ao_sample_accel 32767
139 * = ------------ * ------------
142 * Multiply through by 32767:
144 * ao_sample_accel * 32767
145 * ao_cook_accel = --------------------
148 * Now, the tricky part. Getting this to compile efficiently
149 * and keeping all of the values in-range.
151 * First off, we need to use a shift of 16 instead of * 32767 as SDCC
152 * does the obvious optimizations for byte-granularity shifts:
154 * ao_cook_accel = (ao_sample_accel << 16) / ao_accel_ref
156 * Next, lets check our input ranges:
158 * 0 <= ao_sample_accel <= 0x7fff (singled ended ADC conversion)
159 * 0x7000 <= ao_accel_ref <= 0x7fff (the 5V ref value is close to 0x7fff)
161 * Plugging in our input ranges, we get an output range of 0 - 0x12490,
162 * which is 17 bits. That won't work. If we take the accel ref and shift
163 * by a bit, we'll change its range:
165 * 0xe000 <= ao_accel_ref<<1 <= 0xfffe
167 * ao_cook_accel = (ao_sample_accel << 16) / (ao_accel_ref << 1)
169 * Now the output range is 0 - 0x9248, which nicely fits in 16 bits. It
170 * is, however, one bit too large for our signed computations. So, we
171 * take the result and shift that by a bit:
173 * ao_cook_accel = ((ao_sample_accel << 16) / (ao_accel_ref << 1)) >> 1
175 * This finally creates an output range of 0 - 0x4924. As the ADC only
176 * provides 11 bits of data, we haven't actually lost any precision,
177 * just dropped a bit of noise off the low end.
180 #define ao_data_accel_sample(packet) \
181 ((uint16_t) ((((uint32_t) (packet)->adc.accel << 16) / ((packet)->adc.accel_ref << 1))) >> 1)
183 #define ao_data_accel_sample(packet) ((packet)->adc.accel)
184 #endif /* HAS_ACCEL_REF */
186 #endif /* else some other pressure sensor */
188 #endif /* _AO_DATA_H_ */