Merge remote-tracking branch 'upstream/fix-bl0942-energy-counter-reset' into integration

This commit is contained in:
J. Nick Koston
2026-03-28 14:12:32 -10:00
+12 -1
View File
@@ -163,9 +163,20 @@ void BL0942::received_package_(DataPacket *data) {
// cf_cnt is only 24 bits, so track overflows
uint32_t cf_cnt = (uint24_t) data->cf_cnt;
uint32_t prev_cf_cnt_24 = this->prev_cf_cnt_ & 0x00ffffff;
cf_cnt |= this->prev_cf_cnt_ & 0xff000000;
if (cf_cnt < this->prev_cf_cnt_) {
cf_cnt += 0x1000000;
if (prev_cf_cnt_24 > 0x800000) {
// Previous value was in the upper half of the 24-bit range,
// so this is likely a genuine overflow from 0xFFFFFF to 0x000000.
cf_cnt += 0x1000000;
} else {
// Previous value was low, so the BL0942 chip likely reset its
// counter (e.g. due to electrical disturbance). Don't treat as
// overflow — just continue from the new value to avoid a ~3MWh jump.
ESP_LOGW(TAG, "BL0942 energy counter unexpectedly decreased from %" PRIu32 " to %" PRIu32 ", assuming chip reset",
this->prev_cf_cnt_, cf_cnt);
}
}
this->prev_cf_cnt_ = cf_cnt;