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