ESPHome 2026.11.0b6
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 divison 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
28static const LogString *oversampling_to_str(Oversampling oversampling) {
29 return OversamplingStrings::get_log_str(static_cast<uint8_t>(oversampling), OversamplingStrings::LAST_INDEX);
30}
31
32// IIR filter strings indexed by IIRFilter enum (0-7): OFF, 2, 4, 8, 16, 32, 64, 128
33PROGMEM_STRING_TABLE(IIRFilterStrings, "OFF", "2x", "4x", "8x", "16x", "32x", "64x", "128x", "");
34
35static const LogString *iir_filter_to_str(IIRFilter filter) {
36 return IIRFilterStrings::get_log_str(static_cast<uint8_t>(filter), IIRFilterStrings::LAST_INDEX);
37}
38
40 ESP_LOGCONFIG(TAG, "BMP581:");
41
42 LOG_UPDATE_INTERVAL(this);
43
44 ESP_LOGCONFIG(TAG, " Measurement conversion time: %ums", this->conversion_time_);
45
46 if (this->temperature_sensor_) {
47 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
48 ESP_LOGCONFIG(TAG,
49 " IIR Filter: %s\n"
50 " Oversampling: %s",
51 LOG_STR_ARG(iir_filter_to_str(this->iir_temperature_level_)),
52 LOG_STR_ARG(oversampling_to_str(this->temperature_oversampling_)));
53 }
54
55 if (this->pressure_sensor_) {
56 LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
57 ESP_LOGCONFIG(TAG,
58 " IIR Filter: %s\n"
59 " Oversampling: %s",
60 LOG_STR_ARG(iir_filter_to_str(this->iir_pressure_level_)),
61 LOG_STR_ARG(oversampling_to_str(this->pressure_oversampling_)));
62 }
63}
64
65void BMP581Component::setup() {
66 /*
67 * Setup goes through several stages, which follows the post-power-up procedure (page 18 of datasheet) and then sets
68 * configured options
69 * 1) Soft reboot
70 * 2) Verify ASIC chip ID matches BMP581
71 * 3) Verify sensor status (check if NVM is okay)
72 * 4) Enable data ready interrupt
73 * 5) Write oversampling settings and set internal configuration values
74 * 6) Configure and prime IIR Filter(s), if enabled
75 */
76
78 // 1) Soft reboot //
80
81 // Power-On-Reboot bit is asserted if sensor successfully reset
82 if (!this->reset_()) {
83 this->mark_failed(LOG_STR("Reset failed"));
84
85 return;
86 }
87
89 // 2) Verify ASIC chip ID matches BMP581 //
91
92 uint8_t chip_id;
93
94 // read chip id from sensor
95 if (!this->bmp_read_byte(BMP581_CHIP_ID, &chip_id)) {
96 this->mark_failed(LOG_STR("Read chip ID failed"));
97
98 return;
99 }
100
101 // verify id
102 if (chip_id != BMP581_ASIC_ID && chip_id != BMP585_ASIC_ID) {
103 this->mark_failed(LOG_STR("Unknown chip ID"));
104
105 return;
106 }
107
109 // 3) Verify sensor status (check if NVM is okay) //
111
112 if (!this->bmp_read_byte(BMP581_STATUS, &this->status_.reg)) {
113 this->mark_failed(LOG_STR("Failed to read status register"));
114
115 return;
116 }
117
118 // verify status_nvm_rdy bit (it is asserted if boot was successful)
119 if (!(this->status_.bit.status_nvm_rdy)) {
120 this->mark_failed(LOG_STR("NVM not ready"));
121
122 return;
123 }
124
125 // verify status_nvm_err bit (it is asserted if an error is detected)
126 if (this->status_.bit.status_nvm_err) {
127 this->mark_failed(LOG_STR("NVM error detected"));
128
129 return;
130 }
131
133 // 4) Enable data ready interrupt //
135
136 // enable the data ready interrupt source
137 if (!this->write_interrupt_source_settings_(true)) {
138 this->mark_failed(LOG_STR("Failed to write interrupt source register"));
139
140 return;
141 }
142
144 // 5) Write oversampling settings and set internal configuration values //
146
147 // configure pressure readings, if sensor is defined
148 // otherwise, disable pressure oversampling
149 if (this->pressure_sensor_) {
150 this->osr_config_.bit.press_en = true;
151 } else {
153 }
154
155 // write oversampling settings
157 this->mark_failed(LOG_STR("Failed to write oversampling register"));
158
159 return;
160 }
161
162 // set output data rate to 4 Hz=0x19 (page 65 of datasheet)
163 // - ?shouldn't? matter as this component only uses FORCED_MODE - datasheet is ambiguous
164 // - If in NORMAL_MODE or NONSTOP_MODE, then this would still allow deep standby to save power
165 // - will be written to BMP581 at next requested measurement
166 this->odr_config_.bit.odr = 0x19;
167
171
174 this->mark_failed(LOG_STR("Failed to write IIR configuration registers"));
175
176 return;
177 }
178
179 if (!this->prime_iir_filter_()) {
180 this->mark_failed(LOG_STR("Failed to prime the IIR filter with an initial measurement"));
181
182 return;
183 }
184 }
185}
186
187void BMP581Component::update() {
188 /*
189 * Each update goes through several stages
190 * 0) Verify either a temperature or pressure sensor is defined before proceeding
191 * 1) Request a measurement
192 * 2) Wait for measurement to finish (based on oversampling rates)
193 * 3) Read data registers for temperature and pressure, if applicable
194 * 4) Publish measurements to sensor(s), if applicable
195 */
196
198 // 0) Verify either a temperature or pressure sensor is defined before proceeding //
200
201 if ((!this->temperature_sensor_) && (!this->pressure_sensor_)) {
202 return;
203 }
204
206 // 1) Request a measurement //
208
209 ESP_LOGVV(TAG, "Requesting measurement");
210
211 if (!this->start_measurement_()) {
212 ESP_LOGW(TAG, "Requesting forced measurement failed");
213 this->status_set_warning();
214
215 return;
216 }
217
219 // 2) Wait for measurement to finish (based on oversampling rates) //
221
222 ESP_LOGVV(TAG, "Measurement should take %d ms", this->conversion_time_);
223
224 this->set_timeout(MEASUREMENT_TIMEOUT_ID, this->conversion_time_, [this]() {
225 float temperature = 0.0;
226 float pressure = 0.0;
227
229 // 3) Read data registers for temperature and pressure, if applicable //
231
232 if (this->pressure_sensor_) {
233 if (!this->read_temperature_and_pressure_(temperature, pressure)) {
234 ESP_LOGW(TAG, "Failed to read temperature and pressure; skipping update");
235 this->status_set_warning();
236
237 return;
238 }
239 } else {
240 if (!this->read_temperature_(temperature)) {
241 ESP_LOGW(TAG, "Failed to read temperature; skipping update");
242 this->status_set_warning();
243
244 return;
245 }
246 }
247
249 // 4) Publish measurements to sensor(s), if applicable //
251
252 PUBLISH_STATE(this->temperature_sensor_, temperature);
253 PUBLISH_STATE(this->pressure_sensor_, pressure);
254
255 this->status_clear_warning();
256 });
257}
258
260 // - verifies component is not internally in standby mode
261 // - reads interrupt status register
262 // - checks if data ready bit is asserted
263 // - If true, then internally sets component to standby mode if in forced mode
264 // - returns data readiness state
265
266 if (this->odr_config_.bit.pwr_mode == STANDBY_MODE) {
267 ESP_LOGD(TAG, "Data not ready, sensor is in standby mode");
268 return false;
269 }
270
271 uint8_t status;
272
273 if (!this->bmp_read_byte(BMP581_INT_STATUS, &status)) {
274 ESP_LOGE(TAG, "Failed to read interrupt status register");
275 return false;
276 }
277
278 this->int_status_.reg = status;
279
280 if (this->int_status_.bit.drdy_data_reg) {
281 // If in forced mode, then set internal record of the power mode to STANDBY_MODE
282 // - sensor automatically returns to standby mode after completing a forced measurement
283 if (this->odr_config_.bit.pwr_mode == FORCED_MODE) {
284 this->odr_config_.bit.pwr_mode = STANDBY_MODE;
285 }
286
287 return true;
288 }
289
290 return false;
291}
292
294 // - temporarily disables oversampling for a fast initial measurement; avoids slowing down ESPHome's startup process
295 // - enables IIR filter flushing with forced measurements
296 // - forces a measurement; flushing the IIR filter and priming it with a current value
297 // - disables IIR filter flushing with forced measurements
298 // - reverts to internally configured oversampling rates
299 // - returns success of all register writes/priming
300
301 // store current internal oversampling settings to revert to after priming
302 Oversampling current_temperature_oversampling = (Oversampling) this->osr_config_.bit.osr_t;
303 Oversampling current_pressure_oversampling = (Oversampling) this->osr_config_.bit.osr_p;
304
305 // temporarily disables oversampling for temperature and pressure for a fast priming measurement
307 ESP_LOGE(TAG, "Failed to write oversampling register");
308
309 return false;
310 }
311
312 // flush the IIR filter with forced measurements (we will only flush once)
313 this->dsp_config_.bit.iir_flush_forced_en = true;
314 if (!this->bmp_write_byte(BMP581_DSP, this->dsp_config_.reg)) {
315 ESP_LOGE(TAG, "Failed to write IIR source register");
316
317 return false;
318 }
319
320 // forces an intial measurement
321 // - this measurements flushes the IIR filter reflecting written DSP settings
322 // - flushing with this initial reading avoids having the internal previous data aquisition being 0, which
323 // (I)nfinitely affects future values
324 if (!this->start_measurement_()) {
325 ESP_LOGE(TAG, "Failed to request a forced measurement");
326
327 return false;
328 }
329
330 // wait for priming measurement to complete
331 // - with oversampling disabled, the conversion time for a single measurement for pressure and temperature is
332 // ceilf(1.05*(1.0+1.0)) = 3ms
333 // - see page 12 of datasheet for details
334 delay(3);
335
336 if (!this->check_data_readiness_()) {
337 ESP_LOGE(TAG, "IIR priming measurement was not ready");
338
339 return false;
340 }
341
342 // disable IIR filter flushings on future forced measurements
343 this->dsp_config_.bit.iir_flush_forced_en = false;
344 if (!this->bmp_write_byte(BMP581_DSP, this->dsp_config_.reg)) {
345 ESP_LOGE(TAG, "Failed to write IIR source register");
346
347 return false;
348 }
349
350 // revert oversampling rates to original settings
351 return this->write_oversampling_settings_(current_temperature_oversampling, current_pressure_oversampling);
352}
353
355 // - verifies data is ready to be read
356 // - reads in 3 bytes of temperature data
357 // - returns whether successful, where the the variable parameter contains
358 // - the measured temperature (in degrees Celsius)
359
360 if (!this->check_data_readiness_()) {
361 ESP_LOGW(TAG, "Data not ready, skipping this update");
362 this->status_set_warning();
363
364 return false;
365 }
366
367 uint8_t data[3];
368 if (!this->bmp_read_bytes(BMP581_MEASUREMENT_DATA, &data[0], 3)) {
369 ESP_LOGW(TAG, "Failed to read measurement");
370 this->status_set_warning();
371
372 return false;
373 }
374
375 // temperature MSB is in data[2], LSB is in data[1], XLSB in data[0]
376 int32_t raw_temp = static_cast<int32_t>(encode_uint32(data[2], data[1], data[0], 0)) >> 8;
377 temperature = (float) (raw_temp / 65536.0); // convert measurement to degrees Celsius (page 22 of datasheet)
378
379 return true;
380}
381
383 // - verifies data is ready to be read
384 // - reads in 6 bytes of temperature data (3 for temeperature, 3 for pressure)
385 // - returns whether successful, where the variable parameters contain
386 // - the measured temperature (in degrees Celsius)
387 // - the measured pressure (in Pa)
388
389 if (!this->check_data_readiness_()) {
390 ESP_LOGW(TAG, "Data not ready, skipping this update");
391 this->status_set_warning();
392
393 return false;
394 }
395
396 uint8_t data[6];
397 if (!this->bmp_read_bytes(BMP581_MEASUREMENT_DATA, &data[0], 6)) {
398 ESP_LOGW(TAG, "Failed to read measurement");
399 this->status_set_warning();
400
401 return false;
402 }
403
404 // temperature MSB is in data[2], LSB is in data[1], XLSB in data[0]
405 int32_t raw_temp = static_cast<int32_t>(encode_uint32(data[2], data[1], data[0], 0)) >> 8;
406 temperature = (float) (raw_temp / 65536.0); // convert measurement to degrees Celsius (page 22 of datasheet)
407
408 // pressure MSB is in data[5], LSB is in data[4], XLSB in data[3]
409 int32_t raw_press = encode_uint24(data[5], data[4], data[3]);
410 pressure = (float) (raw_press / 64.0); // Divide by 2^6=64 for Pa (page 22 of datasheet)
411
412 return true;
413}
414
416 // - activates interface (only relevant for SPI mode)
417 // - writes reset command to the command register
418 // - waits for sensor to complete reset
419 // - activates interface (only relevant for SPI mode)
420 // - returns the Power-On-Reboot interrupt status, which is asserted if successful
421
422 // activates communication interface (SPI only)
423 this->activate_interface();
424
425 // writes reset command to BMP's command register
426 if (!this->bmp_write_byte(BMP581_COMMAND, RESET_COMMAND)) {
427 ESP_LOGE(TAG, "Failed to write reset command");
428 return false;
429 }
430
431 // t_{soft_res} = 2ms (page 11 of datasheet); time it takes to enter standby mode
432 // - round up to 3 ms
433 delay(3);
434
435 // reactivates communication interface after reset (SPI only)
436 this->activate_interface();
437
438 // read interrupt status register
439 if (!this->bmp_read_byte(BMP581_INT_STATUS, &this->int_status_.reg)) {
440 ESP_LOGE(TAG, "Failed to read interrupt status register");
441
442 return false;
443 }
444
445 // power-On-Reboot bit is asserted if sensor successfully reset
446 return this->int_status_.bit.por;
447}
448
450 // - only pushes the sensor into FORCED_MODE for a reading if already in STANDBY_MODE
451 // - returns whether a measurement is in progress or has been initiated
452
453 if (this->odr_config_.bit.pwr_mode == STANDBY_MODE) {
454 return this->write_power_mode_(FORCED_MODE);
455 } else {
456 return true;
457 }
458}
459
460bool BMP581Component::write_iir_settings_(IIRFilter temperature_iir, IIRFilter pressure_iir) {
461 // - ensures data registers store filtered values
462 // - sets IIR filter levels on sensor
463 // - matches other default settings on sensor
464 // - writes configuration to the two relevant registers
465 // - returns success or failure of write to the registers
466
467 // If the temperature/pressure IIR filter is configured, then ensure data registers store the filtered measurement
468 this->dsp_config_.bit.shdw_sel_iir_t = (temperature_iir != IIR_FILTER_OFF);
469 this->dsp_config_.bit.shdw_sel_iir_p = (pressure_iir != IIR_FILTER_OFF);
470
471 // set temperature and pressure IIR filter level to configured values
472 this->iir_config_.bit.set_iir_t = temperature_iir;
473 this->iir_config_.bit.set_iir_p = pressure_iir;
474
475 // enable pressure and temperature compensation (page 61 of datasheet)
476 // - ?only relevant if IIR filter is applied?; the datasheet is ambiguous
477 // - matches BMP's default setting
478 this->dsp_config_.bit.comp_pt_en = 0x3;
479
480 // BMP581_DSP register and BMP581_DSP_IIR registers are successive
481 // - allows us to write the IIR configuration with one command to both registers
482 uint8_t register_data[2] = {this->dsp_config_.reg, this->iir_config_.reg};
483 return this->bmp_write_bytes(BMP581_DSP, register_data, sizeof(register_data));
484}
485
487 // - updates component's internal setting
488 // - returns success or failure of write to interrupt source register
489
490 this->int_source_.bit.drdy_data_reg_en = data_ready_enable;
491
492 // write interrupt source register
493 return this->bmp_write_byte(BMP581_INT_SOURCE, this->int_source_.reg);
494}
495
497 Oversampling pressure_oversampling) {
498 // - updates component's internal setting
499 // - returns success or failure of write to Over-Sampling Rate register
500
501 this->osr_config_.bit.osr_t = temperature_oversampling;
502 this->osr_config_.bit.osr_p = pressure_oversampling;
503
504 return this->bmp_write_byte(BMP581_OSR, this->osr_config_.reg);
505}
506
508 // - updates the component's internal power mode
509 // - returns success or failure of write to Output Data Rate register
510
511 this->odr_config_.bit.pwr_mode = mode;
512
513 // write odr register
514 return this->bmp_write_byte(BMP581_ODR, this->odr_config_.reg);
515}
516
517} // 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 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
bool read_temperature_(float &temperature)
union esphome::bmp581_base::BMP581Component::@42 dsp_config_
bool write_interrupt_source_settings_(bool data_ready_enable)
union esphome::bmp581_base::BMP581Component::@43 iir_config_
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 status_
union esphome::bmp581_base::BMP581Component::@44 osr_config_
bool write_iir_settings_(IIRFilter temperature_iir, IIRFilter pressure_iir)
union esphome::bmp581_base::BMP581Component::@39 int_source_
union esphome::bmp581_base::BMP581Component::@40 int_status_
virtual bool bmp_write_bytes(uint8_t a_register, uint8_t *data, size_t len)=0
union esphome::bmp581_base::BMP581Component::@45 odr_config_
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)
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:1047
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:1051
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