ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
ade7953_base.cpp
Go to the documentation of this file.
1#include "ade7953_base.h"
2#include "esphome/core/log.h"
3
4#include <cinttypes>
5
7
8static const char *const TAG = "ade7953";
9
10constexpr uint16_t CONFIG_DEFAULT = 0x8004u;
11constexpr uint16_t CONFIG_LOCK_BIT = 0x8000u;
12
13static const float ADE_POWER_FACTOR = 154.0f;
14static const float ADE_WATTSEC_POWER_FACTOR = ADE_POWER_FACTOR * ADE_POWER_FACTOR / 3600;
15
17 if (this->irq_pin_ != nullptr) {
18 this->irq_pin_->setup();
19 }
20
21 // The chip might take up to 100ms to initialise
22 this->set_timeout(100, [this]() {
23 // Lock communication interface (SPI or I2C)
24 uint16_t config_v = CONFIG_DEFAULT;
25 this->ade_read_16(CONFIG_16, &config_v);
26 config_v &= static_cast<uint16_t>(~CONFIG_LOCK_BIT); // Clear the lock bit
27 this->ade_write_16(CONFIG_16, config_v);
28 // Configure optimum settings according to datasheet
29 this->ade_write_8(0x00FE, 0xAD);
30 this->ade_write_16(0x0120, 0x0030);
31 // Set gains
32 this->ade_write_8(PGA_V_8, pga_v_);
33 this->ade_write_8(PGA_IA_8, pga_ia_);
34 this->ade_write_8(PGA_IB_8, pga_ib_);
35 this->ade_write_32(AVGAIN_32, avgain_);
36 this->ade_write_32(BVGAIN_32, bvgain_);
37 this->ade_write_32(AIGAIN_32, aigain_);
38 this->ade_write_32(BIGAIN_32, bigain_);
39 this->ade_write_32(AWGAIN_32, awgain_);
40 this->ade_write_32(BWGAIN_32, bwgain_);
41 // Read back gains for debugging
42 this->ade_read_8(PGA_V_8, &pga_v_);
43 this->ade_read_8(PGA_IA_8, &pga_ia_);
44 this->ade_read_8(PGA_IB_8, &pga_ib_);
45 this->ade_read_32(AVGAIN_32, &avgain_);
46 this->ade_read_32(BVGAIN_32, &bvgain_);
47 this->ade_read_32(AIGAIN_32, &aigain_);
48 this->ade_read_32(BIGAIN_32, &bigain_);
49 this->ade_read_32(AWGAIN_32, &awgain_);
50 this->ade_read_32(BWGAIN_32, &bwgain_);
51 this->last_update_ = millis();
52 this->is_setup_ = true;
53 });
54}
55
57 LOG_PIN(" IRQ Pin: ", irq_pin_);
58 LOG_UPDATE_INTERVAL(this);
59 LOG_SENSOR(" ", "Voltage Sensor", this->voltage_sensor_);
60 LOG_SENSOR(" ", "Current A Sensor", this->current_a_sensor_);
61 LOG_SENSOR(" ", "Current B Sensor", this->current_b_sensor_);
62 LOG_SENSOR(" ", "Power Factor A Sensor", this->power_factor_a_sensor_);
63 LOG_SENSOR(" ", "Power Factor B Sensor", this->power_factor_b_sensor_);
64 LOG_SENSOR(" ", "Apparent Power A Sensor", this->apparent_power_a_sensor_);
65 LOG_SENSOR(" ", "Apparent Power B Sensor", this->apparent_power_b_sensor_);
66 LOG_SENSOR(" ", "Active Power A Sensor", this->active_power_a_sensor_);
67 LOG_SENSOR(" ", "Active Power B Sensor", this->active_power_b_sensor_);
68 LOG_SENSOR(" ", "Rective Power A Sensor", this->reactive_power_a_sensor_);
69 LOG_SENSOR(" ", "Reactive Power B Sensor", this->reactive_power_b_sensor_);
70 ESP_LOGCONFIG(TAG,
71 " USE_ACC_ENERGY_REGS: %d\n"
72 " PGA_V_8: 0x%X\n"
73 " PGA_IA_8: 0x%X\n"
74 " PGA_IB_8: 0x%X\n"
75 " AVGAIN_32: 0x%08jX\n"
76 " BVGAIN_32: 0x%08jX\n"
77 " AIGAIN_32: 0x%08jX\n"
78 " BIGAIN_32: 0x%08jX\n"
79 " AWGAIN_32: 0x%08jX\n"
80 " BWGAIN_32: 0x%08jX",
81 this->use_acc_energy_regs_, pga_v_, pga_ia_, pga_ib_, (uintmax_t) avgain_, (uintmax_t) bvgain_,
82 (uintmax_t) aigain_, (uintmax_t) bigain_, (uintmax_t) awgain_, (uintmax_t) bwgain_);
83}
84
85#define ADE_PUBLISH_(name, val, factor) \
86 if (err == 0 && this->name##_sensor_) { \
87 float value = (val) / (factor); \
88 this->name##_sensor_->publish_state(value); \
89 }
90#define ADE_PUBLISH(name, val, factor) ADE_PUBLISH_(name, val, factor)
91
93 if (!this->is_setup_)
94 return;
95
96 bool err;
97
98 uint32_t interrupts_a = 0;
99 uint32_t interrupts_b = 0;
100 if (this->irq_pin_ != nullptr) {
101 // Read and reset interrupts
102 this->ade_read_32(0x032E, &interrupts_a);
103 this->ade_read_32(0x0331, &interrupts_b);
104 }
105
107 uint16_t val_16;
108 uint16_t reg;
109
110 // Power factor
111 err = this->ade_read_16(0x010A, &val_16);
112 ADE_PUBLISH(power_factor_a, (int16_t) val_16, (0x7FFF / 100.0f));
113 err = this->ade_read_16(0x010B, &val_16);
114 ADE_PUBLISH(power_factor_b, (int16_t) val_16, (0x7FFF / 100.0f));
115
116 float pf = ADE_POWER_FACTOR;
117 if (this->use_acc_energy_regs_) {
118 const uint32_t now = millis();
119 const auto diff = now - this->last_update_;
120 this->last_update_ = now;
121 // prevent DIV/0
122 pf = ADE_WATTSEC_POWER_FACTOR * (diff < 10 ? 10 : diff) / 1000;
123 ESP_LOGVV(TAG, "ADE7953::update() diff=%" PRIu32 " pf=%f", diff, pf);
124 }
125
126 // Apparent power
127 reg = this->use_acc_energy_regs_ ? 0x0322 : 0x0310;
128 err = this->ade_read_32(reg, &val);
129 ADE_PUBLISH(apparent_power_a, (int32_t) val, pf);
130 err = this->ade_read_32(reg + 1, &val);
131 ADE_PUBLISH(apparent_power_b, (int32_t) val, pf);
132
133 // Active power
134 reg = this->use_acc_energy_regs_ ? 0x031E : 0x0312;
135 err = this->ade_read_32(reg, &val);
136 ADE_PUBLISH(active_power_a, (int32_t) val, pf);
137 err = this->ade_read_32(reg + 1, &val);
138 ADE_PUBLISH(active_power_b, (int32_t) val, pf);
139
140 // Reactive power
141 reg = this->use_acc_energy_regs_ ? 0x0320 : 0x0314;
142 err = this->ade_read_32(reg, &val);
143 ADE_PUBLISH(reactive_power_a, (int32_t) val, pf);
144 err = this->ade_read_32(reg + 1, &val);
145 ADE_PUBLISH(reactive_power_b, (int32_t) val, pf);
146
147 // Current
148 err = this->ade_read_32(0x031A, &val);
149 ADE_PUBLISH(current_a, (uint32_t) val, 100000.0f);
150 err = this->ade_read_32(0x031B, &val);
151 ADE_PUBLISH(current_b, (uint32_t) val, 100000.0f);
152
153 // Voltage
154 err = this->ade_read_32(0x031C, &val);
155 ADE_PUBLISH(voltage, (uint32_t) val, 26000.0f);
156
157 // Frequency
158 err = this->ade_read_16(0x010E, &val_16);
159 ADE_PUBLISH(frequency, 223750.0f, 1 + val_16);
160}
161
162} // namespace esphome::ade7953_base
uint16_le_t frequency
Definition bl0942.h:6
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:493
virtual void setup()=0
virtual bool ade_write_32(uint16_t reg, uint32_t value)=0
virtual bool ade_read_16(uint16_t reg, uint16_t *value)=0
sensor::Sensor * reactive_power_b_sensor_
sensor::Sensor * reactive_power_a_sensor_
sensor::Sensor * current_a_sensor_
sensor::Sensor * power_factor_b_sensor_
sensor::Sensor * power_factor_a_sensor_
virtual bool ade_read_8(uint16_t reg, uint8_t *value)=0
sensor::Sensor * active_power_a_sensor_
virtual bool ade_read_32(uint16_t reg, uint32_t *value)=0
sensor::Sensor * apparent_power_b_sensor_
virtual bool ade_write_16(uint16_t reg, uint16_t value)=0
sensor::Sensor * apparent_power_a_sensor_
sensor::Sensor * current_b_sensor_
virtual bool ade_write_8(uint16_t reg, uint8_t value)=0
sensor::Sensor * active_power_b_sensor_
sensor::Sensor * voltage_sensor_
mopeka_std_values val[3]
constexpr uint16_t CONFIG_LOCK_BIT
constexpr uint16_t CONFIG_DEFAULT
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
static void uint32_t