From 11330af05f0b5da70333d299b2f080518e7ae0ab Mon Sep 17 00:00:00 2001 From: Leon Loopik <489021+Lewn@users.noreply.github.com> Date: Wed, 1 Dec 2021 20:31:04 +0100 Subject: [PATCH] Expand uart invert feature to ESP8266 (#1727) --- esphome/components/uart/__init__.py | 15 +++++++++++++++ .../components/uart/uart_component_esp8266.cpp | 5 +++++ tests/test3.yaml | 4 +++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/esphome/components/uart/__init__.py b/esphome/components/uart/__init__.py index 61b54044d7..a63b220fc7 100644 --- a/esphome/components/uart/__init__.py +++ b/esphome/components/uart/__init__.py @@ -14,6 +14,7 @@ from esphome.const import ( CONF_UART_ID, CONF_DATA, CONF_RX_BUFFER_SIZE, + CONF_INVERTED, CONF_INVERT, CONF_TRIGGER_ID, CONF_SEQUENCE, @@ -67,6 +68,19 @@ def validate_rx_pin(value): return value +def validate_invert_esp32(config): + if ( + CORE.is_esp32 + and CONF_TX_PIN in config + and CONF_RX_PIN in config + and config[CONF_TX_PIN][CONF_INVERTED] != config[CONF_RX_PIN][CONF_INVERTED] + ): + raise cv.Invalid( + "Different invert values for TX and RX pin are not (yet) supported for ESP32." + ) + return config + + def _uart_declare_type(value): if CORE.is_esp8266: return cv.declare_id(ESP8266UartComponent)(value) @@ -162,6 +176,7 @@ CONFIG_SCHEMA = cv.All( } ).extend(cv.COMPONENT_SCHEMA), cv.has_at_least_one_key(CONF_TX_PIN, CONF_RX_PIN), + validate_invert_esp32, ) diff --git a/esphome/components/uart/uart_component_esp8266.cpp b/esphome/components/uart/uart_component_esp8266.cpp index c367de05bb..408c83a0db 100644 --- a/esphome/components/uart/uart_component_esp8266.cpp +++ b/esphome/components/uart/uart_component_esp8266.cpp @@ -45,6 +45,11 @@ uint32_t ESP8266UartComponent::get_config() { else config |= UART_NB_STOP_BIT_2; + if (this->tx_pin_ != nullptr && this->tx_pin_->is_inverted()) + config |= BIT(22); + if (this->rx_pin_ != nullptr && this->rx_pin_->is_inverted()) + config |= BIT(19); + return config; } diff --git a/tests/test3.yaml b/tests/test3.yaml index 8ae4a383e0..50cd6d6cf6 100644 --- a/tests/test3.yaml +++ b/tests/test3.yaml @@ -227,7 +227,9 @@ spi: uart: - id: uart1 - tx_pin: GPIO1 + tx_pin: + number: GPIO1 + inverted: yes rx_pin: GPIO3 baud_rate: 115200 - id: uart2