ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
atm90e32.cpp
Go to the documentation of this file.
1#include "atm90e32.h"
2#include <cinttypes>
3#include <cmath>
4#include <cstring>
5#include <numbers>
6#include "esphome/core/log.h"
7
9
10static const char *const TAG = "atm90e32";
11
12static const LogString *offset_calibration_name(bool power_offsets) {
13 return power_offsets ? LOG_STR("Power offset") : LOG_STR("Offset");
14}
15
16static uint32_t pref_hash(const char *prefix, const char *name_space) {
17 auto hash = fnv1_hash(prefix);
18 return fnv1_hash_extend(hash, name_space);
19}
20
21template<typename T>
22static int migrate_legacy_pref_if_needed(ESPPreferenceObject &current_pref, ESPPreferenceObject &legacy_pref,
23 T *scratch) {
24 T current{};
25 if (current_pref.load(&current)) {
26 return 0;
27 }
28 if (!legacy_pref.load(scratch)) {
29 return 0;
30 }
31 return current_pref.save(scratch) ? 1 : -1;
32}
33
35 if (this->get_publish_interval_flag_()) {
36 this->set_publish_interval_flag_(false);
37 for (uint8_t phase = 0; phase < 3; phase++) {
38 if (this->phase_[phase].voltage_sensor_ != nullptr)
39 this->phase_[phase].voltage_ = this->get_phase_voltage_(phase);
40
41 if (this->phase_[phase].current_sensor_ != nullptr)
42 this->phase_[phase].current_ = this->get_phase_current_(phase);
43
44 if (this->phase_[phase].power_sensor_ != nullptr)
45 this->phase_[phase].active_power_ = this->get_phase_active_power_(phase);
46
47 if (this->phase_[phase].power_factor_sensor_ != nullptr)
48 this->phase_[phase].power_factor_ = this->get_phase_power_factor_(phase);
49
50 if (this->phase_[phase].reactive_power_sensor_ != nullptr)
51 this->phase_[phase].reactive_power_ = this->get_phase_reactive_power_(phase);
52
53 if (this->phase_[phase].apparent_power_sensor_ != nullptr)
54 this->phase_[phase].apparent_power_ = this->get_phase_apparent_power_(phase);
55
56 if (this->phase_[phase].forward_active_energy_sensor_ != nullptr)
58
59 if (this->phase_[phase].reverse_active_energy_sensor_ != nullptr)
61
62 if (this->phase_[phase].phase_angle_sensor_ != nullptr)
63 this->phase_[phase].phase_angle_ = this->get_phase_angle_(phase);
64
65 if (this->phase_[phase].harmonic_active_power_sensor_ != nullptr)
67
68 if (this->phase_[phase].peak_current_sensor_ != nullptr)
69 this->phase_[phase].peak_current_ = this->get_phase_peak_current_(phase);
70
71 // After the local store is collected we can publish them trusting they are within +-1 hardware sampling
72 if (this->phase_[phase].voltage_sensor_ != nullptr)
74
75 if (this->phase_[phase].current_sensor_ != nullptr)
77
78 if (this->phase_[phase].power_sensor_ != nullptr)
80
81 if (this->phase_[phase].power_factor_sensor_ != nullptr)
83
84 if (this->phase_[phase].reactive_power_sensor_ != nullptr)
86
87 if (this->phase_[phase].apparent_power_sensor_ != nullptr)
89
90 if (this->phase_[phase].forward_active_energy_sensor_ != nullptr) {
93 }
94
95 if (this->phase_[phase].reverse_active_energy_sensor_ != nullptr) {
98 }
99
100 if (this->phase_[phase].phase_angle_sensor_ != nullptr)
102
103 if (this->phase_[phase].harmonic_active_power_sensor_ != nullptr) {
106 }
107
108 if (this->phase_[phase].peak_current_sensor_ != nullptr)
110 }
111 if (this->freq_sensor_ != nullptr)
113
114 if (this->chip_temperature_sensor_ != nullptr)
116 }
117}
118
120 if (this->read16_(ATM90E32_REGISTER_METEREN) != 1) {
121 this->status_set_warning();
122 return;
123 }
124 this->set_publish_interval_flag_(true);
125 this->status_clear_warning();
126
127#ifdef USE_TEXT_SENSOR
128 this->check_phase_status();
129 this->check_over_current();
130 this->check_freq_status();
131#endif
132}
133
134void ATM90E32Component::get_cs_summary_(std::span<char, GPIO_SUMMARY_MAX_LEN> buffer) {
135 this->cs_->dump_summary(buffer.data(), buffer.size());
136}
137
139
141 this->spi_setup();
142 const char *cs = this->get_calibration_id_();
143 char legacy_cs[GPIO_SUMMARY_MAX_LEN];
144 this->get_cs_summary_(legacy_cs);
145 const bool has_distinct_legacy_namespace = strcmp(cs, legacy_cs) != 0;
146
147 uint16_t mmode0 = 0x87; // 3P4W 50Hz
148 uint16_t high_thresh = 0;
149 uint16_t low_thresh = 0;
150
151 if (line_freq_ == 60) {
152 mmode0 |= 1 << 12; // sets 12th bit to 1, 60Hz
153 // for freq threshold registers
154 high_thresh = 6300; // 63.00 Hz
155 low_thresh = 5700; // 57.00 Hz
156 } else {
157 high_thresh = 5300; // 53.00 Hz
158 low_thresh = 4700; // 47.00 Hz
159 }
160
161 if (current_phases_ == 2) {
162 mmode0 |= 1 << 8; // sets 8th bit to 1, 3P3W
163 mmode0 |= 0 << 1; // sets 1st bit to 0, phase b is not counted into the all-phase sum energy/power (P/Q/S)
164 }
165
166 this->write16_(ATM90E32_REGISTER_SOFTRESET, 0x789A, false); // Perform soft reset
167 delay(6); // Wait for the minimum 5ms + 1ms
168 this->write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA); // enable register config access
169 if (!this->validate_spi_read_(0x55AA, "setup()")) {
170 ESP_LOGW(TAG, "Could not initialize ATM90E32 IC, check SPI settings");
171 this->mark_failed();
172 return;
173 }
174
175 this->write16_(ATM90E32_REGISTER_METEREN, 0x0001); // Enable Metering
176 this->write16_(ATM90E32_REGISTER_SAGPEAKDETCFG, 0xFF3F); // Peak Detector time (15:8) 255ms, Sag Period (7:0) 63ms
177 this->write16_(ATM90E32_REGISTER_PLCONSTH, 0x0861); // PL Constant MSB (default) = 140625000
178 this->write16_(ATM90E32_REGISTER_PLCONSTL, 0xC468); // PL Constant LSB (default)
179 this->write16_(ATM90E32_REGISTER_ZXCONFIG, 0xD654); // Zero crossing (ZX2, ZX1, ZX0) pin config
180 this->write16_(ATM90E32_REGISTER_MMODE0, mmode0); // Mode Config (frequency set in main program)
181 this->write16_(ATM90E32_REGISTER_MMODE1, pga_gain_); // PGA Gain Configuration for Current Channels
182 this->write16_(ATM90E32_REGISTER_FREQHITH, high_thresh); // Frequency high threshold
183 this->write16_(ATM90E32_REGISTER_FREQLOTH, low_thresh); // Frequency low threshold
184 this->write16_(ATM90E32_REGISTER_PSTARTTH, 0x1D4C); // All Active Startup Power Threshold - 0.02A/0.00032 = 7500
185 this->write16_(ATM90E32_REGISTER_QSTARTTH, 0x1D4C); // All Reactive Startup Power Threshold - 50%
186 this->write16_(ATM90E32_REGISTER_SSTARTTH, 0x1D4C); // All Reactive Startup Power Threshold - 50%
187 this->write16_(ATM90E32_REGISTER_PPHASETH, 0x02EE); // Each Phase Active Phase Threshold - 0.002A/0.00032 = 750
188 this->write16_(ATM90E32_REGISTER_QPHASETH, 0x02EE); // Each phase Reactive Phase Threshold - 10%
189
190 if (this->enable_offset_calibration_) {
191 // Initialize flash storage for offset calibrations
192 uint32_t o_hash = pref_hash("_offset_calibration_", cs);
194 bool migrated_offset = false;
195 if (has_distinct_legacy_namespace) {
196 uint32_t legacy_o_hash = pref_hash("_offset_calibration_", legacy_cs);
197 auto legacy_offset_pref = global_preferences->make_preference<OffsetCalibration[3]>(legacy_o_hash, true);
198 OffsetCalibration offset_data[3]{};
199 int migration_status = migrate_legacy_pref_if_needed(this->offset_pref_, legacy_offset_pref, &offset_data);
200 migrated_offset = migration_status > 0;
201 if (migration_status > 0) {
202 ESP_LOGI(TAG, "[CALIBRATION][%s] Migrated offset calibrations from legacy storage.", cs);
203 } else if (migration_status < 0) {
204 ESP_LOGW(TAG, "[CALIBRATION][%s] Failed to migrate offset calibrations from legacy storage.", cs);
205 }
206 }
207
208 // Initialize flash storage for power offset calibrations
209 uint32_t po_hash = pref_hash("_power_offset_calibration_", cs);
211 bool migrated_power_offset = false;
212 if (has_distinct_legacy_namespace) {
213 uint32_t legacy_po_hash = pref_hash("_power_offset_calibration_", legacy_cs);
214 auto legacy_power_offset_pref = global_preferences->make_preference<OffsetCalibration[3]>(legacy_po_hash, true);
215 OffsetCalibration power_offset_data[3]{};
216 int migration_status =
217 migrate_legacy_pref_if_needed(this->power_offset_pref_, legacy_power_offset_pref, &power_offset_data);
218 migrated_power_offset = migration_status > 0;
219 if (migration_status > 0) {
220 ESP_LOGI(TAG, "[CALIBRATION][%s] Migrated power offset calibrations from legacy storage.", cs);
221 } else if (migration_status < 0) {
222 ESP_LOGW(TAG, "[CALIBRATION][%s] Failed to migrate power offset calibrations from legacy storage.", cs);
223 }
224 }
225
226 if (migrated_offset || migrated_power_offset) {
228 }
229
232 } else {
233 ESP_LOGI(TAG, "[CALIBRATION][%s] Power & Voltage/Current offset calibration is disabled. Using config file values.",
234 cs);
235 for (uint8_t phase = 0; phase < 3; ++phase) {
236 this->write16_(this->voltage_offset_registers[phase],
237 static_cast<uint16_t>(this->offset_phase_[phase].first_offset));
238 this->write16_(this->current_offset_registers[phase],
239 static_cast<uint16_t>(this->offset_phase_[phase].second_offset));
240 this->write16_(this->power_offset_registers[phase],
241 static_cast<uint16_t>(this->power_offset_phase_[phase].first_offset));
242 this->write16_(this->reactive_power_offset_registers[phase],
243 static_cast<uint16_t>(this->power_offset_phase_[phase].second_offset));
244 }
245 }
246
247 if (this->enable_gain_calibration_) {
248 // Initialize flash storage for gain calibration
249 uint32_t g_hash = pref_hash("_gain_calibration_", cs);
251 bool migrated_gain = false;
252 if (has_distinct_legacy_namespace) {
253 uint32_t legacy_g_hash = pref_hash("_gain_calibration_", legacy_cs);
254 auto legacy_gain_calibration_pref = global_preferences->make_preference<GainCalibration[3]>(legacy_g_hash, true);
255 GainCalibration gain_data[3]{};
256 int migration_status =
257 migrate_legacy_pref_if_needed(this->gain_calibration_pref_, legacy_gain_calibration_pref, &gain_data);
258 migrated_gain = migration_status > 0;
259 if (migration_status > 0) {
260 ESP_LOGI(TAG, "[CALIBRATION][%s] Migrated gain calibrations from legacy storage.", cs);
261 } else if (migration_status < 0) {
262 ESP_LOGW(TAG, "[CALIBRATION][%s] Failed to migrate gain calibrations from legacy storage.", cs);
263 }
264 }
265
266 if (migrated_gain) {
268 }
269
271
272 if (!this->using_saved_calibrations_) {
273 for (uint8_t phase = 0; phase < 3; ++phase) {
274 this->write16_(voltage_gain_registers[phase], this->phase_[phase].voltage_gain_);
275 this->write16_(current_gain_registers[phase], this->phase_[phase].ct_gain_);
276 }
277 }
278 } else {
279 ESP_LOGI(TAG, "[CALIBRATION][%s] Gain calibration is disabled. Using config file values.", cs);
280 for (uint8_t phase = 0; phase < 3; ++phase) {
281 this->write16_(voltage_gain_registers[phase], this->phase_[phase].voltage_gain_);
282 this->write16_(current_gain_registers[phase], this->phase_[phase].ct_gain_);
283 }
284 }
285
286 // Sag threshold (78%)
287 uint16_t sagth = calculate_voltage_threshold(line_freq_, this->phase_[0].voltage_gain_, 0.78f);
288 // Overvoltage threshold (122%)
289 uint16_t ovth = calculate_voltage_threshold(line_freq_, this->phase_[0].voltage_gain_, 1.22f);
290
291 // Write to registers
292 this->write16_(ATM90E32_REGISTER_SAGTH, sagth);
293 this->write16_(ATM90E32_REGISTER_OVTH, ovth);
294
295 this->write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x0000); // end configuration
296}
297
299 const char *cs = this->get_calibration_id_();
300
301 bool offset_mismatch = false;
302 bool power_mismatch = false;
303 bool gain_mismatch = false;
304
305 for (uint8_t phase = 0; phase < 3; ++phase) {
306 offset_mismatch |= this->offset_calibration_mismatch_[phase];
307 power_mismatch |= this->power_offset_calibration_mismatch_[phase];
308 gain_mismatch |= this->gain_calibration_mismatch_[phase];
309 }
310
311 if (offset_mismatch) {
312 ESP_LOGW(TAG, "[CALIBRATION][%s] ", cs);
313 ESP_LOGW(TAG,
314 "[CALIBRATION][%s] ===================== Offset mismatch: using flash values =====================", cs);
315 ESP_LOGW(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------------------",
316 cs);
317 ESP_LOGW(TAG, "[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
318 ESP_LOGW(TAG, "[CALIBRATION][%s] | | config | flash | config | flash |", cs);
319 ESP_LOGW(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------------------",
320 cs);
321 for (uint8_t phase = 0; phase < 3; ++phase) {
322 ESP_LOGW(TAG, "[CALIBRATION][%s] | %c | %6d | %6d | %6d | %6d |", cs, 'A' + phase,
323 this->config_offset_phase_[phase].first_offset, this->offset_phase_[phase].first_offset,
324 this->config_offset_phase_[phase].second_offset, this->offset_phase_[phase].second_offset);
325 }
326 ESP_LOGW(TAG,
327 "[CALIBRATION][%s] ===============================================================================", cs);
328 }
329 if (power_mismatch) {
330 ESP_LOGW(TAG, "[CALIBRATION][%s] ", cs);
331 ESP_LOGW(TAG,
332 "[CALIBRATION][%s] ================= Power offset mismatch: using flash values =================", cs);
333 ESP_LOGW(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------------------",
334 cs);
335 ESP_LOGW(TAG, "[CALIBRATION][%s] | Phase | offset_active_power|offset_reactive_power|", cs);
336 ESP_LOGW(TAG, "[CALIBRATION][%s] | | config | flash | config | flash |", cs);
337 ESP_LOGW(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------------------",
338 cs);
339 for (uint8_t phase = 0; phase < 3; ++phase) {
340 ESP_LOGW(TAG, "[CALIBRATION][%s] | %c | %6d | %6d | %6d | %6d |", cs, 'A' + phase,
341 this->config_power_offset_phase_[phase].first_offset, this->power_offset_phase_[phase].first_offset,
342 this->config_power_offset_phase_[phase].second_offset, this->power_offset_phase_[phase].second_offset);
343 }
344 ESP_LOGW(TAG,
345 "[CALIBRATION][%s] ===============================================================================", cs);
346 }
347 if (gain_mismatch) {
348 ESP_LOGW(TAG, "[CALIBRATION][%s] ", cs);
349 ESP_LOGW(TAG,
350 "[CALIBRATION][%s] ====================== Gain mismatch: using flash values =====================", cs);
351 ESP_LOGW(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------------------",
352 cs);
353 ESP_LOGW(TAG, "[CALIBRATION][%s] | Phase | voltage_gain | current_gain |", cs);
354 ESP_LOGW(TAG, "[CALIBRATION][%s] | | config | flash | config | flash |", cs);
355 ESP_LOGW(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------------------",
356 cs);
357 for (uint8_t phase = 0; phase < 3; ++phase) {
358 ESP_LOGW(TAG, "[CALIBRATION][%s] | %c | %6u | %6u | %6u | %6u |", cs, 'A' + phase,
359 this->config_gain_phase_[phase].voltage_gain, this->gain_phase_[phase].voltage_gain,
360 this->config_gain_phase_[phase].current_gain, this->gain_phase_[phase].current_gain);
361 }
362 ESP_LOGW(TAG,
363 "[CALIBRATION][%s] ===============================================================================", cs);
364 }
365 if (!this->enable_offset_calibration_) {
366 ESP_LOGI(TAG, "[CALIBRATION][%s] Power & Voltage/Current offset calibration is disabled. Using config file values.",
367 cs);
368 } else if (this->restored_offset_calibration_ && !offset_mismatch) {
369 ESP_LOGI(TAG, "[CALIBRATION][%s] ", cs);
370 ESP_LOGI(TAG, "[CALIBRATION][%s] ============== Restored offset calibration from memory ==============", cs);
371 ESP_LOGI(TAG, "[CALIBRATION][%s] --------------------------------------------------------------", cs);
372 ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
373 ESP_LOGI(TAG, "[CALIBRATION][%s] --------------------------------------------------------------", cs);
374 for (uint8_t phase = 0; phase < 3; phase++) {
375 ESP_LOGI(TAG, "[CALIBRATION][%s] | %c | %6d | %6d |", cs, 'A' + phase,
376 this->offset_phase_[phase].first_offset, this->offset_phase_[phase].second_offset);
377 }
378 ESP_LOGI(TAG, "[CALIBRATION][%s] ==============================================================\\n", cs);
379 }
380
381 if (this->restored_power_offset_calibration_ && !power_mismatch) {
382 ESP_LOGI(TAG, "[CALIBRATION][%s] ", cs);
383 ESP_LOGI(TAG, "[CALIBRATION][%s] ============ Restored power offset calibration from memory ============", cs);
384 ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
385 ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
386 ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
387 for (uint8_t phase = 0; phase < 3; phase++) {
388 ESP_LOGI(TAG, "[CALIBRATION][%s] | %c | %6d | %6d |", cs, 'A' + phase,
389 this->power_offset_phase_[phase].first_offset, this->power_offset_phase_[phase].second_offset);
390 }
391 ESP_LOGI(TAG, "[CALIBRATION][%s] =====================================================================\n", cs);
392 }
393 if (!this->enable_gain_calibration_) {
394 ESP_LOGI(TAG, "[CALIBRATION][%s] Gain calibration is disabled. Using config file values.", cs);
395 } else if (this->restored_gain_calibration_ && !gain_mismatch) {
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,
403 this->gain_phase_[phase].voltage_gain, this->gain_phase_[phase].current_gain);
404 }
405 ESP_LOGI(TAG, "[CALIBRATION][%s] =====================================================================\\n", cs);
406 ESP_LOGI(TAG, "[CALIBRATION][%s] Gain calibration loaded and verified successfully.\n", cs);
407 }
408 this->calibration_message_printed_ = true;
409}
410
412 ESP_LOGCONFIG("", "ATM90E32:");
413 LOG_PIN(" CS Pin: ", this->cs_);
414 if (this->is_failed()) {
415 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
416 }
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_);
451 LOG_SENSOR(" ", "Frequency", this->freq_sensor_);
452 LOG_SENSOR(" ", "Chip Temp", this->chip_temperature_sensor_);
456 }
457}
458
460
461// R/C registers can conly be cleared after the LastSPIData register is updated (register 78H)
462// Peakdetect period: 05H. Bit 15:8 are PeakDet_period in ms. 7:0 are Sag_period
463// Default is 143FH (20ms, 63ms)
464uint16_t ATM90E32Component::read16_(uint16_t a_register) {
465 this->enable();
466 delay_microseconds_safe(1); // min delay between CS low and first SCK is 200ns - 1us is plenty
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};
470 this->transfer_array(data, 4);
471 uint16_t output = encode_uint16(data[2], data[3]);
472 ESP_LOGVV(TAG, "read16_ 0x%04" PRIX16 " output 0x%04" PRIX16, a_register, output);
473 delay_microseconds_safe(1); // allow the last clock to propagate before releasing CS
474 this->disable();
475 delay_microseconds_safe(1); // meet minimum CS high time before next transaction
476 return output;
477}
478
479int ATM90E32Component::read32_(uint16_t addr_h, uint16_t addr_l) {
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;
483
484 ESP_LOGVV(TAG,
485 "read32_ addr_h 0x%04" PRIX16 " val_h 0x%04" PRIX16 " addr_l 0x%04" PRIX16 " val_l 0x%04" PRIX16
486 " = %" PRId32,
487 addr_h, val_h, addr_l, val_l, val);
488
489 return val;
490}
491
492void ATM90E32Component::write16_(uint16_t a_register, uint16_t val, bool validate) {
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)};
497 this->enable();
498 delay_microseconds_safe(1); // ensure CS setup time
499 this->write_array(data, 4);
500 delay_microseconds_safe(1); // allow clock to settle before raising CS
501 this->disable();
502 delay_microseconds_safe(1); // ensure minimum CS high time
503 if (validate)
504 this->validate_spi_read_(val, "write16()");
505}
506
507float ATM90E32Component::get_local_phase_voltage_(uint8_t phase) { return this->phase_[phase].voltage_; }
508
509float ATM90E32Component::get_local_phase_current_(uint8_t phase) { return this->phase_[phase].current_; }
510
511float ATM90E32Component::get_local_phase_active_power_(uint8_t phase) { return this->phase_[phase].active_power_; }
512
514
516
517float ATM90E32Component::get_local_phase_power_factor_(uint8_t phase) { return this->phase_[phase].power_factor_; }
518
522
526
527float ATM90E32Component::get_local_phase_angle_(uint8_t phase) { return this->phase_[phase].phase_angle_; }
528
532
533float ATM90E32Component::get_local_phase_peak_current_(uint8_t phase) { return this->phase_[phase].peak_current_; }
534
536 const uint16_t voltage = this->read16_(ATM90E32_REGISTER_URMS + phase);
537 this->validate_spi_read_(voltage, "get_phase_voltage()");
538 return (float) voltage / 100;
539}
540
542 const uint8_t reads = 10;
543 uint32_t accumulation = 0;
544 uint16_t voltage = 0;
545 for (uint8_t i = 0; i < reads; i++) {
546 voltage = this->read16_(ATM90E32_REGISTER_URMS + phase);
547 this->validate_spi_read_(voltage, "get_phase_voltage_avg_()");
548 accumulation += voltage;
549 }
550 voltage = accumulation / reads;
551 this->phase_[phase].voltage_ = (float) voltage / 100;
552 return this->phase_[phase].voltage_;
553}
554
556 const uint8_t reads = 10;
557 uint32_t accumulation = 0;
558 uint16_t current = 0;
559 for (uint8_t i = 0; i < reads; i++) {
560 current = this->read16_(ATM90E32_REGISTER_IRMS + phase);
561 this->validate_spi_read_(current, "get_phase_current_avg_()");
562 accumulation += current;
563 }
564 current = accumulation / reads;
565 this->phase_[phase].current_ = (float) current / 1000;
566 return this->phase_[phase].current_;
567}
568
570 const uint16_t current = this->read16_(ATM90E32_REGISTER_IRMS + phase);
571 this->validate_spi_read_(current, "get_phase_current_()");
572 return (float) current / 1000;
573}
574
576 const int val = this->read32_(ATM90E32_REGISTER_PMEAN + phase, ATM90E32_REGISTER_PMEANLSB + phase);
577 return val * 0.00032f;
578}
579
581 const int val = this->read32_(ATM90E32_REGISTER_QMEAN + phase, ATM90E32_REGISTER_QMEANLSB + phase);
582 return val * 0.00032f;
583}
584
586 const int val = this->read32_(ATM90E32_REGISTER_SMEAN + phase, ATM90E32_REGISTER_SMEANLSB + phase);
587 return val * 0.00032f;
588}
589
591 uint16_t powerfactor = this->read16_(ATM90E32_REGISTER_PFMEAN + phase); // unsigned to compare to lastspidata
592 this->validate_spi_read_(powerfactor, "get_phase_power_factor_()");
593 return (float) ((int16_t) powerfactor) / 1000; // make it signed again
594}
595
597 const uint16_t val = this->read16_(ATM90E32_REGISTER_APENERGY + phase);
598 if ((UINT32_MAX - this->phase_[phase].cumulative_forward_active_energy_) > val) {
600 } else {
602 }
603 // 0.01CF resolution = 0.003125 Wh per count
604 return ((float) this->phase_[phase].cumulative_forward_active_energy_ * (10.0f / 3200.0f));
605}
606
608 const uint16_t val = this->read16_(ATM90E32_REGISTER_ANENERGY + phase);
609 if (UINT32_MAX - this->phase_[phase].cumulative_reverse_active_energy_ > val) {
611 } else {
613 }
614 // 0.01CF resolution = 0.003125 Wh per count
615 return ((float) this->phase_[phase].cumulative_reverse_active_energy_ * (10.0f / 3200.0f));
616}
617
619 int val = this->read32_(ATM90E32_REGISTER_PMEANH + phase, ATM90E32_REGISTER_PMEANHLSB + phase);
620 return val * 0.00032f;
621}
622
624 float val = this->read16_(ATM90E32_REGISTER_PANGLE + phase) / 10.0f;
625 return (val > 180.0f) ? val - 360.0f : val;
626}
627
629 int16_t val = (float) this->read16_(ATM90E32_REGISTER_IPEAK + phase);
630 if (!this->peak_current_signed_)
631 val = std::abs(val);
632 // phase register * phase current gain value / 1000 * 2^13
633 return (val * this->phase_[phase].ct_gain_ / 8192000.0);
634}
635
637 const uint16_t freq = this->read16_(ATM90E32_REGISTER_FREQ);
638 return (float) freq / 100;
639}
640
642 const uint16_t ctemp = this->read16_(ATM90E32_REGISTER_TEMP);
643 return (float) ctemp;
644}
645
647 const char *cs = this->get_calibration_id_();
648 if (!this->enable_gain_calibration_) {
649 ESP_LOGW(TAG, "[CALIBRATION][%s] Gain calibration is disabled! Enable it first with enable_gain_calibration: true",
650 cs);
651 return;
652 }
653
654 float ref_voltages[3] = {
655 this->get_reference_voltage(0),
656 this->get_reference_voltage(1),
657 this->get_reference_voltage(2),
658 };
659 float ref_currents[3] = {this->get_reference_current(0), this->get_reference_current(1),
660 this->get_reference_current(2)};
661
662 ESP_LOGI(TAG, "[CALIBRATION][%s] ", cs);
663 ESP_LOGI(TAG, "[CALIBRATION][%s] ========================= Gain Calibration =========================", cs);
664 ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
665 ESP_LOGI(
666 TAG,
667 "[CALIBRATION][%s] | Phase | V_meas (V) | I_meas (A) | V_ref | I_ref | V_gain (old→new) | I_gain (old→new) |",
668 cs);
669 ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
670
671 for (uint8_t phase = 0; phase < 3; phase++) {
672 float measured_voltage = this->get_phase_voltage_avg_(phase);
673 float measured_current = this->get_phase_current_avg_(phase);
674
675 float ref_voltage = ref_voltages[phase];
676 float ref_current = ref_currents[phase];
677
678 uint16_t current_voltage_gain = this->read16_(voltage_gain_registers[phase]);
679 uint16_t current_current_gain = this->read16_(current_gain_registers[phase]);
680
681 bool did_voltage = false;
682 bool did_current = false;
683
684 // Voltage calibration
685 if (ref_voltage <= 0.0f) {
686 ESP_LOGW(TAG, "[CALIBRATION][%s] Phase %s - Skipping voltage calibration: reference voltage is 0.", cs,
687 phase_labels[phase]);
688 } else if (measured_voltage == 0.0f) {
689 ESP_LOGW(TAG, "[CALIBRATION][%s] Phase %s - Skipping voltage calibration: measured voltage is 0.", cs,
690 phase_labels[phase]);
691 } else {
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,
695 phase_labels[phase]);
696 } else {
697 if (new_voltage_gain >= 65535) {
698 ESP_LOGW(TAG,
699 "[CALIBRATION][%s] Phase %s - Voltage gain exceeds 65535. You may need a higher output voltage "
700 "transformer.",
701 cs, phase_labels[phase]);
702 new_voltage_gain = 65535;
703 }
704 this->gain_phase_[phase].voltage_gain = static_cast<uint16_t>(new_voltage_gain);
705 did_voltage = true;
706 }
707 }
708
709 // Current calibration
710 if (ref_current == 0.0f) {
711 ESP_LOGW(TAG, "[CALIBRATION][%s] Phase %s - Skipping current calibration: reference current is 0.", cs,
712 phase_labels[phase]);
713 } else if (measured_current == 0.0f) {
714 ESP_LOGW(TAG, "[CALIBRATION][%s] Phase %s - Skipping current calibration: measured current is 0.", cs,
715 phase_labels[phase]);
716 } else {
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,
720 phase_labels[phase]);
721 } else {
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.",
724 cs, phase_labels[phase]);
725 new_current_gain = 65535;
726 }
727 this->gain_phase_[phase].current_gain = static_cast<uint16_t>(new_current_gain);
728 did_current = true;
729 }
730 }
731
732 // Final row output
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);
737 }
738
739 ESP_LOGI(TAG, "[CALIBRATION][%s] =====================================================================\n", cs);
740
743 this->verify_gain_writes_();
744}
745
747 const char *cs = this->get_calibration_id_();
748 bool success = this->gain_calibration_pref_.save(&this->gain_phase_);
750 if (success) {
751 this->using_saved_calibrations_ = true;
752 ESP_LOGI(TAG, "[CALIBRATION][%s] Gain calibration saved to memory.", cs);
753 } else {
754 this->using_saved_calibrations_ = false;
755 ESP_LOGE(TAG, "[CALIBRATION][%s] Failed to save gain calibration to memory!", cs);
756 }
757}
758
759void ATM90E32Component::finish_offset_calibration_(const OffsetCalibration (&previous)[3], bool previous_restored,
760 bool previous_using_saved, OffsetCalibrationType type) {
762 const char *cs = this->get_calibration_id_();
763 const LogString *name = offset_calibration_name(power_offsets);
764 OffsetCalibration(*offsets)[3] = power_offsets ? &this->power_offset_phase_ : &this->offset_phase_;
765 ESPPreferenceObject *preference = power_offsets ? &this->power_offset_pref_ : &this->offset_pref_;
766 bool *has_stored =
768 bool *restored = power_offsets ? &this->restored_power_offset_calibration_ : &this->restored_offset_calibration_;
769 bool *mismatches = power_offsets ? this->power_offset_calibration_mismatch_ : this->offset_calibration_mismatch_;
770
771 const bool writes_verified = this->verify_offset_writes_(type);
772 bool saved = false;
773 bool synced = false;
774 if (writes_verified) {
775 saved = preference->save(offsets);
776 synced = global_preferences->sync();
777 }
778
779 if (writes_verified && saved && synced) {
780 this->using_saved_calibrations_ = true;
781 *has_stored = true;
782 *restored = true;
783 for (uint8_t phase = 0; phase < 3; phase++)
784 mismatches[phase] = false;
785 ESP_LOGI(TAG, "[CALIBRATION][%s] %s calibration saved to memory. %s calibration completed and verified.", cs,
786 LOG_STR_ARG(name), LOG_STR_ARG(name));
787 return;
788 }
789
790 if (writes_verified) {
791 ESP_LOGE(TAG, "[CALIBRATION][%s] Failed to save %s calibration to memory!", cs, LOG_STR_ARG(name));
792 }
793
794 for (uint8_t phase = 0; phase < 3; phase++) {
795 this->write_offsets_to_registers_(phase, previous[phase].first_offset, previous[phase].second_offset, type);
796 }
797 const bool rollback_verified = this->verify_offset_writes_(type);
798
799 bool rollback_persisted = false;
800 if (writes_verified) {
801 OffsetCalibration rollback[3]{};
802 prepare_offset_rollback(previous, previous_restored, rollback);
803 const bool rollback_saved = preference->save(&rollback);
804 const bool rollback_synced = global_preferences->sync();
805 rollback_persisted = rollback_saved && rollback_synced;
806 if (!rollback_saved || !rollback_synced) {
807 ESP_LOGE(TAG, "[CALIBRATION][%s] Failed to persist restored %s calibration values!", cs, LOG_STR_ARG(name));
808 }
809 }
810
811 *restored = previous_restored;
812 if (rollback_persisted)
813 *has_stored = previous_restored;
814 this->using_saved_calibrations_ = previous_using_saved;
815 if (!rollback_verified) {
816 ESP_LOGE(TAG, "[CALIBRATION][%s] %s calibration failed; rollback readback verification failed.", cs,
817 LOG_STR_ARG(name));
818 return;
819 }
820 ESP_LOGE(TAG, "[CALIBRATION][%s] %s calibration failed; previous values restored.", cs, LOG_STR_ARG(name));
821}
822
824 const char *cs = this->get_calibration_id_();
825 if (!this->enable_offset_calibration_) {
826 ESP_LOGW(TAG,
827 "[CALIBRATION][%s] Offset calibration is disabled! Enable it first with enable_offset_calibration: true",
828 cs);
829 return;
830 }
831
832 ESP_LOGI(TAG, "[CALIBRATION][%s] ", cs);
833 ESP_LOGI(TAG, "[CALIBRATION][%s] ======================== Offset Calibration ========================", cs);
834 ESP_LOGI(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------", cs);
835 ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
836 ESP_LOGI(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------", cs);
837
838 OffsetCalibration previous_offsets[3] = {this->offset_phase_[0], this->offset_phase_[1], this->offset_phase_[2]};
839 const bool previous_restored = this->restored_offset_calibration_;
840 const bool previous_using_saved = this->using_saved_calibrations_;
841
842 for (uint8_t phase = 0; phase < 3; phase++) {
843 int16_t voltage_offset = calibrate_offset(phase, true);
844 int16_t current_offset = calibrate_offset(phase, false);
845
846 this->write_offsets_to_registers_(phase, voltage_offset, current_offset,
848
849 ESP_LOGI(TAG, "[CALIBRATION][%s] | %c | %6d | %6d |", cs, 'A' + phase, voltage_offset,
850 current_offset);
851 }
852
853 ESP_LOGI(TAG, "[CALIBRATION][%s] ==================================================================\n", cs);
854
855 this->finish_offset_calibration_(previous_offsets, previous_restored, previous_using_saved,
857}
858
860 const char *cs = this->get_calibration_id_();
861 if (!this->enable_offset_calibration_) {
862 ESP_LOGW(
863 TAG,
864 "[CALIBRATION][%s] Offset power calibration is disabled! Enable it first with enable_offset_calibration: true",
865 cs);
866 return;
867 }
868
869 ESP_LOGI(TAG, "[CALIBRATION][%s] ", cs);
870 ESP_LOGI(TAG, "[CALIBRATION][%s] ===================== Power Offset Calibration =====================", cs);
871 ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
872 ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
873 ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
874
875 OffsetCalibration previous_offsets[3] = {this->power_offset_phase_[0], this->power_offset_phase_[1],
876 this->power_offset_phase_[2]};
877 const bool previous_restored = this->restored_power_offset_calibration_;
878 const bool previous_using_saved = this->using_saved_calibrations_;
879
880 for (uint8_t phase = 0; phase < 3; ++phase) {
881 int16_t active_offset = calibrate_power_offset(phase, false);
882 int16_t reactive_offset = calibrate_power_offset(phase, true);
883
884 this->write_offsets_to_registers_(phase, active_offset, reactive_offset,
886
887 ESP_LOGI(TAG, "[CALIBRATION][%s] | %c | %6d | %6d |", cs, 'A' + phase, active_offset,
888 reactive_offset);
889 }
890 ESP_LOGI(TAG, "[CALIBRATION][%s] =====================================================================\n", cs);
891
892 this->finish_offset_calibration_(previous_offsets, previous_restored, previous_using_saved,
894}
895
897 this->write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA);
898
899 for (int phase = 0; phase < 3; phase++) {
900 this->write16_(voltage_gain_registers[phase], this->gain_phase_[phase].voltage_gain);
901 this->write16_(current_gain_registers[phase], this->gain_phase_[phase].current_gain);
902 }
903
904 this->write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x0000);
905}
906
907void ATM90E32Component::write_offsets_to_registers_(uint8_t phase, int16_t first_offset, int16_t second_offset,
910 OffsetCalibration &offsets = power_offsets ? this->power_offset_phase_[phase] : this->offset_phase_[phase];
911 offsets.first_offset = first_offset;
912 offsets.second_offset = second_offset;
913 if (power_offsets) {
914 this->phase_[phase].active_power_offset_ = first_offset;
915 this->phase_[phase].reactive_power_offset_ = second_offset;
916 } else {
917 this->phase_[phase].voltage_offset_ = first_offset;
918 this->phase_[phase].current_offset_ = second_offset;
919 }
920
921 const uint16_t *first_registers = power_offsets ? this->power_offset_registers : this->voltage_offset_registers;
922 const uint16_t *second_registers =
923 power_offsets ? this->reactive_power_offset_registers : this->current_offset_registers;
924 this->write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA);
925 this->write16_(first_registers[phase], static_cast<uint16_t>(first_offset));
926 this->write16_(second_registers[phase], static_cast<uint16_t>(second_offset));
927 this->write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x0000);
928}
929
931 const char *cs = this->get_calibration_id_();
932 for (uint8_t i = 0; i < 3; ++i) {
935 this->gain_phase_[i] = this->config_gain_phase_[i];
936 }
937
938 bool have_data = this->gain_calibration_pref_.load(&this->gain_phase_);
939
940 if (have_data) {
941 bool all_zero = true;
942 bool same_as_config = true;
943 for (uint8_t phase = 0; phase < 3; ++phase) {
944 const auto &cfg = this->config_gain_phase_[phase];
945 const auto &saved = this->gain_phase_[phase];
946 if (saved.voltage_gain != 0 || saved.current_gain != 0)
947 all_zero = false;
948 if (saved.voltage_gain != cfg.voltage_gain || saved.current_gain != cfg.current_gain)
949 same_as_config = false;
950 }
951
952 if (!all_zero && !same_as_config) {
953 for (uint8_t phase = 0; phase < 3; ++phase) {
954 bool mismatch = false;
955 if (this->has_config_voltage_gain_[phase] &&
956 this->gain_phase_[phase].voltage_gain != this->config_gain_phase_[phase].voltage_gain)
957 mismatch = true;
958 if (this->has_config_current_gain_[phase] &&
959 this->gain_phase_[phase].current_gain != this->config_gain_phase_[phase].current_gain)
960 mismatch = true;
961 if (mismatch)
962 this->gain_calibration_mismatch_[phase] = true;
963 }
964
966
967 if (this->verify_gain_writes_()) {
968 this->using_saved_calibrations_ = true;
969 this->restored_gain_calibration_ = true;
970 return;
971 }
972
973 this->using_saved_calibrations_ = false;
974 ESP_LOGE(TAG, "[CALIBRATION][%s] Gain verification failed! Calibration may not be applied correctly.", cs);
975 }
976 }
977
978 this->using_saved_calibrations_ = false;
979 for (uint8_t i = 0; i < 3; ++i)
980 this->gain_phase_[i] = this->config_gain_phase_[i];
982
983 ESP_LOGW(TAG, "[CALIBRATION][%s] No stored gain calibrations found. Using config file values.", cs);
984}
985
988 const char *cs = this->get_calibration_id_();
989 const LogString *name = power_offsets ? LOG_STR("power offset") : LOG_STR("offset");
990 OffsetCalibration(*offsets)[3] = power_offsets ? &this->power_offset_phase_ : &this->offset_phase_;
991 OffsetCalibration(*config_offsets)[3] =
992 power_offsets ? &this->config_power_offset_phase_ : &this->config_offset_phase_;
993 ESPPreferenceObject *preference = power_offsets ? &this->power_offset_pref_ : &this->offset_pref_;
994 bool *has_stored =
996 bool *restored = power_offsets ? &this->restored_power_offset_calibration_ : &this->restored_offset_calibration_;
997 bool *mismatches = power_offsets ? this->power_offset_calibration_mismatch_ : this->offset_calibration_mismatch_;
998 const bool *has_first = power_offsets ? this->has_config_active_power_offset_ : this->has_config_voltage_offset_;
999 const bool *has_second = power_offsets ? this->has_config_reactive_power_offset_ : this->has_config_current_offset_;
1000
1001 for (uint8_t i = 0; i < 3; ++i)
1002 (*config_offsets)[i] = (*offsets)[i];
1003
1004 const bool have_data = preference->load(offsets);
1005 bool all_zero = true;
1006 if (have_data) {
1007 for (const auto &phase : *offsets) {
1008 if (phase.first_offset != 0 || phase.second_offset != 0) {
1009 all_zero = false;
1010 break;
1011 }
1012 }
1013 }
1014
1015 *has_stored = have_data && !all_zero;
1016 *restored = false;
1017 for (uint8_t phase = 0; phase < 3; phase++) {
1018 mismatches[phase] = false;
1019 if (*has_stored) {
1020 mismatches[phase] =
1021 (has_first[phase] && (*offsets)[phase].first_offset != (*config_offsets)[phase].first_offset) ||
1022 (has_second[phase] && (*offsets)[phase].second_offset != (*config_offsets)[phase].second_offset);
1023 }
1024 }
1025
1026 if (!*has_stored) {
1027 for (uint8_t phase = 0; phase < 3; phase++)
1028 (*offsets)[phase] = (*config_offsets)[phase];
1029 ESP_LOGW(TAG, "[CALIBRATION][%s] No stored %s calibrations found. Using default values.", cs, LOG_STR_ARG(name));
1030 }
1031
1032 for (uint8_t phase = 0; phase < 3; phase++) {
1033 this->write_offsets_to_registers_(phase, (*offsets)[phase].first_offset, (*offsets)[phase].second_offset, type);
1034 }
1035 const bool initial_values_verified = this->verify_offset_writes_(type);
1036 if (initial_values_verified) {
1037 const auto state = resolve_offset_restore_state(*has_stored, true, false);
1038 *restored = state.restored;
1039 ESP_LOGI(TAG, "[CALIBRATION][%s] %s calibration values verified.", cs, LOG_STR_ARG(name));
1040 return;
1041 }
1042
1043 this->using_saved_calibrations_ = false;
1044 for (uint8_t phase = 0; phase < 3; phase++)
1045 mismatches[phase] = false;
1046 for (uint8_t phase = 0; phase < 3; phase++) {
1047 (*offsets)[phase] = (*config_offsets)[phase];
1048 this->write_offsets_to_registers_(phase, (*offsets)[phase].first_offset, (*offsets)[phase].second_offset, type);
1049 }
1050 const auto state = resolve_offset_restore_state(*has_stored, false, this->verify_offset_writes_(type));
1051 *restored = state.restored;
1052 if (state.values_verified) {
1053 ESP_LOGE(TAG, "[CALIBRATION][%s] %s calibration restore failed verification; config values verified.", cs,
1054 LOG_STR_ARG(name));
1055 } else {
1056 ESP_LOGE(TAG, "[CALIBRATION][%s] %s calibration restore and config fallback both failed verification.", cs,
1057 LOG_STR_ARG(name));
1058 }
1059}
1060
1062 const char *cs = this->get_calibration_id_();
1063 if (!this->using_saved_calibrations_) {
1064 ESP_LOGI(TAG, "[CALIBRATION][%s] No stored gain calibrations to clear. Current values:", cs);
1065 ESP_LOGI(TAG, "[CALIBRATION][%s] ----------------------------------------------------------", cs);
1066 ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | voltage_gain | current_gain |", cs);
1067 ESP_LOGI(TAG, "[CALIBRATION][%s] ----------------------------------------------------------", cs);
1068 for (int phase = 0; phase < 3; phase++) {
1069 ESP_LOGI(TAG, "[CALIBRATION][%s] | %c | %6u | %6u |", cs, 'A' + phase,
1070 this->gain_phase_[phase].voltage_gain, this->gain_phase_[phase].current_gain);
1071 }
1072 ESP_LOGI(TAG, "[CALIBRATION][%s] ==========================================================\n", cs);
1073 return;
1074 }
1075
1076 ESP_LOGI(TAG, "[CALIBRATION][%s] Clearing stored gain calibrations and restoring config-defined values", cs);
1077 ESP_LOGI(TAG, "[CALIBRATION][%s] ----------------------------------------------------------", cs);
1078 ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | voltage_gain | current_gain |", cs);
1079 ESP_LOGI(TAG, "[CALIBRATION][%s] ----------------------------------------------------------", cs);
1080
1081 for (int phase = 0; phase < 3; phase++) {
1082 uint16_t voltage_gain = this->phase_[phase].voltage_gain_;
1083 uint16_t current_gain = this->phase_[phase].ct_gain_;
1084
1085 this->config_gain_phase_[phase].voltage_gain = voltage_gain;
1086 this->config_gain_phase_[phase].current_gain = current_gain;
1087 this->gain_phase_[phase].voltage_gain = voltage_gain;
1088 this->gain_phase_[phase].current_gain = current_gain;
1089
1090 ESP_LOGI(TAG, "[CALIBRATION][%s] | %c | %6u | %6u |", cs, 'A' + phase, voltage_gain, current_gain);
1091 }
1092 ESP_LOGI(TAG, "[CALIBRATION][%s] ==========================================================\n", cs);
1093
1094 GainCalibration zero_gains[3]{{0, 0}, {0, 0}, {0, 0}};
1095 bool success = this->gain_calibration_pref_.save(&zero_gains);
1097
1098 this->using_saved_calibrations_ = false;
1099 this->restored_gain_calibration_ = false;
1100 for (bool &phase : this->gain_calibration_mismatch_)
1101 phase = false;
1102
1103 if (!success) {
1104 ESP_LOGE(TAG, "[CALIBRATION][%s] Failed to clear gain calibrations!", cs);
1105 }
1106
1107 this->write_gains_to_registers_(); // Apply them to the chip immediately
1108}
1109
1111 const char *cs = this->get_calibration_id_();
1112 if (!this->has_stored_offset_calibration_) {
1113 ESP_LOGI(TAG, "[CALIBRATION][%s] No stored offset calibrations to clear. Current values:", cs);
1114 ESP_LOGI(TAG, "[CALIBRATION][%s] --------------------------------------------------------------", cs);
1115 ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
1116 ESP_LOGI(TAG, "[CALIBRATION][%s] --------------------------------------------------------------", cs);
1117 for (uint8_t phase = 0; phase < 3; phase++) {
1118 ESP_LOGI(TAG, "[CALIBRATION][%s] | %c | %6d | %6d |", cs, 'A' + phase,
1119 this->offset_phase_[phase].first_offset, this->offset_phase_[phase].second_offset);
1120 }
1121 ESP_LOGI(TAG, "[CALIBRATION][%s] ==============================================================\n", cs);
1122 return;
1123 }
1124
1125 ESP_LOGI(TAG, "[CALIBRATION][%s] Clearing stored offset calibrations and restoring config-defined values", cs);
1126 ESP_LOGI(TAG, "[CALIBRATION][%s] --------------------------------------------------------------", cs);
1127 ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
1128 ESP_LOGI(TAG, "[CALIBRATION][%s] --------------------------------------------------------------", cs);
1129
1130 for (uint8_t phase = 0; phase < 3; phase++) {
1131 int16_t voltage_offset =
1132 this->has_config_voltage_offset_[phase] ? this->config_offset_phase_[phase].first_offset : 0;
1133 int16_t current_offset =
1134 this->has_config_current_offset_[phase] ? this->config_offset_phase_[phase].second_offset : 0;
1135 this->write_offsets_to_registers_(phase, voltage_offset, current_offset,
1137 ESP_LOGI(TAG, "[CALIBRATION][%s] | %c | %6d | %6d |", cs, 'A' + phase, voltage_offset,
1138 current_offset);
1139 }
1140 ESP_LOGI(TAG, "[CALIBRATION][%s] ==============================================================\n", cs);
1141
1142 OffsetCalibration zero_offsets[3]{{0, 0}, {0, 0}, {0, 0}};
1143 this->offset_pref_.save(&zero_offsets); // Clear stored values in flash
1145
1146 this->has_stored_offset_calibration_ = false;
1147 this->restored_offset_calibration_ = false;
1148 for (bool &phase : this->offset_calibration_mismatch_)
1149 phase = false;
1150
1151 ESP_LOGI(TAG, "[CALIBRATION][%s] Offsets cleared.", cs);
1152}
1153
1155 const char *cs = this->get_calibration_id_();
1157 ESP_LOGI(TAG, "[CALIBRATION][%s] No stored power offsets to clear. Current values:", cs);
1158 ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
1159 ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
1160 ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
1161 for (uint8_t phase = 0; phase < 3; phase++) {
1162 ESP_LOGI(TAG, "[CALIBRATION][%s] | %c | %6d | %6d |", cs, 'A' + phase,
1163 this->power_offset_phase_[phase].first_offset, this->power_offset_phase_[phase].second_offset);
1164 }
1165 ESP_LOGI(TAG, "[CALIBRATION][%s] =====================================================================\n", cs);
1166 return;
1167 }
1168
1169 ESP_LOGI(TAG, "[CALIBRATION][%s] Clearing stored power offsets and restoring config-defined values", cs);
1170 ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
1171 ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
1172 ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
1173
1174 for (uint8_t phase = 0; phase < 3; phase++) {
1175 int16_t active_offset =
1177 int16_t reactive_offset =
1179 this->write_offsets_to_registers_(phase, active_offset, reactive_offset,
1181 ESP_LOGI(TAG, "[CALIBRATION][%s] | %c | %6d | %6d |", cs, 'A' + phase, active_offset,
1182 reactive_offset);
1183 }
1184 ESP_LOGI(TAG, "[CALIBRATION][%s] =====================================================================\n", cs);
1185
1186 OffsetCalibration zero_power_offsets[3]{{0, 0}, {0, 0}, {0, 0}};
1187 this->power_offset_pref_.save(&zero_power_offsets);
1189
1192 for (bool &phase : this->power_offset_calibration_mismatch_)
1193 phase = false;
1194
1195 ESP_LOGI(TAG, "[CALIBRATION][%s] Power offsets cleared.", cs);
1196}
1197
1198int16_t ATM90E32Component::calibrate_offset(uint8_t phase, bool voltage) {
1199 const uint8_t num_reads = 5;
1200 uint64_t total_value = 0;
1201
1202 for (uint8_t i = 0; i < num_reads; ++i) {
1203 uint32_t reading = voltage ? this->read32_(ATM90E32_REGISTER_URMS + phase, ATM90E32_REGISTER_URMSLSB + phase)
1204 : this->read32_(ATM90E32_REGISTER_IRMS + phase, ATM90E32_REGISTER_IRMSLSB + phase);
1205 total_value += reading;
1206 }
1207
1208 const uint32_t average_value = total_value / num_reads;
1209 const uint32_t shifted = average_value >> 7;
1210 const uint32_t offset = ~shifted + 1;
1211 return static_cast<int16_t>(offset); // Takes lower 16 bits
1212}
1213
1214int16_t ATM90E32Component::calibrate_power_offset(uint8_t phase, bool reactive) {
1215 const uint8_t num_reads = 5;
1216 int64_t total_value = 0;
1217
1218 for (uint8_t i = 0; i < num_reads; ++i) {
1219 int32_t reading = reactive ? this->read32_(ATM90E32_REGISTER_QMEAN + phase, ATM90E32_REGISTER_QMEANLSB + phase)
1220 : this->read32_(ATM90E32_REGISTER_PMEAN + phase, ATM90E32_REGISTER_PMEANLSB + phase);
1221 total_value += reading;
1222 }
1223
1224 int32_t average_value = total_value / num_reads;
1225 int32_t power_offset = -average_value;
1226 return static_cast<int16_t>(power_offset); // Takes the lower 16 bits
1227}
1228
1230 const char *cs = this->get_calibration_id_();
1231 bool success = true;
1232 for (uint8_t phase = 0; phase < 3; phase++) {
1233 uint16_t read_voltage = this->read16_(voltage_gain_registers[phase]);
1234 uint16_t read_current = this->read16_(current_gain_registers[phase]);
1235
1236 if (read_voltage != this->gain_phase_[phase].voltage_gain ||
1237 read_current != this->gain_phase_[phase].current_gain) {
1238 ESP_LOGE(TAG, "[CALIBRATION][%s] Mismatch detected for Phase %s!", cs, phase_labels[phase]);
1239 success = false;
1240 }
1241 }
1242 return success; // Return true if all writes were successful, false otherwise
1243}
1244
1246 const bool power_offsets = type == OffsetCalibrationType::OFFSET_CALIBRATION_TYPE_POWER;
1247 const char *cs = this->get_calibration_id_();
1248 const LogString *name = offset_calibration_name(power_offsets);
1249 const LogString *first_name = power_offsets ? LOG_STR("active") : LOG_STR("voltage");
1250 const LogString *second_name = power_offsets ? LOG_STR("reactive") : LOG_STR("current");
1251 const OffsetCalibration *offsets = power_offsets ? this->power_offset_phase_ : this->offset_phase_;
1252 const uint16_t *first_registers = power_offsets ? this->power_offset_registers : this->voltage_offset_registers;
1253 const uint16_t *second_registers =
1254 power_offsets ? this->reactive_power_offset_registers : this->current_offset_registers;
1255 bool success = true;
1256 for (uint8_t phase = 0; phase < 3; phase++) {
1257 const uint16_t first = this->read16_(first_registers[phase]);
1258 const uint16_t second = this->read16_(second_registers[phase]);
1259 if (!offset_register_value_matches(first, offsets[phase].first_offset) ||
1260 !offset_register_value_matches(second, offsets[phase].second_offset)) {
1261 ESP_LOGE(TAG, "[CALIBRATION][%s] %s readback failed for Phase %s: %s %d/%d, %s %d/%d.", cs, LOG_STR_ARG(name),
1262 phase_labels[phase], LOG_STR_ARG(first_name), static_cast<int16_t>(first), offsets[phase].first_offset,
1263 LOG_STR_ARG(second_name), static_cast<int16_t>(second), offsets[phase].second_offset);
1264 success = false;
1265 }
1266 }
1267 return success;
1268}
1269
1270#ifdef USE_TEXT_SENSOR
1272 uint16_t state0 = this->read16_(ATM90E32_REGISTER_EMMSTATE0);
1273 uint16_t state1 = this->read16_(ATM90E32_REGISTER_EMMSTATE1);
1274
1275 for (int phase = 0; phase < 3; phase++) {
1276 std::string status;
1277
1278 if (state0 & over_voltage_flags[phase])
1279 status += "Over Voltage; ";
1280 if (state1 & voltage_sag_flags[phase])
1281 status += "Voltage Sag; ";
1282 if (state1 & phase_loss_flags[phase])
1283 status += "Phase Loss; ";
1284
1285 auto *sensor = this->phase_status_text_sensor_[phase];
1286 if (sensor == nullptr)
1287 continue;
1288
1289 if (!status.empty()) {
1290 status.pop_back(); // remove space
1291 status.pop_back(); // remove semicolon
1292 ESP_LOGW(TAG, "%s: %s", sensor->get_name().c_str(), status.c_str());
1293 sensor->publish_state(status);
1294 } else {
1295 sensor->publish_state("Okay");
1296 }
1297 }
1298}
1299
1301 uint16_t state1 = this->read16_(ATM90E32_REGISTER_EMMSTATE1);
1302
1303 std::string freq_status;
1304
1305 if (state1 & ATM90E32_STATUS_S1_FREQHIST) {
1306 freq_status = "HIGH";
1307 } else if (state1 & ATM90E32_STATUS_S1_FREQLOST) {
1308 freq_status = "LOW";
1309 } else {
1310 freq_status = "Normal";
1311 }
1312 if (this->freq_status_text_sensor_ != nullptr) {
1313 if (freq_status == "Normal") {
1314 ESP_LOGD(TAG, "Frequency status: %s", freq_status.c_str());
1315 } else {
1316 ESP_LOGW(TAG, "Frequency status: %s", freq_status.c_str());
1317 }
1318 this->freq_status_text_sensor_->publish_state(freq_status);
1319 }
1320}
1321
1323 constexpr float max_current_threshold = 65.53f;
1324
1325 for (uint8_t phase = 0; phase < 3; phase++) {
1326 float current_val =
1327 this->phase_[phase].current_sensor_ != nullptr ? this->phase_[phase].current_sensor_->state : 0.0f;
1328
1329 if (current_val > max_current_threshold) {
1330 ESP_LOGW(TAG, "Over current detected on Phase %c: %.2f A", 'A' + phase, current_val);
1331 ESP_LOGW(TAG, "You may need to half your gain_ct: value & multiply the current and power values by 2");
1332 if (this->phase_status_text_sensor_[phase] != nullptr) {
1333 this->phase_status_text_sensor_[phase]->publish_state("Over Current; ");
1334 }
1335 }
1336 }
1337}
1338#endif
1339
1340uint16_t ATM90E32Component::calculate_voltage_threshold(int line_freq, uint16_t ugain, float multiplier) {
1341 // this assumes that 60Hz electrical systems use 120V mains,
1342 // which is usually, but not always the case
1343 float nominal_voltage = (line_freq == 60) ? 120.0f : 220.0f;
1344 float target_voltage = nominal_voltage * multiplier;
1345
1346 float peak_01v = target_voltage * 100.0f * std::numbers::sqrt2_v<float>; // convert RMS → peak, scale to 0.01V
1347 float divider = (2.0f * ugain) / 32768.0f;
1348
1349 float threshold = peak_01v / divider;
1350
1351 return static_cast<uint16_t>(threshold);
1352}
1353
1354bool ATM90E32Component::validate_spi_read_(uint16_t expected, const char *context) {
1355 uint16_t last = this->read16_(ATM90E32_REGISTER_LASTSPIDATA);
1356 if (last != expected) {
1357 if (context != nullptr) {
1358 ESP_LOGW(TAG, "[%s] SPI read mismatch: expected 0x%04X, got 0x%04X", context, expected, last);
1359 } else {
1360 ESP_LOGW(TAG, "SPI read mismatch: expected 0x%04X, got 0x%04X", expected, last);
1361 }
1362 return false;
1363 }
1364 return true;
1365}
1366
1367} // namespace esphome::atm90e32
uint8_t status
Definition bl0942.h:8
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
void status_clear_warning()
Definition component.h:289
virtual size_t dump_summary(char *buffer, size_t len) const
Write a summary of this pin to the provided buffer.
Definition gpio.h:120
float get_local_phase_reactive_power_(uint8_t phase)
Definition atm90e32.cpp:513
float get_phase_forward_active_energy_(uint8_t phase)
Definition atm90e32.cpp:596
OffsetCalibration config_power_offset_phase_[3]
Definition atm90e32.h:259
float get_phase_current_avg_(uint8_t phase)
Definition atm90e32.cpp:555
float get_local_phase_apparent_power_(uint8_t phase)
Definition atm90e32.cpp:515
void write16_(uint16_t a_register, uint16_t val, bool validate=true)
Definition atm90e32.cpp:492
text_sensor::TextSensor * freq_status_text_sensor_
Definition atm90e32.h:282
ESPPreferenceObject power_offset_pref_
Definition atm90e32.h:276
const uint16_t voltage_gain_registers[3]
Definition atm90e32.h:59
void restore_offset_calibrations_(OffsetCalibrationType type)
Definition atm90e32.cpp:986
float get_phase_voltage_avg_(uint8_t phase)
Definition atm90e32.cpp:541
const uint16_t current_gain_registers[3]
Definition atm90e32.h:61
float get_reference_voltage(uint8_t phase)
Definition atm90e32.h:146
struct esphome::atm90e32::ATM90E32Component::GainCalibration gain_phase_[3]
const uint16_t current_offset_registers[3]
Definition atm90e32.h:65
static const uint8_t PHASEB
Definition atm90e32.h:55
float get_phase_reverse_active_energy_(uint8_t phase)
Definition atm90e32.cpp:607
float get_local_phase_harmonic_active_power_(uint8_t phase)
Definition atm90e32.cpp:529
float get_phase_angle_(uint8_t phase)
Definition atm90e32.cpp:623
float get_local_phase_current_(uint8_t phase)
Definition atm90e32.cpp:509
bool validate_spi_read_(uint16_t expected, const char *context=nullptr)
const uint16_t reactive_power_offset_registers[3]
Definition atm90e32.h:69
const uint16_t over_voltage_flags[3]
Definition atm90e32.h:71
float get_phase_voltage_(uint8_t phase)
Definition atm90e32.cpp:535
GainCalibration config_gain_phase_[3]
Definition atm90e32.h:266
int16_t calibrate_offset(uint8_t phase, bool voltage)
float get_local_phase_reverse_active_energy_(uint8_t phase)
Definition atm90e32.cpp:523
void finish_offset_calibration_(const OffsetCalibration(&previous)[3], bool previous_restored, bool previous_using_saved, OffsetCalibrationType type)
Definition atm90e32.cpp:759
float get_local_phase_forward_active_energy_(uint8_t phase)
Definition atm90e32.cpp:519
OffsetCalibration config_offset_phase_[3]
Definition atm90e32.h:257
uint16_t calculate_voltage_threshold(int line_freq, uint16_t ugain, float multiplier)
float get_local_phase_power_factor_(uint8_t phase)
Definition atm90e32.cpp:517
float get_phase_reactive_power_(uint8_t phase)
Definition atm90e32.cpp:580
const uint16_t phase_loss_flags[3]
Definition atm90e32.h:75
float get_phase_apparent_power_(uint8_t phase)
Definition atm90e32.cpp:585
float get_local_phase_voltage_(uint8_t phase)
Definition atm90e32.cpp:507
ESPPreferenceObject gain_calibration_pref_
Definition atm90e32.h:277
static const uint8_t PHASEA
Definition atm90e32.h:54
float get_reference_current(uint8_t phase)
Definition atm90e32.h:153
float get_phase_peak_current_(uint8_t phase)
Definition atm90e32.cpp:628
float get_phase_harmonic_active_power_(uint8_t phase)
Definition atm90e32.cpp:618
const uint16_t voltage_sag_flags[3]
Definition atm90e32.h:73
float get_phase_active_power_(uint8_t phase)
Definition atm90e32.cpp:575
void get_cs_summary_(std::span< char, GPIO_SUMMARY_MAX_LEN > buffer)
Definition atm90e32.cpp:134
static const uint8_t PHASEC
Definition atm90e32.h:56
const uint16_t power_offset_registers[3]
Definition atm90e32.h:67
float get_setup_priority() const override
Definition atm90e32.cpp:459
uint16_t read16_(uint16_t a_register)
Definition atm90e32.cpp:464
int read32_(uint16_t addr_h, uint16_t addr_l)
Definition atm90e32.cpp:479
sensor::Sensor * chip_temperature_sensor_
Definition atm90e32.h:284
void write_offsets_to_registers_(uint8_t phase, int16_t first_offset, int16_t second_offset, OffsetCalibrationType type)
Definition atm90e32.cpp:907
float get_phase_power_factor_(uint8_t phase)
Definition atm90e32.cpp:590
OffsetCalibration power_offset_phase_[3]
Definition atm90e32.h:258
float get_local_phase_angle_(uint8_t phase)
Definition atm90e32.cpp:527
const uint16_t voltage_offset_registers[3]
Definition atm90e32.h:63
OffsetCalibration offset_phase_[3]
Definition atm90e32.h:256
text_sensor::TextSensor * phase_status_text_sensor_[3]
Definition atm90e32.h:281
float get_phase_current_(uint8_t phase)
Definition atm90e32.cpp:569
float get_local_phase_peak_current_(uint8_t phase)
Definition atm90e32.cpp:533
void set_publish_interval_flag_(bool flag)
Definition atm90e32.h:207
int16_t calibrate_power_offset(uint8_t phase, bool reactive)
struct esphome::atm90e32::ATM90E32Component::ATM90E32Phase phase_[3]
bool verify_offset_writes_(OffsetCalibrationType type)
ESPPreferenceObject offset_pref_
Definition atm90e32.h:275
float get_local_phase_active_power_(uint8_t phase)
Definition atm90e32.cpp:511
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:138
void publish_state(const std::string &state)
Definition text_sensor.h:40
uint8_t second
uint16_t type
bool state
Definition fan.h:2
mopeka_std_values val[3]
OffsetRestoreState resolve_offset_restore_state(bool has_stored_values, bool initial_values_verified, bool fallback_values_verified)
Definition atm90e32.h:37
bool offset_register_value_matches(uint16_t actual, int16_t expected)
Definition atm90e32.h:16
void prepare_offset_rollback(const OffsetCalibration(&previous)[3], bool had_stored_values, OffsetCalibration(&rollback)[3])
Definition atm90e32.h:44
constexpr float IO
For components that represent GPIO pins like PCF8573.
Definition component.h:41
constexpr uint32_t fnv1_hash_extend(uint32_t hash, T value)
Extend a FNV-1 hash with an integer (hashes each byte).
Definition helpers.h:814
ESPPreferences * global_preferences
uint32_t fnv1_hash(const char *str)
Calculate a FNV-1 hash of str.
Definition helpers.cpp:160
void delay_microseconds_safe(uint32_t us)
Delay for the given amount of microseconds, possibly yielding to other processes during the wait.
Definition helpers.cpp:867
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:884
constexpr size_t GPIO_SUMMARY_MAX_LEN
Maximum buffer size for dump_summary output.
Definition gpio.h:11
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t
watchdog_hw scratch[0]
ESPPreferenceObject make_preference(size_t, uint32_t, bool)
Definition preferences.h:24
bool sync()
Commit pending writes to flash.
Definition preferences.h:33