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.
18 #include <ao_ms5607.h>
34 ao_ms5607_convert(__xdata struct ao_ms5607_sample *sample,
35 __xdata struct ao_ms5607_value *value)
39 __LOCAL ao_int64_t OFF;
40 __LOCAL ao_int64_t SENS;
43 dT = sample->temp - ((int32_t) ao_ms5607_prom.tref << 8);
45 /* TEMP = 2000 + (((int64_t) dT * ao_ms5607_prom.tempsens) >> 23); */
46 ao_mul64_32_32(&a, dT, ao_ms5607_prom.tempsens);
47 ao_rshift64(&a, &a, 23);
51 /* OFF = ((int64_t) ao_ms5607_prom.off << SHIFT_OFF) + (((int64_t) ao_ms5607_prom.tco * dT) >> SHIFT_TCO);*/
53 OFF.high = ao_ms5607_prom.off >> (32 - SHIFT_OFF);
57 OFF.low = (uint32_t) ao_ms5607_prom.off << SHIFT_OFF;
58 ao_mul64_32_32(&a, ao_ms5607_prom.tco, dT);
59 ao_rshift64(&a, &a, SHIFT_TCO);
60 ao_plus64(&OFF, &OFF, &a);
63 /* SENS = ((int64_t) ao_ms5607_prom.sens << SHIFT_SENS) + (((int64_t) ao_ms5607_prom.tcs * dT) >> SHIFT_TCS); */
65 SENS.low = (uint32_t) ao_ms5607_prom.sens << SHIFT_SENS;
66 ao_mul64_32_32(&a, ao_ms5607_prom.tcs, dT);
67 ao_rshift64(&a, &a, SHIFT_TCS);
68 ao_plus64(&SENS, &SENS, &a);
73 __LOCAL int32_t TEMPM;
74 __LOCAL ao_int64_t OFF2;
75 __LOCAL ao_int64_t SENS2;
77 /* T2 = ((int64_t) dT * (int64_t) dT) >> 31; */
78 ao_mul64_32_32(&a, dT, dT);
79 T2 = (a.low >> 31) | (a.high << 1);
84 /* OFF2 = (61 * (int64_t) TEMPM * (int64_t) TEMPM) >> 4; */
85 ao_mul64_32_32(&OFF2, TEMPM, TEMPM);
86 ao_mul64_64_16(&OFF2, &OFF2, 61);
87 ao_rshift64(&OFF2, &OFF2, 4);
90 /* SENS2 = 2 * (int64_t) TEMPM * (int64_t) TEMPM; */
91 ao_mul64_32_32(&SENS2, TEMPM, TEMPM);
92 ao_lshift64(&SENS2, &SENS2, 1);
100 TEMPP2 = TEMPP * TEMPP;
102 /* OFF2 = OFF2 + 15 * TEMPP2; */
103 ao_mul64_32_32(&a, 15, TEMPP2);
104 ao_plus64(&OFF2, &OFF2, &a);
107 /* SENS2 = SENS2 + 8 * TEMPP2; */
110 ao_lshift64(&a, &a, 3);
111 ao_plus64(&SENS2, &SENS2, &a);
117 ao_minus64(&OFF, &OFF, &OFF2);
121 ao_minus64(&SENS, &SENS, &SENS2);
125 /* value->pres = ((((int64_t) sample->pres * SENS) >> 21) - OFF) >> 15; */
127 a.low = sample->pres;
128 ao_mul64(&a, &a, &SENS);
129 ao_rshift64(&a, &a, 21);
130 ao_minus64(&a, &a, &OFF);
131 ao_rshift64(&a, &a, 15);