ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ina2xx_base.h
Go to the documentation of this file.
1#pragma once
8enum RegisterMap : uint8_t {
9 REG_CONFIG = 0x00,
13 REG_VSHUNT = 0x04,
14 REG_VBUS = 0x05,
17 REG_POWER = 0x08,
18 REG_ENERGY = 0x09,
19 REG_CHARGE = 0x0A,
21 REG_SOVL = 0x0C,
22 REG_SUVL = 0x0D,
23 REG_BOVL = 0x0E,
24 REG_BUVL = 0x0F,
28 REG_DEVICE_ID = 0x3F
29};
30
31enum AdcRange : uint16_t {
34};
35
46
57
59 uint16_t raw_u16;
60 struct {
61 uint16_t reserved_0_3 : 4; // Reserved
62 AdcRange ADCRANGE : 1; // Shunt measurement range 0: ±163.84 mV, 1: ±40.96 mV
63 bool TEMPCOMP : 1; // Temperature compensation enable
64 uint16_t CONVDLY : 8; // Sets the Delay for initial ADC conversion in steps of 2 ms.
65 bool RSTACC : 1; // Reset counters
66 bool RST : 1; // Full device reset
67 } __attribute__((packed));
68};
69
71 uint16_t raw_u16;
72 struct {
74 AdcTime VTCT : 3; // Voltage conversion time
75 AdcTime VSHCT : 3; // Shunt voltage conversion time
76 AdcTime VBUSCT : 3; // Bus voltage conversion time
77 uint16_t MODE : 4;
78 } __attribute__((packed));
79};
80
82 uint16_t raw_u16;
83 struct {
84 uint16_t TEMPCO : 14;
85 uint16_t reserved : 2;
86 } __attribute__((packed));
87};
88
90 uint16_t raw_u16;
91 struct {
92 bool MEMSTAT : 1;
93 bool CNVRF : 1;
94 bool POL : 1;
95 bool BUSUL : 1;
96 bool BUSOL : 1;
97 bool SHNTUL : 1;
98 bool SHNTOL : 1;
99 bool TMPOL : 1;
100 bool RESERVED1 : 1;
101 bool MATHOF : 1;
102 bool CHARGEOF : 1;
103 bool ENERGYOF : 1;
104 bool APOL : 1;
105 bool SLOWALERT : 1;
106 bool CNVR : 1;
107 bool ALATCH : 1;
108 } __attribute__((packed));
109};
110
112
113class INA2XX : public PollingComponent {
114 public:
115 void setup() override;
116 void update() override;
117 void loop() override;
118 void dump_config() override;
119
120 void set_shunt_resistance_ohm(float shunt_resistance_ohm) { this->shunt_resistance_ohm_ = shunt_resistance_ohm; }
121 void set_max_current_a(float max_current_a) { this->max_current_a_ = max_current_a; }
126 void set_adc_avg_samples(AdcAvgSamples samples) { this->adc_avg_samples_ = samples; }
127 void set_shunt_tempco(uint16_t coeff) { this->shunt_tempco_ppm_c_ = coeff; }
129
130 SUB_SENSOR(shunt_voltage)
131 SUB_SENSOR(bus_voltage)
132 SUB_SENSOR(die_temperature)
133 SUB_SENSOR(current)
134 SUB_SENSOR(power)
135 SUB_SENSOR(energy_j)
136 SUB_SENSOR(energy_wh)
137 SUB_SENSOR(charge_c)
138 SUB_SENSOR(charge_ah)
139
140 void set_model(INAModel model) { this->ina_model_ = model; }
141
143
144 protected:
145 bool reset_config_();
146 bool check_device_model_();
147 bool configure_adc_();
148
149 bool configure_shunt_();
152
153 bool read_shunt_voltage_mv_(float &volt_out);
154 bool read_bus_voltage_(float &volt_out);
155 bool read_die_temp_c_(float &temp);
156 bool read_current_a_(float &amps_out);
157 bool read_power_w_(float &power_out);
158 bool read_energy_(double &joules_out, double &watt_hours_out);
159 bool read_charge_(double &coulombs_out, double &amp_hours_out);
160
162
163 //
164 // User configuration
165 //
174 bool reset_on_boot_{true};
175
176 //
177 // Calculated coefficients
178 //
179 uint16_t shunt_cal_{0};
180 float current_lsb_{0};
181
184
185 //
186 // FSM states
187 //
200
202
203 //
204 // Device model
205 //
207 uint16_t dev_id_{0};
208 bool device_mismatch_{false};
209
210 //
211 // Device specific parameters
212 //
213 struct {
214 float vbus_lsb;
223
224 //
225 // Register read/write
226 //
227 bool read_unsigned_(uint8_t reg, uint8_t reg_size, uint64_t &data_out);
228 bool read_unsigned_16_(uint8_t reg, uint16_t &out);
229 bool write_unsigned_16_(uint8_t reg, uint16_t val);
230
231 int64_t two_complement_(uint64_t value, uint8_t bits);
232
233 //
234 // Interface-specific implementation
235 //
236 virtual bool read_ina_register(uint8_t a_register, uint8_t *data, size_t len) = 0;
237 virtual bool write_ina_register(uint8_t a_register, const uint8_t *data, size_t len) = 0;
238};
239} // namespace esphome::ina2xx_base
This class simplifies creating components that periodically check a state.
Definition component.h:532
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_adc_time_die_temperature(AdcTime time)
bool read_unsigned_(uint8_t reg, uint8_t reg_size, uint64_t &data_out)
void set_max_current_a(float max_current_a)
bool read_bus_voltage_(float &volt_out)
void set_reset_on_boot(bool reset)
bool read_unsigned_16_(uint8_t reg, uint16_t &out)
struct esphome::ina2xx_base::INA2XX::@97 cfg_
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)
SUB_SENSOR(shunt_voltage) SUB_SENSOR(bus_voltage) SUB_SENSOR(die_temperature) SUB_SENSOR(current) SUB_SENSOR(power) SUB_SENSOR(energy_j) SUB_SENSOR(energy_wh) SUB_SENSOR(charge_c) SUB_SENSOR(charge_ah) void set_model(INAModel model)
virtual bool read_ina_register(uint8_t a_register, uint8_t *data, size_t len)=0
void set_adc_avg_samples(AdcAvgSamples samples)
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)
bool read_energy_(double &joules_out, double &watt_hours_out)
void set_shunt_tempco(uint16_t coeff)
bool read_current_a_(float &amps_out)
void set_adc_time_shunt_voltage(AdcTime time)
virtual bool write_ina_register(uint8_t a_register, const uint8_t *data, size_t len)=0
struct @66::@67 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:45
std::vector< uint8_t > & out
Definition gif.cpp:49
uint16_t reset
Definition ina226.h:5
mopeka_std_values val[3]
Range range
Definition msa3xx.h:0
uint32_t value
Definition hal.cpp:41
const void size_t len
Definition hal.h:68
static void uint32_t