12#define OKFAILED(b) ((b) ? "OK" : "FAILED")
14static const uint16_t ADC_TIMES[8] = {50, 84, 150, 280, 540, 1052, 2074, 4120};
15static const uint16_t ADC_SAMPLES[8] = {1, 4, 16, 64, 128, 256, 512, 1024};
17static const char *get_device_name(
INAModel model) {
34static bool check_model_and_device_match(
INAModel model, uint16_t dev_id) {
37 return dev_id == 0x228;
39 return dev_id == 0x229;
41 return dev_id == 0x238;
43 return dev_id == 0x239;
45 return dev_id == 0x237;
53 this->
mark_failed(LOG_STR(
"Reset failed, check connection"));
59 this->
mark_failed(LOG_STR(
"Device not supported or model selected improperly in yaml file"));
80 ESP_LOGD(TAG,
"Updating");
82 ESP_LOGD(TAG,
"Initiating new data collection");
90 switch (this->state_) {
98 if (this->shunt_voltage_sensor_ !=
nullptr) {
99 float shunt_voltage{0};
101 this->shunt_voltage_sensor_->publish_state(shunt_voltage);
107 if (this->bus_voltage_sensor_ !=
nullptr) {
108 float bus_voltage{0};
110 this->bus_voltage_sensor_->publish_state(bus_voltage);
116 if (this->die_temperature_sensor_ !=
nullptr) {
117 float die_temperature{0};
119 this->die_temperature_sensor_->publish_state(die_temperature);
125 if (this->current_sensor_ !=
nullptr) {
128 this->current_sensor_->publish_state(current);
134 if (this->power_sensor_ !=
nullptr) {
137 this->power_sensor_->publish_state(power);
144 if (this->energy_j_sensor_ !=
nullptr || this->energy_wh_sensor_ !=
nullptr ||
145 this->charge_c_sensor_ !=
nullptr || this->charge_ah_sensor_ !=
nullptr) {
148 if (this->energy_j_sensor_ !=
nullptr || this->energy_wh_sensor_ !=
nullptr) {
149 double energy_j{0}, energy_wh{0};
151 PUBLISH_STATE(this->energy_j_sensor_, energy_j);
152 PUBLISH_STATE(this->energy_wh_sensor_, energy_wh);
160 if (this->charge_c_sensor_ !=
nullptr || this->charge_ah_sensor_ !=
nullptr) {
161 double charge_c{0}, charge_ah{0};
163 PUBLISH_STATE(this->charge_c_sensor_, charge_c);
164 PUBLISH_STATE(this->charge_ah_sensor_, charge_ah);
176 ESP_LOGW(TAG,
"Unknown state of the component, might be due to memory corruption");
183 ESP_LOGCONFIG(TAG,
"INA2xx:");
184 ESP_LOGCONFIG(TAG,
" Device model = %s", get_device_name(this->
ina_model_));
187 ESP_LOGE(TAG,
" Device model mismatch. Found device with ID = %x. Please check your configuration.",
190 LOG_UPDATE_INTERVAL(
this);
192 " Shunt resistance = %f Ohm\n"
193 " Max current = %f A\n"
194 " Shunt temp coeff = %d ppm/°C\n"
195 " ADCRANGE = %d (%s)\n"
196 " CURRENT_LSB = %f\n"
200 this->adc_range_ ? LOG_STR_LITERAL(
"±40.96 mV") : LOG_STR_LITERAL(
"±163.84 mV"), this->
current_lsb_,
203 ESP_LOGCONFIG(TAG,
" ADC Samples = %d; ADC times: Bus = %d μs, Shunt = %d μs, Temp = %d μs",
209 ESP_LOGCONFIG(TAG,
" Device is %s", get_device_name(this->
ina_model_));
211 LOG_SENSOR(
" ",
"Shunt Voltage", this->shunt_voltage_sensor_);
212 LOG_SENSOR(
" ",
"Bus Voltage", this->bus_voltage_sensor_);
213 LOG_SENSOR(
" ",
"Die Temperature", this->die_temperature_sensor_);
214 LOG_SENSOR(
" ",
"Current", this->current_sensor_);
215 LOG_SENSOR(
" ",
"Power", this->power_sensor_);
218 LOG_SENSOR(
" ",
"Energy J", this->energy_j_sensor_);
219 LOG_SENSOR(
" ",
"Energy Wh", this->energy_wh_sensor_);
220 LOG_SENSOR(
" ",
"Charge C", this->charge_c_sensor_);
221 LOG_SENSOR(
" ",
"Charge Ah", this->charge_ah_sensor_);
229 ESP_LOGV(TAG,
"reset_energy_counters");
243 ESP_LOGV(TAG,
"Reset");
246 ESP_LOGI(TAG,
"Skipping on-boot device reset");
255 constexpr uint16_t manufacturer_ti = 0x5449;
257 uint16_t manufacturer_id{0}, rev_id{0};
261 ESP_LOGV(TAG,
"Can't read device ID");
265 ESP_LOGI(TAG,
"Manufacturer: 0x%04X, Device ID: 0x%04X, Revision: %d", manufacturer_id, this->
dev_id_, rev_id);
267 if (manufacturer_id != manufacturer_ti) {
268 ESP_LOGE(TAG,
"Manufacturer ID doesn't match original 0x5449");
274 ESP_LOGI(TAG,
"Supported device found: INA%x, 85-V, 20-Bit, Ultra-Precise Power/Energy/Charge Monitor",
277 ESP_LOGI(TAG,
"Supported device found: INA%x, 85-V, 16-Bit, High-Precision Power Monitor", this->
dev_id_);
279 ESP_LOGI(TAG,
"We assume device is: INA237 85-V, 16-Bit, Precision Power Monitor");
282 ESP_LOGE(TAG,
"Unknown device ID %x.", this->
dev_id_);
289 ESP_LOGE(TAG,
"Selected model %s doesn't match found device INA%x", get_device_name(this->
ina_model_),
297 this->
cfg_.vbus_lsb = 0.0001953125f;
298 this->
cfg_.v_shunt_lsb_range0 = 0.0003125f;
299 this->
cfg_.v_shunt_lsb_range1 = 0.000078125f;
300 this->
cfg_.shunt_cal_scale = 13107.2f * 1000000.0f;
301 this->
cfg_.current_lsb_scale_factor = -19;
302 this->
cfg_.die_temp_lsb = 0.0078125f;
303 this->
cfg_.power_coeff = 3.2f;
304 this->
cfg_.energy_coeff = 16.0f * 3.2f;
306 this->
cfg_.vbus_lsb = 0.0031250000f;
307 this->
cfg_.v_shunt_lsb_range0 = 0.0050000f;
308 this->
cfg_.v_shunt_lsb_range1 = 0.001250000f;
309 this->
cfg_.shunt_cal_scale = 819.2f * 1000000.0f;
310 this->
cfg_.current_lsb_scale_factor = -15;
311 this->
cfg_.die_temp_lsb = 0.1250000f;
312 this->
cfg_.power_coeff = 0.2f;
313 this->
cfg_.energy_coeff = 0.0f;
320 ESP_LOGV(TAG,
"Setting ADCRANGE = %d", (uint8_t) this->
adc_range_);
343 this->
shunt_cal_ = (uint16_t) (this->
cfg_.shunt_cal_scale * this->current_lsb_ * this->shunt_resistance_ohm_);
349 ESP_LOGW(TAG,
"Shunt value too high");
352 ESP_LOGV(TAG,
"Rshunt=%f Ohm, max current=%.3f A, current LSB=%f, shunt cal=%u", this->
shunt_resistance_ohm_,
375 float volt_reading{0};
387 volt_out = (this->
adc_range_ ? this->
cfg_.v_shunt_lsb_range1 : this->
cfg_.v_shunt_lsb_range0) * volt_reading;
390 ESP_LOGV(TAG,
"read_shunt_voltage_mv_ ret=%s, shunt_cal=%d, reading_lsb=%f", OKFAILED(
ret), this->
shunt_cal_,
402 float volt_reading{0};
413 volt_out = this->
cfg_.vbus_lsb * (float) volt_reading;
416 ESP_LOGV(TAG,
"read_bus_voltage_ ret=%s, reading_lsb=%f", OKFAILED(
ret), volt_reading);
426 float temp_reading{0};
438 temp_out = this->
cfg_.die_temp_lsb * (float) temp_reading;
441 ESP_LOGV(TAG,
"read_die_temp_c_ ret=%s, reading_lsb=%f", OKFAILED(
ret), temp_reading);
450 float amps_reading{0};
462 ESP_LOGV(TAG,
"read_current_a_ ret=%s. current_lsb=%f. reading_lsb=%f", OKFAILED(
ret), this->
current_lsb_,
475 uint64_t power_reading{0};
478 ESP_LOGV(TAG,
"read_power_w_ ret=%s, reading_lsb=%" PRIu32, OKFAILED(
ret), (
uint32_t) power_reading);
480 power_out = this->
cfg_.power_coeff * this->
current_lsb_ * (float) power_reading;
494 uint64_t joules_reading = 0;
498 ESP_LOGV(TAG,
"read_energy_j_ ret=%s, reading_lsb=0x%" PRIX64
", current_lsb=%f, overflow_cnt=%" PRIu32,
501 joules_out = this->
cfg_.energy_coeff * this->
current_lsb_ * (double) joules_reading + (
double) previous_energy;
502 watt_hours_out = joules_out / 3600.0;
518 double coulombs_reading = 0;
523 ESP_LOGV(TAG,
"read_charge_c_ ret=%d, curr_charge=%f + 39-bit overflow_cnt=%" PRIu32,
ret, coulombs_reading,
526 coulombs_out = this->
current_lsb_ * (double) coulombs_reading + (
double) previous_charge;
527 amp_hours_out = coulombs_out / 3600.0;
539 ESP_LOGV(TAG,
"read_diagnostics_and_act_ ret=%s, 0x%04X", OKFAILED(
ret), diag.raw_u16);
556 ESP_LOGV(TAG,
"write_unsigned_16_ FAILED reg=0x%02X, val=0x%04X", reg,
val);
563 if (reg_size >
sizeof(rx_buf)) {
570 data_out = rx_buf[0];
571 for (uint8_t i = 1; i < reg_size; i++) {
572 data_out = (data_out << 8) | rx_buf[i];
574 ESP_LOGV(TAG,
"read_unsigned_ reg=0x%02X, ret=%s, len=%d, val=0x%" PRIX64, reg, OKFAILED(
ret), reg_size, data_out);
583 ESP_LOGV(TAG,
"read_unsigned_16_ 0x%02X, ret= %s, val=0x%04X", reg, OKFAILED(
ret),
out);
588 return (int64_t) (
value << (64 - bits)) >> (64 - bits);
void mark_failed()
Mark this component as failed.
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
bool reset_energy_counters()
bool configure_shunt_tempco_()
bool check_device_model_()
bool read_die_temp_c_(float &temp)
AdcAvgSamples adc_avg_samples_
bool write_unsigned_16_(uint8_t reg, uint16_t val)
bool read_unsigned_(uint8_t reg, uint8_t reg_size, uint64_t &data_out)
bool configure_adc_range_()
bool read_bus_voltage_(float &volt_out)
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)
uint16_t shunt_tempco_ppm_c_
float shunt_resistance_ohm_
virtual bool read_ina_register(uint8_t a_register, uint8_t *data, size_t len)=0
bool read_power_w_(float &power_out)
int64_t two_complement_(uint64_t value, uint8_t bits)
bool read_charge_(double &coulombs_out, double &_hours_out)
void dump_config() override
AdcTime adc_time_shunt_voltage_
bool read_energy_(double &joules_out, double &watt_hours_out)
AdcTime adc_time_die_temperature_
bool read_diagnostics_and_act_()
AdcTime adc_time_bus_voltage_
uint32_t energy_overflows_count_
bool read_current_a_(float &s_out)
uint32_t charge_overflows_count_
virtual bool write_ina_register(uint8_t a_register, const uint8_t *data, size_t len)=0
std::vector< uint8_t > & out
ESPHOME_LOG_TAG(TAG, "ina2xx")
constexpr int32_t sign_extend(uint32_t value)
Sign-extend the low BITS bits of value, e.g. sign_extend<24>(0xFFFFFE) == -2. Higher bits are ignored...
void HOT delay(uint32_t ms)