ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
bmp581_base.cpp
Go to the documentation of this file.
1/*
2 * Adds support for Bosch's BMP581 high accuracy pressure and temperature sensor
3 * - Component structure based on ESPHome's BMP3XX component (as of March, 2023)
4 * - Implementation is easier as the sensor itself automatically compensates pressure for the temperature
5 * - Temperature and pressure data is converted via simple division operations in this component
6 * - IIR filter level can independently be applied to temperature and pressure measurements
7 * - Bosch's BMP5-Sensor-API was consulted to verify that sensor configuration is done correctly
8 * - Copyright (c) 2022 Bosch Sensortec Gmbh, SPDX-License-Identifier: BSD-3-Clause
9 * - This component uses forced power mode only so measurements are synchronized by the host
10 * - All datasheet page references refer to Bosch Document Number BST-BMP581-DS004-04 (revision number 1.4)
11 */
12
13#include "bmp581_base.h"
14#include "esphome/core/hal.h"
16#include "esphome/core/log.h"
18
20
21ESPHOME_LOG_TAG(TAG, "bmp581");
22
23static constexpr uint32_t MEASUREMENT_TIMEOUT_ID = 0;
24
25// Oversampling strings indexed by Oversampling enum (0-7): NONE, X2, X4, X8, X16, X32, X64, X128
26PROGMEM_STRING_TABLE(OversamplingStrings, "None", "2x", "4x", "8x", "16x", "32x", "64x", "128x", "");
27
28// IIR filter strings indexed by IIRFilter enum (0-7): OFF, 2, 4, 8, 16, 32, 64, 128
29PROGMEM_STRING_TABLE(IIRFilterStrings, "OFF", "2x", "4x", "8x", "16x", "32x", "64x", "128x", "");
30
32 ESP_LOGCONFIG(TAG, "BMP581:");
33
34 LOG_UPDATE_INTERVAL(this);
35
36 ESP_LOGCONFIG(TAG, " Measurement conversion time: %ums", this->conversion_time_);
37
38 if (this->temperature_sensor_) {
39 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
40 ESP_LOGCONFIG(TAG,
41 " IIR Filter: %s\n"
42 " Oversampling: %s",
43 LOG_STR_ARG(IIRFilterStrings::get_log_str(this->iir_temperature_level_)),
44 LOG_STR_ARG(OversamplingStrings::get_log_str(this->temperature_oversampling_)));
45 }
46
47 if (this->pressure_sensor_) {
48 LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
49 ESP_LOGCONFIG(TAG,
50 " IIR Filter: %s\n"
51 " Oversampling: %s",
52 LOG_STR_ARG(IIRFilterStrings::get_log_str(this->iir_pressure_level_)),
53 LOG_STR_ARG(OversamplingStrings::get_log_str(this->pressure_oversampling_)));
54 }
55}
56
57void BMP581Component::setup() {
58 /*
59 * Setup goes through several stages, which follows the post-power-up procedure (page 18 of datasheet) and then sets
60 * configured options
61 * 1) Soft reboot
62 * 2) Verify ASIC chip ID matches BMP581
63 * 3) Verify sensor status (check if NVM is okay)
64 * 4) Enable data ready interrupt
65 * 5) Write oversampling settings and set internal configuration values
66 * 6) Configure and prime IIR Filter(s), if enabled
67 */
68
70 // 1) Soft reboot //
72
73 // Power-On-Reboot bit is asserted if sensor successfully reset
74 if (!this->reset_()) {
75 this->mark_failed(LOG_STR("Reset failed"));
76
77 return;
78 }
79
81 // 2) Verify ASIC chip ID matches BMP581 //
83
84 uint8_t chip_id;
85
86 // read chip id from sensor
87 if (!this->bmp_read_byte(BMP581_CHIP_ID, &chip_id)) {
88 this->mark_failed(LOG_STR("Read chip ID failed"));
89
90 return;
91 }
92
93 // verify id
94 if (chip_id != BMP581_ASIC_ID && chip_id != BMP585_ASIC_ID) {
95 this->mark_failed(LOG_STR("Unknown chip ID"));
96
97 return;
98 }
99
101 // 3) Verify sensor status (check if NVM is okay) //
103
104 if (!this->bmp_read_byte(BMP581_STATUS, &this->status_.reg)) {
105 this->mark_failed(LOG_STR("Failed to read status register"));
106
107 return;
108 }
109
110 // verify status_nvm_rdy bit (it is asserted if boot was successful)
111 if (!(this->status_.bit.status_nvm_rdy)) {
112 this->mark_failed(LOG_STR("NVM not ready"));
113
114 return;
115 }
116
117 // verify status_nvm_err bit (it is asserted if an error is detected)
118 if (this->status_.bit.status_nvm_err) {
119 this->mark_failed(LOG_STR("NVM error detected"));
120
121 return;
122 }
123
125 // 4) Enable data ready interrupt //
127
128 // enable the data ready interrupt source
129 if (!this->write_interrupt_source_settings_(true)) {
130 this->mark_failed(LOG_STR("Failed to write interrupt source register"));
131
132 return;
133 }
134
136 // 5) Write oversampling settings and set internal configuration values //
138
139 // configure pressure readings, if sensor is defined
140 // otherwise, disable pressure oversampling
141 if (this->pressure_sensor_) {
142 this->osr_config_.bit.press_en = true;
143 } else {
145 }
146
147 // write oversampling settings
149 this->mark_failed(LOG_STR("Failed to write oversampling register"));
150
151 return;
152 }
153
154 // set output data rate to 4 Hz=0x19 (page 65 of datasheet)
155 // - ?shouldn't? matter as this component only uses FORCED_MODE - datasheet is ambiguous
156 // - If in NORMAL_MODE or NONSTOP_MODE, then this would still allow deep standby to save power
157 // - will be written to BMP581 at next requested measurement
158 this->odr_config_.bit.odr = 0x19;
159
163
166 this->mark_failed(LOG_STR("Failed to write IIR configuration registers"));
167
168 return;
169 }
170
171 if (!this->prime_iir_filter_()) {
172 this->mark_failed(LOG_STR("Failed to prime the IIR filter with an initial measurement"));
173
174 return;
175 }
176 }
177}
178
179void BMP581Component::update() {
180 /*
181 * Each update goes through several stages
182 * 0) Verify either a temperature or pressure sensor is defined before proceeding
183 * 1) Request a measurement
184 * 2) Wait for measurement to finish (based on oversampling rates)
185 * 3) Read data registers for temperature and pressure, if applicable
186 * 4) Publish measurements to sensor(s), if applicable
187 */
188
190 // 0) Verify either a temperature or pressure sensor is defined before proceeding //
192
193 if ((!this->temperature_sensor_) && (!this->pressure_sensor_)) {
194 return;
195 }
196
198 // 1) Request a measurement //
200
201 ESP_LOGVV(TAG, "Requesting measurement");
202
203 if (!this->start_measurement_()) {
204 ESP_LOGW(TAG, "Requesting forced measurement failed");
205 this->status_set_warning();
206
207 return;
208 }
209
211 // 2) Wait for measurement to finish (based on oversampling rates) //
213
214 ESP_LOGVV(TAG, "Measurement should take %d ms", this->conversion_time_);
215
216 this->set_timeout(MEASUREMENT_TIMEOUT_ID, this->conversion_time_, [this]() {
217 float temperature = 0.0;
218 float pressure = 0.0;
219
221 // 3) Read data registers for temperature and pressure, if applicable //
223
224 if (this->pressure_sensor_) {
225 if (!this->read_temperature_and_pressure_(temperature, pressure)) {
226 ESP_LOGW(TAG, "Failed to read temperature and pressure; skipping update");
227 this->status_set_warning();
228
229 return;
230 }
231 } else {
232 if (!this->read_temperature_(temperature)) {
233 ESP_LOGW(TAG, "Failed to read temperature; skipping update");
234 this->status_set_warning();
235
236 return;
237 }
238 }
239
241 // 4) Publish measurements to sensor(s), if applicable //
243
244 PUBLISH_STATE(this->temperature_sensor_, temperature);
245 PUBLISH_STATE(this->pressure_sensor_, pressure);
246
247 this->status_clear_warning();
248 });
249}
250
252 // - verifies component is not internally in standby mode
253 // - reads interrupt status register
254 // - checks if data ready bit is asserted
255 // - If true, then internally sets component to standby mode if in forced mode
256 // - returns data readiness state
257
258 if (this->odr_config_.bit.pwr_mode == STANDBY_MODE) {
259 ESP_LOGD(TAG, "Data not ready, sensor is in standby mode");
260 return false;
261 }
262
263 uint8_t status;
264
265 if (!this->bmp_read_byte(BMP581_INT_STATUS, &status)) {
266 ESP_LOGE(TAG, "Failed to read interrupt status register");
267 return false;
268 }
269
270 this->int_status_.reg = status;
271
272 if (this->int_status_.bit.drdy_data_reg) {
273 // If in forced mode, then set internal record of the power mode to STANDBY_MODE
274 // - sensor automatically returns to standby mode after completing a forced measurement
275 if (this->odr_config_.bit.pwr_mode == FORCED_MODE) {
276 this->odr_config_.bit.pwr_mode = STANDBY_MODE;
277 }
278
279 return true;
280 }
281
282 return false;
283}
284
286 // - temporarily disables oversampling for a fast initial measurement; avoids slowing down ESPHome's startup process
287 // - enables IIR filter flushing with forced measurements
288 // - forces a measurement; flushing the IIR filter and priming it with a current value
289 // - disables IIR filter flushing with forced measurements
290 // - reverts to internally configured oversampling rates
291 // - returns success of all register writes/priming
292
293 // store current internal oversampling settings to revert to after priming
294 Oversampling current_temperature_oversampling = (Oversampling) this->osr_config_.bit.osr_t;
295 Oversampling current_pressure_oversampling = (Oversampling) this->osr_config_.bit.osr_p;
296
297 // temporarily disables oversampling for temperature and pressure for a fast priming measurement
299 ESP_LOGE(TAG, "Failed to write oversampling register");
300
301 return false;
302 }
303
304 // flush the IIR filter with forced measurements (we will only flush once)
305 this->dsp_config_.bit.iir_flush_forced_en = true;
306 if (!this->bmp_write_byte(BMP581_DSP, this->dsp_config_.reg)) {
307 ESP_LOGE(TAG, "Failed to write IIR source register");
308
309 return false;
310 }
311
312 // forces an initial measurement
313 // - this measurements flushes the IIR filter reflecting written DSP settings
314 // - flushing with this initial reading avoids having the internal previous data acquisition being 0, which
315 // (I)nfinitely affects future values
316 if (!this->start_measurement_()) {
317 ESP_LOGE(TAG, "Failed to request a forced measurement");
318
319 return false;
320 }
321
322 // wait for priming measurement to complete
323 // - with oversampling disabled, the conversion time for a single measurement for pressure and temperature is
324 // ceilf(1.05*(1.0+1.0)) = 3ms
325 // - see page 12 of datasheet for details
326 delay(3);
327
328 if (!this->check_data_readiness_()) {
329 ESP_LOGE(TAG, "IIR priming measurement was not ready");
330
331 return false;
332 }
333
334 // disable IIR filter flushings on future forced measurements
335 this->dsp_config_.bit.iir_flush_forced_en = false;
336 if (!this->bmp_write_byte(BMP581_DSP, this->dsp_config_.reg)) {
337 ESP_LOGE(TAG, "Failed to write IIR source register");
338
339 return false;
340 }
341
342 // revert oversampling rates to original settings
343 return this->write_oversampling_settings_(current_temperature_oversampling, current_pressure_oversampling);
344}
345
347 // - verifies data is ready to be read
348 // - reads in 3 bytes of temperature data
349 // - returns whether successful, where the the variable parameter contains
350 // - the measured temperature (in degrees Celsius)
351
352 if (!this->check_data_readiness_()) {
353 ESP_LOGW(TAG, "Data not ready, skipping this update");
354 this->status_set_warning();
355
356 return false;
357 }
358
359 uint8_t data[3];
360 if (!this->bmp_read_bytes(BMP581_MEASUREMENT_DATA, &data[0], 3)) {
361 ESP_LOGW(TAG, "Failed to read measurement");
362 this->status_set_warning();
363
364 return false;
365 }
366
367 // temperature MSB is in data[2], LSB is in data[1], XLSB in data[0]
368 int32_t raw_temp = static_cast<int32_t>(encode_uint32(data[2], data[1], data[0], 0)) >> 8;
369 temperature = (float) (raw_temp / 65536.0); // convert measurement to degrees Celsius (page 22 of datasheet)
370
371 return true;
372}
373
375 // - verifies data is ready to be read
376 // - reads in 6 bytes of temperature data (3 for temeperature, 3 for pressure)
377 // - returns whether successful, where the variable parameters contain
378 // - the measured temperature (in degrees Celsius)
379 // - the measured pressure (in Pa)
380
381 if (!this->check_data_readiness_()) {
382 ESP_LOGW(TAG, "Data not ready, skipping this update");
383 this->status_set_warning();
384
385 return false;
386 }
387
388 uint8_t data[6];
389 if (!this->bmp_read_bytes(BMP581_MEASUREMENT_DATA, &data[0], 6)) {
390 ESP_LOGW(TAG, "Failed to read measurement");
391 this->status_set_warning();
392
393 return false;
394 }
395
396 // temperature MSB is in data[2], LSB is in data[1], XLSB in data[0]
397 int32_t raw_temp = static_cast<int32_t>(encode_uint32(data[2], data[1], data[0], 0)) >> 8;
398 temperature = (float) (raw_temp / 65536.0); // convert measurement to degrees Celsius (page 22 of datasheet)
399
400 // pressure MSB is in data[5], LSB is in data[4], XLSB in data[3]
401 int32_t raw_press = encode_uint24(data[5], data[4], data[3]);
402 pressure = (float) (raw_press / 64.0); // Divide by 2^6=64 for Pa (page 22 of datasheet)
403
404 return true;
405}
406
408 // - activates interface (only relevant for SPI mode)
409 // - writes reset command to the command register
410 // - waits for sensor to complete reset
411 // - activates interface (only relevant for SPI mode)
412 // - returns the Power-On-Reboot interrupt status, which is asserted if successful
413
414 // activates communication interface (SPI only)
415 this->activate_interface();
416
417 // writes reset command to BMP's command register
418 if (!this->bmp_write_byte(BMP581_COMMAND, RESET_COMMAND)) {
419 ESP_LOGE(TAG, "Failed to write reset command");
420 return false;
421 }
422
423 // t_{soft_res} = 2ms (page 11 of datasheet); time it takes to enter standby mode
424 // - round up to 3 ms
425 delay(3);
426
427 // reactivates communication interface after reset (SPI only)
428 this->activate_interface();
429
430 // read interrupt status register
431 if (!this->bmp_read_byte(BMP581_INT_STATUS, &this->int_status_.reg)) {
432 ESP_LOGE(TAG, "Failed to read interrupt status register");
433
434 return false;
435 }
436
437 // power-On-Reboot bit is asserted if sensor successfully reset
438 return this->int_status_.bit.por;
439}
440
442 // - only pushes the sensor into FORCED_MODE for a reading if already in STANDBY_MODE
443 // - returns whether a measurement is in progress or has been initiated
444
445 if (this->odr_config_.bit.pwr_mode == STANDBY_MODE) {
446 return this->write_power_mode_(FORCED_MODE);
447 } else {
448 return true;
449 }
450}
451
452bool BMP581Component::write_iir_settings_(IIRFilter temperature_iir, IIRFilter pressure_iir) {
453 // - ensures data registers store filtered values
454 // - sets IIR filter levels on sensor
455 // - matches other default settings on sensor
456 // - writes configuration to the two relevant registers
457 // - returns success or failure of write to the registers
458
459 // If the temperature/pressure IIR filter is configured, then ensure data registers store the filtered measurement
460 this->dsp_config_.bit.shdw_sel_iir_t = (temperature_iir != IIR_FILTER_OFF);
461 this->dsp_config_.bit.shdw_sel_iir_p = (pressure_iir != IIR_FILTER_OFF);
462
463 // set temperature and pressure IIR filter level to configured values
464 this->iir_config_.bit.set_iir_t = temperature_iir;
465 this->iir_config_.bit.set_iir_p = pressure_iir;
466
467 // enable pressure and temperature compensation (page 61 of datasheet)
468 // - ?only relevant if IIR filter is applied?; the datasheet is ambiguous
469 // - matches BMP's default setting
470 this->dsp_config_.bit.comp_pt_en = 0x3;
471
472 // BMP581_DSP register and BMP581_DSP_IIR registers are successive
473 // - allows us to write the IIR configuration with one command to both registers
474 uint8_t register_data[2] = {this->dsp_config_.reg, this->iir_config_.reg};
475 return this->bmp_write_bytes(BMP581_DSP, register_data, sizeof(register_data));
476}
477
479 // - updates component's internal setting
480 // - returns success or failure of write to interrupt source register
481
482 this->int_source_.bit.drdy_data_reg_en = data_ready_enable;
483
484 // write interrupt source register
485 return this->bmp_write_byte(BMP581_INT_SOURCE, this->int_source_.reg);
486}
487
489 Oversampling pressure_oversampling) {
490 // - updates component's internal setting
491 // - returns success or failure of write to Over-Sampling Rate register
492
493 this->osr_config_.bit.osr_t = temperature_oversampling;
494 this->osr_config_.bit.osr_p = pressure_oversampling;
495
496 return this->bmp_write_byte(BMP581_OSR, this->osr_config_.reg);
497}
498
500 // - updates the component's internal power mode
501 // - returns success or failure of write to Output Data Rate register
502
503 this->odr_config_.bit.pwr_mode = mode;
504
505 // write odr register
506 return this->bmp_write_byte(BMP581_ODR, this->odr_config_.reg);
507}
508
509} // namespace esphome::bmp581_base
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
void mark_failed()
Mark this component as failed.
virtual void dump_config()
void status_clear_warning()
Definition component.h:318
void set_timeout(const char *name, uint32_t timeout, SchedulerCallback &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
bool read_temperature_(float &temperature)
union esphome::bmp581_base::BMP581Component::@42 iir_config_
bool write_interrupt_source_settings_(bool data_ready_enable)
bool write_oversampling_settings_(Oversampling temperature_oversampling, Oversampling pressure_oversampling)
virtual bool bmp_read_byte(uint8_t a_register, uint8_t *data)=0
virtual bool bmp_read_bytes(uint8_t a_register, uint8_t *data, size_t len)=0
union esphome::bmp581_base::BMP581Component::@41 dsp_config_
bool write_iir_settings_(IIRFilter temperature_iir, IIRFilter pressure_iir)
union esphome::bmp581_base::BMP581Component::@39 int_status_
union esphome::bmp581_base::BMP581Component::@38 int_source_
virtual bool bmp_write_bytes(uint8_t a_register, uint8_t *data, size_t len)=0
union esphome::bmp581_base::BMP581Component::@44 odr_config_
union esphome::bmp581_base::BMP581Component::@40 status_
virtual bool bmp_write_byte(uint8_t a_register, uint8_t data)=0
bool read_temperature_and_pressure_(float &temperature, float &pressure)
bool write_power_mode_(OperationMode mode)
union esphome::bmp581_base::BMP581Component::@43 osr_config_
u_int8_t raw_temp
ESPHOME_LOG_TAG(TAG, "bmp581")
PROGMEM_STRING_TABLE(OversamplingStrings, "None", "2x", "4x", "8x", "16x", "32x", "64x", "128x", "")
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:1231
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Definition helpers.h:1235
void HOT delay(uint32_t ms)
Definition hal.cpp:78
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12
uint8_t pressure
Definition tt21100.cpp:7