Skyward boardcore
Loading...
Searching...
No Matches
BME280.h
Go to the documentation of this file.
1/* Copyright (c) 2021 Skyward Experimental Rocketry
2 * Author: Alberto Nidasio
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
27#include <sensors/Sensor.h>
28
29#include "BME280Data.h"
30
31namespace Boardcore
32{
33
34class BME280 : public Sensor<BME280Data>
35{
36public:
46
47 enum Mode
48 {
49 SLEEP_MODE = 0x0,
51 NORMAL_MODE = 0x3
52 };
53
65
74
76 {
77 struct __attribute__((packed)) BME280ConfigBits
78 {
80 oversamplingHumidity : 3;
81 uint8_t : 5;
82
83 // status
88 uint8_t imUpdate : 1;
89 uint8_t : 2;
94 uint8_t measuring : 1;
95 uint8_t : 4;
96
97 Mode mode : 2;
99 oversamplingPressure : 3;
101 oversamplingTemperature : 3;
102
103 // config
104 uint8_t spi3wEn : 1;
105 uint8_t : 1;
106 FilterCoeff filter : 3;
107 StandbyTime standbyTime : 3;
109
110 struct
111 {
112 uint8_t ctrlHumidity;
113 uint8_t status;
116 uint8_t config;
118
119 uint8_t bytesArray[4];
120 };
121
123 {
124 struct __attribute__((packed))
125 {
126 uint16_t dig_T1;
127 int16_t dig_T2;
128 int16_t dig_T3;
129 uint16_t dig_P1;
130 int16_t dig_P2;
131 int16_t dig_P3;
132 int16_t dig_P4;
133 int16_t dig_P5;
134 int16_t dig_P6;
135 int16_t dig_P7;
136 int16_t dig_P8;
137 int16_t dig_P9;
138 uint8_t dig_H1;
139 int16_t dig_H2;
140 uint8_t dig_H3;
141 int16_t dig_H4 : 12;
142 int16_t dig_H5 : 12;
143 int8_t dig_H6;
145
146 uint8_t bytesArray[32];
147 };
148
149 static constexpr uint8_t REG_ID_VAL = 0x60;
150
153
156
159
160 explicit BME280(SPISlave spiSlave,
162
166 bool init() override;
167
179 void setSensorMode(Mode mode);
180
185 void setHumidityOversampling(Oversampling oversampling);
186
191 void setPressureOversampling(Oversampling oversampling);
192
197 void setTemperatureOversampling(Oversampling oversampling);
198
203 void setFilterCoeff(FilterCoeff filterCoeff);
204
208 void setStandbyTime(StandbyTime standbyTime);
209
214
219
224
230 static unsigned int calculateMaxMeasurementTime(BME280Config config);
231
232 unsigned int getMaxMeasurementTime();
233
239 bool selfTest() override;
240
241protected:
242 BME280Data sampleImpl() override;
243
244private:
245 void reset();
246
247 void setConfiguration();
248
249 void setConfiguration(BME280Config config);
250
251 BME280Config readConfiguration();
252
253 void loadCompensationParameters();
254
255 // Compensation algorithm rev.1.1 from Bosh datasheet
256
257 int32_t computeFineTemperature(int32_t adcTemperature);
258
259 int32_t compensateTemperature(int32_t fineTemperature);
260
261 uint32_t compensatePressure(int32_t adcPressure);
262
263 uint32_t compensateHumidity(int32_t adcHumidity);
264
270 bool checkWhoAmI();
271
272 enum BME280Registers : uint8_t
273 {
274 REG_CALIB_0 = 0x88,
275 // Calibration register 1-25
276
277 REG_ID = 0x50,
278 REG_RESET = 0x60,
279
280 REG_CALIB_26 = 0xE1,
281 // Calibration register 27-41
282
283 REG_CTRL_HUM = 0x72,
284 REG_STATUS = 0x73,
285 REG_CTRL_MEAS = 0x74,
286 REG_CONFIG = 0x75,
287
288 REG_PRESS_MSB = 0x77,
289 REG_PRESS_LSB = 0x78,
290 REG_PRESS_XLSB = 0x79,
291 REG_TEMP_MSB = 0x7A,
292 REG_TEMP_LSB = 0x7B,
293 REG_TEMP_XLSB = 0x7C,
294 REG_HUM_MSB = 0x7D,
295 REG_HUM_LSB = 0x7E,
296 };
297
298 const SPISlave spiSlave;
299 BME280Config config;
300 BME280Comp compParams;
301 int32_t fineTemperature; // Used in compensation algorithm
302
303 PrintLogger logger = Logging::getLogger("bme280");
304};
305
306} // namespace Boardcore
static const BME280Config BME280_CONFIG_TEMP_SINGLE
Definition BME280.h:158
void setHumidityOversampling(Oversampling oversampling)
Sets the oversampling for humidity readings, use SKIPPED to disable humidity sampling.
Definition BME280.cpp:103
@ OVERSAMPLING_1
Oversampling x1.
Definition BME280.h:40
@ OVERSAMPLING_8
Oversampling x8.
Definition BME280.h:43
@ OVERSAMPLING_4
Oversampling x4.
Definition BME280.h:42
@ OVERSAMPLING_16
Oversampling x16.
Definition BME280.h:44
@ OVERSAMPLING_2
Oversampling x2.
Definition BME280.h:41
@ SKIPPED
Skipped (output set to 0x8000)
Definition BME280.h:39
static const BME280Config BME280_CONFIG_ALL_ENABLED
Temperature enabled in forced mode.
Definition BME280.h:155
void setTemperatureOversampling(Oversampling oversampling)
Sets the oversampling for temperature readings, use SKIPPED to disable temperature sampling.
Definition BME280.cpp:117
bool init() override
Initialize the device with the specified configuration.
Definition BME280.cpp:55
void setStandbyTime(StandbyTime standbyTime)
Sets the standby time between readings in normal mode.
Definition BME280.cpp:131
void setSensorMode(Mode mode)
Sets the sensor mode.
Definition BME280.cpp:96
@ FILTER_COEFF_8
Filter coefficient = 8.
Definition BME280.h:71
@ FILTER_OFF
Filter off.
Definition BME280.h:68
@ FILTER_COEFF_2
Filter coefficient = 2.
Definition BME280.h:69
@ FILTER_COEFF_16
Filter coefficient = 16.
Definition BME280.h:72
@ FILTER_COEFF_4
Filter coefficient = 4.
Definition BME280.h:70
static const BME280Config BME280_DEFAULT_CONFIG
Datasheet values for indoor navigation.
Definition BME280.h:152
@ SLEEP_MODE
Sleep mode.
Definition BME280.h:49
@ NORMAL_MODE
Normal mode.
Definition BME280.h:51
@ FORCED_MODE
Forced mode.
Definition BME280.h:50
unsigned int getMaxMeasurementTime()
Definition BME280.cpp:209
bool selfTest() override
Reads the WHO AM I register.
Definition BME280.cpp:214
static unsigned int calculateMaxMeasurementTime(BME280Config config)
Maximum measurement time formula from datasheet page 51.
Definition BME280.cpp:202
HumidityData readHumidity()
Reads only the humidity, does not set the configuration.
Definition BME280.cpp:138
PressureData readPressure()
Reads only the pressure, does not set the configuration.
Definition BME280.cpp:158
void setPressureOversampling(Oversampling oversampling)
Sets the oversampling for pressure readings, use SKIPPED to disable pressure sampling.
Definition BME280.cpp:110
void setFilterCoeff(FilterCoeff filterCoeff)
Sets the coefficient for the IIR filter (applied to temperature and pressure)
Definition BME280.cpp:124
TemperatureData readTemperature()
Reads only the temperature, does not set the configuration.
Definition BME280.cpp:179
@ STB_TIME_500
500 ms
Definition BME280.h:60
@ STB_TIME_0_5
0.5 ms
Definition BME280.h:56
@ STB_TIME_250
250 ms
Definition BME280.h:59
@ STB_TIME_20
20 ms
Definition BME280.h:63
@ STB_TIME_10
10 ms
Definition BME280.h:62
@ STB_TIME_62_5
62.5 ms
Definition BME280.h:57
@ STB_TIME_125
125 ms
Definition BME280.h:58
@ STB_TIME_1000
1000 ms
Definition BME280.h:61
BME280Data sampleImpl() override
Read a data sample from the sensor. In case of errors, the method should return the last available co...
Definition BME280.cpp:216
static constexpr uint8_t REG_ID_VAL
Who am I value.
Definition BME280.h:149
static PrintLogger getLogger(const string &name)
Base sensor class with has to be extended by any sensor driver.
Definition Sensor.h:91
Driver for the VN100S IMU.
Structure to handle humidity data.
Definition SensorData.h:104
Contains information about a single SPI slave device.
struct __attribute__((packed))
Definition BME280.h:124
uint8_t status
Device status.
Definition BME280.h:113
uint8_t ctrlHumidity
Humidity options.
Definition BME280.h:112
struct __attribute__((packed)) BME280ConfigBits
Definition BME280.h:77
struct Boardcore::BME280::BME280Config::@2 bytes
uint8_t config
Rate, filter and interface options.
Definition BME280.h:116