17static constexpr int CSE7761_UREF = 42563;
18static constexpr int CSE7761_IREF = 52241;
19static constexpr int CSE7761_PREF = 44513;
22static constexpr uint32_t CURRENT_TRANSFORMER_COEFFICIENT_SCALE = 5;
25static constexpr uint16_t SYSCON_SHUNT = 0xFF04;
27static constexpr uint16_t SYSCON_CURRENT_TRANSFORMER = 0xFE00;
30static constexpr float CSE7761_UFREQ_CLOCK = 3579545.0f / 8.0f;
32static constexpr uint8_t CSE7761_REG_SYSCON = 0x00;
33static constexpr uint8_t CSE7761_REG_EMUCON = 0x01;
34static constexpr uint8_t CSE7761_REG_HFCONST = 0x02;
35static constexpr uint8_t CSE7761_REG_EMUCON2 = 0x13;
36static constexpr uint8_t CSE7761_REG_PULSE1SEL = 0x1D;
38static constexpr uint8_t CSE7761_REG_UFREQ = 0x23;
39static constexpr uint8_t CSE7761_REG_RMSIA = 0x24;
40static constexpr uint8_t CSE7761_REG_RMSIB = 0x25;
41static constexpr uint8_t CSE7761_REG_RMSU = 0x26;
42static constexpr uint8_t CSE7761_REG_POWERFACTOR = 0x27;
43static constexpr uint8_t CSE7761_REG_ENERGY_PA = 0x28;
44static constexpr uint8_t CSE7761_REG_ENERGY_PB = 0x29;
45static constexpr uint8_t CSE7761_REG_POWERPA = 0x2C;
46static constexpr uint8_t CSE7761_REG_POWERPB = 0x2D;
47static constexpr uint8_t CSE7761_REG_SYSSTATUS = 0x43;
49static constexpr uint8_t CSE7761_REG_COEFFCHKSUM = 0x6F;
50static constexpr uint8_t CSE7761_REG_RMSIAC = 0x70;
53static constexpr float CSE7761_ENERGY_WH_SCALE = 1000.0f / 2199023255552.0f;
55static constexpr uint8_t CSE7761_SPECIAL_COMMAND = 0xEA;
56static constexpr uint8_t CSE7761_CMD_RESET = 0x96;
57static constexpr uint8_t CSE7761_CMD_CLOSE_WRITE = 0xDC;
58static constexpr uint8_t CSE7761_CMD_ENABLE_WRITE = 0xE5;
63 this->
write_(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_RESET);
64 uint16_t syscon = this->
read_(0x00, 2);
65 if ((0x0A04 == syscon) && this->
chip_init_()) {
66 this->
write_(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_CLOSE_WRITE);
67 ESP_LOGD(TAG,
"CSE7761 found");
75 ESP_LOGCONFIG(TAG,
"CSE7761:");
76 ESP_LOGCONFIG(TAG,
" Current input: %s",
78 LOG_UPDATE_INTERVAL(
this);
107 buffer[2] = (data >> 8) & 0xFF;
127 int value = this->
read();
128 if (value > -1 && rcvd <
sizeof(
buffer) - 1) {
134 ESP_LOGD(TAG,
"Received 0 bytes for register %hhu", reg);
143 for (
uint32_t i = 0; i < rcvd; i++) {
144 result = (result << 8) |
buffer[i];
152 for (uint8_t retry = 0; retry < 3; retry++) {
156 ESP_LOGE(TAG,
"Reading register %hhu failed!", reg);
162 return this->
read_(reg,
size, &value) ? value : 0;
171 return coeff ? 0x400000 * 100 / coeff : 0;
174 return coeff ? (0x800000 * 100 / coeff) * 10 : 0;
177 return coeff ? 0x80000000 / coeff : 0;
185 uint16_t calc_chksum = 0xFFFF;
190 calc_chksum = ~calc_chksum;
192 uint16_t coeff_chksum = this->
read_(CSE7761_REG_COEFFCHKSUM, 2);
193 if ((calc_chksum != coeff_chksum) || (!calc_chksum)) {
194 ESP_LOGD(TAG,
"Default calibration");
198 if (energy_configured) {
199 ESP_LOGW(TAG,
"No energy calibration, energy unavailable");
201 }
else if (energy_configured) {
203 if (!this->
read_(CSE7761_REG_HFCONST, 2, &hf_const)) {
204 ESP_LOGW(TAG,
"HFConst read failed, energy unavailable");
208 for (uint8_t channel = 0; channel < 2; channel++) {
210 this->
energy_[channel].
wh_per_pulse =
static_cast<float>(scale) * CSE7761_ENERGY_WH_SCALE * input_scale;
214 this->
write_(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_ENABLE_WRITE);
216 uint8_t sys_status = this->
read_(CSE7761_REG_SYSSTATUS, 1);
217 if (sys_status & 0x10) {
218 this->
write_(CSE7761_REG_SYSCON | 0x80,
220 this->
write_(CSE7761_REG_EMUCON | 0x80, 0x1183);
223 this->
write_(CSE7761_REG_PULSE1SEL | 0x80, 0x3290);
225 ESP_LOGD(TAG,
"Write failed at chip_init");
241 value = this->
read_(CSE7761_REG_RMSIA, 3);
242 this->
data_.
current_rms[0] = ((value >= 0x800000) || (value < 1600)) ? 0 : value;
243 value = this->
read_(CSE7761_REG_POWERPA, 4);
247 value = this->
read_(CSE7761_REG_RMSIB, 3);
248 this->
data_.
current_rms[1] = ((value >= 0x800000) || (value < 1600)) ? 0 : value;
249 value = this->
read_(CSE7761_REG_POWERPB, 4);
263 float frequency = (raw_reg == 0 || raw_reg >= 0x8000) ? 0.0f : CSE7761_UFREQ_CLOCK / raw_reg;
269 int24_t pf{
static_cast<int32_t
>(raw_reg)};
273 for (uint8_t channel = 0; channel < 2; channel++) {
278 ESP_LOGD(TAG,
"Channel %d power %f W, current %f A", channel + 1, active_power, amps);
281 uint8_t energy_reg = (channel == 0) ? CSE7761_REG_ENERGY_PA : CSE7761_REG_ENERGY_PB;
283 if (energy_sensor !=
nullptr && energy.
wh_per_pulse > 0.0f && this->read_(energy_reg, 3, &
raw)) {
296 }
else if (channel == 1) {
constexpr uint8_t sum_bytes(const uint8_t *data, size_t len, uint8_t sum=0)
Add up the bytes of data with size len, truncated to 8 bits: the checksum many serial and IR protocol...