-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmp085.c
270 lines (225 loc) · 7.3 KB
/
bmp085.c
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
FreeCopter IMU firmware - Copyright (C) 2012, +inf
Roberto Marino
FreeCopter IMU 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; either version 3 of the License, or
(at your option) any later version.
FreeCopter IMU 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, see <http://www.gnu.org/licenses/>.
FreeCopter is based on
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
2011,2012 Giovanni Di Sirio.
ChibiOS/RT 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; either version 3 of the License, or
(at your option) any later version.
ChibiOS/RT 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, see <http://www.gnu.org/licenses/>.
---
A special exception to the GPL can be applied should you wish to distribute
a combined work that includes ChibiOS/RT, without being obliged to provide
the source code for any proprietary components. See the file exception.txt
for full details of how and when the exception can be applied.
*/
#include <stdlib.h>
#include <ch.h>
#include <hal.h>
#include "bmp085.h"
#include "hw.h"
#ifndef BMP085_I2CD
#define BMP085_I2CD I2CD1
#endif
static bmp085_param param;
int bmp085_init(void)
{
msg_t status = MSG_OK;
uint8_t buffer_tx;
uint8_t buffer_rx[2];
systime_t tmo = MS2ST(4);
i2cAcquireBus(&BMP085_I2CD);
buffer_tx = BMP_AC1;
status = i2cMasterTransmitTimeout(&BMP085_I2CD,BMP_ADDR,&buffer_tx,1,buffer_rx,2,tmo);
if (status != MSG_OK){
i2cReleaseBus(&BMP085_I2CD);
return 1;
}
param.ac1 = ((buffer_rx[0] << 8) | buffer_rx[1]);
buffer_tx = BMP_AC2;
status = i2cMasterTransmitTimeout(&BMP085_I2CD,BMP_ADDR,&buffer_tx,1,buffer_rx,2,tmo);
if (status != MSG_OK){
i2cReleaseBus(&BMP085_I2CD);
return 2;
}
param.ac2 = ((buffer_rx[0] << 8) | buffer_rx[1]);
buffer_tx = BMP_AC3;
status = i2cMasterTransmitTimeout(&BMP085_I2CD,BMP_ADDR,&buffer_tx,1,buffer_rx,2,tmo);
if (status != MSG_OK){
i2cReleaseBus(&BMP085_I2CD);
return 3;
}
param.ac3 = ((buffer_rx[0] << 8) | buffer_rx[1]);
buffer_tx = BMP_AC4;
status = i2cMasterTransmitTimeout(&BMP085_I2CD,BMP_ADDR,&buffer_tx,1,buffer_rx,2,tmo);
if (status != MSG_OK){
i2cReleaseBus(&BMP085_I2CD);
return 4;
}
param.ac4 = ((buffer_rx[0] << 8) | buffer_rx[1]);
buffer_tx = BMP_AC5;
status = i2cMasterTransmitTimeout(&BMP085_I2CD,BMP_ADDR,&buffer_tx,1,buffer_rx,2,tmo);
if (status != MSG_OK){
i2cReleaseBus(&BMP085_I2CD);
return 5;
}
param.ac5 = ((buffer_rx[0] << 8) | buffer_rx[1]);
buffer_tx = BMP_AC6;
status = i2cMasterTransmitTimeout(&BMP085_I2CD,BMP_ADDR,&buffer_tx,1,buffer_rx,2,tmo);
if (status != MSG_OK){
i2cReleaseBus(&BMP085_I2CD);
return 6;
}
param.ac6 = ((buffer_rx[0] << 8) | buffer_rx[1]);
buffer_tx = BMP_B1;
status = i2cMasterTransmitTimeout(&BMP085_I2CD,BMP_ADDR,&buffer_tx,1,buffer_rx,2,tmo);
if (status != MSG_OK){
i2cReleaseBus(&BMP085_I2CD);
return 7;
}
param.b1 = ((buffer_rx[0] << 8) | buffer_rx[1]);
buffer_tx = BMP_B2;
status = i2cMasterTransmitTimeout(&BMP085_I2CD,BMP_ADDR,&buffer_tx,1,buffer_rx,2,tmo);
if (status != MSG_OK){
i2cReleaseBus(&BMP085_I2CD);
return 8;
}
param.b2 = ((buffer_rx[0] << 8) | buffer_rx[1]);
buffer_tx = BMP_MB;
status = i2cMasterTransmitTimeout(&BMP085_I2CD,BMP_ADDR,&buffer_tx,1,buffer_rx,2,tmo);
if (status != MSG_OK){
i2cReleaseBus(&BMP085_I2CD);
return 9;
}
param.mb = ((buffer_rx[0] << 8) | buffer_rx[1]);
buffer_tx = BMP_MC;
status = i2cMasterTransmitTimeout(&BMP085_I2CD,BMP_ADDR,&buffer_tx,1,buffer_rx,2,tmo);
if (status != MSG_OK){
i2cReleaseBus(&BMP085_I2CD);
return 10;
}
param.mc = ((buffer_rx[0] << 8) | buffer_rx[1]);
buffer_tx = BMP_MD;
status = i2cMasterTransmitTimeout(&BMP085_I2CD,BMP_ADDR,&buffer_tx,1,buffer_rx,2,tmo);
if (status != MSG_OK){
i2cReleaseBus(&BMP085_I2CD);
return 11;
}
param.md = ((buffer_rx[0] << 8) | buffer_rx[1]);
i2cReleaseBus(&BMP085_I2CD);
return 0;
}
int32_t bmp085_read_temp(void)
{
int32_t utemp;
int32_t x1,x2;
int32_t temperature = 0;
msg_t status = MSG_OK;
uint8_t buffer_tx[2];
uint8_t buffer_rx[2];
systime_t tmo = MS2ST(4);
// Read from I2C BUS
i2cAcquireBus(&BMP085_I2CD);
buffer_tx[0] = BMP_CR;
buffer_tx[1] = BMP_MODE_TEMP;
status = i2cMasterTransmitTimeout(&BMP085_I2CD,BMP_ADDR,buffer_tx,2,NULL,0,tmo);
if (status != MSG_OK){
i2cReleaseBus(&BMP085_I2CD);
return 273;
}
buffer_tx[0] = BMP_DATA;
status = i2cMasterTransmitTimeout(&BMP085_I2CD,BMP_ADDR,&buffer_tx[0],1,buffer_rx,2,tmo);
if (status != MSG_OK){
i2cReleaseBus(&BMP085_I2CD);
return 275;
}
i2cReleaseBus(&BMP085_I2CD);
// Building value
utemp = (int32_t)((buffer_rx[0] << 8) | buffer_rx[1]);
// Converting value
x1 = ((utemp - param.ac6) * param.ac5)/32768;
x2 = (param.mc*2048) / (x1 + param.md);
param.b5 = x1 + x2;
temperature = (param.b5 + 8) / 16;
return temperature;
}
int32_t bmp085_read_press(void)
{
int32_t upress;
int32_t x1,x2,x3;
int32_t b3,b6;
uint32_t b4,b7;
uint8_t oss = 3;
int32_t pressure = 0;
msg_t status = MSG_OK;
uint8_t buffer_tx[2];
uint8_t buffer_rx[3];
systime_t tmo = MS2ST(4);
// Reading from I2C BUS
i2cAcquireBus(&BMP085_I2CD);
buffer_tx[0] = BMP_CR;
buffer_tx[1] = BMP_MODE_PR0+(oss<<6);
status = i2cMasterTransmitTimeout(&BMP085_I2CD, BMP_ADDR, buffer_tx, 2, NULL, 0, tmo);
if (status != MSG_OK) {
i2cReleaseBus(&BMP085_I2CD);
return 22;
}
buffer_tx[0] = BMP_DATA;
status = i2cMasterTransmitTimeout(&BMP085_I2CD, BMP_ADDR, &buffer_tx[0], 1, buffer_rx, 3, tmo);
if (status != MSG_OK) {
i2cReleaseBus(&BMP085_I2CD);
return 2;
}
i2cReleaseBus(&BMP085_I2CD);
// Building value
upress = (int32_t)((buffer_rx[0] << 16) | (buffer_rx[1] << 8) | buffer_rx[2]);
upress = upress >> (8-oss);/*
return upress;*/
// Converting value
b6 = param.b5 - 4000;
x1 = (param.b2 * ((b6 * b6)/4096))/2048;
x2 = (param.ac2 * b6)/2048;
x3 = x1 + x2;
if (oss == 3) {
b3 = ((int32_t)param.ac1 * 4 + x3 + 2) << 1;
} else if (oss == 2) {
b3 = ((int32_t)param.ac1 * 4 + x3 + 2);
} else if (oss == 1) {
b3 = ((int32_t)param.ac1 * 4 + x3 + 2) >> 1;
} else if (oss == 0) {
b3 = ((int32_t)param.ac1 * 4 + x3 + 2) >> 2;
}
x1 = ((param.ac3)*b6)/8192;
x2 = (param.b1 * (b6*b6/4096))/65536;
x3 = ((x1 + x2) + 2)/4;
b4 = param.ac4 * (uint32_t)(x3 + 32768)/32768;
b7 = ((uint32_t)upress - b3)*(50000 >> oss);
if (b7 < 0x80000000) {
pressure = (b7*2)/b4;
}
else {
pressure = (b7/b4)*2;
}
x1 = (pressure/256)*(pressure/256);
x1 = (x1*3038)/65536;
x2 = (-7357*pressure)/65536;
pressure = pressure + (x1 + x2 + 3791)/16;
return pressure;
}