From 9922eb83e2851f23992d021ae59adba5d56d73d7 Mon Sep 17 00:00:00 2001 From: Fabian Date: Fri, 31 Mar 2023 06:30:24 +0200 Subject: [PATCH] Support advanced UART customization (#4465) * Add methods to get hardware uart details. * Fix `setRxBufferSize` error. --------- Co-authored-by: Your Name --- esphome/components/uart/uart_component_esp32_arduino.cpp | 2 +- esphome/components/uart/uart_component_esp32_arduino.h | 3 +++ esphome/components/uart/uart_component_esp_idf.cpp | 7 ++++++- esphome/components/uart/uart_component_esp_idf.h | 4 ++++ esphome/components/uart/uart_component_rp2040.h | 3 +++ 5 files changed, 17 insertions(+), 2 deletions(-) diff --git a/esphome/components/uart/uart_component_esp32_arduino.cpp b/esphome/components/uart/uart_component_esp32_arduino.cpp index 402e41e0b5..8bbbc1a650 100644 --- a/esphome/components/uart/uart_component_esp32_arduino.cpp +++ b/esphome/components/uart/uart_component_esp32_arduino.cpp @@ -100,8 +100,8 @@ void ESP32ArduinoUARTComponent::setup() { invert = true; if (rx_pin_ != nullptr && rx_pin_->is_inverted()) invert = true; - this->hw_serial_->begin(this->baud_rate_, get_config(), rx, tx, invert); this->hw_serial_->setRxBufferSize(this->rx_buffer_size_); + this->hw_serial_->begin(this->baud_rate_, get_config(), rx, tx, invert); } void ESP32ArduinoUARTComponent::dump_config() { diff --git a/esphome/components/uart/uart_component_esp32_arduino.h b/esphome/components/uart/uart_component_esp32_arduino.h index 4a000b12d2..f85c709097 100644 --- a/esphome/components/uart/uart_component_esp32_arduino.h +++ b/esphome/components/uart/uart_component_esp32_arduino.h @@ -28,6 +28,9 @@ class ESP32ArduinoUARTComponent : public UARTComponent, public Component { uint32_t get_config(); + HardwareSerial *get_hw_serial() { return this->hw_serial_; } + uint8_t get_hw_serial_number() { return this->number_; } + protected: void check_logger_conflict() override; diff --git a/esphome/components/uart/uart_component_esp_idf.cpp b/esphome/components/uart/uart_component_esp_idf.cpp index 80255ccddf..1560409772 100644 --- a/esphome/components/uart/uart_component_esp_idf.cpp +++ b/esphome/components/uart/uart_component_esp_idf.cpp @@ -79,7 +79,12 @@ void IDFUARTComponent::setup() { return; } - err = uart_driver_install(this->uart_num_, this->rx_buffer_size_, 0, 0, nullptr, 0); + err = uart_driver_install(this->uart_num_, /* UART RX ring buffer size. */ this->rx_buffer_size_, + /* UART TX ring buffer size. If set to zero, driver will not use TX buffer, TX function will + block task until all data have been sent out.*/ + 0, + /* UART event queue size/depth. */ 20, &(this->uart_event_queue_), + /* Flags used to allocate the interrupt. */ 0); if (err != ESP_OK) { ESP_LOGW(TAG, "uart_driver_install failed: %s", esp_err_to_name(err)); this->mark_failed(); diff --git a/esphome/components/uart/uart_component_esp_idf.h b/esphome/components/uart/uart_component_esp_idf.h index 27fb80d2cc..fdaa4da9a7 100644 --- a/esphome/components/uart/uart_component_esp_idf.h +++ b/esphome/components/uart/uart_component_esp_idf.h @@ -23,9 +23,13 @@ class IDFUARTComponent : public UARTComponent, public Component { int available() override; void flush() override; + uint8_t get_hw_serial_number() { return this->uart_num_; } + QueueHandle_t *get_uart_event_queue() { return &this->uart_event_queue_; } + protected: void check_logger_conflict() override; uart_port_t uart_num_; + QueueHandle_t uart_event_queue_; uart_config_t get_config_(); SemaphoreHandle_t lock_; diff --git a/esphome/components/uart/uart_component_rp2040.h b/esphome/components/uart/uart_component_rp2040.h index 163315dee7..f26c913cff 100644 --- a/esphome/components/uart/uart_component_rp2040.h +++ b/esphome/components/uart/uart_component_rp2040.h @@ -30,6 +30,9 @@ class RP2040UartComponent : public UARTComponent, public Component { uint16_t get_config(); + bool is_hw_serial() { return this->hw_serial_; } + HardwareSerial *get_hw_serial() { return this->serial_; } + protected: void check_logger_conflict() override {} bool hw_serial_{false};