mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 10:00:36 +01:00
Add support for RS485 hardware flow control
This commit is contained in:
parent
6392adb6b2
commit
86045b08a5
@ -11,6 +11,7 @@ from esphome.const import (
|
||||
CONF_NUMBER,
|
||||
CONF_RX_PIN,
|
||||
CONF_TX_PIN,
|
||||
CONF_FLOW_CONTROL_PIN,
|
||||
CONF_UART_ID,
|
||||
CONF_DATA,
|
||||
CONF_RX_BUFFER_SIZE,
|
||||
@ -28,6 +29,8 @@ from esphome.const import (
|
||||
CONF_DUMMY_RECEIVER,
|
||||
CONF_DUMMY_RECEIVER_ID,
|
||||
CONF_LAMBDA,
|
||||
KEY_CORE,
|
||||
KEY_FRAMEWORK_VERSION,
|
||||
)
|
||||
from esphome.core import CORE
|
||||
|
||||
@ -86,7 +89,25 @@ def validate_invert_esp32(config):
|
||||
)
|
||||
return config
|
||||
|
||||
def validate_tx_buffer_support(config):
|
||||
if CORE.is_esp32:
|
||||
return config
|
||||
raise cv.Invalid(
|
||||
"Hardware does not support UART TX buffer."
|
||||
)
|
||||
|
||||
def validate_flow_control_support(config):
|
||||
if CONF_FLOW_CONTROL_PIN not in config:
|
||||
return config
|
||||
if CORE.is_esp32:
|
||||
if CORE.using_arduino and CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION] < cv.Version(2, 0, 8):
|
||||
raise cv.Invalid(
|
||||
"ESP32 RS485 UART Flow Control requires framework version 2.0.8 or higher."
|
||||
)
|
||||
return config
|
||||
raise cv.Invalid(
|
||||
"Hardware does not support RS485 flow control."
|
||||
)
|
||||
def _uart_declare_type(value):
|
||||
if CORE.is_esp8266:
|
||||
return cv.declare_id(ESP8266UartComponent)(value)
|
||||
@ -173,6 +194,7 @@ 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): pins.internal_gpio_output_pin_schema,
|
||||
cv.Optional(CONF_TX_BUFFER_SIZE, default=0): cv.validate_bytes,
|
||||
cv.Optional(CONF_RX_BUFFER_SIZE, default=256): cv.validate_bytes,
|
||||
cv.Optional(CONF_STOP_BITS, default=1): cv.one_of(1, 2, int=True),
|
||||
@ -188,6 +210,8 @@ CONFIG_SCHEMA = cv.All(
|
||||
).extend(cv.COMPONENT_SCHEMA),
|
||||
cv.has_at_least_one_key(CONF_TX_PIN, CONF_RX_PIN),
|
||||
validate_invert_esp32,
|
||||
validate_tx_buffer_support,
|
||||
validate_flow_control_support,
|
||||
)
|
||||
|
||||
|
||||
@ -229,6 +253,10 @@ 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))
|
||||
cg.add_define("USE_UART_FLOW_CONTROL")
|
||||
cg.add(var.set_tx_buffer_size(config[CONF_TX_BUFFER_SIZE]))
|
||||
cg.add(var.set_rx_buffer_size(config[CONF_RX_BUFFER_SIZE]))
|
||||
cg.add(var.set_stop_bits(config[CONF_STOP_BITS]))
|
||||
|
@ -82,6 +82,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 rx_pin Pointer to the internal GPIO pin used for reception.
|
||||
void set_flow_control_pin(InternalGPIOPin *flow_control_pin) { this->flow_control_pin_ = flow_control_pin; }
|
||||
|
||||
// Sets the size of the TX buffer.
|
||||
// @param tx_buffer_size Size of the TX buffer in bytes.
|
||||
void set_tx_buffer_size(size_t tx_buffer_size) { this->tx_buffer_size_ = tx_buffer_size; }
|
||||
@ -169,6 +173,7 @@ class UARTComponent {
|
||||
|
||||
InternalGPIOPin *tx_pin_;
|
||||
InternalGPIOPin *rx_pin_;
|
||||
InternalGPIOPin *flow_control_pin_;
|
||||
size_t tx_buffer_size_;
|
||||
size_t rx_buffer_size_;
|
||||
uint32_t baud_rate_;
|
||||
|
@ -120,6 +120,7 @@ void ESP32ArduinoUARTComponent::setup() {
|
||||
void ESP32ArduinoUARTComponent::load_settings(bool dump_config) {
|
||||
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;
|
||||
bool invert = false;
|
||||
if (tx_pin_ != nullptr && tx_pin_->is_inverted())
|
||||
invert = true;
|
||||
@ -128,6 +129,12 @@ void ESP32ArduinoUARTComponent::load_settings(bool dump_config) {
|
||||
this->hw_serial_->setTxBufferSize(this->tx_buffer_size_);
|
||||
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, flow_control);
|
||||
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();
|
||||
@ -138,6 +145,9 @@ void ESP32ArduinoUARTComponent::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "UART Bus %d:", this->number_);
|
||||
LOG_PIN(" TX Pin: ", tx_pin_);
|
||||
LOG_PIN(" RX Pin: ", rx_pin_);
|
||||
#ifdef USE_UART_FLOW_CONTROL
|
||||
LOG_PIN(" Flow Control Pin: ", flow_control_pin_);
|
||||
#endif
|
||||
if (this->tx_pin_ != nullptr) {
|
||||
ESP_LOGCONFIG(TAG, " TX Buffer Size: %u", this->tx_buffer_size_);
|
||||
}
|
||||
|
@ -86,6 +86,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())
|
||||
@ -113,12 +114,22 @@ void IDFUARTComponent::setup() {
|
||||
this->tx_buffer_size_,
|
||||
/* 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();
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->flow_control_pin_ != nullptr) {
|
||||
err = uart_set_mode(uart_num, UART_MODE_RS485_HALF_DUPLEX);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "uart_set_mode failed: %s", esp_err_to_name(err));
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
xSemaphoreGive(this->lock_);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user