ESPHome 2026.3.2
Loading...
Searching...
No Matches
cse7761.cpp
Go to the documentation of this file.
1#include "cse7761.h"
2
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace cse7761 {
7
8static const char *const TAG = "cse7761";
9
10/*********************************************************************************************\
11 * CSE7761 - Energy (Sonoff Dual R3 Pow v1.x)
12 *
13 * Based on Tasmota source code
14 * See https://github.com/arendst/Tasmota/discussions/10793
15 * https://github.com/arendst/Tasmota/blob/development/tasmota/xnrg_19_cse7761.ino
16\*********************************************************************************************/
17
18static constexpr int CSE7761_UREF = 42563; // RmsUc
19static constexpr int CSE7761_IREF = 52241; // RmsIAC
20static constexpr int CSE7761_PREF = 44513; // PowerPAC
21
22static constexpr uint8_t CSE7761_REG_SYSCON = 0x00; // (2) System Control Register (0x0A04)
23static constexpr uint8_t CSE7761_REG_EMUCON = 0x01; // (2) Metering control register (0x0000)
24static constexpr uint8_t CSE7761_REG_EMUCON2 = 0x13; // (2) Metering control register 2 (0x0001)
25static constexpr uint8_t CSE7761_REG_PULSE1SEL = 0x1D; // (2) Pin function output select register (0x3210)
26
27static constexpr uint8_t CSE7761_REG_RMSIA = 0x24; // (3) The effective value of channel A current (0x000000)
28static constexpr uint8_t CSE7761_REG_RMSIB = 0x25; // (3) The effective value of channel B current (0x000000)
29static constexpr uint8_t CSE7761_REG_RMSU = 0x26; // (3) Voltage RMS (0x000000)
30static constexpr uint8_t CSE7761_REG_POWERPA = 0x2C; // (4) Channel A active power, update rate 27.2Hz (0x00000000)
31static constexpr uint8_t CSE7761_REG_POWERPB = 0x2D; // (4) Channel B active power, update rate 27.2Hz (0x00000000)
32static constexpr uint8_t CSE7761_REG_SYSSTATUS = 0x43; // (1) System status register
33
34static constexpr uint8_t CSE7761_REG_COEFFCHKSUM = 0x6F; // (2) Coefficient checksum
35static constexpr uint8_t CSE7761_REG_RMSIAC = 0x70; // (2) Channel A effective current conversion coefficient
36
37static constexpr uint8_t CSE7761_SPECIAL_COMMAND = 0xEA; // Start special command
38static constexpr uint8_t CSE7761_CMD_RESET = 0x96; // Reset command, after receiving the command, the chip resets
39static constexpr uint8_t CSE7761_CMD_CLOSE_WRITE = 0xDC; // Close write operation
40static constexpr uint8_t CSE7761_CMD_ENABLE_WRITE = 0xE5; // Enable write operation
41
43
45 this->write_(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_RESET);
46 uint16_t syscon = this->read_(0x00, 2); // Default 0x0A04
47 if ((0x0A04 == syscon) && this->chip_init_()) {
48 this->write_(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_CLOSE_WRITE);
49 ESP_LOGD(TAG, "CSE7761 found");
50 this->data_.ready = true;
51 } else {
52 this->mark_failed();
53 }
54}
55
57 ESP_LOGCONFIG(TAG, "CSE7761:");
58 if (this->is_failed()) {
59 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
60 }
61 LOG_UPDATE_INTERVAL(this);
63}
64
66 if (this->data_.ready) {
67 this->get_data_();
68 }
69}
70
71void CSE7761Component::write_(uint8_t reg, uint16_t data) {
72 uint8_t buffer[5];
73
74 buffer[0] = 0xA5;
75 buffer[1] = reg;
76 uint32_t len = 2;
77 if (data) {
78 if (data < 0xFF) {
79 buffer[2] = data & 0xFF;
80 len = 3;
81 } else {
82 buffer[2] = (data >> 8) & 0xFF;
83 buffer[3] = data & 0xFF;
84 len = 4;
85 }
86 uint8_t crc = 0;
87 for (uint32_t i = 0; i < len; i++) {
88 crc += buffer[i];
89 }
90 buffer[len] = ~crc;
91 len++;
92 }
93
94 this->write_array(buffer, len);
95}
96
97bool CSE7761Component::read_once_(uint8_t reg, uint8_t size, uint32_t *value) {
98 while (this->available()) {
99 this->read();
100 }
101
102 this->write_(reg, 0);
103
104 uint8_t buffer[8] = {0};
105 uint32_t rcvd = 0;
106
107 for (uint32_t i = 0; i <= size; i++) {
108 int value = this->read();
109 if (value > -1 && rcvd < sizeof(buffer) - 1) {
110 buffer[rcvd++] = value;
111 }
112 }
113
114 if (!rcvd) {
115 ESP_LOGD(TAG, "Received 0 bytes for register %hhu", reg);
116 return false;
117 }
118
119 rcvd--;
120 uint32_t result = 0;
121 // CRC check
122 uint8_t crc = 0xA5 + reg;
123 for (uint32_t i = 0; i < rcvd; i++) {
124 result = (result << 8) | buffer[i];
125 crc += buffer[i];
126 }
127 crc = ~crc;
128 if (crc != buffer[rcvd]) {
129 return false;
130 }
131
132 *value = result;
133 return true;
134}
135
136uint32_t CSE7761Component::read_(uint8_t reg, uint8_t size) {
137 bool result = false; // Start loop
138 uint8_t retry = 3; // Retry up to three times
139 uint32_t value = 0; // Default no value
140 while (!result && retry > 0) {
141 retry--;
142 if (this->read_once_(reg, size, &value))
143 return value;
144 }
145 ESP_LOGE(TAG, "Reading register %hhu failed!", reg);
146 return value;
147}
148
150 uint32_t coeff = 0;
151 switch (unit) {
152 case RMS_UC:
153 coeff = this->data_.coefficient[RMS_UC];
154 return coeff ? 0x400000 * 100 / coeff : 0;
155 case RMS_IAC:
156 coeff = this->data_.coefficient[RMS_IAC];
157 return coeff ? (0x800000 * 100 / coeff) * 10 : 0; // Stay within 32 bits
158 case POWER_PAC:
159 coeff = this->data_.coefficient[POWER_PAC];
160 return coeff ? 0x80000000 / coeff : 0;
161 }
162 return 0;
163}
164
166 uint16_t calc_chksum = 0xFFFF;
167 for (uint32_t i = 0; i < 8; i++) {
168 this->data_.coefficient[i] = this->read_(CSE7761_REG_RMSIAC + i, 2);
169 calc_chksum += this->data_.coefficient[i];
170 }
171 calc_chksum = ~calc_chksum;
172 uint16_t coeff_chksum = this->read_(CSE7761_REG_COEFFCHKSUM, 2);
173 if ((calc_chksum != coeff_chksum) || (!calc_chksum)) {
174 ESP_LOGD(TAG, "Default calibration");
175 this->data_.coefficient[RMS_IAC] = CSE7761_IREF;
176 this->data_.coefficient[RMS_UC] = CSE7761_UREF;
177 this->data_.coefficient[POWER_PAC] = CSE7761_PREF;
178 }
179
180 this->write_(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_ENABLE_WRITE);
181
182 uint8_t sys_status = this->read_(CSE7761_REG_SYSSTATUS, 1);
183 if (sys_status & 0x10) { // Write enable to protected registers (WREN)
184 this->write_(CSE7761_REG_SYSCON | 0x80, 0xFF04);
185 this->write_(CSE7761_REG_EMUCON | 0x80, 0x1183);
186 this->write_(CSE7761_REG_EMUCON2 | 0x80, 0x0FC1);
187 this->write_(CSE7761_REG_PULSE1SEL | 0x80, 0x3290);
188 } else {
189 ESP_LOGD(TAG, "Write failed at chip_init");
190 return false;
191 }
192 return true;
193}
194
196 // The effective value of current and voltage Rms is a 24-bit signed number,
197 // the highest bit is 0 for valid data,
198 // and when the highest bit is 1, the reading will be processed as zero
199 // The active power parameter PowerA/B is in two’s complement format, 32-bit
200 // data, the highest bit is Sign bit.
201 uint32_t value = this->read_(CSE7761_REG_RMSU, 3);
202 this->data_.voltage_rms = (value >= 0x800000) ? 0 : value;
203
204 value = this->read_(CSE7761_REG_RMSIA, 3);
205 this->data_.current_rms[0] = ((value >= 0x800000) || (value < 1600)) ? 0 : value; // No load threshold of 10mA
206 value = this->read_(CSE7761_REG_POWERPA, 4);
207 this->data_.active_power[0] = (0 == this->data_.current_rms[0]) ? 0 : ((uint32_t) abs((int) value));
208
209 value = this->read_(CSE7761_REG_RMSIB, 3);
210 this->data_.current_rms[1] = ((value >= 0x800000) || (value < 1600)) ? 0 : value; // No load threshold of 10mA
211 value = this->read_(CSE7761_REG_POWERPB, 4);
212 this->data_.active_power[1] = (0 == this->data_.current_rms[1]) ? 0 : ((uint32_t) abs((int) value));
213
214 // convert values and publish to sensors
215
216 float voltage = (float) this->data_.voltage_rms / this->coefficient_by_unit_(RMS_UC);
217 if (this->voltage_sensor_ != nullptr) {
218 this->voltage_sensor_->publish_state(voltage);
219 }
220
221 for (uint8_t channel = 0; channel < 2; channel++) {
222 // Active power = PowerPA * PowerPAC * 1000 / 0x80000000
223 float active_power = (float) this->data_.active_power[channel] / this->coefficient_by_unit_(POWER_PAC); // W
224 float amps = (float) this->data_.current_rms[channel] / this->coefficient_by_unit_(RMS_IAC); // A
225 ESP_LOGD(TAG, "Channel %d power %f W, current %f A", channel + 1, active_power, amps);
226 if (channel == 0) {
227 if (this->power_sensor_1_ != nullptr) {
228 this->power_sensor_1_->publish_state(active_power);
229 }
230 if (this->current_sensor_1_ != nullptr) {
231 this->current_sensor_1_->publish_state(amps);
232 }
233 } else if (channel == 1) {
234 if (this->power_sensor_2_ != nullptr) {
235 this->power_sensor_2_->publish_state(active_power);
236 }
237 if (this->current_sensor_2_ != nullptr) {
238 this->current_sensor_2_->publish_state(amps);
239 }
240 }
241 }
242}
243
244} // namespace cse7761
245} // namespace esphome
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:233
sensor::Sensor * current_sensor_1_
Definition cse7761.h:37
sensor::Sensor * power_sensor_2_
Definition cse7761.h:38
sensor::Sensor * voltage_sensor_
Definition cse7761.h:35
uint32_t coefficient_by_unit_(uint32_t unit)
Definition cse7761.cpp:149
uint32_t read_(uint8_t reg, uint8_t size)
Definition cse7761.cpp:136
sensor::Sensor * current_sensor_2_
Definition cse7761.h:39
sensor::Sensor * power_sensor_1_
Definition cse7761.h:36
bool read_once_(uint8_t reg, uint8_t size, uint32_t *value)
Definition cse7761.cpp:97
void write_(uint8_t reg, uint16_t data)
Definition cse7761.cpp:71
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
void check_uart_settings(uint32_t baud_rate, uint8_t stop_bits=1, UARTParityOptions parity=UART_CONFIG_PARITY_NONE, uint8_t data_bits=8)
Check that the configuration of the UART bus matches the provided values and otherwise print a warnin...
Definition uart.cpp:16
void write_array(const uint8_t *data, size_t len)
Definition uart.h:26
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:892
size_t size
Definition helpers.h:929
static void uint32_t