f47972c98d2adc950b5bceec39f4411d1e650524
[fw/altos] / src / drivers / ao_ms5607_convert_8051.c
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 #include <ao_ms5607.h>
19
20 void
21 ao_ms5607_convert(struct ao_ms5607_sample *sample, struct ao_ms5607_value *value)
22 {
23 #if 0
24         int32_t dT;
25         int32_t TEMP;
26         int64_t OFF;
27         int64_t SENS;
28
29         dT = sample->temp - ((int32_t) ms5607_prom.tref << 8);
30         
31         TEMP = 2000 + (((int64_t) dT * ms5607_prom.tempsens) >> 23);
32
33 #if HAS_MS5611
34         OFF = ((int64_t) ms5607_prom.off << 16) + (((int64_t) ms5607_prom.tco * dT) >> 7);
35         SENS = ((int64_t) ms5607_prom.sens << 15) + (((int64_t) ms5607_prom.tcs * dT) >> 8);
36 #else
37         OFF = ((int64_t) ms5607_prom.off << 17) + (((int64_t) ms5607_prom.tco * dT) >> 6);
38         SENS = ((int64_t) ms5607_prom.sens << 16) + (((int64_t) ms5607_prom.tcs * dT) >> 7);
39 #endif
40
41         if (TEMP < 2000) {
42                 int32_t T2 = ((int64_t) dT * (int64_t) dT) >> 31;
43                 int32_t TEMPM = TEMP - 2000;
44                 int64_t OFF2 = (61 * (int64_t) TEMPM * (int64_t) TEMPM) >> 4;
45                 int64_t SENS2 = 2 * (int64_t) TEMPM * (int64_t) TEMPM;
46                 if (TEMP < 1500) {
47                         int32_t TEMPP = TEMP + 1500;
48                         int64_t TEMPP2 = TEMPP * TEMPP;
49                         OFF2 = OFF2 + 15 * TEMPP2;
50                         SENS2 = SENS2 + 8 * TEMPP2;
51                 }
52                 TEMP -= T2;
53                 OFF -= OFF2;
54                 SENS -= SENS2;
55         }
56
57         value->pres = ((((int64_t) sample->pres * SENS) >> 21) - OFF) >> 15;
58         value->temp = TEMP;
59 #endif
60 }