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 #define AO_DATA_ADC (1 << 0)
28 #include <ao_ms5607.h>
29 #define AO_DATA_MS5607 (1 << 1)
31 #define AO_DATA_MS5607 (1 << 1)
35 #include <ao_mpu6000.h>
36 #define AO_DATA_MPU6000 (1 << 2)
38 #define AO_DATA_MPU6000 0
42 #include <ao_hmc5883.h>
43 #define AO_DATA_HMC5883 (1 << 3)
45 #define AO_DATA_HMC5883 0
49 #include <ao_mma655x.h>
50 #define AO_DATA_MMA655X (1 << 4)
52 #define AO_DATA_MMA655X 0
55 #define AO_DATA_ALL (AO_DATA_ADC|AO_DATA_MS5607|AO_DATA_MPU6000|AO_DATA_HMC5883|AO_DATA_MMA655X)
63 struct ao_ms5607_sample ms5607_raw;
64 struct ao_ms5607_value ms5607_cooked;
67 struct ao_mpu6000_sample mpu6000;
70 struct ao_hmc5883_sample hmc5883;
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))
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;
86 * Mark a section of data as ready, check for data complete
88 #define AO_DATA_PRESENT(bit) do { \
89 if ((ao_data_present |= (bit)) == AO_DATA_ALL) { \
90 ao_data_ring[ao_data_head].tick = ao_tick_count; \
91 ao_data_head = ao_data_ring_next(ao_data_head); \
92 ao_data_present = 0; \
93 ao_wakeup((void *) &ao_data_head); \
98 * Wait for data to be completed by looking at the
101 #define AO_DATA_WAIT() do { \
102 ao_sleep((void *) &ao_data_count); \
108 typedef int32_t pres_t;
109 typedef int32_t alt_t;
111 #define ao_data_pres_cook(packet) ao_ms5607_convert(&packet->ms5607_raw, &packet->ms5607_cooked)
113 #define ao_data_pres(packet) ((packet)->ms5607_cooked.pres)
114 #define ao_data_temp(packet) ((packet)->ms5607_cooked.temp)
116 #define pres_to_altitude(p) ao_pa_to_altitude(p)
118 #else /* HAS_MS5607 */
120 typedef int16_t pres_t;
121 typedef int16_t alt_t;
123 #define ao_data_pres(packet) ((packet)->adc.pres)
124 #define ao_data_temp(packet) ((packet)->adc.temp)
125 #define pres_to_altitude(p) ao_pres_to_altitude(p)
126 #define ao_data_pres_cook(p)
128 #endif /* else HAS_MS5607 */
131 * Need a few macros to pull data from the sensors:
133 * ao_data_accel_sample - pull raw sensor and convert to normalized values
134 * ao_data_accel - pull normalized value (lives in the same memory)
135 * ao_data_set_accel - store normalized value back in the sensor location
136 * ao_data_accel_invert - flip rocket ends for positive acceleration
139 #if HAS_MPU6000 && !HAS_HIGHG_ACCEL
141 typedef int16_t accel_t;
143 /* MPU6000 is hooked up so that positive y is positive acceleration */
144 #define ao_data_accel(packet) ((packet)->mpu6000.accel_y)
145 #define ao_data_accel_cook(packet) (-(packet)->mpu6000.accel_y)
146 #define ao_data_set_accel(packet, accel) ((packet)->mpu6000.accel_y = (accel))
147 #define ao_data_accel_invert(a) (-(a))
149 #else /* HAS_MPU6000 && !HAS_HIGHG_ACCEL */
151 typedef int16_t accel_t;
152 #define ao_data_accel(packet) ((packet)->adc.accel)
153 #define ao_data_set_accel(packet, a) ((packet)->adc.accel = (a))
154 #define ao_data_accel_invert(a) (0x7fff -(a))
157 * Ok, the math here is a bit tricky.
159 * ao_sample_accel: ADC output for acceleration
160 * ao_accel_ref: ADC output for the 5V reference.
161 * ao_cook_accel: Corrected acceleration value
162 * Vcc: 3.3V supply to the CC1111
163 * Vac: 5V supply to the accelerometer
164 * accel: input voltage to accelerometer ADC pin
165 * ref: input voltage to 5V reference ADC pin
168 * Measured acceleration is ratiometric to Vcc:
170 * ao_sample_accel accel
171 * ------------ = -----
174 * Measured 5v reference is also ratiometric to Vcc:
177 * ------------ = -----
181 * ao_accel_ref = 32767 * (ref / Vcc)
183 * Acceleration is measured ratiometric to the 5V supply,
184 * so what we want is:
186 * ao_cook_accel accel
187 * ------------- = -----
195 * ao_sample_accel 32767
196 * = ------------ * ------------
199 * Multiply through by 32767:
201 * ao_sample_accel * 32767
202 * ao_cook_accel = --------------------
205 * Now, the tricky part. Getting this to compile efficiently
206 * and keeping all of the values in-range.
208 * First off, we need to use a shift of 16 instead of * 32767 as SDCC
209 * does the obvious optimizations for byte-granularity shifts:
211 * ao_cook_accel = (ao_sample_accel << 16) / ao_accel_ref
213 * Next, lets check our input ranges:
215 * 0 <= ao_sample_accel <= 0x7fff (singled ended ADC conversion)
216 * 0x7000 <= ao_accel_ref <= 0x7fff (the 5V ref value is close to 0x7fff)
218 * Plugging in our input ranges, we get an output range of 0 - 0x12490,
219 * which is 17 bits. That won't work. If we take the accel ref and shift
220 * by a bit, we'll change its range:
222 * 0xe000 <= ao_accel_ref<<1 <= 0xfffe
224 * ao_cook_accel = (ao_sample_accel << 16) / (ao_accel_ref << 1)
226 * Now the output range is 0 - 0x9248, which nicely fits in 16 bits. It
227 * is, however, one bit too large for our signed computations. So, we
228 * take the result and shift that by a bit:
230 * ao_cook_accel = ((ao_sample_accel << 16) / (ao_accel_ref << 1)) >> 1
232 * This finally creates an output range of 0 - 0x4924. As the ADC only
233 * provides 11 bits of data, we haven't actually lost any precision,
234 * just dropped a bit of noise off the low end.
239 #define ao_data_accel_cook(packet) \
240 ((uint16_t) ((((uint32_t) (packet)->adc.accel << 16) / ((packet)->adc.accel_ref << 1))) >> 1)
244 #define ao_data_accel_cook(packet) ((packet)->adc.accel)
246 #endif /* HAS_ACCEL_REF */
248 #endif /* else some other accel sensor */
250 #endif /* _AO_DATA_H_ */