ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
xgzp6899dc.cpp
Go to the documentation of this file.
1#include "xgzp6899dc.h"
3#include "esphome/core/log.h"
4
5#include <cinttypes>
6#include <cstddef>
7
9
10ESPHOME_LOG_TAG(TAG, "xgzp6899dc.sensor");
11
12static constexpr uint32_t MEASUREMENT_TIMEOUT_ID = 0;
13
14static constexpr uint8_t ID_REGISTER = 0x00;
15static constexpr uint8_t CHIP_CONTROL_REGISTER = 0x01;
16static constexpr uint8_t CFG_OSR_REGISTER = 0x02;
17static constexpr uint8_t DATA_REGISTER = 0x04; // 3 pressure bytes then 2 temperature bytes, MSB first
18static constexpr uint8_t CAL_OFFSET_REGISTER = 0x20;
19static constexpr uint8_t CAL_SHIFT_REGISTER = 0x21;
20
21static constexpr uint8_t START_MEASUREMENT = 0x01;
22static constexpr uint8_t CFG_OSR_P_MASK = 0x1C;
23static constexpr uint8_t CFG_OSR_MODE_MASK = 0x03;
24static constexpr uint8_t CFG_OSR_MODE_NORMAL = 0x01;
25static constexpr uint32_t MEASUREMENT_TIME_MS = 20;
26// Pressure (Pa) = signed 24-bit raw / 2^21 * (PMAX - PMIN)
27static constexpr float PRESSURE_FULL_SCALE = 2097152.0f;
28static constexpr float TEMPERATURE_REFERENCE = 25.0f;
29
30// 2^exponent built from its bits, without pulling in libm; valid for exponents -126 to 127
31static float power_of_two(int exponent) { return bit_cast<float>(static_cast<uint32_t>(127 + exponent) << 23); }
32
33XGZP6899DCComponent::XGZP6899DCComponent(float pressure_span) : pressure_scale_(pressure_span / PRESSURE_FULL_SCALE) {}
34
36 uint8_t chip_id;
37 uint8_t cal_offset;
38 uint8_t cal_shift;
39 if (!this->read_byte(ID_REGISTER, &chip_id) || !this->read_byte(CAL_OFFSET_REGISTER, &cal_offset) ||
40 !this->read_byte(CAL_SHIFT_REGISTER, &cal_shift)) {
41 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
42 return;
43 }
44 // The ID register holds the I2C address of the part, so a mismatch most likely means a different sensor
45 if (chip_id != this->get_i2c_address()) {
46 ESP_LOGW(TAG, "ID register reads 0x%02X, expected 0x%02X; check the configured variant", chip_id,
47 this->get_i2c_address());
48 }
49
50 // Temperature = (raw - offset) / 2^shift + 25. Offset bits 6:0 are a power-of-two exponent and bit 7 is the
51 // sign, e.g. 0x8D = -8192; the shift is a tenth of its register
52 const float offset = power_of_two(cal_offset & 0x7F) * ((cal_offset & 0x80) ? -1.0f : 1.0f);
53 this->temperature_scale_ = power_of_two(-(cal_shift / 10));
54 this->temperature_bias_ = TEMPERATURE_REFERENCE - offset * this->temperature_scale_;
55 ESP_LOGD(TAG, "Cal_coff 0x%02X 0x%02X (offset %.0f, shift %u)", cal_offset, cal_shift, offset, cal_shift / 10);
56
57 if (this->pressure_oversampling_ != UINT8_MAX) {
58 if (!this->update_register(CFG_OSR_REGISTER, CFG_OSR_P_MASK | CFG_OSR_MODE_MASK,
59 (this->pressure_oversampling_ << 2) | CFG_OSR_MODE_NORMAL)) {
60 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
61 return;
62 }
63 }
64}
65
67 if (!this->status_comm_result(this->write_byte(CHIP_CONTROL_REGISTER, START_MEASUREMENT)))
68 return;
69 this->set_timeout(MEASUREMENT_TIMEOUT_ID, MEASUREMENT_TIME_MS, [this]() { this->read_data_(); });
70}
71
73 // The datasheet only documents single-register reads, so read each byte separately
74 uint8_t data[5];
75 for (size_t i = 0; i < sizeof(data); i++) {
76 if (!this->status_comm_result(this->read_byte(DATA_REGISTER + i, &data[i])))
77 return;
78 }
79 const uint32_t pressure_raw = encode_uint24(data[0], data[1], data[2]);
80 const int16_t temperature_raw = static_cast<int16_t>(encode_uint16(data[3], data[4]));
81 ESP_LOGV(TAG, "Got raw pressure=%" PRIu32 ", raw temperature=%d", pressure_raw, temperature_raw);
82
83 PUBLISH_STATE(this->pressure_sensor_, static_cast<float>(sign_extend<24>(pressure_raw)) * this->pressure_scale_);
84 PUBLISH_STATE(this->temperature_sensor_, temperature_raw * this->temperature_scale_ + this->temperature_bias_);
85}
86
88 ESP_LOGCONFIG(TAG, "XGZP6899DC:");
89 LOG_SENSOR(" ", "Temperature: ", this->temperature_sensor_);
90 LOG_SENSOR(" ", "Pressure: ", this->pressure_sensor_);
91 if (this->pressure_sensor_ != nullptr) {
92 ESP_LOGCONFIG(TAG, " Span: %.0f Pa", this->pressure_scale_ * PRESSURE_FULL_SCALE);
93 if (this->pressure_oversampling_ == UINT8_MAX) {
94 ESP_LOGCONFIG(TAG, " Oversampling: factory setting");
95 } else {
96 ESP_LOGCONFIG(TAG, " Oversampling: %" PRIu32 "x", UINT32_C(256) << this->pressure_oversampling_);
97 }
98 }
99 LOG_I2C_DEVICE(this);
100 LOG_UPDATE_INTERVAL(this);
101}
102
103} // namespace esphome::xgzp68xx
void mark_failed()
Mark this component as failed.
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
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
uint8_t get_i2c_address() const
Returns the I2C address of the object.
Definition i2c.h:155
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
bool update_register(uint8_t a_register, uint8_t mask, uint8_t value)
Read a register, replace the bits in mask with those of value and write it back.
Definition i2c.cpp:95
XGZP6899DCComponent(float pressure_span)
ESPHOME_LOG_TAG(TAG, "xgzp6899dc.sensor")
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 int32_t sign_extend(uint32_t value)
Sign-extend the low BITS bits of value, e.g. sign_extend<24>(0xFFFFFE) == -2. Higher bits are ignored...
Definition helpers.h:1057
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:1043
To bit_cast(const From &src)
Convert data between types, without aliasing issues or undefined behaviour.
Definition helpers.h:85
static void uint32_t