From 2f47a4b5c974ade48d8bb7efcd59609fa6adad3a Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Mon, 7 Dec 2020 17:03:22 +0100 Subject: [PATCH] Refactor serial init --- tasmota/support.ino | 8 ++++++-- tasmota/tasmota.ino | 13 ++++++------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/tasmota/support.ino b/tasmota/support.ino index ef6b3813b..cb60c41fa 100644 --- a/tasmota/support.ino +++ b/tasmota/support.ino @@ -911,6 +911,10 @@ String GetSerialConfig(void) { return String(config); } +uint32_t GetSerialBaudrate(void) { + return (Serial.baudRate() / 300) * 300; // Fix ESP32 strange results like 115201 +} + void SetSerialBegin(void) { TasmotaGlobal.baudrate = Settings.baudrate * 300; AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_SERIAL "Set to %s %d bit/s"), GetSerialConfig().c_str(), TasmotaGlobal.baudrate); @@ -940,7 +944,7 @@ void SetSerialConfig(uint32_t serial_config) { void SetSerialBaudrate(uint32_t baudrate) { TasmotaGlobal.baudrate = baudrate; Settings.baudrate = TasmotaGlobal.baudrate / 300; - if (Serial.baudRate() != TasmotaGlobal.baudrate) { + if (GetSerialBaudrate() != TasmotaGlobal.baudrate) { SetSerialBegin(); } } @@ -958,7 +962,7 @@ void ClaimSerial(void) { TasmotaGlobal.serial_local = true; AddLog_P(LOG_LEVEL_INFO, PSTR("SNS: Hardware Serial")); SetSeriallog(LOG_LEVEL_NONE); - TasmotaGlobal.baudrate = Serial.baudRate(); + TasmotaGlobal.baudrate = GetSerialBaudrate(); Settings.baudrate = TasmotaGlobal.baudrate / 300; } diff --git a/tasmota/tasmota.ino b/tasmota/tasmota.ino index 3dce0c860..d8d5a82f9 100644 --- a/tasmota/tasmota.ino +++ b/tasmota/tasmota.ino @@ -193,6 +193,7 @@ void setup(void) { memset(&TasmotaGlobal, 0, sizeof(TasmotaGlobal)); TasmotaGlobal.baudrate = APP_BAUDRATE; + TasmotaGlobal.seriallog_timer = SERIALLOG_TIMER; TasmotaGlobal.temperature_celsius = NAN; TasmotaGlobal.blinks = 201; TasmotaGlobal.wifi_state_flag = WIFI_RESTART; @@ -229,20 +230,20 @@ void setup(void) { OsWatchInit(); + TasmotaGlobal.seriallog_level = Settings.seriallog_level; + TasmotaGlobal.syslog_level = Settings.syslog_level; + TasmotaGlobal.module_changed = (Settings.module != Settings.last_module); if (TasmotaGlobal.module_changed) { Settings.baudrate = APP_BAUDRATE / 300; Settings.serial_config = TS_SERIAL_8N1; } - SetSerialBegin(); + SetSerialBaudrate(Settings.baudrate * 300); // Reset serial interface if current baudrate is different from requested baudrate - if (1 == RtcReboot.fast_reboot_count) { // Allow setting override only when all is well + if (1 == RtcReboot.fast_reboot_count) { // Allow setting override only when all is well UpdateQuickPowerCycle(true); } - TasmotaGlobal.seriallog_level = Settings.seriallog_level; - TasmotaGlobal.seriallog_timer = SERIALLOG_TIMER; - TasmotaGlobal.syslog_level = Settings.syslog_level; TasmotaGlobal.stop_flash_rotate = Settings.flag.stop_flash_rotate; // SetOption12 - Switch between dynamic or fixed slot flash save location TasmotaGlobal.save_data_counter = Settings.save_data; TasmotaGlobal.sleep = Settings.sleep; @@ -305,8 +306,6 @@ void setup(void) { GetEspHardwareType(); GpioInit(); -// SetSerialBaudrate(Settings.baudrate * 300); // Allow reset of serial interface if current baudrate is different from requested baudrate - WifiConnect(); SetPowerOnState();