19 static const char *
const TAG =
"ade7880";
24 if (this->irq0_pin_ !=
nullptr) {
25 this->irq0_pin_->setup();
27 this->irq1_pin_->setup();
28 if (this->reset_pin_ !=
nullptr) {
29 this->reset_pin_->setup();
31 this->store_.irq1_pin = this->irq1_pin_->to_isr();
35 if (this->irq1_pin_->digital_read() == 0) {
36 ESP_LOGD(TAG,
"IRQ1 found asserted during setup()");
37 auto status1 = read_u32_register16_(
STATUS1);
40 ESP_LOGD(TAG,
"IRQ1 asserted for !RSTDONE, resetting device");
41 this->reset_device_();
46 ESP_LOGD(TAG,
"Acknowledging RSTDONE");
47 this->write_u32_register16_(
STATUS0, 0xFFFF);
48 this->write_u32_register16_(
STATUS1, 0xFFFF);
54 this->reset_device_();
59 if (!this->store_.reset_done) {
63 ESP_LOGD(TAG,
"Acknowledging RSTDONE");
64 this->write_u32_register16_(
STATUS0, 0xFFFF);
65 this->write_u32_register16_(
STATUS1, 0xFFFF);
67 this->store_.reset_done =
false;
68 this->store_.reset_pending =
false;
73 if (sensor ==
nullptr) {
77 float val = this->read_s24zp_register16_(a_register);
83 if (sensor ==
nullptr) {
87 float val = this->read_s16_register16_(a_register);
93 if (sensor ==
nullptr) {
97 float val = this->read_s32_register16_(a_register);
102 if (this->store_.reset_pending) {
108 if (this->channel_n_ !=
nullptr) {
109 auto *chan = this->channel_n_;
110 this->update_sensor_from_s24zp_register16_(chan->current,
NIRMS, [](
float val) { return val / 100000.0f; });
113 if (this->channel_a_ !=
nullptr) {
114 auto *chan = this->channel_a_;
115 this->update_sensor_from_s24zp_register16_(chan->current,
AIRMS, [](
float val) { return val / 100000.0f; });
116 this->update_sensor_from_s24zp_register16_(chan->voltage,
BVRMS, [](
float val) { return val / 10000.0f; });
117 this->update_sensor_from_s24zp_register16_(chan->active_power,
AWATT, [](
float val) { return val / 100.0f; });
118 this->update_sensor_from_s24zp_register16_(chan->apparent_power,
AVA, [](
float val) { return val / 100.0f; });
119 this->update_sensor_from_s16_register16_(chan->power_factor,
APF,
120 [](
float val) { return std::abs(val / -327.68f); });
121 this->update_sensor_from_s32_register16_(chan->forward_active_energy,
AFWATTHR, [&chan](
float val) {
122 return chan->forward_active_energy_total += val / 14400.0f;
124 this->update_sensor_from_s32_register16_(chan->reverse_active_energy,
AFWATTHR, [&chan](
float val) {
125 return chan->reverse_active_energy_total += val / 14400.0f;
129 if (this->channel_b_ !=
nullptr) {
130 auto *chan = this->channel_b_;
131 this->update_sensor_from_s24zp_register16_(chan->current,
BIRMS, [](
float val) { return val / 100000.0f; });
132 this->update_sensor_from_s24zp_register16_(chan->voltage,
BVRMS, [](
float val) { return val / 10000.0f; });
133 this->update_sensor_from_s24zp_register16_(chan->active_power,
BWATT, [](
float val) { return val / 100.0f; });
134 this->update_sensor_from_s24zp_register16_(chan->apparent_power,
BVA, [](
float val) { return val / 100.0f; });
135 this->update_sensor_from_s16_register16_(chan->power_factor,
BPF,
136 [](
float val) { return std::abs(val / -327.68f); });
137 this->update_sensor_from_s32_register16_(chan->forward_active_energy,
BFWATTHR, [&chan](
float val) {
138 return chan->forward_active_energy_total += val / 14400.0f;
140 this->update_sensor_from_s32_register16_(chan->reverse_active_energy,
BFWATTHR, [&chan](
float val) {
141 return chan->reverse_active_energy_total += val / 14400.0f;
145 if (this->channel_c_ !=
nullptr) {
146 auto *chan = this->channel_c_;
147 this->update_sensor_from_s24zp_register16_(chan->current,
CIRMS, [](
float val) { return val / 100000.0f; });
148 this->update_sensor_from_s24zp_register16_(chan->voltage,
CVRMS, [](
float val) { return val / 10000.0f; });
149 this->update_sensor_from_s24zp_register16_(chan->active_power,
CWATT, [](
float val) { return val / 100.0f; });
150 this->update_sensor_from_s24zp_register16_(chan->apparent_power,
CVA, [](
float val) { return val / 100.0f; });
151 this->update_sensor_from_s16_register16_(chan->power_factor,
CPF,
152 [](
float val) { return std::abs(val / -327.68f); });
153 this->update_sensor_from_s32_register16_(chan->forward_active_energy,
CFWATTHR, [&chan](
float val) {
154 return chan->forward_active_energy_total += val / 14400.0f;
156 this->update_sensor_from_s32_register16_(chan->reverse_active_energy,
CFWATTHR, [&chan](
float val) {
157 return chan->reverse_active_energy_total += val / 14400.0f;
161 ESP_LOGD(TAG,
"update took %" PRIu32
" ms",
millis() - start);
165 ESP_LOGCONFIG(TAG,
"ADE7880:");
166 LOG_PIN(
" IRQ0 Pin: ", this->irq0_pin_);
167 LOG_PIN(
" IRQ1 Pin: ", this->irq1_pin_);
168 LOG_PIN(
" RESET Pin: ", this->reset_pin_);
169 ESP_LOGCONFIG(TAG,
" Frequency: %.0f Hz", this->frequency_);
171 if (this->channel_a_ !=
nullptr) {
172 ESP_LOGCONFIG(TAG,
" Phase A:");
173 LOG_SENSOR(
" ",
"Current", this->channel_a_->current);
174 LOG_SENSOR(
" ",
"Voltage", this->channel_a_->voltage);
175 LOG_SENSOR(
" ",
"Active Power", this->channel_a_->active_power);
176 LOG_SENSOR(
" ",
"Apparent Power", this->channel_a_->apparent_power);
177 LOG_SENSOR(
" ",
"Power Factor", this->channel_a_->power_factor);
178 LOG_SENSOR(
" ",
"Forward Active Energy", this->channel_a_->forward_active_energy);
179 LOG_SENSOR(
" ",
"Reverse Active Energy", this->channel_a_->reverse_active_energy);
180 ESP_LOGCONFIG(TAG,
" Calibration:");
181 ESP_LOGCONFIG(TAG,
" Current: %" PRId32, this->channel_a_->current_gain_calibration);
182 ESP_LOGCONFIG(TAG,
" Voltage: %" PRId32, this->channel_a_->voltage_gain_calibration);
183 ESP_LOGCONFIG(TAG,
" Power: %" PRId32, this->channel_a_->power_gain_calibration);
184 ESP_LOGCONFIG(TAG,
" Phase Angle: %u", this->channel_a_->phase_angle_calibration);
187 if (this->channel_b_ !=
nullptr) {
188 ESP_LOGCONFIG(TAG,
" Phase B:");
189 LOG_SENSOR(
" ",
"Current", this->channel_b_->current);
190 LOG_SENSOR(
" ",
"Voltage", this->channel_b_->voltage);
191 LOG_SENSOR(
" ",
"Active Power", this->channel_b_->active_power);
192 LOG_SENSOR(
" ",
"Apparent Power", this->channel_b_->apparent_power);
193 LOG_SENSOR(
" ",
"Power Factor", this->channel_b_->power_factor);
194 LOG_SENSOR(
" ",
"Forward Active Energy", this->channel_b_->forward_active_energy);
195 LOG_SENSOR(
" ",
"Reverse Active Energy", this->channel_b_->reverse_active_energy);
196 ESP_LOGCONFIG(TAG,
" Calibration:");
197 ESP_LOGCONFIG(TAG,
" Current: %" PRId32, this->channel_b_->current_gain_calibration);
198 ESP_LOGCONFIG(TAG,
" Voltage: %" PRId32, this->channel_b_->voltage_gain_calibration);
199 ESP_LOGCONFIG(TAG,
" Power: %" PRId32, this->channel_b_->power_gain_calibration);
200 ESP_LOGCONFIG(TAG,
" Phase Angle: %u", this->channel_b_->phase_angle_calibration);
203 if (this->channel_c_ !=
nullptr) {
204 ESP_LOGCONFIG(TAG,
" Phase C:");
205 LOG_SENSOR(
" ",
"Current", this->channel_c_->current);
206 LOG_SENSOR(
" ",
"Voltage", this->channel_c_->voltage);
207 LOG_SENSOR(
" ",
"Active Power", this->channel_c_->active_power);
208 LOG_SENSOR(
" ",
"Apparent Power", this->channel_c_->apparent_power);
209 LOG_SENSOR(
" ",
"Power Factor", this->channel_c_->power_factor);
210 LOG_SENSOR(
" ",
"Forward Active Energy", this->channel_c_->forward_active_energy);
211 LOG_SENSOR(
" ",
"Reverse Active Energy", this->channel_c_->reverse_active_energy);
212 ESP_LOGCONFIG(TAG,
" Calibration:");
213 ESP_LOGCONFIG(TAG,
" Current: %" PRId32, this->channel_c_->current_gain_calibration);
214 ESP_LOGCONFIG(TAG,
" Voltage: %" PRId32, this->channel_c_->voltage_gain_calibration);
215 ESP_LOGCONFIG(TAG,
" Power: %" PRId32, this->channel_c_->power_gain_calibration);
216 ESP_LOGCONFIG(TAG,
" Phase Angle: %u", this->channel_c_->phase_angle_calibration);
219 if (this->channel_n_ !=
nullptr) {
220 ESP_LOGCONFIG(TAG,
" Neutral:");
221 LOG_SENSOR(
" ",
"Current", this->channel_n_->current);
222 ESP_LOGCONFIG(TAG,
" Calibration:");
223 ESP_LOGCONFIG(TAG,
" Current: %" PRId32, this->channel_n_->current_gain_calibration);
226 LOG_I2C_DEVICE(
this);
227 LOG_UPDATE_INTERVAL(
this);
231 if (calibration == 0) {
235 this->write_s10zp_register16_(a_register, calibration);
239 if (calibration == 0) {
243 this->write_s24zpse_register16_(a_register, calibration);
249 this->write_u16_register16_(
GAIN, 0);
251 if (this->frequency_ > 55) {
255 if (this->channel_n_ !=
nullptr) {
256 this->calibrate_s24zpse_reading_(
NIGAIN, this->channel_n_->current_gain_calibration);
259 if (this->channel_a_ !=
nullptr) {
260 this->calibrate_s24zpse_reading_(
AIGAIN, this->channel_a_->current_gain_calibration);
261 this->calibrate_s24zpse_reading_(
AVGAIN, this->channel_a_->voltage_gain_calibration);
262 this->calibrate_s24zpse_reading_(
APGAIN, this->channel_a_->power_gain_calibration);
263 this->calibrate_s10zp_reading_(
APHCAL, this->channel_a_->phase_angle_calibration);
266 if (this->channel_b_ !=
nullptr) {
267 this->calibrate_s24zpse_reading_(
BIGAIN, this->channel_b_->current_gain_calibration);
268 this->calibrate_s24zpse_reading_(
BVGAIN, this->channel_b_->voltage_gain_calibration);
269 this->calibrate_s24zpse_reading_(
BPGAIN, this->channel_b_->power_gain_calibration);
270 this->calibrate_s10zp_reading_(
BPHCAL, this->channel_b_->phase_angle_calibration);
273 if (this->channel_c_ !=
nullptr) {
274 this->calibrate_s24zpse_reading_(
CIGAIN, this->channel_c_->current_gain_calibration);
275 this->calibrate_s24zpse_reading_(
CVGAIN, this->channel_c_->voltage_gain_calibration);
276 this->calibrate_s24zpse_reading_(
CPGAIN, this->channel_c_->power_gain_calibration);
277 this->calibrate_s10zp_reading_(
CPHCAL, this->channel_c_->phase_angle_calibration);
281 this->write_s32_register16_(
VLEVEL, 0);
282 this->write_s32_register16_(
VLEVEL, 0);
283 this->write_s32_register16_(
VLEVEL, 0);
291 if (this->reset_pin_ !=
nullptr) {
292 ESP_LOGD(TAG,
"Reset device using RESET pin");
293 this->reset_pin_->digital_write(
false);
295 this->reset_pin_->digital_write(
true);
297 ESP_LOGD(TAG,
"Reset device using SWRST command");
300 this->store_.reset_pending =
true;
constexpr uint16_t DSPWP_SEL
constexpr uint16_t BVGAIN
constexpr uint16_t CIGAIN
constexpr uint16_t CONFIG2
constexpr uint16_t STATUS0
constexpr uint16_t CONFIG
constexpr uint16_t AVGAIN
constexpr uint16_t CVGAIN
constexpr uint16_t CPHCAL
constexpr uint16_t CONFIG_SWRST
constexpr uint16_t COMPMODE
constexpr uint16_t APGAIN
uint32_t IRAM_ATTR HOT millis()
constexpr uint16_t CFWATTHR
constexpr uint16_t STATUS1
constexpr uint16_t BIGAIN
constexpr uint16_t CPGAIN
static void gpio_intr(ADE7880Store *arg)
constexpr uint16_t BFWATTHR
constexpr uint16_t COMPMODE_DEFAULT
constexpr uint16_t DSPWP_SET
void publish_state(float state)
Publish a new state to the front-end.
void update_sensor_from_s32_register16_(sensor::Sensor *sensor, uint16_t a_register, F &&f)
constexpr uint8_t CONFIG2_I2C_LOCK
constexpr uint16_t RUN_ENABLE
void update_sensor_from_s16_register16_(sensor::Sensor *sensor, uint16_t a_register, F &&f)
constexpr uint8_t DSPWP_SET_RO
constexpr uint8_t DSPWP_SEL_SET
void update_sensor_from_s24zp_register16_(sensor::Sensor *sensor, uint16_t a_register, F &&f)
constexpr uint16_t BPHCAL
constexpr uint16_t NIGAIN
constexpr uint16_t AIGAIN
constexpr uint16_t COMPMODE_SELFREQ
constexpr uint32_t STATUS1_RSTDONE
void dump_config() override
void calibrate_s10zp_reading_(uint16_t a_register, int16_t calibration)
constexpr uint16_t APHCAL
constexpr uint16_t VLEVEL
Implementation of SPI Controller mode.
void calibrate_s24zpse_reading_(uint16_t a_register, int32_t calibration)
Base-class for all sensors.
constexpr uint16_t AFWATTHR
constexpr uint16_t BPGAIN
esphome::sensor::Sensor * sensor
void IRAM_ATTR HOT delay(uint32_t ms)