From b608685c6099786e20dcd42ccbdeafd60ce1ebdf Mon Sep 17 00:00:00 2001 From: brambo123 Date: Fri, 17 Jan 2025 17:06:11 +0100 Subject: [PATCH] [uart] flow control - fix ESP32 Arduino < 2.0.8 --- esphome/components/uart/__init__.py | 19 ++++++++++++++++--- .../uart/uart_component_esp32_arduino.cpp | 2 ++ .../uart/uart_component_esp_idf.cpp | 2 +- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/esphome/components/uart/__init__.py b/esphome/components/uart/__init__.py index fdc85b40e0..9b6dc65897 100644 --- a/esphome/components/uart/__init__.py +++ b/esphome/components/uart/__init__.py @@ -30,6 +30,8 @@ from esphome.const import ( CONF_DUMMY_RECEIVER_ID, CONF_LAMBDA, PLATFORM_HOST, + KEY_CORE, + KEY_FRAMEWORK_VERSION ) from esphome.core import CORE @@ -117,10 +119,21 @@ def validate_rx_pin(value): raise cv.Invalid("Pins GPIO16 and GPIO17 cannot be used as RX pins on ESP8266.") return value + def validate_flow_control_support(config): - if CONF_FLOW_CONTROL_PIN in config and not CORE.is_esp32: - raise cv.Invalid("Hardware does not support flow control.") - return config + if CORE.is_esp32 and CORE.using_arduino: + if CORE.data[KEY_CORE][ + KEY_FRAMEWORK_VERSION + ] >= cv.Version(2, 0, 8): + cg.add_define("USE_UART_FLOW_CONTROL") + elif CONF_FLOW_CONTROL_PIN in config: + raise cv.Invalid( + "ESP32 RS485 UART Flow Control requires Arduino framework version 2.0.8 or higher." + ) + elif CORE.is_esp32: + return config + else: + raise cv.Invalid("Hardware does not support RS485 flow control.") def validate_invert_esp32(config): diff --git a/esphome/components/uart/uart_component_esp32_arduino.cpp b/esphome/components/uart/uart_component_esp32_arduino.cpp index 99f0f99f2d..abfd48551b 100644 --- a/esphome/components/uart/uart_component_esp32_arduino.cpp +++ b/esphome/components/uart/uart_component_esp32_arduino.cpp @@ -141,10 +141,12 @@ void ESP32ArduinoUARTComponent::load_settings(bool dump_config) { invert = true; this->hw_serial_->setRxBufferSize(this->rx_buffer_size_); this->hw_serial_->begin(this->baud_rate_, get_config(), rx, tx, invert); +#ifdef USE_UART_FLOW_CONTROL if (this->flow_control_pin_ != nullptr) { this->hw_serial_->setPins(-1, -1, -1, this->flow_control_pin_->get_pin()); this->hw_serial_->setMode(UART_MODE_RS485_HALF_DUPLEX); } +#endif if (dump_config) { ESP_LOGCONFIG(TAG, "UART %u was reloaded.", this->number_); this->dump_config(); diff --git a/esphome/components/uart/uart_component_esp_idf.cpp b/esphome/components/uart/uart_component_esp_idf.cpp index 0493bd372a..b6bd08e005 100644 --- a/esphome/components/uart/uart_component_esp_idf.cpp +++ b/esphome/components/uart/uart_component_esp_idf.cpp @@ -139,7 +139,7 @@ void IDFUARTComponent::setup() { this->mark_failed(); return; } - + if (this->flow_control_pin_ != nullptr) { err = uart_set_mode(this->uart_num_, UART_MODE_RS485_HALF_DUPLEX); if (err != ESP_OK) {