ESPHome 2026.11.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
7ESPHOME_LOG_TAG(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(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
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(LOG_STR("Checksum mismatch"));
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(LOG_STR("Checksum mismatch"));
129 }
130}
131
133 ESP_LOGCONFIG("", "ATM90E26:");
134 LOG_PIN(" CS Pin: ", this->cs_);
135 LOG_UPDATE_INTERVAL(this);
136 LOG_SENSOR(" ", "Voltage A", this->voltage_sensor_);
137 LOG_SENSOR(" ", "Current A", this->current_sensor_);
138 LOG_SENSOR(" ", "Power A", this->power_sensor_);
139 LOG_SENSOR(" ", "Reactive Power A", this->reactive_power_sensor_);
140 LOG_SENSOR(" ", "PF A", this->power_factor_sensor_);
141 LOG_SENSOR(" ", "Active Forward Energy A", this->forward_active_energy_sensor_);
142 LOG_SENSOR(" ", "Active Reverse Energy A", this->reverse_active_energy_sensor_);
143 LOG_SENSOR(" ", "Frequency", this->freq_sensor_);
144}
145
146uint16_t ATM90E26Component::read16_(uint8_t a_register) {
147 uint8_t data[2];
148 uint16_t output;
149
150 this->enable();
152 this->write_byte(a_register | 0x80);
154 this->read_array(data, 2);
155 this->disable();
156
157 output = (uint16_t(data[0] & 0xFF) << 8) | (data[1] & 0xFF);
158 ESP_LOGVV(TAG, "read16_ 0x%04X output 0x%04X", a_register, output);
159 return output;
160}
161
162void ATM90E26Component::write16_(uint8_t a_register, uint16_t val) {
163 ESP_LOGVV(TAG, "write16_ 0x%04X val 0x%04X", a_register, val);
164 this->enable();
166 this->write_byte(a_register & 0x7F);
168 this->write_byte((val >> 8) & 0xFF);
169 this->write_byte(val & 0xFF);
170 this->disable();
171}
172
174 const uint16_t current = this->read16_(ATM90E26_REGISTER_IRMS);
175 return current / 1000.0f;
176}
177
179 const uint16_t voltage = this->read16_(ATM90E26_REGISTER_URMS);
180 return voltage / 100.0f;
181}
182
184 const int16_t val = this->read16_(ATM90E26_REGISTER_PMEAN); // two's complement
185 return (float) val;
186}
187
189 const int16_t val = this->read16_(ATM90E26_REGISTER_QMEAN); // two's complement
190 return (float) val;
191}
192
194 const uint16_t val = this->read16_(ATM90E26_REGISTER_POWERF); // signed
195 if (val & 0x8000) {
196 return -(val & 0x7FFF) / 1000.0f;
197 } else {
198 return val / 1000.0f;
199 }
200}
201
203 const uint16_t val = this->read16_(ATM90E26_REGISTER_APENERGY);
204 if ((UINT32_MAX - this->cumulative_forward_active_energy_) > val) {
206 } else {
208 }
209 // The register holds thenths of pulses, we want to output Wh
210 return (this->cumulative_forward_active_energy_ * 100.0f / meter_constant_);
211}
212
214 const uint16_t val = this->read16_(ATM90E26_REGISTER_ANENERGY);
215 if (UINT32_MAX - this->cumulative_reverse_active_energy_ > val) {
217 } else {
219 }
220 return (this->cumulative_reverse_active_energy_ * 100.0f / meter_constant_);
221}
222
224 const uint16_t freq = this->read16_(ATM90E26_REGISTER_FREQ);
225 return freq / 100.0f;
226}
227
228} // namespace esphome::atm90e26
void mark_failed()
Mark this component as failed.
void status_clear_warning()
Definition component.h:296
sensor::Sensor * power_factor_sensor_
Definition atm90e26.h:54
uint16_t read16_(uint8_t a_register)
Definition atm90e26.cpp:146
void write16_(uint8_t a_register, uint16_t val)
Definition atm90e26.cpp:162
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:65
mopeka_std_values val[3]
ESPHOME_LOG_TAG(TAG, "atm90e26")
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition hal.cpp:48