10static const char *
const TAG =
"atm90e32";
14 for (uint8_t phase = 0; phase < 3; phase++) {
15 if (this->
phase_[phase].voltage_sensor_ !=
nullptr)
18 if (this->
phase_[phase].current_sensor_ !=
nullptr)
21 if (this->
phase_[phase].power_sensor_ !=
nullptr)
24 if (this->
phase_[phase].power_factor_sensor_ !=
nullptr)
27 if (this->
phase_[phase].reactive_power_sensor_ !=
nullptr)
30 if (this->
phase_[phase].apparent_power_sensor_ !=
nullptr)
33 if (this->
phase_[phase].forward_active_energy_sensor_ !=
nullptr)
36 if (this->
phase_[phase].reverse_active_energy_sensor_ !=
nullptr)
39 if (this->
phase_[phase].phase_angle_sensor_ !=
nullptr)
42 if (this->
phase_[phase].harmonic_active_power_sensor_ !=
nullptr)
45 if (this->
phase_[phase].peak_current_sensor_ !=
nullptr)
49 if (this->
phase_[phase].voltage_sensor_ !=
nullptr)
52 if (this->
phase_[phase].current_sensor_ !=
nullptr)
55 if (this->
phase_[phase].power_sensor_ !=
nullptr)
58 if (this->
phase_[phase].power_factor_sensor_ !=
nullptr)
61 if (this->
phase_[phase].reactive_power_sensor_ !=
nullptr)
64 if (this->
phase_[phase].apparent_power_sensor_ !=
nullptr)
67 if (this->
phase_[phase].forward_active_energy_sensor_ !=
nullptr) {
72 if (this->
phase_[phase].reverse_active_energy_sensor_ !=
nullptr) {
77 if (this->
phase_[phase].phase_angle_sensor_ !=
nullptr)
80 if (this->
phase_[phase].harmonic_active_power_sensor_ !=
nullptr) {
85 if (this->
phase_[phase].peak_current_sensor_ !=
nullptr)
97 if (this->
read16_(ATM90E32_REGISTER_METEREN) != 1) {
104#ifdef USE_TEXT_SENSOR
116 uint16_t mmode0 = 0x87;
117 uint16_t high_thresh = 0;
118 uint16_t low_thresh = 0;
135 this->
write16_(ATM90E32_REGISTER_SOFTRESET, 0x789A,
false);
137 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA);
139 ESP_LOGW(TAG,
"Could not initialize ATM90E32 IC, check SPI settings");
144 this->
write16_(ATM90E32_REGISTER_METEREN, 0x0001);
145 this->
write16_(ATM90E32_REGISTER_SAGPEAKDETCFG, 0xFF3F);
146 this->
write16_(ATM90E32_REGISTER_PLCONSTH, 0x0861);
147 this->
write16_(ATM90E32_REGISTER_PLCONSTL, 0xC468);
148 this->
write16_(ATM90E32_REGISTER_ZXCONFIG, 0xD654);
149 this->
write16_(ATM90E32_REGISTER_MMODE0, mmode0);
151 this->
write16_(ATM90E32_REGISTER_FREQHITH, high_thresh);
152 this->
write16_(ATM90E32_REGISTER_FREQLOTH, low_thresh);
153 this->
write16_(ATM90E32_REGISTER_PSTARTTH, 0x1D4C);
154 this->
write16_(ATM90E32_REGISTER_QSTARTTH, 0x1D4C);
155 this->
write16_(ATM90E32_REGISTER_SSTARTTH, 0x1D4C);
156 this->
write16_(ATM90E32_REGISTER_PPHASETH, 0x02EE);
157 this->
write16_(ATM90E32_REGISTER_QPHASETH, 0x02EE);
170 ESP_LOGI(TAG,
"[CALIBRATION][%s] Power & Voltage/Current offset calibration is disabled. Using config file values.",
172 for (uint8_t phase = 0; phase < 3; ++phase) {
174 static_cast<uint16_t
>(this->
offset_phase_[phase].voltage_offset_));
176 static_cast<uint16_t
>(this->
offset_phase_[phase].current_offset_));
191 for (uint8_t phase = 0; phase < 3; ++phase) {
197 ESP_LOGI(TAG,
"[CALIBRATION][%s] Gain calibration is disabled. Using config file values.", cs);
198 for (uint8_t phase = 0; phase < 3; ++phase) {
210 this->
write16_(ATM90E32_REGISTER_SAGTH, sagth);
211 this->
write16_(ATM90E32_REGISTER_OVTH, ovth);
213 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x0000);
219 bool offset_mismatch =
false;
220 bool power_mismatch =
false;
221 bool gain_mismatch =
false;
223 for (uint8_t phase = 0; phase < 3; ++phase) {
229 if (offset_mismatch) {
230 ESP_LOGW(TAG,
"[CALIBRATION][%s] ", cs);
232 "[CALIBRATION][%s] ===================== Offset mismatch: using flash values =====================", cs);
233 ESP_LOGW(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------------------",
235 ESP_LOGW(TAG,
"[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
236 ESP_LOGW(TAG,
"[CALIBRATION][%s] | | config | flash | config | flash |", cs);
237 ESP_LOGW(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------------------",
239 for (uint8_t phase = 0; phase < 3; ++phase) {
240 ESP_LOGW(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d | %6d | %6d |", cs,
'A' + phase,
245 "[CALIBRATION][%s] ===============================================================================", cs);
247 if (power_mismatch) {
248 ESP_LOGW(TAG,
"[CALIBRATION][%s] ", cs);
250 "[CALIBRATION][%s] ================= Power offset mismatch: using flash values =================", cs);
251 ESP_LOGW(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------------------",
253 ESP_LOGW(TAG,
"[CALIBRATION][%s] | Phase | offset_active_power|offset_reactive_power|", cs);
254 ESP_LOGW(TAG,
"[CALIBRATION][%s] | | config | flash | config | flash |", cs);
255 ESP_LOGW(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------------------",
257 for (uint8_t phase = 0; phase < 3; ++phase) {
258 ESP_LOGW(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d | %6d | %6d |", cs,
'A' + phase,
265 "[CALIBRATION][%s] ===============================================================================", cs);
268 ESP_LOGW(TAG,
"[CALIBRATION][%s] ", cs);
270 "[CALIBRATION][%s] ====================== Gain mismatch: using flash values =====================", cs);
271 ESP_LOGW(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------------------",
273 ESP_LOGW(TAG,
"[CALIBRATION][%s] | Phase | voltage_gain | current_gain |", cs);
274 ESP_LOGW(TAG,
"[CALIBRATION][%s] | | config | flash | config | flash |", cs);
275 ESP_LOGW(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------------------",
277 for (uint8_t phase = 0; phase < 3; ++phase) {
278 ESP_LOGW(TAG,
"[CALIBRATION][%s] | %c | %6u | %6u | %6u | %6u |", cs,
'A' + phase,
283 "[CALIBRATION][%s] ===============================================================================", cs);
286 ESP_LOGI(TAG,
"[CALIBRATION][%s] Power & Voltage/Current offset calibration is disabled. Using config file values.",
289 ESP_LOGI(TAG,
"[CALIBRATION][%s] ", cs);
290 ESP_LOGI(TAG,
"[CALIBRATION][%s] ============== Restored offset calibration from memory ==============", cs);
291 ESP_LOGI(TAG,
"[CALIBRATION][%s] --------------------------------------------------------------", cs);
292 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
293 ESP_LOGI(TAG,
"[CALIBRATION][%s] --------------------------------------------------------------", cs);
294 for (uint8_t phase = 0; phase < 3; phase++) {
295 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase,
298 ESP_LOGI(TAG,
"[CALIBRATION][%s] ==============================================================\\n", cs);
302 ESP_LOGI(TAG,
"[CALIBRATION][%s] ", cs);
303 ESP_LOGI(TAG,
"[CALIBRATION][%s] ============ Restored power offset calibration from memory ============", cs);
304 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
305 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
306 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
307 for (uint8_t phase = 0; phase < 3; phase++) {
308 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase,
312 ESP_LOGI(TAG,
"[CALIBRATION][%s] =====================================================================\n", cs);
315 ESP_LOGI(TAG,
"[CALIBRATION][%s] Gain calibration is disabled. Using config file values.", cs);
317 ESP_LOGI(TAG,
"[CALIBRATION][%s] ", cs);
318 ESP_LOGI(TAG,
"[CALIBRATION][%s] ============ Restoring saved gain calibrations to registers ============", cs);
319 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
320 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | voltage_gain | current_gain |", cs);
321 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
322 for (uint8_t phase = 0; phase < 3; phase++) {
323 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6u | %6u |", cs,
'A' + phase,
326 ESP_LOGI(TAG,
"[CALIBRATION][%s] =====================================================================\\n", cs);
327 ESP_LOGI(TAG,
"[CALIBRATION][%s] Gain calibration loaded and verified successfully.\n", cs);
333 ESP_LOGCONFIG(
"",
"ATM90E32:");
334 LOG_PIN(
" CS Pin: ", this->
cs_);
336 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
338 LOG_UPDATE_INTERVAL(
this);
339 LOG_SENSOR(
" ",
"Voltage A", this->
phase_[
PHASEA].voltage_sensor_);
340 LOG_SENSOR(
" ",
"Current A", this->
phase_[
PHASEA].current_sensor_);
341 LOG_SENSOR(
" ",
"Power A", this->
phase_[
PHASEA].power_sensor_);
342 LOG_SENSOR(
" ",
"Reactive Power A", this->
phase_[
PHASEA].reactive_power_sensor_);
343 LOG_SENSOR(
" ",
"Apparent Power A", this->
phase_[
PHASEA].apparent_power_sensor_);
344 LOG_SENSOR(
" ",
"PF A", this->
phase_[
PHASEA].power_factor_sensor_);
345 LOG_SENSOR(
" ",
"Active Forward Energy A", this->
phase_[
PHASEA].forward_active_energy_sensor_);
346 LOG_SENSOR(
" ",
"Active Reverse Energy A", this->
phase_[
PHASEA].reverse_active_energy_sensor_);
347 LOG_SENSOR(
" ",
"Harmonic Power A", this->
phase_[
PHASEA].harmonic_active_power_sensor_);
348 LOG_SENSOR(
" ",
"Phase Angle A", this->
phase_[
PHASEA].phase_angle_sensor_);
349 LOG_SENSOR(
" ",
"Peak Current A", this->
phase_[
PHASEA].peak_current_sensor_);
350 LOG_SENSOR(
" ",
"Voltage B", this->
phase_[
PHASEB].voltage_sensor_);
351 LOG_SENSOR(
" ",
"Current B", this->
phase_[
PHASEB].current_sensor_);
352 LOG_SENSOR(
" ",
"Power B", this->
phase_[
PHASEB].power_sensor_);
353 LOG_SENSOR(
" ",
"Reactive Power B", this->
phase_[
PHASEB].reactive_power_sensor_);
354 LOG_SENSOR(
" ",
"Apparent Power B", this->
phase_[
PHASEB].apparent_power_sensor_);
355 LOG_SENSOR(
" ",
"PF B", this->
phase_[
PHASEB].power_factor_sensor_);
356 LOG_SENSOR(
" ",
"Active Forward Energy B", this->
phase_[
PHASEB].forward_active_energy_sensor_);
357 LOG_SENSOR(
" ",
"Active Reverse Energy B", this->
phase_[
PHASEB].reverse_active_energy_sensor_);
358 LOG_SENSOR(
" ",
"Harmonic Power B", this->
phase_[
PHASEB].harmonic_active_power_sensor_);
359 LOG_SENSOR(
" ",
"Phase Angle B", this->
phase_[
PHASEB].phase_angle_sensor_);
360 LOG_SENSOR(
" ",
"Peak Current B", this->
phase_[
PHASEB].peak_current_sensor_);
361 LOG_SENSOR(
" ",
"Voltage C", this->
phase_[
PHASEC].voltage_sensor_);
362 LOG_SENSOR(
" ",
"Current C", this->
phase_[
PHASEC].current_sensor_);
363 LOG_SENSOR(
" ",
"Power C", this->
phase_[
PHASEC].power_sensor_);
364 LOG_SENSOR(
" ",
"Reactive Power C", this->
phase_[
PHASEC].reactive_power_sensor_);
365 LOG_SENSOR(
" ",
"Apparent Power C", this->
phase_[
PHASEC].apparent_power_sensor_);
366 LOG_SENSOR(
" ",
"PF C", this->
phase_[
PHASEC].power_factor_sensor_);
367 LOG_SENSOR(
" ",
"Active Forward Energy C", this->
phase_[
PHASEC].forward_active_energy_sensor_);
368 LOG_SENSOR(
" ",
"Active Reverse Energy C", this->
phase_[
PHASEC].reverse_active_energy_sensor_);
369 LOG_SENSOR(
" ",
"Harmonic Power C", this->
phase_[
PHASEC].harmonic_active_power_sensor_);
370 LOG_SENSOR(
" ",
"Phase Angle C", this->
phase_[
PHASEC].phase_angle_sensor_);
371 LOG_SENSOR(
" ",
"Peak Current C", this->
phase_[
PHASEC].peak_current_sensor_);
388 uint8_t addrh = (1 << 7) | ((a_register >> 8) & 0x03);
389 uint8_t addrl = (a_register & 0xFF);
390 uint8_t data[4] = {addrh, addrl, 0x00, 0x00};
393 ESP_LOGVV(TAG,
"read16_ 0x%04" PRIX16
" output 0x%04" PRIX16, a_register, output);
401 const uint16_t val_h = this->
read16_(addr_h);
402 const uint16_t val_l = this->
read16_(addr_l);
403 const int32_t
val = (val_h << 16) | val_l;
406 "read32_ addr_h 0x%04" PRIX16
" val_h 0x%04" PRIX16
" addr_l 0x%04" PRIX16
" val_l 0x%04" PRIX16
408 addr_h, val_h, addr_l, val_l,
val);
414 ESP_LOGVV(TAG,
"write16_ 0x%04" PRIX16
" val 0x%04" PRIX16, a_register,
val);
415 uint8_t addrh = ((a_register >> 8) & 0x03);
416 uint8_t addrl = (a_register & 0xFF);
417 uint8_t data[4] = {addrh, addrl, uint8_t((
val >> 8) & 0xFF), uint8_t(
val & 0xFF)};
457 const uint16_t voltage = this->
read16_(ATM90E32_REGISTER_URMS + phase);
459 return (
float) voltage / 100;
463 const uint8_t reads = 10;
464 uint32_t accumulation = 0;
465 uint16_t voltage = 0;
466 for (uint8_t i = 0; i < reads; i++) {
467 voltage = this->
read16_(ATM90E32_REGISTER_URMS + phase);
469 accumulation += voltage;
471 voltage = accumulation / reads;
477 const uint8_t reads = 10;
478 uint32_t accumulation = 0;
479 uint16_t current = 0;
480 for (uint8_t i = 0; i < reads; i++) {
481 current = this->
read16_(ATM90E32_REGISTER_IRMS + phase);
483 accumulation += current;
485 current = accumulation / reads;
491 const uint16_t current = this->
read16_(ATM90E32_REGISTER_IRMS + phase);
493 return (
float) current / 1000;
497 const int val = this->
read32_(ATM90E32_REGISTER_PMEAN + phase, ATM90E32_REGISTER_PMEANLSB + phase);
498 return val * 0.00032f;
502 const int val = this->
read32_(ATM90E32_REGISTER_QMEAN + phase, ATM90E32_REGISTER_QMEANLSB + phase);
503 return val * 0.00032f;
507 const int val = this->
read32_(ATM90E32_REGISTER_SMEAN + phase, ATM90E32_REGISTER_SMEANLSB + phase);
508 return val * 0.00032f;
512 uint16_t powerfactor = this->
read16_(ATM90E32_REGISTER_PFMEAN + phase);
514 return (
float) ((int16_t) powerfactor) / 1000;
518 const uint16_t
val = this->
read16_(ATM90E32_REGISTER_APENERGY + phase);
519 if ((UINT32_MAX - this->
phase_[phase].cumulative_forward_active_energy_) >
val) {
525 return ((
float) this->
phase_[phase].cumulative_forward_active_energy_ * (10.0f / 3200.0f));
529 const uint16_t
val = this->
read16_(ATM90E32_REGISTER_ANENERGY + phase);
530 if (UINT32_MAX - this->
phase_[phase].cumulative_reverse_active_energy_ >
val) {
536 return ((
float) this->
phase_[phase].cumulative_reverse_active_energy_ * (10.0f / 3200.0f));
540 int val = this->
read32_(ATM90E32_REGISTER_PMEANH + phase, ATM90E32_REGISTER_PMEANHLSB + phase);
541 return val * 0.00032f;
545 uint16_t
val = this->
read16_(ATM90E32_REGISTER_PANGLE + phase) / 10.0;
546 return (
val > 180) ? (float) (
val - 360.0f) : (float)
val;
550 int16_t
val = (float) this->
read16_(ATM90E32_REGISTER_IPEAK + phase);
554 return (
val * this->
phase_[phase].ct_gain_ / 8192000.0);
558 const uint16_t freq = this->
read16_(ATM90E32_REGISTER_FREQ);
559 return (
float) freq / 100;
563 const uint16_t ctemp = this->
read16_(ATM90E32_REGISTER_TEMP);
564 return (
float) ctemp;
570 ESP_LOGW(TAG,
"[CALIBRATION][%s] Gain calibration is disabled! Enable it first with enable_gain_calibration: true",
575 float ref_voltages[3] = {
583 ESP_LOGI(TAG,
"[CALIBRATION][%s] ", cs);
584 ESP_LOGI(TAG,
"[CALIBRATION][%s] ========================= Gain Calibration =========================", cs);
585 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
588 "[CALIBRATION][%s] | Phase | V_meas (V) | I_meas (A) | V_ref | I_ref | V_gain (old→new) | I_gain (old→new) |",
590 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
592 for (uint8_t phase = 0; phase < 3; phase++) {
596 float ref_voltage = ref_voltages[phase];
597 float ref_current = ref_currents[phase];
602 bool did_voltage =
false;
603 bool did_current =
false;
606 if (ref_voltage <= 0.0f) {
607 ESP_LOGW(TAG,
"[CALIBRATION][%s] Phase %s - Skipping voltage calibration: reference voltage is 0.", cs,
609 }
else if (measured_voltage == 0.0f) {
610 ESP_LOGW(TAG,
"[CALIBRATION][%s] Phase %s - Skipping voltage calibration: measured voltage is 0.", cs,
613 uint32_t new_voltage_gain =
static_cast<uint16_t
>((ref_voltage / measured_voltage) * current_voltage_gain);
614 if (new_voltage_gain == 0) {
615 ESP_LOGW(TAG,
"[CALIBRATION][%s] Phase %s - Voltage gain would be 0. Check reference and measured voltage.", cs,
618 if (new_voltage_gain >= 65535) {
620 "[CALIBRATION][%s] Phase %s - Voltage gain exceeds 65535. You may need a higher output voltage "
623 new_voltage_gain = 65535;
631 if (ref_current == 0.0f) {
632 ESP_LOGW(TAG,
"[CALIBRATION][%s] Phase %s - Skipping current calibration: reference current is 0.", cs,
634 }
else if (measured_current == 0.0f) {
635 ESP_LOGW(TAG,
"[CALIBRATION][%s] Phase %s - Skipping current calibration: measured current is 0.", cs,
638 uint32_t new_current_gain =
static_cast<uint16_t
>((ref_current / measured_current) * current_current_gain);
639 if (new_current_gain == 0) {
640 ESP_LOGW(TAG,
"[CALIBRATION][%s] Phase %s - Current gain would be 0. Check reference and measured current.", cs,
643 if (new_current_gain >= 65535) {
644 ESP_LOGW(TAG,
"[CALIBRATION][%s] Phase %s - Current gain exceeds 65535. You may need to turn up pga gain.",
646 new_current_gain = 65535;
654 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %9.2f | %9.4f | %5.2f | %6.4f | %5u → %-5u | %5u → %-5u |", cs,
655 'A' + phase, measured_voltage, measured_current, ref_voltage, ref_current, current_voltage_gain,
656 did_voltage ? this->
gain_phase_[phase].voltage_gain : current_voltage_gain, current_current_gain,
657 did_current ? this->
gain_phase_[phase].current_gain : current_current_gain);
660 ESP_LOGI(TAG,
"[CALIBRATION][%s] =====================================================================\n", cs);
673 ESP_LOGI(TAG,
"[CALIBRATION][%s] Gain calibration saved to memory.", cs);
676 ESP_LOGE(TAG,
"[CALIBRATION][%s] Failed to save gain calibration to memory!", cs);
689 ESP_LOGI(TAG,
"[CALIBRATION][%s] Offset calibration saved to memory.", cs);
692 ESP_LOGE(TAG,
"[CALIBRATION][%s] Failed to save offset calibration to memory!", cs);
705 ESP_LOGI(TAG,
"[CALIBRATION][%s] Power offset calibration saved to memory.", cs);
708 ESP_LOGE(TAG,
"[CALIBRATION][%s] Failed to save power offset calibration to memory!", cs);
716 "[CALIBRATION][%s] Offset calibration is disabled! Enable it first with enable_offset_calibration: true",
721 ESP_LOGI(TAG,
"[CALIBRATION][%s] ", cs);
722 ESP_LOGI(TAG,
"[CALIBRATION][%s] ======================== Offset Calibration ========================", cs);
723 ESP_LOGI(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------", cs);
724 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
725 ESP_LOGI(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------", cs);
727 for (uint8_t phase = 0; phase < 3; phase++) {
733 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase, voltage_offset,
737 ESP_LOGI(TAG,
"[CALIBRATION][%s] ==================================================================\n", cs);
747 "[CALIBRATION][%s] Offset power calibration is disabled! Enable it first with enable_offset_calibration: true",
752 ESP_LOGI(TAG,
"[CALIBRATION][%s] ", cs);
753 ESP_LOGI(TAG,
"[CALIBRATION][%s] ===================== Power Offset Calibration =====================", cs);
754 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
755 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
756 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
758 for (uint8_t phase = 0; phase < 3; ++phase) {
764 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase, active_offset,
767 ESP_LOGI(TAG,
"[CALIBRATION][%s] =====================================================================\n", cs);
773 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA);
775 for (
int phase = 0; phase < 3; phase++) {
780 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x0000);
793 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA);
796 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x0000);
809 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA);
812 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x0000);
817 for (uint8_t i = 0; i < 3; ++i) {
824 bool all_zero =
true;
825 bool same_as_config =
true;
826 for (uint8_t phase = 0; phase < 3; ++phase) {
829 if (saved.voltage_gain != 0 || saved.current_gain != 0)
831 if (saved.voltage_gain != cfg.voltage_gain || saved.current_gain != cfg.current_gain)
832 same_as_config =
false;
835 if (!all_zero && !same_as_config) {
836 for (uint8_t phase = 0; phase < 3; ++phase) {
837 bool mismatch =
false;
857 ESP_LOGE(TAG,
"[CALIBRATION][%s] Gain verification failed! Calibration may not be applied correctly.", cs);
862 for (uint8_t i = 0; i < 3; ++i)
866 ESP_LOGW(TAG,
"[CALIBRATION][%s] No stored gain calibrations found. Using config file values.", cs);
871 for (uint8_t i = 0; i < 3; ++i)
875 bool all_zero =
true;
878 if (phase.voltage_offset_ != 0 || phase.current_offset_ != 0) {
885 if (have_data && !all_zero) {
887 for (uint8_t phase = 0; phase < 3; phase++) {
889 bool mismatch =
false;
891 offset.voltage_offset_ != this->config_offset_phase_[phase].voltage_offset_)
894 offset.current_offset_ != this->config_offset_phase_[phase].current_offset_)
900 for (uint8_t phase = 0; phase < 3; phase++)
902 ESP_LOGW(TAG,
"[CALIBRATION][%s] No stored offset calibrations found. Using default values.", cs);
905 for (uint8_t phase = 0; phase < 3; phase++) {
913 for (uint8_t i = 0; i < 3; ++i)
917 bool all_zero =
true;
920 if (phase.active_power_offset != 0 || phase.reactive_power_offset != 0) {
927 if (have_data && !all_zero) {
929 for (uint8_t phase = 0; phase < 3; ++phase) {
931 bool mismatch =
false;
933 offset.active_power_offset != this->config_power_offset_phase_[phase].active_power_offset)
936 offset.reactive_power_offset != this->config_power_offset_phase_[phase].reactive_power_offset)
942 for (uint8_t phase = 0; phase < 3; ++phase)
944 ESP_LOGW(TAG,
"[CALIBRATION][%s] No stored power offsets found. Using default values.", cs);
947 for (uint8_t phase = 0; phase < 3; ++phase) {
956 ESP_LOGI(TAG,
"[CALIBRATION][%s] No stored gain calibrations to clear. Current values:", cs);
957 ESP_LOGI(TAG,
"[CALIBRATION][%s] ----------------------------------------------------------", cs);
958 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | voltage_gain | current_gain |", cs);
959 ESP_LOGI(TAG,
"[CALIBRATION][%s] ----------------------------------------------------------", cs);
960 for (
int phase = 0; phase < 3; phase++) {
961 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6u | %6u |", cs,
'A' + phase,
964 ESP_LOGI(TAG,
"[CALIBRATION][%s] ==========================================================\n", cs);
968 ESP_LOGI(TAG,
"[CALIBRATION][%s] Clearing stored gain calibrations and restoring config-defined values", cs);
969 ESP_LOGI(TAG,
"[CALIBRATION][%s] ----------------------------------------------------------", cs);
970 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | voltage_gain | current_gain |", cs);
971 ESP_LOGI(TAG,
"[CALIBRATION][%s] ----------------------------------------------------------", cs);
973 for (
int phase = 0; phase < 3; phase++) {
982 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6u | %6u |", cs,
'A' + phase, voltage_gain, current_gain);
984 ESP_LOGI(TAG,
"[CALIBRATION][%s] ==========================================================\n", cs);
996 ESP_LOGE(TAG,
"[CALIBRATION][%s] Failed to clear gain calibrations!", cs);
1005 ESP_LOGI(TAG,
"[CALIBRATION][%s] No stored offset calibrations to clear. Current values:", cs);
1006 ESP_LOGI(TAG,
"[CALIBRATION][%s] --------------------------------------------------------------", cs);
1007 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
1008 ESP_LOGI(TAG,
"[CALIBRATION][%s] --------------------------------------------------------------", cs);
1009 for (uint8_t phase = 0; phase < 3; phase++) {
1010 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase,
1013 ESP_LOGI(TAG,
"[CALIBRATION][%s] ==============================================================\n", cs);
1017 ESP_LOGI(TAG,
"[CALIBRATION][%s] Clearing stored offset calibrations and restoring config-defined values", cs);
1018 ESP_LOGI(TAG,
"[CALIBRATION][%s] --------------------------------------------------------------", cs);
1019 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
1020 ESP_LOGI(TAG,
"[CALIBRATION][%s] --------------------------------------------------------------", cs);
1022 for (uint8_t phase = 0; phase < 3; phase++) {
1023 int16_t voltage_offset =
1025 int16_t current_offset =
1028 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase, voltage_offset,
1031 ESP_LOGI(TAG,
"[CALIBRATION][%s] ==============================================================\n", cs);
1041 ESP_LOGI(TAG,
"[CALIBRATION][%s] Offsets cleared.", cs);
1047 ESP_LOGI(TAG,
"[CALIBRATION][%s] No stored power offsets to clear. Current values:", cs);
1048 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
1049 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
1050 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
1051 for (uint8_t phase = 0; phase < 3; phase++) {
1052 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase,
1056 ESP_LOGI(TAG,
"[CALIBRATION][%s] =====================================================================\n", cs);
1060 ESP_LOGI(TAG,
"[CALIBRATION][%s] Clearing stored power offsets and restoring config-defined values", cs);
1061 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
1062 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
1063 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
1065 for (uint8_t phase = 0; phase < 3; phase++) {
1066 int16_t active_offset =
1072 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase, active_offset,
1075 ESP_LOGI(TAG,
"[CALIBRATION][%s] =====================================================================\n", cs);
1085 ESP_LOGI(TAG,
"[CALIBRATION][%s] Power offsets cleared.", cs);
1089 const uint8_t num_reads = 5;
1090 uint64_t total_value = 0;
1092 for (uint8_t i = 0; i < num_reads; ++i) {
1093 uint32_t reading = voltage ? this->
read32_(ATM90E32_REGISTER_URMS + phase, ATM90E32_REGISTER_URMSLSB + phase)
1094 : this->
read32_(ATM90E32_REGISTER_IRMS + phase, ATM90E32_REGISTER_IRMSLSB + phase);
1095 total_value += reading;
1098 const uint32_t average_value = total_value / num_reads;
1099 const uint32_t shifted = average_value >> 7;
1100 const uint32_t offset = ~shifted + 1;
1101 return static_cast<int16_t
>(offset);
1105 const uint8_t num_reads = 5;
1106 int64_t total_value = 0;
1108 for (uint8_t i = 0; i < num_reads; ++i) {
1109 int32_t reading = reactive ? this->
read32_(ATM90E32_REGISTER_QMEAN + phase, ATM90E32_REGISTER_QMEANLSB + phase)
1110 : this->
read32_(ATM90E32_REGISTER_PMEAN + phase, ATM90E32_REGISTER_PMEANLSB + phase);
1111 total_value += reading;
1114 int32_t average_value = total_value / num_reads;
1115 int32_t power_offset = -average_value;
1116 return static_cast<int16_t
>(power_offset);
1121 bool success =
true;
1122 for (uint8_t phase = 0; phase < 3; phase++) {
1126 if (read_voltage != this->
gain_phase_[phase].voltage_gain ||
1127 read_current != this->
gain_phase_[phase].current_gain) {
1128 ESP_LOGE(TAG,
"[CALIBRATION][%s] Mismatch detected for Phase %s!", cs,
phase_labels[phase]);
1135#ifdef USE_TEXT_SENSOR
1137 uint16_t state0 = this->
read16_(ATM90E32_REGISTER_EMMSTATE0);
1138 uint16_t state1 = this->
read16_(ATM90E32_REGISTER_EMMSTATE1);
1140 for (
int phase = 0; phase < 3; phase++) {
1144 status +=
"Over Voltage; ";
1146 status +=
"Voltage Sag; ";
1148 status +=
"Phase Loss; ";
1151 if (sensor ==
nullptr)
1157 ESP_LOGW(TAG,
"%s: %s", sensor->get_name().c_str(),
status.c_str());
1158 sensor->publish_state(
status);
1160 sensor->publish_state(
"Okay");
1166 uint16_t state1 = this->
read16_(ATM90E32_REGISTER_EMMSTATE1);
1168 std::string freq_status;
1170 if (state1 & ATM90E32_STATUS_S1_FREQHIST) {
1171 freq_status =
"HIGH";
1172 }
else if (state1 & ATM90E32_STATUS_S1_FREQLOST) {
1173 freq_status =
"LOW";
1175 freq_status =
"Normal";
1178 if (freq_status ==
"Normal") {
1179 ESP_LOGD(TAG,
"Frequency status: %s", freq_status.c_str());
1181 ESP_LOGW(TAG,
"Frequency status: %s", freq_status.c_str());
1188 constexpr float max_current_threshold = 65.53f;
1190 for (uint8_t phase = 0; phase < 3; phase++) {
1194 if (current_val > max_current_threshold) {
1195 ESP_LOGW(TAG,
"Over current detected on Phase %c: %.2f A",
'A' + phase, current_val);
1196 ESP_LOGW(TAG,
"You may need to half your gain_ct: value & multiply the current and power values by 2");
1208 float nominal_voltage = (line_freq == 60) ? 120.0f : 220.0f;
1209 float target_voltage = nominal_voltage * multiplier;
1211 float peak_01v = target_voltage * 100.0f * std::numbers::sqrt2_v<float>;
1212 float divider = (2.0f * ugain) / 32768.0f;
1214 float threshold = peak_01v / divider;
1216 return static_cast<uint16_t
>(threshold);
1220 uint16_t last = this->
read16_(ATM90E32_REGISTER_LASTSPIDATA);
1221 if (last != expected) {
1222 if (context !=
nullptr) {
1223 ESP_LOGW(TAG,
"[%s] SPI read mismatch: expected 0x%04X, got 0x%04X", context, expected, last);
1225 ESP_LOGW(TAG,
"SPI read mismatch: expected 0x%04X, got 0x%04X", expected, last);
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
virtual bool sync()=0
Commit pending writes to flash.
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
virtual std::string dump_summary() const =0
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()
bool enable_gain_calibration_
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)
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)
const float IO
For components that represent GPIO pins like PCF8573.
Providing packet encoding functions for exchanging data with a remote host.
uint32_t fnv1_hash(const std::string &str)
Calculate a FNV-1 hash of str.
ESPPreferences * global_preferences
void IRAM_ATTR HOT 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.
void IRAM_ATTR HOT delay(uint32_t ms)
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