mirror of https://github.com/esphome/esphome.git
Merge 168ea40da6
into c7c0d97a5e
This commit is contained in:
commit
92a6f26e12
|
@ -190,10 +190,11 @@ void Modbus::send(uint8_t address, uint8_t function_code, uint16_t start_address
|
|||
this->flow_control_pin_->digital_write(true);
|
||||
|
||||
this->write_array(data);
|
||||
this->flush();
|
||||
|
||||
if (this->flow_control_pin_ != nullptr)
|
||||
if (this->flow_control_pin_ != nullptr) {
|
||||
this->flush();
|
||||
this->flow_control_pin_->digital_write(false);
|
||||
}
|
||||
waiting_for_response = address;
|
||||
last_send_ = millis();
|
||||
ESP_LOGV(TAG, "Modbus write: %s", format_hex_pretty(data).c_str());
|
||||
|
@ -213,9 +214,11 @@ void Modbus::send_raw(const std::vector<uint8_t> &payload) {
|
|||
this->write_array(payload);
|
||||
this->write_byte(crc & 0xFF);
|
||||
this->write_byte((crc >> 8) & 0xFF);
|
||||
this->flush();
|
||||
if (this->flow_control_pin_ != nullptr)
|
||||
|
||||
if (this->flow_control_pin_ != nullptr) {
|
||||
this->flush();
|
||||
this->flow_control_pin_->digital_write(false);
|
||||
}
|
||||
waiting_for_response = payload[0];
|
||||
ESP_LOGV(TAG, "Modbus write raw: %s", format_hex_pretty(payload).c_str());
|
||||
last_send_ = millis();
|
||||
|
|
|
@ -11,9 +11,11 @@ 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,
|
||||
CONF_TX_BUFFER_SIZE,
|
||||
CONF_INVERTED,
|
||||
CONF_INVERT,
|
||||
CONF_TRIGGER_ID,
|
||||
|
@ -27,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
|
||||
|
||||
|
@ -95,6 +99,27 @@ def validate_invert_esp32(config):
|
|||
return config
|
||||
|
||||
|
||||
def validate_tx_buffer(value):
|
||||
value = cv.validate_bytes(value)
|
||||
if value <= 0 or CORE.is_esp32:
|
||||
return value
|
||||
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)
|
||||
|
@ -181,6 +206,8 @@ 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): validate_tx_buffer,
|
||||
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),
|
||||
cv.Optional(CONF_DATA_BITS, default=8): cv.int_range(min=5, max=8),
|
||||
|
@ -195,6 +222,7 @@ CONFIG_SCHEMA = cv.All(
|
|||
).extend(cv.COMPONENT_SCHEMA),
|
||||
cv.has_at_least_one_key(CONF_TX_PIN, CONF_RX_PIN),
|
||||
validate_invert_esp32,
|
||||
validate_flow_control_support,
|
||||
)
|
||||
|
||||
|
||||
|
@ -236,6 +264,11 @@ 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]))
|
||||
cg.add(var.set_data_bits(config[CONF_DATA_BITS]))
|
||||
|
|
|
@ -82,10 +82,22 @@ 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; }
|
||||
|
||||
// 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; }
|
||||
|
||||
// Gets the size of the TX buffer.
|
||||
// @return Size of the TX buffer in bytes.
|
||||
size_t get_tx_buffer_size() { return this->tx_buffer_size_; }
|
||||
|
||||
// Gets the size of the RX buffer.
|
||||
// @return Size of the RX buffer in bytes.
|
||||
size_t get_rx_buffer_size() { return this->rx_buffer_size_; }
|
||||
|
@ -161,6 +173,8 @@ 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_;
|
||||
uint8_t stop_bits_;
|
||||
|
|
|
@ -125,8 +125,15 @@ void ESP32ArduinoUARTComponent::load_settings(bool dump_config) {
|
|||
invert = true;
|
||||
if (rx_pin_ != nullptr && rx_pin_->is_inverted())
|
||||
invert = true;
|
||||
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, 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();
|
||||
|
@ -137,6 +144,12 @@ 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_);
|
||||
}
|
||||
if (this->rx_pin_ != nullptr) {
|
||||
ESP_LOGCONFIG(TAG, " RX Buffer Size: %u", this->rx_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())
|
||||
|
@ -100,7 +101,7 @@ 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();
|
||||
|
@ -110,15 +111,25 @@ void IDFUARTComponent::setup() {
|
|||
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,
|
||||
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(this->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_);
|
||||
}
|
||||
|
||||
|
@ -139,6 +150,9 @@ 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_);
|
||||
if (this->tx_pin_ != nullptr) {
|
||||
ESP_LOGCONFIG(TAG, " TX Buffer Size: %u", this->tx_buffer_size_);
|
||||
}
|
||||
if (this->rx_pin_ != nullptr) {
|
||||
ESP_LOGCONFIG(TAG, " RX Buffer Size: %u", this->rx_buffer_size_);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue