altos: Make TeleMini v2.0 fit
[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 #include <ao_int64.h>
20
21 #if HAS_MS5611
22 #define SHIFT_OFF       16
23 #define SHIFT_TCO       7
24 #define SHIFT_SENS      15
25 #define SHFIT_TCS       8
26 #else
27 #define SHIFT_OFF       17
28 #define SHIFT_TCO       6
29 #define SHIFT_SENS      16
30 #define SHIFT_TCS       7
31 #endif
32
33 void
34 ao_ms5607_convert(__xdata struct ao_ms5607_sample *sample,
35                   __xdata struct ao_ms5607_value *value)
36 {
37         __LOCAL int32_t dT;
38         __LOCAL int32_t TEMP;
39         __LOCAL ao_int64_t OFF;
40         __LOCAL ao_int64_t SENS;
41         __LOCAL ao_int64_t a;
42
43         dT = sample->temp - ((int32_t) ms5607_prom.tref << 8);
44         
45         /* TEMP = 2000 + (((int64_t) dT * ms5607_prom.tempsens) >> 23); */
46         ao_mul64_32_32(&a, dT, ms5607_prom.tempsens);
47         ao_rshift64(&a, &a, 23);
48         TEMP = 2000 + a.low;
49         /* */
50
51         /* OFF = ((int64_t) ms5607_prom.off << SHIFT_OFF) + (((int64_t) ms5607_prom.tco * dT) >> SHIFT_TCO);*/
52 #if SHIFT_OFF > 16
53         OFF.high = ms5607_prom.off >> (32 - SHIFT_OFF);
54 #else
55         OFF.high = 0;
56 #endif
57         OFF.low = (uint32_t) ms5607_prom.off << SHIFT_OFF;
58         ao_mul64_32_32(&a, ms5607_prom.tco, dT);
59         ao_rshift64(&a, &a, SHIFT_TCO);
60         ao_plus64(&OFF, &OFF, &a);
61         /**/
62
63         /* SENS = ((int64_t) ms5607_prom.sens << SHIFT_SENS) + (((int64_t) ms5607_prom.tcs * dT) >> SHIFT_TCS); */
64         SENS.high = 0;
65         SENS.low = (uint32_t) ms5607_prom.sens << SHIFT_SENS;
66         ao_mul64_32_32(&a, ms5607_prom.tcs, dT);
67         ao_rshift64(&a, &a, SHIFT_TCS);
68         ao_plus64(&SENS, &SENS, &a);
69         /**/
70
71         if (TEMP < 2000) {
72                 __LOCAL int32_t T2;
73                 __LOCAL int32_t TEMPM;
74                 __LOCAL ao_int64_t OFF2;
75                 __LOCAL ao_int64_t SENS2;
76
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);
80                 /**/
81
82                 TEMPM = TEMP - 2000;
83
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);
88                 /**/
89                 
90                 /* SENS2 = 2 * (int64_t) TEMPM * (int64_t) TEMPM; */
91                 ao_mul64_32_32(&SENS2, TEMPM, TEMPM);
92                 ao_lshift64(&SENS2, &SENS2, 1);
93                 /**/
94
95                 if (TEMP < -1500) {
96                         int32_t TEMPP;
97                         int32_t TEMPP2;
98
99                         TEMPP = TEMP + 1500;
100                         TEMPP2 = TEMPP * TEMPP;
101
102                         /* OFF2 = OFF2 + 15 * TEMPP2; */
103                         ao_mul64_32_32(&a, 15, TEMPP2);
104                         ao_plus64(&OFF2, &OFF2, &a);
105                         /**/
106
107                         /* SENS2 = SENS2 + 8 * TEMPP2; */
108                         a.high = 0;
109                         a.low = TEMPP2;
110                         ao_lshift64(&a, &a, 3);
111                         ao_plus64(&SENS2, &SENS2, &a);
112                         /**/
113                 }
114                 TEMP -= T2;
115
116                 /* OFF -= OFF2; */
117                 ao_minus64(&OFF, &OFF, &OFF2);
118                 /**/
119
120                 /* SENS -= SENS2; */
121                 ao_minus64(&SENS, &SENS, &SENS2);
122                 /**/
123         }
124
125         /* value->pres = ((((int64_t) sample->pres * SENS) >> 21) - OFF) >> 15; */
126         a.high = 0;
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);
132         value->pres = a.low;
133         /**/
134         
135         value->temp = TEMP;
136 }