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