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