10static const char *
const TAG =
"atm90e32";
12static uint32_t pref_hash(
const char *prefix,
const char *name_space) {
21 if (current_pref.
load(¤t)) {
33 for (uint8_t phase = 0; phase < 3; phase++) {
34 if (this->
phase_[phase].voltage_sensor_ !=
nullptr)
37 if (this->
phase_[phase].current_sensor_ !=
nullptr)
40 if (this->
phase_[phase].power_sensor_ !=
nullptr)
43 if (this->
phase_[phase].power_factor_sensor_ !=
nullptr)
46 if (this->
phase_[phase].reactive_power_sensor_ !=
nullptr)
49 if (this->
phase_[phase].apparent_power_sensor_ !=
nullptr)
52 if (this->
phase_[phase].forward_active_energy_sensor_ !=
nullptr)
55 if (this->
phase_[phase].reverse_active_energy_sensor_ !=
nullptr)
58 if (this->
phase_[phase].phase_angle_sensor_ !=
nullptr)
61 if (this->
phase_[phase].harmonic_active_power_sensor_ !=
nullptr)
64 if (this->
phase_[phase].peak_current_sensor_ !=
nullptr)
68 if (this->
phase_[phase].voltage_sensor_ !=
nullptr)
71 if (this->
phase_[phase].current_sensor_ !=
nullptr)
74 if (this->
phase_[phase].power_sensor_ !=
nullptr)
77 if (this->
phase_[phase].power_factor_sensor_ !=
nullptr)
80 if (this->
phase_[phase].reactive_power_sensor_ !=
nullptr)
83 if (this->
phase_[phase].apparent_power_sensor_ !=
nullptr)
86 if (this->
phase_[phase].forward_active_energy_sensor_ !=
nullptr) {
91 if (this->
phase_[phase].reverse_active_energy_sensor_ !=
nullptr) {
96 if (this->
phase_[phase].phase_angle_sensor_ !=
nullptr)
99 if (this->
phase_[phase].harmonic_active_power_sensor_ !=
nullptr) {
104 if (this->
phase_[phase].peak_current_sensor_ !=
nullptr)
116 if (this->
read16_(ATM90E32_REGISTER_METEREN) != 1) {
123#ifdef USE_TEXT_SENSOR
141 const bool has_distinct_legacy_namespace = strcmp(cs, legacy_cs) != 0;
143 uint16_t mmode0 = 0x87;
144 uint16_t high_thresh = 0;
145 uint16_t low_thresh = 0;
162 this->
write16_(ATM90E32_REGISTER_SOFTRESET, 0x789A,
false);
164 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA);
166 ESP_LOGW(TAG,
"Could not initialize ATM90E32 IC, check SPI settings");
171 this->
write16_(ATM90E32_REGISTER_METEREN, 0x0001);
172 this->
write16_(ATM90E32_REGISTER_SAGPEAKDETCFG, 0xFF3F);
173 this->
write16_(ATM90E32_REGISTER_PLCONSTH, 0x0861);
174 this->
write16_(ATM90E32_REGISTER_PLCONSTL, 0xC468);
175 this->
write16_(ATM90E32_REGISTER_ZXCONFIG, 0xD654);
176 this->
write16_(ATM90E32_REGISTER_MMODE0, mmode0);
178 this->
write16_(ATM90E32_REGISTER_FREQHITH, high_thresh);
179 this->
write16_(ATM90E32_REGISTER_FREQLOTH, low_thresh);
180 this->
write16_(ATM90E32_REGISTER_PSTARTTH, 0x1D4C);
181 this->
write16_(ATM90E32_REGISTER_QSTARTTH, 0x1D4C);
182 this->
write16_(ATM90E32_REGISTER_SSTARTTH, 0x1D4C);
183 this->
write16_(ATM90E32_REGISTER_PPHASETH, 0x02EE);
184 this->
write16_(ATM90E32_REGISTER_QPHASETH, 0x02EE);
188 uint32_t o_hash = pref_hash(
"_offset_calibration_", cs);
190 bool migrated_offset =
false;
191 if (has_distinct_legacy_namespace) {
192 uint32_t legacy_o_hash = pref_hash(
"_offset_calibration_", legacy_cs);
195 int migration_status = migrate_legacy_pref_if_needed(this->
offset_pref_, legacy_offset_pref, &offset_data);
196 migrated_offset = migration_status > 0;
197 if (migration_status > 0) {
198 ESP_LOGI(TAG,
"[CALIBRATION][%s] Migrated offset calibrations from legacy storage.", cs);
199 }
else if (migration_status < 0) {
200 ESP_LOGW(TAG,
"[CALIBRATION][%s] Failed to migrate offset calibrations from legacy storage.", cs);
205 uint32_t po_hash = pref_hash(
"_power_offset_calibration_", cs);
207 bool migrated_power_offset =
false;
208 if (has_distinct_legacy_namespace) {
209 uint32_t legacy_po_hash = pref_hash(
"_power_offset_calibration_", legacy_cs);
210 auto legacy_power_offset_pref =
213 int migration_status =
214 migrate_legacy_pref_if_needed(this->
power_offset_pref_, legacy_power_offset_pref, &power_offset_data);
215 migrated_power_offset = migration_status > 0;
216 if (migration_status > 0) {
217 ESP_LOGI(TAG,
"[CALIBRATION][%s] Migrated power offset calibrations from legacy storage.", cs);
218 }
else if (migration_status < 0) {
219 ESP_LOGW(TAG,
"[CALIBRATION][%s] Failed to migrate power offset calibrations from legacy storage.", cs);
223 if (migrated_offset || migrated_power_offset) {
230 ESP_LOGI(TAG,
"[CALIBRATION][%s] Power & Voltage/Current offset calibration is disabled. Using config file values.",
232 for (uint8_t phase = 0; phase < 3; ++phase) {
234 static_cast<uint16_t
>(this->
offset_phase_[phase].voltage_offset_));
236 static_cast<uint16_t
>(this->
offset_phase_[phase].current_offset_));
246 uint32_t g_hash = pref_hash(
"_gain_calibration_", cs);
248 bool migrated_gain =
false;
249 if (has_distinct_legacy_namespace) {
250 uint32_t legacy_g_hash = pref_hash(
"_gain_calibration_", legacy_cs);
253 int migration_status =
255 migrated_gain = migration_status > 0;
256 if (migration_status > 0) {
257 ESP_LOGI(TAG,
"[CALIBRATION][%s] Migrated gain calibrations from legacy storage.", cs);
258 }
else if (migration_status < 0) {
259 ESP_LOGW(TAG,
"[CALIBRATION][%s] Failed to migrate gain calibrations from legacy storage.", cs);
270 for (uint8_t phase = 0; phase < 3; ++phase) {
276 ESP_LOGI(TAG,
"[CALIBRATION][%s] Gain calibration is disabled. Using config file values.", cs);
277 for (uint8_t phase = 0; phase < 3; ++phase) {
289 this->
write16_(ATM90E32_REGISTER_SAGTH, sagth);
290 this->
write16_(ATM90E32_REGISTER_OVTH, ovth);
292 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x0000);
298 bool offset_mismatch =
false;
299 bool power_mismatch =
false;
300 bool gain_mismatch =
false;
302 for (uint8_t phase = 0; phase < 3; ++phase) {
308 if (offset_mismatch) {
309 ESP_LOGW(TAG,
"[CALIBRATION][%s] ", cs);
311 "[CALIBRATION][%s] ===================== Offset mismatch: using flash values =====================", cs);
312 ESP_LOGW(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------------------",
314 ESP_LOGW(TAG,
"[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
315 ESP_LOGW(TAG,
"[CALIBRATION][%s] | | config | flash | config | flash |", cs);
316 ESP_LOGW(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------------------",
318 for (uint8_t phase = 0; phase < 3; ++phase) {
319 ESP_LOGW(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d | %6d | %6d |", cs,
'A' + phase,
324 "[CALIBRATION][%s] ===============================================================================", cs);
326 if (power_mismatch) {
327 ESP_LOGW(TAG,
"[CALIBRATION][%s] ", cs);
329 "[CALIBRATION][%s] ================= Power offset mismatch: using flash values =================", cs);
330 ESP_LOGW(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------------------",
332 ESP_LOGW(TAG,
"[CALIBRATION][%s] | Phase | offset_active_power|offset_reactive_power|", cs);
333 ESP_LOGW(TAG,
"[CALIBRATION][%s] | | config | flash | config | flash |", cs);
334 ESP_LOGW(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------------------",
336 for (uint8_t phase = 0; phase < 3; ++phase) {
337 ESP_LOGW(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d | %6d | %6d |", cs,
'A' + phase,
344 "[CALIBRATION][%s] ===============================================================================", cs);
347 ESP_LOGW(TAG,
"[CALIBRATION][%s] ", cs);
349 "[CALIBRATION][%s] ====================== Gain mismatch: using flash values =====================", cs);
350 ESP_LOGW(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------------------",
352 ESP_LOGW(TAG,
"[CALIBRATION][%s] | Phase | voltage_gain | current_gain |", cs);
353 ESP_LOGW(TAG,
"[CALIBRATION][%s] | | config | flash | config | flash |", cs);
354 ESP_LOGW(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------------------",
356 for (uint8_t phase = 0; phase < 3; ++phase) {
357 ESP_LOGW(TAG,
"[CALIBRATION][%s] | %c | %6u | %6u | %6u | %6u |", cs,
'A' + phase,
362 "[CALIBRATION][%s] ===============================================================================", cs);
365 ESP_LOGI(TAG,
"[CALIBRATION][%s] Power & Voltage/Current offset calibration is disabled. Using config file values.",
368 ESP_LOGI(TAG,
"[CALIBRATION][%s] ", cs);
369 ESP_LOGI(TAG,
"[CALIBRATION][%s] ============== Restored offset calibration from memory ==============", cs);
370 ESP_LOGI(TAG,
"[CALIBRATION][%s] --------------------------------------------------------------", cs);
371 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
372 ESP_LOGI(TAG,
"[CALIBRATION][%s] --------------------------------------------------------------", cs);
373 for (uint8_t phase = 0; phase < 3; phase++) {
374 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase,
377 ESP_LOGI(TAG,
"[CALIBRATION][%s] ==============================================================\\n", cs);
381 ESP_LOGI(TAG,
"[CALIBRATION][%s] ", cs);
382 ESP_LOGI(TAG,
"[CALIBRATION][%s] ============ Restored power offset calibration from memory ============", cs);
383 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
384 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
385 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
386 for (uint8_t phase = 0; phase < 3; phase++) {
387 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase,
391 ESP_LOGI(TAG,
"[CALIBRATION][%s] =====================================================================\n", cs);
394 ESP_LOGI(TAG,
"[CALIBRATION][%s] Gain calibration is disabled. Using config file values.", cs);
396 ESP_LOGI(TAG,
"[CALIBRATION][%s] ", cs);
397 ESP_LOGI(TAG,
"[CALIBRATION][%s] ============ Restoring saved gain calibrations to registers ============", cs);
398 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
399 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | voltage_gain | current_gain |", cs);
400 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
401 for (uint8_t phase = 0; phase < 3; phase++) {
402 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6u | %6u |", cs,
'A' + phase,
405 ESP_LOGI(TAG,
"[CALIBRATION][%s] =====================================================================\\n", cs);
406 ESP_LOGI(TAG,
"[CALIBRATION][%s] Gain calibration loaded and verified successfully.\n", cs);
412 ESP_LOGCONFIG(
"",
"ATM90E32:");
413 LOG_PIN(
" CS Pin: ", this->
cs_);
415 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
417 LOG_UPDATE_INTERVAL(
this);
418 LOG_SENSOR(
" ",
"Voltage A", this->
phase_[
PHASEA].voltage_sensor_);
419 LOG_SENSOR(
" ",
"Current A", this->
phase_[
PHASEA].current_sensor_);
420 LOG_SENSOR(
" ",
"Power A", this->
phase_[
PHASEA].power_sensor_);
421 LOG_SENSOR(
" ",
"Reactive Power A", this->
phase_[
PHASEA].reactive_power_sensor_);
422 LOG_SENSOR(
" ",
"Apparent Power A", this->
phase_[
PHASEA].apparent_power_sensor_);
423 LOG_SENSOR(
" ",
"PF A", this->
phase_[
PHASEA].power_factor_sensor_);
424 LOG_SENSOR(
" ",
"Active Forward Energy A", this->
phase_[
PHASEA].forward_active_energy_sensor_);
425 LOG_SENSOR(
" ",
"Active Reverse Energy A", this->
phase_[
PHASEA].reverse_active_energy_sensor_);
426 LOG_SENSOR(
" ",
"Harmonic Power A", this->
phase_[
PHASEA].harmonic_active_power_sensor_);
427 LOG_SENSOR(
" ",
"Phase Angle A", this->
phase_[
PHASEA].phase_angle_sensor_);
428 LOG_SENSOR(
" ",
"Peak Current A", this->
phase_[
PHASEA].peak_current_sensor_);
429 LOG_SENSOR(
" ",
"Voltage B", this->
phase_[
PHASEB].voltage_sensor_);
430 LOG_SENSOR(
" ",
"Current B", this->
phase_[
PHASEB].current_sensor_);
431 LOG_SENSOR(
" ",
"Power B", this->
phase_[
PHASEB].power_sensor_);
432 LOG_SENSOR(
" ",
"Reactive Power B", this->
phase_[
PHASEB].reactive_power_sensor_);
433 LOG_SENSOR(
" ",
"Apparent Power B", this->
phase_[
PHASEB].apparent_power_sensor_);
434 LOG_SENSOR(
" ",
"PF B", this->
phase_[
PHASEB].power_factor_sensor_);
435 LOG_SENSOR(
" ",
"Active Forward Energy B", this->
phase_[
PHASEB].forward_active_energy_sensor_);
436 LOG_SENSOR(
" ",
"Active Reverse Energy B", this->
phase_[
PHASEB].reverse_active_energy_sensor_);
437 LOG_SENSOR(
" ",
"Harmonic Power B", this->
phase_[
PHASEB].harmonic_active_power_sensor_);
438 LOG_SENSOR(
" ",
"Phase Angle B", this->
phase_[
PHASEB].phase_angle_sensor_);
439 LOG_SENSOR(
" ",
"Peak Current B", this->
phase_[
PHASEB].peak_current_sensor_);
440 LOG_SENSOR(
" ",
"Voltage C", this->
phase_[
PHASEC].voltage_sensor_);
441 LOG_SENSOR(
" ",
"Current C", this->
phase_[
PHASEC].current_sensor_);
442 LOG_SENSOR(
" ",
"Power C", this->
phase_[
PHASEC].power_sensor_);
443 LOG_SENSOR(
" ",
"Reactive Power C", this->
phase_[
PHASEC].reactive_power_sensor_);
444 LOG_SENSOR(
" ",
"Apparent Power C", this->
phase_[
PHASEC].apparent_power_sensor_);
445 LOG_SENSOR(
" ",
"PF C", this->
phase_[
PHASEC].power_factor_sensor_);
446 LOG_SENSOR(
" ",
"Active Forward Energy C", this->
phase_[
PHASEC].forward_active_energy_sensor_);
447 LOG_SENSOR(
" ",
"Active Reverse Energy C", this->
phase_[
PHASEC].reverse_active_energy_sensor_);
448 LOG_SENSOR(
" ",
"Harmonic Power C", this->
phase_[
PHASEC].harmonic_active_power_sensor_);
449 LOG_SENSOR(
" ",
"Phase Angle C", this->
phase_[
PHASEC].phase_angle_sensor_);
450 LOG_SENSOR(
" ",
"Peak Current C", this->
phase_[
PHASEC].peak_current_sensor_);
467 uint8_t addrh = (1 << 7) | ((a_register >> 8) & 0x03);
468 uint8_t addrl = (a_register & 0xFF);
469 uint8_t data[4] = {addrh, addrl, 0x00, 0x00};
472 ESP_LOGVV(TAG,
"read16_ 0x%04" PRIX16
" output 0x%04" PRIX16, a_register, output);
480 const uint16_t val_h = this->
read16_(addr_h);
481 const uint16_t val_l = this->
read16_(addr_l);
482 const int32_t
val = (val_h << 16) | val_l;
485 "read32_ addr_h 0x%04" PRIX16
" val_h 0x%04" PRIX16
" addr_l 0x%04" PRIX16
" val_l 0x%04" PRIX16
487 addr_h, val_h, addr_l, val_l,
val);
493 ESP_LOGVV(TAG,
"write16_ 0x%04" PRIX16
" val 0x%04" PRIX16, a_register,
val);
494 uint8_t addrh = ((a_register >> 8) & 0x03);
495 uint8_t addrl = (a_register & 0xFF);
496 uint8_t data[4] = {addrh, addrl, uint8_t((
val >> 8) & 0xFF), uint8_t(
val & 0xFF)};
536 const uint16_t voltage = this->
read16_(ATM90E32_REGISTER_URMS + phase);
538 return (
float) voltage / 100;
542 const uint8_t reads = 10;
544 uint16_t voltage = 0;
545 for (uint8_t i = 0; i < reads; i++) {
546 voltage = this->
read16_(ATM90E32_REGISTER_URMS + phase);
548 accumulation += voltage;
550 voltage = accumulation / reads;
556 const uint8_t reads = 10;
558 uint16_t current = 0;
559 for (uint8_t i = 0; i < reads; i++) {
560 current = this->
read16_(ATM90E32_REGISTER_IRMS + phase);
562 accumulation += current;
564 current = accumulation / reads;
570 const uint16_t current = this->
read16_(ATM90E32_REGISTER_IRMS + phase);
572 return (
float) current / 1000;
576 const int val = this->
read32_(ATM90E32_REGISTER_PMEAN + phase, ATM90E32_REGISTER_PMEANLSB + phase);
577 return val * 0.00032f;
581 const int val = this->
read32_(ATM90E32_REGISTER_QMEAN + phase, ATM90E32_REGISTER_QMEANLSB + phase);
582 return val * 0.00032f;
586 const int val = this->
read32_(ATM90E32_REGISTER_SMEAN + phase, ATM90E32_REGISTER_SMEANLSB + phase);
587 return val * 0.00032f;
591 uint16_t powerfactor = this->
read16_(ATM90E32_REGISTER_PFMEAN + phase);
593 return (
float) ((int16_t) powerfactor) / 1000;
597 const uint16_t
val = this->
read16_(ATM90E32_REGISTER_APENERGY + phase);
598 if ((UINT32_MAX - this->
phase_[phase].cumulative_forward_active_energy_) >
val) {
604 return ((
float) this->
phase_[phase].cumulative_forward_active_energy_ * (10.0f / 3200.0f));
608 const uint16_t
val = this->
read16_(ATM90E32_REGISTER_ANENERGY + phase);
609 if (UINT32_MAX - this->
phase_[phase].cumulative_reverse_active_energy_ >
val) {
615 return ((
float) this->
phase_[phase].cumulative_reverse_active_energy_ * (10.0f / 3200.0f));
619 int val = this->
read32_(ATM90E32_REGISTER_PMEANH + phase, ATM90E32_REGISTER_PMEANHLSB + phase);
620 return val * 0.00032f;
624 float val = this->
read16_(ATM90E32_REGISTER_PANGLE + phase) / 10.0f;
625 return (
val > 180.0f) ?
val - 360.0f :
val;
629 int16_t
val = (float) this->
read16_(ATM90E32_REGISTER_IPEAK + phase);
633 return (
val * this->
phase_[phase].ct_gain_ / 8192000.0);
637 const uint16_t freq = this->
read16_(ATM90E32_REGISTER_FREQ);
638 return (
float) freq / 100;
642 const uint16_t ctemp = this->
read16_(ATM90E32_REGISTER_TEMP);
643 return (
float) ctemp;
649 ESP_LOGW(TAG,
"[CALIBRATION][%s] Gain calibration is disabled! Enable it first with enable_gain_calibration: true",
654 float ref_voltages[3] = {
662 ESP_LOGI(TAG,
"[CALIBRATION][%s] ", cs);
663 ESP_LOGI(TAG,
"[CALIBRATION][%s] ========================= Gain Calibration =========================", cs);
664 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
667 "[CALIBRATION][%s] | Phase | V_meas (V) | I_meas (A) | V_ref | I_ref | V_gain (old→new) | I_gain (old→new) |",
669 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
671 for (uint8_t phase = 0; phase < 3; phase++) {
675 float ref_voltage = ref_voltages[phase];
676 float ref_current = ref_currents[phase];
681 bool did_voltage =
false;
682 bool did_current =
false;
685 if (ref_voltage <= 0.0f) {
686 ESP_LOGW(TAG,
"[CALIBRATION][%s] Phase %s - Skipping voltage calibration: reference voltage is 0.", cs,
688 }
else if (measured_voltage == 0.0f) {
689 ESP_LOGW(TAG,
"[CALIBRATION][%s] Phase %s - Skipping voltage calibration: measured voltage is 0.", cs,
692 uint32_t new_voltage_gain =
static_cast<uint32_t>((ref_voltage / measured_voltage) * current_voltage_gain);
693 if (new_voltage_gain == 0) {
694 ESP_LOGW(TAG,
"[CALIBRATION][%s] Phase %s - Voltage gain would be 0. Check reference and measured voltage.", cs,
697 if (new_voltage_gain >= 65535) {
699 "[CALIBRATION][%s] Phase %s - Voltage gain exceeds 65535. You may need a higher output voltage "
702 new_voltage_gain = 65535;
710 if (ref_current == 0.0f) {
711 ESP_LOGW(TAG,
"[CALIBRATION][%s] Phase %s - Skipping current calibration: reference current is 0.", cs,
713 }
else if (measured_current == 0.0f) {
714 ESP_LOGW(TAG,
"[CALIBRATION][%s] Phase %s - Skipping current calibration: measured current is 0.", cs,
717 uint32_t new_current_gain =
static_cast<uint32_t>((ref_current / measured_current) * current_current_gain);
718 if (new_current_gain == 0) {
719 ESP_LOGW(TAG,
"[CALIBRATION][%s] Phase %s - Current gain would be 0. Check reference and measured current.", cs,
722 if (new_current_gain >= 65535) {
723 ESP_LOGW(TAG,
"[CALIBRATION][%s] Phase %s - Current gain exceeds 65535. You may need to turn up pga gain.",
725 new_current_gain = 65535;
733 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %9.2f | %9.4f | %5.2f | %6.4f | %5u → %-5u | %5u → %-5u |", cs,
734 'A' + phase, measured_voltage, measured_current, ref_voltage, ref_current, current_voltage_gain,
735 did_voltage ? this->
gain_phase_[phase].voltage_gain : current_voltage_gain, current_current_gain,
736 did_current ? this->
gain_phase_[phase].current_gain : current_current_gain);
739 ESP_LOGI(TAG,
"[CALIBRATION][%s] =====================================================================\n", cs);
752 ESP_LOGI(TAG,
"[CALIBRATION][%s] Gain calibration saved to memory.", cs);
755 ESP_LOGE(TAG,
"[CALIBRATION][%s] Failed to save gain calibration to memory!", cs);
768 ESP_LOGI(TAG,
"[CALIBRATION][%s] Offset calibration saved to memory.", cs);
771 ESP_LOGE(TAG,
"[CALIBRATION][%s] Failed to save offset calibration to memory!", cs);
784 ESP_LOGI(TAG,
"[CALIBRATION][%s] Power offset calibration saved to memory.", cs);
787 ESP_LOGE(TAG,
"[CALIBRATION][%s] Failed to save power offset calibration to memory!", cs);
795 "[CALIBRATION][%s] Offset calibration is disabled! Enable it first with enable_offset_calibration: true",
800 ESP_LOGI(TAG,
"[CALIBRATION][%s] ", cs);
801 ESP_LOGI(TAG,
"[CALIBRATION][%s] ======================== Offset Calibration ========================", cs);
802 ESP_LOGI(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------", cs);
803 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
804 ESP_LOGI(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------", cs);
806 for (uint8_t phase = 0; phase < 3; phase++) {
812 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase, voltage_offset,
816 ESP_LOGI(TAG,
"[CALIBRATION][%s] ==================================================================\n", cs);
826 "[CALIBRATION][%s] Offset power calibration is disabled! Enable it first with enable_offset_calibration: true",
831 ESP_LOGI(TAG,
"[CALIBRATION][%s] ", cs);
832 ESP_LOGI(TAG,
"[CALIBRATION][%s] ===================== Power Offset Calibration =====================", cs);
833 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
834 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
835 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
837 for (uint8_t phase = 0; phase < 3; ++phase) {
843 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase, active_offset,
846 ESP_LOGI(TAG,
"[CALIBRATION][%s] =====================================================================\n", cs);
852 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA);
854 for (
int phase = 0; phase < 3; phase++) {
859 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x0000);
872 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA);
875 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x0000);
888 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA);
891 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x0000);
896 for (uint8_t i = 0; i < 3; ++i) {
905 bool all_zero =
true;
906 bool same_as_config =
true;
907 for (uint8_t phase = 0; phase < 3; ++phase) {
910 if (saved.voltage_gain != 0 || saved.current_gain != 0)
912 if (saved.voltage_gain != cfg.voltage_gain || saved.current_gain != cfg.current_gain)
913 same_as_config =
false;
916 if (!all_zero && !same_as_config) {
917 for (uint8_t phase = 0; phase < 3; ++phase) {
918 bool mismatch =
false;
938 ESP_LOGE(TAG,
"[CALIBRATION][%s] Gain verification failed! Calibration may not be applied correctly.", cs);
943 for (uint8_t i = 0; i < 3; ++i)
947 ESP_LOGW(TAG,
"[CALIBRATION][%s] No stored gain calibrations found. Using config file values.", cs);
952 for (uint8_t i = 0; i < 3; ++i)
957 bool all_zero =
true;
960 if (phase.voltage_offset_ != 0 || phase.current_offset_ != 0) {
967 if (have_data && !all_zero) {
969 for (uint8_t phase = 0; phase < 3; phase++) {
971 bool mismatch =
false;
973 offset.voltage_offset_ != this->config_offset_phase_[phase].voltage_offset_)
976 offset.current_offset_ != this->config_offset_phase_[phase].current_offset_)
982 for (uint8_t phase = 0; phase < 3; phase++)
984 ESP_LOGW(TAG,
"[CALIBRATION][%s] No stored offset calibrations found. Using default values.", cs);
987 for (uint8_t phase = 0; phase < 3; phase++) {
995 for (uint8_t i = 0; i < 3; ++i)
1000 bool all_zero =
true;
1003 if (phase.active_power_offset != 0 || phase.reactive_power_offset != 0) {
1010 if (have_data && !all_zero) {
1012 for (uint8_t phase = 0; phase < 3; ++phase) {
1014 bool mismatch =
false;
1016 offset.active_power_offset != this->config_power_offset_phase_[phase].active_power_offset)
1019 offset.reactive_power_offset != this->config_power_offset_phase_[phase].reactive_power_offset)
1025 for (uint8_t phase = 0; phase < 3; ++phase)
1027 ESP_LOGW(TAG,
"[CALIBRATION][%s] No stored power offsets found. Using default values.", cs);
1030 for (uint8_t phase = 0; phase < 3; ++phase) {
1039 ESP_LOGI(TAG,
"[CALIBRATION][%s] No stored gain calibrations to clear. Current values:", cs);
1040 ESP_LOGI(TAG,
"[CALIBRATION][%s] ----------------------------------------------------------", cs);
1041 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | voltage_gain | current_gain |", cs);
1042 ESP_LOGI(TAG,
"[CALIBRATION][%s] ----------------------------------------------------------", cs);
1043 for (
int phase = 0; phase < 3; phase++) {
1044 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6u | %6u |", cs,
'A' + phase,
1047 ESP_LOGI(TAG,
"[CALIBRATION][%s] ==========================================================\n", cs);
1051 ESP_LOGI(TAG,
"[CALIBRATION][%s] Clearing stored gain calibrations and restoring config-defined values", cs);
1052 ESP_LOGI(TAG,
"[CALIBRATION][%s] ----------------------------------------------------------", cs);
1053 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | voltage_gain | current_gain |", cs);
1054 ESP_LOGI(TAG,
"[CALIBRATION][%s] ----------------------------------------------------------", cs);
1056 for (
int phase = 0; phase < 3; phase++) {
1065 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6u | %6u |", cs,
'A' + phase, voltage_gain, current_gain);
1067 ESP_LOGI(TAG,
"[CALIBRATION][%s] ==========================================================\n", cs);
1079 ESP_LOGE(TAG,
"[CALIBRATION][%s] Failed to clear gain calibrations!", cs);
1088 ESP_LOGI(TAG,
"[CALIBRATION][%s] No stored offset calibrations to clear. Current values:", cs);
1089 ESP_LOGI(TAG,
"[CALIBRATION][%s] --------------------------------------------------------------", cs);
1090 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
1091 ESP_LOGI(TAG,
"[CALIBRATION][%s] --------------------------------------------------------------", cs);
1092 for (uint8_t phase = 0; phase < 3; phase++) {
1093 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase,
1096 ESP_LOGI(TAG,
"[CALIBRATION][%s] ==============================================================\n", cs);
1100 ESP_LOGI(TAG,
"[CALIBRATION][%s] Clearing stored offset calibrations and restoring config-defined values", cs);
1101 ESP_LOGI(TAG,
"[CALIBRATION][%s] --------------------------------------------------------------", cs);
1102 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
1103 ESP_LOGI(TAG,
"[CALIBRATION][%s] --------------------------------------------------------------", cs);
1105 for (uint8_t phase = 0; phase < 3; phase++) {
1106 int16_t voltage_offset =
1108 int16_t current_offset =
1111 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase, voltage_offset,
1114 ESP_LOGI(TAG,
"[CALIBRATION][%s] ==============================================================\n", cs);
1124 ESP_LOGI(TAG,
"[CALIBRATION][%s] Offsets cleared.", cs);
1130 ESP_LOGI(TAG,
"[CALIBRATION][%s] No stored power offsets to clear. Current values:", cs);
1131 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
1132 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
1133 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
1134 for (uint8_t phase = 0; phase < 3; phase++) {
1135 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase,
1139 ESP_LOGI(TAG,
"[CALIBRATION][%s] =====================================================================\n", cs);
1143 ESP_LOGI(TAG,
"[CALIBRATION][%s] Clearing stored power offsets and restoring config-defined values", cs);
1144 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
1145 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
1146 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
1148 for (uint8_t phase = 0; phase < 3; phase++) {
1149 int16_t active_offset =
1155 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase, active_offset,
1158 ESP_LOGI(TAG,
"[CALIBRATION][%s] =====================================================================\n", cs);
1168 ESP_LOGI(TAG,
"[CALIBRATION][%s] Power offsets cleared.", cs);
1172 const uint8_t num_reads = 5;
1173 uint64_t total_value = 0;
1175 for (uint8_t i = 0; i < num_reads; ++i) {
1176 uint32_t reading = voltage ? this->
read32_(ATM90E32_REGISTER_URMS + phase, ATM90E32_REGISTER_URMSLSB + phase)
1177 : this->
read32_(ATM90E32_REGISTER_IRMS + phase, ATM90E32_REGISTER_IRMSLSB + phase);
1178 total_value += reading;
1181 const uint32_t average_value = total_value / num_reads;
1182 const uint32_t shifted = average_value >> 7;
1183 const uint32_t offset = ~shifted + 1;
1184 return static_cast<int16_t
>(offset);
1188 const uint8_t num_reads = 5;
1189 int64_t total_value = 0;
1191 for (uint8_t i = 0; i < num_reads; ++i) {
1192 int32_t reading = reactive ? this->
read32_(ATM90E32_REGISTER_QMEAN + phase, ATM90E32_REGISTER_QMEANLSB + phase)
1193 : this->
read32_(ATM90E32_REGISTER_PMEAN + phase, ATM90E32_REGISTER_PMEANLSB + phase);
1194 total_value += reading;
1197 int32_t average_value = total_value / num_reads;
1198 int32_t power_offset = -average_value;
1199 return static_cast<int16_t
>(power_offset);
1204 bool success =
true;
1205 for (uint8_t phase = 0; phase < 3; phase++) {
1209 if (read_voltage != this->
gain_phase_[phase].voltage_gain ||
1210 read_current != this->
gain_phase_[phase].current_gain) {
1211 ESP_LOGE(TAG,
"[CALIBRATION][%s] Mismatch detected for Phase %s!", cs,
phase_labels[phase]);
1218#ifdef USE_TEXT_SENSOR
1220 uint16_t state0 = this->
read16_(ATM90E32_REGISTER_EMMSTATE0);
1221 uint16_t state1 = this->
read16_(ATM90E32_REGISTER_EMMSTATE1);
1223 for (
int phase = 0; phase < 3; phase++) {
1227 status +=
"Over Voltage; ";
1229 status +=
"Voltage Sag; ";
1231 status +=
"Phase Loss; ";
1234 if (sensor ==
nullptr)
1240 ESP_LOGW(TAG,
"%s: %s", sensor->get_name().c_str(),
status.c_str());
1241 sensor->publish_state(
status);
1243 sensor->publish_state(
"Okay");
1249 uint16_t state1 = this->
read16_(ATM90E32_REGISTER_EMMSTATE1);
1251 std::string freq_status;
1253 if (state1 & ATM90E32_STATUS_S1_FREQHIST) {
1254 freq_status =
"HIGH";
1255 }
else if (state1 & ATM90E32_STATUS_S1_FREQLOST) {
1256 freq_status =
"LOW";
1258 freq_status =
"Normal";
1261 if (freq_status ==
"Normal") {
1262 ESP_LOGD(TAG,
"Frequency status: %s", freq_status.c_str());
1264 ESP_LOGW(TAG,
"Frequency status: %s", freq_status.c_str());
1271 constexpr float max_current_threshold = 65.53f;
1273 for (uint8_t phase = 0; phase < 3; phase++) {
1277 if (current_val > max_current_threshold) {
1278 ESP_LOGW(TAG,
"Over current detected on Phase %c: %.2f A",
'A' + phase, current_val);
1279 ESP_LOGW(TAG,
"You may need to half your gain_ct: value & multiply the current and power values by 2");
1291 float nominal_voltage = (line_freq == 60) ? 120.0f : 220.0f;
1292 float target_voltage = nominal_voltage * multiplier;
1294 float peak_01v = target_voltage * 100.0f * std::numbers::sqrt2_v<float>;
1295 float divider = (2.0f * ugain) / 32768.0f;
1297 float threshold = peak_01v / divider;
1299 return static_cast<uint16_t
>(threshold);
1303 uint16_t last = this->
read16_(ATM90E32_REGISTER_LASTSPIDATA);
1304 if (last != expected) {
1305 if (context !=
nullptr) {
1306 ESP_LOGW(TAG,
"[%s] SPI read mismatch: expected 0x%04X, got 0x%04X", context, expected, last);
1308 ESP_LOGW(TAG,
"SPI read mismatch: expected 0x%04X, got 0x%04X", expected, last);
void mark_failed()
Mark this component as failed.
void status_set_warning()
void status_clear_warning()
virtual size_t dump_summary(char *buffer, size_t len) const
Write a summary of this pin to the provided buffer.
bool enable_offset_calibration_
float get_local_phase_reactive_power_(uint8_t phase)
float get_phase_forward_active_energy_(uint8_t phase)
bool restored_gain_calibration_
bool has_config_voltage_gain_[3]
sensor::Sensor * freq_sensor_
void run_gain_calibrations()
bool gain_calibration_mismatch_[3]
bool has_config_active_power_offset_[3]
float get_phase_current_avg_(uint8_t phase)
float get_local_phase_apparent_power_(uint8_t phase)
void write16_(uint16_t a_register, uint16_t val, bool validate=true)
bool has_config_reactive_power_offset_[3]
void save_offset_calibration_to_memory_()
text_sensor::TextSensor * freq_status_text_sensor_
ESPPreferenceObject power_offset_pref_
const uint16_t voltage_gain_registers[3]
float get_phase_voltage_avg_(uint8_t phase)
void write_power_offsets_to_registers_(uint8_t phase, int16_t p_offset, int16_t q_offset)
const uint16_t current_gain_registers[3]
void run_offset_calibrations()
float get_reference_voltage(uint8_t phase)
struct esphome::atm90e32::ATM90E32Component::GainCalibration gain_phase_[3]
bool get_publish_interval_flag_()
const uint16_t current_offset_registers[3]
static const uint8_t PHASEB
float get_phase_reverse_active_energy_(uint8_t phase)
float get_local_phase_harmonic_active_power_(uint8_t phase)
void check_phase_status()
float get_phase_angle_(uint8_t phase)
float get_local_phase_current_(uint8_t phase)
bool validate_spi_read_(uint16_t expected, const char *context=nullptr)
void restore_offset_calibrations_()
const uint16_t reactive_power_offset_registers[3]
const uint16_t over_voltage_flags[3]
void clear_power_offset_calibrations()
float get_phase_voltage_(uint8_t phase)
float get_chip_temperature_()
GainCalibration config_gain_phase_[3]
bool restored_power_offset_calibration_
int16_t calibrate_offset(uint8_t phase, bool voltage)
float get_local_phase_reverse_active_energy_(uint8_t phase)
void dump_config() override
void restore_gain_calibrations_()
float get_local_phase_forward_active_energy_(uint8_t phase)
OffsetCalibration config_offset_phase_[3]
bool peak_current_signed_
struct esphome::atm90e32::ATM90E32Component::OffsetCalibration offset_phase_[3]
void restore_power_offset_calibrations_()
uint16_t calculate_voltage_threshold(int line_freq, uint16_t ugain, float multiplier)
float get_local_phase_power_factor_(uint8_t phase)
bool calibration_message_printed_
float get_phase_reactive_power_(uint8_t phase)
void clear_offset_calibrations()
bool has_config_current_offset_[3]
const uint16_t phase_loss_flags[3]
void log_calibration_status_()
float get_phase_apparent_power_(uint8_t phase)
void write_gains_to_registers_()
float get_local_phase_voltage_(uint8_t phase)
void check_over_current()
ESPPreferenceObject gain_calibration_pref_
void write_offsets_to_registers_(uint8_t phase, int16_t voltage_offset, int16_t current_offset)
static const uint8_t PHASEA
struct esphome::atm90e32::ATM90E32Component::PowerOffsetCalibration power_offset_phase_[3]
float get_reference_current(uint8_t phase)
bool verify_gain_writes_()
bool using_saved_calibrations_
float get_phase_peak_current_(uint8_t phase)
void clear_gain_calibrations()
float get_phase_harmonic_active_power_(uint8_t phase)
bool has_config_current_gain_[3]
const uint16_t voltage_sag_flags[3]
bool restored_offset_calibration_
float get_phase_active_power_(uint8_t phase)
PowerOffsetCalibration config_power_offset_phase_[3]
void run_power_offset_calibrations()
const char * get_calibration_id_()
bool enable_gain_calibration_
void get_cs_summary_(std::span< char, GPIO_SUMMARY_MAX_LEN > buffer)
static const uint8_t PHASEC
bool power_offset_calibration_mismatch_[3]
const uint16_t power_offset_registers[3]
float get_setup_priority() const override
uint16_t read16_(uint16_t a_register)
const char * instance_id_
int read32_(uint16_t addr_h, uint16_t addr_l)
sensor::Sensor * chip_temperature_sensor_
float get_phase_power_factor_(uint8_t phase)
float get_local_phase_angle_(uint8_t phase)
const uint16_t voltage_offset_registers[3]
void save_gain_calibration_to_memory_()
bool has_config_voltage_offset_[3]
bool offset_calibration_mismatch_[3]
void save_power_offset_calibration_to_memory_()
text_sensor::TextSensor * phase_status_text_sensor_[3]
float get_phase_current_(uint8_t phase)
float get_local_phase_peak_current_(uint8_t phase)
void set_publish_interval_flag_(bool flag)
int16_t calibrate_power_offset(uint8_t phase, bool reactive)
struct esphome::atm90e32::ATM90E32Component::ATM90E32Phase phase_[3]
const char * phase_labels[3]
ESPPreferenceObject offset_pref_
float get_local_phase_active_power_(uint8_t phase)
void publish_state(float state)
Publish a new state to the front-end.
float state
This member variable stores the last state that has passed through all filters.
void spi_setup() override
void transfer_array(uint8_t *data, size_t length)
void write_array(const uint8_t *data, size_t length)
void publish_state(const std::string &state)
constexpr float IO
For components that represent GPIO pins like PCF8573.
constexpr uint32_t fnv1_hash_extend(uint32_t hash, T value)
Extend a FNV-1 hash with an integer (hashes each byte).
ESPPreferences * global_preferences
uint32_t fnv1_hash(const char *str)
Calculate a FNV-1 hash of str.
void delay_microseconds_safe(uint32_t us)
Delay for the given amount of microseconds, possibly yielding to other processes during the wait.
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
constexpr size_t GPIO_SUMMARY_MAX_LEN
Maximum buffer size for dump_summary output.
void HOT delay(uint32_t ms)
ESPPreferenceObject make_preference(size_t, uint32_t, bool)
bool sync()
Commit pending writes to flash.
uint32_t cumulative_reverse_active_energy_
float reverse_active_energy_
int16_t active_power_offset_
sensor::Sensor * reactive_power_sensor_
sensor::Sensor * peak_current_sensor_
float forward_active_energy_
sensor::Sensor * power_factor_sensor_
sensor::Sensor * phase_angle_sensor_
sensor::Sensor * apparent_power_sensor_
sensor::Sensor * power_sensor_
sensor::Sensor * current_sensor_
sensor::Sensor * forward_active_energy_sensor_
sensor::Sensor * harmonic_active_power_sensor_
int16_t reactive_power_offset_
sensor::Sensor * reverse_active_energy_sensor_
float harmonic_active_power_
sensor::Sensor * voltage_sensor_
uint32_t cumulative_forward_active_energy_
int16_t reactive_power_offset
int16_t active_power_offset