ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
spa06_base.cpp
Go to the documentation of this file.
1#include "spa06_base.h"
2
3#include <cinttypes>
4
6
8
9ESPHOME_LOG_TAG(TAG, "spa06");
10
11static constexpr uint32_t MEASUREMENT_TIMEOUT_ID = 0;
12
13// Sign extension function for <=16 bit types
14inline int16_t decode16(uint8_t msb, uint8_t lsb, size_t bits, size_t head = 0) {
15 return static_cast<int16_t>(encode_uint16(msb, lsb) << head) >> (16 - bits);
16}
17
18// Sign extension function for <=32 bit types
19inline int32_t decode32(uint8_t xmsb, uint8_t msb, uint8_t lsb, uint8_t xlsb, size_t bits, size_t head = 0) {
20 return static_cast<int32_t>(encode_uint32(xmsb, msb, lsb, xlsb) << head) >> (32 - bits);
21}
22
24 ESP_LOGCONFIG(TAG, "SPA06:");
25 LOG_UPDATE_INTERVAL(this);
26 ESP_LOGCONFIG(TAG, " Measurement conversion time: %ums", this->conversion_time_);
27 if (this->temperature_sensor_) {
28 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
29 ESP_LOGCONFIG(TAG,
30 " Oversampling: %s\n"
31 " Rate: %s",
32 LOG_STR_ARG(OversamplingStrings::get_log_str(this->temperature_oversampling_)),
33 LOG_STR_ARG(MeasRateStrings::get_log_str(this->temperature_rate_)));
34 }
35 if (this->pressure_sensor_) {
36 LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
37 ESP_LOGCONFIG(TAG,
38 " Oversampling: %s\n"
39 " Rate: %s",
40 LOG_STR_ARG(OversamplingStrings::get_log_str(this->pressure_oversampling_)),
41 LOG_STR_ARG(MeasRateStrings::get_log_str(this->pressure_rate_)));
42 }
43}
44
46 // Startup sequence for SPA06 (Pg. 16, Figure 4.6.4):
47 // 1. Perform a soft reset
48 // 2. Verify sensor chip ID matches
49 // 3. Verify coefficients are ready
50 // 4. Read coefficients
51 // 5. Configure temperature and pressure sensors
52 // 6. Write communication settings
53 // 7. Write measurement settings (background measurement mode)
54
55 // 1. Soft reset
56 if (!this->setup_check(this->soft_reset_(), LOG_STR("Reset failed")))
57 return;
58
59 // soft_reset_() internally delays by 3ms to make sure that
60 // the sensor is in a ready state and coefficients are ready.
61
62 // 2. Read chip ID
63 // TODO: check ID for consistency?
64 if (!this->setup_check(spa_read_byte(SPA06_ID, &this->prod_id_.reg), LOG_STR("Chip ID read failure")))
65 return;
66 ESP_LOGV(TAG,
67 "Product Info:\n"
68 " Prod ID: %u\n"
69 " Rev ID: %u",
70 this->prod_id_.bit.prod_id, this->prod_id_.bit.rev_id);
71
72 // 3. Read chip readiness from MEAS_CFG
73 // First check if the sensor reports ready
74 if (!this->setup_check(spa_read_byte(SPA06_MEAS_CFG, &this->meas_.reg), LOG_STR("Sensor status read failure")))
75 return;
76 // Check if the sensor reports coefficients are ready
77 if (!this->setup_check(meas_.bit.coef_ready, LOG_STR("Coefficients not ready")))
78 return;
79
80 // 4. Read coefficients
81 if (!this->setup_check(this->read_coefficients_(), LOG_STR("Coefficients read error")))
82 return;
83
84 // 5. Configure temperature and pressure sensors
85 // Default to measuring both temperature and pressure
86
87 // Temperature must be read regardless of configuration to compute pressure
88 // If temperature is not configured in config:
89 // - No oversampling is used
90 // - Lowest possible rate is configured
91 if (!this->temperature_sensor_) {
95 }
96
97 // If pressure is not configured in config
98 // - No oversampling is used
99 // - Lowest possible rate is configured
100 if (!this->pressure_sensor_) {
104 }
105
106 // Write temperature settings
108 LOG_STR("Temperature settings write fail")))
109 return;
110
111 // Write pressure settings
113 LOG_STR("Pressure settings write fail")))
114 return;
115 // 6. Write communication settings
116 // This call sets the bit shifts for pressure and temperature if
117 // their respective oversampling config is > X8
118 // This call also disables interrupts, FIFO, and specifies SPI 4-wire
121 this->mark_failed(LOG_STR("Comm settings write fail"));
122 return;
123 }
124
125 // 7. Write measurement settings
126 // This function sets background measurement mode without FIFO
127 if (!this->setup_check(
129 LOG_STR("Measurement settings write fail")))
130 return;
131}
132
134 return this->write_sensor_settings_(oversampling, rate, SPA06_TMP_CFG);
135}
136
138 return this->write_sensor_settings_(oversampling, rate, SPA06_PSR_CFG);
139}
140
142 if (reg != SPA06_PSR_CFG && reg != SPA06_TMP_CFG) {
143 return false;
144 }
145 this->pt_meas_cfg_.bit.rate = rate;
146 this->pt_meas_cfg_.bit.prc = oversampling;
147 ESP_LOGD(TAG, "Config write: %02x", this->pt_meas_cfg_.reg);
148 return spa_write_byte(reg, this->pt_meas_cfg_.reg);
149}
150
152 this->meas_.bit.meas_crtl = crtl;
153 return spa_write_byte(SPA06_MEAS_CFG, this->meas_.reg);
154}
155
156bool SPA06Component::write_communication_settings_(bool pressure_shift, bool temperature_shift, bool interrupt_hl,
157 bool interrupt_fifo, bool interrupt_tmp, bool interrupt_prs,
158 bool enable_fifo, bool spi_3wire) {
159 this->cfg_.bit.p_shift = pressure_shift;
160 this->cfg_.bit.t_shift = temperature_shift;
161 this->cfg_.bit.int_hl = interrupt_hl;
162 this->cfg_.bit.int_fifo = interrupt_fifo;
163 this->cfg_.bit.int_tmp = interrupt_tmp;
164 this->cfg_.bit.int_prs = interrupt_prs;
165 this->cfg_.bit.fifo_en = enable_fifo;
166 this->cfg_.bit.spi_3wire = spi_3wire;
167 return spa_write_byte(SPA06_CFG_REG, this->cfg_.reg);
168}
169
171 uint8_t coef[SPA06_COEF_LEN];
173 return false;
174 }
175 this->c0_ = decode16(coef[0], coef[1], 12);
176 this->c1_ = decode16(coef[1], coef[2], 12, 4);
177 this->c00_ = decode32(coef[3], coef[4], coef[5], 0, 20);
178 this->c10_ = decode32(coef[5], coef[6], coef[7], 0, 20, 4);
179 this->c01_ = decode16(coef[8], coef[9], 16);
180 this->c11_ = decode16(coef[10], coef[11], 16);
181 this->c20_ = decode16(coef[12], coef[13], 16);
182 this->c21_ = decode16(coef[14], coef[15], 16);
183 this->c30_ = decode16(coef[16], coef[17], 16);
184 this->c31_ = decode16(coef[18], coef[19], 12);
185 this->c40_ = decode16(coef[19], coef[20], 12, 4);
186
187 ESP_LOGV(TAG,
188 "Coefficients:\n"
189 " c0: %i, c1: %i,\n"
190 " c00: %" PRIi32 ", c10: %" PRIi32 ", c20: %i, c30: %i, c40: %i,\n"
191 " c01: %i, c11: %i, c21: %i, c31: %i",
192 this->c0_, this->c1_, this->c00_, this->c10_, this->c20_, this->c30_, this->c40_, this->c01_, this->c11_,
193 this->c21_, this->c31_);
194 return true;
195}
196
198 // Setup steps for SPA06:
199 // 1. Perform a protocol reset (required to write command for SPI code, noop for I2C)
200 this->protocol_reset();
201
202 // 2. Perform the actual reset
203 this->reset_.bit.fifo_flush = true;
204 this->reset_.bit.soft_rst = SPA06_SOFT_RESET;
205 if (!this->spa_write_byte(SPA06_RESET, this->reset_.reg)) {
206 return false;
207 }
208
209 // 3. Wait for chip to become ready. Datasheet specifies 2 ms; wait 3
210 delay(3);
211 // 4. Perform another protocol reset (required for SPI code, noop for I2C)
212 this->protocol_reset();
213 return true;
214}
215
216// Temperature conversion formula. See datasheet pg. 14
217float SPA06Component::convert_temperature_(const float &t_raw_sc) { return this->c0_ * 0.5f + this->c1_ * t_raw_sc; }
218// Pressure conversion formula. See datasheet pg. 14
219float SPA06Component::convert_pressure_(const float &p_raw_sc, const float &t_raw_sc) {
220 float p2_raw_sc = p_raw_sc * p_raw_sc;
221 float p3_raw_sc = p2_raw_sc * p_raw_sc;
222 float p4_raw_sc = p3_raw_sc * p_raw_sc;
223 return this->c00_ + (float) this->c10_ * p_raw_sc + (float) this->c20_ * p2_raw_sc + (float) this->c30_ * p3_raw_sc +
224 (float) this->c40_ * p4_raw_sc +
225 t_raw_sc * ((float) this->c01_ + (float) this->c11_ * p_raw_sc + (float) this->c21_ * p2_raw_sc +
226 (float) this->c31_ * p3_raw_sc);
227}
228
230 // Verify either a temperature or pressure sensor is defined before proceeding
231 if ((!this->temperature_sensor_) && (!this->pressure_sensor_)) {
232 return;
233 }
234
235 // Queue a background task for retrieving the measurement
236 this->set_timeout(MEASUREMENT_TIMEOUT_ID, this->conversion_time_, [this]() {
237 float raw_temperature;
238 float temperature = 0.0;
239 float pressure = 0.0;
240
241 // Check measurement register for readiness
242 if (!this->spa_read_byte(SPA06_MEAS_CFG, &this->meas_.reg)) {
243 ESP_LOGW(TAG, "Cannot read meas config");
244 this->status_set_warning();
245 return;
246 }
247 if (this->pressure_sensor_) {
248 if (!this->meas_.bit.prs_ready || !this->meas_.bit.tmp_ready) {
249 ESP_LOGW(TAG, "Temperature and pressure not ready");
250 this->status_set_warning();
251 return;
252 }
253 if (!this->read_temperature_and_pressure_(temperature, pressure, raw_temperature)) {
254 ESP_LOGW(TAG, "Temperature and pressure read failure");
255 this->status_set_warning();
256 return;
257 }
258 } else {
259 if (!this->meas_.bit.tmp_ready) {
260 ESP_LOGW(TAG, "Temperature not ready");
261 this->status_set_warning();
262 return;
263 }
264 if (!this->read_temperature_(temperature, raw_temperature)) {
265 ESP_LOGW(TAG, "Temperature read fail");
266 this->status_set_warning();
267 return;
268 }
269 }
270 if (this->temperature_sensor_) {
271 this->temperature_sensor_->publish_state(temperature);
272 } else {
273 ESP_LOGV(TAG, "No temperature sensor configured");
274 }
275 if (this->pressure_sensor_) {
276 this->pressure_sensor_->publish_state(pressure);
277 } else {
278 ESP_LOGV(TAG, "No pressure sensor configured");
279 }
280 this->status_clear_warning();
281 });
282}
283
285 // Temperature read and decode
286 if (!this->read_temperature_(temperature, t_raw_sc)) {
287 return false;
288 }
289 // Read raw pressure from device
290 uint8_t buf[3];
291 if (!this->spa_read_bytes(SPA06_PSR, buf, 3)) {
292 return false;
293 }
294 // Calculate raw scaled pressure value
295 float p_raw_sc = (float) decode32(buf[0], buf[1], buf[2], 0, 24) / (float) this->kp_;
296
297 // Calculate full pressure values
298 pressure = this->convert_pressure_(p_raw_sc, t_raw_sc);
299 return true;
300}
301
302bool SPA06Component::read_temperature_(float &temperature, float &t_raw_sc) {
303 uint8_t buf[3];
304 if (!this->spa_read_bytes(SPA06_TMP, buf, 3)) {
305 return false;
306 }
307
308 t_raw_sc = (float) decode32(buf[0], buf[1], buf[2], 0, 24) / (float) this->kt_;
309 temperature = this->convert_temperature_(t_raw_sc);
310 return true;
311}
312} // namespace esphome::spa06_base
void mark_failed()
Mark this component as failed.
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:236
void status_clear_warning()
Definition component.h:318
void set_timeout(const char *name, uint32_t timeout, SchedulerCallback &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
union esphome::spa06_base::SPA06Component::@170 prod_id_
virtual bool spa_read_bytes(uint8_t reg, uint8_t *data, size_t len)=0
union esphome::spa06_base::SPA06Component::@164 pt_meas_cfg_
union esphome::spa06_base::SPA06Component::@165 meas_
bool write_measurement_settings_(MeasCrtl crtl)
float convert_temperature_(const float &t_raw_sc)
bool read_temperature_(float &temperature, float &t_raw_sc)
virtual bool spa_write_byte(uint8_t reg, uint8_t data)=0
bool read_temperature_and_pressure_(float &temperature, float &pressure, float &t_raw_sc)
bool write_temperature_settings_(Oversampling oversampling, SampleRate rate)
union esphome::spa06_base::SPA06Component::@167 cfg_
bool write_pressure_settings_(Oversampling oversampling, SampleRate rate)
virtual bool spa_read_byte(uint8_t reg, uint8_t *data)=0
bool write_communication_settings_(bool pressure_shift, bool temperature_shift, bool interrupt_hl=false, bool interrupt_fifo=false, bool interrupt_tmp=false, bool interrupt_prs=false, bool enable_fifo=false, bool spi_3wire=false)
bool write_sensor_settings_(Oversampling oversampling, SampleRate rate, uint8_t reg)
float convert_pressure_(const float &p_raw_sc, const float &t_raw_sc)
union esphome::spa06_base::SPA06Component::@169 reset_
constexpr size_t SPA06_COEF_LEN
Definition spa06_base.h:15
int32_t decode32(uint8_t xmsb, uint8_t msb, uint8_t lsb, uint8_t xlsb, size_t bits, size_t head=0)
uint32_t oversampling_to_scale_factor(const Oversampling oversampling)
Definition spa06_base.h:90
constexpr uint8_t SPA06_SOFT_RESET
Definition spa06_base.h:18
int16_t decode16(uint8_t msb, uint8_t lsb, size_t bits, size_t head=0)
ESPHOME_LOG_TAG(TAG, "spa06")
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:1235
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:1227
void HOT delay(uint32_t ms)
Definition hal.cpp:78
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12
uint8_t pressure
Definition tt21100.cpp:7