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