mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	[uart] Multiple ESP32 features and fixes (#8103)
Co-authored-by: Keith Burzinski <kbx81x@gmail.com> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
		| @@ -1,3 +1,4 @@ | ||||
| import math | ||||
| import re | ||||
|  | ||||
| from esphome import automation, pins | ||||
| @@ -14,6 +15,7 @@ from esphome.const import ( | ||||
|     CONF_DIRECTION, | ||||
|     CONF_DUMMY_RECEIVER, | ||||
|     CONF_DUMMY_RECEIVER_ID, | ||||
|     CONF_FLOW_CONTROL_PIN, | ||||
|     CONF_ID, | ||||
|     CONF_INVERT, | ||||
|     CONF_LAMBDA, | ||||
| @@ -152,6 +154,8 @@ UART_PARITY_OPTIONS = { | ||||
| CONF_STOP_BITS = "stop_bits" | ||||
| CONF_DATA_BITS = "data_bits" | ||||
| CONF_PARITY = "parity" | ||||
| CONF_RX_FULL_THRESHOLD = "rx_full_threshold" | ||||
| CONF_RX_TIMEOUT = "rx_timeout" | ||||
|  | ||||
| UARTDirection = uart_ns.enum("UARTDirection") | ||||
| UART_DIRECTIONS = { | ||||
| @@ -219,8 +223,17 @@ CONFIG_SCHEMA = cv.All( | ||||
|             cv.Required(CONF_BAUD_RATE): cv.int_range(min=1), | ||||
|             cv.Optional(CONF_TX_PIN): pins.internal_gpio_output_pin_schema, | ||||
|             cv.Optional(CONF_RX_PIN): validate_rx_pin, | ||||
|             cv.Optional(CONF_FLOW_CONTROL_PIN): cv.All( | ||||
|                 cv.only_on_esp32, pins.internal_gpio_output_pin_schema | ||||
|             ), | ||||
|             cv.Optional(CONF_PORT): cv.All(validate_port, cv.only_on(PLATFORM_HOST)), | ||||
|             cv.Optional(CONF_RX_BUFFER_SIZE, default=256): cv.validate_bytes, | ||||
|             cv.Optional(CONF_RX_FULL_THRESHOLD): cv.All( | ||||
|                 cv.only_on_esp32, cv.validate_bytes, cv.int_range(min=1, max=120) | ||||
|             ), | ||||
|             cv.SplitDefault(CONF_RX_TIMEOUT, esp32=2): cv.All( | ||||
|                 cv.only_on_esp32, cv.validate_bytes, cv.int_range(min=0, max=92) | ||||
|             ), | ||||
|             cv.Optional(CONF_STOP_BITS, default=1): cv.one_of(1, 2, int=True), | ||||
|             cv.Optional(CONF_DATA_BITS, default=8): cv.int_range(min=5, max=8), | ||||
|             cv.Optional(CONF_PARITY, default="NONE"): cv.enum( | ||||
| @@ -275,9 +288,27 @@ async def to_code(config): | ||||
|     if CONF_RX_PIN in config: | ||||
|         rx_pin = await cg.gpio_pin_expression(config[CONF_RX_PIN]) | ||||
|         cg.add(var.set_rx_pin(rx_pin)) | ||||
|     if CONF_FLOW_CONTROL_PIN in config: | ||||
|         flow_control_pin = await cg.gpio_pin_expression(config[CONF_FLOW_CONTROL_PIN]) | ||||
|         cg.add(var.set_flow_control_pin(flow_control_pin)) | ||||
|     if CONF_PORT in config: | ||||
|         cg.add(var.set_name(config[CONF_PORT])) | ||||
|     cg.add(var.set_rx_buffer_size(config[CONF_RX_BUFFER_SIZE])) | ||||
|     if CORE.is_esp32: | ||||
|         if CONF_RX_FULL_THRESHOLD not in config: | ||||
|             # Calculate rx_full_threshold to be 10ms | ||||
|             bytelength = config[CONF_DATA_BITS] + config[CONF_STOP_BITS] + 1 | ||||
|             if config[CONF_PARITY] != "NONE": | ||||
|                 bytelength += 1 | ||||
|             config[CONF_RX_FULL_THRESHOLD] = max( | ||||
|                 1, | ||||
|                 min( | ||||
|                     120, | ||||
|                     math.floor((config[CONF_BAUD_RATE] / (bytelength * 1000 / 10)) - 1), | ||||
|                 ), | ||||
|             ) | ||||
|         cg.add(var.set_rx_full_threshold(config[CONF_RX_FULL_THRESHOLD])) | ||||
|         cg.add(var.set_rx_timeout(config[CONF_RX_TIMEOUT])) | ||||
|     cg.add(var.set_stop_bits(config[CONF_STOP_BITS])) | ||||
|     cg.add(var.set_data_bits(config[CONF_DATA_BITS])) | ||||
|     cg.add(var.set_parity(config[CONF_PARITY])) | ||||
|   | ||||
| @@ -18,6 +18,12 @@ class UARTDevice { | ||||
|  | ||||
|   void write_byte(uint8_t data) { this->parent_->write_byte(data); } | ||||
|  | ||||
|   void set_rx_full_threshold(size_t rx_full_threshold) { this->parent_->set_rx_full_threshold(rx_full_threshold); } | ||||
|   void set_rx_full_threshold_ms(size_t time) { this->parent_->set_rx_full_threshold_ms(time); } | ||||
|   size_t get_rx_full_threshold() { return this->parent_->get_rx_full_threshold(); } | ||||
|   void set_rx_timeout(size_t rx_timeout) { this->parent_->set_rx_timeout(rx_timeout); } | ||||
|   size_t get_rx_timeout() { return this->parent_->get_rx_timeout(); } | ||||
|  | ||||
|   void write_array(const uint8_t *data, size_t len) { this->parent_->write_array(data, len); } | ||||
|   void write_array(const std::vector<uint8_t> &data) { this->parent_->write_array(data); } | ||||
|   template<size_t N> void write_array(const std::array<uint8_t, N> &data) { | ||||
|   | ||||
| @@ -20,5 +20,13 @@ bool UARTComponent::check_read_timeout_(size_t len) { | ||||
|   return true; | ||||
| } | ||||
|  | ||||
| void UARTComponent::set_rx_full_threshold_ms(uint8_t time) { | ||||
|   uint8_t bytelength = this->data_bits_ + this->stop_bits_ + 1; | ||||
|   if (this->parity_ != UARTParityOptions::UART_CONFIG_PARITY_NONE) | ||||
|     bytelength += 1; | ||||
|   int32_t val = clamp<int32_t>((this->baud_rate_ / (bytelength * 1000 / time)) - 1, 1, 120); | ||||
|   this->set_rx_full_threshold(val); | ||||
| } | ||||
|  | ||||
| }  // namespace uart | ||||
| }  // namespace esphome | ||||
|   | ||||
| @@ -6,6 +6,7 @@ | ||||
| #include "esphome/core/component.h" | ||||
| #include "esphome/core/hal.h" | ||||
| #include "esphome/core/log.h" | ||||
| #include "esphome/core/helpers.h" | ||||
| #ifdef USE_UART_DEBUGGER | ||||
| #include "esphome/core/automation.h" | ||||
| #endif | ||||
| @@ -82,6 +83,10 @@ class UARTComponent { | ||||
|   // @param rx_pin Pointer to the internal GPIO pin used for reception. | ||||
|   void set_rx_pin(InternalGPIOPin *rx_pin) { this->rx_pin_ = rx_pin; } | ||||
|  | ||||
|   // Sets the flow control pin for the UART bus. | ||||
|   // @param flow_control_pin Pointer to the internal GPIO pin used for flow control. | ||||
|   void set_flow_control_pin(InternalGPIOPin *flow_control_pin) { this->flow_control_pin_ = flow_control_pin; } | ||||
|  | ||||
|   // Sets the size of the RX buffer. | ||||
|   // @param rx_buffer_size Size of the RX buffer in bytes. | ||||
|   void set_rx_buffer_size(size_t rx_buffer_size) { this->rx_buffer_size_ = rx_buffer_size; } | ||||
| @@ -90,6 +95,26 @@ class UARTComponent { | ||||
|   // @return Size of the RX buffer in bytes. | ||||
|   size_t get_rx_buffer_size() { return this->rx_buffer_size_; } | ||||
|  | ||||
|   // Sets the RX FIFO full interrupt threshold. | ||||
|   // @param rx_full_threshold RX full interrupt threshold in bytes. | ||||
|   virtual void set_rx_full_threshold(size_t rx_full_threshold) {} | ||||
|  | ||||
|   // Sets the RX FIFO full interrupt threshold. | ||||
|   // @param time RX full interrupt threshold in ms. | ||||
|   void set_rx_full_threshold_ms(uint8_t time); | ||||
|  | ||||
|   // Gets the RX FIFO full interrupt threshold. | ||||
|   // @return RX full interrupt threshold in bytes. | ||||
|   size_t get_rx_full_threshold() { return this->rx_full_threshold_; } | ||||
|  | ||||
|   // Sets the RX timeout interrupt threshold. | ||||
|   // @param rx_timeout RX timeout interrupt threshold (unit: time of sending one byte). | ||||
|   virtual void set_rx_timeout(size_t rx_timeout) {} | ||||
|  | ||||
|   // Gets the RX timeout interrupt threshold. | ||||
|   // @return RX timeout interrupt threshold (unit: time of sending one byte). | ||||
|   size_t get_rx_timeout() { return this->rx_timeout_; } | ||||
|  | ||||
|   // Sets the number of stop bits used in UART communication. | ||||
|   // @param stop_bits Number of stop bits. | ||||
|   void set_stop_bits(uint8_t stop_bits) { this->stop_bits_ = stop_bits; } | ||||
| @@ -161,7 +186,10 @@ class UARTComponent { | ||||
|  | ||||
|   InternalGPIOPin *tx_pin_; | ||||
|   InternalGPIOPin *rx_pin_; | ||||
|   InternalGPIOPin *flow_control_pin_; | ||||
|   size_t rx_buffer_size_; | ||||
|   size_t rx_full_threshold_{1}; | ||||
|   size_t rx_timeout_{0}; | ||||
|   uint32_t baud_rate_; | ||||
|   uint8_t stop_bits_; | ||||
|   uint8_t data_bits_; | ||||
|   | ||||
| @@ -90,6 +90,12 @@ void IDFUARTComponent::setup() { | ||||
|  | ||||
|   xSemaphoreTake(this->lock_, portMAX_DELAY); | ||||
|  | ||||
|   this->load_settings(false); | ||||
|  | ||||
|   xSemaphoreGive(this->lock_); | ||||
| } | ||||
|  | ||||
| void IDFUARTComponent::load_settings(bool dump_config) { | ||||
|   uart_config_t uart_config = this->get_config_(); | ||||
|   esp_err_t err = uart_param_config(this->uart_num_, &uart_config); | ||||
|   if (err != ESP_OK) { | ||||
| @@ -100,6 +106,7 @@ void IDFUARTComponent::setup() { | ||||
|  | ||||
|   int8_t tx = this->tx_pin_ != nullptr ? this->tx_pin_->get_pin() : -1; | ||||
|   int8_t rx = this->rx_pin_ != nullptr ? this->rx_pin_->get_pin() : -1; | ||||
|   int8_t flow_control = this->flow_control_pin_ != nullptr ? this->flow_control_pin_->get_pin() : -1; | ||||
|  | ||||
|   uint32_t invert = 0; | ||||
|   if (this->tx_pin_ != nullptr && this->tx_pin_->is_inverted()) | ||||
| @@ -114,13 +121,21 @@ void IDFUARTComponent::setup() { | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   err = uart_set_pin(this->uart_num_, tx, rx, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); | ||||
|   err = uart_set_pin(this->uart_num_, tx, rx, flow_control, UART_PIN_NO_CHANGE); | ||||
|   if (err != ESP_OK) { | ||||
|     ESP_LOGW(TAG, "uart_set_pin failed: %s", esp_err_to_name(err)); | ||||
|     this->mark_failed(); | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   if (uart_is_driver_installed(this->uart_num_)) { | ||||
|     uart_driver_delete(this->uart_num_); | ||||
|     if (err != ESP_OK) { | ||||
|       ESP_LOGW(TAG, "uart_driver_delete failed: %s", esp_err_to_name(err)); | ||||
|       this->mark_failed(); | ||||
|       return; | ||||
|     } | ||||
|   } | ||||
|   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.*/ | ||||
| @@ -133,17 +148,29 @@ void IDFUARTComponent::setup() { | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   xSemaphoreGive(this->lock_); | ||||
| } | ||||
|  | ||||
| void IDFUARTComponent::load_settings(bool dump_config) { | ||||
|   uart_config_t uart_config = this->get_config_(); | ||||
|   esp_err_t err = uart_param_config(this->uart_num_, &uart_config); | ||||
|   err = uart_set_rx_full_threshold(this->uart_num_, this->rx_full_threshold_); | ||||
|   if (err != ESP_OK) { | ||||
|     ESP_LOGW(TAG, "uart_param_config failed: %s", esp_err_to_name(err)); | ||||
|     ESP_LOGW(TAG, "uart_set_rx_full_threshold failed: %s", esp_err_to_name(err)); | ||||
|     this->mark_failed(); | ||||
|     return; | ||||
|   } else if (dump_config) { | ||||
|   } | ||||
|  | ||||
|   err = uart_set_rx_timeout(this->uart_num_, this->rx_timeout_); | ||||
|   if (err != ESP_OK) { | ||||
|     ESP_LOGW(TAG, "uart_set_rx_timeout failed: %s", esp_err_to_name(err)); | ||||
|     this->mark_failed(); | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   auto mode = this->flow_control_pin_ != nullptr ? UART_MODE_RS485_HALF_DUPLEX : UART_MODE_UART; | ||||
|   err = uart_set_mode(this->uart_num_, mode); | ||||
|   if (err != ESP_OK) { | ||||
|     ESP_LOGW(TAG, "uart_set_mode failed: %s", esp_err_to_name(err)); | ||||
|     this->mark_failed(); | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   if (dump_config) { | ||||
|     ESP_LOGCONFIG(TAG, "UART %u was reloaded.", this->uart_num_); | ||||
|     this->dump_config(); | ||||
|   } | ||||
| @@ -153,8 +180,13 @@ void IDFUARTComponent::dump_config() { | ||||
|   ESP_LOGCONFIG(TAG, "UART Bus %u:", this->uart_num_); | ||||
|   LOG_PIN("  TX Pin: ", tx_pin_); | ||||
|   LOG_PIN("  RX Pin: ", rx_pin_); | ||||
|   LOG_PIN("  Flow Control Pin: ", flow_control_pin_); | ||||
|   if (this->rx_pin_ != nullptr) { | ||||
|     ESP_LOGCONFIG(TAG, "  RX Buffer Size: %u", this->rx_buffer_size_); | ||||
|     ESP_LOGCONFIG(TAG, | ||||
|                   "  RX Buffer Size: %u\n" | ||||
|                   "  RX Full Threshold: %u\n" | ||||
|                   "  RX Timeout: %u", | ||||
|                   this->rx_buffer_size_, this->rx_full_threshold_, this->rx_timeout_); | ||||
|   } | ||||
|   ESP_LOGCONFIG(TAG, | ||||
|                 "  Baud Rate: %" PRIu32 " baud\n" | ||||
| @@ -165,6 +197,28 @@ void IDFUARTComponent::dump_config() { | ||||
|   this->check_logger_conflict(); | ||||
| } | ||||
|  | ||||
| void IDFUARTComponent::set_rx_full_threshold(size_t rx_full_threshold) { | ||||
|   if (this->is_ready()) { | ||||
|     esp_err_t err = uart_set_rx_full_threshold(this->uart_num_, rx_full_threshold); | ||||
|     if (err != ESP_OK) { | ||||
|       ESP_LOGW(TAG, "uart_set_rx_full_threshold failed: %s", esp_err_to_name(err)); | ||||
|       return; | ||||
|     } | ||||
|   } | ||||
|   this->rx_full_threshold_ = rx_full_threshold; | ||||
| } | ||||
|  | ||||
| void IDFUARTComponent::set_rx_timeout(size_t rx_timeout) { | ||||
|   if (this->is_ready()) { | ||||
|     esp_err_t err = uart_set_rx_timeout(this->uart_num_, rx_timeout); | ||||
|     if (err != ESP_OK) { | ||||
|       ESP_LOGW(TAG, "uart_set_rx_timeout failed: %s", esp_err_to_name(err)); | ||||
|       return; | ||||
|     } | ||||
|   } | ||||
|   this->rx_timeout_ = rx_timeout; | ||||
| } | ||||
|  | ||||
| void IDFUARTComponent::write_array(const uint8_t *data, size_t len) { | ||||
|   xSemaphoreTake(this->lock_, portMAX_DELAY); | ||||
|   uart_write_bytes(this->uart_num_, data, len); | ||||
|   | ||||
| @@ -15,6 +15,9 @@ class IDFUARTComponent : public UARTComponent, public Component { | ||||
|   void dump_config() override; | ||||
|   float get_setup_priority() const override { return setup_priority::BUS; } | ||||
|  | ||||
|   void set_rx_full_threshold(size_t rx_full_threshold) override; | ||||
|   void set_rx_timeout(size_t rx_timeout) override; | ||||
|  | ||||
|   void write_array(const uint8_t *data, size_t len) override; | ||||
|  | ||||
|   bool peek_byte(uint8_t *data) override; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user