ESPHome 2025.9.0-dev
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/log.h"
10#include "esphome/core/hal.h"
11#include <cinttypes>
12
13namespace esphome {
14namespace bmp3xx_base {
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
29static const LogString *oversampling_to_str(Oversampling oversampling) {
30 switch (oversampling) {
32 return LOG_STR("None");
34 return LOG_STR("2x");
36 return LOG_STR("4x");
38 return LOG_STR("8x");
40 return LOG_STR("16x");
42 return LOG_STR("32x");
43 default:
44 return LOG_STR("");
45 }
46}
47
48static const LogString *iir_filter_to_str(IIRFilter filter) {
49 switch (filter) {
51 return LOG_STR("OFF");
53 return LOG_STR("2x");
55 return LOG_STR("4x");
57 return LOG_STR("8x");
59 return LOG_STR("16x");
61 return LOG_STR("32x");
63 return LOG_STR("64x");
65 return LOG_STR("128x");
66 default:
67 return LOG_STR("");
68 }
69}
70
72 this->error_code_ = NONE;
73 // Call the Device base class "initialise" function
74 if (!reset()) {
75 ESP_LOGE(TAG, "Failed to reset");
76 this->error_code_ = ERROR_SENSOR_RESET;
77 this->mark_failed();
78 }
79
80 if (!read_byte(BMP388_CHIP_ID, &this->chip_id_.reg)) {
81 ESP_LOGE(TAG, "Can't read chip id");
82 this->error_code_ = ERROR_COMMUNICATION_FAILED;
83 this->mark_failed();
84 return;
85 }
86 ESP_LOGCONFIG(TAG, "Chip %s Id 0x%X", LOG_STR_ARG(chip_type_to_str(this->chip_id_.reg)), this->chip_id_.reg);
87
88 if (chip_id_.reg != BMP388_ID && chip_id_.reg != BMP390_ID) {
89 ESP_LOGE(TAG, "Unknown chip id - is this really a BMP388 or BMP390?");
90 this->error_code_ = ERROR_WRONG_CHIP_ID;
91 this->mark_failed();
92 return;
93 }
94 // set sensor in sleep mode
96 // Read the calibration parameters into the params structure
97 if (!read_bytes(BMP388_TRIM_PARAMS, (uint8_t *) &compensation_params_, sizeof(compensation_params_))) {
98 ESP_LOGE(TAG, "Can't read calibration data");
99 this->error_code_ = ERROR_COMMUNICATION_FAILED;
100 this->mark_failed();
101 return;
102 }
104 (float) compensation_params_.param_T1 / powf(2.0f, -8.0f); // Calculate the floating point trim parameters
105 compensation_float_params_.param_T2 = (float) compensation_params_.param_T2 / powf(2.0f, 30.0f);
106 compensation_float_params_.param_T3 = (float) compensation_params_.param_T3 / powf(2.0f, 48.0f);
107 compensation_float_params_.param_P1 = ((float) compensation_params_.param_P1 - powf(2.0f, 14.0f)) / powf(2.0f, 20.0f);
108 compensation_float_params_.param_P2 = ((float) compensation_params_.param_P2 - powf(2.0f, 14.0f)) / powf(2.0f, 29.0f);
109 compensation_float_params_.param_P3 = (float) compensation_params_.param_P3 / powf(2.0f, 32.0f);
110 compensation_float_params_.param_P4 = (float) compensation_params_.param_P4 / powf(2.0f, 37.0f);
111 compensation_float_params_.param_P5 = (float) compensation_params_.param_P5 / powf(2.0f, -3.0f);
112 compensation_float_params_.param_P6 = (float) compensation_params_.param_P6 / powf(2.0f, 6.0f);
113 compensation_float_params_.param_P7 = (float) compensation_params_.param_P7 / powf(2.0f, 8.0f);
114 compensation_float_params_.param_P8 = (float) compensation_params_.param_P8 / powf(2.0f, 15.0f);
115 compensation_float_params_.param_P9 = (float) compensation_params_.param_P9 / powf(2.0f, 48.0f);
116 compensation_float_params_.param_P10 = (float) compensation_params_.param_P10 / powf(2.0f, 48.0f);
117 compensation_float_params_.param_P11 = (float) compensation_params_.param_P11 / powf(2.0f, 65.0f);
118
119 // Initialise the BMP388 IIR filter register
120 if (!set_iir_filter(this->iir_filter_)) {
121 ESP_LOGE(TAG, "Failed to set IIR filter");
122 this->error_code_ = ERROR_COMMUNICATION_FAILED;
123 this->mark_failed();
124 return;
125 }
126
127 // Set power control registers
128 pwr_ctrl_.bit.press_en = 1;
129 pwr_ctrl_.bit.temp_en = 1;
130 // Disable pressure if no sensor defined
131 // keep temperature enabled since it's needed for compensation
132 if (this->pressure_sensor_ == nullptr) {
133 pwr_ctrl_.bit.press_en = 0;
135 }
136 // just disable oeversampling for temp if not used
137 if (this->temperature_sensor_ == nullptr) {
139 }
140 // Initialise the BMP388 oversampling register
142 ESP_LOGE(TAG, "Failed to set oversampling register");
143 this->error_code_ = ERROR_COMMUNICATION_FAILED;
144 this->mark_failed();
145 return;
146 }
147}
148
150 ESP_LOGCONFIG(TAG,
151 "BMP3XX:\n"
152 " Type: %s (0x%X)",
153 LOG_STR_ARG(chip_type_to_str(this->chip_id_.reg)), this->chip_id_.reg);
154 switch (this->error_code_) {
155 case NONE:
156 break;
158 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
159 break;
161 ESP_LOGE(TAG, "Wrong chip ID (reported id: 0x%X) - please check if you are really using a BMP 388 or BMP 390",
162 this->chip_id_.reg);
163 break;
165 ESP_LOGE(TAG, "Failed to reset");
166 break;
167 default:
168 ESP_LOGE(TAG, "Error code %d", (int) this->error_code_);
169 break;
170 }
171 ESP_LOGCONFIG(TAG, " IIR Filter: %s", LOG_STR_ARG(iir_filter_to_str(this->iir_filter_)));
172 LOG_UPDATE_INTERVAL(this);
173 if (this->temperature_sensor_) {
174 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
175 ESP_LOGCONFIG(TAG, " Oversampling: %s", LOG_STR_ARG(oversampling_to_str(this->temperature_oversampling_)));
176 }
177 if (this->pressure_sensor_) {
178 LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
179 ESP_LOGCONFIG(TAG, " Oversampling: %s", LOG_STR_ARG(oversampling_to_str(this->pressure_oversampling_)));
180 }
181}
183
184inline uint8_t oversampling_to_time(Oversampling over_sampling) { return (1 << uint8_t(over_sampling)); }
185
187 // Enable sensor
188 ESP_LOGV(TAG, "Sending conversion request");
189 float meas_time = 1.0f;
190 // Ref: https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp390-ds002.pdf 3.9.2
191 meas_time += 2.02f * oversampling_to_time(this->temperature_oversampling_) + 0.163f;
192 meas_time += 2.02f * oversampling_to_time(this->pressure_oversampling_) + 0.392f;
193 meas_time += 0.234f;
194 if (!set_mode(FORCED_MODE)) {
195 ESP_LOGE(TAG, "Failed start forced mode");
196 this->mark_failed();
197 return;
198 }
199
200 const uint32_t meas_timeout = uint32_t(ceilf(meas_time));
201 ESP_LOGVV(TAG, "measurement time %" PRIu32, meas_timeout);
202 this->set_timeout("data", meas_timeout, [this]() {
203 float temperature = 0.0f;
204 float pressure = 0.0f;
205 if (this->pressure_sensor_ != nullptr) {
206 if (!get_measurements(temperature, pressure)) {
207 ESP_LOGW(TAG, "Failed to read pressure and temperature - skipping update");
208 this->status_set_warning();
209 return;
210 }
211 ESP_LOGD(TAG, "Got temperature=%.1f°C pressure=%.1fhPa", temperature, pressure);
212 } else {
214 ESP_LOGW(TAG, "Failed to read temperature - skipping update");
215 this->status_set_warning();
216 return;
217 }
218 ESP_LOGD(TAG, "Got temperature=%.1f°C", temperature);
219 }
220
221 if (this->temperature_sensor_ != nullptr)
222 this->temperature_sensor_->publish_state(temperature);
223 if (this->pressure_sensor_ != nullptr)
224 this->pressure_sensor_->publish_state(pressure);
225 this->status_clear_warning();
227 });
228}
229
230// Reset the BMP3XX
232 write_byte(BMP388_CMD, RESET_CODE); // Write the reset code to the command register
233 // Wait for 10ms
234 delay(10);
235 this->read_byte(BMP388_EVENT, &event_.reg); // Read the BMP388's event register
236 return event_.bit.por_detected; // Return if device reset is complete
237}
238
239// Start a one shot measurement in FORCED_MODE
241 // Only set FORCED_MODE if we're already in SLEEP_MODE
242 if (pwr_ctrl_.bit.mode == SLEEP_MODE) {
243 return set_mode(FORCED_MODE);
244 }
245 return true;
246}
247
248// Stop the conversion and return to SLEEP_MODE
250
251// Set the pressure oversampling rate
253 osr_.bit.osr_p = oversampling;
254 return this->write_byte(BMP388_OSR, osr_.reg);
255}
256
257// Set the temperature oversampling rate
259 osr_.bit.osr_t = oversampling;
260 return this->write_byte(BMP388_OSR, osr_.reg);
261}
262
263// Set the IIR filter setting
265 config_.bit.iir_filter = iir_filter;
266 return this->write_byte(BMP388_CONFIG, config_.reg);
267}
268
269// Get temperature
271 // Check if a measurement is ready
272 if (!data_ready()) {
273 return false;
274 }
275 uint8_t data[3];
276 // Read the temperature
277 if (!this->read_bytes(BMP388_DATA_3, &data[0], 3)) {
278 ESP_LOGE(TAG, "Failed to read temperature");
279 return false;
280 }
281 // Copy the temperature data into the adc variables
282 int32_t adc_temp = (int32_t) data[2] << 16 | (int32_t) data[1] << 8 | (int32_t) data[0];
283 // Temperature compensation (function from BMP388 datasheet)
285 return true;
286}
287
288// Get the pressure
293
294// Get temperature and pressure
296 // Check if a measurement is ready
297 if (!data_ready()) {
298 ESP_LOGD(TAG, "Get measurement - data not ready skipping update");
299 return false;
300 }
301
302 uint8_t data[6];
303 // Read the temperature and pressure data
304 if (!this->read_bytes(BMP388_DATA_0, &data[0], 6)) {
305 ESP_LOGE(TAG, "Failed to read measurements");
306 return false;
307 }
308 // Copy the temperature and pressure data into the adc variables
309 int32_t adc_pres = (int32_t) data[2] << 16 | (int32_t) data[1] << 8 | (int32_t) data[0];
310 int32_t adc_temp = (int32_t) data[5] << 16 | (int32_t) data[4] << 8 | (int32_t) data[3];
311
312 // Temperature compensation (function from BMP388 datasheet)
314 // Pressure compensation (function from BMP388 datasheet)
316 // Calculate the pressure in millibar/hPa
317 pressure /= 100.0f;
318 return true;
319}
320
321// Set the BMP388's mode in the power control register
326
327// Set the BMP388 oversampling register
329 Oversampling temperature_oversampling) {
330 osr_.reg = temperature_oversampling << 3 | pressure_oversampling;
331 return this->write_byte(BMP388_OSR, osr_.reg);
332}
333
334// Check if measurement data is ready
336 // If we're in SLEEP_MODE return immediately
337 if (pwr_ctrl_.bit.mode == SLEEP_MODE) {
338 ESP_LOGD(TAG, "Not ready - sensor is in sleep mode");
339 return false;
340 }
341 // Read the interrupt status register
342 uint8_t status;
343 if (!this->read_byte(BMP388_INT_STATUS, &status)) {
344 ESP_LOGE(TAG, "Failed to read status register");
345 return false;
346 }
347 int_status_.reg = status;
348 ESP_LOGVV(TAG, "data ready status %d", status);
349 // If we're in FORCED_MODE switch back to SLEEP_MODE
350 if (int_status_.bit.drdy) {
351 if (pwr_ctrl_.bit.mode == FORCED_MODE) {
352 pwr_ctrl_.bit.mode = SLEEP_MODE;
353 }
354 return true; // The measurement is ready
355 }
356 return false; // The measurement is still pending
357}
358
360// Bosch BMP3XXComponent (Private) Member Functions
362
364 float partial_data1 = uncomp_temp - compensation_float_params_.param_T1;
365 float partial_data2 = partial_data1 * compensation_float_params_.param_T2;
366 return partial_data2 + partial_data1 * partial_data1 * compensation_float_params_.param_T3;
367}
368
369float BMP3XXComponent::bmp388_compensate_pressure_(float uncomp_press, float t_lin) {
370 float partial_data1 = compensation_float_params_.param_P6 * t_lin;
371 float partial_data2 = compensation_float_params_.param_P7 * t_lin * t_lin;
372 float partial_data3 = compensation_float_params_.param_P8 * t_lin * t_lin * t_lin;
373 float partial_out1 = compensation_float_params_.param_P5 + partial_data1 + partial_data2 + partial_data3;
374 partial_data1 = compensation_float_params_.param_P2 * t_lin;
375 partial_data2 = compensation_float_params_.param_P3 * t_lin * t_lin;
376 partial_data3 = compensation_float_params_.param_P4 * t_lin * t_lin * t_lin;
377 float partial_out2 =
378 uncomp_press * (compensation_float_params_.param_P1 + partial_data1 + partial_data2 + partial_data3);
379 partial_data1 = uncomp_press * uncomp_press;
381 partial_data3 = partial_data1 * partial_data2;
382 float partial_data4 =
383 partial_data3 + uncomp_press * uncomp_press * uncomp_press * compensation_float_params_.param_P11;
384 return partial_out1 + partial_out2 + partial_data4;
385}
386
387} // namespace bmp3xx_base
388} // namespace esphome
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
union esphome::bmp3xx_base::BMP3XXComponent::@26 event_
struct esphome::bmp3xx_base::BMP3XXComponent::FloatParams compensation_float_params_
bool get_pressure(float &pressure)
Get a pressure measurement.
bool get_measurements(float &temperature, float &pressure)
Get a temperature and pressure measurement.
enum esphome::bmp3xx_base::BMP3XXComponent::ErrorCode NONE
bool start_forced_conversion()
Start a one shot measurement in FORCED_MODE.
union esphome::bmp3xx_base::BMP3XXComponent::@25 chip_id_
union esphome::bmp3xx_base::BMP3XXComponent::@31 config_
bool set_iir_filter(IIRFilter iir_filter)
Set the IIR filter setting: OFF, 2, 3, 8, 16, 32.
bool set_pressure_oversampling(Oversampling pressure_oversampling)
Set the pressure oversampling: OFF, X1, X2, X4, X8, X16, X32.
union esphome::bmp3xx_base::BMP3XXComponent::@28 pwr_ctrl_
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)
float get_setup_priority() const override
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.
union esphome::bmp3xx_base::BMP3XXComponent::@27 int_status_
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
union esphome::bmp3xx_base::BMP3XXComponent::@29 osr_
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:45
uint8_t oversampling_to_time(Oversampling over_sampling)
Oversampling
Oversampling bit fields in the control and measurement register.
Definition bmp3xx_base.h:50
OperationMode
Device mode bitfield in the control and measurement register.
Definition bmp3xx_base.h:47
IIRFilter
Infinite Impulse Response (IIR) filter bit field in the configuration register.
Definition bmp3xx_base.h:60
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:50
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29
uint16_t temperature
Definition sun_gtil2.cpp:12
uint8_t pressure
Definition tt21100.cpp:7