Refactor serial config

Refactor serial config removing specific stopbit count and only support serial config constants (#14212)
This commit is contained in:
Theo Arends 2021-12-30 14:19:24 +01:00
parent 730f3451ac
commit 5a73aa1e1f
6 changed files with 20 additions and 23 deletions

View File

@ -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

View File

@ -42,7 +42,7 @@
#include <TasmotaSerial.h>
/* 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);
}

View File

@ -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;

View File

@ -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;

View File

@ -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();
}

View File

@ -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) {