From dc8f2d4a63bdb5840f78e3bf62a2e00fdcd03dd3 Mon Sep 17 00:00:00 2001 From: Jason2866 <24528715+Jason2866@users.noreply.github.com> Date: Wed, 3 Mar 2021 11:22:18 +0100 Subject: [PATCH 1/6] Flash mode has to be DIO --- boards/esp32s2.json | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/boards/esp32s2.json b/boards/esp32s2.json index b9142d4c8..d53adefc6 100644 --- a/boards/esp32s2.json +++ b/boards/esp32s2.json @@ -4,21 +4,23 @@ "ldscript": "esp32s2_out.ld" }, "core": "esp32", - "mcu": "esp32s2", - "extra_flags": "-Desp32S2_dev_module", "f_cpu": "240000000L", "f_flash": "80000000L", - "flash_mode": "qio", + "flash_mode": "dio", "mcu": "esp32s2", "variant": "esp32s2" }, "connectivity": [ "wifi" ], + "debug": { + "openocd_target": "esp32s2.cfg" + }, "frameworks": [ + "espidf", "arduino" ], - "name": "ESP32S2 Dev Module", + "name": "Espressif ESP32-S2-Saola-1", "upload": { "flash_size": "4MB", "maximum_ram_size": 327680, @@ -26,6 +28,6 @@ "require_upload_port": true, "speed": 460800 }, - "url": "https://espressif.com", - "vendor": "espressif" + "url": "https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/hw-reference/esp32s2/user-guide-saola-1-v1.2.html", + "vendor": "Espressif" } From 605e5931edcbc7bbc78a9afb09ce308ff899e108 Mon Sep 17 00:00:00 2001 From: Jason2866 <24528715+Jason2866@users.noreply.github.com> Date: Wed, 3 Mar 2021 11:25:32 +0100 Subject: [PATCH 2/6] Use qio for S2 every other mode generates `SHA-256 comparison failed` --- platformio_override_sample.ini | 1 + 1 file changed, 1 insertion(+) diff --git a/platformio_override_sample.ini b/platformio_override_sample.ini index 1f124264d..3b7ff4d7a 100644 --- a/platformio_override_sample.ini +++ b/platformio_override_sample.ini @@ -171,6 +171,7 @@ build_flags = ${common32.build_flags} [env:tasmota32s2] extends = env:tasmota32 board = esp32s2 +board_build.flash_mode = qio platform_packages = framework-arduinoespressif32 @ https://github.com/Jason2866/arduino-esp32/releases/download/s2-1.0.5-rc6/esp32-s2-1.0.5-rc6.zip platformio/tool-mklittlefs @ ~1.203.200522 platformio/tool-esptoolpy @ ~1.30000.0 From 4d0db83b62f355f940679ad74d81c5201c6c6f9d Mon Sep 17 00:00:00 2001 From: bovirus <1262554+bovirus@users.noreply.github.com> Date: Wed, 3 Mar 2021 14:48:05 +0100 Subject: [PATCH 3/6] Update Italian language --- tasmota/language/it_IT.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tasmota/language/it_IT.h b/tasmota/language/it_IT.h index 3adc683d8..3540c0cb0 100644 --- a/tasmota/language/it_IT.h +++ b/tasmota/language/it_IT.h @@ -1,7 +1,7 @@ /* it-IT.h - localization for Italian - Italy for Tasmota - Copyright (C) 2021 Gennaro Tortone - some mods by Antonio Fragola - Updated by bovirus - rev. 27.02.2021 + Copyright (C) 2021 Gennaro Tortone - some mods by Antonio Fragola - Updated by bovirus - rev. 03.03.2021 This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -673,9 +673,9 @@ #define D_SENSOR_HJL_CF "BL0937 - CF" #define D_SENSOR_MCP39F5_TX "MCP39F5 - TX" #define D_SENSOR_MCP39F5_RX "MCP39F5 - RX" -#define D_SENSOR_MCP39F5_RST "MCP39F5 - Reset" -#define D_SENSOR_CSE7761_TX "CSE7761 - Tx" -#define D_SENSOR_CSE7761_RX "CSE7761 - Rx" +#define D_SENSOR_MCP39F5_RST "MCP39F5 - RESET" +#define D_SENSOR_CSE7761_TX "CSE7761 - TX" +#define D_SENSOR_CSE7761_RX "CSE7761 - RX" #define D_SENSOR_CSE7766_TX "CSE7766 - TX" #define D_SENSOR_CSE7766_RX "CSE7766 - RX" #define D_SENSOR_PN532_TX "PN532 - TX" @@ -696,10 +696,10 @@ #define D_SENSOR_HRE_DATA "HRE - Dati" #define D_SENSOR_ADE7953_IRQ "ADE7953 - IRQ" #define D_SENSOR_BUZZER "Cicalino" -#define D_SENSOR_OLED_RESET "OLED - Reset" +#define D_SENSOR_OLED_RESET "OLED - RESET" #define D_SENSOR_ZIGBEE_TXD "Zigbee - TX" #define D_SENSOR_ZIGBEE_RXD "Zigbee - RX" -#define D_SENSOR_ZIGBEE_RST "Zigbee - Reset" +#define D_SENSOR_ZIGBEE_RST "Zigbee - RESET" #define D_SENSOR_SOLAXX1_TX "SolaxX1 - TX" #define D_SENSOR_SOLAXX1_RX "SolaxX1 - RX" #define D_SENSOR_IBEACON_TX "iBeacon - TX" From 22408beacd5d830346e1039317cf49be1e8b63ae Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Wed, 3 Mar 2021 17:51:33 +0100 Subject: [PATCH 4/6] Fix CSE7761 read crc calculation --- tasmota/xnrg_19_cse7761.ino | 156 +++++++++++++++++++++++++----------- 1 file changed, 110 insertions(+), 46 deletions(-) diff --git a/tasmota/xnrg_19_cse7761.ino b/tasmota/xnrg_19_cse7761.ino index e83dc09b5..adbe094ff 100644 --- a/tasmota/xnrg_19_cse7761.ino +++ b/tasmota/xnrg_19_cse7761.ino @@ -25,25 +25,63 @@ * Based on datasheet from ChipSea \*********************************************************************************************/ -#define XNRG_19 19 +#define XNRG_19 19 -#define CSE7761_K1 2 // Current channel sampling resistance in milli Ohm -#define CSE7761_K2 2 // Voltage divider resistance in 1k/1M +#define CSE7761_REMOVE_CHECKS -#define CSE7761_2POWER22 4194304 -#define CSE7761_2POWER23 8388608 -#define CSE7761_2POWER31 2147483648 +#define CSE7761_DUAL_K1 2 // Current channel sampling resistance in milli Ohm +#define CSE7761_DUAL_K2 2 // Voltage divider resistance in 1k/1M +#define CSE7761_DUAL_CLK1 3579545.0f // System clock (3.579545MHz) as used in frequency calculation -enum CSE7761 { RmsIAC, RmsIBC, RmsUC, PowerPAC, PowerPBC, PowerSC, EnergyAc, EnergyBC }; +#define CSE7761_2POWER22 4194304 +#define CSE7761_2POWER23 8388608 +#define CSE7761_2POWER31 2147483648 + +#define CSE7761_REG_SYSCON 0x00 // System Control Register +#define CSE7761_REG_EMUCON 0x01 // Metering control register +#define CSE7761_REG_EMUCON2 0x13 // Metering control register 2 + +#define CSE7761_REG_UFREQ 0x23 // Voltage Frequency Register +#define CSE7761_REG_RMSIA 0x24 // The effective value of channel A current +#define CSE7761_REG_RMSIB 0x25 // The effective value of channel B current +#define CSE7761_REG_RMSU 0x26 // Voltage RMS +#define CSE7761_REG_POWERPA 0x2C // Channel A active power, update rate 27.2Hz +#define CSE7761_REG_POWERPB 0x2D // Channel B active power, update rate 27.2Hz +#define CSE7761_REG_SYSSTATUS 0x43 // System status register + +#define CSE7761_REG_COEFFOFFSET 0x6E // Coefficient checksum offset (0xFFFF) +#define CSE7761_REG_COEFFCHKSUM 0x6F // Coefficient checksum +#define CSE7761_REG_RMSIAC 0x70 // Channel A effective current conversion coefficient +#define CSE7761_REG_RMSIBC 0x71 // Channel B effective current conversion coefficient +#define CSE7761_REG_RMSUC 0x72 // Effective voltage conversion coefficient +#define CSE7761_REG_POWERPAC 0x73 // Channel A active power conversion coefficient +#define CSE7761_REG_POWERPBC 0x74 // Channel B active power conversion coefficient +#define CSE7761_REG_POWERSC 0x75 // Apparent power conversion coefficient +#define CSE7761_REG_ENERGYAC 0x76 // Channel A energy conversion coefficient +#define CSE7761_REG_ENERGYBC 0x77 // Channel B energy conversion coefficient + +#define CSE7761_SPECIAL_COMMAND 0xEA // Start special command +#define CSE7761_CMD_RESET 0x96 // Reset command, after receiving the command, the chip resets +#define CSE7761_CMD_CHAN_A_SELECT 0x5A // Current channel A setting command, which specifies the current used to calculate apparent power, + // Power factor, phase angle, instantaneous active power, instantaneous apparent power and + // The channel indicated by the signal of power overload is channel A +#define CSE7761_CMD_CHAN_B_SELECT 0xA5 // Current channel B setting command, which specifies the current used to calculate apparent power, + // Power factor, phase angle, instantaneous active power, instantaneous apparent power and + // The channel indicated by the signal of power overload is channel B +#define CSE7761_CMD_CLOSE_WRITE 0xDC // Close write operation +#define CSE7761_CMD_ENABLE_WRITE 0xE5 // Enable write operation + +enum CSE7761 { RmsIAC, RmsIBC, RmsUC, PowerPAC, PowerPBC, PowerSC, EnergyAC, EnergyBC }; #include TasmotaSerial *Cse7761Serial = nullptr; struct { + uint32_t frequency = 0; uint32_t voltage_rms = 0; uint32_t current_rms[2] = { 0 }; - uint32_t active_power[2] = { 0 }; + int active_power[2] = { 0 }; uint16_t coefficient[8] = { 0 }; uint8_t init = 0; bool found = false; @@ -74,10 +112,10 @@ void Cse7761Write(uint32_t reg, uint32_t data) { Cse7761Serial->write(buffer, len); - AddLog(LOG_LEVEL_DEBUG, PSTR("C61: Send %d, Data %*_H"), len, len, buffer); + AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("C61: Send %d, Data %*_H"), len, len, buffer); } -uint32_t Cse7761Read(uint32_t reg, uint32_t request) { +uint32_t Cse7761Read(uint32_t reg) { Cse7761Serial->flush(); Cse7761Write(reg, 0); @@ -92,21 +130,25 @@ uint32_t Cse7761Read(uint32_t reg, uint32_t request) { } if (!rcvd) { - AddLog(LOG_LEVEL_DEBUG, PSTR("C61: Rcvd %d"), rcvd); + AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("C61: Rcvd %d"), rcvd); return 0; } - AddLog(LOG_LEVEL_DEBUG, PSTR("C61: Rcvd %d, Data %*_H"), rcvd, rcvd, buffer); + AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("C61: Rcvd %d, Data %*_H"), rcvd, rcvd, buffer); int result = 0; uint8_t crc = 0xA5 + reg; + rcvd--; for (uint32_t i = 0; i < rcvd -1; i++) { result = (result << 8) | buffer[i]; crc += buffer[i]; } crc = ~crc; if (crc != buffer[rcvd]) { + AddLog(LOG_LEVEL_DEBUG, PSTR("C61: CRC error")); +#ifndef CSE7761_REMOVE_CHECKS result = 0; +#endif } return result; @@ -115,22 +157,23 @@ uint32_t Cse7761Read(uint32_t reg, uint32_t request) { bool Cse7761ChipInit(void) { uint16_t calc_chksum = 0xFFFF; for (uint32_t i = 0; i < 8; i++) { - CSE7761Data.coefficient[i] = Cse7761Read(0x70 + i, 2); + CSE7761Data.coefficient[i] = Cse7761Read(CSE7761_REG_RMSIAC + i); calc_chksum += CSE7761Data.coefficient[i]; } - uint16_t dummy = Cse7761Read(0x6E, 2); - uint16_t coeff_chksum = Cse7761Read(0x6F, 2); + calc_chksum = ~calc_chksum; + uint16_t dummy = Cse7761Read(CSE7761_REG_COEFFOFFSET); + uint16_t coeff_chksum = Cse7761Read(CSE7761_REG_COEFFCHKSUM); if (calc_chksum != coeff_chksum) { - AddLog(LOG_LEVEL_DEBUG, PSTR("C61: Coefficients invalid")); -// return false; + AddLog(LOG_LEVEL_DEBUG, PSTR("C61: Coefficients CRC error")); +#ifndef CSE7761_REMOVE_CHECKS + return false; +#endif } - delay(3); - Cse7761Write(0xEA, 0xE5); // Enable write operation - delay(5); - uint8_t sys_status = Cse7761Read(0x43, 1); + Cse7761Write(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_ENABLE_WRITE); + delay(8); + uint8_t sys_status = Cse7761Read(CSE7761_REG_SYSSTATUS); if (sys_status & 0x10) { // Write enable to protected registers (WREN) - delay(3); /* System Control Register (SYSCON) Addr:0x00 Default value: 0x0A04 Bit name Function description @@ -140,25 +183,25 @@ bool Cse7761ChipInit(void) { =0, means ADC current channel B is closed 9 NC -, the default is 1. 8-6 PGAIB[2:0] Current channel B analog gain selection highest bit - =1XX, PGA of current channel B=16 + =1XX, PGA of current channel B=16 (Sonoff Dual R3 Pow) =011, PGA of current channel B=8 =010, PGA of current channel B=4 =001, PGA of current channel B=2 - =000, PGA of current channel B=1 (Sonoff Dual R3 Pow) + =000, PGA of current channel B=1 5-3 PGAU[2:0] Highest bit of voltage channel analog gain selection =1XX, PGA of current channel U=16 =011, PGA of current channel U=8 =010, PGA of current channel U=4 - =001, PGA of current channel U=2 (Sonoff Dual R3 Pow) - =000, PGA of current channel U=1 + =001, PGA of current channel U=2 + =000, PGA of current channel U=1 (Sonoff Dual R3 Pow) 2-0 PGAIA[2:0] Current channel A analog gain selection highest bit - =1XX, PGA of current channel A=16 + =1XX, PGA of current channel A=16 (Sonoff Dual R3 Pow) =011, PGA of current channel A=8 =010, PGA of current channel A=4 =001, PGA of current channel A=2 - =000, PGA of current channel A=1 (Sonoff Dual R3 Pow) + =000, PGA of current channel A=1 */ - Cse7761Write(0x80, 0xFF04); // Set SYSCON + Cse7761Write(CSE7761_REG_SYSCON | 0x80, 0xFF04); /* Energy Measure Control Register (EMUCON) Addr:0x01 Default value: 0x0000 Bit name Function description @@ -205,14 +248,14 @@ bool Cse7761ChipInit(void) { =1, enable PFA pulse output and active energy register accumulation; (Sonoff Dual R3 Pow) =0 (default), turn off PFA pulse output and active energy register accumulation. */ - Cse7761Write(0x81, 0x1003); // Set EMUCON + Cse7761Write(CSE7761_REG_EMUCON | 0x80, 0x1003); /* Energy Measure Control Register (EMUCON2) Addr: 0x13 Default value: 0x0001 Bit name Function description 15-13 NC - 12 SDOCmos - =1, SDO pin CMOS open-drain output (Sonoff Dual R3 Pow) - =0, SDO pin CMOS output + =1, SDO pin CMOS open-drain output + =0, SDO pin CMOS output (Sonoff Dual R3 Pow) 11 EPB_CB Energy_PB clear signal control, the default is 0, and it needs to be configured to 1 in UART mode. Clear after reading is not supported in UART mode =1, Energy_PB will not be cleared after reading; (Sonoff Dual R3 Pow) @@ -249,33 +292,50 @@ bool Cse7761ChipInit(void) { =0, turn off the peak detection function (Sonoff Dual R3 Pow) 0 NC Default is 1 */ - Cse7761Write(0x93, 0x0FC1); // Set EMUCON2 + Cse7761Write(CSE7761_REG_EMUCON2 | 0x80, 0x0FC1); } else { AddLog(LOG_LEVEL_DEBUG, PSTR("C61: Write enable failed")); -// return false; +#ifndef CSE7761_REMOVE_CHECKS + return false; +#endif } delay(80); - Cse7761Write(0xEA, 0xDC); // Close write operation + Cse7761Write(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_CLOSE_WRITE); return true; } void Cse7761GetData(void) { - CSE7761Data.voltage_rms = Cse7761Read(0x26, 3); - CSE7761Data.current_rms[0] = Cse7761Read(0x24, 3); - CSE7761Data.active_power[0] = Cse7761Read(0x2C, 4); - CSE7761Data.current_rms[1] = Cse7761Read(0x25, 3); - CSE7761Data.active_power[1] = Cse7761Read(0x2D, 4); + CSE7761Data.voltage_rms = Cse7761Read(CSE7761_REG_RMSU); + CSE7761Data.frequency = Cse7761Read(CSE7761_REG_UFREQ); + CSE7761Data.current_rms[0] = Cse7761Read(CSE7761_REG_RMSIA); + CSE7761Data.active_power[0] = Cse7761Read(CSE7761_REG_POWERPA); + CSE7761Data.current_rms[1] = Cse7761Read(CSE7761_REG_RMSIB); + CSE7761Data.active_power[1] = Cse7761Read(CSE7761_REG_POWERPB); + + AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("C61: U %d, F %d, I %d/%d, P %d/%d"), + CSE7761Data.voltage_rms, CSE7761Data.frequency, + CSE7761Data.current_rms[0], CSE7761Data.current_rms[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 two’s complement format, 32-bit data, the highest bit is Sign bit. if (Energy.power_on) { // Powered on - Energy.voltage[0] = ((float)CSE7761Data.voltage_rms * ((double)CSE7761Data.coefficient[RmsUC] / (CSE7761_K2 * 2 * CSE7761_2POWER22))) / 1000; // V + Energy.voltage[0] = ((float)CSE7761Data.voltage_rms * ((double)CSE7761Data.coefficient[RmsUC] / (CSE7761_DUAL_K2 * 2 * CSE7761_2POWER22))) / 1000; // V + Energy.frequency[0] = CSE7761_DUAL_CLK1 / 8 / ((float)CSE7761Data.frequency + 1); for (uint32_t channel = 0; channel < 2; channel++) { Energy.data_valid[channel] = 0; - Energy.active_power[channel] = (float)CSE7761Data.active_power[channel] * ((double)CSE7761Data.coefficient[PowerPAC + channel] / (CSE7761_K1 * CSE7761_K2 * 2 * CSE7761_2POWER31)); // W + Energy.active_power[channel] = (float)CSE7761Data.active_power[channel] * ((double)CSE7761Data.coefficient[PowerPAC + channel] / (CSE7761_DUAL_K1 * CSE7761_DUAL_K2 * 2 * CSE7761_2POWER31)); // W if (0 == Energy.active_power[channel]) { Energy.current[channel] = 0; } else { - Energy.current[channel] = (float)CSE7761Data.current_rms[channel] * ((double)CSE7761Data.coefficient[RmsIAC + channel] / (CSE7761_K1 * 2 * CSE7761_2POWER23)); // mA + Energy.current[channel] = (float)CSE7761Data.current_rms[channel] * ((double)CSE7761Data.coefficient[RmsIAC + channel] / (CSE7761_DUAL_K1 * 2 * CSE7761_2POWER23)); // mA } } @@ -295,13 +355,17 @@ void Cse7761GetData(void) { void Cse7761EverySecond(void) { if (CSE7761Data.init) { if (2 == CSE7761Data.init) { - Cse7761Write(0xEA, 0x96); // Reset chip + Cse7761Write(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_RESET); } else if (1 == CSE7761Data.init) { - uint16_t syscon = Cse7761Read(0x00, 2); // Default 0x0A04 + uint16_t syscon = Cse7761Read(0x00); // Default 0x0A04 +#ifndef CSE7761_REMOVE_CHECKS if (0x0A04 == syscon) { CSE7761Data.found = Cse7761ChipInit(); } +#else + CSE7761Data.found = Cse7761ChipInit(); +#endif if (CSE7761Data.found) { AddLog(LOG_LEVEL_INFO, PSTR("C61: CSE7761 found")); } @@ -320,7 +384,7 @@ void Cse7761SnsInit(void) { Cse7761Serial = new TasmotaSerial(Pin(GPIO_CSE7761_RX), Pin(GPIO_CSE7761_TX), 1); if (Cse7761Serial->begin(38400, SERIAL_8E1)) { if (Cse7761Serial->hardwareSerial()) { -// SetSerial(38400, TS_SERIAL_8E1); + SetSerial(38400, TS_SERIAL_8E1); ClaimSerial(); } } else { From 2b1be6ef3d31b754a18e7ac75c44c7eedc2e321b Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Wed, 3 Mar 2021 18:17:54 +0100 Subject: [PATCH 5/6] Fix CSE7761 read crc calculation --- tasmota/xnrg_19_cse7761.ino | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tasmota/xnrg_19_cse7761.ino b/tasmota/xnrg_19_cse7761.ino index adbe094ff..3973b7ade 100644 --- a/tasmota/xnrg_19_cse7761.ino +++ b/tasmota/xnrg_19_cse7761.ino @@ -136,10 +136,16 @@ uint32_t Cse7761Read(uint32_t reg) { AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("C61: Rcvd %d, Data %*_H"), rcvd, rcvd, buffer); - int result = 0; - uint8_t crc = 0xA5 + reg; +#ifndef CSE7761_REMOVE_CHECKS + if (rcvd > 5) { + return 0; + } +#endif + rcvd--; - for (uint32_t i = 0; i < rcvd -1; i++) { + uint32_t result = 0; + uint8_t crc = 0xA5 + reg; + for (uint32_t i = 0; i < rcvd; i++) { result = (result << 8) | buffer[i]; crc += buffer[i]; } @@ -147,7 +153,7 @@ uint32_t Cse7761Read(uint32_t reg) { if (crc != buffer[rcvd]) { AddLog(LOG_LEVEL_DEBUG, PSTR("C61: CRC error")); #ifndef CSE7761_REMOVE_CHECKS - result = 0; + return 0; #endif } @@ -398,6 +404,7 @@ void Cse7761DrvInit(void) { CSE7761Data.init = 3; // Init setup steps Energy.phase_count = 2; // Handle two channels as two phases Energy.voltage_common = true; // Use common voltage + Energy.frequency_common = true; // Use common frequency TasmotaGlobal.energy_driver = XNRG_19; } } From 42a1dc3d9aec09f6bc8a67daec69eb5d05b7fa47 Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Wed, 3 Mar 2021 18:40:13 +0000 Subject: [PATCH 6/6] Enable restart of BLE after upload. Add cmnd 'BLEEnableUnsaved' to enable BLE without saveing the enable. Fix a bug with operations - where the op could be stolen by mqtt before begin seen by another driver. --- tasmota/xdrv_79_esp32_ble.ino | 135 ++++++++++++++++++++++------------ 1 file changed, 90 insertions(+), 45 deletions(-) diff --git a/tasmota/xdrv_79_esp32_ble.ino b/tasmota/xdrv_79_esp32_ble.ino index 5775c3016..706536675 100644 --- a/tasmota/xdrv_79_esp32_ble.ino +++ b/tasmota/xdrv_79_esp32_ble.ino @@ -347,6 +347,10 @@ TaskHandle_t TasmotaMainTask; static int BLEMasterEnable = 0; +static uint8_t BLEEnableUnsaved = 0; +static uint8_t BLEEnableMask = 1; + + static int BLEInitState = 0; static int BLERunningScan = 0; static uint32_t BLEScanCount = 0; @@ -391,8 +395,10 @@ BLE_ESP32::generic_sensor_t* prepOperation = nullptr; std::deque queuedOperations; // operations in progress (at the moment, only one) std::deque currentOperations; -// operations which have completed or failed, ready to send to MQTT +// operations which have completed or failed std::deque completedOperations; +// operations which are ready to send to MQTT +std::deque mqttOperations; // seen devices #define MAX_BLE_DEVICES_LOGGED 80 @@ -422,7 +428,7 @@ std::deque aliases; #define D_CMND_BLE "BLE" const char kBLE_Commands[] PROGMEM = D_CMND_BLE "|" - "Period|Adv|Op|Mode|Details|Scan|Alias|Name|Debug|Devices|MaxAge|AddrFilter"; + "Period|Adv|Op|Mode|Details|Scan|Alias|Name|Debug|Devices|MaxAge|AddrFilter|EnableUnsaved"; static void CmndBLEPeriod(void); static void CmndBLEAdv(void); @@ -436,6 +442,7 @@ static void CmndBLEDebug(void); static void CmndBLEDevices(void); static void CmndBLEMaxAge(void); static void CmndBLEAddrFilter(void); +static void CmndBLEEnableUnsaved(void); void (*const BLE_Commands[])(void) PROGMEM = { &BLE_ESP32::CmndBLEPeriod, @@ -449,7 +456,8 @@ void (*const BLE_Commands[])(void) PROGMEM = { &BLE_ESP32::CmndBLEDebug, &BLE_ESP32::CmndBLEDevices, &BLE_ESP32::CmndBLEMaxAge, - &BLE_ESP32::CmndBLEAddrFilter + &BLE_ESP32::CmndBLEAddrFilter, + &BLE_ESP32::CmndBLEEnableUnsaved }; const char *successStates[] PROGMEM = { @@ -1412,8 +1420,12 @@ static void BLEGenNotifyCB(NimBLERemoteCharacteristic* pRemoteCharacteristic, ui #endif } else { if (devaddr == op->addr){ - thisop = op; - break; + if (op->notifytimer){ + thisop = op; + break; + } else { + AddLog(LOG_LEVEL_ERROR,PSTR("BLE: notify: op addr match but op found which is not waiting.")); + } } } } @@ -1511,13 +1523,6 @@ static void BLEInit(void) { BLEInitState = 1; - // dont start of disabled - BLEMasterEnable = Settings.flag5.mi32_enable; - if (!BLEMasterEnable) return; - - - StartBLE(); - return; } @@ -1807,12 +1812,12 @@ static void BLETaskRunCurrentOperation(BLE_ESP32::generic_sensor_t** pCurrentOpe #endif op->notifylen = 0; if(pNCharacteristic->canNotify()) { + uint64_t now = esp_timer_get_time(); + op->notifytimer = now; if(pNCharacteristic->subscribe(true, BLE_ESP32::BLEGenNotifyCB)) { #ifdef BLE_ESP32_DEBUG if (BLEDebugMode > 0) AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: subscribe for notify")); #endif - uint64_t now = esp_timer_get_time(); - op->notifytimer = now; // this will get changed to read or write, // but here in case it's notify only (can that happen?) notifystate = GEN_STATE_WAITNOTIFY; @@ -1822,22 +1827,24 @@ static void BLETaskRunCurrentOperation(BLE_ESP32::generic_sensor_t** pCurrentOpe AddLog(LOG_LEVEL_ERROR,PSTR("BLE: failed subscribe for notify")); #endif newstate = GEN_STATE_FAILED_NOTIFY; + op->notifytimer = 0L; } } else { if(pNCharacteristic->canIndicate()) { + uint64_t now = esp_timer_get_time(); + op->notifytimer = now; if(pNCharacteristic->subscribe(false, BLE_ESP32::BLEGenNotifyCB)) { #ifdef BLE_ESP32_DEBUG AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: subscribe for indicate")); #endif notifystate = GEN_STATE_WAITINDICATE; - uint64_t now = esp_timer_get_time(); - op->notifytimer = now; waitNotify = true; } else { #ifdef BLE_ESP32_DEBUG AddLog(LOG_LEVEL_ERROR,PSTR("BLE: failed subscribe for indicate")); #endif newstate = GEN_STATE_FAILED_INDICATE; + op->notifytimer = 0L; } } else { newstate = GEN_STATE_FAILED_CANTNOTIFYORINDICATE; @@ -2147,6 +2154,23 @@ void BLEEvery50mSecond(){ } +static void stopStartBLE(){ + // dont start of disabled + uint8_t enable = (Settings.flag5.mi32_enable || BLEEnableUnsaved) && BLEEnableMask; + + if (enable != BLEMasterEnable){ + if (enable){ + if (StartBLE()){ + BLEMasterEnable = enable; + } + } else { + if (StopBLE()){ + BLEMasterEnable = enable; + } + } + AddLog(LOG_LEVEL_INFO,PSTR("BLE: MasterEnable->%d"), BLEMasterEnable); + } +} /** * @brief Main loop of the driver, "high level"-loop @@ -2159,27 +2183,14 @@ static void BLEEverySecond(bool restart){ checkDeviceTimouts(); - - if (Settings.flag5.mi32_enable != BLEMasterEnable){ - if (Settings.flag5.mi32_enable){ - if (StartBLE()){ - BLEMasterEnable = Settings.flag5.mi32_enable; - } - } else { - if (StopBLE()){ - BLEMasterEnable = Settings.flag5.mi32_enable; - } - } - AddLog(LOG_LEVEL_INFO,PSTR("BLE: MasterEnable->%d"), BLEMasterEnable); - } - + stopStartBLE(); // check for application callbacks here. // this may remove complete items. BLE_ESP32::mainThreadOpCallbacks(); // post any MQTT data if we completed anything in the last second - if (completedOperations.size()){ + if (mqttOperations.size()){ BLE_ESP32::BLEPostMQTT(true); // send only completed } @@ -2314,12 +2325,18 @@ int extQueueOperation(BLE_ESP32::generic_sensor_t** op){ AddLog(LOG_LEVEL_ERROR,PSTR("BLE: op invalid")); return 0; } + + if (!BLEMasterEnable){ + AddLog(LOG_LEVEL_ERROR,PSTR("BLE: extQueueOperation: BLE is deiabled")); + return 0; + } + (*op)->state = GEN_STATE_START; // trigger request later (*op)->opid = lastopid++; int res = addOperation(&queuedOperations, op); if (!res){ - AddLog(LOG_LEVEL_ERROR,PSTR("BLE: extQueueOperation: op added id %d failed"), (lastopid-1)); + AddLog(LOG_LEVEL_ERROR,PSTR("BLE: extQueueOperation: op adding id %d failed"), (lastopid-1)); } return res; } @@ -2451,7 +2468,7 @@ static int StopBLE(void){ AddLog(LOG_LEVEL_INFO,PSTR("BLE: StopBLE - BLEStop->1")); BLEStopAt = esp_timer_get_time(); // give a little time for it to stop. - vTaskDelay(1000/ portTICK_PERIOD_MS); + vTaskDelay(100/ portTICK_PERIOD_MS); return 1; } AddLog(LOG_LEVEL_ERROR,PSTR("BLE: StopBLE - wait as BLEStop==1")); @@ -2654,6 +2671,21 @@ void CmndBLEMode(void){ } } +////////////////////////////////////////////////////////////// +// Enables BLE even if master enable is unset +// use to temporarily enable after boot - e.g. at the end of autoexec +void CmndBLEEnableUnsaved(void){ + int val = -1; + if (XdrvMailbox.data_len > 0) { + val = XdrvMailbox.payload; + } + + if (val >= 0){ + BLEEnableUnsaved = val; + } + ResponseCmndNumber(BLEEnableUnsaved); +} + ////////////////////////////////////////// // get more drtails for a single MAC address @@ -3036,7 +3068,7 @@ static void BLEPostMQTT(bool onlycompleted) { // if (TasmotaGlobal.ota_state_flag) return; - if (prepOperation || completedOperations.size() || queuedOperations.size() || currentOperations.size()){ + if (prepOperation || mqttOperations.size() || queuedOperations.size() || currentOperations.size()){ #ifdef BLE_ESP32_DEBUG if (BLEDebugMode > 0) AddLog(LOG_LEVEL_INFO,PSTR("BLE: some to show")); #endif @@ -3094,17 +3126,17 @@ static void BLEPostMQTT(bool onlycompleted) { } } - if (completedOperations.size()){ + if (mqttOperations.size()){ #ifdef BLE_ESP32_DEBUG - if (BLEDebugMode > 0) AddLog(LOG_LEVEL_INFO,PSTR("BLE: completed %d"), completedOperations.size()); + if (BLEDebugMode > 0) AddLog(LOG_LEVEL_INFO,PSTR("BLE: completed %d"), mqttOperations.size()); #endif do { - generic_sensor_t *toSend = nextOperation(&completedOperations); + generic_sensor_t *toSend = nextOperation(&mqttOperations); if (!toSend) { break; // break from while loop } else { #ifdef BLE_ESP32_DEBUG - if (BLEDebugMode > 0) AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: completedOperation removed")); + if (BLEDebugMode > 0) AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: mqttOperation removed opid %d"), toSend->opid); #endif std::string out = BLETriggerResponse(toSend); snprintf_P(TasmotaGlobal.mqtt_data, sizeof(TasmotaGlobal.mqtt_data), PSTR("%s"), out.c_str()); @@ -3176,12 +3208,13 @@ static void mainThreadOpCallbacks() { bool callbackres = false; + if (BLEDebugMode > 0) AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: op->completecallback is %u opid %d"), op->completecallback, op->opid); if (op->completecallback){ try { OPCOMPLETE_CALLBACK *pFn = (OPCOMPLETE_CALLBACK *)(op->completecallback); callbackres = pFn(op); #ifdef BLE_ESP32_DEBUG - if (BLEDebugMode > 0) AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: op->completecallback %d"), callbackres); + if (BLEDebugMode > 0) AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: op->completecallback %d opid %d"), callbackres, op->opid); #endif } catch(const std::exception& e){ #ifdef BLE_ESP32_DEBUG @@ -3193,6 +3226,7 @@ static void mainThreadOpCallbacks() { if (!callbackres){ for (int i = 0; i < operationsCallbacks.size(); i++){ try { + if (BLEDebugMode > 0) AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: operationsCallbacks %d is %u"), i, operationsCallbacks[i]); OPCOMPLETE_CALLBACK *pFn = operationsCallbacks[i]; callbackres = pFn(op); #ifdef BLE_ESP32_DEBUG @@ -3209,12 +3243,16 @@ static void mainThreadOpCallbacks() { } } - // if some callback told us not to send on MQTT, then remove from completed and delete the data - if (callbackres){ + // always remove from here + completedOperations.erase(completedOperations.begin() + i); + // unless some callback told us not to send on MQTT, then remove from completed and + // add to mqtt list + if (!callbackres){ + addOperation(&mqttOperations, &op); + } else { #ifdef BLE_ESP32_DEBUG - if (BLEDebugMode > 0) AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: callbackres true -> delete op")); + if (BLEDebugMode > 0) AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: callbackres true -> delete op opid %d"), op->opid); #endif - completedOperations.erase(completedOperations.begin() + i); delete op; } } @@ -3462,8 +3500,15 @@ void HandleBleConfiguration(void) int ExtStopBLE(){ AddLog(LOG_LEVEL_INFO, PSTR("BLE: Stopping if active")); - BLE_ESP32::BLEMode = BLE_ESP32::BLEModeDisabled; - BLE_ESP32::StopBLE(); + BLE_ESP32::BLEEnableMask = 0; + BLE_ESP32::stopStartBLE(); + return 0; +} + +int ExtRestartBLEIfEnabled(){ + AddLog(LOG_LEVEL_INFO, PSTR("BLE: Starting if active")); + BLE_ESP32::BLEEnableMask = 1; + BLE_ESP32::stopStartBLE(); return 0; }