From 5a73aa1e1f50d1f97ce87227cec29dac6bad8107 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Thu, 30 Dec 2021 14:19:24 +0100 Subject: [PATCH] Refactor serial config Refactor serial config removing specific stopbit count and only support serial config constants (#14212) --- .../TasmotaSerial-3.3.0/src/TasmotaSerial.cpp | 27 +++++++++---------- tasmota/xdrv_44_miel_hvac.ino | 8 +++--- tasmota/xdrv_53_projector_ctrl.ino | 2 +- tasmota/xnrg_06_pzem_dc.ino | 2 +- tasmota/xnrg_14_bl09xx.ino | 2 +- tasmota/xsns_22_sr04.ino | 2 +- 6 files changed, 20 insertions(+), 23 deletions(-) diff --git a/lib/default/TasmotaSerial-3.3.0/src/TasmotaSerial.cpp b/lib/default/TasmotaSerial-3.3.0/src/TasmotaSerial.cpp index 07754719e..5cd0614fe 100644 --- a/lib/default/TasmotaSerial-3.3.0/src/TasmotaSerial.cpp +++ b/lib/default/TasmotaSerial-3.3.0/src/TasmotaSerial.cpp @@ -121,21 +121,6 @@ bool TasmotaSerial::isValidGPIOpin(int pin) { bool TasmotaSerial::begin(uint32_t speed, uint32_t config) { if (!m_valid) { return false; } - if (config > 2) { - // Legacy support where software serial fakes two stop bits if either stop bits is 2 or parity is not None - m_stop_bits = ((config &0x30) >> 5) +1; - if ((1 == m_stop_bits) && (config &0x03)) { - m_stop_bits++; - } - } else { - m_stop_bits = ((config -1) &1) +1; -#ifdef ESP8266 - config = (2 == m_stop_bits) ? (uint32_t)SERIAL_8N2 : (uint32_t)SERIAL_8N1; -#endif // ESP8266 -#ifdef ESP32 - config = (2 == m_stop_bits) ? SERIAL_8N2 : SERIAL_8N1; -#endif // ESP32 - } if (m_hardserial) { #ifdef ESP8266 @@ -181,6 +166,18 @@ bool TasmotaSerial::begin(uint32_t speed, uint32_t config) { // Serial.printf("TSR: Using UART%d\n", m_uart); #endif // ESP32 } else { + // Software serial fakes two stop bits if either stop bits is 2 or parity is not None + // #define UART_NB_STOP_BIT_0 0B00000000 + // #define UART_NB_STOP_BIT_1 0B00010000 + // #define UART_NB_STOP_BIT_15 0B00100000 + // #define UART_NB_STOP_BIT_2 0B00110000 + m_stop_bits = ((config &0x30) >> 5) +1; + // #define UART_PARITY_NONE 0B00000000 + // #define UART_PARITY_EVEN 0B00000010 + // #define UART_PARITY_ODD 0B00000011 + if ((1 == m_stop_bits) && (config &0x03)) { + m_stop_bits++; + } // Use getCycleCount() loop to get as exact timing as possible m_bit_time = ESP.getCpuFreqMHz() * 1000000 / speed; m_bit_start_time = m_bit_time + m_bit_time/3 - (ESP.getCpuFreqMHz() > 120 ? 700 : 500); // pre-compute first wait diff --git a/tasmota/xdrv_44_miel_hvac.ino b/tasmota/xdrv_44_miel_hvac.ino index 66e034fec..e5f37fbc1 100644 --- a/tasmota/xdrv_44_miel_hvac.ino +++ b/tasmota/xdrv_44_miel_hvac.ino @@ -42,7 +42,7 @@ #include /* from hvac */ -bool temp_type = false; +bool temp_type = false; struct miel_hvac_header { uint8_t start; @@ -713,7 +713,7 @@ miel_hvac_cmnd_settemp(void) if (XdrvMailbox.data_len == 0) return; - + degc = strtof(XdrvMailbox.data, nullptr); if (degc < MIEL_HVAC_UPDATE_TEMP_MIN || degc > MIEL_HVAC_UPDATE_TEMP_MAX) { @@ -1069,7 +1069,7 @@ miel_hvac_pre_init(void) sc->sc_serial = new TasmotaSerial(Pin(GPIO_MIEL_HVAC_RX), Pin(GPIO_MIEL_HVAC_TX), 2); - if (!sc->sc_serial->begin(baudrate, 2)) { + if (!sc->sc_serial->begin(baudrate, SERIAL_8E1)) { AddLog(LOG_LEVEL_ERROR, PSTR(MIEL_HVAC_LOGNAME ": unable to begin serial " "(baudrate %d)"), baudrate); @@ -1207,7 +1207,7 @@ miel_hvac_connect(struct miel_hvac_softc *sc) return; } - serial->begin(baudrate, 2); + serial->begin(baudrate, SERIAL_8E1); if (serial->hardwareSerial()) SetSerial(baudrate, TS_SERIAL_8E1); } diff --git a/tasmota/xdrv_53_projector_ctrl.ino b/tasmota/xdrv_53_projector_ctrl.ino index 6e559b71e..1fd8f8446 100644 --- a/tasmota/xdrv_53_projector_ctrl.ino +++ b/tasmota/xdrv_53_projector_ctrl.ino @@ -112,7 +112,7 @@ projector_ctrl_pre_init(void) sc->sc_serial = new TasmotaSerial(Pin(GPIO_PROJECTOR_CTRL_RX), Pin(GPIO_PROJECTOR_CTRL_TX), 2); - if (!sc->sc_serial->begin(baudrate, 2)) { + if (!sc->sc_serial->begin(baudrate)) { AddLog(LOG_LEVEL_ERROR, PSTR(PROJECTOR_CTRL_LOGNAME ": unable to begin serial " "(baudrate %d)"), baudrate); goto del; diff --git a/tasmota/xnrg_06_pzem_dc.ino b/tasmota/xnrg_06_pzem_dc.ino index 074152684..4e325594f 100644 --- a/tasmota/xnrg_06_pzem_dc.ino +++ b/tasmota/xnrg_06_pzem_dc.ino @@ -112,7 +112,7 @@ void PzemDcEverySecond(void) void PzemDcSnsInit(void) { PzemDcModbus = new TasmotaModbus(Pin(GPIO_PZEM017_RX), Pin(GPIO_PZEM0XX_TX)); - uint8_t result = PzemDcModbus->Begin(9600, 2); // Uses two stop bits!! + uint8_t result = PzemDcModbus->Begin(9600, SERIAL_8N2); if (result) { if (2 == result) { ClaimSerial(); } Energy.type_dc = true; diff --git a/tasmota/xnrg_14_bl09xx.ino b/tasmota/xnrg_14_bl09xx.ino index bca55c513..040739c24 100644 --- a/tasmota/xnrg_14_bl09xx.ino +++ b/tasmota/xnrg_14_bl09xx.ino @@ -279,7 +279,7 @@ void Bl09XXEverySecond(void) { void Bl09XXInit(void) { // Software serial init needs to be done here as earlier (serial) interrupts may lead to Exceptions Bl09XXSerial = new TasmotaSerial(Bl09XX.rx_pin, Pin(GPIO_TXD), 1); - if (Bl09XXSerial->begin(4800, 1)) { + if (Bl09XXSerial->begin(4800)) { if (Bl09XXSerial->hardwareSerial()) { ClaimSerial(); } diff --git a/tasmota/xsns_22_sr04.ino b/tasmota/xsns_22_sr04.ino index d941b25c2..7a1b10670 100644 --- a/tasmota/xsns_22_sr04.ino +++ b/tasmota/xsns_22_sr04.ino @@ -50,7 +50,7 @@ uint8_t Sr04TModeDetect(void) int sr04_trig_pin = (PinUsed(GPIO_SR04_TRIG)) ? Pin(GPIO_SR04_TRIG) : Pin(GPIO_SR04_ECHO); // if GPIO_SR04_TRIG is not configured use single PIN mode with GPIO_SR04_ECHO only sonar_serial = new TasmotaSerial(sr04_echo_pin, sr04_trig_pin, 1); - if (sonar_serial->begin(9600,1)) { + if (sonar_serial->begin(9600)) { DEBUG_SENSOR_LOG(PSTR("SR4: Detect mode")); if (sr04_trig_pin != -1) {