Fix UARTComponent hardware vs software UART0 conflict (#2229)

Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl>
This commit is contained in:
Christian Ferbar 2021-09-04 04:49:34 +02:00 committed by GitHub
parent 54de0ca0da
commit 77508f7e44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 3 deletions

View File

@ -118,6 +118,11 @@ class UARTComponent : public Component, public Stream {
uint8_t stop_bits_;
uint8_t data_bits_;
UARTParityOptions parity_;
private:
#ifdef ARDUINO_ARCH_ESP8266
static bool serial0InUse;
#endif
};
#ifdef ARDUINO_ARCH_ESP32

View File

@ -4,11 +4,17 @@
#include "esphome/core/helpers.h"
#include "esphome/core/application.h"
#include "esphome/core/defines.h"
#
#ifdef USE_LOGGER
#include "esphome/components/logger/logger.h"
#endif
namespace esphome {
namespace uart {
static const char *const TAG = "uart_esp8266";
bool UARTComponent::serial0InUse = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
uint32_t UARTComponent::get_config() {
uint32_t config = 0;
@ -49,15 +55,31 @@ void UARTComponent::setup() {
// is 1 we still want to use Serial.
SerialConfig config = static_cast<SerialConfig>(get_config());
if (this->tx_pin_.value_or(1) == 1 && this->rx_pin_.value_or(3) == 3) {
if (!UARTComponent::serial0InUse && this->tx_pin_.value_or(1) == 1 &&
this->rx_pin_.value_or(3) == 3
#ifdef USE_LOGGER
// we will use UART0 if logger isn't using it in swapped mode
&& (logger::global_logger->get_hw_serial() == nullptr ||
logger::global_logger->get_uart() != logger::UART_SELECTION_UART0_SWAP)
#endif
) {
this->hw_serial_ = &Serial;
this->hw_serial_->begin(this->baud_rate_, config);
this->hw_serial_->setRxBufferSize(this->rx_buffer_size_);
} else if (this->tx_pin_.value_or(15) == 15 && this->rx_pin_.value_or(13) == 13) {
UARTComponent::serial0InUse = true;
} else if (!UARTComponent::serial0InUse && this->tx_pin_.value_or(15) == 15 &&
this->rx_pin_.value_or(13) == 13
#ifdef USE_LOGGER
// we will use UART0 swapped if logger isn't using it in regular mode
&& (logger::global_logger->get_hw_serial() == nullptr ||
logger::global_logger->get_uart() != logger::UART_SELECTION_UART0)
#endif
) {
this->hw_serial_ = &Serial;
this->hw_serial_->begin(this->baud_rate_, config);
this->hw_serial_->setRxBufferSize(this->rx_buffer_size_);
this->hw_serial_->swap();
UARTComponent::serial0InUse = true;
} else if (this->tx_pin_.value_or(2) == 2 && this->rx_pin_.value_or(8) == 8) {
this->hw_serial_ = &Serial1;
this->hw_serial_->begin(this->baud_rate_, config);