ESPHome  2024.4.0
bmp3xx.h
Go to the documentation of this file.
1 /*
2  based on BMP388_DEV by Martin Lindupp
3  under MIT License (MIT)
4  Copyright (C) Martin Lindupp 2020
5  http://github.com/MartinL1/BMP388_DEV
6 */
7 
8 #pragma once
9 
10 #include "esphome/core/component.h"
13 
14 namespace esphome {
15 namespace bmp3xx {
16 
17 static const uint8_t BMP388_ID = 0x50; // The BMP388 device ID
18 static const uint8_t BMP390_ID = 0x60; // The BMP390 device ID
19 static const uint8_t RESET_CODE = 0xB6; // The BMP388 reset code
20 
22 enum {
23  BMP388_CHIP_ID = 0x00, // Chip ID register sub-address
24  BMP388_ERR_REG = 0x02, // Error register sub-address
25  BMP388_STATUS = 0x03, // Status register sub-address
26  BMP388_DATA_0 = 0x04, // Pressure eXtended Least Significant Byte (XLSB) register sub-address
27  BMP388_DATA_1 = 0x05, // Pressure Least Significant Byte (LSB) register sub-address
28  BMP388_DATA_2 = 0x06, // Pressure Most Significant Byte (MSB) register sub-address
29  BMP388_DATA_3 = 0x07, // Temperature eXtended Least Significant Byte (XLSB) register sub-address
30  BMP388_DATA_4 = 0x08, // Temperature Least Significant Byte (LSB) register sub-address
31  BMP388_DATA_5 = 0x09, // Temperature Most Significant Byte (MSB) register sub-address
32  BMP388_SENSORTIME_0 = 0x0C, // Sensor time register 0 sub-address
33  BMP388_SENSORTIME_1 = 0x0D, // Sensor time register 1 sub-address
34  BMP388_SENSORTIME_2 = 0x0E, // Sensor time register 2 sub-address
35  BMP388_EVENT = 0x10, // Event register sub-address
36  BMP388_INT_STATUS = 0x11, // Interrupt Status register sub-address
37  BMP388_INT_CTRL = 0x19, // Interrupt Control register sub-address
38  BMP388_IF_CONFIG = 0x1A, // Interface Configuration register sub-address
39  BMP388_PWR_CTRL = 0x1B, // Power Control register sub-address
40  BMP388_OSR = 0x1C, // Oversampling register sub-address
41  BMP388_ODR = 0x1D, // Output Data Rate register sub-address
42  BMP388_CONFIG = 0x1F, // Configuration register sub-address
43  BMP388_TRIM_PARAMS = 0x31, // Trim parameter registers' base sub-address
44  BMP388_CMD = 0x7E // Command register sub-address
45 };
46 
48 enum OperationMode { SLEEP_MODE = 0x00, FORCED_MODE = 0x01, NORMAL_MODE = 0x03 };
49 
58 };
59 
61 enum IIRFilter {
63  IIR_FILTER_2 = 0x01,
64  IIR_FILTER_4 = 0x02,
65  IIR_FILTER_8 = 0x03,
66  IIR_FILTER_16 = 0x04,
67  IIR_FILTER_32 = 0x05,
68  IIR_FILTER_64 = 0x06,
70 };
71 
74  public:
75  void setup() override;
76  void dump_config() override;
77  float get_setup_priority() const override;
78  void update() override;
79 
80  void set_temperature_sensor(sensor::Sensor *temperature_sensor) { temperature_sensor_ = temperature_sensor; }
81  void set_pressure_sensor(sensor::Sensor *pressure_sensor) { pressure_sensor_ = pressure_sensor; }
82 
84  void set_temperature_oversampling_config(Oversampling temperature_oversampling) {
85  this->temperature_oversampling_ = temperature_oversampling;
86  }
88  void set_pressure_oversampling_config(Oversampling pressure_oversampling) {
89  this->pressure_oversampling_ = pressure_oversampling;
90  }
93 
95  uint8_t reset();
101  bool stop_conversion();
103  bool set_pressure_oversampling(Oversampling pressure_oversampling);
105  bool set_temperature_oversampling(Oversampling temperature_oversampling);
109  bool get_temperature(float &temperature);
111  bool get_pressure(float &pressure);
113  bool get_measurements(float &temperature, float &pressure);
115  bool get_measurement();
119  bool set_oversampling_register(Oversampling pressure_oversampling, Oversampling temperature_oversampling);
121  bool data_ready();
122 
123  protected:
130  enum ErrorCode {
131  NONE = 0,
136  } error_code_{NONE};
138  struct { // The BMP388 compensation trim parameters (coefficients)
139  uint16_t param_T1;
140  uint16_t param_T2;
141  int8_t param_T3;
142  int16_t param_P1;
143  int16_t param_P2;
144  int8_t param_P3;
145  int8_t param_P4;
146  uint16_t param_P5;
147  uint16_t param_P6;
148  int8_t param_P7;
149  int8_t param_P8;
150  int16_t param_P9;
151  int8_t param_P10;
152  int8_t param_P11;
153  } __attribute__((packed)) compensation_params_;
154 
155  struct FloatParams { // The BMP388 float point compensation trim parameters
156  float param_T1;
157  float param_T2;
158  float param_T3;
159  float param_P1;
160  float param_P2;
161  float param_P3;
162  float param_P4;
163  float param_P5;
164  float param_P6;
165  float param_P7;
166  float param_P8;
167  float param_P9;
168  float param_P10;
169  float param_P11;
171 
172  union { // Copy of the BMP388's chip id register
173  struct {
174  uint8_t chip_id_nvm : 4;
175  uint8_t chip_id_fixed : 4;
176  } bit;
177  uint8_t reg;
178  } chip_id_ = {.reg = 0};
179 
180  union { // Copy of the BMP388's event register
181  struct {
182  uint8_t por_detected : 1;
183  } bit;
184  uint8_t reg;
185  } event_ = {.reg = 0};
186 
187  union { // Copy of the BMP388's interrupt status register
188  struct {
189  uint8_t fwm_int : 1;
190  uint8_t ffull_int : 1;
191  uint8_t : 1;
192  uint8_t drdy : 1;
193  } bit;
194  uint8_t reg;
195  } int_status_ = {.reg = 0};
196 
197  union { // Copy of the BMP388's power control register
198  struct {
199  uint8_t press_en : 1;
200  uint8_t temp_en : 1;
201  uint8_t : 2;
202  uint8_t mode : 2;
203  } bit;
204  uint8_t reg;
205  } pwr_ctrl_ = {.reg = 0};
206 
207  union { // Copy of the BMP388's oversampling register
208  struct {
209  uint8_t osr_p : 3;
210  uint8_t osr_t : 3;
211  } bit;
212  uint8_t reg;
213  } osr_ = {.reg = 0};
214 
215  union { // Copy of the BMP388's output data rate register
216  struct {
217  uint8_t odr_sel : 5;
218  } bit;
219  uint8_t reg;
220  } odr_ = {.reg = 0};
221 
222  union { // Copy of the BMP388's configuration register
223  struct {
224  uint8_t : 1;
225  uint8_t iir_filter : 3;
226  } bit;
227  uint8_t reg;
228  } config_ = {.reg = 0};
229 
230  // Bosch temperature compensation function
231  float bmp388_compensate_temperature_(float uncomp_temp);
232  // Bosch pressure compensation function
233  float bmp388_compensate_pressure_(float uncomp_press, float t_lin);
234 };
235 
236 } // namespace bmp3xx
237 } // namespace esphome
union esphome::bmp3xx::BMP3XXComponent::@27 pwr_ctrl_
bool set_iir_filter(IIRFilter iir_filter)
Set the IIR filter setting: OFF, 2, 3, 8, 16, 32.
Definition: bmp3xx.cpp:266
OperationMode
Device mode bitfield in the control and measurement register.
Definition: bmp3xx.h:48
Oversampling temperature_oversampling_
Definition: bmp3xx.h:124
struct esphome::bmp3xx::BMP3XXComponent::@24::@31 bit
float bmp388_compensate_temperature_(float uncomp_temp)
Definition: bmp3xx.cpp:365
uint8_t pressure
Definition: tt21100.cpp:19
bool set_mode(OperationMode mode)
Set the barometer mode.
Definition: bmp3xx.cpp:324
union esphome::bmp3xx::BMP3XXComponent::@26 int_status_
enum esphome::bmp3xx::BMP3XXComponent::ErrorCode NONE
union esphome::bmp3xx::BMP3XXComponent::@24 chip_id_
bool set_oversampling_register(Oversampling pressure_oversampling, Oversampling temperature_oversampling)
Set the BMP388 oversampling register.
Definition: bmp3xx.cpp:330
union esphome::bmp3xx::BMP3XXComponent::@30 config_
uint8_t reset()
Soft reset the sensor.
Definition: bmp3xx.cpp:233
OperationMode operation_mode_
Definition: bmp3xx.h:127
float get_setup_priority() const override
Definition: bmp3xx.cpp:184
struct esphome::bmp3xx::BMP3XXComponent::@23 __attribute__((packed)) compensation_params_
This class simplifies creating components that periodically check a state.
Definition: component.h:283
void set_temperature_oversampling_config(Oversampling temperature_oversampling)
Set the oversampling value for the temperature sensor. Default is 16x.
Definition: bmp3xx.h:84
void set_pressure_sensor(sensor::Sensor *pressure_sensor)
Definition: bmp3xx.h:81
This class implements support for the BMP3XX Temperature+Pressure i2c sensor.
Definition: bmp3xx.h:73
bool get_measurements(float &temperature, float &pressure)
Get a temperature and pressure measurement.
Definition: bmp3xx.cpp:297
bool get_temperature(float &temperature)
Get a temperature measurement.
Definition: bmp3xx.cpp:272
bool get_pressure(float &pressure)
Get a pressure measurement.
Definition: bmp3xx.cpp:291
void set_iir_filter_config(IIRFilter iir_filter)
Set the IIR Filter used to increase accuracy, defaults to no IIR Filter.
Definition: bmp3xx.h:92
union esphome::bmp3xx::BMP3XXComponent::@29 odr_
uint16_t temperature
Definition: sun_gtil2.cpp:26
sensor::Sensor * pressure_sensor_
Definition: bmp3xx.h:129
bool get_measurement()
Get a temperature and pressure measurement.
float bmp388_compensate_pressure_(float uncomp_press, float t_lin)
Definition: bmp3xx.cpp:371
bool data_ready()
Checks if a measurement is ready.
Definition: bmp3xx.cpp:337
bool set_pressure_oversampling(Oversampling pressure_oversampling)
Set the pressure oversampling: OFF, X1, X2, X4, X8, X16, X32.
Definition: bmp3xx.cpp:254
bool set_temperature_oversampling(Oversampling temperature_oversampling)
Set the temperature oversampling: OFF, X1, X2, X4, X8, X16, X32.
Definition: bmp3xx.cpp:260
sensor::Sensor * temperature_sensor_
Definition: bmp3xx.h:128
union esphome::bmp3xx::BMP3XXComponent::@28 osr_
bool stop_conversion()
Stop the conversion and return to SLEEP_MODE.
Definition: bmp3xx.cpp:251
bool start_forced_conversion()
Start a one shot measurement in FORCED_MODE.
Definition: bmp3xx.cpp:242
struct esphome::bmp3xx::BMP3XXComponent::FloatParams compensation_float_params_
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
bool start_normal_conversion()
Start continuous measurement in NORMAL_MODE.
union esphome::bmp3xx::BMP3XXComponent::@25 event_
Base-class for all sensors.
Definition: sensor.h:57
Oversampling pressure_oversampling_
Definition: bmp3xx.h:125
This Class provides the methods to read/write bytes from/to an i2c device.
Definition: i2c.h:133
void set_pressure_oversampling_config(Oversampling pressure_oversampling)
Set the oversampling value for the pressure sensor. Default is 16x.
Definition: bmp3xx.h:88
void set_temperature_sensor(sensor::Sensor *temperature_sensor)
Definition: bmp3xx.h:80
Oversampling
Oversampling bit fields in the control and measurement register.
Definition: bmp3xx.h:51
IIRFilter
Infinite Impulse Response (IIR) filter bit field in the configuration register.
Definition: bmp3xx.h:61