Refactor serial init

This commit is contained in:
Theo Arends 2020-12-07 17:03:22 +01:00
parent b9a3fd4342
commit 2f47a4b5c9
2 changed files with 12 additions and 9 deletions

View File

@ -911,6 +911,10 @@ String GetSerialConfig(void) {
return String(config); return String(config);
} }
uint32_t GetSerialBaudrate(void) {
return (Serial.baudRate() / 300) * 300; // Fix ESP32 strange results like 115201
}
void SetSerialBegin(void) { void SetSerialBegin(void) {
TasmotaGlobal.baudrate = Settings.baudrate * 300; 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); 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) { void SetSerialBaudrate(uint32_t baudrate) {
TasmotaGlobal.baudrate = baudrate; TasmotaGlobal.baudrate = baudrate;
Settings.baudrate = TasmotaGlobal.baudrate / 300; Settings.baudrate = TasmotaGlobal.baudrate / 300;
if (Serial.baudRate() != TasmotaGlobal.baudrate) { if (GetSerialBaudrate() != TasmotaGlobal.baudrate) {
SetSerialBegin(); SetSerialBegin();
} }
} }
@ -958,7 +962,7 @@ void ClaimSerial(void) {
TasmotaGlobal.serial_local = true; TasmotaGlobal.serial_local = true;
AddLog_P(LOG_LEVEL_INFO, PSTR("SNS: Hardware Serial")); AddLog_P(LOG_LEVEL_INFO, PSTR("SNS: Hardware Serial"));
SetSeriallog(LOG_LEVEL_NONE); SetSeriallog(LOG_LEVEL_NONE);
TasmotaGlobal.baudrate = Serial.baudRate(); TasmotaGlobal.baudrate = GetSerialBaudrate();
Settings.baudrate = TasmotaGlobal.baudrate / 300; Settings.baudrate = TasmotaGlobal.baudrate / 300;
} }

View File

@ -193,6 +193,7 @@ void setup(void) {
memset(&TasmotaGlobal, 0, sizeof(TasmotaGlobal)); memset(&TasmotaGlobal, 0, sizeof(TasmotaGlobal));
TasmotaGlobal.baudrate = APP_BAUDRATE; TasmotaGlobal.baudrate = APP_BAUDRATE;
TasmotaGlobal.seriallog_timer = SERIALLOG_TIMER;
TasmotaGlobal.temperature_celsius = NAN; TasmotaGlobal.temperature_celsius = NAN;
TasmotaGlobal.blinks = 201; TasmotaGlobal.blinks = 201;
TasmotaGlobal.wifi_state_flag = WIFI_RESTART; TasmotaGlobal.wifi_state_flag = WIFI_RESTART;
@ -229,20 +230,20 @@ void setup(void) {
OsWatchInit(); OsWatchInit();
TasmotaGlobal.seriallog_level = Settings.seriallog_level;
TasmotaGlobal.syslog_level = Settings.syslog_level;
TasmotaGlobal.module_changed = (Settings.module != Settings.last_module); TasmotaGlobal.module_changed = (Settings.module != Settings.last_module);
if (TasmotaGlobal.module_changed) { if (TasmotaGlobal.module_changed) {
Settings.baudrate = APP_BAUDRATE / 300; Settings.baudrate = APP_BAUDRATE / 300;
Settings.serial_config = TS_SERIAL_8N1; 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); 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.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.save_data_counter = Settings.save_data;
TasmotaGlobal.sleep = Settings.sleep; TasmotaGlobal.sleep = Settings.sleep;
@ -305,8 +306,6 @@ void setup(void) {
GetEspHardwareType(); GetEspHardwareType();
GpioInit(); GpioInit();
// SetSerialBaudrate(Settings.baudrate * 300); // Allow reset of serial interface if current baudrate is different from requested baudrate
WifiConnect(); WifiConnect();
SetPowerOnState(); SetPowerOnState();