ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
bmp280_base.cpp
Go to the documentation of this file.
1#include "bmp280_base.h"
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
6
8
9ESPHOME_LOG_TAG(TAG, "bmp280.sensor");
10
11static constexpr uint32_t DATA_TIMEOUT_ID = 0;
12
13static const uint8_t BMP280_REGISTER_STATUS = 0xF3;
14static const uint8_t BMP280_REGISTER_CONTROL = 0xF4;
15static const uint8_t BMP280_REGISTER_CONFIG = 0xF5;
16static const uint8_t BMP280_REGISTER_PRESSUREDATA = 0xF7;
17static const uint8_t BMP280_REGISTER_TEMPDATA = 0xFA;
18static const uint8_t BMP280_REGISTER_RESET = 0xE0;
19
20static const uint8_t BMP280_MODE_FORCED = 0b01;
21static const uint8_t BMP280_SOFT_RESET = 0xB6;
22static const uint8_t BMP280_STATUS_IM_UPDATE = 0b01;
23
24// Indexed by BMP280Oversampling
25PROGMEM_STRING_TABLE(OversamplingStrings, "None", "1x", "2x", "4x", "8x", "16x", "UNKNOWN");
26
27[[maybe_unused]] static const LogString *oversampling_to_str(BMP280Oversampling value) {
28 return OversamplingStrings::get_log_str(static_cast<uint8_t>(value), OversamplingStrings::LAST_INDEX);
29}
30
31// Indexed by BMP280IIRFilter
32PROGMEM_STRING_TABLE(IIRFilterStrings, "OFF", "2x", "4x", "8x", "16x", "UNKNOWN");
33
34[[maybe_unused]] static const LogString *iir_filter_to_str(BMP280IIRFilter value) {
35 return IIRFilterStrings::get_log_str(static_cast<uint8_t>(value), IIRFilterStrings::LAST_INDEX);
36}
37
38void BMP280Component::setup() {
39 uint8_t chip_id = 0;
40
41 // Read the chip id twice, to work around a bug where the first read is 0.
42 // https://community.st.com/t5/stm32-mcus-products/issue-with-reading-bmp280-chip-id-using-spi/td-p/691855
43 if (!this->bmp_read_byte(0xD0, &chip_id)) {
44 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
45 return;
46 }
47 if (!this->bmp_read_byte(0xD0, &chip_id)) {
48 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
49 return;
50 }
51 if (chip_id != 0x58) {
52 this->mark_failed(LOG_STR("Wrong chip ID or no response"));
53 return;
54 }
55
56 // Send a soft reset.
57 if (!this->bmp_write_byte(BMP280_REGISTER_RESET, BMP280_SOFT_RESET)) {
58 this->mark_failed(LOG_STR("Reset failed"));
59 return;
60 }
61 // Wait until the NVM data has finished loading.
62 uint8_t status;
63 uint8_t retry = 5;
64 do {
65 delay(2);
66 if (!this->bmp_read_byte(BMP280_REGISTER_STATUS, &status)) {
67 this->mark_failed(LOG_STR("Error reading status register"));
68 return;
69 }
70 } while ((status & BMP280_STATUS_IM_UPDATE) && (--retry));
71 if (status & BMP280_STATUS_IM_UPDATE) {
72 this->mark_failed(LOG_STR("Timeout loading NVM"));
73 return;
74 }
75
76 // Read calibration
77 this->calibration_.t1 = this->read_u16_le_(0x88);
78 this->calibration_.t2 = this->read_s16_le_(0x8A);
79 this->calibration_.t3 = this->read_s16_le_(0x8C);
80
81 this->calibration_.p1 = this->read_u16_le_(0x8E);
82 this->calibration_.p2 = this->read_s16_le_(0x90);
83 this->calibration_.p3 = this->read_s16_le_(0x92);
84 this->calibration_.p4 = this->read_s16_le_(0x94);
85 this->calibration_.p5 = this->read_s16_le_(0x96);
86 this->calibration_.p6 = this->read_s16_le_(0x98);
87 this->calibration_.p7 = this->read_s16_le_(0x9A);
88 this->calibration_.p8 = this->read_s16_le_(0x9C);
89 this->calibration_.p9 = this->read_s16_le_(0x9E);
90
91 uint8_t config_register = 0;
92 if (!this->bmp_read_byte(BMP280_REGISTER_CONFIG, &config_register)) {
93 this->mark_failed(LOG_STR("Read config"));
94 return;
95 }
96 config_register &= ~0b11111100;
97 config_register |= 0b000 << 5; // 0.5 ms standby time
98 config_register |= (this->iir_filter_ & 0b111) << 2;
99 if (!this->bmp_write_byte(BMP280_REGISTER_CONFIG, config_register)) {
100 this->mark_failed(LOG_STR("Write config"));
101 return;
102 }
103}
104void BMP280Component::dump_config() {
105 ESP_LOGCONFIG(TAG, "BMP280:");
106 ESP_LOGCONFIG(TAG, " IIR Filter: %s", LOG_STR_ARG(iir_filter_to_str(this->iir_filter_)));
107 LOG_UPDATE_INTERVAL(this);
108
109 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
110 ESP_LOGCONFIG(TAG, " Oversampling: %s", LOG_STR_ARG(oversampling_to_str(this->temperature_oversampling_)));
111 LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
112 ESP_LOGCONFIG(TAG, " Oversampling: %s", LOG_STR_ARG(oversampling_to_str(this->pressure_oversampling_)));
113}
114
115inline uint8_t oversampling_to_time(BMP280Oversampling over_sampling) { return (1 << uint8_t(over_sampling)) >> 1; }
116
117void BMP280Component::update() {
118 // Enable sensor
119 ESP_LOGV(TAG, "Sending conversion request");
120 uint8_t meas_value = 0;
121 meas_value |= (this->temperature_oversampling_ & 0b111) << 5;
122 meas_value |= (this->pressure_oversampling_ & 0b111) << 2;
123 meas_value |= 0b01; // Forced mode
124 if (!this->bmp_write_byte(BMP280_REGISTER_CONTROL, meas_value)) {
125 this->status_set_warning();
126 return;
127 }
128
129 float meas_time = 1;
130 meas_time += 2.3f * oversampling_to_time(this->temperature_oversampling_);
131 meas_time += 2.3f * oversampling_to_time(this->pressure_oversampling_) + 0.575f;
132
133 this->set_timeout(DATA_TIMEOUT_ID, uint32_t(ceilf(meas_time)), [this]() {
134 int32_t t_fine = 0;
135 float temperature = this->read_temperature_(&t_fine);
136 if (std::isnan(temperature)) {
137 ESP_LOGW(TAG, "Invalid temperature");
138 this->status_set_warning();
139 return;
140 }
141 float pressure = this->read_pressure_(t_fine);
142
143 ESP_LOGV(TAG, "Temperature=%.1f°C Pressure=%.1fhPa", temperature, pressure);
144 PUBLISH_STATE(this->temperature_sensor_, temperature);
145 PUBLISH_STATE(this->pressure_sensor_, pressure);
146 this->status_clear_warning();
147 });
148}
149
152 return NAN;
153 }
154 uint8_t data[3]{};
155 if (!this->bmp_read_bytes(BMP280_REGISTER_TEMPDATA, data, 3))
156 return NAN;
157 ESP_LOGV(TAG, "Read temperature data, raw: %02X %02X %02X", data[0], data[1], data[2]);
158 int32_t adc = encode_uint24(data[0], data[1], data[2]);
159 adc >>= 4;
160
161 const int32_t t1 = this->calibration_.t1;
162 const int32_t t2 = this->calibration_.t2;
163 const int32_t t3 = this->calibration_.t3;
164
165 int32_t var1 = (((adc >> 3) - (t1 << 1)) * t2) >> 11;
166 int32_t var2 = (((((adc >> 4) - t1) * ((adc >> 4) - t1)) >> 12) * t3) >> 14;
167 *t_fine = var1 + var2;
168
169 float temperature = (*t_fine * 5 + 128);
170 return temperature / 25600.0f;
171}
172
173float BMP280Component::read_pressure_(int32_t t_fine) {
175 return NAN;
176 }
177 uint8_t data[3];
178 if (!this->bmp_read_bytes(BMP280_REGISTER_PRESSUREDATA, data, 3))
179 return NAN;
180 int32_t adc = encode_uint24(data[0], data[1], data[2]);
181 adc >>= 4;
182 const int64_t p1 = this->calibration_.p1;
183 const int64_t p2 = this->calibration_.p2;
184 const int64_t p3 = this->calibration_.p3;
185 const int64_t p4 = this->calibration_.p4;
186 const int64_t p5 = this->calibration_.p5;
187 const int64_t p6 = this->calibration_.p6;
188 const int64_t p7 = this->calibration_.p7;
189 const int64_t p8 = this->calibration_.p8;
190 const int64_t p9 = this->calibration_.p9;
191
192 int64_t var1, var2, p;
193 var1 = int64_t(t_fine) - 128000;
194 var2 = var1 * var1 * p6;
195 var2 = var2 + ((var1 * p5) << 17);
196 var2 = var2 + (p4 << 35);
197 var1 = ((var1 * var1 * p3) >> 8) + ((var1 * p2) << 12);
198 var1 = ((int64_t(1) << 47) + var1) * p1 >> 33;
199
200 if (var1 == 0)
201 return NAN;
202
203 p = 1048576 - adc;
204 p = (((p << 31) - var2) * 3125) / var1;
205 var1 = (p9 * (p >> 13) * (p >> 13)) >> 25;
206 var2 = (p8 * p) >> 19;
207
208 p = ((p + var1 + var2) >> 8) + (p7 << 4);
209 return (p / 256.0f) / 100.0f;
210}
211void BMP280Component::set_temperature_oversampling(BMP280Oversampling temperature_over_sampling) {
212 this->temperature_oversampling_ = temperature_over_sampling;
213}
214void BMP280Component::set_pressure_oversampling(BMP280Oversampling pressure_over_sampling) {
215 this->pressure_oversampling_ = pressure_over_sampling;
216}
217uint8_t BMP280Component::read_u8_(uint8_t a_register) {
218 uint8_t data = 0;
219 this->bmp_read_byte(a_register, &data);
220 return data;
221}
222uint16_t BMP280Component::read_u16_le_(uint8_t a_register) {
223 uint16_t data = 0;
224 this->bmp_read_byte_16_le(a_register, &data);
225 return data;
226}
227int16_t BMP280Component::read_s16_le_(uint8_t a_register) { return this->read_u16_le_(a_register); }
228
229} // namespace esphome::bmp280_base
uint8_t status
Definition bl0942.h:8
void mark_failed()
Mark this component as failed.
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
virtual bool bmp_write_byte(uint8_t a_register, uint8_t data)=0
virtual bool bmp_read_byte_16_le(uint8_t a_register, uint16_t *data)=0
uint8_t read_u8_(uint8_t a_register)
virtual bool bmp_read_bytes(uint8_t a_register, uint8_t *data, size_t len)=0
uint16_t read_u16_le_(uint8_t a_register)
int16_t read_s16_le_(uint8_t a_register)
float read_pressure_(int32_t t_fine)
Read the pressure value in hPa using the provided t_fine value.
BMP280Oversampling pressure_oversampling_
Definition bmp280_base.h:84
virtual bool bmp_read_byte(uint8_t a_register, uint8_t *data)=0
float read_temperature_(int32_t *t_fine)
Read the temperature value and store the calculated ambient temperature in t_fine.
BMP280Oversampling temperature_oversampling_
Definition bmp280_base.h:83
BMP280CalibrationData calibration_
Definition bmp280_base.h:82
BMP280IIRFilter
Enum listing all Infinite Impulse Filter values for the BMP280.
Definition bmp280_base.h:43
ESPHOME_LOG_TAG(TAG, "bmp280.sensor")
BMP280Oversampling
Enum listing all Oversampling values for the BMP280.
Definition bmp280_base.h:30
PROGMEM_STRING_TABLE(OversamplingStrings, "None", "1x", "2x", "4x", "8x", "16x", "UNKNOWN")
uint8_t oversampling_to_time(BMP280Oversampling over_sampling)
uint32_t value
Definition hal.cpp:41
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