9 static const char *
const TAG =
"ina226";
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;
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};
40 ESP_LOGCONFIG(TAG,
"Setting up INA226...");
72 uint32_t lsb =
static_cast<uint32_t
>(ceilf(this->
max_current_a_ * 1000000.0f / 32768));
78 ESP_LOGV(TAG,
" Using LSB=%" PRIu32
" calibration=%" PRIu32, lsb, calibration);
80 if (!this->
write_byte_16(INA226_REGISTER_CALIBRATION, calibration)) {
87 ESP_LOGCONFIG(TAG,
"INA226:");
91 ESP_LOGE(TAG,
"Communication with INA226 failed!");
94 LOG_UPDATE_INTERVAL(
this);
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]);
110 uint16_t raw_bus_voltage;
111 if (!this->
read_byte_16(INA226_REGISTER_BUS_VOLTAGE, &raw_bus_voltage)) {
117 bus_voltage_v *= 0.00125f;
122 uint16_t raw_shunt_voltage;
123 if (!this->
read_byte_16(INA226_REGISTER_SHUNT_VOLTAGE, &raw_shunt_voltage)) {
129 shunt_voltage_v *= 0.0000025f;
134 uint16_t raw_current;
135 if (!this->
read_byte_16(INA226_REGISTER_CURRENT, &raw_current)) {
147 if (!this->
read_byte_16(INA226_REGISTER_POWER, &raw_power)) {
151 float power_mw = int16_t(raw_power) * (this->
calibration_lsb_ * 25.0f / 1000.0f);
159 if (val & ((uint32_t) 1 << (bits - 1))) {
160 val -= (uint32_t) 1 << bits;
sensor::Sensor * power_sensor_
AdcTime adc_time_current_
bool read_byte_16(uint8_t a_register, uint16_t *data)
AdcTime bus_voltage_conversion_time
const float DATA
For components that import data from directly connected sensors like DHT.
AdcAvgSamples adc_avg_samples_
void status_set_warning(const char *message="unspecified")
uint32_t calibration_lsb_
sensor::Sensor * bus_voltage_sensor_
sensor::Sensor * current_sensor_
float get_setup_priority() const override
AdcTime adc_time_voltage_
float shunt_resistance_ohm_
AdcAvgSamples avg_samples
void status_clear_warning()
void publish_state(float state)
Publish a new state to the front-end.
virtual void mark_failed()
Mark this component as failed.
int32_t twos_complement_(int32_t val, uint8_t bits)
Implementation of SPI Controller mode.
bool write_byte_16(uint8_t a_register, uint16_t data)
AdcTime shunt_voltage_conversion_time
void dump_config() override
sensor::Sensor * shunt_voltage_sensor_
void IRAM_ATTR HOT delay(uint32_t ms)