summaryrefslogtreecommitdiff
path: root/src/drivers/ao_ms5607_convert_8051.c
blob: f3a48c464b784830462bd29b840abaa57a826f11 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Copyright © 2012 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#include <ao_ms5607.h>
#include <ao_int64.h>

#if HAS_MS5611
#define SHIFT_OFF	16
#define SHIFT_TCO	7
#define SHIFT_SENS	15
#define SHFIT_TCS	8
#else
#define SHIFT_OFF	17
#define SHIFT_TCO	6
#define SHIFT_SENS	16
#define SHIFT_TCS	7
#endif

void
ao_ms5607_convert(__xdata struct ao_ms5607_sample *sample,
		  __xdata struct ao_ms5607_value *value)
{
	__LOCAL int32_t	dT;
	__LOCAL int32_t TEMP;
	__LOCAL ao_int64_t OFF;
	__LOCAL ao_int64_t SENS;
	__LOCAL ao_int64_t a;

	dT = sample->temp - ((int32_t) ms5607_prom.tref << 8);
	
	/* TEMP = 2000 + (((int64_t) dT * ms5607_prom.tempsens) >> 23); */
	ao_mul64_32_32(&a, dT, ms5607_prom.tempsens);
	ao_rshift64(&a, &a, 23);
	TEMP = 2000 + a.low;
	/* */

	/* OFF = ((int64_t) ms5607_prom.off << SHIFT_OFF) + (((int64_t) ms5607_prom.tco * dT) >> SHIFT_TCO);*/
#if SHIFT_OFF > 16
	OFF.high = ms5607_prom.off >> (32 - SHIFT_OFF);
#else
	OFF.high = 0;
#endif
	OFF.low = (uint32_t) ms5607_prom.off << SHIFT_OFF;
	ao_mul64_32_32(&a, ms5607_prom.tco, dT);
	ao_rshift64(&a, &a, SHIFT_TCO);
	ao_plus64(&OFF, &OFF, &a);
	/**/

	/* SENS = ((int64_t) ms5607_prom.sens << SHIFT_SENS) + (((int64_t) ms5607_prom.tcs * dT) >> SHIFT_TCS); */
	SENS.high = 0;
	SENS.low = (uint32_t) ms5607_prom.sens << SHIFT_SENS;
	ao_mul64_32_32(&a, ms5607_prom.tcs, dT);
	ao_rshift64(&a, &a, SHIFT_TCS);
	ao_plus64(&SENS, &SENS, &a);
	/**/

	if (TEMP < 2000) {
		__LOCAL int32_t	T2;
		__LOCAL int32_t TEMPM;
		__LOCAL ao_int64_t OFF2;
		__LOCAL ao_int64_t SENS2;

		/* T2 = ((int64_t) dT * (int64_t) dT) >> 31; */
		ao_mul64_32_32(&a, dT, dT);
		T2 = (a.low >> 31) | (a.high << 1);
		/**/

		TEMPM = TEMP - 2000;

		/* OFF2 = (61 * (int64_t) TEMPM * (int64_t) TEMPM) >> 4; */
		ao_mul64_32_32(&OFF2, TEMPM, TEMPM);
		ao_mul64_64_16(&OFF2, &OFF2, 61);
		ao_rshift64(&OFF2, &OFF2, 4);
		/**/
		
		/* SENS2 = 2 * (int64_t) TEMPM * (int64_t) TEMPM; */
		ao_mul64_32_32(&SENS2, TEMPM, TEMPM);
		ao_lshift64(&SENS2, &SENS2, 1);
		/**/

		if (TEMP < -1500) {
			int32_t TEMPP;
			int32_t TEMPP2;

			TEMPP = TEMP + 1500;
			TEMPP2 = TEMPP * TEMPP;

			/* OFF2 = OFF2 + 15 * TEMPP2; */
			ao_mul64_32_32(&a, 15, TEMPP2);
			ao_plus64(&OFF2, &OFF2, &a);
			/**/

			/* SENS2 = SENS2 + 8 * TEMPP2; */
			a.high = 0;
			a.low = TEMPP2;
			ao_lshift64(&a, &a, 3);
			ao_plus64(&SENS2, &SENS2, &a);
			/**/
		}
		TEMP -= T2;

		/* OFF -= OFF2; */
		ao_minus64(&OFF, &OFF, &OFF2);
		/**/

		/* SENS -= SENS2; */
		ao_minus64(&SENS, &SENS, &SENS2);
		/**/
	}

	/* value->pres = ((((int64_t) sample->pres * SENS) >> 21) - OFF) >> 15; */
	a.high = 0;
	a.low = sample->pres;
	ao_mul64(&a, &a, &SENS);
	ao_rshift64(&a, &a, 21);
	ao_minus64(&a, &a, &OFF);
	ao_rshift64(&a, &a, 15);
	value->pres = a.low;
	/**/
	
	value->temp = TEMP;
}