ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
dps310.cpp
Go to the documentation of this file.
1#include "dps310.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
4
5namespace esphome::dps310 {
6
7static const char *const TAG = "dps310";
8
10 uint8_t coef_data_raw[DPS310_NUM_COEF_REGS];
11 auto timer = DPS310_INIT_TIMEOUT;
12 uint8_t reg = 0;
13 // first, reset the sensor
14 if (!this->write_byte(DPS310_REG_RESET, DPS310_CMD_RESET)) {
15 this->mark_failed();
16 return;
17 }
18 delay(10);
19 // wait for the sensor and its coefficients to be ready
20 while (timer-- && (!(reg & DPS310_BIT_SENSOR_RDY) || !(reg & DPS310_BIT_COEF_RDY))) {
21 reg = this->read_byte(DPS310_REG_MEAS_CFG).value_or(0);
22 delay(5);
23 }
24
25 if (!(reg & DPS310_BIT_SENSOR_RDY) || !(reg & DPS310_BIT_COEF_RDY)) { // the flags were not set in time
26 this->mark_failed();
27 return;
28 }
29 // read device ID
30 if (!this->read_byte(DPS310_REG_PROD_REV_ID, &this->prod_rev_id_)) {
31 this->mark_failed();
32 return;
33 }
34 // read in coefficients used to calculate the compensated pressure and temperature values
35 if (!this->read_bytes(DPS310_REG_COEF, coef_data_raw, DPS310_NUM_COEF_REGS)) {
36 this->mark_failed();
37 return;
38 }
39 // read in coefficients source register, too -- we need this a few lines down
40 if (!this->read_byte(DPS310_REG_TMP_COEF_SRC, &reg)) {
41 this->mark_failed();
42 return;
43 }
44 // set up operational stuff
45 if (!this->write_byte(DPS310_REG_PRS_CFG, DPS310_VAL_PRS_CFG)) {
46 this->mark_failed();
47 return;
48 }
49 if (!this->write_byte(DPS310_REG_TMP_CFG, DPS310_VAL_TMP_CFG | (reg & DPS310_BIT_TMP_COEF_SRC))) {
50 this->mark_failed();
51 return;
52 }
53 if (!this->write_byte(DPS310_REG_CFG, DPS310_VAL_REG_CFG)) {
54 this->mark_failed();
55 return;
56 }
57 if (!this->write_byte(DPS310_REG_MEAS_CFG, 0x07)) { // enable background mode
58 this->mark_failed();
59 return;
60 }
61
62 this->c0_ = // we only ever use c0/2, so just divide by 2 here to save time later
64 int16_t(((uint16_t) coef_data_raw[0] << 4) | (((uint16_t) coef_data_raw[1] >> 4) & 0x0F)), 12) /
65 2;
66
67 this->c1_ =
68 DPS310Component::twos_complement(int16_t((((uint16_t) coef_data_raw[1] & 0x0F) << 8) | coef_data_raw[2]), 12);
69
70 this->c00_ = ((uint32_t) coef_data_raw[3] << 12) | ((uint32_t) coef_data_raw[4] << 4) |
71 (((uint32_t) coef_data_raw[5] >> 4) & 0x0F);
73
74 this->c10_ =
75 (((uint32_t) coef_data_raw[5] & 0x0F) << 16) | ((uint32_t) coef_data_raw[6] << 8) | (uint32_t) coef_data_raw[7];
77
78 this->c01_ = int16_t(((uint16_t) coef_data_raw[8] << 8) | (uint16_t) coef_data_raw[9]);
79 this->c11_ = int16_t(((uint16_t) coef_data_raw[10] << 8) | (uint16_t) coef_data_raw[11]);
80 this->c20_ = int16_t(((uint16_t) coef_data_raw[12] << 8) | (uint16_t) coef_data_raw[13]);
81 this->c21_ = int16_t(((uint16_t) coef_data_raw[14] << 8) | (uint16_t) coef_data_raw[15]);
82 this->c30_ = int16_t(((uint16_t) coef_data_raw[16] << 8) | (uint16_t) coef_data_raw[17]);
83}
84
86 ESP_LOGCONFIG(TAG,
87 "DPS310:\n"
88 " Product ID: %u\n"
89 " Revision ID: %u",
90 this->prod_rev_id_ & 0x0F, (this->prod_rev_id_ >> 4) & 0x0F);
91 LOG_I2C_DEVICE(this);
92 if (this->is_failed()) {
93 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
94 }
95 LOG_UPDATE_INTERVAL(this);
96 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
97 LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
98}
99
101 if (!this->update_in_progress_) {
102 this->update_in_progress_ = true;
103 this->read_();
104 }
105}
106
108 uint8_t reg = 0;
109 if (!this->read_byte(DPS310_REG_MEAS_CFG, &reg)) {
110 this->status_set_warning();
111 return;
112 }
113
114 if ((!this->got_pres_) && (reg & DPS310_BIT_PRS_RDY)) {
115 this->read_pressure_();
116 }
117
118 if ((!this->got_temp_) && (reg & DPS310_BIT_TMP_RDY)) {
119 this->read_temperature_();
120 }
121
122 if (this->got_pres_ && this->got_temp_) {
124 this->got_pres_ = false;
125 this->got_temp_ = false;
126 this->update_in_progress_ = false;
127 this->status_clear_warning();
128 } else {
129 this->set_timeout("dps310", 10, [this]() { this->read_(); });
130 }
131}
132
134 uint8_t bytes[3];
135 if (!this->read_bytes(DPS310_REG_PRS_B2, bytes, 3)) {
136 this->status_set_warning();
137 return;
138 }
139 this->got_pres_ = true;
141 int32_t((uint32_t(bytes[0]) << 16) | (uint32_t(bytes[1]) << 8) | (uint32_t(bytes[2]))), 24);
142}
143
145 uint8_t bytes[3];
146 if (!this->read_bytes(DPS310_REG_TMP_B2, bytes, 3)) {
147 this->status_set_warning();
148 return;
149 }
150 this->got_temp_ = true;
152 int32_t((uint32_t(bytes[0]) << 16) | (uint32_t(bytes[1]) << 8) | (uint32_t(bytes[2]))), 24);
153}
154
155// Calculations are taken from the datasheet which can be found here:
156// https://www.infineon.com/dgdl/Infineon-DPS310-DataSheet-v01_02-EN.pdf?fileId=5546d462576f34750157750826c42242
157// Sections "How to Calculate Compensated Pressure Values" and "How to Calculate Compensated Temperature Values"
158// Variable names below match variable names from the datasheet but lowercased
159void DPS310Component::calculate_values_(int32_t raw_temperature, int32_t raw_pressure) {
160 const float t_raw_sc = (float) raw_temperature / DPS310_SCALE_FACTOR;
161 const float p_raw_sc = (float) raw_pressure / DPS310_SCALE_FACTOR;
162
163 const float temperature = t_raw_sc * this->c1_ + this->c0_; // c0/2 done earlier!
164
165 const float pressure = (this->c00_ + p_raw_sc * (this->c10_ + p_raw_sc * (this->c20_ + p_raw_sc * this->c30_)) +
166 t_raw_sc * this->c01_ + t_raw_sc * p_raw_sc * (this->c11_ + p_raw_sc * this->c21_)) /
167 100; // divide by 100 for hPa
168
169 if (this->temperature_sensor_ != nullptr) {
170 this->temperature_sensor_->publish_state(temperature);
171 }
172 if (this->pressure_sensor_ != nullptr) {
173 this->pressure_sensor_->publish_state(pressure);
174 }
175}
176
177int32_t DPS310Component::twos_complement(int32_t val, uint8_t bits) {
178 if (val & ((uint32_t) 1 << (bits - 1))) {
179 val -= (uint32_t) 1 << bits;
180 }
181 return val;
182}
183
184} // namespace esphome::dps310
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:284
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:510
void status_clear_warning()
Definition component.h:306
sensor::Sensor * pressure_sensor_
Definition dps310.h:55
sensor::Sensor * temperature_sensor_
Definition dps310.h:54
void calculate_values_(int32_t raw_temperature, int32_t raw_pressure)
Definition dps310.cpp:159
static int32_t twos_complement(int32_t val, uint8_t bits)
Definition dps310.cpp:177
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:265
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:240
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:152
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:217
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
mopeka_std_values val[3]
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