From 48faabc1cf299bbfb0797f60814ba4e52a19fca0 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Mon, 4 Feb 2019 18:17:50 +0100 Subject: [PATCH] Revert "Add user configuration of MCP39F501" This reverts commit c340e2b8bd3f6189b7ec0b5c5038effc15a7dcd5. --- sonoff/_changelog.ino | 3 +- sonoff/sonoff_template.h | 62 ++++++------ sonoff/xdrv_01_webserver.ino | 2 +- sonoff/xdrv_03_energy.ino | 3 - sonoff/xnrg_04_mcp39f501.ino | 185 ++++++++++++++++------------------- 5 files changed, 113 insertions(+), 142 deletions(-) diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index 339ae2bfe..42de29186 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -2,8 +2,7 @@ * Add SetOption32 until SetOption49 diagnostic information to Status 3 report as replacement for second property value in SetOption property name * Add Resolution property to Status 3 report providing previous SetOption second value property * Fix IR local echo - * Add user configuration of HLW8012 and HJL-01/BL0937 Energy Monitoring as used in Sonoff S31, Pow Ra and many Tuya based devices - * Add user configuration of MCP39F501 Energy Monitoring as used in Shelly2 + * Add user configuration of HLW8012 and HJL-01/BL0937 Energy Monitoring * * 6.4.1.13 20190130 * Add command SetOption36 to control boot loop default restoration (#4645, #5063) diff --git a/sonoff/sonoff_template.h b/sonoff/sonoff_template.h index f86a6725d..668dd02c5 100644 --- a/sonoff/sonoff_template.h +++ b/sonoff/sonoff_template.h @@ -159,9 +159,6 @@ enum UserSelectablePins { GPIO_NRG_CF1, // HLW8012/HLJ-01 CF1 voltage / current GPIO_HLW_CF, // HLW8012 CF power GPIO_HJL_CF, // HJL-01/BL0937 CF power - GPIO_MCP39F5_TX, // MCP39F501 Serial interface (Shelly2) - GPIO_MCP39F5_RX, // MCP39F501 Serial interface (Shelly2) - GPIO_MCP39F5_RST, // MCP39F501 Reset (Shelly2) GPIO_SENSOR_END }; // Programmer selectable GPIO functionality offset by user selectable GPIOs @@ -178,6 +175,9 @@ enum ProgramSelectablePins { GPIO_ROT_B, // Rotary switch B Pin GPIO_CSE7766_TX, // CSE7766 Serial interface (S31 and Pow R2) GPIO_CSE7766_RX, // CSE7766 Serial interface (S31 and Pow R2) + GPIO_MCP39_TX, // MCP39F501 Serial interface (Shelly2) + GPIO_MCP39_RX, // MCP39F501 Serial interface (Shelly2) + GPIO_MCP39_RST, // MCP39F501 Reset (Shelly2) GPIO_USER, // User configurable GPIO_MAX }; @@ -225,8 +225,7 @@ const char kSensorNames[] PROGMEM = D_SENSOR_MAX31855_CS "|" D_SENSOR_MAX31855_CLK "|" D_SENSOR_MAX31855_DO "|" D_SENSOR_BUTTON "1i|" D_SENSOR_BUTTON "2i|" D_SENSOR_BUTTON "3i|" D_SENSOR_BUTTON "4i|" D_SENSOR_BUTTON "1in|" D_SENSOR_BUTTON "2in|" D_SENSOR_BUTTON "3in|" D_SENSOR_BUTTON "4in|" - D_SENSOR_NRG_SEL "|" D_SENSOR_NRG_SEL "i|" D_SENSOR_NRG_CF1 "|" D_SENSOR_HLW_CF "|" D_SENSOR_HJL_CF "|" - D_SENSOR_MCP39F5_TX "|" D_SENSOR_MCP39F5_RX "|" D_SENSOR_MCP39F5_RST + D_SENSOR_NRG_SEL "|" D_SENSOR_NRG_SEL "i|" D_SENSOR_NRG_CF1 "|" D_SENSOR_HLW_CF "|" D_SENSOR_HJL_CF ; /********************************************************************************************/ @@ -472,17 +471,21 @@ const uint8_t kGpioNiceList[] PROGMEM = { GPIO_HX711_SCK, // HX711 Load Cell clock GPIO_HX711_DAT, // HX711 Load Cell data #endif -#if defined(USE_ENERGY_SENSOR) && defined(USE_HLW8012) - GPIO_NRG_SEL, // HLW8012/HLJ-01 Sel output (1 = Voltage) - GPIO_NRG_SEL_INV, // HLW8012/HLJ-01 Sel output (0 = Voltage) - GPIO_NRG_CF1, // HLW8012/HLJ-01 CF1 voltage / current - GPIO_HLW_CF, // HLW8012 CF power - GPIO_HJL_CF, // HJL-01/BL0937 CF power +#ifdef USE_SERIAL_BRIDGE + GPIO_SBR_TX, // Serial Bridge Serial interface + GPIO_SBR_RX, // Serial Bridge Serial interface #endif -#if defined(USE_ENERGY_SENSOR) && defined(USE_MCP39F501) - GPIO_MCP39F5_TX, // MCP39F501 Serial interface (Shelly2) - GPIO_MCP39F5_RX, // MCP39F501 Serial interface (Shelly2) - GPIO_MCP39F5_RST, // MCP39F501 Reset (Shelly2) +#ifdef USE_MHZ19 + GPIO_MHZ_TXD, // MH-Z19 Serial interface + GPIO_MHZ_RXD, // MH-Z19 Serial interface +#endif +#ifdef USE_SENSEAIR + GPIO_SAIR_TX, // SenseAir Serial interface + GPIO_SAIR_RX, // SenseAir Serial interface +#endif +#ifdef USE_NOVA_SDS + GPIO_SDS0X1_TX, // Nova Fitness SDS011 Serial interface + GPIO_SDS0X1_RX, // Nova Fitness SDS011 Serial interface #endif #if defined(USE_PZEM004T) || defined(USE_PZEM_AC) || defined(USE_PZEM_DC) GPIO_PZEM0XX_TX, // PZEM0XX Serial interface @@ -504,22 +507,6 @@ const uint8_t kGpioNiceList[] PROGMEM = { GPIO_SDM630_TX, // SDM630 Serial interface GPIO_SDM630_RX, // SDM630 Serial interface #endif -#ifdef USE_SERIAL_BRIDGE - GPIO_SBR_TX, // Serial Bridge Serial interface - GPIO_SBR_RX, // Serial Bridge Serial interface -#endif -#ifdef USE_MHZ19 - GPIO_MHZ_TXD, // MH-Z19 Serial interface - GPIO_MHZ_RXD, // MH-Z19 Serial interface -#endif -#ifdef USE_SENSEAIR - GPIO_SAIR_TX, // SenseAir Serial interface - GPIO_SAIR_RX, // SenseAir Serial interface -#endif -#ifdef USE_NOVA_SDS - GPIO_SDS0X1_TX, // Nova Fitness SDS011 Serial interface - GPIO_SDS0X1_RX, // Nova Fitness SDS011 Serial interface -#endif #ifdef USE_PMS5003 GPIO_PMS5003, // Plantower PMS5003 Serial interface #endif @@ -546,6 +533,13 @@ const uint8_t kGpioNiceList[] PROGMEM = { GPIO_MAX31855CLK, // MAX31855 Serial interface GPIO_MAX31855DO, // MAX31855 Serial interface #endif +#if defined(USE_ENERGY_SENSOR) && defined(USE_HLW8012) + GPIO_NRG_SEL, // HLW8012/HLJ-01 Sel output (1 = Voltage) + GPIO_NRG_SEL_INV, // HLW8012/HLJ-01 Sel output (0 = Voltage) + GPIO_NRG_CF1, // HLW8012/HLJ-01 CF1 voltage / current + GPIO_HLW_CF, // HLW8012 CF power + GPIO_HJL_CF, // HJL-01/BL0937 CF power +#endif }; const uint8_t kModuleNiceList[MAXMODULE] PROGMEM = { @@ -1448,9 +1442,9 @@ const mytmplt kModules[MAXMODULE] PROGMEM = { }, { "Shelly 2", // Shelly2 (ESP8266 - 2MB) - https://shelly.cloud/shelly2/ 0, - GPIO_MCP39F5_TX, // GPIO01 MCP39F501 Serial input + GPIO_MCP39_TX, // GPIO01 MCP39F501 Serial input 0, - GPIO_MCP39F5_RX, // GPIO03 MCP39F501 Serial output + GPIO_MCP39_RX, // GPIO03 MCP39F501 Serial output GPIO_REL1, // GPIO04 GPIO_REL2, // GPIO05 // GPIO06 (SD_CLK Flash) @@ -1462,7 +1456,7 @@ const mytmplt kModules[MAXMODULE] PROGMEM = { GPIO_SWT1, // GPIO12 0, GPIO_SWT2, // GPIO14 - GPIO_MCP39F5_RST, // GPIO15 MCP39F501 Reset + GPIO_MCP39_RST, // GPIO15 MCP39F501 Reset 0, GPIO_FLAG_PULLUP // Allow input pull-up control }, diff --git a/sonoff/xdrv_01_webserver.ino b/sonoff/xdrv_01_webserver.ino index 51498abb4..daf45be9f 100644 --- a/sonoff/xdrv_01_webserver.ino +++ b/sonoff/xdrv_01_webserver.ino @@ -864,7 +864,7 @@ void HandleModuleConfiguration(void) for (uint8_t i = 0; i < sizeof(cmodule); i++) { if (GPIO_USER == ValidGPIO(i, cmodule.io[i])) { snprintf_P(stemp, 3, PINS_WEMOS +i*2); - snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s " D_GPIO "%d %s"), + snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s " D_GPIO "%d %s"), (WEMOS==Settings.module)?stemp:"", i, (0==i)? D_SENSOR_BUTTON "1":(1==i)? D_SERIAL_OUT :(3==i)? D_SERIAL_IN :(9==i)? "ESP8285" :(10==i)? "ESP8285" :(12==i)? D_SENSOR_RELAY "1":(13==i)? D_SENSOR_LED "1i":(14==i)? D_SENSOR :"", i, i); page += mqtt_data; } diff --git a/sonoff/xdrv_03_energy.ino b/sonoff/xdrv_03_energy.ino index 62e445c91..4662fec3d 100644 --- a/sonoff/xdrv_03_energy.ino +++ b/sonoff/xdrv_03_energy.ino @@ -695,9 +695,6 @@ bool Xdrv03(uint8_t function) case FUNC_SET_POWER: EnergySetPowerSteadyCounter(); break; - case FUNC_LOOP: - XnrgCall(FUNC_LOOP); - break; case FUNC_SERIAL: result = XnrgCall(FUNC_SERIAL); break; diff --git a/sonoff/xnrg_04_mcp39f501.ino b/sonoff/xnrg_04_mcp39f501.ino index 53034a73c..4c58dfa90 100644 --- a/sonoff/xnrg_04_mcp39f501.ino +++ b/sonoff/xnrg_04_mcp39f501.ino @@ -63,11 +63,6 @@ #define MCP_FREQUENCY_GAIN_BASE 0x00AE #define MCP_FREQUENCY_LEN 4 -#define MCP_BUFFER_SIZE 60 - -#include -TasmotaSerial *McpSerial; - typedef struct mcp_cal_registers_type { uint16_t gain_current_rms; uint16_t gain_voltage_rms; @@ -91,9 +86,6 @@ typedef struct mcp_cal_registers_type { uint16_t accumulation_interval; } mcp_cal_registers_type; -char *mcp_buffer = NULL; // Serial receive buffer -int mcp_byte_counter = 0; // Index in serial receive buffer - unsigned long mcp_kWhcounter = 0; uint32_t mcp_system_configuration = 0x03000000; uint32_t mcp_active_power; @@ -154,7 +146,7 @@ void McpSend(uint8_t *data) // AddLogBuffer(LOG_LEVEL_DEBUG_MORE, data, data[1]); for (uint8_t i = 0; i < data[1]; i++) { - McpSerial->write(data[i]); + Serial.write(data[i]); } } @@ -170,7 +162,7 @@ void McpGetAddress(void) void McpAddressReceive(void) { // 06 05 004D 58 - mcp_address = mcp_buffer[3]; + mcp_address = serial_in_buffer[3]; } /********************************************************************************************/ @@ -191,26 +183,26 @@ void McpParseCalibration(void) mcp_cal_registers_type cal_registers; // 06 37 C882 B6AD 0781 9273 06000000 00000000 00000000 0000 D3FF 0300 00000003 9204 120C1300 204E0000 9808 E0AB0000 D9940000 0200 24 - cal_registers.gain_current_rms = McpExtractInt(mcp_buffer, 2, 2); - cal_registers.gain_voltage_rms = McpExtractInt(mcp_buffer, 4, 2); - cal_registers.gain_active_power = McpExtractInt(mcp_buffer, 6, 2); - cal_registers.gain_reactive_power = McpExtractInt(mcp_buffer, 8, 2); - cal_registers.offset_current_rms = McpExtractInt(mcp_buffer, 10, 4); - cal_registers.offset_active_power = McpExtractInt(mcp_buffer, 14, 4); - cal_registers.offset_reactive_power = McpExtractInt(mcp_buffer, 18, 4); - cal_registers.dc_offset_current = McpExtractInt(mcp_buffer, 22, 2); - cal_registers.phase_compensation = McpExtractInt(mcp_buffer, 24, 2); - cal_registers.apparent_power_divisor = McpExtractInt(mcp_buffer, 26, 2); + cal_registers.gain_current_rms = McpExtractInt(serial_in_buffer, 2, 2); + cal_registers.gain_voltage_rms = McpExtractInt(serial_in_buffer, 4, 2); + cal_registers.gain_active_power = McpExtractInt(serial_in_buffer, 6, 2); + cal_registers.gain_reactive_power = McpExtractInt(serial_in_buffer, 8, 2); + cal_registers.offset_current_rms = McpExtractInt(serial_in_buffer, 10, 4); + cal_registers.offset_active_power = McpExtractInt(serial_in_buffer, 14, 4); + cal_registers.offset_reactive_power = McpExtractInt(serial_in_buffer, 18, 4); + cal_registers.dc_offset_current = McpExtractInt(serial_in_buffer, 22, 2); + cal_registers.phase_compensation = McpExtractInt(serial_in_buffer, 24, 2); + cal_registers.apparent_power_divisor = McpExtractInt(serial_in_buffer, 26, 2); - cal_registers.system_configuration = McpExtractInt(mcp_buffer, 28, 4); - cal_registers.dio_configuration = McpExtractInt(mcp_buffer, 32, 2); - cal_registers.range = McpExtractInt(mcp_buffer, 34, 4); + cal_registers.system_configuration = McpExtractInt(serial_in_buffer, 28, 4); + cal_registers.dio_configuration = McpExtractInt(serial_in_buffer, 32, 2); + cal_registers.range = McpExtractInt(serial_in_buffer, 34, 4); - cal_registers.calibration_current = McpExtractInt(mcp_buffer, 38, 4); - cal_registers.calibration_voltage = McpExtractInt(mcp_buffer, 42, 2); - cal_registers.calibration_active_power = McpExtractInt(mcp_buffer, 44, 4); - cal_registers.calibration_reactive_power = McpExtractInt(mcp_buffer, 48, 4); - cal_registers.accumulation_interval = McpExtractInt(mcp_buffer, 52, 2); + cal_registers.calibration_current = McpExtractInt(serial_in_buffer, 38, 4); + cal_registers.calibration_voltage = McpExtractInt(serial_in_buffer, 42, 2); + cal_registers.calibration_active_power = McpExtractInt(serial_in_buffer, 44, 4); + cal_registers.calibration_reactive_power = McpExtractInt(serial_in_buffer, 48, 4); + cal_registers.accumulation_interval = McpExtractInt(serial_in_buffer, 52, 2); if (mcp_calibrate & MCP_CALIBRATE_POWER) { cal_registers.calibration_active_power = Settings.energy_power_calibration; @@ -381,8 +373,8 @@ void McpGetFrequency(void) void McpParseFrequency(void) { // 06 07 C350 8000 A0 - uint16_t line_frequency_ref = mcp_buffer[2] * 256 + mcp_buffer[3]; - uint16_t gain_line_frequency = mcp_buffer[4] * 256 + mcp_buffer[5]; + uint16_t line_frequency_ref = serial_in_buffer[2] * 256 + serial_in_buffer[3]; + uint16_t gain_line_frequency = serial_in_buffer[4] * 256 + serial_in_buffer[5]; if (mcp_calibrate & MCP_CALIBRATE_FREQUENCY) { line_frequency_ref = Settings.energy_frequency_calibration; @@ -446,12 +438,12 @@ void McpParseData(void) // 06 19 CE 18 00 00 F2 08 3A 38 00 00 66 00 00 00 93 38 00 00 36 7F 9A C6 B7 // Ak Ln Current---- Volt- ActivePower ReActivePow ApparentPow Factr Frequ Ck - mcp_current_rms = McpExtractInt(mcp_buffer, 2, 4); - mcp_voltage_rms = McpExtractInt(mcp_buffer, 6, 2); - mcp_active_power = McpExtractInt(mcp_buffer, 8, 4); -// mcp_reactive_power = McpExtractInt(mcp_buffer, 12, 4); -// mcp_power_factor = McpExtractInt(mcp_buffer, 20, 2); - mcp_line_frequency = McpExtractInt(mcp_buffer, 22, 2); + mcp_current_rms = McpExtractInt(serial_in_buffer, 2, 4); + mcp_voltage_rms = McpExtractInt(serial_in_buffer, 6, 2); + mcp_active_power = McpExtractInt(serial_in_buffer, 8, 4); +// mcp_reactive_power = McpExtractInt(serial_in_buffer, 12, 4); +// mcp_power_factor = McpExtractInt(serial_in_buffer, 20, 2); + mcp_line_frequency = McpExtractInt(serial_in_buffer, 22, 2); if (energy_power_on) { // Powered on energy_frequency = (float)mcp_line_frequency / 1000; @@ -472,52 +464,49 @@ void McpParseData(void) /********************************************************************************************/ -void McpSerialInput(void) +bool McpSerialInput(void) { - if (McpSerial->available()) { - unsigned long start = millis(); - while (millis() - start < 20) { - yield(); - if (McpSerial->available()) { - mcp_buffer[mcp_byte_counter++] = McpSerial->read(); - start = millis(); - } + serial_in_buffer[serial_in_byte_counter++] = serial_in_byte; + unsigned long start = millis(); + while (millis() - start < 20) { + yield(); + if (Serial.available()) { + serial_in_buffer[serial_in_byte_counter++] = Serial.read(); + start = millis(); } - - AddLogBuffer(LOG_LEVEL_DEBUG_MORE, (uint8_t*)mcp_buffer, mcp_byte_counter); - - if (1 == mcp_byte_counter) { - if (MCP_ERROR_CRC == mcp_buffer[0]) { -// AddLog_P(LOG_LEVEL_DEBUG, PSTR("MCP: Send " D_CHECKSUM_FAILURE)); - mcp_timeout = 0; - } - else if (MCP_ERROR_NAK == mcp_buffer[0]) { -// AddLog_P(LOG_LEVEL_DEBUG, PSTR("MCP: NAck")); - mcp_timeout = 0; - } - } - else if (MCP_ACK_FRAME == mcp_buffer[0]) { - if (mcp_byte_counter == mcp_buffer[1]) { - - if (McpChecksum((uint8_t *)mcp_buffer) != mcp_buffer[mcp_byte_counter -1]) { - AddLog_P(LOG_LEVEL_DEBUG, PSTR("MCP: " D_CHECKSUM_FAILURE)); - } else { - if (5 == mcp_buffer[1]) { McpAddressReceive(); } - if (25 == mcp_buffer[1]) { McpParseData(); } - if (MCP_CALIBRATION_LEN + 3 == mcp_buffer[1]) { McpParseCalibration(); } - if (MCP_FREQUENCY_LEN + 3 == mcp_buffer[1]) { McpParseFrequency(); } - } - - } - mcp_timeout = 0; - } - else if (MCP_SINGLE_WIRE == mcp_buffer[0]) { - mcp_timeout = 0; - } - - mcp_byte_counter = 0; - McpSerial->flush(); } + + AddLogSerial(LOG_LEVEL_DEBUG_MORE); + + if (1 == serial_in_byte_counter) { + if (MCP_ERROR_CRC == serial_in_buffer[0]) { +// AddLog_P(LOG_LEVEL_DEBUG, PSTR("MCP: Send " D_CHECKSUM_FAILURE)); + mcp_timeout = 0; + } + else if (MCP_ERROR_NAK == serial_in_buffer[0]) { +// AddLog_P(LOG_LEVEL_DEBUG, PSTR("MCP: NAck")); + mcp_timeout = 0; + } + } + else if (MCP_ACK_FRAME == serial_in_buffer[0]) { + if (serial_in_byte_counter == serial_in_buffer[1]) { + + if (McpChecksum((uint8_t *)serial_in_buffer) != serial_in_buffer[serial_in_byte_counter -1]) { + AddLog_P(LOG_LEVEL_DEBUG, PSTR("MCP: " D_CHECKSUM_FAILURE)); + } else { + if (5 == serial_in_buffer[1]) { McpAddressReceive(); } + if (25 == serial_in_buffer[1]) { McpParseData(); } + if (MCP_CALIBRATION_LEN + 3 == serial_in_buffer[1]) { McpParseCalibration(); } + if (MCP_FREQUENCY_LEN + 3 == serial_in_buffer[1]) { McpParseFrequency(); } + } + + } + mcp_timeout = 0; + } + else if (MCP_SINGLE_WIRE == serial_in_buffer[0]) { + mcp_timeout = 0; + } + return 1; } /********************************************************************************************/ @@ -554,33 +543,25 @@ void McpEverySecond(void) void McpSnsInit(void) { - // Software serial init needs to be done here as earlier (serial) interrupts may lead to Exceptions - McpSerial = new TasmotaSerial(pin[GPIO_MCP39F5_RX], pin[GPIO_MCP39F5_TX], 1); - if (McpSerial->begin(4800)) { - if (McpSerial->hardwareSerial()) { ClaimSerial(); } - if (pin[GPIO_MCP39F5_RST] < 99) { - digitalWrite(pin[GPIO_MCP39F5_RST], 1); // MCP enable - } - } else { - energy_flg = ENERGY_NONE; + SetSeriallog(LOG_LEVEL_NONE); // Free serial interface from logging interference + if (pin[GPIO_MCP39_RST] < 99) { + digitalWrite(pin[GPIO_MCP39_RST], 1); // MCP enable } } void McpDrvInit(void) { if (!energy_flg) { - if ((pin[GPIO_MCP39F5_RX] < 99) && (pin[GPIO_MCP39F5_TX] < 99)) { - mcp_buffer = (char*)(malloc(MCP_BUFFER_SIZE)); - if (mcp_buffer != NULL) { - if (pin[GPIO_MCP39F5_RST] < 99) { - pinMode(pin[GPIO_MCP39F5_RST], OUTPUT); - digitalWrite(pin[GPIO_MCP39F5_RST], 0); // MCP disable - Reset Delta Sigma ADC's - } - mcp_calibrate = 0; - mcp_timeout = 2; // Initial wait - mcp_init = 2; // Initial setup steps - energy_flg = XNRG_04; + if ((pin[GPIO_MCP39_RX] < 99) && (pin[GPIO_MCP39_TX] < 99)) { + if (pin[GPIO_MCP39_RST] < 99) { + pinMode(pin[GPIO_MCP39_RST], OUTPUT); + digitalWrite(pin[GPIO_MCP39_RST], 0); // MCP disable - Reset Delta Sigma ADC's } + baudrate = 4800; + mcp_calibrate = 0; + mcp_timeout = 2; // Initial wait + mcp_init = 2; // Initial setup steps + energy_flg = XNRG_04; } } } @@ -651,15 +632,15 @@ int Xnrg04(uint8_t function) case FUNC_INIT: McpSnsInit(); break; - case FUNC_LOOP: - McpSerialInput(); - break; case FUNC_EVERY_SECOND: McpEverySecond(); break; case FUNC_COMMAND: result = McpCommand(); break; + case FUNC_SERIAL: + result = McpSerialInput(); + break; } } return result;