9static const uint8_t INA3221_REGISTER_CONFIG = 0x00;
10static const uint8_t INA3221_REGISTER_CHANNEL1_SHUNT_VOLTAGE = 0x01;
11static const uint8_t INA3221_REGISTER_CHANNEL1_BUS_VOLTAGE = 0x02;
12static const uint8_t INA3221_REGISTER_CHANNEL2_SHUNT_VOLTAGE = 0x03;
13static const uint8_t INA3221_REGISTER_CHANNEL2_BUS_VOLTAGE = 0x04;
14static const uint8_t INA3221_REGISTER_CHANNEL3_SHUNT_VOLTAGE = 0x05;
15static const uint8_t INA3221_REGISTER_CHANNEL3_BUS_VOLTAGE = 0x06;
16#ifdef USE_INA3221_ALERT_LIMITS
17static constexpr uint8_t INA3221_REGISTER_CHANNEL1_CRITICAL_ALERT = 0x07;
18static constexpr uint8_t INA3221_REGISTER_CHANNEL1_WARNING_ALERT = 0x0A;
20#ifdef USE_INA3221_SUMMATION
21static constexpr uint8_t INA3221_REGISTER_SHUNT_VOLTAGE_SUM = 0x0D;
22static constexpr uint8_t INA3221_REGISTER_MASK_ENABLE = 0x0F;
28static constexpr uint16_t INA3221_CONFIG_RESET = 0x8000;
29static constexpr uint16_t INA3221_CONFIG_CHANNEL1_ENABLE = 0x4000;
30static constexpr uint16_t INA3221_CONFIG_CHANNEL_MASK = 0x7000;
31static constexpr uint16_t INA3221_CONFIG_MODE_MASK = 0x0007;
33static constexpr uint32_t READ_TIMEOUT_ID = 0;
42 if (!this->
write_byte_16(INA3221_REGISTER_CONFIG, INA3221_CONFIG_RESET)) {
53#ifdef USE_INA3221_ALERT_LIMITS
54 for (uint8_t i = 0; i < 3; i++) {
55 const INA3221Channel &channel = this->
channels_[i];
56 if ((channel.critical_limit_ != 0 &&
57 !this->write_byte_16(INA3221_REGISTER_CHANNEL1_CRITICAL_ALERT + i, channel.critical_limit_)) ||
58 (channel.warning_limit_ != 0 &&
59 !this->write_byte_16(INA3221_REGISTER_CHANNEL1_WARNING_ALERT + i, channel.warning_limit_))) {
66#ifdef USE_INA3221_SUMMATION
68 if (this->sum_shunt_voltage_sensor_ !=
nullptr &&
69 !this->
write_byte_16(INA3221_REGISTER_MASK_ENABLE, this->
config_ & INA3221_CONFIG_CHANNEL_MASK)) {
76void INA3221Component::dump_config() {
77 ESP_LOGCONFIG(TAG,
"INA3221:");
79 LOG_UPDATE_INTERVAL(
this);
80 ESP_LOGCONFIG(TAG,
" Config register: 0x%04X", this->
config_);
85 LOG_SENSOR(
" ",
"Bus Voltage #1", this->
channels_[0].bus_voltage_sensor_);
86 LOG_SENSOR(
" ",
"Shunt Voltage #1", this->
channels_[0].shunt_voltage_sensor_);
87 LOG_SENSOR(
" ",
"Current #1", this->
channels_[0].current_sensor_);
88 LOG_SENSOR(
" ",
"Power #1", this->
channels_[0].power_sensor_);
89 LOG_SENSOR(
" ",
"Bus Voltage #2", this->
channels_[1].bus_voltage_sensor_);
90 LOG_SENSOR(
" ",
"Shunt Voltage #2", this->
channels_[1].shunt_voltage_sensor_);
91 LOG_SENSOR(
" ",
"Current #2", this->
channels_[1].current_sensor_);
92 LOG_SENSOR(
" ",
"Power #2", this->
channels_[1].power_sensor_);
93 LOG_SENSOR(
" ",
"Bus Voltage #3", this->
channels_[2].bus_voltage_sensor_);
94 LOG_SENSOR(
" ",
"Shunt Voltage #3", this->
channels_[2].shunt_voltage_sensor_);
95 LOG_SENSOR(
" ",
"Current #3", this->
channels_[2].current_sensor_);
96 LOG_SENSOR(
" ",
"Power #3", this->
channels_[2].power_sensor_);
97#ifdef USE_INA3221_SUMMATION
98 LOG_SENSOR(
" ",
"Sum Shunt Voltage", this->sum_shunt_voltage_sensor_);
99 LOG_SENSOR(
" ",
"Sum Current", this->sum_current_sensor_);
100 LOG_SENSOR(
" ",
"Sum Power", this->sum_power_sensor_);
108void INA3221Component::update() {
122#ifdef USE_INA3221_SUMMATION
124 const bool sum_all = this->sum_current_sensor_ !=
nullptr || this->sum_power_sensor_ !=
nullptr;
125 float total_current_a = 0.0f;
126 float total_power_w = 0.0f;
128 constexpr bool sum_all =
false;
131 for (
int i = 0; i < 3; i++) {
132 if ((this->
config_ & (INA3221_CONFIG_CHANNEL1_ENABLE >> i)) == 0) {
136 float bus_voltage_v = NAN, current_a = NAN;
143 bus_voltage_v = int16_t(
raw) / 1000.0f;
152 const float shunt_voltage_v = int16_t(
raw) * 40.0f / 8.0f / 1000000.0f;
162#ifdef USE_INA3221_SUMMATION
164 total_current_a += current_a;
165 total_power_w += bus_voltage_v * current_a;
170#ifdef USE_INA3221_SUMMATION
171 if (this->sum_shunt_voltage_sensor_ !=
nullptr) {
178 this->sum_shunt_voltage_sensor_->publish_state(int16_t(
raw) * 40.0f / 2.0f / 1000000.0f);
180 if (this->sum_current_sensor_ !=
nullptr) {
181 this->sum_current_sensor_->publish_state(total_current_a);
183 if (this->sum_power_sensor_ !=
nullptr) {
184 this->sum_power_sensor_->publish_state(total_power_w);
189void INA3221Component::on_powerdown() {
191 this->
write_byte_16(INA3221_REGISTER_CONFIG,
static_cast<uint16_t
>(this->
config_ & ~INA3221_CONFIG_MODE_MASK));
194void INA3221Component::set_shunt_resistance(
int channel,
float resistance_ohm) {
202 return this->bus_voltage_sensor_ !=
nullptr || this->power_sensor_ !=
nullptr;