From d231bb47314dabcfd074784bf330552be376d47b Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Mon, 8 Mar 2021 16:56:33 +0100 Subject: [PATCH] Prep CSE7761 for final --- tasmota/xnrg_19_cse7761.ino | 46 +++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/tasmota/xnrg_19_cse7761.ino b/tasmota/xnrg_19_cse7761.ino index 571f50fc4..98ad863bb 100644 --- a/tasmota/xnrg_19_cse7761.ino +++ b/tasmota/xnrg_19_cse7761.ino @@ -114,7 +114,7 @@ void Cse7761Write(uint32_t reg, uint32_t data) { AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("C61: Tx %*_H"), len, buffer); } -uint32_t Cse7761Read(uint32_t reg) { +uint32_t Cse7761Read(uint32_t reg, uint32_t size) { while (Cse7761Serial->available()) { Cse7761Serial->read(); } Cse7761Write(reg, 0); @@ -122,6 +122,8 @@ uint32_t Cse7761Read(uint32_t reg) { uint8_t buffer[8] = { 0 }; uint32_t rcvd = 0; uint32_t timeout = millis() + 3; + +// while (!TimeReached(timeout) && (rcvd <= size)) { while (!TimeReached(timeout)) { int value = Cse7761Serial->read(); if ((value > -1) && (rcvd < sizeof(buffer) -1)) { @@ -155,8 +157,8 @@ uint32_t Cse7761Read(uint32_t reg) { return result; } -uint32_t Cse7761ReadFallback(uint32_t reg, uint32_t prev) { - uint32_t value = Cse7761Read(reg); +uint32_t Cse7761ReadFallback(uint32_t reg, uint32_t prev, uint32_t size) { + uint32_t value = Cse7761Read(reg, size); if (1 == value) { // CRC Error so use previous value read value = prev; } @@ -165,9 +167,9 @@ uint32_t Cse7761ReadFallback(uint32_t reg, uint32_t prev) { uint32_t Cse7761Ref(uint32_t unit) { switch (unit) { - case 1: return 0x400000 * 100 / CSE7761Data.coefficient[RmsUC]; - case 2: return (0x800000 * 100 / CSE7761Data.coefficient[RmsIAC]) * 10; // Stay within 32 bits - case 3: return 0x80000000 / CSE7761Data.coefficient[PowerPAC]; + case RmsUC: return 0x400000 * 100 / CSE7761Data.coefficient[RmsUC]; + case RmsIAC: return (0x800000 * 100 / CSE7761Data.coefficient[RmsIAC]) * 10; // Stay within 32 bits + case PowerPAC: return 0x80000000 / CSE7761Data.coefficient[PowerPAC]; } return 0; } @@ -175,12 +177,12 @@ uint32_t Cse7761Ref(uint32_t unit) { bool Cse7761ChipInit(void) { uint16_t calc_chksum = 0xFFFF; for (uint32_t i = 0; i < 8; i++) { - CSE7761Data.coefficient[i] = Cse7761Read(CSE7761_REG_RMSIAC + i); + CSE7761Data.coefficient[i] = Cse7761Read(CSE7761_REG_RMSIAC + i, 2); calc_chksum += CSE7761Data.coefficient[i]; } calc_chksum = ~calc_chksum; -// uint16_t dummy = Cse7761Read(CSE7761_REG_COEFFOFFSET); - uint16_t coeff_chksum = Cse7761Read(CSE7761_REG_COEFFCHKSUM); +// uint16_t dummy = Cse7761Read(CSE7761_REG_COEFFOFFSET, 2); + uint16_t coeff_chksum = Cse7761Read(CSE7761_REG_COEFFCHKSUM, 2); if ((calc_chksum != coeff_chksum) || (!calc_chksum)) { AddLog(LOG_LEVEL_DEBUG, PSTR("C61: Default calibration")); CSE7761Data.coefficient[RmsIAC] = CSE7761_IREF; @@ -190,9 +192,9 @@ bool Cse7761ChipInit(void) { // CSE7761Data.coefficient[PowerPBC] = 0xADD7; } if (HLW_PREF_PULSE == Settings.energy_power_calibration) { - Settings.energy_voltage_calibration = Cse7761Ref(1); - Settings.energy_current_calibration = Cse7761Ref(2); - Settings.energy_power_calibration = Cse7761Ref(3); + Settings.energy_voltage_calibration = Cse7761Ref(RmsUC); + Settings.energy_current_calibration = Cse7761Ref(RmsIAC); + Settings.energy_power_calibration = Cse7761Ref(PowerPAC); } Cse7761Write(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_ENABLE_WRITE); @@ -201,7 +203,7 @@ bool Cse7761ChipInit(void) { uint32_t timeout = millis() + 8; while (!TimeReached(timeout)) { } - uint8_t sys_status = Cse7761Read(CSE7761_REG_SYSSTATUS); + uint8_t sys_status = Cse7761Read(CSE7761_REG_SYSSTATUS, 1); #ifdef CSE7761_SIMULATE sys_status = 0x11; #endif @@ -336,29 +338,29 @@ void Cse7761GetData(void) { // The effective value of current and voltage Rms is a 24-bit signed number, the highest bit is 0 for valid data, // and when the highest bit is 1, the reading will be processed as zero // The active power parameter PowerA/B is in two’s complement format, 32-bit data, the highest bit is Sign bit. - uint32_t value = Cse7761ReadFallback(CSE7761_REG_RMSU, CSE7761Data.voltage_rms); + uint32_t value = Cse7761ReadFallback(CSE7761_REG_RMSU, CSE7761Data.voltage_rms, 3); #ifdef CSE7761_SIMULATE value = 2342160; // 237.7V #endif CSE7761Data.voltage_rms = (value >= 0x800000) ? 0 : value; - value = Cse7761ReadFallback(CSE7761_REG_RMSIA, CSE7761Data.current_rms[0]); + value = Cse7761ReadFallback(CSE7761_REG_RMSIA, CSE7761Data.current_rms[0], 3); #ifdef CSE7761_SIMULATE value = 455; #endif CSE7761Data.current_rms[0] = ((value >= 0x800000) || (value < 1600)) ? 0 : value; // No load threshold of 10mA - value = Cse7761ReadFallback(CSE7761_REG_POWERPA, CSE7761Data.active_power[0]); + value = Cse7761ReadFallback(CSE7761_REG_POWERPA, CSE7761Data.active_power[0], 4); #ifdef CSE7761_SIMULATE value = 217; #endif CSE7761Data.active_power[0] = (0 == CSE7761Data.current_rms[0]) ? 0 : (value & 0x80000000) ? (~value) + 1 : value; - value = Cse7761ReadFallback(CSE7761_REG_RMSIB, CSE7761Data.current_rms[1]); + value = Cse7761ReadFallback(CSE7761_REG_RMSIB, CSE7761Data.current_rms[1], 3); #ifdef CSE7761_SIMULATE value = 29760; // 0.185A #endif CSE7761Data.current_rms[1] = ((value >= 0x800000) || (value < 1600)) ? 0 : value; // No load threshold of 10mA - value = Cse7761ReadFallback(CSE7761_REG_POWERPB, CSE7761Data.active_power[1]); + value = Cse7761ReadFallback(CSE7761_REG_POWERPB, CSE7761Data.active_power[1], 4); #ifdef CSE7761_SIMULATE value = 2126641; // 44.05W #endif @@ -406,7 +408,7 @@ void Cse7761EverySecond(void) { Cse7761Write(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_RESET); } else if (2 == CSE7761Data.init) { - uint16_t syscon = Cse7761Read(0x00); // Default 0x0A04 + uint16_t syscon = Cse7761Read(0x00, 2); // Default 0x0A04 #ifdef CSE7761_SIMULATE syscon = 0x0A04; #endif @@ -469,15 +471,15 @@ bool Cse7761Command(void) { uint32_t value = (uint32_t)(CharToFloat(XdrvMailbox.data) * 100); // 1.23 = 123 if (CMND_POWERCAL == Energy.command_code) { - if (1 == XdrvMailbox.payload) { XdrvMailbox.payload = Cse7761Ref(3); } + if (1 == XdrvMailbox.payload) { XdrvMailbox.payload = Cse7761Ref(PowerPAC); } // Service in xdrv_03_energy.ino } else if (CMND_VOLTAGECAL == Energy.command_code) { - if (1 == XdrvMailbox.payload) { XdrvMailbox.payload = Cse7761Ref(1); } + if (1 == XdrvMailbox.payload) { XdrvMailbox.payload = Cse7761Ref(RmsUC); } // Service in xdrv_03_energy.ino } else if (CMND_CURRENTCAL == Energy.command_code) { - if (1 == XdrvMailbox.payload) { XdrvMailbox.payload = Cse7761Ref(2); } + if (1 == XdrvMailbox.payload) { XdrvMailbox.payload = Cse7761Ref(RmsIAC); } // Service in xdrv_03_energy.ino } else if (CMND_POWERSET == Energy.command_code) {