8#include <unordered_map>
21 return actual ==
static_cast<uint16_t
>(expected);
35static constexpr size_t CALIBRATION_PAYLOAD_SIZE = 12;
37static_assert(
sizeof(OffsetCalibration[3]) == CALIBRATION_PAYLOAD_SIZE,
38 "Offset calibration preference layout must remain compatible");
39static_assert(
sizeof(GainCalibration[3]) == CALIBRATION_PAYLOAD_SIZE,
40 "Gain calibration preference layout must remain compatible");
48 public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_HIGH,
49 spi::CLOCK_PHASE_TRAILING, spi::DATA_RATE_1MHZ> {
57 ATM90E32_REGISTER_UGAINC};
59 ATM90E32_REGISTER_IGAINC};
61 ATM90E32_REGISTER_UOFFSETC};
63 ATM90E32_REGISTER_IOFFSETC};
65 ATM90E32_REGISTER_POFFSETC};
67 ATM90E32_REGISTER_QOFFSETC};
68 const uint16_t
over_voltage_flags[3] = {ATM90E32_STATUS_S0_OVPHASEAST, ATM90E32_STATUS_S0_OVPHASEBST,
69 ATM90E32_STATUS_S0_OVPHASECST};
70 const uint16_t
voltage_sag_flags[3] = {ATM90E32_STATUS_S1_SAGPHASEAST, ATM90E32_STATUS_S1_SAGPHASEBST,
71 ATM90E32_STATUS_S1_SAGPHASECST};
72 const uint16_t
phase_loss_flags[3] = {ATM90E32_STATUS_S1_PHASELOSSAST, ATM90E32_STATUS_S1_PHASELOSSBST,
73 ATM90E32_STATUS_S1_PHASELOSSCST};
75 void setup()
override;
144 const LogString *hint =
nullptr,
const LogString *hint2 =
nullptr);
163#ifdef USE_TEXT_SENSOR
179 uint16_t
read16_(uint16_t a_register);
180 int read32_(uint16_t addr_h, uint16_t addr_l);
181 void write16_(uint16_t a_register, uint16_t
val,
bool validate =
true);
216 template<
typename T,
typename F>
218 const T (&values)[3],
bool writes_verified,
bool &has_stored,
bool &restored,
219 bool (&mismatches)[3], F &&rollback);
221 bool (&mismatches)[3]);
288#ifdef USE_TEXT_SENSOR
311#ifdef USE_TEXT_SENSOR
321 SUB_SENSOR(chip_temperature)
322#ifdef USE_TEXT_SENSOR
323 SUB_TEXT_SENSOR(freq_status)
This class simplifies creating components that periodically check a state.
bool enable_offset_calibration_
float threshold_voltage_peak_v_
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]
void run_gain_calibrations()
bool gain_calibration_mismatch_[3]
OffsetCalibration config_power_offset_phase_[3]
bool has_config_active_power_offset_[3]
float get_phase_current_avg_(uint8_t phase)
bool has_stored_power_offset_calibration_
float get_local_phase_apparent_power_(uint8_t phase)
void set_frequency_thresholds(uint16_t low, uint16_t high)
void write16_(uint16_t a_register, uint16_t val, bool validate=true)
void finish_gain_calibration_(const GainCalibration(&previous)[3])
bool has_config_reactive_power_offset_[3]
void set_apparent_power_sensor(int phase, sensor::Sensor *obj)
bool use_voltage_threshold_flags_
uint16_t frequency_low_threshold_
ESPPreferenceObject power_offset_pref_
const uint16_t voltage_gain_registers[3]
float threshold_current_peak_a_
void restore_offset_calibrations_(OffsetCalibrationType type)
float get_phase_voltage_avg_(uint8_t phase)
const uint16_t current_gain_registers[3]
void run_offset_calibrations()
float get_reference_voltage(uint8_t phase)
number::Number * ref_voltages_[3]
void set_power_sensor(int phase, sensor::Sensor *obj)
void set_current_offset(uint8_t phase, int16_t offset)
bool get_publish_interval_flag_()
void press_calibration_button(EntityBase *entity, void(ATM90E32Component::*action)(), const LogString *hint=nullptr, const LogString *hint2=nullptr)
const uint16_t current_offset_registers[3]
static const uint8_t PHASEB
float get_phase_reverse_active_energy_(uint8_t phase)
GainCalibration gain_phase_[3]
void set_reactive_power_offset(uint8_t phase, int16_t offset)
float get_local_phase_harmonic_active_power_(uint8_t phase)
void check_phase_status()
void set_line_freq(int freq)
bool has_stored_gain_calibration_
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 set_peak_current_sensor(int phase, sensor::Sensor *obj)
const uint16_t reactive_power_offset_registers[3]
const uint16_t over_voltage_flags[3]
void clear_power_offset_calibrations()
void set_reverse_active_energy_sensor(int phase, sensor::Sensor *obj)
bool has_stored_offset_calibration_
float get_phase_voltage_(uint8_t phase)
void set_current_sensor(int phase, sensor::Sensor *obj)
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 set_harmonic_active_power_sensor(int phase, sensor::Sensor *obj)
void restore_gain_calibrations_()
void set_current_phases(int phases)
float get_local_phase_forward_active_energy_(uint8_t phase)
OffsetCalibration config_offset_phase_[3]
bool peak_current_signed_
void set_active_power_offset(uint8_t phase, int16_t offset)
static uint16_t calculate_voltage_threshold_from_volts(float voltage_rms, uint16_t ugain)
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()
friend class ATM90E32ThresholdRegisters
friend class ATM90E32CalibrationTestAccess
uint16_t frequency_high_threshold_
void set_enable_gain_calibration(bool flag)
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_()
void set_threshold_current_peak_a(float current)
float get_local_phase_voltage_(uint8_t phase)
void set_phase_status_text_sensor(uint8_t phase, text_sensor::TextSensor *sensor)
void set_pga_gain(uint16_t gain)
void set_reference_current(uint8_t phase, number::Number *ref_current)
ESPPreferenceObject gain_calibration_pref_
void set_instance_id(const char *id)
void set_threshold_voltage_peak_v(float voltage)
static const uint8_t PHASEA
float get_reference_current(uint8_t phase)
number::Number * ref_currents_[3]
bool verify_gain_writes_()
float get_phase_peak_current_(uint8_t phase)
void set_voltage_offset(uint8_t phase, int16_t offset)
void finish_offset_calibration_(const OffsetCalibration(&previous)[3], OffsetCalibrationType type)
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)
void run_power_offset_calibrations()
const char * get_calibration_id_()
bool enable_gain_calibration_
void get_cs_summary_(std::span< char, GPIO_SUMMARY_MAX_LEN > buffer)
float threshold_voltage_sag_v_
bool use_current_threshold_flags_
static const uint8_t PHASEC
bool power_offset_calibration_mismatch_[3]
const uint16_t power_offset_registers[3]
float get_setup_priority() const override
uint16_t read16_(uint16_t a_register)
const char * instance_id_
int read32_(uint16_t addr_h, uint16_t addr_l)
void write_offsets_to_registers_(uint8_t phase, int16_t first_offset, int16_t second_offset, OffsetCalibrationType type)
void set_threshold_voltage_sag_v(float voltage)
float get_phase_power_factor_(uint8_t phase)
void set_voltage_sensor(int phase, sensor::Sensor *obj)
OffsetCalibration power_offset_phase_[3]
float get_local_phase_angle_(uint8_t phase)
const uint16_t voltage_offset_registers[3]
void set_ct_gain(int phase, uint16_t gain)
void set_phase_angle_sensor(int phase, sensor::Sensor *obj)
OffsetCalibration offset_phase_[3]
void set_reference_voltage(uint8_t phase, number::Number *ref_voltage)
bool has_config_voltage_offset_[3]
bool offset_calibration_mismatch_[3]
bool clear_calibration_pref_(ESPPreferenceObject &preference, bool &has_stored, bool &restored, bool(&mismatches)[3])
void set_power_factor_sensor(int phase, sensor::Sensor *obj)
void set_forward_active_energy_sensor(int phase, sensor::Sensor *obj)
void set_volt_gain(int phase, uint16_t gain)
bool publish_interval_flag_
static uint16_t calculate_current_threshold_from_amps(float current_rms, uint16_t igain)
text_sensor::TextSensor * phase_status_text_sensor_[3]
float get_phase_current_(uint8_t phase)
void set_peak_current_signed(bool flag)
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]
bool verify_offset_writes_(OffsetCalibrationType type)
const char * phase_labels[3]
void set_enable_offset_calibration(bool flag)
ESPPreferenceObject offset_pref_
void set_reactive_power_sensor(int phase, sensor::Sensor *obj)
void finish_calibration_(const LogString *name, const LogString *noun, ESPPreferenceObject &preference, const T(&values)[3], bool writes_verified, bool &has_stored, bool &restored, bool(&mismatches)[3], F &&rollback)
float get_local_phase_active_power_(uint8_t phase)
Base-class for all numbers.
Base-class for all sensors.
The SPIDevice is what components using the SPI will create.
bool offset_register_value_matches(uint16_t actual, int16_t expected)
@ OFFSET_CALIBRATION_TYPE_VOLTAGE_CURRENT
@ OFFSET_CALIBRATION_TYPE_POWER
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_