Start UART assignment at UART0 if the logger is not enabled or is not configured for hardware logging on ESP32 (#4762)

This commit is contained in:
Justin Gerace 2023-05-15 16:00:05 -07:00 committed by Jesse Hills
parent daa966975e
commit c71e7d0132
No known key found for this signature in database
GPG key ID: BEAAE804EFD8E83A
2 changed files with 19 additions and 2 deletions

View file

@ -86,10 +86,26 @@ void ESP32ArduinoUARTComponent::setup() {
is_default_tx = tx_pin_ == nullptr || tx_pin_->get_pin() == 1;
is_default_rx = rx_pin_ == nullptr || rx_pin_->get_pin() == 3;
#endif
if (is_default_tx && is_default_rx) {
static uint8_t next_uart_num = 0;
if (is_default_tx && is_default_rx && next_uart_num == 0) {
this->hw_serial_ = &Serial;
next_uart_num++;
} else {
static uint8_t next_uart_num = 1;
#ifdef USE_LOGGER
// The logger doesn't use this UART component, instead it targets the UARTs
// directly (i.e. Serial/Serial0, Serial1, and Serial2). If the logger is
// enabled, skip the UART that it is configured to use.
if (logger::global_logger->get_baud_rate() > 0 && logger::global_logger->get_uart() == next_uart_num) {
next_uart_num++;
}
#endif // USE_LOGGER
if (next_uart_num >= UART_NUM_MAX) {
ESP_LOGW(TAG, "Maximum number of UART components created already.");
this->mark_failed();
return;
}
this->number_ = next_uart_num;
this->hw_serial_ = new HardwareSerial(next_uart_num++); // NOLINT(cppcoreguidelines-owning-memory)
}

View file

@ -2,6 +2,7 @@
#ifdef USE_ESP32_FRAMEWORK_ARDUINO
#include <driver/uart.h>
#include <HardwareSerial.h>
#include <vector>
#include "esphome/core/component.h"