Switch from GPLv2 to GPLv2+
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <ao_ms5607.h>
20 #include <ao_int64.h>
21
22 #if HAS_MS5611
23 #define SHIFT_OFF       16
24 #define SHIFT_TCO       7
25 #define SHIFT_SENS      15
26 #define SHFIT_TCS       8
27 #else
28 #define SHIFT_OFF       17
29 #define SHIFT_TCO       6
30 #define SHIFT_SENS      16
31 #define SHIFT_TCS       7
32 #endif
33
34 void
35 ao_ms5607_convert(__xdata struct ao_ms5607_sample *sample,
36                   __xdata struct ao_ms5607_value *value)
37 {
38         __LOCAL int32_t dT;
39         __LOCAL int32_t TEMP;
40         __LOCAL ao_int64_t OFF;
41         __LOCAL ao_int64_t SENS;
42         __LOCAL ao_int64_t a;
43
44         dT = sample->temp - ((int32_t) ao_ms5607_prom.tref << 8);
45         
46         /* TEMP = 2000 + (((int64_t) dT * ao_ms5607_prom.tempsens) >> 23); */
47         ao_mul64_32_32(&a, dT, ao_ms5607_prom.tempsens);
48         ao_rshift64(&a, &a, 23);
49         TEMP = 2000 + a.low;
50         /* */
51
52         /* OFF = ((int64_t) ao_ms5607_prom.off << SHIFT_OFF) + (((int64_t) ao_ms5607_prom.tco * dT) >> SHIFT_TCO);*/
53 #if SHIFT_OFF > 16
54         OFF.high = ao_ms5607_prom.off >> (32 - SHIFT_OFF);
55 #else
56         OFF.high = 0;
57 #endif
58         OFF.low = (uint32_t) ao_ms5607_prom.off << SHIFT_OFF;
59         ao_mul64_32_32(&a, ao_ms5607_prom.tco, dT);
60         ao_rshift64(&a, &a, SHIFT_TCO);
61         ao_plus64(&OFF, &OFF, &a);
62         /**/
63
64         /* SENS = ((int64_t) ao_ms5607_prom.sens << SHIFT_SENS) + (((int64_t) ao_ms5607_prom.tcs * dT) >> SHIFT_TCS); */
65         SENS.high = 0;
66         SENS.low = (uint32_t) ao_ms5607_prom.sens << SHIFT_SENS;
67         ao_mul64_32_32(&a, ao_ms5607_prom.tcs, dT);
68         ao_rshift64(&a, &a, SHIFT_TCS);
69         ao_plus64(&SENS, &SENS, &a);
70         /**/
71
72         if (TEMP < 2000) {
73                 __LOCAL int32_t T2;
74                 __LOCAL int32_t TEMPM;
75                 __LOCAL ao_int64_t OFF2;
76                 __LOCAL ao_int64_t SENS2;
77
78                 /* T2 = ((int64_t) dT * (int64_t) dT) >> 31; */
79                 ao_mul64_32_32(&a, dT, dT);
80                 T2 = (a.low >> 31) | (a.high << 1);
81                 /**/
82
83                 TEMPM = TEMP - 2000;
84
85                 /* OFF2 = (61 * (int64_t) TEMPM * (int64_t) TEMPM) >> 4; */
86                 ao_mul64_32_32(&OFF2, TEMPM, TEMPM);
87                 ao_mul64_64_16(&OFF2, &OFF2, 61);
88                 ao_rshift64(&OFF2, &OFF2, 4);
89                 /**/
90                 
91                 /* SENS2 = 2 * (int64_t) TEMPM * (int64_t) TEMPM; */
92                 ao_mul64_32_32(&SENS2, TEMPM, TEMPM);
93                 ao_lshift64(&SENS2, &SENS2, 1);
94                 /**/
95
96                 if (TEMP < -1500) {
97                         int32_t TEMPP;
98                         int32_t TEMPP2;
99
100                         TEMPP = TEMP + 1500;
101                         TEMPP2 = TEMPP * TEMPP;
102
103                         /* OFF2 = OFF2 + 15 * TEMPP2; */
104                         ao_mul64_32_32(&a, 15, TEMPP2);
105                         ao_plus64(&OFF2, &OFF2, &a);
106                         /**/
107
108                         /* SENS2 = SENS2 + 8 * TEMPP2; */
109                         a.high = 0;
110                         a.low = TEMPP2;
111                         ao_lshift64(&a, &a, 3);
112                         ao_plus64(&SENS2, &SENS2, &a);
113                         /**/
114                 }
115                 TEMP -= T2;
116
117                 /* OFF -= OFF2; */
118                 ao_minus64(&OFF, &OFF, &OFF2);
119                 /**/
120
121                 /* SENS -= SENS2; */
122                 ao_minus64(&SENS, &SENS, &SENS2);
123                 /**/
124         }
125
126         /* value->pres = ((((int64_t) sample->pres * SENS) >> 21) - OFF) >> 15; */
127         a.high = 0;
128         a.low = sample->pres;
129         ao_mul64(&a, &a, &SENS);
130         ao_rshift64(&a, &a, 21);
131         ao_minus64(&a, &a, &OFF);
132         ao_rshift64(&a, &a, 15);
133         value->pres = a.low;
134         /**/
135         
136         value->temp = TEMP;
137 }