ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
atm90e26.cpp
Go to the documentation of this file.
1#include "atm90e26.h"
2#include "atm90e26_reg.h"
3#include "esphome/core/log.h"
5
7
8ESPHOME_LOG_TAG(TAG, "atm90e26");
9
10void ATM90E26Component::update() {
11 if (this->read16_(ATM90E26_REGISTER_FUNCEN) != 0x0030) {
12 this->status_set_warning();
13 return;
14 }
15
16 PUBLISH_STATE(this->voltage_sensor_, this->get_line_voltage_());
17 PUBLISH_STATE(this->current_sensor_, this->get_line_current_());
18 PUBLISH_STATE(this->power_sensor_, this->get_active_power_());
19 PUBLISH_STATE(this->reactive_power_sensor_, this->get_reactive_power_());
20 PUBLISH_STATE(this->power_factor_sensor_, this->get_power_factor_());
21 PUBLISH_STATE(this->forward_active_energy_sensor_, this->get_forward_active_energy_());
22 PUBLISH_STATE(this->reverse_active_energy_sensor_, this->get_reverse_active_energy_());
23 PUBLISH_STATE(this->freq_sensor_, this->get_frequency_());
25}
26
28 this->spi_setup();
29
30 uint16_t mmode = 0x422; // default values for everything but L/N line current gains
31 mmode |= (gain_pga_ & 0x7) << 13;
32 mmode |= (n_line_gain_ & 0x3) << 11;
33
34 this->write16_(ATM90E26_REGISTER_SOFTRESET, 0x789A); // Perform soft reset
35 this->write16_(ATM90E26_REGISTER_FUNCEN,
36 0x0030); // Voltage sag irq=1, report on warnout pin=1, energy dir change irq=0
37 uint16_t read = this->read16_(ATM90E26_REGISTER_LASTDATA);
38 if (read != 0x0030) {
39 ESP_LOGW(TAG, "Could not initialize ATM90E26 IC, check SPI settings: %d", read);
40 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
41 return;
42 }
43 // TODO: 100 * <nominal voltage, e.g. 230> * sqrt(2) * <fraction of nominal, e.g. 0.9> / (4 * gain_voltage/32768)
44 this->write16_(ATM90E26_REGISTER_SAGTH, 0x17DD); // Voltage sag threshhold 0x1F2F
45
46 // Set metering calibration values
47 this->write16_(ATM90E26_REGISTER_CALSTART, 0x5678); // CAL Metering calibration startup command
48
49 // Configure
50 this->write16_(ATM90E26_REGISTER_MMODE, mmode); // Metering Mode Configuration (see above)
51
52 this->write16_(ATM90E26_REGISTER_PLCONSTH, (pl_const_ >> 16)); // PL Constant MSB
53 this->write16_(ATM90E26_REGISTER_PLCONSTL, pl_const_ & 0xFFFF); // PL Constant LSB
54
55 // Calibrate this to be 1 pulse per Wh
56 this->write16_(ATM90E26_REGISTER_LGAIN, gain_metering_); // L Line Calibration Gain (active power metering)
57 this->write16_(ATM90E26_REGISTER_LPHI, 0x0000); // L Line Calibration Angle
58 this->write16_(ATM90E26_REGISTER_NGAIN, 0x0000); // N Line Calibration Gain
59 this->write16_(ATM90E26_REGISTER_NPHI, 0x0000); // N Line Calibration Angle
60 this->write16_(ATM90E26_REGISTER_PSTARTTH, 0x08BD); // Active Startup Power Threshold (default) = 2237
61 this->write16_(ATM90E26_REGISTER_PNOLTH, 0x0000); // Active No-Load Power Threshold
62 this->write16_(ATM90E26_REGISTER_QSTARTTH, 0x0AEC); // Reactive Startup Power Threshold (default) = 2796
63 this->write16_(ATM90E26_REGISTER_QNOLTH, 0x0000); // Reactive No-Load Power Threshold
64
65 // Compute Checksum for the registers we set above
66 // low byte = sum of all bytes
67 uint16_t cs =
68 ((mmode >> 8) + (mmode & 0xFF) + (pl_const_ >> 24) + ((pl_const_ >> 16) & 0xFF) + ((pl_const_ >> 8) & 0xFF) +
69 (pl_const_ & 0xFF) + (gain_metering_ >> 8) + (gain_metering_ & 0xFF) + 0x08 + 0xBD + 0x0A + 0xEC) &
70 0xFF;
71 // high byte = XOR of all bytes
72 cs |= ((mmode >> 8) ^ (mmode & 0xFF) ^ (pl_const_ >> 24) ^ ((pl_const_ >> 16) & 0xFF) ^ ((pl_const_ >> 8) & 0xFF) ^
73 (pl_const_ & 0xFF) ^ (gain_metering_ >> 8) ^ (gain_metering_ & 0xFF) ^ 0x08 ^ 0xBD ^ 0x0A ^ 0xEC)
74 << 8;
75
76 this->write16_(ATM90E26_REGISTER_CS1, cs);
77 ESP_LOGVV(TAG, "Set CS1 to: 0x%04X", cs);
78
79 // Set measurement calibration values
80 this->write16_(ATM90E26_REGISTER_ADJSTART, 0x5678); // Measurement calibration startup command, registers 31-3A
81 this->write16_(ATM90E26_REGISTER_UGAIN, gain_voltage_); // Voltage RMS gain
82 this->write16_(ATM90E26_REGISTER_IGAINL, gain_ct_); // L line current RMS gain
83 this->write16_(ATM90E26_REGISTER_IGAINN, 0x7530); // N Line Current RMS Gain
84 this->write16_(ATM90E26_REGISTER_UOFFSET, 0x0000); // Voltage Offset
85 this->write16_(ATM90E26_REGISTER_IOFFSETL, 0x0000); // L Line Current Offset
86 this->write16_(ATM90E26_REGISTER_IOFFSETN, 0x0000); // N Line Current Offse
87 this->write16_(ATM90E26_REGISTER_POFFSETL, 0x0000); // L Line Active Power Offset
88 this->write16_(ATM90E26_REGISTER_QOFFSETL, 0x0000); // L Line Reactive Power Offset
89 this->write16_(ATM90E26_REGISTER_POFFSETN, 0x0000); // N Line Active Power Offset
90 this->write16_(ATM90E26_REGISTER_QOFFSETN, 0x0000); // N Line Reactive Power Offset
91
92 // Compute Checksum for the registers we set above
93 cs = ((gain_voltage_ >> 8) + (gain_voltage_ & 0xFF) + (gain_ct_ >> 8) + (gain_ct_ & 0xFF) + 0x75 + 0x30) & 0xFF;
94 cs |= ((gain_voltage_ >> 8) ^ (gain_voltage_ & 0xFF) ^ (gain_ct_ >> 8) ^ (gain_ct_ & 0xFF) ^ 0x75 ^ 0x30) << 8;
95 this->write16_(ATM90E26_REGISTER_CS2, cs);
96 ESP_LOGVV(TAG, "Set CS2 to: 0x%04X", cs);
97
98 this->write16_(ATM90E26_REGISTER_CALSTART,
99 0x8765); // Checks correctness of 21-2B registers and starts normal metering if ok
100 this->write16_(ATM90E26_REGISTER_ADJSTART,
101 0x8765); // Checks correctness of 31-3A registers and starts normal measurement if ok
102
103 const uint16_t sys_status = this->read16_(ATM90E26_REGISTER_SYSSTATUS);
104 if (sys_status & 0xC000) { // Checksum 1 Error
105
106 ESP_LOGW(TAG, "Could not initialize ATM90E26 IC: CS1 was incorrect, expected: 0x%04X",
107 this->read16_(ATM90E26_REGISTER_CS1));
108 this->mark_failed(LOG_STR("Checksum mismatch"));
109 }
110 if (sys_status & 0x3000) { // Checksum 2 Error
111 ESP_LOGW(TAG, "Could not initialize ATM90E26 IC: CS2 was incorrect, expected: 0x%04X",
112 this->read16_(ATM90E26_REGISTER_CS2));
113 this->mark_failed(LOG_STR("Checksum mismatch"));
114 }
115}
116
117void ATM90E26Component::dump_config() {
118 ESP_LOGCONFIG("", "ATM90E26:");
119 LOG_PIN(" CS Pin: ", this->cs_);
120 LOG_UPDATE_INTERVAL(this);
121 LOG_SENSOR(" ", "Voltage A", this->voltage_sensor_);
122 LOG_SENSOR(" ", "Current A", this->current_sensor_);
123 LOG_SENSOR(" ", "Power A", this->power_sensor_);
124 LOG_SENSOR(" ", "Reactive Power A", this->reactive_power_sensor_);
125 LOG_SENSOR(" ", "PF A", this->power_factor_sensor_);
126 LOG_SENSOR(" ", "Active Forward Energy A", this->forward_active_energy_sensor_);
127 LOG_SENSOR(" ", "Active Reverse Energy A", this->reverse_active_energy_sensor_);
128 LOG_SENSOR(" ", "Frequency", this->freq_sensor_);
129}
130
131uint16_t ATM90E26Component::read16_(uint8_t a_register) {
132 uint8_t data[2];
133 uint16_t output;
134
135 this->enable();
137 this->write_byte(a_register | 0x80);
139 this->read_array(data, 2);
140 this->disable();
141
142 output = encode_uint16(data[0], data[1]);
143 ESP_LOGVV(TAG, "read16_ 0x%04X output 0x%04X", a_register, output);
144 return output;
145}
146
147void ATM90E26Component::write16_(uint8_t a_register, uint16_t val) {
148 ESP_LOGVV(TAG, "write16_ 0x%04X val 0x%04X", a_register, val);
149 this->enable();
151 this->write_byte(a_register & 0x7F);
153 this->write_byte((val >> 8) & 0xFF);
154 this->write_byte(val & 0xFF);
155 this->disable();
156}
157
159 const uint16_t current = this->read16_(ATM90E26_REGISTER_IRMS);
160 return current / 1000.0f;
161}
162
164 const uint16_t voltage = this->read16_(ATM90E26_REGISTER_URMS);
165 return voltage / 100.0f;
166}
167
169 const int16_t val = this->read16_(ATM90E26_REGISTER_PMEAN); // two's complement
170 return (float) val;
171}
172
174 const int16_t val = this->read16_(ATM90E26_REGISTER_QMEAN); // two's complement
175 return (float) val;
176}
177
179 const uint16_t val = this->read16_(ATM90E26_REGISTER_POWERF); // signed
180 if (val & 0x8000) {
181 return -(val & 0x7FFF) / 1000.0f;
182 } else {
183 return val / 1000.0f;
184 }
185}
186
188 const uint16_t val = this->read16_(ATM90E26_REGISTER_APENERGY);
189 if ((UINT32_MAX - this->cumulative_forward_active_energy_) > val) {
191 } else {
193 }
194 // The register holds thenths of pulses, we want to output Wh
195 return (this->cumulative_forward_active_energy_ * 100.0f / meter_constant_);
196}
197
199 const uint16_t val = this->read16_(ATM90E26_REGISTER_ANENERGY);
200 if (UINT32_MAX - this->cumulative_reverse_active_energy_ > val) {
202 } else {
204 }
205 return (this->cumulative_reverse_active_energy_ * 100.0f / meter_constant_);
206}
207
209 const uint16_t freq = this->read16_(ATM90E26_REGISTER_FREQ);
210 return freq / 100.0f;
211}
212
213} // namespace esphome::atm90e26
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
void status_clear_warning()
Definition component.h:309
uint16_t read16_(uint8_t a_register)
Definition atm90e26.cpp:131
void write16_(uint8_t a_register, uint16_t val)
Definition atm90e26.cpp:147
mopeka_std_values val[3]
ESPHOME_LOG_TAG(TAG, "atm90e26")
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition hal.cpp:48
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:1043