-
Notifications
You must be signed in to change notification settings - Fork 3
/
BMP085.h
349 lines (307 loc) · 9.32 KB
/
BMP085.h
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* @file BMP085.h
* @version 1.0
*
* @section License
* Copyright (C) 2017, Mikael Patel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*/
#ifndef BMP085_H
#define BMP085_H
#include "TWI.h"
/**
* TWI Device Driver for the Bosch BMP085 Digital Pressure Sensor.
*
* @section Circuit
* The GY-80 10DOF module with pull-up resistors (4K7) for TWI signals
* and internal 3V3 voltage converter.
* @code
* GY-80
* +------------+
* (VCC)---------------1-|VCC |
* 2-|3V3 |
* (GND)---------------3-|GND |
* (A5/SCL)------------4-|SCL |
* (A4/SDA)------------5-|SDA |
* 6-|M-DRDY |
* 7-|A-INT1 |
* 8-|T-INT1 |
* 9-|P-XCLR |
* (Dn/EXTn)----------10-|P-EOC |
* +------------+
* @endcode
*
* @section References
* 1. http://media.digikey.com/pdf/Data%20Sheets/Bosch/BMP085.pdf
* BST-BMP085-DS000-03, Rev. 1.0, 01 July 2008.
*/
class BMP085 : protected TWI::Device {
public:
/**
* Oversampling modes (table, pp. 10).
*/
enum Mode {
ULTRA_LOW_POWER = 0,
STANDARD = 1,
HIGH_RESOLUTION = 2,
ULTRA_HIGH_RESOLUTION = 3
} __attribute__((packed));
/**
* Construct BMP085 driver with I2C address(0x77) and default
* ULTRA_LOW_POWER mode.
*/
BMP085(TWI& twi) :
TWI::Device(twi, 0x77),
m_mode(ULTRA_LOW_POWER),
m_cmd(0),
m_start(0),
B5(0),
m_pressure(0)
{}
/**
* Initiate device driver. Load calibration coefficients from device.
* Return true(1) if successful otherwise false(0).
* @param[in] mode oversampling (Default ULTRA_LOW_POWER).
* @return bool.
*/
bool begin(Mode mode = ULTRA_LOW_POWER)
{
// Set the operation mode
m_mode = mode;
// Read coefficients from the device
if (!acquire()) return (false);
uint8_t reg = COEFF_REG;
write(®, sizeof(reg));
int res = read(&m_param, sizeof(m_param));
if (!release()) return (false);
if (res != sizeof(m_param)) return (false);
// Adjust coefficients to little endian
uint16_t* p = (uint16_t*) &m_param;
for (size_t i = 0; i < sizeof(param_t) / sizeof(uint16_t); i++, p++)
*p = bswap16(*p);
return (true);
}
/**
* Issue a sample raw temperature sensor request. Return true(1) if
* successful otherwise false.
* @return bool.
*/
bool sample_temperature_request()
{
// Check that a conversion request is not in process
if (m_cmd != 0) return (false);
// Start a temperature measurement and wait
m_cmd = TEMP_CONV_CMD;
uint8_t req[2] = { CMD_REG, m_cmd };
if (!acquire()) return (false);
write(req, sizeof(req));
if (!release()) return (false);
// Set start time for completion
m_start = millis();
return (true);
}
/**
* Read the raw temperature sensor. Will wait for the conversion to
* complete. Return true(1) if successful otherwise false(0).
* @return bool.
*/
bool read_temperature()
{
// Check that a temperature conversion request was issued
if (m_cmd != TEMP_CONV_CMD) return (false);
m_cmd = 0;
// Check if we need to wait for the conversion to complete
uint16_t run = millis() - m_start;
if (run < TEMP_CONV_MS) delay(TEMP_CONV_MS - run);
// Read the raw temperature sensor data
uint8_t reg = RES_REG;
int16_t UT;
if (!acquire()) return (false);
write(®, sizeof(reg));
read(&UT, sizeof(UT));
if (!release()) return (false);
// Adjust for little-endian
UT = bswap16(UT);
// Temperature calculation
int32_t X1 = ((((int32_t) UT) - m_param.ac6) * m_param.ac5) >> 15;
int32_t X2 = (((int32_t) m_param.mc) << 11) / (X1 + m_param.md);
B5 = X1 + X2;
return (true);
}
/**
* Sample the raw temperature sensor and read. Return true(1) if
* successful otherwise false(0).
* @return bool.
*/
bool sample_temperature()
{
return (sample_temperature_request() && read_temperature());
}
/**
* Issue a sample request of the raw pressure sensor. Return true(1)
* if successful otherwise false(0).
* @return bool.
*/
bool sample_pressure_request()
{
// Check that a conversion request is not in process
if (m_cmd != 0) return (false);
// Start a pressure measurement
if (!acquire()) return (false);
m_cmd = PRESSURE_CONV_CMD + (m_mode << 6);
uint8_t req[2] = { CMD_REG, m_cmd };
write(&req, sizeof(req));
if (!release()) return (false);
// Set start time for completion
m_start = millis();
return (true);
}
/**
* Read the raw pressure sensor. Will wait for the conversion to
* complete. Return true(1) if successful otherwise false(0).
* @return bool.
*/
bool read_pressure()
{
/** Pressure conversion time max table (ms), index with mode. */
static const uint8_t PRESSURE_CONV_MS[] PROGMEM = {
5, 8, 14, 26
};
// Check that a conversion request was issued
if (m_cmd != (PRESSURE_CONV_CMD + (m_mode << 6))) return (false);
m_cmd = 0;
// Check if we need to wait for the conversion to complete
uint16_t run = millis() - m_start;
uint16_t ms = pgm_read_byte(&PRESSURE_CONV_MS[m_mode]);
if (run < ms) delay(ms - run);
// Read the raw pressure sensor data
union {
uint32_t as_int32;
uint8_t as_uint8[4];
} res;
res.as_uint8[0] = 0;
uint8_t reg = RES_REG;
if (!acquire()) return (false);
write(®, sizeof(reg));
read(&res.as_uint8[1], 3);
if (!release()) return (false);
// Adjust for little endian and resolution (oversampling mode)
int32_t UP = bswap32(res.as_int32) >> (8 - m_mode);
int32_t B3, B6, X1, X2, X3;
uint32_t B4, B7;
// Pressure calculation
B6 = B5 - 4000;
X1 = (m_param.b2 * ((B6 * B6) >> 12)) >> 11;
X2 = (m_param.ac2 * B6) >> 11;
X3 = X1 + X2;
B3 = ((((((int32_t) m_param.ac1) << 2) + X3) << m_mode) + 2) >> 2;
X1 = (m_param.ac3 * B6) >> 13;
X2 = (m_param.b1 * ((B6 * B6) >> 12)) >> 16;
X3 = ((X1 + X2) + 2) >> 2;
B4 = (m_param.ac4 * (uint32_t) (X3 + 32768)) >> 15;
B7 = ((uint32_t) UP - B3) * (50000 >> m_mode);
m_pressure = (B7 < 0x80000000) ? (B7 << 1) / B4 : (B7 / B4) << 1;
X1 = (m_pressure >> 8) * (m_pressure >> 8);
X1 = (X1 * 3038) >> 16;
X2 = (-7357 * m_pressure) >> 16;
m_pressure += (X1 + X2 + 3791) >> 4;
return (true);
}
/**
* Sample and read the raw pressure sensor. Return true(1) if
* successful otherwise false(0).
* @return bool.
*/
bool sample_pressure()
__attribute__((always_inline))
{
return (sample_pressure_request() && read_pressure());
}
/**
* Sample and read the raw temperature and pressure sensor. Return
* true(1) if successful otherwise false(0). Retrieve calculated
* values with temperature() and pressure().
* @return bool.
*/
bool sample()
__attribute__((always_inline))
{
return (sample_temperature() && sample_pressure());
}
/**
* Calculate temperature from the latest raw sensor reading.
* @return calculated temperature in steps of 0.1 C
*/
int16_t temperature() const
__attribute__((always_inline))
{
return ((B5 + 8) >> 4);
}
/**
* Return latest calculated pressure from temperature and pressure
* raw sensor data.
* @return calculated pressure in steps of 1 Pa (0,01 hPa).
*/
int32_t pressure() const
{
return (m_pressure);
}
protected:
/** Temperature conversion time max (ms). */
static const uint16_t TEMP_CONV_MS = 5;
/**
* Calibration coefficients (chap. 3.4, pp. 11). Data from the
* device is in big-endian order.
*/
struct param_t {
int16_t ac1;
int16_t ac2;
int16_t ac3;
uint16_t ac4;
uint16_t ac5;
uint16_t ac6;
int16_t b1;
int16_t b2;
int16_t mb;
int16_t mc;
int16_t md;
} __attribute__((packed));
/**
* EEPROM parameters, command and result registers (chap. 4.5, pp. 17).
* Parameter and result registers are 16-bit, in big-endian order.
*/
enum reg_t {
COEFF_REG = 0xAA, //!< Calibration coefficients register address.
CMD_REG = 0xF4, //!< Command register address (8-bit).
RES_REG = 0xF6 //!< Result register address (16-bit).
} __attribute__((packed));
/**
* Measurement/Control register value (chap. 4.4, pp. 16).
*/
enum cmd_t {
TEMP_CONV_CMD = 0x2E, //!< Temperature conversion command.
PRESSURE_CONV_CMD = 0x34 //!< Pressure conversion command.
} __attribute__((packed));
/** Device calibration data (from EEPROM data registers). */
param_t m_param;
/** Pressure conversion mode. */
Mode m_mode;
/** Currrent command. */
uint8_t m_cmd;
/** Sample request start time (ms). */
uint16_t m_start;
/** Common intermediate temperature factor. */
int32_t B5;
/** Latest calculated pressure. */
int32_t m_pressure;
};
#endif