ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
ade7880.cpp
Go to the documentation of this file.
1// This component was developed using knowledge gathered by a number
2// of people who reverse-engineered the Shelly 3EM:
3//
4// @AndreKR on GitHub
5// Axel (@Axel830 on GitHub)
6// Marko (@goodkiller on GitHub)
7// Michaƫl Piron (@michaelpiron on GitHub)
8// Theo Arends (@arendst on GitHub)
9
10#include "ade7880.h"
11#include "ade7880_registers.h"
12#include "esphome/core/log.h"
13
14#include <cinttypes>
15
17
18static const char *const TAG = "ade7880";
19
20void IRAM_ATTR ADE7880Store::gpio_intr(ADE7880Store *arg) { arg->reset_done = true; }
21
23 if (this->irq0_pin_ != nullptr) {
24 this->irq0_pin_->setup();
25 }
26 this->irq1_pin_->setup();
27 if (this->reset_pin_ != nullptr) {
28 this->reset_pin_->setup();
29 }
30 this->store_.irq1_pin = this->irq1_pin_->to_isr();
32
33 // if IRQ1 is already asserted, the cause must be determined
34 if (this->irq1_pin_->digital_read() == 0) {
35 ESP_LOGD(TAG, "IRQ1 found asserted during setup()");
36 auto status1 = read_u32_register16_(STATUS1);
37 if ((status1 & ~STATUS1_RSTDONE) != 0) {
38 // not safe to proceed, must initiate reset
39 ESP_LOGD(TAG, "IRQ1 asserted for !RSTDONE, resetting device");
40 this->reset_device_();
41 return;
42 }
43 if ((status1 & STATUS1_RSTDONE) == STATUS1_RSTDONE) {
44 // safe to proceed, device has just completed reset cycle
45 ESP_LOGD(TAG, "Acknowledging RSTDONE");
46 this->write_u32_register16_(STATUS0, 0xFFFF);
47 this->write_u32_register16_(STATUS1, 0xFFFF);
48 this->init_device_();
49 return;
50 }
51 }
52
53 this->reset_device_();
54}
55
57 // check for completion of a reset cycle
58 if (!this->store_.reset_done) {
59 return;
60 }
61
62 ESP_LOGD(TAG, "Acknowledging RSTDONE");
63 this->write_u32_register16_(STATUS0, 0xFFFF);
64 this->write_u32_register16_(STATUS1, 0xFFFF);
65 this->init_device_();
66 this->store_.reset_done = false;
67 this->store_.reset_pending = false;
68}
69
70template<typename F>
71void ADE7880::update_sensor_from_s24zp_register16_(sensor::Sensor *sensor, uint16_t a_register, F &&f) {
72 if (sensor == nullptr) {
73 return;
74 }
75
76 float val = this->read_s24zp_register16_(a_register);
77 sensor->publish_state(f(val));
78}
79
80template<typename F>
81void ADE7880::update_sensor_from_s16_register16_(sensor::Sensor *sensor, uint16_t a_register, F &&f) {
82 if (sensor == nullptr) {
83 return;
84 }
85
86 float val = this->read_s16_register16_(a_register);
87 sensor->publish_state(f(val));
88}
89
90template<typename F>
91void ADE7880::update_sensor_from_s32_register16_(sensor::Sensor *sensor, uint16_t a_register, F &&f) {
92 if (sensor == nullptr) {
93 return;
94 }
95
96 float val = this->read_s32_register16_(a_register);
97 sensor->publish_state(f(val));
98}
99
101 if (this->store_.reset_pending) {
102 return;
103 }
104
105 auto start = millis();
106
107 if (this->channel_n_ != nullptr) {
108 auto *chan = this->channel_n_;
109 this->update_sensor_from_s24zp_register16_(chan->current, NIRMS, [](float val) { return val / 100000.0f; });
110 }
111
112 if (this->channel_a_ != nullptr) {
113 auto *chan = this->channel_a_;
114 this->update_sensor_from_s24zp_register16_(chan->current, AIRMS, [](float val) { return val / 100000.0f; });
115 this->update_sensor_from_s24zp_register16_(chan->voltage, AVRMS, [](float val) { return val / 10000.0f; });
116 this->update_sensor_from_s24zp_register16_(chan->active_power, AWATT, [](float val) { return val / 100.0f; });
117 this->update_sensor_from_s24zp_register16_(chan->apparent_power, AVA, [](float val) { return val / 100.0f; });
118 this->update_sensor_from_s16_register16_(chan->power_factor, APF,
119 [](float val) { return std::abs(val / -327.68f); });
120 this->update_sensor_from_s32_register16_(chan->forward_active_energy, AFWATTHR, [&chan](float val) {
121 return chan->forward_active_energy_total += val / 14400.0f;
122 });
123 this->update_sensor_from_s32_register16_(chan->reverse_active_energy, ARWATTHR, [&chan](float val) {
124 return chan->reverse_active_energy_total += val / 14400.0f;
125 });
126 }
127
128 if (this->channel_b_ != nullptr) {
129 auto *chan = this->channel_b_;
130 this->update_sensor_from_s24zp_register16_(chan->current, BIRMS, [](float val) { return val / 100000.0f; });
131 this->update_sensor_from_s24zp_register16_(chan->voltage, BVRMS, [](float val) { return val / 10000.0f; });
132 this->update_sensor_from_s24zp_register16_(chan->active_power, BWATT, [](float val) { return val / 100.0f; });
133 this->update_sensor_from_s24zp_register16_(chan->apparent_power, BVA, [](float val) { return val / 100.0f; });
134 this->update_sensor_from_s16_register16_(chan->power_factor, BPF,
135 [](float val) { return std::abs(val / -327.68f); });
136 this->update_sensor_from_s32_register16_(chan->forward_active_energy, BFWATTHR, [&chan](float val) {
137 return chan->forward_active_energy_total += val / 14400.0f;
138 });
139 this->update_sensor_from_s32_register16_(chan->reverse_active_energy, BRWATTHR, [&chan](float val) {
140 return chan->reverse_active_energy_total += val / 14400.0f;
141 });
142 }
143
144 if (this->channel_c_ != nullptr) {
145 auto *chan = this->channel_c_;
146 this->update_sensor_from_s24zp_register16_(chan->current, CIRMS, [](float val) { return val / 100000.0f; });
147 this->update_sensor_from_s24zp_register16_(chan->voltage, CVRMS, [](float val) { return val / 10000.0f; });
148 this->update_sensor_from_s24zp_register16_(chan->active_power, CWATT, [](float val) { return val / 100.0f; });
149 this->update_sensor_from_s24zp_register16_(chan->apparent_power, CVA, [](float val) { return val / 100.0f; });
150 this->update_sensor_from_s16_register16_(chan->power_factor, CPF,
151 [](float val) { return std::abs(val / -327.68f); });
152 this->update_sensor_from_s32_register16_(chan->forward_active_energy, CFWATTHR, [&chan](float val) {
153 return chan->forward_active_energy_total += val / 14400.0f;
154 });
155 this->update_sensor_from_s32_register16_(chan->reverse_active_energy, CRWATTHR, [&chan](float val) {
156 return chan->reverse_active_energy_total += val / 14400.0f;
157 });
158 }
159
160 ESP_LOGD(TAG, "update took %" PRIu32 " ms", millis() - start);
161}
162
164 ESP_LOGCONFIG(TAG,
165 "ADE7880:\n"
166 " Frequency: %.0f Hz",
167 this->frequency_);
168 LOG_PIN(" IRQ0 Pin: ", this->irq0_pin_);
169 LOG_PIN(" IRQ1 Pin: ", this->irq1_pin_);
170 LOG_PIN(" RESET Pin: ", this->reset_pin_);
171
172 if (this->channel_a_ != nullptr) {
173 ESP_LOGCONFIG(TAG, " Phase A:");
174 LOG_SENSOR(" ", "Current", this->channel_a_->current);
175 LOG_SENSOR(" ", "Voltage", this->channel_a_->voltage);
176 LOG_SENSOR(" ", "Active Power", this->channel_a_->active_power);
177 LOG_SENSOR(" ", "Apparent Power", this->channel_a_->apparent_power);
178 LOG_SENSOR(" ", "Power Factor", this->channel_a_->power_factor);
179 LOG_SENSOR(" ", "Forward Active Energy", this->channel_a_->forward_active_energy);
180 LOG_SENSOR(" ", "Reverse Active Energy", this->channel_a_->reverse_active_energy);
181 ESP_LOGCONFIG(TAG,
182 " Calibration:\n"
183 " Current: %" PRId32 "\n"
184 " Voltage: %" PRId32 "\n"
185 " Power: %" PRId32 "\n"
186 " Phase Angle: %u",
187 this->channel_a_->current_gain_calibration, this->channel_a_->voltage_gain_calibration,
188 this->channel_a_->power_gain_calibration, this->channel_a_->phase_angle_calibration);
189 }
190
191 if (this->channel_b_ != nullptr) {
192 ESP_LOGCONFIG(TAG, " Phase B:");
193 LOG_SENSOR(" ", "Current", this->channel_b_->current);
194 LOG_SENSOR(" ", "Voltage", this->channel_b_->voltage);
195 LOG_SENSOR(" ", "Active Power", this->channel_b_->active_power);
196 LOG_SENSOR(" ", "Apparent Power", this->channel_b_->apparent_power);
197 LOG_SENSOR(" ", "Power Factor", this->channel_b_->power_factor);
198 LOG_SENSOR(" ", "Forward Active Energy", this->channel_b_->forward_active_energy);
199 LOG_SENSOR(" ", "Reverse Active Energy", this->channel_b_->reverse_active_energy);
200 ESP_LOGCONFIG(TAG,
201 " Calibration:\n"
202 " Current: %" PRId32 "\n"
203 " Voltage: %" PRId32 "\n"
204 " Power: %" PRId32 "\n"
205 " Phase Angle: %u",
206 this->channel_b_->current_gain_calibration, this->channel_b_->voltage_gain_calibration,
207 this->channel_b_->power_gain_calibration, this->channel_b_->phase_angle_calibration);
208 }
209
210 if (this->channel_c_ != nullptr) {
211 ESP_LOGCONFIG(TAG, " Phase C:");
212 LOG_SENSOR(" ", "Current", this->channel_c_->current);
213 LOG_SENSOR(" ", "Voltage", this->channel_c_->voltage);
214 LOG_SENSOR(" ", "Active Power", this->channel_c_->active_power);
215 LOG_SENSOR(" ", "Apparent Power", this->channel_c_->apparent_power);
216 LOG_SENSOR(" ", "Power Factor", this->channel_c_->power_factor);
217 LOG_SENSOR(" ", "Forward Active Energy", this->channel_c_->forward_active_energy);
218 LOG_SENSOR(" ", "Reverse Active Energy", this->channel_c_->reverse_active_energy);
219 ESP_LOGCONFIG(TAG,
220 " Calibration:\n"
221 " Current: %" PRId32 "\n"
222 " Voltage: %" PRId32 "\n"
223 " Power: %" PRId32 "\n"
224 " Phase Angle: %u",
225 this->channel_c_->current_gain_calibration, this->channel_c_->voltage_gain_calibration,
226 this->channel_c_->power_gain_calibration, this->channel_c_->phase_angle_calibration);
227 }
228
229 if (this->channel_n_ != nullptr) {
230 ESP_LOGCONFIG(TAG, " Neutral:");
231 LOG_SENSOR(" ", "Current", this->channel_n_->current);
232 ESP_LOGCONFIG(TAG,
233 " Calibration:\n"
234 " Current: %" PRId32,
236 }
237
238 LOG_I2C_DEVICE(this);
239 LOG_UPDATE_INTERVAL(this);
240}
241
242void ADE7880::calibrate_s10zp_reading_(uint16_t a_register, int16_t calibration) {
243 if (calibration == 0) {
244 return;
245 }
246
247 this->write_s10zp_register16_(a_register, calibration);
248}
249
250void ADE7880::calibrate_s24zpse_reading_(uint16_t a_register, int32_t calibration) {
251 if (calibration == 0) {
252 return;
253 }
254
255 this->write_s24zpse_register16_(a_register, calibration);
256}
257
260
261 this->write_u16_register16_(GAIN, 0);
262
263 if (this->frequency_ > 55) {
265 }
266
267 if (this->channel_n_ != nullptr) {
269 }
270
271 if (this->channel_a_ != nullptr) {
276 }
277
278 if (this->channel_b_ != nullptr) {
283 }
284
285 if (this->channel_c_ != nullptr) {
290 }
291
292 // write three default values to data memory RAM to flush the I2C write queue
296
300}
301
303 if (this->reset_pin_ != nullptr) {
304 ESP_LOGD(TAG, "Reset device using RESET pin");
305 this->reset_pin_->digital_write(false);
306 delay(1);
307 this->reset_pin_->digital_write(true);
308 } else {
309 ESP_LOGD(TAG, "Reset device using SWRST command");
311 }
312 this->store_.reset_pending = true;
313}
314
315} // namespace esphome::ade7880
ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.", "2026.2.0") void set_retry(const std uint32_t uint8_t std::function< RetryResult(uint8_t)> && f
Definition component.h:454
virtual void setup()=0
virtual void digital_write(bool value)=0
virtual bool digital_read()=0
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition gpio.h:107
virtual ISRInternalGPIOPin to_isr() const =0
void update_sensor_from_s24zp_register16_(sensor::Sensor *sensor, uint16_t a_register, F &&f)
Definition ade7880.cpp:71
int16_t read_s16_register16_(uint16_t a_register)
int32_t read_s32_register16_(uint16_t a_register)
PowerChannel * channel_b_
Definition ade7880.h:95
void dump_config() override
Definition ade7880.cpp:163
void write_u8_register16_(uint16_t a_register, uint8_t value)
InternalGPIOPin * irq1_pin_
Definition ade7880.h:90
void write_s32_register16_(uint16_t a_register, int32_t value)
int32_t read_s24zp_register16_(uint16_t a_register)
void update_sensor_from_s32_register16_(sensor::Sensor *sensor, uint16_t a_register, F &&f)
Definition ade7880.cpp:91
void calibrate_s24zpse_reading_(uint16_t a_register, int32_t calibration)
Definition ade7880.cpp:250
void write_s24zpse_register16_(uint16_t a_register, int32_t value)
void update_sensor_from_s16_register16_(sensor::Sensor *sensor, uint16_t a_register, F &&f)
Definition ade7880.cpp:81
uint32_t read_u32_register16_(uint16_t a_register)
PowerChannel * channel_a_
Definition ade7880.h:94
ADE7880Store store_
Definition ade7880.h:88
void update() override
Definition ade7880.cpp:100
void setup() override
Definition ade7880.cpp:22
InternalGPIOPin * irq0_pin_
Definition ade7880.h:89
void loop() override
Definition ade7880.cpp:56
void write_s10zp_register16_(uint16_t a_register, int16_t value)
void write_u16_register16_(uint16_t a_register, uint16_t value)
void write_u32_register16_(uint16_t a_register, uint32_t value)
void calibrate_s10zp_reading_(uint16_t a_register, int16_t calibration)
Definition ade7880.cpp:242
InternalGPIOPin * reset_pin_
Definition ade7880.h:91
NeutralChannel * channel_n_
Definition ade7880.h:93
PowerChannel * channel_c_
Definition ade7880.h:96
Base-class for all sensors.
Definition sensor.h:47
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
mopeka_std_values val[3]
constexpr uint16_t CIRMS
constexpr uint16_t APGAIN
constexpr uint16_t CIGAIN
constexpr uint16_t CWATT
constexpr uint16_t AIGAIN
constexpr uint16_t DSPWP_SEL
constexpr uint16_t AWATT
constexpr uint16_t CPHCAL
constexpr uint16_t GAIN
constexpr uint8_t DSPWP_SEL_SET
constexpr uint16_t APHCAL
constexpr uint16_t BPHCAL
constexpr uint16_t VLEVEL
constexpr uint16_t AFWATTHR
constexpr uint16_t AVGAIN
constexpr uint16_t NIGAIN
constexpr uint16_t BVA
constexpr uint16_t APF
constexpr uint8_t DSPWP_SET_RO
constexpr uint16_t AIRMS
constexpr uint16_t BVGAIN
constexpr uint16_t CVRMS
constexpr uint16_t BWATT
constexpr uint16_t BVRMS
constexpr uint16_t NIRMS
constexpr uint16_t RUN_ENABLE
constexpr uint16_t CFWATTHR
constexpr uint16_t BPGAIN
constexpr uint16_t CVA
constexpr uint16_t BIGAIN
constexpr uint16_t CONFIG
constexpr uint16_t CONFIG_SWRST
constexpr uint16_t AVA
constexpr uint16_t RUN
constexpr uint16_t CONFIG2
constexpr uint16_t BIRMS
constexpr uint16_t STATUS0
constexpr uint16_t CPF
constexpr uint16_t BPF
constexpr uint16_t COMPMODE
constexpr uint16_t BRWATTHR
constexpr uint8_t CONFIG2_I2C_LOCK
constexpr uint16_t COMPMODE_SELFREQ
constexpr uint16_t ARWATTHR
constexpr uint16_t BFWATTHR
constexpr uint16_t DSPWP_SET
constexpr uint16_t CVGAIN
constexpr uint16_t AVRMS
constexpr uint16_t COMPMODE_DEFAULT
constexpr uint16_t STATUS1
constexpr uint16_t CPGAIN
constexpr uint16_t CRWATTHR
constexpr uint32_t STATUS1_RSTDONE
@ INTERRUPT_FALLING_EDGE
Definition gpio.h:51
void HOT delay(uint32_t ms)
Definition hal.cpp:82
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
static void gpio_intr(ADE7880Store *arg)
Definition ade7880.cpp:20
ISRInternalGPIOPin irq1_pin
Definition ade7880.h:63
sensor::Sensor * active_power
Definition ade7880.h:46
sensor::Sensor * reverse_active_energy
Definition ade7880.h:50
sensor::Sensor * current
Definition ade7880.h:44
sensor::Sensor * voltage
Definition ade7880.h:45
sensor::Sensor * forward_active_energy
Definition ade7880.h:49
sensor::Sensor * apparent_power
Definition ade7880.h:47
sensor::Sensor * power_factor
Definition ade7880.h:48