ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
bmp3xx_base.cpp
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#include "bmp3xx_base.h"
9#include "esphome/core/hal.h"
11#include "esphome/core/log.h"
13#include <cinttypes>
14
16
17ESPHOME_LOG_TAG(TAG, "bmp3xx.sensor");
18
19static constexpr uint32_t DATA_TIMEOUT_ID = 0;
20
21static const LogString *chip_type_to_str(uint8_t chip_type) {
22 switch (chip_type) {
23 case BMP388_ID:
24 return LOG_STR("BMP 388");
25 case BMP390_ID:
26 return LOG_STR("BMP 390");
27 default:
28 return LOG_STR("Unknown Chip Type");
29 }
30}
31
32// Oversampling strings indexed by Oversampling enum (0-5): NONE, X2, X4, X8, X16, X32
33PROGMEM_STRING_TABLE(OversamplingStrings, "None", "2x", "4x", "8x", "16x", "32x", "");
34
35static const LogString *oversampling_to_str(Oversampling oversampling) {
36 return OversamplingStrings::get_log_str(static_cast<uint8_t>(oversampling), OversamplingStrings::LAST_INDEX);
37}
38
39// IIR filter strings indexed by IIRFilter enum (0-7): OFF, 2, 4, 8, 16, 32, 64, 128
40PROGMEM_STRING_TABLE(IIRFilterStrings, "OFF", "2x", "4x", "8x", "16x", "32x", "64x", "128x", "");
41
42static const LogString *iir_filter_to_str(IIRFilter filter) {
43 return IIRFilterStrings::get_log_str(static_cast<uint8_t>(filter), IIRFilterStrings::LAST_INDEX);
44}
45
47 // Call the Device base class "initialise" function
48 if (!reset()) {
49 this->mark_failed(LOG_STR("Failed to reset"));
50 return;
51 }
52
53 if (!read_byte(BMP388_CHIP_ID, &this->chip_id_.reg)) {
54 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
55 return;
56 }
57 ESP_LOGCONFIG(TAG, "Chip %s Id 0x%X", LOG_STR_ARG(chip_type_to_str(this->chip_id_.reg)), this->chip_id_.reg);
58
59 if (chip_id_.reg != BMP388_ID && chip_id_.reg != BMP390_ID) {
60 this->mark_failed(LOG_STR("Wrong chip ID - please check if you are really using a BMP 388 or BMP 390"));
61 return;
62 }
63 // set sensor in sleep mode
64 stop_conversion();
65 // Read the calibration parameters into the params structure
66 if (!read_bytes(BMP388_TRIM_PARAMS, (uint8_t *) &compensation_params_, sizeof(compensation_params_))) {
67 this->mark_failed(LOG_STR("Can't read calibration data"));
68 return;
69 }
71 (float) compensation_params_.param_T1 / powf(2.0f, -8.0f); // Calculate the floating point trim parameters
72 compensation_float_params_.param_T2 = (float) compensation_params_.param_T2 / powf(2.0f, 30.0f);
73 compensation_float_params_.param_T3 = (float) compensation_params_.param_T3 / powf(2.0f, 48.0f);
74 compensation_float_params_.param_P1 = ((float) compensation_params_.param_P1 - powf(2.0f, 14.0f)) / powf(2.0f, 20.0f);
75 compensation_float_params_.param_P2 = ((float) compensation_params_.param_P2 - powf(2.0f, 14.0f)) / powf(2.0f, 29.0f);
76 compensation_float_params_.param_P3 = (float) compensation_params_.param_P3 / powf(2.0f, 32.0f);
77 compensation_float_params_.param_P4 = (float) compensation_params_.param_P4 / powf(2.0f, 37.0f);
78 compensation_float_params_.param_P5 = (float) compensation_params_.param_P5 / powf(2.0f, -3.0f);
79 compensation_float_params_.param_P6 = (float) compensation_params_.param_P6 / powf(2.0f, 6.0f);
80 compensation_float_params_.param_P7 = (float) compensation_params_.param_P7 / powf(2.0f, 8.0f);
81 compensation_float_params_.param_P8 = (float) compensation_params_.param_P8 / powf(2.0f, 15.0f);
82 compensation_float_params_.param_P9 = (float) compensation_params_.param_P9 / powf(2.0f, 48.0f);
83 compensation_float_params_.param_P10 = (float) compensation_params_.param_P10 / powf(2.0f, 48.0f);
84 compensation_float_params_.param_P11 = (float) compensation_params_.param_P11 / powf(2.0f, 65.0f);
85
86 // Initialise the BMP388 IIR filter register
87 if (!set_iir_filter(this->iir_filter_)) {
88 this->mark_failed(LOG_STR("Failed to set IIR filter"));
89 return;
90 }
91
92 // Set power control registers
93 pwr_ctrl_.bit.press_en = 1;
94 pwr_ctrl_.bit.temp_en = 1;
95 // Disable pressure if no sensor defined
96 // keep temperature enabled since it's needed for compensation
97 if (this->pressure_sensor_ == nullptr) {
98 pwr_ctrl_.bit.press_en = 0;
100 }
101 // just disable oeversampling for temp if not used
102 if (this->temperature_sensor_ == nullptr) {
104 }
105 // Initialise the BMP388 oversampling register
106 if (!set_oversampling_register(this->pressure_oversampling_, this->temperature_oversampling_)) {
107 this->mark_failed(LOG_STR("Failed to set oversampling register"));
108 return;
109 }
110}
111
112void BMP3XXComponent::dump_config() {
113 ESP_LOGCONFIG(TAG,
114 "BMP3XX:\n"
115 " Type: %s (0x%X)",
116 LOG_STR_ARG(chip_type_to_str(this->chip_id_.reg)), this->chip_id_.reg);
117 ESP_LOGCONFIG(TAG, " IIR Filter: %s", LOG_STR_ARG(iir_filter_to_str(this->iir_filter_)));
118 LOG_UPDATE_INTERVAL(this);
119 if (this->temperature_sensor_) {
120 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
121 ESP_LOGCONFIG(TAG, " Oversampling: %s", LOG_STR_ARG(oversampling_to_str(this->temperature_oversampling_)));
122 }
123 if (this->pressure_sensor_) {
124 LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
125 ESP_LOGCONFIG(TAG, " Oversampling: %s", LOG_STR_ARG(oversampling_to_str(this->pressure_oversampling_)));
126 }
127}
128
129inline uint8_t oversampling_to_time(Oversampling over_sampling) { return (1 << uint8_t(over_sampling)); }
130
131void BMP3XXComponent::update() {
132 // Enable sensor
133 ESP_LOGV(TAG, "Sending conversion request");
134 float meas_time = 1.0f;
135 // Ref: https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp390-ds002.pdf 3.9.2
136 meas_time += 2.02f * oversampling_to_time(this->temperature_oversampling_) + 0.163f;
137 meas_time += 2.02f * oversampling_to_time(this->pressure_oversampling_) + 0.392f;
138 meas_time += 0.234f;
139 if (!set_mode(FORCED_MODE)) {
140 this->mark_failed(LOG_STR("Failed start forced mode"));
141 return;
142 }
143
144 const uint32_t meas_timeout = uint32_t(ceilf(meas_time));
145 ESP_LOGVV(TAG, "measurement time %" PRIu32, meas_timeout);
146 this->set_timeout(DATA_TIMEOUT_ID, meas_timeout, [this]() {
147 float temperature = 0.0f;
148 float pressure = 0.0f;
149 if (this->pressure_sensor_ != nullptr) {
150 if (!get_measurements(temperature, pressure)) {
151 ESP_LOGW(TAG, "Failed to read pressure and temperature - skipping update");
152 this->status_set_warning();
153 return;
154 }
155 ESP_LOGD(TAG, "Got temperature=%.1f°C pressure=%.1fhPa", temperature, pressure);
156 } else {
157 if (!get_temperature(temperature)) {
158 ESP_LOGW(TAG, "Failed to read temperature - skipping update");
159 this->status_set_warning();
160 return;
161 }
162 ESP_LOGD(TAG, "Got temperature=%.1f°C", temperature);
163 }
164
165 PUBLISH_STATE(this->temperature_sensor_, temperature);
166 PUBLISH_STATE(this->pressure_sensor_, pressure);
167 this->status_clear_warning();
168 set_mode(SLEEP_MODE);
169 });
170}
171
172// Reset the BMP3XX
173uint8_t BMP3XXComponent::reset() {
174 write_byte(BMP388_CMD, RESET_CODE); // Write the reset code to the command register
175 // Wait for 10ms
176 delay(10);
177 this->read_byte(BMP388_EVENT, &event_.reg); // Read the BMP388's event register
178 return event_.bit.por_detected; // Return if device reset is complete
179}
180
181// Start a one shot measurement in FORCED_MODE
182bool BMP3XXComponent::start_forced_conversion() {
183 // Only set FORCED_MODE if we're already in SLEEP_MODE
184 if (pwr_ctrl_.bit.mode == SLEEP_MODE) {
185 return set_mode(FORCED_MODE);
186 }
187 return true;
188}
189
190// Stop the conversion and return to SLEEP_MODE
191bool BMP3XXComponent::stop_conversion() { return set_mode(SLEEP_MODE); }
192
193// Set the pressure oversampling rate
194bool BMP3XXComponent::set_pressure_oversampling(Oversampling oversampling) {
195 osr_.bit.osr_p = oversampling;
196 return this->write_byte(BMP388_OSR, osr_.reg);
197}
198
199// Set the temperature oversampling rate
200bool BMP3XXComponent::set_temperature_oversampling(Oversampling oversampling) {
201 osr_.bit.osr_t = oversampling;
202 return this->write_byte(BMP388_OSR, osr_.reg);
203}
204
205// Set the IIR filter setting
206bool BMP3XXComponent::set_iir_filter(IIRFilter iir_filter) {
207 config_.bit.iir_filter = iir_filter;
208 return this->write_byte(BMP388_CONFIG, config_.reg);
209}
210
211// Get temperature
212bool BMP3XXComponent::get_temperature(float &temperature) {
213 // Check if a measurement is ready
214 if (!data_ready()) {
215 return false;
216 }
217 uint8_t data[3];
218 // Read the temperature
219 if (!this->read_bytes(BMP388_DATA_3, &data[0], 3)) {
220 ESP_LOGE(TAG, "Failed to read temperature");
221 return false;
222 }
223 // Copy the temperature data into the adc variables
224 int32_t adc_temp = encode_uint24(data[2], data[1], data[0]);
225 // Temperature compensation (function from BMP388 datasheet)
227 return true;
228}
229
230// Get the pressure
231bool BMP3XXComponent::get_pressure(float &pressure) {
232 float temperature;
233 return get_measurements(temperature, pressure);
234}
235
236// Get temperature and pressure
237bool BMP3XXComponent::get_measurements(float &temperature, float &pressure) {
238 // Check if a measurement is ready
239 if (!data_ready()) {
240 ESP_LOGD(TAG, "Get measurement - data not ready skipping update");
241 return false;
242 }
243
244 uint8_t data[6];
245 // Read the temperature and pressure data
246 if (!this->read_bytes(BMP388_DATA_0, &data[0], 6)) {
247 ESP_LOGE(TAG, "Failed to read measurements");
248 return false;
249 }
250 // Copy the temperature and pressure data into the adc variables
251 int32_t adc_pres = encode_uint24(data[2], data[1], data[0]);
252 int32_t adc_temp = encode_uint24(data[5], data[4], data[3]);
253
254 // Temperature compensation (function from BMP388 datasheet)
256 // Pressure compensation (function from BMP388 datasheet)
258 // Calculate the pressure in millibar/hPa
259 pressure /= 100.0f;
260 return true;
261}
262
263// Set the BMP388's mode in the power control register
264bool BMP3XXComponent::set_mode(OperationMode mode) {
265 pwr_ctrl_.bit.mode = mode;
266 return this->write_byte(BMP388_PWR_CTRL, pwr_ctrl_.reg);
267}
268
269// Set the BMP388 oversampling register
270bool BMP3XXComponent::set_oversampling_register(Oversampling pressure_oversampling,
271 Oversampling temperature_oversampling) {
272 osr_.reg = temperature_oversampling << 3 | pressure_oversampling;
273 return this->write_byte(BMP388_OSR, osr_.reg);
274}
275
276// Check if measurement data is ready
277bool BMP3XXComponent::data_ready() {
278 // If we're in SLEEP_MODE return immediately
279 if (pwr_ctrl_.bit.mode == SLEEP_MODE) {
280 ESP_LOGD(TAG, "Not ready - sensor is in sleep mode");
281 return false;
282 }
283 // Read the interrupt status register
284 uint8_t status;
285 if (!this->read_byte(BMP388_INT_STATUS, &status)) {
286 ESP_LOGE(TAG, "Failed to read status register");
287 return false;
288 }
289 int_status_.reg = status;
290 ESP_LOGVV(TAG, "data ready status %d", status);
291 // If we're in FORCED_MODE switch back to SLEEP_MODE
292 if (int_status_.bit.drdy) {
293 if (pwr_ctrl_.bit.mode == FORCED_MODE) {
294 pwr_ctrl_.bit.mode = SLEEP_MODE;
295 }
296 return true; // The measurement is ready
297 }
298 return false; // The measurement is still pending
299}
300
302// Bosch BMP3XXComponent (Private) Member Functions
304
306 float partial_data1 = uncomp_temp - compensation_float_params_.param_T1;
307 float partial_data2 = partial_data1 * compensation_float_params_.param_T2;
308 return partial_data2 + partial_data1 * partial_data1 * compensation_float_params_.param_T3;
309}
310
311float BMP3XXComponent::bmp388_compensate_pressure_(float uncomp_press, float t_lin) {
312 float partial_data1 = compensation_float_params_.param_P6 * t_lin;
313 float partial_data2 = compensation_float_params_.param_P7 * t_lin * t_lin;
314 float partial_data3 = compensation_float_params_.param_P8 * t_lin * t_lin * t_lin;
315 float partial_out1 = compensation_float_params_.param_P5 + partial_data1 + partial_data2 + partial_data3;
316 partial_data1 = compensation_float_params_.param_P2 * t_lin;
317 partial_data2 = compensation_float_params_.param_P3 * t_lin * t_lin;
318 partial_data3 = compensation_float_params_.param_P4 * t_lin * t_lin * t_lin;
319 float partial_out2 =
320 uncomp_press * (compensation_float_params_.param_P1 + partial_data1 + partial_data2 + partial_data3);
321 partial_data1 = uncomp_press * uncomp_press;
323 partial_data3 = partial_data1 * partial_data2;
324 float partial_data4 =
325 partial_data3 + uncomp_press * uncomp_press * uncomp_press * compensation_float_params_.param_P11;
326 return partial_out1 + partial_out2 + partial_data4;
327}
328
329} // namespace esphome::bmp3xx_base
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
void status_clear_warning()
Definition component.h:309
union esphome::bmp3xx_base::BMP3XXComponent::@24 chip_id_
union esphome::bmp3xx_base::BMP3XXComponent::@28 osr_
struct esphome::bmp3xx_base::BMP3XXComponent::FloatParams compensation_float_params_
union esphome::bmp3xx_base::BMP3XXComponent::@26 int_status_
union esphome::bmp3xx_base::BMP3XXComponent::@27 pwr_ctrl_
float bmp388_compensate_temperature_(float uncomp_temp)
virtual bool write_byte(uint8_t a_register, uint8_t data)=0
virtual bool read_bytes(uint8_t a_register, uint8_t *data, size_t len)=0
union esphome::bmp3xx_base::BMP3XXComponent::@30 config_
union esphome::bmp3xx_base::BMP3XXComponent::@25 event_
virtual bool read_byte(uint8_t a_register, uint8_t *data)=0
float bmp388_compensate_pressure_(float uncomp_press, float t_lin)
uint8_t oversampling_to_time(Oversampling over_sampling)
Oversampling
Oversampling bit fields in the control and measurement register.
Definition bmp3xx_base.h:49
ESPHOME_LOG_TAG(TAG, "bmp3xx.sensor")
OperationMode
Device mode bitfield in the control and measurement register.
Definition bmp3xx_base.h:46
PROGMEM_STRING_TABLE(OversamplingStrings, "None", "2x", "4x", "8x", "16x", "32x", "")
IIRFilter
Infinite Impulse Response (IIR) filter bit field in the configuration register.
Definition bmp3xx_base.h:59
FLAG_HAS_TRANSITION float
std::span< const uint8_t > data
constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3)
Encode a 24-bit value given three bytes in most to least significant byte order.
Definition helpers.h:1047
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12
uint8_t pressure
Definition tt21100.cpp:7