ESPHome 2025.5.0
Loading...
Searching...
No Matches
ina2xx_base.h
Go to the documentation of this file.
1#pragma once
6namespace esphome {
7namespace ina2xx_base {
9enum RegisterMap : uint8_t {
10 REG_CONFIG = 0x00,
14 REG_VSHUNT = 0x04,
15 REG_VBUS = 0x05,
18 REG_POWER = 0x08,
19 REG_ENERGY = 0x09,
20 REG_CHARGE = 0x0A,
22 REG_SOVL = 0x0C,
23 REG_SUVL = 0x0D,
24 REG_BOVL = 0x0E,
25 REG_BUVL = 0x0F,
29 REG_DEVICE_ID = 0x3F
30};
31
32enum AdcRange : uint16_t {
35};
36
47
58
60 uint16_t raw_u16;
61 struct {
62 uint16_t reserved_0_3 : 4; // Reserved
63 AdcRange ADCRANGE : 1; // Shunt measurement range 0: ±163.84 mV, 1: ±40.96 mV
64 bool TEMPCOMP : 1; // Temperature compensation enable
65 uint16_t CONVDLY : 8; // Sets the Delay for initial ADC conversion in steps of 2 ms.
66 bool RSTACC : 1; // Reset counters
67 bool RST : 1; // Full device reset
68 } __attribute__((packed));
69};
70
72 uint16_t raw_u16;
73 struct {
75 AdcTime VTCT : 3; // Voltage conversion time
76 AdcTime VSHCT : 3; // Shunt voltage conversion time
77 AdcTime VBUSCT : 3; // Bus voltage conversion time
78 uint16_t MODE : 4;
79 } __attribute__((packed));
80};
81
83 uint16_t raw_u16;
84 struct {
85 uint16_t TEMPCO : 14;
86 uint16_t reserved : 2;
87 } __attribute__((packed));
88};
89
91 uint16_t raw_u16;
92 struct {
93 bool MEMSTAT : 1;
94 bool CNVRF : 1;
95 bool POL : 1;
96 bool BUSUL : 1;
97 bool BUSOL : 1;
98 bool SHNTUL : 1;
99 bool SHNTOL : 1;
100 bool TMPOL : 1;
101 bool RESERVED1 : 1;
102 bool MATHOF : 1;
103 bool CHARGEOF : 1;
104 bool ENERGYOF : 1;
105 bool APOL : 1;
106 bool SLOWALERT : 1;
107 bool CNVR : 1;
108 bool ALATCH : 1;
109 } __attribute__((packed));
110};
111
113
114class INA2XX : public PollingComponent {
115 public:
116 void setup() override;
117 float get_setup_priority() const override;
118 void update() override;
119 void loop() override;
120 void dump_config() override;
121
122 void set_shunt_resistance_ohm(float shunt_resistance_ohm) { this->shunt_resistance_ohm_ = shunt_resistance_ohm; }
123 void set_max_current_a(float max_current_a) { this->max_current_a_ = max_current_a; }
128 void set_adc_avg_samples(AdcAvgSamples samples) { this->adc_avg_samples_ = samples; }
129 void set_shunt_tempco(uint16_t coeff) { this->shunt_tempco_ppm_c_ = coeff; }
130
134 void set_current_sensor(sensor::Sensor *sensor) { this->current_sensor_ = sensor; }
135 void set_power_sensor(sensor::Sensor *sensor) { this->power_sensor_ = sensor; }
136 void set_energy_sensor_j(sensor::Sensor *sensor) { this->energy_sensor_j_ = sensor; }
137 void set_energy_sensor_wh(sensor::Sensor *sensor) { this->energy_sensor_wh_ = sensor; }
138 void set_charge_sensor_c(sensor::Sensor *sensor) { this->charge_sensor_c_ = sensor; }
139 void set_charge_sensor_ah(sensor::Sensor *sensor) { this->charge_sensor_ah_ = sensor; }
140
141 void set_model(INAModel model) { this->ina_model_ = model; }
142
144
145 protected:
146 bool reset_config_();
147 bool check_device_model_();
148 bool configure_adc_();
149
150 bool configure_shunt_();
153
154 bool read_shunt_voltage_mv_(float &volt_out);
155 bool read_bus_voltage_(float &volt_out);
156 bool read_die_temp_c_(float &temp);
157 bool read_current_a_(float &amps_out);
158 bool read_power_w_(float &power_out);
159 bool read_energy_(double &joules_out, double &watt_hours_out);
160 bool read_charge_(double &coulombs_out, double &amp_hours_out);
161
163
164 //
165 // User configuration
166 //
175
176 //
177 // Calculated coefficients
178 //
179 uint16_t shunt_cal_{0};
180 float current_lsb_{0};
181
184
185 //
186 // Sensor objects
187 //
197
198 //
199 // FSM states
200 //
213
215
216 //
217 // Device model
218 //
220 uint16_t dev_id_{0};
221 bool device_mismatch_{false};
222
223 //
224 // Device specific parameters
225 //
226 struct {
227 float vbus_lsb;
236
237 //
238 // Register read/write
239 //
240 bool read_unsigned_(uint8_t reg, uint8_t reg_size, uint64_t &data_out);
241 bool read_unsigned_16_(uint8_t reg, uint16_t &out);
242 bool write_unsigned_16_(uint8_t reg, uint16_t val);
243
244 int64_t two_complement_(uint64_t value, uint8_t bits);
245
246 //
247 // Interface-specific implementation
248 //
249 virtual bool read_ina_register(uint8_t a_register, uint8_t *data, size_t len) = 0;
250 virtual bool write_ina_register(uint8_t a_register, const uint8_t *data, size_t len) = 0;
251};
252} // namespace ina2xx_base
253} // namespace esphome
This class simplifies creating components that periodically check a state.
Definition component.h:301
enum esphome::ina2xx_base::INA2XX::State NOT_INITIALIZED
bool read_die_temp_c_(float &temp)
bool write_unsigned_16_(uint8_t reg, uint16_t val)
void set_bus_voltage_sensor(sensor::Sensor *sensor)
sensor::Sensor * bus_voltage_sensor_
void set_adc_time_die_temperature(AdcTime time)
bool read_unsigned_(uint8_t reg, uint8_t reg_size, uint64_t &data_out)
sensor::Sensor * energy_sensor_j_
void set_energy_sensor_j(sensor::Sensor *sensor)
void set_charge_sensor_ah(sensor::Sensor *sensor)
void set_max_current_a(float max_current_a)
bool read_bus_voltage_(float &volt_out)
void set_power_sensor(sensor::Sensor *sensor)
sensor::Sensor * power_sensor_
bool read_unsigned_16_(uint8_t reg, uint16_t &out)
sensor::Sensor * charge_sensor_c_
sensor::Sensor * die_temperature_sensor_
sensor::Sensor * energy_sensor_wh_
void set_current_sensor(sensor::Sensor *sensor)
bool read_shunt_voltage_mv_(float &volt_out)
void set_adc_time_bus_voltage(AdcTime time)
void set_shunt_resistance_ohm(float shunt_resistance_ohm)
void set_charge_sensor_c(sensor::Sensor *sensor)
virtual bool read_ina_register(uint8_t a_register, uint8_t *data, size_t len)=0
void set_adc_avg_samples(AdcAvgSamples samples)
sensor::Sensor * charge_sensor_ah_
bool read_power_w_(float &power_out)
int64_t two_complement_(uint64_t value, uint8_t bits)
void set_adc_range(uint8_t range)
bool read_charge_(double &coulombs_out, double &amp_hours_out)
sensor::Sensor * current_sensor_
bool read_energy_(double &joules_out, double &watt_hours_out)
float get_setup_priority() const override
void set_shunt_voltage_sensor(sensor::Sensor *sensor)
void set_energy_sensor_wh(sensor::Sensor *sensor)
void set_shunt_tempco(uint16_t coeff)
void set_die_temperature_sensor(sensor::Sensor *sensor)
bool read_current_a_(float &amps_out)
void set_model(INAModel model)
struct esphome::ina2xx_base::INA2XX::@87 cfg_
void set_adc_time_shunt_voltage(AdcTime time)
sensor::Sensor * shunt_voltage_sensor_
virtual bool write_ina_register(uint8_t a_register, const uint8_t *data, size_t len)=0
Base-class for all sensors.
Definition sensor.h:57
struct @67::@68 __attribute__
mopeka_std_values val[4]
Range range
Definition msa3xx.h:0
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:301