ESPHome 2025.9.0-dev
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"
3#include "esphome/core/log.h"
4
5#define BMP280_ERROR_WRONG_CHIP_ID "Wrong chip ID"
6
7namespace esphome {
8namespace bmp280_base {
9
10static const char *const TAG = "bmp280.sensor";
11
12static const uint8_t BMP280_REGISTER_STATUS = 0xF3;
13static const uint8_t BMP280_REGISTER_CONTROL = 0xF4;
14static const uint8_t BMP280_REGISTER_CONFIG = 0xF5;
15static const uint8_t BMP280_REGISTER_PRESSUREDATA = 0xF7;
16static const uint8_t BMP280_REGISTER_TEMPDATA = 0xFA;
17static const uint8_t BMP280_REGISTER_RESET = 0xE0;
18
19static const uint8_t BMP280_MODE_FORCED = 0b01;
20static const uint8_t BMP280_SOFT_RESET = 0xB6;
21static const uint8_t BMP280_STATUS_IM_UPDATE = 0b01;
22
23inline uint16_t combine_bytes(uint8_t msb, uint8_t lsb) { return ((msb & 0xFF) << 8) | (lsb & 0xFF); }
24
25static const char *oversampling_to_str(BMP280Oversampling oversampling) {
26 switch (oversampling) {
28 return "None";
30 return "1x";
32 return "2x";
34 return "4x";
36 return "8x";
38 return "16x";
39 default:
40 return "UNKNOWN";
41 }
42}
43
44static const char *iir_filter_to_str(BMP280IIRFilter filter) {
45 switch (filter) {
47 return "OFF";
49 return "2x";
51 return "4x";
53 return "8x";
55 return "16x";
56 default:
57 return "UNKNOWN";
58 }
59}
60
62 uint8_t chip_id = 0;
63
64 // Read the chip id twice, to work around a bug where the first read is 0.
65 // https://community.st.com/t5/stm32-mcus-products/issue-with-reading-bmp280-chip-id-using-spi/td-p/691855
66 if (!this->read_byte(0xD0, &chip_id)) {
67 this->error_code_ = COMMUNICATION_FAILED;
68 this->mark_failed(ESP_LOG_MSG_COMM_FAIL);
69 return;
70 }
71 if (!this->read_byte(0xD0, &chip_id)) {
72 this->error_code_ = COMMUNICATION_FAILED;
73 this->mark_failed(ESP_LOG_MSG_COMM_FAIL);
74 return;
75 }
76 if (chip_id != 0x58) {
77 this->error_code_ = WRONG_CHIP_ID;
78 this->mark_failed(BMP280_ERROR_WRONG_CHIP_ID);
79 return;
80 }
81
82 // Send a soft reset.
83 if (!this->write_byte(BMP280_REGISTER_RESET, BMP280_SOFT_RESET)) {
84 this->mark_failed("Reset failed");
85 return;
86 }
87 // Wait until the NVM data has finished loading.
88 uint8_t status;
89 uint8_t retry = 5;
90 do {
91 delay(2);
92 if (!this->read_byte(BMP280_REGISTER_STATUS, &status)) {
93 this->mark_failed("Error reading status register");
94 return;
95 }
96 } while ((status & BMP280_STATUS_IM_UPDATE) && (--retry));
97 if (status & BMP280_STATUS_IM_UPDATE) {
98 this->mark_failed("Timeout loading NVM");
99 return;
100 }
101
102 // Read calibration
103 this->calibration_.t1 = this->read_u16_le_(0x88);
104 this->calibration_.t2 = this->read_s16_le_(0x8A);
105 this->calibration_.t3 = this->read_s16_le_(0x8C);
106
107 this->calibration_.p1 = this->read_u16_le_(0x8E);
108 this->calibration_.p2 = this->read_s16_le_(0x90);
109 this->calibration_.p3 = this->read_s16_le_(0x92);
110 this->calibration_.p4 = this->read_s16_le_(0x94);
111 this->calibration_.p5 = this->read_s16_le_(0x96);
112 this->calibration_.p6 = this->read_s16_le_(0x98);
113 this->calibration_.p7 = this->read_s16_le_(0x9A);
114 this->calibration_.p8 = this->read_s16_le_(0x9C);
115 this->calibration_.p9 = this->read_s16_le_(0x9E);
116
117 uint8_t config_register = 0;
118 if (!this->read_byte(BMP280_REGISTER_CONFIG, &config_register)) {
119 this->mark_failed("Read config");
120 return;
121 }
122 config_register &= ~0b11111100;
123 config_register |= 0b000 << 5; // 0.5 ms standby time
124 config_register |= (this->iir_filter_ & 0b111) << 2;
125 if (!this->write_byte(BMP280_REGISTER_CONFIG, config_register)) {
126 this->mark_failed("Write config");
127 return;
128 }
129}
131 ESP_LOGCONFIG(TAG, "BMP280:");
132 switch (this->error_code_) {
134 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
135 break;
136 case WRONG_CHIP_ID:
137 ESP_LOGE(TAG, BMP280_ERROR_WRONG_CHIP_ID);
138 break;
139 case NONE:
140 default:
141 break;
142 }
143 ESP_LOGCONFIG(TAG, " IIR Filter: %s", iir_filter_to_str(this->iir_filter_));
144 LOG_UPDATE_INTERVAL(this);
145
146 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
147 ESP_LOGCONFIG(TAG, " Oversampling: %s", oversampling_to_str(this->temperature_oversampling_));
148 LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
149 ESP_LOGCONFIG(TAG, " Oversampling: %s", oversampling_to_str(this->pressure_oversampling_));
150}
152
153inline uint8_t oversampling_to_time(BMP280Oversampling over_sampling) { return (1 << uint8_t(over_sampling)) >> 1; }
154
156 // Enable sensor
157 ESP_LOGV(TAG, "Sending conversion request");
158 uint8_t meas_value = 0;
159 meas_value |= (this->temperature_oversampling_ & 0b111) << 5;
160 meas_value |= (this->pressure_oversampling_ & 0b111) << 2;
161 meas_value |= 0b01; // Forced mode
162 if (!this->write_byte(BMP280_REGISTER_CONTROL, meas_value)) {
163 this->status_set_warning();
164 return;
165 }
166
167 float meas_time = 1;
168 meas_time += 2.3f * oversampling_to_time(this->temperature_oversampling_);
169 meas_time += 2.3f * oversampling_to_time(this->pressure_oversampling_) + 0.575f;
170
171 this->set_timeout("data", uint32_t(ceilf(meas_time)), [this]() {
172 int32_t t_fine = 0;
173 float temperature = this->read_temperature_(&t_fine);
174 if (std::isnan(temperature)) {
175 ESP_LOGW(TAG, "Invalid temperature");
176 this->status_set_warning();
177 return;
178 }
179 float pressure = this->read_pressure_(t_fine);
180
181 ESP_LOGV(TAG, "Temperature=%.1f°C Pressure=%.1fhPa", temperature, pressure);
182 if (this->temperature_sensor_ != nullptr)
183 this->temperature_sensor_->publish_state(temperature);
184 if (this->pressure_sensor_ != nullptr)
185 this->pressure_sensor_->publish_state(pressure);
186 this->status_clear_warning();
187 });
188}
189
191 uint8_t data[3];
192 if (!this->read_bytes(BMP280_REGISTER_TEMPDATA, data, 3))
193 return NAN;
194 int32_t adc = ((data[0] & 0xFF) << 16) | ((data[1] & 0xFF) << 8) | (data[2] & 0xFF);
195 adc >>= 4;
196 if (adc == 0x80000) {
197 // temperature was disabled
198 return NAN;
199 }
200
201 const int32_t t1 = this->calibration_.t1;
202 const int32_t t2 = this->calibration_.t2;
203 const int32_t t3 = this->calibration_.t3;
204
205 int32_t var1 = (((adc >> 3) - (t1 << 1)) * t2) >> 11;
206 int32_t var2 = (((((adc >> 4) - t1) * ((adc >> 4) - t1)) >> 12) * t3) >> 14;
207 *t_fine = var1 + var2;
208
209 float temperature = (*t_fine * 5 + 128);
210 return temperature / 25600.0f;
211}
212
213float BMP280Component::read_pressure_(int32_t t_fine) {
214 uint8_t data[3];
215 if (!this->read_bytes(BMP280_REGISTER_PRESSUREDATA, data, 3))
216 return NAN;
217 int32_t adc = ((data[0] & 0xFF) << 16) | ((data[1] & 0xFF) << 8) | (data[2] & 0xFF);
218 adc >>= 4;
219 if (adc == 0x80000) {
220 // pressure was disabled
221 return NAN;
222 }
223 const int64_t p1 = this->calibration_.p1;
224 const int64_t p2 = this->calibration_.p2;
225 const int64_t p3 = this->calibration_.p3;
226 const int64_t p4 = this->calibration_.p4;
227 const int64_t p5 = this->calibration_.p5;
228 const int64_t p6 = this->calibration_.p6;
229 const int64_t p7 = this->calibration_.p7;
230 const int64_t p8 = this->calibration_.p8;
231 const int64_t p9 = this->calibration_.p9;
232
233 int64_t var1, var2, p;
234 var1 = int64_t(t_fine) - 128000;
235 var2 = var1 * var1 * p6;
236 var2 = var2 + ((var1 * p5) << 17);
237 var2 = var2 + (p4 << 35);
238 var1 = ((var1 * var1 * p3) >> 8) + ((var1 * p2) << 12);
239 var1 = ((int64_t(1) << 47) + var1) * p1 >> 33;
240
241 if (var1 == 0)
242 return NAN;
243
244 p = 1048576 - adc;
245 p = (((p << 31) - var2) * 3125) / var1;
246 var1 = (p9 * (p >> 13) * (p >> 13)) >> 25;
247 var2 = (p8 * p) >> 19;
248
249 p = ((p + var1 + var2) >> 8) + (p7 << 4);
250 return (p / 256.0f) / 100.0f;
251}
253 this->temperature_oversampling_ = temperature_over_sampling;
254}
256 this->pressure_oversampling_ = pressure_over_sampling;
257}
258void BMP280Component::set_iir_filter(BMP280IIRFilter iir_filter) { this->iir_filter_ = iir_filter; }
259uint8_t BMP280Component::read_u8_(uint8_t a_register) {
260 uint8_t data = 0;
261 this->read_byte(a_register, &data);
262 return data;
263}
264uint16_t BMP280Component::read_u16_le_(uint8_t a_register) {
265 uint16_t data = 0;
266 this->read_byte_16(a_register, &data);
267 return (data >> 8) | (data << 8);
268}
269int16_t BMP280Component::read_s16_le_(uint8_t a_register) { return this->read_u16_le_(a_register); }
270
271} // namespace bmp280_base
272} // namespace esphome
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.
enum esphome::bmp280_base::BMP280Component::ErrorCode NONE
uint8_t read_u8_(uint8_t a_register)
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:86
virtual bool read_bytes(uint8_t a_register, uint8_t *data, size_t len)=0
virtual bool read_byte_16(uint8_t a_register, uint16_t *data)=0
float read_temperature_(int32_t *t_fine)
Read the temperature value and store the calculated ambient temperature in t_fine.
void set_pressure_oversampling(BMP280Oversampling pressure_over_sampling)
Set the oversampling value for the pressure sensor. Default is 16x.
virtual bool write_byte(uint8_t a_register, uint8_t data)=0
BMP280Oversampling temperature_oversampling_
Definition bmp280_base.h:85
void set_temperature_oversampling(BMP280Oversampling temperature_over_sampling)
Set the oversampling value for the temperature sensor. Default is 16x.
BMP280CalibrationData calibration_
Definition bmp280_base.h:84
virtual bool read_byte(uint8_t a_register, uint8_t *data)=0
void set_iir_filter(BMP280IIRFilter iir_filter)
Set the IIR Filter used to increase accuracy, defaults to no IIR Filter.
float get_setup_priority() const override
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:45
const char * iir_filter_to_str(BME280IIRFilter filter)
uint16_t combine_bytes(uint8_t msb, uint8_t lsb)
BMP280IIRFilter
Enum listing all Infinite Impulse Filter values for the BMP280.
Definition bmp280_base.h:44
BMP280Oversampling
Enum listing all Oversampling values for the BMP280.
Definition bmp280_base.h:31
uint8_t oversampling_to_time(BMP280Oversampling over_sampling)
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