forked from opendata-stuttgart/sensors-software
-
Notifications
You must be signed in to change notification settings - Fork 5
/
bmx280_i2c.cpp
381 lines (322 loc) · 10.9 KB
/
bmx280_i2c.cpp
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*!
* @file BMX280_i2c.h
*
* Copied from Adafruits BME280 Library. SPI removed, BMP280 added
*
* Driver for the BMX280 humidity, temperature & pressure sensor
*
* These sensors use I2C or SPI to communicate, 2 or 4 pins are required
* to interface.
*
* Designed specifically to work with the Adafruit BME280 Breakout
* ----> http://www.adafruit.com/products/2652
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* @section author Author
*
* Written by Kevin "KTOWN" Townsend for Adafruit Industries.
*
* @section license License
*
* BSD license, all text here must be included in any redistribution.
* See the LICENSE file for details.
*
*/
#include "bmx280_i2c.h"
#include "Arduino.h"
#include <Wire.h>
/*!
* @brief Register addresses
*/
enum {
BMX280_REGISTER_DIG_T1 = 0x88,
BMX280_REGISTER_DIG_T2 = 0x8A,
BMX280_REGISTER_DIG_T3 = 0x8C,
BMX280_REGISTER_DIG_P1 = 0x8E,
BMX280_REGISTER_DIG_P2 = 0x90,
BMX280_REGISTER_DIG_P3 = 0x92,
BMX280_REGISTER_DIG_P4 = 0x94,
BMX280_REGISTER_DIG_P5 = 0x96,
BMX280_REGISTER_DIG_P6 = 0x98,
BMX280_REGISTER_DIG_P7 = 0x9A,
BMX280_REGISTER_DIG_P8 = 0x9C,
BMX280_REGISTER_DIG_P9 = 0x9E,
BME280_REGISTER_DIG_H1 = 0xA1,
BME280_REGISTER_DIG_H2 = 0xE1,
BME280_REGISTER_DIG_H3 = 0xE3,
BME280_REGISTER_DIG_H4 = 0xE4,
BME280_REGISTER_DIG_H5 = 0xE5,
BME280_REGISTER_DIG_H6 = 0xE7,
BMX280_REGISTER_CHIPID = 0xD0,
BMX280_REGISTER_VERSION = 0xD1,
BMX280_REGISTER_SOFTRESET = 0xE0,
BMX280_REGISTER_CAL26 = 0xE1, // R calibration stored in 0xE1-0xF0
BMX280_REGISTER_CONTROLHUMID = 0xF2,
BMX280_REGISTER_STATUS = 0XF3,
BMX280_REGISTER_CONTROL = 0xF4,
BMX280_REGISTER_CONFIG = 0xF5,
BMX280_REGISTER_PRESSUREDATA = 0xF7,
BMX280_REGISTER_TEMPDATA = 0xFA,
BMX280_REGISTER_HUMIDDATA = 0xFD,
};
/*!
* @brief Initialise sensor with given parameters / settings
* @param addr the I2C address the device can be found on
* @returns true on success, false otherwise
*/
bool BMX280::begin(uint8_t addr) {
_i2caddr = addr;
_wire = &Wire;
return init();
}
/*!
* @brief Initialise sensor with given parameters / settings
* @returns true on success, false otherwise
*/
bool BMX280::init() {
// I2C
_wire->begin();
// check if sensor, i.e. the chip ID is correct
_sensorID = read8(BMX280_REGISTER_CHIPID);
if (_sensorID != BMP280_SENSOR_ID && _sensorID != BME280_SENSOR_ID)
return false;
// reset the device using soft-reset
// this makes sure the IIR is off, etc.
write8(BMX280_REGISTER_SOFTRESET, 0xB6);
// wait for chip to wake up.
delay(30);
// if chip is still reading calibration, delay
unsigned attempts = 50;
while (--attempts && isReadingCalibration())
delay(10);
readCoefficients(); // read trimming parameters, see DS 4.2.2
setSampling(); // use defaults
return true;
}
/*!
* @brief setup sensor with given parameters / settings
*
* This is simply a overload to the normal begin()-function, so SPI users
* don't get confused about the library requiring an address.
* @param mode the power mode to use for the sensor
* @param tempSampling the temp samping rate to use
* @param pressSampling the pressure sampling rate to use
* @param humSampling the humidity sampling rate to use
* @param duration the standby duration to use
*/
void BMX280::setSampling(sensor_mode mode,
sensor_sampling tempSampling,
sensor_sampling pressSampling,
sensor_sampling humSampling,
standby_duration duration) {
_measReg.mode = mode;
_measReg.osrs_t = tempSampling;
_measReg.osrs_p = pressSampling;
ctrl_hum _humReg;
_humReg.osrs_h = humSampling;
config _configReg;
_configReg.t_sb = duration;
// making sure sensor is in sleep mode before setting configuration
// as it otherwise may be ignored
write8(BMX280_REGISTER_CONTROL, MODE_SLEEP);
if (_sensorID == BME280_SENSOR_ID) {
// you must make sure to also set REGISTER_CONTROL after setting the
// CONTROLHUMID register, otherwise the values won't be applied (see
// DS 5.4.3)
write8(BMX280_REGISTER_CONTROLHUMID, _humReg.get());
}
write8(BMX280_REGISTER_CONFIG, _configReg.get());
write8(BMX280_REGISTER_CONTROL, _measReg.get());
}
/*!
* @brief Writes an 8 bit value over I2C
* @param reg the register address to write to
* @param value the value to write to the register
*/
void BMX280::write8(uint8_t reg, uint8_t value) {
_wire->beginTransmission((uint8_t)_i2caddr);
_wire->write((uint8_t)reg);
_wire->write((uint8_t)value);
_wire->endTransmission();
}
/*!
* @brief Reads an 8 bit value over I2C
* @param reg the register address to read from
* @returns the data byte read from the device
*/
uint8_t BMX280::read8(uint8_t reg) {
_wire->beginTransmission((uint8_t)_i2caddr);
_wire->write((uint8_t)reg);
_wire->endTransmission();
_wire->requestFrom((uint8_t)_i2caddr, (uint8_t)1);
return _wire->read();
}
uint16_t BMX280::read16_LE(uint8_t reg) {
uint16_t value;
_wire->beginTransmission((uint8_t)_i2caddr);
_wire->write((uint8_t)reg);
_wire->endTransmission();
_wire->requestFrom((uint8_t)_i2caddr, (uint8_t)2);
value = _wire->read();
value |= (uint16_t) _wire->read() << 8;
return value;
}
/*!
* @brief Reads a signed little endian 16 bit value over I2C or SPI
* @param reg the register address to read from
* @returns the 16 bit data value read from the device
*/
int16_t BMX280::readS16_LE(uint8_t reg) {
return (int16_t)read16_LE(reg);
}
/*!
* @brief Reads a 24 bit value over I2C
* @param reg the register address to read from
* @returns the 24 bit data value read from the device
*/
uint32_t BMX280::read24(uint8_t reg) {
uint32_t value;
_wire->beginTransmission((uint8_t)_i2caddr);
_wire->write((uint8_t)reg);
_wire->endTransmission();
_wire->requestFrom((uint8_t)_i2caddr, (uint8_t)3);
value = _wire->read();
value <<= 8;
value |= _wire->read();
value <<= 8;
value |= _wire->read();
return value;
}
/*!
* @brief Take a new measurement (only possible in forced mode)
*/
void BMX280::takeForcedMeasurement() {
// set to forced mode, i.e. "take next measurement"
write8(BMX280_REGISTER_CONTROL, _measReg.get());
// wait until measurement has been completed, otherwise we would read
// the values from the last measurement
unsigned attempts = 15;
while (--attempts && (read8(BMX280_REGISTER_STATUS) & 0x08))
delay(1);
}
/*!
* @brief Reads the factory-set coefficients
*/
void BMX280::readCoefficients(void) {
dig_T1 = read16_LE(BMX280_REGISTER_DIG_T1);
dig_T2 = readS16_LE(BMX280_REGISTER_DIG_T2);
dig_T3 = readS16_LE(BMX280_REGISTER_DIG_T3);
dig_P1 = read16_LE(BMX280_REGISTER_DIG_P1);
dig_P2 = readS16_LE(BMX280_REGISTER_DIG_P2);
dig_P3 = readS16_LE(BMX280_REGISTER_DIG_P3);
dig_P4 = readS16_LE(BMX280_REGISTER_DIG_P4);
dig_P5 = readS16_LE(BMX280_REGISTER_DIG_P5);
dig_P6 = readS16_LE(BMX280_REGISTER_DIG_P6);
dig_P7 = readS16_LE(BMX280_REGISTER_DIG_P7);
dig_P8 = readS16_LE(BMX280_REGISTER_DIG_P8);
dig_P9 = readS16_LE(BMX280_REGISTER_DIG_P9);
if (_sensorID == BME280_SENSOR_ID) {
dig_H1 = read8(BME280_REGISTER_DIG_H1);
dig_H2 = readS16_LE(BME280_REGISTER_DIG_H2);
dig_H3 = read8(BME280_REGISTER_DIG_H3);
dig_H4 = (read8(BME280_REGISTER_DIG_H4) << 4) |
(read8(BME280_REGISTER_DIG_H4 + 1) & 0xF);
dig_H5 = readS16_LE(BME280_REGISTER_DIG_H5) >> 4;
dig_H6 = (int8_t)read8(BME280_REGISTER_DIG_H6);
}
}
/*!
* @brief return true if chip is busy reading cal data
* @returns true if reading calibration, false otherwise
*/
bool BMX280::isReadingCalibration(void) {
uint8_t const rStatus = read8(BMX280_REGISTER_STATUS);
return (rStatus & (1 << 0)) != 0;
}
/*!
* @brief Returns the temperature from the sensor
* @returns the temperature read from the device
*/
float BMX280::readTemperature(void) {
int32_t var1, var2;
int32_t adc_T = read24(BMX280_REGISTER_TEMPDATA);
if (adc_T == 0x800000) // value in case temp measurement was disabled
return NAN;
adc_T >>= 4;
var1 = ((((adc_T >> 3) - ((int32_t)dig_T1 << 1))) *
((int32_t)dig_T2)) >>
11;
var2 = (((((adc_T >> 4) - ((int32_t)dig_T1)) *
((adc_T >> 4) - ((int32_t)dig_T1))) >>
12) *
((int32_t)dig_T3)) >>
14;
t_fine = var1 + var2;
return float(t_fine) / 5120.0f;
}
/*!
* @brief Returns the pressure from the sensor
* @returns the pressure value (in Pascal) read from the device
*/
float BMX280::readPressure(void) {
readTemperature(); // must be done first to get t_fine
uint32_t adc_P = read24(BMX280_REGISTER_PRESSUREDATA);
if (adc_P == 0x800000) // value in case pressure measurement was disabled
return NAN;
adc_P >>= 4;
int64_t var1, var2, p;
var1 = ((int64_t)t_fine) - 128000;
var2 = var1 * var1 * (int64_t)dig_P6;
var2 = var2 + ((var1 * (int64_t)dig_P5) << 17);
var2 = var2 + (((int64_t)dig_P4) << 35);
var1 = ((var1 * var1 * (int64_t)dig_P3) >> 8) +
((var1 * (int64_t)dig_P2) << 12);
var1 =
(((((int64_t)1) << 47) + var1)) * ((int64_t)dig_P1) >> 33;
// avoid exception caused by division by zero
if (var1 == 0) {
return 30000.0f;
}
p = 1048576 - adc_P;
p = (((p << 31) - var2) * 3125) / var1;
var1 = (((int64_t)dig_P9) * (p >> 13) * (p >> 13)) >> 25;
var2 = (((int64_t)dig_P8) * p) >> 19;
int32_t ps = int32_t((p + var1 + var2) >> 8) + (((int32_t)dig_P7) << 4);
return float(ps >> 3) / 32.0f;
}
/*!
* @brief Returns the humidity from the sensor
* @returns the humidity value read from the device
*/
float BMX280::readHumidity(void) {
if (_sensorID != BME280_SENSOR_ID)
return NAN;
readTemperature(); // must be done first to get t_fine
uint16_t raw_h = read16_LE(BMX280_REGISTER_HUMIDDATA);
int32_t adc_H = (uint16_t) ((raw_h >> 8) | (raw_h << 8));
if (adc_H == 0x8000) // value in case humidity measurement was disabled
return NAN;
int32_t v_x1_u32r;
v_x1_u32r = (t_fine - ((int32_t)76800));
v_x1_u32r = (((((adc_H << 14) - (((int32_t)dig_H4) << 20) -
(((int32_t)dig_H5) * v_x1_u32r)) +
((int32_t)16384)) >>
15) *
(((((((v_x1_u32r * ((int32_t)dig_H6)) >> 10) *
(((v_x1_u32r * ((int32_t)dig_H3)) >> 11) +
((int32_t)32768))) >>
10) +
((int32_t)2097152)) *
((int32_t)dig_H2) +
8192) >>
14));
v_x1_u32r = (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) *
((int32_t)dig_H1)) >>
4));
v_x1_u32r = (v_x1_u32r < 0) ? 0 : v_x1_u32r;
v_x1_u32r = (v_x1_u32r > 419430400) ? 419430400 : v_x1_u32r;
return float(v_x1_u32r >> 12) / 1024.0f;
}