Skyward boardcore
Loading...
Searching...
No Matches
BME280I2C.h
Go to the documentation of this file.
1/* Copyright (c) 2023 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
26#include <drivers/i2c/I2C.h>
27#include <sensors/Sensor.h>
28
29#include "BME280Data.h"
30
31namespace Boardcore
32{
33
34class BME280I2C : 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 BME280I2C(I2C& bus,
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 bool 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 Registers : uint8_t
273 {
274 REG_CALIB_0 = 0x88,
275 // Calibration register 1-25
276
277 REG_ID = 0xD0,
278 REG_RESET = 0xE0,
279
280 REG_CALIB_26 = 0xE1,
281 // Calibration register 27-41
282
283 REG_CTRL_HUM = 0xF2,
284 REG_STATUS = 0xF3,
285 REG_CTRL_MEAS = 0xF4,
286 REG_CONFIG = 0xF5,
287
288 REG_PRESS_MSB = 0xF7,
289 REG_PRESS_LSB = 0xF8,
290 REG_PRESS_XLSB = 0xF9,
291 REG_TEMP_MSB = 0xFA,
292 REG_TEMP_LSB = 0xFB,
293 REG_TEMP_XLSB = 0xFC,
294 REG_HUM_MSB = 0xFD,
295 REG_HUM_LSB = 0xFE,
296 };
297
298 I2C& bus;
301
302 BME280Config config;
303 BME280Comp compParams;
304 int32_t fineTemperature; // Used in compensation algorithm
305
306 PrintLogger logger = Logging::getLogger("bme280");
307};
308
309} // namespace Boardcore
@ OVERSAMPLING_8
Oversampling x8.
Definition BME280I2C.h:43
@ OVERSAMPLING_2
Oversampling x2.
Definition BME280I2C.h:41
@ SKIPPED
Skipped (output set to 0x8000)
Definition BME280I2C.h:39
@ OVERSAMPLING_16
Oversampling x16.
Definition BME280I2C.h:44
@ OVERSAMPLING_4
Oversampling x4.
Definition BME280I2C.h:42
@ OVERSAMPLING_1
Oversampling x1.
Definition BME280I2C.h:40
bool init() override
Initialize the device with the specified configuration.
Definition BME280I2C.cpp:55
static const BME280Config BME280_CONFIG_TEMP_SINGLE
Definition BME280I2C.h:158
void setTemperatureOversampling(Oversampling oversampling)
Sets the oversampling for temperature readings, use SKIPPED to disable temperature sampling.
@ FILTER_OFF
Filter off.
Definition BME280I2C.h:68
@ FILTER_COEFF_16
Filter coefficient = 16.
Definition BME280I2C.h:72
@ FILTER_COEFF_2
Filter coefficient = 2.
Definition BME280I2C.h:69
@ FILTER_COEFF_8
Filter coefficient = 8.
Definition BME280I2C.h:71
@ FILTER_COEFF_4
Filter coefficient = 4.
Definition BME280I2C.h:70
static constexpr uint8_t REG_ID_VAL
Who am I value.
Definition BME280I2C.h:149
void setHumidityOversampling(Oversampling oversampling)
Sets the oversampling for humidity readings, use SKIPPED to disable humidity sampling.
void setSensorMode(Mode mode)
Sets the sensor mode.
Definition BME280I2C.cpp:97
static unsigned int calculateMaxMeasurementTime(BME280Config config)
Maximum measurement time formula from datasheet page 51.
BME280Data sampleImpl() override
Read a data sample from the sensor. In case of errors, the method should return the last available co...
PressureData readPressure()
Reads only the pressure, does not set the configuration.
@ SLEEP_MODE
Sleep mode.
Definition BME280I2C.h:49
@ NORMAL_MODE
Normal mode.
Definition BME280I2C.h:51
@ FORCED_MODE
Forced mode.
Definition BME280I2C.h:50
static const BME280Config BME280_DEFAULT_CONFIG
Datasheet values for indoor navigation.
Definition BME280I2C.h:152
static const BME280Config BME280_CONFIG_ALL_ENABLED
Temperature enabled in forced mode.
Definition BME280I2C.h:155
@ STB_TIME_62_5
62.5 ms
Definition BME280I2C.h:57
@ STB_TIME_1000
1000 ms
Definition BME280I2C.h:61
HumidityData readHumidity()
Reads only the humidity, does not set the configuration.
void setPressureOversampling(Oversampling oversampling)
Sets the oversampling for pressure readings, use SKIPPED to disable pressure sampling.
BME280I2C(I2C &bus, BME280Config config=BME280_CONFIG_ALL_ENABLED)
Definition BME280I2C.cpp:51
bool selfTest() override
Reads the WHO AM I register.
void setStandbyTime(StandbyTime standbyTime)
Sets the standby time between readings in normal mode.
unsigned int getMaxMeasurementTime()
void setFilterCoeff(FilterCoeff filterCoeff)
Sets the coefficient for the IIR filter (applied to temperature and pressure)
TemperatureData readTemperature()
Reads only the temperature, does not set the configuration.
High level driver for the I2C peripherals.
Definition I2C.h:40
static PrintLogger getLogger(const string &name)
Base sensor class with has to be extended by any sensor driver.
Definition Sensor.h:91
This file includes all the types the logdecoder script will decode.
Structure to handle humidity data.
Definition SensorData.h:105
Configuration struct for a slave device. This will be used for configuring the bus in order to commun...
Definition I2CDriver.h:104
struct Boardcore::BME280I2C::BME280Config::@3 bytes
struct __attribute__((packed)) BME280ConfigBits
Definition BME280I2C.h:77
uint8_t config
Rate, filter and interface options.
Definition BME280I2C.h:116
uint8_t status
Device status.
Definition BME280I2C.h:113
uint8_t ctrlHumidity
Humidity options.
Definition BME280I2C.h:112