From b2792b7e29d5245e3ca89eabe73a691a843226e6 Mon Sep 17 00:00:00 2001 From: Balduin Date: Tue, 14 Apr 2026 10:02:27 +0200 Subject: [PATCH] feat(ina2xx): count ignored zero readings with perf counter --- src/drivers/power_monitor/ina220/ina220.cpp | 5 +++++ src/drivers/power_monitor/ina220/ina220.h | 1 + src/drivers/power_monitor/ina226/ina226.cpp | 5 +++++ src/drivers/power_monitor/ina226/ina226.h | 1 + src/drivers/power_monitor/ina228/ina228.cpp | 5 +++++ src/drivers/power_monitor/ina228/ina228.h | 1 + src/drivers/power_monitor/ina238/ina238.cpp | 4 ++++ src/drivers/power_monitor/ina238/ina238.h | 1 + 8 files changed, 23 insertions(+) diff --git a/src/drivers/power_monitor/ina220/ina220.cpp b/src/drivers/power_monitor/ina220/ina220.cpp index 05ad028f22..b055192d3c 100644 --- a/src/drivers/power_monitor/ina220/ina220.cpp +++ b/src/drivers/power_monitor/ina220/ina220.cpp @@ -51,6 +51,7 @@ INA220::INA220(const I2CSPIDriverConfig &config, int battery_index) : _comms_errors(perf_alloc(PC_COUNT, "ina220_com_err")), _collection_errors(perf_alloc(PC_COUNT, "ina220_collection_err")), _measure_errors(perf_alloc(PC_COUNT, "ina220_measurement_err")), + _zero_reading_perf(perf_alloc(PC_COUNT, "ina220_zero_reading")), _ch_type((PM_CH_TYPE)config.custom2), _battery(battery_index, this, INA220_SAMPLE_INTERVAL_US, battery_status_s::SOURCE_POWER_MODULE) { @@ -105,6 +106,7 @@ INA220::~INA220() perf_free(_comms_errors); perf_free(_collection_errors); perf_free(_measure_errors); + perf_free(_zero_reading_perf); } int INA220::read(uint8_t address, int16_t &data) @@ -253,6 +255,8 @@ INA220::collect() // exactly 0 is very improbable, we just ignore those readings. // The battery library keeps the old value. + if (!_bus_voltage || !_bus_current) { perf_count(_zero_reading_perf); } + if (_bus_voltage) { _battery.updateVoltage(_voltage); } if (_bus_current) { _battery.updateCurrent(_current); } @@ -354,6 +358,7 @@ INA220::print_status() if (_initialized) { perf_print_counter(_sample_perf); perf_print_counter(_comms_errors); + perf_print_counter(_zero_reading_perf); switch (_ch_type) { case PM_CH_TYPE_VBATT: diff --git a/src/drivers/power_monitor/ina220/ina220.h b/src/drivers/power_monitor/ina220/ina220.h index bbbcb27c66..87af74705d 100644 --- a/src/drivers/power_monitor/ina220/ina220.h +++ b/src/drivers/power_monitor/ina220/ina220.h @@ -180,6 +180,7 @@ private: perf_counter_t _comms_errors; perf_counter_t _collection_errors; perf_counter_t _measure_errors; + perf_counter_t _zero_reading_perf; int16_t _bus_voltage{0}; int16_t _bus_power{0}; diff --git a/src/drivers/power_monitor/ina226/ina226.cpp b/src/drivers/power_monitor/ina226/ina226.cpp index a02b91eb2c..19a46c0eb3 100644 --- a/src/drivers/power_monitor/ina226/ina226.cpp +++ b/src/drivers/power_monitor/ina226/ina226.cpp @@ -49,6 +49,7 @@ INA226::INA226(const I2CSPIDriverConfig &config, int battery_index) : _comms_errors(perf_alloc(PC_COUNT, "ina226_com_err")), _collection_errors(perf_alloc(PC_COUNT, "ina226_collection_err")), _measure_errors(perf_alloc(PC_COUNT, "ina226_measurement_err")), + _zero_reading_perf(perf_alloc(PC_COUNT, "ina226_zero_reading")), _battery(battery_index, this, INA226_SAMPLE_INTERVAL_US, battery_status_s::SOURCE_POWER_MODULE) { float fvalue = MAX_CURRENT; @@ -96,6 +97,7 @@ INA226::~INA226() perf_free(_comms_errors); perf_free(_collection_errors); perf_free(_measure_errors); + perf_free(_zero_reading_perf); } int INA226::read(uint8_t address, int16_t &data) @@ -233,6 +235,8 @@ INA226::collect() // exactly 0 is very improbable, we just ignore those readings. // The battery library keeps the old value. + if (!_bus_voltage || !_current) { perf_count(_zero_reading_perf); } + if (_bus_voltage) { _battery.updateVoltage(static_cast(_bus_voltage * INA226_VSCALE)); } if (_current) { _battery.updateCurrent(static_cast(_current * _current_lsb)); } @@ -341,6 +345,7 @@ INA226::print_status() if (_initialized) { perf_print_counter(_sample_perf); perf_print_counter(_comms_errors); + perf_print_counter(_zero_reading_perf); printf("poll interval: %u \n", _measure_interval); diff --git a/src/drivers/power_monitor/ina226/ina226.h b/src/drivers/power_monitor/ina226/ina226.h index a69695a1cc..277120f796 100644 --- a/src/drivers/power_monitor/ina226/ina226.h +++ b/src/drivers/power_monitor/ina226/ina226.h @@ -186,6 +186,7 @@ private: perf_counter_t _comms_errors; perf_counter_t _collection_errors; perf_counter_t _measure_errors; + perf_counter_t _zero_reading_perf; int16_t _bus_voltage{0}; int16_t _power{0}; diff --git a/src/drivers/power_monitor/ina228/ina228.cpp b/src/drivers/power_monitor/ina228/ina228.cpp index fbc015b57c..d461ed23a9 100644 --- a/src/drivers/power_monitor/ina228/ina228.cpp +++ b/src/drivers/power_monitor/ina228/ina228.cpp @@ -49,6 +49,7 @@ INA228::INA228(const I2CSPIDriverConfig &config, int battery_index) : _comms_errors(perf_alloc(PC_COUNT, "ina228_com_err")), _collection_errors(perf_alloc(PC_COUNT, "ina228_collection_err")), _measure_errors(perf_alloc(PC_COUNT, "ina228_measurement_err")), + _zero_reading_perf(perf_alloc(PC_COUNT, "ina228_zero_reading")), _battery(battery_index, this, INA228_SAMPLE_INTERVAL_US, battery_status_s::SOURCE_POWER_MODULE) { float fvalue = MAX_CURRENT; @@ -111,6 +112,7 @@ INA228::~INA228() perf_free(_comms_errors); perf_free(_collection_errors); perf_free(_measure_errors); + perf_free(_zero_reading_perf); } int INA228::read(uint8_t address, int16_t &data) @@ -328,6 +330,8 @@ INA228::collect() // exactly 0 is very improbable, we just ignore those readings. // The battery library keeps the old value. + if (!_bus_voltage || !_current || !_temperature) { perf_count(_zero_reading_perf); } + if (_bus_voltage) { _battery.updateVoltage(static_cast(_bus_voltage * INA228_VSCALE)); } if (_current) { _battery.updateCurrent(static_cast(_current * _current_lsb)); } @@ -438,6 +442,7 @@ INA228::print_status() if (_initialized) { perf_print_counter(_sample_perf); perf_print_counter(_comms_errors); + perf_print_counter(_zero_reading_perf); printf("poll interval: %u \n", _measure_interval); diff --git a/src/drivers/power_monitor/ina228/ina228.h b/src/drivers/power_monitor/ina228/ina228.h index 72a89e6dcb..7788bb67ab 100644 --- a/src/drivers/power_monitor/ina228/ina228.h +++ b/src/drivers/power_monitor/ina228/ina228.h @@ -338,6 +338,7 @@ private: perf_counter_t _comms_errors; perf_counter_t _collection_errors; perf_counter_t _measure_errors; + perf_counter_t _zero_reading_perf; int32_t _bus_voltage{0}; int64_t _power{0}; diff --git a/src/drivers/power_monitor/ina238/ina238.cpp b/src/drivers/power_monitor/ina238/ina238.cpp index 233f4677d9..85581f5c68 100644 --- a/src/drivers/power_monitor/ina238/ina238.cpp +++ b/src/drivers/power_monitor/ina238/ina238.cpp @@ -102,6 +102,7 @@ INA238::~INA238() perf_free(_sample_perf); perf_free(_comms_errors); perf_free(_collection_errors); + perf_free(_zero_reading_perf); } int INA238::read(uint8_t address, uint16_t &data) @@ -257,6 +258,8 @@ int INA238::collect() // exactly 0 is very improbable, we just ignore those readings. // The battery library keeps the old value. + if (!bus_voltage || !current || !temperature) { perf_count(_zero_reading_perf); } + if (bus_voltage) { _battery.updateVoltage(static_cast(bus_voltage * INA238_VSCALE)); } if (current) { _battery.updateCurrent(static_cast(current * _current_lsb)); } @@ -382,6 +385,7 @@ void INA238::print_status() if (_initialized) { perf_print_counter(_sample_perf); perf_print_counter(_comms_errors); + perf_print_counter(_zero_reading_perf); printf("poll interval: %u \n", _measure_interval); diff --git a/src/drivers/power_monitor/ina238/ina238.h b/src/drivers/power_monitor/ina238/ina238.h index 62750c44c4..2f0fe7f354 100644 --- a/src/drivers/power_monitor/ina238/ina238.h +++ b/src/drivers/power_monitor/ina238/ina238.h @@ -133,6 +133,7 @@ private: perf_counter_t _comms_errors; perf_counter_t _collection_errors; perf_counter_t _bad_register_perf{perf_alloc(PC_COUNT, MODULE_NAME": bad register")}; + perf_counter_t _zero_reading_perf{perf_alloc(PC_COUNT, MODULE_NAME": zero reading")}; // Configuration state, computed from params float _max_current;