37 ESP_LOGCONFIG(TAG,
"Setting up INA219...");
66 uint16_t config = 0x0000;
68 config |= 0b0000000000000111;
70 config |= 0b0000011110000000;
71 config |= 0b0000000001111000;
75 bool bus_32v_range = this->
max_voltage_v_ > 16.0f || shunt_max_voltage > 0.16f;
78 config |= 0b0010000000000000;
81 config |= 0b0000000000000000;
87 if (shunt_max_voltage * multiplier <= 0.02f) {
89 }
else if (shunt_max_voltage * multiplier <= 0.04f) {
91 }
else if (shunt_max_voltage * multiplier <= 0.08f) {
94 if (
int(shunt_max_voltage * multiplier * 100) > 16) {
96 " Max voltage across shunt resistor (resistance*current) exceeds %dmV. "
97 "This could damage the sensor!",
98 int(160 / multiplier));
103 config |= shunt_gain << 11;
104 ESP_LOGCONFIG(TAG,
" Using %dV-Range Shunt Gain=%dmV", bus_32v_range ? 32 : 16, 40 << shunt_gain);
110 auto min_lsb = uint32_t(ceilf(this->max_current_a_ * 1000000.0f / 0x8000));
111 auto max_lsb = uint32_t(floorf(this->max_current_a_ * 1000000.0f / 0x1000));
112 uint32_t lsb = min_lsb;
113 for (; lsb <= max_lsb; lsb++) {
114 float max_current_before_overflow = lsb * 0x7FFF / 1000000.0f;
115 if (this->max_current_a_ <= max_current_before_overflow)
120 ESP_LOGW(TAG,
" The requested current (%0.02fA) cannot be achieved without an overflow", this->max_current_a_);
125 ESP_LOGV(TAG,
" Using LSB=%" PRIu32
" calibration=%" PRIu32, lsb, calibration);
126 if (!this->
write_byte_16(INA219_REGISTER_CALIBRATION, calibration)) {
152 uint16_t raw_bus_voltage;
153 if (!this->
read_byte_16(INA219_REGISTER_BUS_VOLTAGE, &raw_bus_voltage)) {
157 raw_bus_voltage >>= 3;
158 float bus_voltage_v = int16_t(raw_bus_voltage) * 0.004f;
163 uint16_t raw_shunt_voltage;
164 if (!this->
read_byte_16(INA219_REGISTER_SHUNT_VOLTAGE, &raw_shunt_voltage)) {
168 float shunt_voltage_mv = int16_t(raw_shunt_voltage) * 0.01f;
173 uint16_t raw_current;
174 if (!this->
read_byte_16(INA219_REGISTER_CURRENT, &raw_current)) {
178 float current_ma = int16_t(raw_current) * (this->
calibration_lsb_ / 1000.0f);
184 if (!this->
read_byte_16(INA219_REGISTER_POWER, &raw_power)) {
188 float power_mw = int16_t(raw_power) * (this->
calibration_lsb_ * 20.0f / 1000.0f);