ESPHome  2024.9.2
ina226.cpp
Go to the documentation of this file.
1 #include "ina226.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/hal.h"
4 #include <cinttypes>
5 
6 namespace esphome {
7 namespace ina226 {
8 
9 static const char *const TAG = "ina226";
10 
11 // | A0 | A1 | Address |
12 // | GND | GND | 0x40 |
13 // | GND | V_S+ | 0x41 |
14 // | GND | SDA | 0x42 |
15 // | GND | SCL | 0x43 |
16 // | V_S+ | GND | 0x44 |
17 // | V_S+ | V_S+ | 0x45 |
18 // | V_S+ | SDA | 0x46 |
19 // | V_S+ | SCL | 0x47 |
20 // | SDA | GND | 0x48 |
21 // | SDA | V_S+ | 0x49 |
22 // | SDA | SDA | 0x4A |
23 // | SDA | SCL | 0x4B |
24 // | SCL | GND | 0x4C |
25 // | SCL | V_S+ | 0x4D |
26 // | SCL | SDA | 0x4E |
27 // | SCL | SCL | 0x4F |
28 
29 static const uint8_t INA226_REGISTER_CONFIG = 0x00;
30 static const uint8_t INA226_REGISTER_SHUNT_VOLTAGE = 0x01;
31 static const uint8_t INA226_REGISTER_BUS_VOLTAGE = 0x02;
32 static const uint8_t INA226_REGISTER_POWER = 0x03;
33 static const uint8_t INA226_REGISTER_CURRENT = 0x04;
34 static const uint8_t INA226_REGISTER_CALIBRATION = 0x05;
35 
36 static const uint16_t INA226_ADC_TIMES[] = {140, 204, 332, 588, 1100, 2116, 4156, 8244};
37 static const uint16_t INA226_ADC_AVG_SAMPLES[] = {1, 4, 16, 64, 128, 256, 512, 1024};
38 
40  ESP_LOGCONFIG(TAG, "Setting up INA226...");
41 
42  ConfigurationRegister config;
43 
44  config.reset = 1;
45  if (!this->write_byte_16(INA226_REGISTER_CONFIG, config.raw)) {
46  this->mark_failed();
47  return;
48  }
49  delay(1);
50 
51  config.raw = 0;
52  config.reserved = 0b100; // as per datasheet
53 
54  // Averaging Mode AVG Bit Settings[11:9] (000 -> 1 sample, 001 -> 4 sample, 111 -> 1024 samples)
55  config.avg_samples = this->adc_avg_samples_;
56 
57  // Bus Voltage Conversion Time VBUSCT Bit Settings [8:6] (100 -> 1.1ms, 111 -> 8.244 ms)
59 
60  // Shunt Voltage Conversion Time VSHCT Bit Settings [5:3] (100 -> 1.1ms, 111 -> 8.244 ms)
62 
63  // Mode Settings [2:0] Combinations (111 -> Shunt and Bus, Continuous)
64  config.mode = 0b111;
65 
66  if (!this->write_byte_16(INA226_REGISTER_CONFIG, config.raw)) {
67  this->mark_failed();
68  return;
69  }
70 
71  // lsb is multiplied by 1000000 to store it as an integer value
72  uint32_t lsb = static_cast<uint32_t>(ceilf(this->max_current_a_ * 1000000.0f / 32768));
73 
74  this->calibration_lsb_ = lsb;
75 
76  auto calibration = uint32_t(0.00512 / (lsb * this->shunt_resistance_ohm_ / 1000000.0f));
77 
78  ESP_LOGV(TAG, " Using LSB=%" PRIu32 " calibration=%" PRIu32, lsb, calibration);
79 
80  if (!this->write_byte_16(INA226_REGISTER_CALIBRATION, calibration)) {
81  this->mark_failed();
82  return;
83  }
84 }
85 
87  ESP_LOGCONFIG(TAG, "INA226:");
88  LOG_I2C_DEVICE(this);
89 
90  if (this->is_failed()) {
91  ESP_LOGE(TAG, "Communication with INA226 failed!");
92  return;
93  }
94  LOG_UPDATE_INTERVAL(this);
95 
96  ESP_LOGCONFIG(TAG, " ADC Conversion Time Bus Voltage: %d", INA226_ADC_TIMES[this->adc_time_voltage_ & 0b111]);
97  ESP_LOGCONFIG(TAG, " ADC Conversion Time Shunt Voltage: %d", INA226_ADC_TIMES[this->adc_time_current_ & 0b111]);
98  ESP_LOGCONFIG(TAG, " ADC Averaging Samples: %d", INA226_ADC_AVG_SAMPLES[this->adc_avg_samples_ & 0b111]);
99 
100  LOG_SENSOR(" ", "Bus Voltage", this->bus_voltage_sensor_);
101  LOG_SENSOR(" ", "Shunt Voltage", this->shunt_voltage_sensor_);
102  LOG_SENSOR(" ", "Current", this->current_sensor_);
103  LOG_SENSOR(" ", "Power", this->power_sensor_);
104 }
105 
107 
109  if (this->bus_voltage_sensor_ != nullptr) {
110  uint16_t raw_bus_voltage;
111  if (!this->read_byte_16(INA226_REGISTER_BUS_VOLTAGE, &raw_bus_voltage)) {
112  this->status_set_warning();
113  return;
114  }
115  // Convert for 2's compliment and signed value (though always positive)
116  float bus_voltage_v = this->twos_complement_(raw_bus_voltage, 16);
117  bus_voltage_v *= 0.00125f;
118  this->bus_voltage_sensor_->publish_state(bus_voltage_v);
119  }
120 
121  if (this->shunt_voltage_sensor_ != nullptr) {
122  uint16_t raw_shunt_voltage;
123  if (!this->read_byte_16(INA226_REGISTER_SHUNT_VOLTAGE, &raw_shunt_voltage)) {
124  this->status_set_warning();
125  return;
126  }
127  // Convert for 2's compliment and signed value
128  float shunt_voltage_v = this->twos_complement_(raw_shunt_voltage, 16);
129  shunt_voltage_v *= 0.0000025f;
130  this->shunt_voltage_sensor_->publish_state(shunt_voltage_v);
131  }
132 
133  if (this->current_sensor_ != nullptr) {
134  uint16_t raw_current;
135  if (!this->read_byte_16(INA226_REGISTER_CURRENT, &raw_current)) {
136  this->status_set_warning();
137  return;
138  }
139  // Convert for 2's compliment and signed value
140  float current_ma = this->twos_complement_(raw_current, 16);
141  current_ma *= (this->calibration_lsb_ / 1000.0f);
142  this->current_sensor_->publish_state(current_ma / 1000.0f);
143  }
144 
145  if (this->power_sensor_ != nullptr) {
146  uint16_t raw_power;
147  if (!this->read_byte_16(INA226_REGISTER_POWER, &raw_power)) {
148  this->status_set_warning();
149  return;
150  }
151  float power_mw = int16_t(raw_power) * (this->calibration_lsb_ * 25.0f / 1000.0f);
152  this->power_sensor_->publish_state(power_mw / 1000.0f);
153  }
154 
155  this->status_clear_warning();
156 }
157 
158 int32_t INA226Component::twos_complement_(int32_t val, uint8_t bits) {
159  if (val & ((uint32_t) 1 << (bits - 1))) {
160  val -= (uint32_t) 1 << bits;
161  }
162  return val;
163 }
164 
165 } // namespace ina226
166 } // namespace esphome
sensor::Sensor * power_sensor_
Definition: ina226.h:72
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition: i2c.h:246
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
AdcAvgSamples adc_avg_samples_
Definition: ina226.h:67
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
bool is_failed() const
Definition: component.cpp:143
mopeka_std_values val[4]
sensor::Sensor * bus_voltage_sensor_
Definition: ina226.h:69
sensor::Sensor * current_sensor_
Definition: ina226.h:71
float get_setup_priority() const override
Definition: ina226.cpp:106
void status_clear_warning()
Definition: component.cpp:166
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
int32_t twos_complement_(int32_t val, uint8_t bits)
Definition: ina226.cpp:158
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool write_byte_16(uint8_t a_register, uint16_t data)
Definition: i2c.h:266
void dump_config() override
Definition: ina226.cpp:86
sensor::Sensor * shunt_voltage_sensor_
Definition: ina226.h:70
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26