Merge branch 'development' of github.com:arendst/Tasmota into pr_tm1638

This commit is contained in:
Ajith Vasudevan 2021-03-05 08:16:51 +05:30
commit edd44e256b
1 changed files with 120 additions and 71 deletions

View File

@ -27,15 +27,13 @@
#define XNRG_19 19 #define XNRG_19 19
#define CSE7761_REMOVE_CHECKS #define CSE7761_DUAL_K1 1 // Current channel sampling resistance in milli Ohm
#define CSE7761_DUAL_K2 1 // Voltage divider resistance in 1k/1M
#define CSE7761_DUAL_CLK1 3579545 // System clock (3.579545MHz) used in frequency calculation
#define CSE7761_DUAL_K1 2 // Current channel sampling resistance in milli Ohm #define CSE7761_UREF 4194304 // 2^22
#define CSE7761_DUAL_K2 2 // Voltage divider resistance in 1k/1M #define CSE7761_IREF 8388608 // 2^23
#define CSE7761_DUAL_CLK1 3579545.0f // System clock (3.579545MHz) as used in frequency calculation #define CSE7761_PREF 2147483648 // 2^31
#define CSE7761_2POWER22 4194304
#define CSE7761_2POWER23 8388608
#define CSE7761_2POWER31 2147483648
#define CSE7761_REG_SYSCON 0x00 // System Control Register #define CSE7761_REG_SYSCON 0x00 // System Control Register
#define CSE7761_REG_EMUCON 0x01 // Metering control register #define CSE7761_REG_EMUCON 0x01 // Metering control register
@ -81,10 +79,10 @@ struct {
uint32_t frequency = 0; uint32_t frequency = 0;
uint32_t voltage_rms = 0; uint32_t voltage_rms = 0;
uint32_t current_rms[2] = { 0 }; uint32_t current_rms[2] = { 0 };
int active_power[2] = { 0 }; uint32_t energy[2] = { 0 };
uint16_t coefficient[8] = { 0 }; uint32_t active_power[2] = { 0 };
uint8_t init = 0; uint8_t init = 4;
bool found = false; uint8_t ready = 0;
} CSE7761Data; } CSE7761Data;
void Cse7761Write(uint32_t reg, uint32_t data) { void Cse7761Write(uint32_t reg, uint32_t data) {
@ -112,14 +110,15 @@ void Cse7761Write(uint32_t reg, uint32_t data) {
Cse7761Serial->write(buffer, len); Cse7761Serial->write(buffer, len);
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("C61: Send %d, Data %*_H"), len, len, buffer); AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("C61: Tx %*_H"), len, buffer);
} }
uint32_t Cse7761Read(uint32_t reg) { uint32_t Cse7761Read(uint32_t reg) {
Cse7761Serial->flush(); while (Cse7761Serial->available()) { Cse7761Serial->read(); }
Cse7761Write(reg, 0); Cse7761Write(reg, 0);
uint8_t buffer[8]; uint8_t buffer[8] = { 0 };
uint32_t rcvd = 0; uint32_t rcvd = 0;
uint32_t timeout = millis() + 3; uint32_t timeout = millis() + 3;
while (!TimeReached(timeout)) { while (!TimeReached(timeout)) {
@ -130,17 +129,14 @@ uint32_t Cse7761Read(uint32_t reg) {
} }
if (!rcvd) { if (!rcvd) {
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("C61: Rcvd %d"), rcvd); AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("C61: Rx %d"), rcvd);
return 0; return 0;
} }
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("C61: Rx %*_H"), rcvd, buffer);
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("C61: Rcvd %d, Data %*_H"), rcvd, rcvd, buffer);
#ifndef CSE7761_REMOVE_CHECKS
if (rcvd > 5) { if (rcvd > 5) {
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("C61: Rx overflow"));
return 0; return 0;
} }
#endif
rcvd--; rcvd--;
uint32_t result = 0; uint32_t result = 0;
@ -151,29 +147,34 @@ uint32_t Cse7761Read(uint32_t reg) {
} }
crc = ~crc; crc = ~crc;
if (crc != buffer[rcvd]) { if (crc != buffer[rcvd]) {
AddLog(LOG_LEVEL_DEBUG, PSTR("C61: CRC error")); AddLog(LOG_LEVEL_DEBUG, PSTR("C61: Rx CRC error"));
#ifndef CSE7761_REMOVE_CHECKS
return 0; return 0;
#endif
} }
return result; return result;
} }
bool Cse7761ChipInit(void) { bool Cse7761ChipInit(void) {
uint16_t coefficient[8] = { 0 };
uint16_t calc_chksum = 0xFFFF; uint16_t calc_chksum = 0xFFFF;
for (uint32_t i = 0; i < 8; i++) { for (uint32_t i = 0; i < 8; i++) {
CSE7761Data.coefficient[i] = Cse7761Read(CSE7761_REG_RMSIAC + i); coefficient[i] = Cse7761Read(CSE7761_REG_RMSIAC + i);
calc_chksum += CSE7761Data.coefficient[i]; calc_chksum += coefficient[i];
} }
calc_chksum = ~calc_chksum; calc_chksum = ~calc_chksum;
uint16_t dummy = Cse7761Read(CSE7761_REG_COEFFOFFSET); uint16_t dummy = Cse7761Read(CSE7761_REG_COEFFOFFSET);
uint16_t coeff_chksum = Cse7761Read(CSE7761_REG_COEFFCHKSUM); uint16_t coeff_chksum = Cse7761Read(CSE7761_REG_COEFFCHKSUM);
if (calc_chksum != coeff_chksum) { if (calc_chksum != coeff_chksum) {
AddLog(LOG_LEVEL_DEBUG, PSTR("C61: Coefficients CRC error")); AddLog(LOG_LEVEL_DEBUG, PSTR("C61: Default coefficients"));
#ifndef CSE7761_REMOVE_CHECKS coefficient[RmsIAC] = 0xCC11;
return false; coefficient[RmsUC] = 0xA643;
#endif coefficient[PowerPAC] = 0xADE1;
}
if (HLW_PREF_PULSE == Settings.energy_power_calibration) {
Settings.energy_voltage_calibration = 1000; // Gain 1 * 1000
Settings.energy_frequency_calibration = 2750;
Settings.energy_current_calibration = 160; // Gain 16 * 10
Settings.energy_power_calibration = 50000;
} }
Cse7761Write(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_ENABLE_WRITE); Cse7761Write(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_ENABLE_WRITE);
@ -301,55 +302,46 @@ bool Cse7761ChipInit(void) {
Cse7761Write(CSE7761_REG_EMUCON2 | 0x80, 0x0FC1); Cse7761Write(CSE7761_REG_EMUCON2 | 0x80, 0x0FC1);
} else { } else {
AddLog(LOG_LEVEL_DEBUG, PSTR("C61: Write enable failed")); AddLog(LOG_LEVEL_DEBUG, PSTR("C61: Write enable failed"));
#ifndef CSE7761_REMOVE_CHECKS
return false; return false;
#endif
} }
delay(80);
Cse7761Write(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_CLOSE_WRITE);
return true; return true;
} }
void Cse7761GetData(void) { void Cse7761GetData(void) {
CSE7761Data.voltage_rms = Cse7761Read(CSE7761_REG_RMSU);
CSE7761Data.frequency = Cse7761Read(CSE7761_REG_UFREQ); CSE7761Data.frequency = Cse7761Read(CSE7761_REG_UFREQ);
CSE7761Data.current_rms[0] = Cse7761Read(CSE7761_REG_RMSIA); uint32_t value = Cse7761Read(CSE7761_REG_RMSU);
CSE7761Data.active_power[0] = Cse7761Read(CSE7761_REG_POWERPA); // The effective value of current and voltage Rms is a 24-bit signed number, the highest bit is 0 for valid data,
CSE7761Data.current_rms[1] = Cse7761Read(CSE7761_REG_RMSIB); // and when the highest bit is 1, the reading will be processed as zero
CSE7761Data.active_power[1] = Cse7761Read(CSE7761_REG_POWERPB); CSE7761Data.voltage_rms = (value >= 0x800000) ? 0 : value;
value = Cse7761Read(CSE7761_REG_RMSIA);
CSE7761Data.current_rms[0] = (value >= 0x800000) ? 0 : value;
value = Cse7761Read(CSE7761_REG_RMSIB);
CSE7761Data.current_rms[1] = (value >= 0x800000) ? 0 : value;
// The active power parameter PowerA/B is in twos complement format, 32-bit data, the highest bit is Sign bit.
value = Cse7761Read(CSE7761_REG_POWERPA);
CSE7761Data.active_power[0] = (value & 0x80000000) ? (~value) + 1 : value;
value = Cse7761Read(CSE7761_REG_POWERPB);
CSE7761Data.active_power[1] = (value & 0x80000000) ? (~value) + 1 : value;
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("C61: U %d, F %d, I %d/%d, P %d/%d"), AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("C61: U %d, F %d, I %d/%d, P %d/%d"),
CSE7761Data.voltage_rms, CSE7761Data.frequency, CSE7761Data.voltage_rms, CSE7761Data.frequency,
CSE7761Data.current_rms[0], CSE7761Data.current_rms[1], CSE7761Data.current_rms[0], CSE7761Data.current_rms[1],
CSE7761Data.active_power[0], CSE7761Data.active_power[1]); CSE7761Data.active_power[0], CSE7761Data.active_power[1]);
// 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
if (CSE7761Data.voltage_rms & 0x800000) { CSE7761Data.voltage_rms = 0; }
if (CSE7761Data.current_rms[0] & 0x800000) { CSE7761Data.current_rms[0] = 0; }
if (CSE7761Data.current_rms[1] & 0x800000) { CSE7761Data.current_rms[1] = 0; }
// The active power parameter PowerA/B is in twos complement format, 32-bit data, the highest bit is Sign bit.
if (Energy.power_on) { // Powered on if (Energy.power_on) { // Powered on
Energy.voltage[0] = ((float)CSE7761Data.voltage_rms * ((double)CSE7761Data.coefficient[RmsUC] / (CSE7761_DUAL_K2 * 2 * CSE7761_2POWER22))) / 1000; // V Energy.voltage[0] = ((float)CSE7761Data.voltage_rms / Settings.energy_voltage_calibration); // V
Energy.frequency[0] = CSE7761_DUAL_CLK1 / 8 / ((float)CSE7761Data.frequency + 1); Energy.frequency[0] = (float)Settings.energy_frequency_calibration / ((float)CSE7761Data.frequency + 1); // Hz
for (uint32_t channel = 0; channel < 2; channel++) { for (uint32_t channel = 0; channel < 2; channel++) {
Energy.data_valid[channel] = 0; Energy.data_valid[channel] = 0;
Energy.active_power[channel] = (float)CSE7761Data.active_power[channel] * ((double)CSE7761Data.coefficient[PowerPAC + channel] / (CSE7761_DUAL_K1 * CSE7761_DUAL_K2 * 2 * CSE7761_2POWER31)); // W Energy.active_power[channel] = (float)CSE7761Data.active_power[channel] / Settings.energy_power_calibration; // W
if (0 == Energy.active_power[channel]) { if (0 == Energy.active_power[channel]) {
Energy.current[channel] = 0; Energy.current[channel] = 0;
} else { } else {
Energy.current[channel] = (float)CSE7761Data.current_rms[channel] * ((double)CSE7761Data.coefficient[RmsIAC + channel] / (CSE7761_DUAL_K1 * 2 * CSE7761_2POWER23)); // mA Energy.current[channel] = ((float)CSE7761Data.current_rms[channel] / Settings.energy_current_calibration) / 10; // mA
CSE7761Data.energy[channel] += Energy.active_power[channel];
} }
} }
uint32_t active_power_sum = (Energy.active_power[0] + Energy.active_power[1]) * 1000;
if (active_power_sum) {
Energy.kWhtoday_delta += active_power_sum / 36;
EnergyUpdateToday();
}
} else { // Powered off } else { // Powered off
Energy.data_valid[0] = ENERGY_WATCHDOG; Energy.data_valid[0] = ENERGY_WATCHDOG;
Energy.data_valid[1] = ENERGY_WATCHDOG; Energy.data_valid[1] = ENERGY_WATCHDOG;
@ -358,29 +350,41 @@ void Cse7761GetData(void) {
/********************************************************************************************/ /********************************************************************************************/
void Cse7761Every200ms(void) {
if (2 == CSE7761Data.ready) {
Cse7761GetData();
}
}
void Cse7761EverySecond(void) { void Cse7761EverySecond(void) {
if (CSE7761Data.init) { if (CSE7761Data.init) {
if (2 == CSE7761Data.init) { if (3 == CSE7761Data.init) {
Cse7761Write(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_RESET); Cse7761Write(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_RESET);
} }
else if (1 == CSE7761Data.init) { else if (2 == CSE7761Data.init) {
uint16_t syscon = Cse7761Read(0x00); // Default 0x0A04 uint16_t syscon = Cse7761Read(0x00); // Default 0x0A04
#ifndef CSE7761_REMOVE_CHECKS if ((0x0A04 == syscon) && Cse7761ChipInit()) {
if (0x0A04 == syscon) { CSE7761Data.ready = 1;
CSE7761Data.found = Cse7761ChipInit();
} }
#else }
CSE7761Data.found = Cse7761ChipInit(); else if (1 == CSE7761Data.init) {
#endif if (1 == CSE7761Data.ready) {
if (CSE7761Data.found) { Cse7761Write(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_CLOSE_WRITE);
AddLog(LOG_LEVEL_INFO, PSTR("C61: CSE7761 found")); AddLog(LOG_LEVEL_INFO, PSTR("C61: CSE7761 found"));
CSE7761Data.ready = 2;
} }
} }
CSE7761Data.init--; CSE7761Data.init--;
} }
else { else {
if (CSE7761Data.found) { if (2 == CSE7761Data.ready) {
Cse7761GetData(); uint32_t energy_sum = (CSE7761Data.energy[0] + CSE7761Data.energy[1]) * 1000;
if (energy_sum) {
Energy.kWhtoday_delta += energy_sum / 36;
EnergyUpdateToday();
CSE7761Data.energy[0] = 0;
CSE7761Data.energy[1] = 0;
}
} }
} }
} }
@ -400,8 +404,8 @@ void Cse7761SnsInit(void) {
void Cse7761DrvInit(void) { void Cse7761DrvInit(void) {
if (PinUsed(GPIO_CSE7761_RX) && PinUsed(GPIO_CSE7761_TX)) { if (PinUsed(GPIO_CSE7761_RX) && PinUsed(GPIO_CSE7761_TX)) {
CSE7761Data.found = false; CSE7761Data.ready = 0;
CSE7761Data.init = 3; // Init setup steps CSE7761Data.init = 4; // Init setup steps
Energy.phase_count = 2; // Handle two channels as two phases Energy.phase_count = 2; // Handle two channels as two phases
Energy.voltage_common = true; // Use common voltage Energy.voltage_common = true; // Use common voltage
Energy.frequency_common = true; // Use common frequency Energy.frequency_common = true; // Use common frequency
@ -409,6 +413,45 @@ void Cse7761DrvInit(void) {
} }
} }
bool Cse7761Command(void) {
bool serviced = true;
uint32_t channel = (2 == XdrvMailbox.index) ? 1 : 0;
uint32_t value = (uint32_t)(CharToFloat(XdrvMailbox.data) * 100); // 1.23 = 123
if (CMND_POWERSET == Energy.command_code) {
if (XdrvMailbox.data_len && CSE7761Data.active_power[channel]) {
if ((value > 100) && (value < 200000)) { // Between 1W and 2000W
Settings.energy_power_calibration = (CSE7761Data.active_power[channel] * 100) / value;
}
}
}
else if (CMND_VOLTAGESET == Energy.command_code) {
if (XdrvMailbox.data_len && CSE7761Data.voltage_rms) {
if ((value > 10000) && (value < 26000)) { // Between 100V and 260V
Settings.energy_voltage_calibration = (CSE7761Data.voltage_rms * 100) / value;
}
}
}
else if (CMND_CURRENTSET == Energy.command_code) {
if (XdrvMailbox.data_len && CSE7761Data.current_rms[channel]) {
if ((value > 2000) && (value < 1000000)) { // Between 20mA and 10A
Settings.energy_current_calibration = (CSE7761Data.current_rms[channel] * 100) / value;
}
}
}
else if (CMND_FREQUENCYSET == Energy.command_code) {
if (XdrvMailbox.data_len && CSE7761Data.frequency) {
if ((value > 4500) && (value < 6500)) { // Between 45Hz and 65Hz
Settings.energy_frequency_calibration = CSE7761Data.frequency * value / 100;
}
}
}
else serviced = false; // Unknown command
return serviced;
}
/*********************************************************************************************\ /*********************************************************************************************\
* Interface * Interface
\*********************************************************************************************/ \*********************************************************************************************/
@ -417,9 +460,15 @@ bool Xnrg19(uint8_t function) {
bool result = false; bool result = false;
switch (function) { switch (function) {
case FUNC_EVERY_SECOND: case FUNC_EVERY_200_MSECOND:
Cse7761Every200ms();
break;
case FUNC_ENERGY_EVERY_SECOND:
Cse7761EverySecond(); Cse7761EverySecond();
break; break;
case FUNC_COMMAND:
result = Cse7761Command();
break;
case FUNC_INIT: case FUNC_INIT:
Cse7761SnsInit(); Cse7761SnsInit();
break; break;