Skyward boardcore
Loading...
Searching...
No Matches
BMP280.h
Go to the documentation of this file.
1/* Copyright (c) 2022 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 "BMP280Data.h"
30
31namespace Boardcore
32{
33
34class BMP280 : 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 BMP280(SPISlave spiSlave,
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 void 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 = 0x08,
250 // Calibration register 1-25
251
252 REG_ID = 0x50,
253 REG_RESET = 0x60,
254
255 REG_STATUS = 0x73,
256 REG_CTRL_MEAS = 0x74,
257 REG_CONFIG = 0x75,
258
259 REG_PRESS_MSB = 0x77,
260 REG_PRESS_LSB = 0x78,
261 REG_PRESS_XLSB = 0x79,
262 REG_TEMP_MSB = 0x7A,
263 REG_TEMP_LSB = 0x7B,
264 REG_TEMP_XLSB = 0x7C
265 };
266
267 const SPISlave spiSlave;
268 BMP280Config config;
269 BMP280Comp compParams;
270 int32_t fineTemperature; // Used in compensation algorithm
271
272 PrintLogger logger = Logging::getLogger("bmp280");
273};
274
275} // namespace Boardcore
BMP280(SPISlave spiSlave, BMP280Config config=BMP280_CONFIG_ALL_ENABLED)
Definition BMP280.cpp:48
void setStandbyTime(StandbyTime standbyTime)
Sets the standby time between readings in normal mode.
Definition BMP280.cpp:121
void setSensorMode(Mode mode)
Sets the sensor mode.
Definition BMP280.cpp:93
bool init() override
Initialize the device with the specified configuration.
Definition BMP280.cpp:53
PressureData readPressure()
Reads only the pressure, does not set the configuration.
Definition BMP280.cpp:128
static const BMP280Config BMP280_CONFIG_ALL_ENABLED
Temperature enabled in forced mode.
Definition BMP280.h:144
@ STB_TIME_0_5
0.5 ms
Definition BMP280.h:56
@ STB_TIME_125
125 ms
Definition BMP280.h:58
@ STB_TIME_250
250 ms
Definition BMP280.h:59
@ STB_TIME_62_5
62.5 ms
Definition BMP280.h:57
@ STB_TIME_500
500 ms
Definition BMP280.h:60
@ STB_TIME_40
40 ms
Definition BMP280.h:63
@ STB_TIME_20
20 ms
Definition BMP280.h:62
@ STB_TIME_1000
1000 ms
Definition BMP280.h:61
@ FILTER_OFF
Filter off.
Definition BMP280.h:68
@ FILTER_COEFF_4
Filter coefficient = 4.
Definition BMP280.h:70
@ FILTER_COEFF_2
Filter coefficient = 2.
Definition BMP280.h:69
@ FILTER_COEFF_16
Filter coefficient = 16.
Definition BMP280.h:72
@ FILTER_COEFF_8
Filter coefficient = 8.
Definition BMP280.h:71
BMP280Data sampleImpl() override
Read a data sample from the sensor. In case of errors, the method should return the last available co...
Definition BMP280.cpp:189
TemperatureData readTemperature()
Reads only the temperature, does not set the configuration.
Definition BMP280.cpp:150
static constexpr uint8_t REG_ID_VAL
Who am I value.
Definition BMP280.h:138
@ SLEEP_MODE
Sleep mode.
Definition BMP280.h:49
@ FORCED_MODE
Forced mode.
Definition BMP280.h:50
@ NORMAL_MODE
Normal mode.
Definition BMP280.h:51
void setTemperatureOversampling(Oversampling oversampling)
Sets the oversampling for temperature readings, use SKIPPED to disable temperature sampling.
Definition BMP280.cpp:107
static unsigned int calculateMaxMeasurementTime(BMP280Config config)
Maximum measurement time formula from datasheet page 51.
Definition BMP280.cpp:174
static const BMP280Config BMP280_CONFIG_TEMP_SINGLE
Definition BMP280.h:147
void setFilterCoeff(FilterCoeff filterCoeff)
Sets the coefficient for the IIR filter (applied to temperature and pressure)
Definition BMP280.cpp:114
unsigned int getMaxMeasurementTime()
Definition BMP280.cpp:182
@ OVERSAMPLING_1
Oversampling x1.
Definition BMP280.h:40
@ OVERSAMPLING_8
Oversampling x8.
Definition BMP280.h:43
@ SKIPPED
Skipped (output set to 0x8000)
Definition BMP280.h:39
@ OVERSAMPLING_16
Oversampling x16.
Definition BMP280.h:44
@ OVERSAMPLING_4
Oversampling x4.
Definition BMP280.h:42
@ OVERSAMPLING_2
Oversampling x2.
Definition BMP280.h:41
void setPressureOversampling(Oversampling oversampling)
Sets the oversampling for pressure readings, use SKIPPED to disable pressure sampling.
Definition BMP280.cpp:100
bool selfTest() override
Reads the WHO AM I register.
Definition BMP280.cpp:187
static const BMP280Config BMP280_DEFAULT_CONFIG
Datasheet values for indoor navigation.
Definition BMP280.h:141
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.
Contains information about a single SPI slave device.
struct __attribute__((packed))
Definition BMP280.h:119
struct Boardcore::BMP280::BMP280Config::@4 bytes
struct __attribute__((packed)) BMP280ConfigBits
Definition BMP280.h:77
uint8_t status
Device status.
Definition BMP280.h:108
uint8_t config
Rate, filter and interface options.
Definition BMP280.h:111