ESPHome  2024.11.0
ina260.cpp
Go to the documentation of this file.
1 #include "ina260.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/hal.h"
4 
5 namespace esphome {
6 namespace ina260 {
7 
8 static const char *const TAG = "ina260";
9 
10 // | A0 | A1 | Address |
11 // | GND | GND | 0x40 |
12 // | GND | V_S+ | 0x41 |
13 // | GND | SDA | 0x42 |
14 // | GND | SCL | 0x43 |
15 // | V_S+ | GND | 0x44 |
16 // | V_S+ | V_S+ | 0x45 |
17 // | V_S+ | SDA | 0x46 |
18 // | V_S+ | SCL | 0x47 |
19 // | SDA | GND | 0x48 |
20 // | SDA | V_S+ | 0x49 |
21 // | SDA | SDA | 0x4A |
22 // | SDA | SCL | 0x4B |
23 // | SCL | GND | 0x4C |
24 // | SCL | V_S+ | 0x4D |
25 // | SCL | SDA | 0x4E |
26 // | SCL | SCL | 0x4F |
27 
28 static const uint8_t INA260_REGISTER_CONFIG = 0x00;
29 static const uint8_t INA260_REGISTER_CURRENT = 0x01;
30 static const uint8_t INA260_REGISTER_BUS_VOLTAGE = 0x02;
31 static const uint8_t INA260_REGISTER_POWER = 0x03;
32 static const uint8_t INA260_REGISTER_MASK_ENABLE = 0x06;
33 static const uint8_t INA260_REGISTER_ALERT_LIMIT = 0x07;
34 static const uint8_t INA260_REGISTER_MANUFACTURE_ID = 0xFE;
35 static const uint8_t INA260_REGISTER_DEVICE_ID = 0xFF;
36 
38  ESP_LOGCONFIG(TAG, "Setting up INA260...");
39 
40  // Reset device on setup
41  if (!this->write_byte_16(INA260_REGISTER_CONFIG, 0x8000)) {
42  this->error_code_ = DEVICE_RESET_FAILED;
43  this->mark_failed();
44  return;
45  }
46 
47  delay(2);
48 
49  this->read_byte_16(INA260_REGISTER_MANUFACTURE_ID, &this->manufacture_id_);
50  this->read_byte_16(INA260_REGISTER_DEVICE_ID, &this->device_id_);
51 
52  if (this->manufacture_id_ != (uint16_t) 0x5449 || this->device_id_ != (uint16_t) 0x2270) {
53  this->error_code_ = COMMUNICATION_FAILED;
54  this->mark_failed();
55  return;
56  }
57 
58  if (!this->write_byte_16(INA260_REGISTER_CONFIG, (uint16_t) 0b0000001100000111)) {
59  this->error_code_ = FAILED_TO_UPDATE_CONFIGURATION;
60  this->mark_failed();
61  return;
62  }
63 }
64 
66  ESP_LOGCONFIG(TAG, "INA260:");
67  LOG_I2C_DEVICE(this);
68  LOG_UPDATE_INTERVAL(this);
69 
70  ESP_LOGCONFIG(TAG, " Manufacture ID: 0x%x", this->manufacture_id_);
71  ESP_LOGCONFIG(TAG, " Device ID: 0x%x", this->device_id_);
72 
73  LOG_SENSOR(" ", "Bus Voltage", this->bus_voltage_sensor_);
74  LOG_SENSOR(" ", "Current", this->current_sensor_);
75  LOG_SENSOR(" ", "Power", this->power_sensor_);
76 
77  switch (this->error_code_) {
79  ESP_LOGE(TAG, "Connected device does not match a known INA260 sensor");
80  break;
82  ESP_LOGE(TAG, "Device reset failed - Is the device connected?");
83  break;
85  ESP_LOGE(TAG, "Failed to update device configuration");
86  break;
87  case NONE:
88  default:
89  break;
90  }
91 }
92 
94  if (this->bus_voltage_sensor_ != nullptr) {
95  uint16_t raw_bus_voltage;
96  if (!this->read_byte_16(INA260_REGISTER_BUS_VOLTAGE, &raw_bus_voltage)) {
97  this->status_set_warning();
98  return;
99  }
100  float bus_voltage_v = int16_t(raw_bus_voltage) * 0.00125f;
101  this->bus_voltage_sensor_->publish_state(bus_voltage_v);
102  }
103 
104  if (this->current_sensor_ != nullptr) {
105  uint16_t raw_current;
106  if (!this->read_byte_16(INA260_REGISTER_CURRENT, &raw_current)) {
107  this->status_set_warning();
108  return;
109  }
110  float current_a = int16_t(raw_current) * 0.00125f;
111  this->current_sensor_->publish_state(current_a);
112  }
113 
114  if (this->power_sensor_ != nullptr) {
115  uint16_t raw_power;
116  if (!this->read_byte_16(INA260_REGISTER_POWER, &raw_power)) {
117  this->status_set_warning();
118  return;
119  }
120  float power_w = ((int16_t(raw_power) * 10.0f) / 1000.0f);
121  this->power_sensor_->publish_state(power_w);
122  }
123 
124  this->status_clear_warning();
125 }
126 
127 } // namespace ina260
128 } // namespace esphome
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition: i2c.h:246
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
enum esphome::ina260::INA260Component::ErrorCode NONE
sensor::Sensor * bus_voltage_sensor_
Definition: ina260.h:26
void dump_config() override
Definition: ina260.cpp:65
sensor::Sensor * power_sensor_
Definition: ina260.h:28
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
sensor::Sensor * current_sensor_
Definition: ina260.h:27
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
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 IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26