ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
ina3221.cpp
Go to the documentation of this file.
1#include "ina3221.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
4
5namespace esphome::ina3221 {
6
7ESPHOME_LOG_TAG(TAG, "ina3221");
8
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;
19#endif
20#ifdef USE_INA3221_SUMMATION
21static constexpr uint8_t INA3221_REGISTER_SHUNT_VOLTAGE_SUM = 0x0D;
22static constexpr uint8_t INA3221_REGISTER_MASK_ENABLE = 0x0F;
23#endif
24
25// Configuration register: bit 15 resets the chip, bits 14..12 enable channels 1..3 (the same bit
26// positions select channels for summation in the mask/enable register) and bits 2..0 set the mode.
27// Code generation fills in the averaging and conversion time fields.
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;
32
33static constexpr uint32_t READ_TIMEOUT_ID = 0;
34
35// Addresses:
36// A0 = GND -> 0x40
37// A0 = VS -> 0x41
38// A0 = SDA -> 0x42
39// A0 = SCL -> 0x43
40
42 if (!this->write_byte_16(INA3221_REGISTER_CONFIG, INA3221_CONFIG_RESET)) {
43 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
44 return;
45 }
46 delay(1);
47
48 if (!this->write_byte_16(INA3221_REGISTER_CONFIG, this->config_)) {
49 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
50 return;
51 }
52
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_))) {
60 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
61 return;
62 }
63 }
64#endif
65
66#ifdef USE_INA3221_SUMMATION
67 // Sum the shunt voltages of every enabled channel
68 if (this->sum_shunt_voltage_sensor_ != nullptr &&
69 !this->write_byte_16(INA3221_REGISTER_MASK_ENABLE, this->config_ & INA3221_CONFIG_CHANNEL_MASK)) {
70 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
71 return;
72 }
73#endif
74}
75
76void INA3221Component::dump_config() {
77 ESP_LOGCONFIG(TAG, "INA3221:");
78 LOG_I2C_DEVICE(this);
79 LOG_UPDATE_INTERVAL(this);
80 ESP_LOGCONFIG(TAG, " Config register: 0x%04X", this->config_);
81 if (this->single_shot_wait_ms_ != 0) {
82 ESP_LOGCONFIG(TAG, " Single-shot conversion: %u ms", this->single_shot_wait_ms_);
83 }
84
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_);
101#endif
102}
103
104inline uint8_t ina3221_bus_voltage_register(int channel) { return 0x02 + channel * 2; }
105
106inline uint8_t ina3221_shunt_voltage_register(int channel) { return 0x01 + channel * 2; }
107
108void INA3221Component::update() {
109 if (this->single_shot_wait_ms_ == 0) {
110 this->read_data_();
111 return;
112 }
113 // In single-shot mode, writing the configuration register starts one conversion of every channel
114 if (!this->write_byte_16(INA3221_REGISTER_CONFIG, this->config_)) {
115 this->status_set_warning();
116 return;
117 }
118 this->set_timeout(READ_TIMEOUT_ID, this->single_shot_wait_ms_, [this]() { this->read_data_(); });
119}
120
122#ifdef USE_INA3221_SUMMATION
123 // The software sums need every enabled channel's readings, not only the ones with their own sensors
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;
127#else
128 constexpr bool sum_all = false;
129#endif
130
131 for (int i = 0; i < 3; i++) {
132 if ((this->config_ & (INA3221_CONFIG_CHANNEL1_ENABLE >> i)) == 0) {
133 continue;
134 }
135 INA3221Channel &channel = this->channels_[i];
136 float bus_voltage_v = NAN, current_a = NAN;
137 uint16_t raw;
138 if (sum_all || channel.should_measure_bus_voltage()) {
140 this->status_set_warning();
141 return;
142 }
143 bus_voltage_v = int16_t(raw) / 1000.0f;
144 if (channel.bus_voltage_sensor_ != nullptr)
145 channel.bus_voltage_sensor_->publish_state(bus_voltage_v);
146 }
147 if (sum_all || channel.should_measure_shunt_voltage()) {
149 this->status_set_warning();
150 return;
151 }
152 const float shunt_voltage_v = int16_t(raw) * 40.0f / 8.0f / 1000000.0f;
153 if (channel.shunt_voltage_sensor_ != nullptr)
154 channel.shunt_voltage_sensor_->publish_state(shunt_voltage_v);
155 current_a = shunt_voltage_v / channel.shunt_resistance_;
156 if (channel.current_sensor_ != nullptr)
157 channel.current_sensor_->publish_state(current_a);
158 }
159 if (channel.power_sensor_ != nullptr) {
160 channel.power_sensor_->publish_state(bus_voltage_v * current_a);
161 }
162#ifdef USE_INA3221_SUMMATION
163 if (sum_all) {
164 total_current_a += current_a;
165 total_power_w += bus_voltage_v * current_a;
166 }
167#endif
168 }
169
170#ifdef USE_INA3221_SUMMATION
171 if (this->sum_shunt_voltage_sensor_ != nullptr) {
172 uint16_t raw;
173 if (!this->read_byte_16(INA3221_REGISTER_SHUNT_VOLTAGE_SUM, &raw)) {
174 this->status_set_warning();
175 return;
176 }
177 // 40 uV per step, held in bits 15..1
178 this->sum_shunt_voltage_sensor_->publish_state(int16_t(raw) * 40.0f / 2.0f / 1000000.0f);
179 }
180 if (this->sum_current_sensor_ != nullptr) {
181 this->sum_current_sensor_->publish_state(total_current_a);
182 }
183 if (this->sum_power_sensor_ != nullptr) {
184 this->sum_power_sensor_->publish_state(total_power_w);
185 }
186#endif
187}
188
189void INA3221Component::on_powerdown() {
190 // Mode 0 powers the chip down; setup() resets it on the next boot
191 this->write_byte_16(INA3221_REGISTER_CONFIG, static_cast<uint16_t>(this->config_ & ~INA3221_CONFIG_MODE_MASK));
192}
193
194void INA3221Component::set_shunt_resistance(int channel, float resistance_ohm) {
195 this->channels_[channel].shunt_resistance_ = resistance_ohm;
196}
197
199 return this->shunt_voltage_sensor_ != nullptr || this->current_sensor_ != nullptr || this->power_sensor_ != nullptr;
200}
202 return this->bus_voltage_sensor_ != nullptr || this->power_sensor_ != nullptr;
203}
204
205} // namespace esphome::ina3221
uint8_t raw[35]
Definition bl0939.h:0
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:281
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.cpp:101
struct esphome::ina3221::INA3221Component::INA3221Channel channels_[3]
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
ESPHOME_LOG_TAG(TAG, "ina3221")
uint8_t ina3221_shunt_voltage_register(int channel)
Definition ina3221.cpp:106
uint8_t ina3221_bus_voltage_register(int channel)
Definition ina3221.cpp:104
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t