ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
spa06_base.h
Go to the documentation of this file.
1// SPA06 interface code for ESPHome
2// All datasheet page references refer to Goermicro SPA06-003 datasheet version 2.0
3
4#pragma once
5
7#include "esphome/core/hal.h"
10
11namespace esphome::spa06_base {
12
13// Read sizes. All other registers are size 1
14constexpr size_t SPA06_MEAS_LEN = 3;
15constexpr size_t SPA06_COEF_LEN = 21;
16
17// Soft reset command (0b1001, 0x9)
18constexpr uint8_t SPA06_SOFT_RESET = 0x9;
19
20// SPA06 Register Addresses
21enum Register : uint8_t {
22 SPA06_PSR = 0x00, // Pressure Reading MSB (or all 3)
23 SPA06_PSR_B1 = 0x01, // Pressure Reading LSB
24 SPA06_PSR_B0 = 0x02, // Pressure Reading XLSB (LSB: Pressure flag in FIFO)
25 SPA06_TMP = 0x03, // Temperature Reading MSB (or all 3)
26 SPA06_TMP_B1 = 0x04, // Temperature Reading LSB
27 SPA06_TMP_B0 = 0x05, // Temperature Reading XLSB
28 SPA06_PSR_CFG = 0x06, // Pressure Configuration
29 SPA06_TMP_CFG = 0x07, // Temperature Configuration
30 SPA06_MEAS_CFG = 0x08, // Measurement Configuration (includes readiness)
31 SPA06_CFG_REG = 0x09, // Configuration Register
32 SPA06_INT_STS = 0x0A, // Interrupt Status
33 SPA06_FIFO_STS = 0x0B, // FIFO Status
34 SPA06_RESET = 0x0C, // Reset + FIFO Flush
35 SPA06_ID = 0x0D, // Product ID and revision
36 SPA06_COEF = 0x10, // Coefficients (0x10-0x24)
37 SPA06_INVALID_CMD = 0x25, // End of enum command
38};
39
40// Oversampling config.
52
53// Measuring rate config
72
73// Measuring control config, set in MEAS_CFG register.
74// See datasheet pages 28-29
83
84// Oversampling scale factors. See datasheet page 15.
85constexpr uint32_t OVERSAMPLING_K_LUT[8] = {524288, 1572864, 3670016, 7864320, 253952, 516096, 1040384, 2088960};
86PROGMEM_STRING_TABLE(MeasRateStrings, "1Hz", "2Hz", "4Hz", "8Hz", "16Hz", "32Hz", "64Hz", "128Hz", "1.5625Hz",
87 "3.125Hz", "6.25Hz", "12.5Hz", "25Hz", "50Hz", "100Hz", "200Hz");
88PROGMEM_STRING_TABLE(OversamplingStrings, "X1", "X2", "X4", "X8", "X16", "X32", "X64", "X128");
89
91 return OVERSAMPLING_K_LUT[static_cast<uint8_t>(oversampling)];
92};
93
95 public:
97 void setup() override;
98 void update() override;
99 void dump_config() override;
100
102 void set_conversion_time(uint16_t conversion_time) { this->conversion_time_ = conversion_time; }
105 void set_temperature_oversampling_config(Oversampling temperature_oversampling) {
106 this->temperature_oversampling_ = temperature_oversampling;
107 this->kt_ = oversampling_to_scale_factor(temperature_oversampling);
108 }
109 void set_pressure_oversampling_config(Oversampling pressure_oversampling) {
110 this->pressure_oversampling_ = pressure_oversampling;
111 this->kp_ = oversampling_to_scale_factor(pressure_oversampling);
112 }
115
116 protected:
117 // Virtual read functions. Implemented in SPI/I2C components
118 virtual bool spa_read_byte(uint8_t reg, uint8_t *data) = 0;
119 virtual bool spa_write_byte(uint8_t reg, uint8_t data) = 0;
120 virtual bool spa_read_bytes(uint8_t reg, uint8_t *data, size_t len) = 0;
121 virtual bool spa_write_bytes(uint8_t reg, uint8_t *data, size_t len) = 0;
122
124 // Soft reset
125 bool soft_reset_();
126 // Protocol-specific reset (used for SPI only, implemented as noop for I2C)
127 virtual void protocol_reset() {}
128 // Read temperature and calculate Celsius and scaled raw temperatures
129 bool read_temperature_(float &temperature, float &t_raw_sc);
130 // No pressure only read! Pressure calculation depends on scaled temperature value
131 // Read temperature and calculate Celsius temperature, Pascal pressure, and scaled raw temperature
132 bool read_temperature_and_pressure_(float &temperature, float &pressure, float &t_raw_sc);
133 // Read coefficients. Stores in class variables.
134 bool read_coefficients_();
135
137 // Write temperature settings to TMP_CFG register
139 // Write pressure settings to PRS_CFG register
141 // Write measurement settings to MEAS_CRTL register
143
144 // Write communication settings to CFG_REG register
145 // Set pressure_shift to true if pressure oversampling >X8
146 // Set temperature_shift to true if temperature oversampling >X8
147 bool write_communication_settings_(bool pressure_shift, bool temperature_shift, bool interrupt_hl = false,
148 bool interrupt_fifo = false, bool interrupt_tmp = false,
149 bool interrupt_prs = false, bool enable_fifo = false, bool spi_3wire = false);
150
152 // Write function for both temperature and pressure (deduplicates code)
153 bool write_sensor_settings_(Oversampling oversampling, SampleRate rate, uint8_t reg);
154 // Convert raw temperature reading into Celsius
155 float convert_temperature_(const float &t_raw_sc);
156 // Convert raw pressure and scaled raw temperature into Pascals
157 float convert_pressure_(const float &p_raw_sc, const float &t_raw_sc);
158
160 // Oversampling scale factors. Defaults are for X16 (pressure) and X1 (temp)
161 uint32_t kp_{253952}, kt_{524288};
162 // Coefficients for calculating pressure and temperature from raw values
163 // Obtained from IC during setup
164 int32_t c00_{0}, c10_{0};
165 int16_t c0_{0}, c1_{0}, c01_{0}, c11_{0}, c20_{0}, c21_{0}, c30_{0}, c31_{0}, c40_{0};
166
172 // Default conversion time: 27.6ms (16x pres) + 3.6ms (1x temp) ~ 32ms
173 uint16_t conversion_time_{32};
174
175 union {
176 struct {
180 uint8_t reg;
181 } pt_meas_cfg_ = {.reg = 0}; // PRS_CFG and TMP_CFG
182
183 union {
184 struct {
185 uint8_t meas_crtl : 3;
186 bool tmp_ext : 1;
187 bool prs_ready : 1;
188 bool tmp_ready : 1;
189 bool sensor_ready : 1;
190 bool coef_ready : 1;
192 uint8_t reg;
193 } meas_ = {.reg = 0}; // MEAS_REG
194
195 union {
196 struct {
197 uint8_t _reserved : 5;
198 bool int_prs : 1;
199 bool int_tmp : 1;
202 uint8_t reg;
203 } int_status_ = {.reg = 0}; // INT_STS
204
205 union {
206 struct {
207 bool spi_3wire : 1;
208 bool fifo_en : 1;
209 bool p_shift : 1;
210 bool t_shift : 1;
211 bool int_prs : 1;
212 bool int_tmp : 1;
213 bool int_fifo : 1;
214 bool int_hl : 1;
216 uint8_t reg;
217 } cfg_ = {.reg = 0}; // CFG_REG
218
219 union {
220 struct {
221 bool fifo_empty : 1;
222 bool fifo_full : 1;
223 uint8_t _reserved : 6;
225 uint8_t reg;
226 } fifo_sts_ = {.reg = 0}; // FIFO_STS
227
228 union {
229 struct {
230 // Set to true to flush FIFO
231 bool fifo_flush : 1;
232 // Reserved bits
233 uint8_t _reserved : 3;
234 // Soft reset. Set to 1001 (0x9) to perform reset.
235 uint8_t soft_rst : 4;
237 uint8_t reg = 0;
238 } reset_ = {.reg = 0}; // RESET
239
240 union {
241 struct {
242 uint8_t prod_id : 4;
243 uint8_t rev_id : 4;
245 uint8_t reg = 0;
246 } prod_id_ = {.reg = 0}; // ID
247
248}; // class SPA06Component
249} // namespace esphome::spa06_base
This class simplifies creating components that periodically check a state.
Definition component.h:542
void set_pressure_sample_rate_config(SampleRate rate)
Definition spa06_base.h:113
void set_temperature_sample_rate_config(SampleRate rate)
Definition spa06_base.h:114
union esphome::spa06_base::SPA06Component::@170 prod_id_
virtual bool spa_write_bytes(uint8_t reg, uint8_t *data, size_t len)=0
virtual bool spa_read_bytes(uint8_t reg, uint8_t *data, size_t len)=0
union esphome::spa06_base::SPA06Component::@168 fifo_sts_
union esphome::spa06_base::SPA06Component::@164 pt_meas_cfg_
union esphome::spa06_base::SPA06Component::@165 meas_
struct esphome::spa06_base::SPA06Component::@164::@171 bit
bool write_measurement_settings_(MeasCrtl crtl)
float convert_temperature_(const float &t_raw_sc)
SUB_SENSOR(temperature) SUB_SENSOR(pressure) void set_temperature_oversampling_config(Oversampling temperature_oversampling)
Definition spa06_base.h:103
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)
union esphome::spa06_base::SPA06Component::@166 int_status_
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_
void set_pressure_oversampling_config(Oversampling pressure_oversampling)
Definition spa06_base.h:109
void set_conversion_time(uint16_t conversion_time)
Definition spa06_base.h:102
constexpr size_t SPA06_COEF_LEN
Definition spa06_base.h:15
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
constexpr size_t SPA06_MEAS_LEN
Definition spa06_base.h:14
constexpr uint32_t OVERSAMPLING_K_LUT[8]
Definition spa06_base.h:85
PROGMEM_STRING_TABLE(MeasRateStrings, "1Hz", "2Hz", "4Hz", "8Hz", "16Hz", "32Hz", "64Hz", "128Hz", "1.5625Hz", "3.125Hz", "6.25Hz", "12.5Hz", "25Hz", "50Hz", "100Hz", "200Hz")
const void size_t len
Definition hal.h:68
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12
uint8_t pressure
Definition tt21100.cpp:7