ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
qmp6988.cpp
Go to the documentation of this file.
1#include "qmp6988.h"
2
3#include <cinttypes>
4#include <cmath>
5
6namespace esphome::qmp6988 {
7
8static const uint8_t QMP6988_CHIP_ID = 0x5C;
9
10static const uint8_t QMP6988_CHIP_ID_REG = 0xD1; /* Chip ID confirmation Register */
11static const uint8_t QMP6988_RESET_REG = 0xE0; /* Device reset register */
12static const uint8_t QMP6988_DEVICE_STAT_REG = 0xF3; /* Device state register */
13static const uint8_t QMP6988_CTRLMEAS_REG = 0xF4; /* Measurement Condition Control Register */
14/* data */
15static const uint8_t QMP6988_PRESSURE_MSB_REG = 0xF7; /* Pressure MSB Register */
16static const uint8_t QMP6988_TEMPERATURE_MSB_REG = 0xFA; /* Temperature MSB Reg */
17
18/* compensation calculation */
19static const uint8_t QMP6988_CALIBRATION_DATA_START = 0xA0; /* QMP6988 compensation coefficients */
20static const uint8_t QMP6988_CALIBRATION_DATA_LENGTH = 25;
21
22/* power mode */
23static const uint8_t QMP6988_SLEEP_MODE = 0x00;
24static const uint8_t QMP6988_FORCED_MODE = 0x01;
25static const uint8_t QMP6988_NORMAL_MODE = 0x03;
26
27static const uint8_t QMP6988_CTRLMEAS_REG_MODE_POS = 0;
28static const uint8_t QMP6988_CTRLMEAS_REG_MODE_MSK = 0x03;
29static const uint8_t QMP6988_CTRLMEAS_REG_MODE_LEN = 2;
30
31static const uint8_t QMP6988_CTRLMEAS_REG_OSRST_POS = 5;
32static const uint8_t QMP6988_CTRLMEAS_REG_OSRST_MSK = 0xE0;
33static const uint8_t QMP6988_CTRLMEAS_REG_OSRST_LEN = 3;
34
35static const uint8_t QMP6988_CTRLMEAS_REG_OSRSP_POS = 2;
36static const uint8_t QMP6988_CTRLMEAS_REG_OSRSP_MSK = 0x1C;
37static const uint8_t QMP6988_CTRLMEAS_REG_OSRSP_LEN = 3;
38
39static const uint8_t QMP6988_CONFIG_REG = 0xF1; /*IIR filter co-efficient setting Register*/
40static const uint8_t QMP6988_CONFIG_REG_FILTER_POS = 0;
41static const uint8_t QMP6988_CONFIG_REG_FILTER_MSK = 0x07;
42static const uint8_t QMP6988_CONFIG_REG_FILTER_LEN = 3;
43
44static const uint32_t SUBTRACTOR = 8388608;
45
46static const char *const TAG = "qmp6988";
47
48static const char *oversampling_to_str(QMP6988Oversampling oversampling) {
49 switch (oversampling) {
51 return "None";
53 return "1x";
55 return "2x";
57 return "4x";
59 return "8x";
61 return "16x";
63 return "32x";
65 return "64x";
66 default:
67 return "UNKNOWN";
68 }
69}
70
71static const char *iir_filter_to_str(QMP6988IIRFilter filter) {
72 switch (filter) {
74 return "OFF";
76 return "2x";
78 return "4x";
80 return "8x";
82 return "16x";
84 return "32x";
85 default:
86 return "UNKNOWN";
87 }
88}
89
91 if (this->read_register(QMP6988_CHIP_ID_REG, &(qmp6988_data_.chip_id), 1) != i2c::ERROR_OK) {
92 ESP_LOGE(TAG, "Read chip ID (0xD1) failed");
93 return false;
94 }
95 ESP_LOGV(TAG, "Read chip ID = 0x%x", qmp6988_data_.chip_id);
96
97 return qmp6988_data_.chip_id == QMP6988_CHIP_ID;
98}
99
101 // BITFIELDS temp_COE;
102 uint8_t a_data_uint8_tr[QMP6988_CALIBRATION_DATA_LENGTH] = {0};
103
104 for (uint8_t len = 0; len < QMP6988_CALIBRATION_DATA_LENGTH; len += 1) {
105 if (this->read_register(QMP6988_CALIBRATION_DATA_START + len, &a_data_uint8_tr[len], 1) != i2c::ERROR_OK) {
106 ESP_LOGE(TAG, "Read calibration data (0xA0) error");
107 return false;
108 }
109 }
110
111 qmp6988_data_.qmp6988_cali.COE_a0 =
112 (int32_t) encode_uint32(a_data_uint8_tr[18], a_data_uint8_tr[19], (a_data_uint8_tr[24] & 0x0f) << 4, 0);
113 qmp6988_data_.qmp6988_cali.COE_a0 = qmp6988_data_.qmp6988_cali.COE_a0 >> 12;
114
115 qmp6988_data_.qmp6988_cali.COE_a1 = (int16_t) encode_uint16(a_data_uint8_tr[20], a_data_uint8_tr[21]);
116 qmp6988_data_.qmp6988_cali.COE_a2 = (int16_t) encode_uint16(a_data_uint8_tr[22], a_data_uint8_tr[23]);
117
118 qmp6988_data_.qmp6988_cali.COE_b00 =
119 (int32_t) encode_uint32(a_data_uint8_tr[0], a_data_uint8_tr[1], a_data_uint8_tr[24] & 0xf0, 0);
120 qmp6988_data_.qmp6988_cali.COE_b00 = qmp6988_data_.qmp6988_cali.COE_b00 >> 12;
121
122 qmp6988_data_.qmp6988_cali.COE_bt1 = (int16_t) encode_uint16(a_data_uint8_tr[2], a_data_uint8_tr[3]);
123 qmp6988_data_.qmp6988_cali.COE_bt2 = (int16_t) encode_uint16(a_data_uint8_tr[4], a_data_uint8_tr[5]);
124 qmp6988_data_.qmp6988_cali.COE_bp1 = (int16_t) encode_uint16(a_data_uint8_tr[6], a_data_uint8_tr[7]);
125 qmp6988_data_.qmp6988_cali.COE_b11 = (int16_t) encode_uint16(a_data_uint8_tr[8], a_data_uint8_tr[9]);
126 qmp6988_data_.qmp6988_cali.COE_bp2 = (int16_t) encode_uint16(a_data_uint8_tr[10], a_data_uint8_tr[11]);
127 qmp6988_data_.qmp6988_cali.COE_b12 = (int16_t) encode_uint16(a_data_uint8_tr[12], a_data_uint8_tr[13]);
128 qmp6988_data_.qmp6988_cali.COE_b21 = (int16_t) encode_uint16(a_data_uint8_tr[14], a_data_uint8_tr[15]);
129 qmp6988_data_.qmp6988_cali.COE_bp3 = (int16_t) encode_uint16(a_data_uint8_tr[16], a_data_uint8_tr[17]);
130
131 ESP_LOGV(TAG,
132 "Calibration data:\n"
133 " COE_a0[%" PRId32 "] COE_a1[%d] COE_a2[%d] COE_b00[%" PRId32 "]\n"
134 " COE_bt1[%d] COE_bt2[%d] COE_bp1[%d] COE_b11[%d]\n"
135 " COE_bp2[%d] COE_b12[%d] COE_b21[%d] COE_bp3[%d]",
136 qmp6988_data_.qmp6988_cali.COE_a0, qmp6988_data_.qmp6988_cali.COE_a1, qmp6988_data_.qmp6988_cali.COE_a2,
137 qmp6988_data_.qmp6988_cali.COE_b00, qmp6988_data_.qmp6988_cali.COE_bt1, qmp6988_data_.qmp6988_cali.COE_bt2,
138 qmp6988_data_.qmp6988_cali.COE_bp1, qmp6988_data_.qmp6988_cali.COE_b11, qmp6988_data_.qmp6988_cali.COE_bp2,
139 qmp6988_data_.qmp6988_cali.COE_b12, qmp6988_data_.qmp6988_cali.COE_b21, qmp6988_data_.qmp6988_cali.COE_bp3);
140
141 qmp6988_data_.ik.a0 = qmp6988_data_.qmp6988_cali.COE_a0; // 20Q4
142 qmp6988_data_.ik.b00 = qmp6988_data_.qmp6988_cali.COE_b00; // 20Q4
143
144 qmp6988_data_.ik.a1 = 3608L * (int32_t) qmp6988_data_.qmp6988_cali.COE_a1 - 1731677965L; // 31Q23
145 qmp6988_data_.ik.a2 = 16889L * (int32_t) qmp6988_data_.qmp6988_cali.COE_a2 - 87619360L; // 30Q47
146
147 qmp6988_data_.ik.bt1 = 2982L * (int64_t) qmp6988_data_.qmp6988_cali.COE_bt1 + 107370906L; // 28Q15
148 qmp6988_data_.ik.bt2 = 329854L * (int64_t) qmp6988_data_.qmp6988_cali.COE_bt2 + 108083093L; // 34Q38
149 qmp6988_data_.ik.bp1 = 19923L * (int64_t) qmp6988_data_.qmp6988_cali.COE_bp1 + 1133836764L; // 31Q20
150 qmp6988_data_.ik.b11 = 2406L * (int64_t) qmp6988_data_.qmp6988_cali.COE_b11 + 118215883L; // 28Q34
151 qmp6988_data_.ik.bp2 = 3079L * (int64_t) qmp6988_data_.qmp6988_cali.COE_bp2 - 181579595L; // 29Q43
152 qmp6988_data_.ik.b12 = 6846L * (int64_t) qmp6988_data_.qmp6988_cali.COE_b12 + 85590281L; // 29Q53
153 qmp6988_data_.ik.b21 = 13836L * (int64_t) qmp6988_data_.qmp6988_cali.COE_b21 + 79333336L; // 29Q60
154 qmp6988_data_.ik.bp3 = 2915L * (int64_t) qmp6988_data_.qmp6988_cali.COE_bp3 + 157155561L; // 28Q65
155 ESP_LOGV(TAG,
156 "Int calibration data:\n"
157 " a0[%" PRId32 "] a1[%" PRId32 "] a2[%" PRId32 "] b00[%" PRId32 "]\n"
158 " bt1[%lld] bt2[%lld] bp1[%lld] b11[%lld]\n"
159 " bp2[%lld] b12[%lld] b21[%lld] bp3[%lld]",
160 qmp6988_data_.ik.a0, qmp6988_data_.ik.a1, qmp6988_data_.ik.a2, qmp6988_data_.ik.b00, qmp6988_data_.ik.bt1,
161 qmp6988_data_.ik.bt2, qmp6988_data_.ik.bp1, qmp6988_data_.ik.b11, qmp6988_data_.ik.bp2, qmp6988_data_.ik.b12,
162 qmp6988_data_.ik.b21, qmp6988_data_.ik.bp3);
163 return true;
164}
165
167 int16_t ret;
168 int64_t wk1, wk2;
169
170 // wk1: 60Q4 // bit size
171 wk1 = ((int64_t) ik->a1 * (int64_t) dt); // 31Q23+24-1=54 (54Q23)
172 wk2 = ((int64_t) ik->a2 * (int64_t) dt) >> 14; // 30Q47+24-1=53 (39Q33)
173 wk2 = (wk2 * (int64_t) dt) >> 10; // 39Q33+24-1=62 (52Q23)
174 wk2 = ((wk1 + wk2) / 32767) >> 19; // 54,52->55Q23 (20Q04)
175 ret = (int16_t) ((ik->a0 + wk2) >> 4); // 21Q4 -> 17Q0
176 return ret;
177}
178
180 int32_t ret;
181 int64_t wk1, wk2, wk3;
182
183 // wk1 = 48Q16 // bit size
184 wk1 = ((int64_t) ik->bt1 * (int64_t) tx); // 28Q15+16-1=43 (43Q15)
185 wk2 = ((int64_t) ik->bp1 * (int64_t) dp) >> 5; // 31Q20+24-1=54 (49Q15)
186 wk1 += wk2; // 43,49->50Q15
187 wk2 = ((int64_t) ik->bt2 * (int64_t) tx) >> 1; // 34Q38+16-1=49 (48Q37)
188 wk2 = (wk2 * (int64_t) tx) >> 8; // 48Q37+16-1=63 (55Q29)
189 wk3 = wk2; // 55Q29
190 wk2 = ((int64_t) ik->b11 * (int64_t) tx) >> 4; // 28Q34+16-1=43 (39Q30)
191 wk2 = (wk2 * (int64_t) dp) >> 1; // 39Q30+24-1=62 (61Q29)
192 wk3 += wk2; // 55,61->62Q29
193 wk2 = ((int64_t) ik->bp2 * (int64_t) dp) >> 13; // 29Q43+24-1=52 (39Q30)
194 wk2 = (wk2 * (int64_t) dp) >> 1; // 39Q30+24-1=62 (61Q29)
195 wk3 += wk2; // 62,61->63Q29
196 wk1 += wk3 >> 14; // Q29 >> 14 -> Q15
197 wk2 = ((int64_t) ik->b12 * (int64_t) tx); // 29Q53+16-1=45 (45Q53)
198 wk2 = (wk2 * (int64_t) tx) >> 22; // 45Q53+16-1=61 (39Q31)
199 wk2 = (wk2 * (int64_t) dp) >> 1; // 39Q31+24-1=62 (61Q30)
200 wk3 = wk2; // 61Q30
201 wk2 = ((int64_t) ik->b21 * (int64_t) tx) >> 6; // 29Q60+16-1=45 (39Q54)
202 wk2 = (wk2 * (int64_t) dp) >> 23; // 39Q54+24-1=62 (39Q31)
203 wk2 = (wk2 * (int64_t) dp) >> 1; // 39Q31+24-1=62 (61Q20)
204 wk3 += wk2; // 61,61->62Q30
205 wk2 = ((int64_t) ik->bp3 * (int64_t) dp) >> 12; // 28Q65+24-1=51 (39Q53)
206 wk2 = (wk2 * (int64_t) dp) >> 23; // 39Q53+24-1=62 (39Q30)
207 wk2 = (wk2 * (int64_t) dp); // 39Q30+24-1=62 (62Q30)
208 wk3 += wk2; // 62,62->63Q30
209 wk1 += wk3 >> 15; // Q30 >> 15 = Q15
210 wk1 /= 32767L;
211 wk1 >>= 11; // Q15 >> 7 = Q4
212 wk1 += ik->b00; // Q4 + 20Q4
213 // wk1 >>= 4; // 28Q4 -> 24Q0
214 ret = (int32_t) wk1;
215 return ret;
216}
217
219 uint8_t ret = 0;
220
221 ret = this->write_byte(QMP6988_RESET_REG, 0xe6);
222 if (ret != i2c::ERROR_OK) {
223 ESP_LOGE(TAG, "Software Reset (0xe6) failed");
224 }
225 delay(10);
226
227 this->write_byte(QMP6988_RESET_REG, 0x00);
228}
229
231 uint8_t data;
232
233 ESP_LOGD(TAG, "Setting Power mode to: %d", power_mode);
234
235 qmp6988_data_.power_mode = power_mode;
236 this->read_register(QMP6988_CTRLMEAS_REG, &data, 1);
237 data = data & 0xfc;
238 if (power_mode == QMP6988_SLEEP_MODE) {
239 data |= 0x00;
240 } else if (power_mode == QMP6988_FORCED_MODE) {
241 data |= 0x01;
242 } else if (power_mode == QMP6988_NORMAL_MODE) {
243 data |= 0x03;
244 }
245 this->write_byte(QMP6988_CTRLMEAS_REG, data);
246
247 ESP_LOGD(TAG, "Set Power mode 0xf4=0x%x \r\n", data);
248
249 delay(10);
250}
251
253 uint8_t data;
254
255 data = (filter & QMP6988_CONFIG_REG_FILTER_MSK);
256 this->write_byte(QMP6988_CONFIG_REG, data);
257 delay(10);
258}
259
261 uint8_t data;
262
263 this->read_register(QMP6988_CTRLMEAS_REG, &data, 1);
264 data &= 0xe3;
265 data |= (oversampling_p << 2);
266 this->write_byte(QMP6988_CTRLMEAS_REG, data);
267 delay(10);
268}
269
271 uint8_t data;
272
273 this->read_register(QMP6988_CTRLMEAS_REG, &data, 1);
274 data &= 0x1f;
275 data |= (oversampling_t << 5);
276 this->write_byte(QMP6988_CTRLMEAS_REG, data);
277 delay(10);
278}
279
281 float altitude;
282 altitude = (pow((101325 / pressure), 1 / 5.257) - 1) * (temp + 273.15) / 0.0065;
283 this->qmp6988_data_.altitude = altitude;
284}
285
287 uint8_t err = 0;
288 uint32_t p_read, t_read;
289 int32_t p_raw, t_raw;
290 uint8_t a_data_uint8_tr[6] = {0};
291 int32_t t_int, p_int;
292 this->qmp6988_data_.temperature = 0;
293 this->qmp6988_data_.pressure = 0;
294
295 err = this->read_register(QMP6988_PRESSURE_MSB_REG, a_data_uint8_tr, 6);
296 if (err != i2c::ERROR_OK) {
297 ESP_LOGE(TAG, "Error reading raw pressure/temp values");
298 return;
299 }
300 p_read = encode_uint24(a_data_uint8_tr[0], a_data_uint8_tr[1], a_data_uint8_tr[2]);
301 p_raw = (int32_t) (p_read - SUBTRACTOR);
302
303 t_read = encode_uint24(a_data_uint8_tr[3], a_data_uint8_tr[4], a_data_uint8_tr[5]);
304 t_raw = (int32_t) (t_read - SUBTRACTOR);
305
306 t_int = this->get_compensated_temperature_(&(qmp6988_data_.ik), t_raw);
307 p_int = this->get_compensated_pressure_(&(qmp6988_data_.ik), p_raw, t_int);
308
309 this->qmp6988_data_.temperature = (float) t_int / 256.0f;
310 this->qmp6988_data_.pressure = (float) p_int / 16.0f;
311}
312
314 if (!this->device_check_()) {
315 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
316 return;
317 }
318
319 this->software_reset_();
320 this->get_calibration_data_();
321 this->set_power_mode_(QMP6988_NORMAL_MODE);
325}
326
328 ESP_LOGCONFIG(TAG, "QMP6988:");
329 LOG_I2C_DEVICE(this);
330 LOG_UPDATE_INTERVAL(this);
331
332 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
333 ESP_LOGCONFIG(TAG, " Temperature Oversampling: %s", oversampling_to_str(this->temperature_oversampling_));
334 LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
335 ESP_LOGCONFIG(TAG,
336 " Pressure Oversampling: %s\n"
337 " IIR Filter: %s",
338 oversampling_to_str(this->pressure_oversampling_), iir_filter_to_str(this->iir_filter_));
339}
340
342 this->calculate_pressure_();
343 float pressurehectopascals = this->qmp6988_data_.pressure / 100;
344 float temperature = this->qmp6988_data_.temperature;
345
346 ESP_LOGD(TAG, "Temperature=%.2f°C, Pressure=%.2fhPa", temperature, pressurehectopascals);
347 if (this->temperature_sensor_ != nullptr)
348 this->temperature_sensor_->publish_state(temperature);
349 if (this->pressure_sensor_ != nullptr)
350 this->pressure_sensor_->publish_state(pressurehectopascals);
351}
352
353} // namespace esphome::qmp6988
void mark_failed()
Mark this component as failed.
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:265
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:25
void write_filter_(QMP6988IIRFilter filter)
Definition qmp6988.cpp:252
sensor::Sensor * temperature_sensor_
Definition qmp6988.h:87
void calculate_altitude_(float pressure, float temp)
Definition qmp6988.cpp:280
int32_t get_compensated_pressure_(qmp6988_ik_data_t *ik, int32_t dp, int16_t tx)
Definition qmp6988.cpp:179
QMP6988Oversampling pressure_oversampling_
Definition qmp6988.h:91
void write_oversampling_pressure_(QMP6988Oversampling oversampling_p)
Definition qmp6988.cpp:260
sensor::Sensor * pressure_sensor_
Definition qmp6988.h:88
QMP6988Oversampling temperature_oversampling_
Definition qmp6988.h:90
int16_t get_compensated_temperature_(qmp6988_ik_data_t *ik, int32_t dt)
Definition qmp6988.cpp:166
void set_power_mode_(uint8_t power_mode)
Definition qmp6988.cpp:230
void write_oversampling_temperature_(QMP6988Oversampling oversampling_t)
Definition qmp6988.cpp:270
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
PowerMode power_mode
Definition msa3xx.h:3
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
struct Qmp6988IkData { int32_t a0, b00; int32_t a1, a2; int64_t bt1, bt2, bp1, b11, bp2, b12, b21, bp3;} qmp6988_ik_data_t
Definition qmp6988.h:54
@ QMP6988_OVERSAMPLING_8X
Definition qmp6988.h:18
@ QMP6988_OVERSAMPLING_SKIPPED
Definition qmp6988.h:14
@ QMP6988_OVERSAMPLING_16X
Definition qmp6988.h:19
@ QMP6988_OVERSAMPLING_32X
Definition qmp6988.h:20
@ QMP6988_OVERSAMPLING_2X
Definition qmp6988.h:16
@ QMP6988_OVERSAMPLING_64X
Definition qmp6988.h:21
@ QMP6988_OVERSAMPLING_1X
Definition qmp6988.h:15
@ QMP6988_OVERSAMPLING_4X
Definition qmp6988.h:17
@ QMP6988_IIR_FILTER_2X
Definition qmp6988.h:27
@ QMP6988_IIR_FILTER_4X
Definition qmp6988.h:28
@ QMP6988_IIR_FILTER_16X
Definition qmp6988.h:30
@ QMP6988_IIR_FILTER_8X
Definition qmp6988.h:29
@ QMP6988_IIR_FILTER_32X
Definition qmp6988.h:31
@ QMP6988_IIR_FILTER_OFF
Definition qmp6988.h:26
std::string size_t len
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:863
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:867
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:859
void HOT delay(uint32_t ms)
Definition hal.cpp:82
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12
uint8_t pressure
Definition tt21100.cpp:7