ESPHome  2024.11.0
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 
5 namespace esphome {
6 namespace ina3221 {
7 
8 static const char *const TAG = "ina3221";
9 
10 static const uint8_t INA3221_REGISTER_CONFIG = 0x00;
11 static const uint8_t INA3221_REGISTER_CHANNEL1_SHUNT_VOLTAGE = 0x01;
12 static const uint8_t INA3221_REGISTER_CHANNEL1_BUS_VOLTAGE = 0x02;
13 static const uint8_t INA3221_REGISTER_CHANNEL2_SHUNT_VOLTAGE = 0x03;
14 static const uint8_t INA3221_REGISTER_CHANNEL2_BUS_VOLTAGE = 0x04;
15 static const uint8_t INA3221_REGISTER_CHANNEL3_SHUNT_VOLTAGE = 0x05;
16 static const uint8_t INA3221_REGISTER_CHANNEL3_BUS_VOLTAGE = 0x06;
17 
18 // Addresses:
19 // A0 = GND -> 0x40
20 // A0 = VS -> 0x41
21 // A0 = SDA -> 0x42
22 // A0 = SCL -> 0x43
23 
25  ESP_LOGCONFIG(TAG, "Setting up INA3221...");
26  // Config Register
27  // 0bx000000000000000 << 15 RESET Bit (1 -> trigger reset)
28  if (!this->write_byte_16(INA3221_REGISTER_CONFIG, 0x8000)) {
29  this->mark_failed();
30  return;
31  }
32  delay(1);
33 
34  uint16_t config = 0;
35  // 0b0xxx000000000000 << 12 Channel Enables (1 -> ON)
36  if (this->channels_[0].exists()) {
37  config |= 0b0100000000000000;
38  }
39  if (this->channels_[1].exists()) {
40  config |= 0b0010000000000000;
41  }
42  if (this->channels_[2].exists()) {
43  config |= 0b0001000000000000;
44  }
45  // 0b0000xxx000000000 << 9 Averaging Mode (0 -> 1 sample, 111 -> 1024 samples)
46  config |= 0b0000000000000000;
47  // 0b0000000xxx000000 << 6 Bus Voltage Conversion time (100 -> 1.1ms, 111 -> 8.244 ms)
48  config |= 0b0000000111000000;
49  // 0b0000000000xxx000 << 3 Shunt Voltage Conversion time (same as above)
50  config |= 0b0000000000111000;
51  // 0b0000000000000xxx << 0 Operating mode (111 -> Shunt and bus, continuous)
52  config |= 0b0000000000000111;
53  if (!this->write_byte_16(INA3221_REGISTER_CONFIG, config)) {
54  this->mark_failed();
55  return;
56  }
57 }
58 
60  ESP_LOGCONFIG(TAG, "INA3221:");
61  LOG_I2C_DEVICE(this);
62  if (this->is_failed()) {
63  ESP_LOGE(TAG, "Communication with INA3221 failed!");
64  }
65  LOG_UPDATE_INTERVAL(this);
66 
67  LOG_SENSOR(" ", "Bus Voltage #1", this->channels_[0].bus_voltage_sensor_);
68  LOG_SENSOR(" ", "Shunt Voltage #1", this->channels_[0].shunt_voltage_sensor_);
69  LOG_SENSOR(" ", "Current #1", this->channels_[0].current_sensor_);
70  LOG_SENSOR(" ", "Power #1", this->channels_[0].power_sensor_);
71  LOG_SENSOR(" ", "Bus Voltage #2", this->channels_[1].bus_voltage_sensor_);
72  LOG_SENSOR(" ", "Shunt Voltage #2", this->channels_[1].shunt_voltage_sensor_);
73  LOG_SENSOR(" ", "Current #2", this->channels_[1].current_sensor_);
74  LOG_SENSOR(" ", "Power #2", this->channels_[1].power_sensor_);
75  LOG_SENSOR(" ", "Bus Voltage #3", this->channels_[2].bus_voltage_sensor_);
76  LOG_SENSOR(" ", "Shunt Voltage #3", this->channels_[2].shunt_voltage_sensor_);
77  LOG_SENSOR(" ", "Current #3", this->channels_[2].current_sensor_);
78  LOG_SENSOR(" ", "Power #3", this->channels_[2].power_sensor_);
79 }
80 
81 inline uint8_t ina3221_bus_voltage_register(int channel) { return 0x02 + channel * 2; }
82 
83 inline uint8_t ina3221_shunt_voltage_register(int channel) { return 0x01 + channel * 2; }
84 
86  for (int i = 0; i < 3; i++) {
87  INA3221Channel &channel = this->channels_[i];
88  float bus_voltage_v = NAN, current_a = NAN;
89  uint16_t raw;
90  if (channel.should_measure_bus_voltage()) {
91  if (!this->read_byte_16(ina3221_bus_voltage_register(i), &raw)) {
92  this->status_set_warning();
93  return;
94  }
95  bus_voltage_v = int16_t(raw) / 1000.0f;
96  if (channel.bus_voltage_sensor_ != nullptr)
97  channel.bus_voltage_sensor_->publish_state(bus_voltage_v);
98  }
99  if (channel.should_measure_shunt_voltage()) {
100  if (!this->read_byte_16(ina3221_shunt_voltage_register(i), &raw)) {
101  this->status_set_warning();
102  return;
103  }
104  const float shunt_voltage_v = int16_t(raw) * 40.0f / 8.0f / 1000000.0f;
105  if (channel.shunt_voltage_sensor_ != nullptr)
106  channel.shunt_voltage_sensor_->publish_state(shunt_voltage_v);
107  current_a = shunt_voltage_v / channel.shunt_resistance_;
108  if (channel.current_sensor_ != nullptr)
109  channel.current_sensor_->publish_state(current_a);
110  }
111  if (channel.power_sensor_ != nullptr) {
112  channel.power_sensor_->publish_state(bus_voltage_v * current_a);
113  }
114  }
115 }
116 
118 void INA3221Component::set_shunt_resistance(int channel, float resistance_ohm) {
119  this->channels_[channel].shunt_resistance_ = resistance_ohm;
120 }
121 
123  return this->bus_voltage_sensor_ != nullptr || this->shunt_voltage_sensor_ != nullptr ||
124  this->current_sensor_ != nullptr || this->power_sensor_ != nullptr;
125 }
127  return this->shunt_voltage_sensor_ != nullptr || this->current_sensor_ != nullptr || this->power_sensor_ != nullptr;
128 }
130  return this->bus_voltage_sensor_ != nullptr || this->power_sensor_ != nullptr;
131 }
132 
133 } // namespace ina3221
134 } // namespace esphome
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
uint8_t raw[35]
Definition: bl0939.h:19
float get_setup_priority() const override
Definition: ina3221.cpp:117
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
bool is_failed() const
Definition: component.cpp:143
uint8_t ina3221_shunt_voltage_register(int channel)
Definition: ina3221.cpp:83
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
struct esphome::ina3221::INA3221Component::INA3221Channel channels_[3]
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
uint8_t ina3221_bus_voltage_register(int channel)
Definition: ina3221.cpp:81
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_shunt_resistance(int channel, float resistance_ohm)
Definition: ina3221.cpp:118
bool write_byte_16(uint8_t a_register, uint16_t data)
Definition: i2c.h:266
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26