Skyward boardcore
Loading...
Searching...
No Matches
BMP280I2C.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 "BMP280Data.h"
30
31namespace Boardcore
32{
33
34class BMP280I2C : public Sensor<BMP280Data>
35{
36public:
46
47 enum Mode
48 {
49 SLEEP_MODE = 0x0,
51 NORMAL_MODE = 0x3
52 };
53
65
74
76 {
77 struct __attribute__((packed)) BMP280ConfigBits
78 {
79 // status
84 uint8_t imUpdate : 1;
85 uint8_t : 2;
90 uint8_t measuring : 1;
91 uint8_t : 4;
92
93 Mode mode : 2;
95 oversamplingPressure : 3;
97 oversamplingTemperature : 3;
98
99 // config
100 uint8_t spi3wEn : 1;
101 uint8_t : 1;
102 FilterCoeff filter : 3;
103 StandbyTime standbyTime : 3;
105
106 struct
107 {
108 uint8_t status;
111 uint8_t config;
113
114 uint8_t bytesArray[3];
115 };
116
118 {
119 struct __attribute__((packed))
120 {
121 uint16_t dig_T1;
122 int16_t dig_T2;
123 int16_t dig_T3;
124 uint16_t dig_P1;
125 int16_t dig_P2;
126 int16_t dig_P3;
127 int16_t dig_P4;
128 int16_t dig_P5;
129 int16_t dig_P6;
130 int16_t dig_P7;
131 int16_t dig_P8;
132 int16_t dig_P9;
134
135 uint8_t bytesArray[24];
136 };
137
138 static constexpr uint8_t REG_ID_VAL = 0x58;
139
142
145
148
149 explicit BMP280I2C(I2C& bus,
151
155 bool init() override;
156
168 void setSensorMode(Mode mode);
169
174 void setPressureOversampling(Oversampling oversampling);
175
180 void setTemperatureOversampling(Oversampling oversampling);
181
186 void setFilterCoeff(FilterCoeff filterCoeff);
187
191 void setStandbyTime(StandbyTime standbyTime);
192
197
202
208 static unsigned int calculateMaxMeasurementTime(BMP280Config config);
209
210 unsigned int getMaxMeasurementTime();
211
217 bool selfTest() override;
218
219protected:
220 BMP280Data sampleImpl() override;
221
222private:
223 bool reset();
224
225 void setConfiguration();
226
227 void setConfiguration(BMP280Config config);
228
229 BMP280Config readConfiguration();
230
231 void loadCompensationParameters();
232
233 // Compensation algorithm rev.1.1 from Bosh datasheet
234
235 int32_t computeFineTemperature(int32_t adcTemperature);
236
237 int32_t compensateTemperature(int32_t fineTemperature);
238
239 uint32_t compensatePressure(int32_t adcPressure);
245 bool checkWhoAmI();
246
247 enum Registers : uint8_t
248 {
249 REG_CALIB_0 = 0x88,
250 // Calibration register 1-25
251
252 REG_ID = 0xD0,
253 REG_RESET = 0xE0,
254
255 REG_STATUS = 0xF3,
256 REG_CTRL_MEAS = 0xF4,
257 REG_CONFIG = 0xF5,
258
259 REG_PRESS_MSB = 0xF7,
260 REG_PRESS_LSB = 0xF8,
261 REG_PRESS_XLSB = 0xF9,
262 REG_TEMP_MSB = 0xFA,
263 REG_TEMP_LSB = 0xFB,
264 REG_TEMP_XLSB = 0xFC
265 };
266
267 I2C& bus;
270
271 BMP280Config config;
272 BMP280Comp compParams;
273 int32_t fineTemperature; // Used in compensation algorithm
274
275 PrintLogger logger = Logging::getLogger("bmp280");
276};
277
278} // namespace Boardcore
@ STB_TIME_62_5
62.5 ms
Definition BMP280I2C.h:57
@ STB_TIME_1000
1000 ms
Definition BMP280I2C.h:61
void setSensorMode(Mode mode)
Sets the sensor mode.
Definition BMP280I2C.cpp:94
bool init() override
Initialize the device with the specified configuration.
Definition BMP280I2C.cpp:53
BMP280I2C(I2C &bus, BMP280Config config=BMP280_CONFIG_ALL_ENABLED)
Definition BMP280I2C.cpp:49
void setTemperatureOversampling(Oversampling oversampling)
Sets the oversampling for temperature readings, use SKIPPED to disable temperature sampling.
@ SLEEP_MODE
Sleep mode.
Definition BMP280I2C.h:49
@ FORCED_MODE
Forced mode.
Definition BMP280I2C.h:50
@ NORMAL_MODE
Normal mode.
Definition BMP280I2C.h:51
void setPressureOversampling(Oversampling oversampling)
Sets the oversampling for pressure readings, use SKIPPED to disable pressure sampling.
PressureData readPressure()
Reads only the pressure, does not set the configuration.
BMP280Data sampleImpl() override
Read a data sample from the sensor. In case of errors, the method should return the last available co...
static const BMP280Config BMP280_CONFIG_TEMP_SINGLE
Definition BMP280I2C.h:147
@ OVERSAMPLING_2
Oversampling x2.
Definition BMP280I2C.h:41
@ OVERSAMPLING_8
Oversampling x8.
Definition BMP280I2C.h:43
@ OVERSAMPLING_16
Oversampling x16.
Definition BMP280I2C.h:44
@ OVERSAMPLING_1
Oversampling x1.
Definition BMP280I2C.h:40
@ SKIPPED
Skipped (output set to 0x8000)
Definition BMP280I2C.h:39
@ OVERSAMPLING_4
Oversampling x4.
Definition BMP280I2C.h:42
void setStandbyTime(StandbyTime standbyTime)
Sets the standby time between readings in normal mode.
bool selfTest() override
Reads the WHO AM I register.
TemperatureData readTemperature()
Reads only the temperature, does not set the configuration.
static constexpr uint8_t REG_ID_VAL
Who am I value.
Definition BMP280I2C.h:138
static const BMP280Config BMP280_DEFAULT_CONFIG
Datasheet values for indoor navigation.
Definition BMP280I2C.h:141
unsigned int getMaxMeasurementTime()
static unsigned int calculateMaxMeasurementTime(BMP280Config config)
Maximum measurement time formula from datasheet page 51.
@ FILTER_OFF
Filter off.
Definition BMP280I2C.h:68
@ FILTER_COEFF_4
Filter coefficient = 4.
Definition BMP280I2C.h:70
@ FILTER_COEFF_2
Filter coefficient = 2.
Definition BMP280I2C.h:69
@ FILTER_COEFF_16
Filter coefficient = 16.
Definition BMP280I2C.h:72
@ FILTER_COEFF_8
Filter coefficient = 8.
Definition BMP280I2C.h:71
static const BMP280Config BMP280_CONFIG_ALL_ENABLED
Temperature enabled in forced mode.
Definition BMP280I2C.h:144
void setFilterCoeff(FilterCoeff filterCoeff)
Sets the coefficient for the IIR filter (applied to temperature and pressure)
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.
Configuration struct for a slave device. This will be used for configuring the bus in order to commun...
Definition I2CDriver.h:104
struct __attribute__((packed)) BMP280ConfigBits
Definition BMP280I2C.h:77
struct Boardcore::BMP280I2C::BMP280Config::@5 bytes
uint8_t config
Rate, filter and interface options.
Definition BMP280I2C.h:111
uint8_t status
Device status.
Definition BMP280I2C.h:108