Skip to content

Commit

Permalink
Extend uart: with rx_buffer_size: (esphome#1006)
Browse files Browse the repository at this point in the history
* Extend `uart:` with `rx_buffer_size:`

This allows to configure `rx_buffer_size:`
to efficiently receive big payloads over UART

* lint

* remove old default value

* add test

Co-authored-by: Guillermo Ruffino <[email protected]>
  • Loading branch information
ayufan and glmnet authored Jun 10, 2020
1 parent 64bd33a commit cd7af19
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 5 deletions.
5 changes: 4 additions & 1 deletion esphome/components/uart/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins, automation
from esphome.const import CONF_BAUD_RATE, CONF_ID, CONF_RX_PIN, CONF_TX_PIN, CONF_UART_ID, CONF_DATA
from esphome.const import CONF_BAUD_RATE, CONF_ID, CONF_RX_PIN, CONF_TX_PIN, CONF_UART_ID, \
CONF_DATA, CONF_RX_BUFFER_SIZE
from esphome.core import CORE, coroutine

uart_ns = cg.esphome_ns.namespace('uart')
Expand Down Expand Up @@ -44,6 +45,7 @@ def validate_rx_pin(value):
cv.Required(CONF_BAUD_RATE): cv.int_range(min=1),
cv.Optional(CONF_TX_PIN): pins.output_pin,
cv.Optional(CONF_RX_PIN): validate_rx_pin,
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),
cv.Optional(CONF_PARITY, default="NONE"): cv.enum(UART_PARITY_OPTIONS, upper=True)
Expand All @@ -61,6 +63,7 @@ def to_code(config):
cg.add(var.set_tx_pin(config[CONF_TX_PIN]))
if CONF_RX_PIN in config:
cg.add(var.set_rx_pin(config[CONF_RX_PIN]))
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]))
cg.add(var.set_parity(config[CONF_PARITY]))
Expand Down
6 changes: 4 additions & 2 deletions esphome/components/uart/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const char *parity_to_str(UARTParityOptions parity);
class ESP8266SoftwareSerial {
public:
void setup(int8_t tx_pin, int8_t rx_pin, uint32_t baud_rate, uint8_t stop_bits, uint32_t nr_bits,
UARTParityOptions parity);
UARTParityOptions parity, size_t rx_buffer_size);

uint8_t read_byte();
uint8_t peek_byte();
Expand All @@ -44,7 +44,7 @@ class ESP8266SoftwareSerial {

uint32_t bit_time_{0};
uint8_t *rx_buffer_{nullptr};
size_t rx_buffer_size_{512};
size_t rx_buffer_size_;
volatile size_t rx_in_pos_{0};
size_t rx_out_pos_{0};
uint8_t stop_bits_;
Expand Down Expand Up @@ -93,6 +93,7 @@ class UARTComponent : public Component, public Stream {

void set_tx_pin(uint8_t tx_pin) { this->tx_pin_ = tx_pin; }
void set_rx_pin(uint8_t rx_pin) { this->rx_pin_ = rx_pin; }
void set_rx_buffer_size(size_t rx_buffer_size) { this->rx_buffer_size_ = rx_buffer_size; }
void set_stop_bits(uint8_t stop_bits) { this->stop_bits_ = stop_bits; }
void set_data_bits(uint8_t nr_bits) { this->nr_bits_ = nr_bits; }
void set_parity(UARTParityOptions parity) { this->parity_ = parity; }
Expand All @@ -108,6 +109,7 @@ class UARTComponent : public Component, public Stream {
#endif
optional<uint8_t> tx_pin_;
optional<uint8_t> rx_pin_;
size_t rx_buffer_size_;
uint32_t baud_rate_;
uint8_t stop_bits_;
uint8_t nr_bits_;
Expand Down
2 changes: 2 additions & 0 deletions esphome/components/uart/uart_esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ void UARTComponent::setup() {
int8_t tx = this->tx_pin_.has_value() ? *this->tx_pin_ : -1;
int8_t rx = this->rx_pin_.has_value() ? *this->rx_pin_ : -1;
this->hw_serial_->begin(this->baud_rate_, get_config(), rx, tx);
this->hw_serial_->setRxBufferSize(this->rx_buffer_size_);
}

void UARTComponent::dump_config() {
Expand All @@ -90,6 +91,7 @@ void UARTComponent::dump_config() {
}
if (this->rx_pin_.has_value()) {
ESP_LOGCONFIG(TAG, " RX Pin: GPIO%d", *this->rx_pin_);
ESP_LOGCONFIG(TAG, " RX Buffer Size: %u", this->rx_buffer_size_);
}
ESP_LOGCONFIG(TAG, " Baud Rate: %u baud", this->baud_rate_);
ESP_LOGCONFIG(TAG, " Bits: %u", this->nr_bits_);
Expand Down
10 changes: 8 additions & 2 deletions esphome/components/uart/uart_esp8266.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,22 @@ void UARTComponent::setup() {
if (this->tx_pin_.value_or(1) == 1 && this->rx_pin_.value_or(3) == 3) {
this->hw_serial_ = &Serial;
this->hw_serial_->begin(this->baud_rate_, config);
this->hw_serial_->setRxBufferSize(this->rx_buffer_size_);
} else if (this->tx_pin_.value_or(15) == 15 && this->rx_pin_.value_or(13) == 13) {
this->hw_serial_ = &Serial;
this->hw_serial_->begin(this->baud_rate_, config);
this->hw_serial_->setRxBufferSize(this->rx_buffer_size_);
this->hw_serial_->swap();
} else if (this->tx_pin_.value_or(2) == 2 && this->rx_pin_.value_or(8) == 8) {
this->hw_serial_ = &Serial1;
this->hw_serial_->begin(this->baud_rate_, config);
this->hw_serial_->setRxBufferSize(this->rx_buffer_size_);
} else {
this->sw_serial_ = new ESP8266SoftwareSerial();
int8_t tx = this->tx_pin_.has_value() ? *this->tx_pin_ : -1;
int8_t rx = this->rx_pin_.has_value() ? *this->rx_pin_ : -1;
this->sw_serial_->setup(tx, rx, this->baud_rate_, this->stop_bits_, this->nr_bits_, this->parity_);
this->sw_serial_->setup(tx, rx, this->baud_rate_, this->stop_bits_, this->nr_bits_, this->parity_,
this->rx_buffer_size_);
}
}

Expand All @@ -74,6 +78,7 @@ void UARTComponent::dump_config() {
}
if (this->rx_pin_.has_value()) {
ESP_LOGCONFIG(TAG, " RX Pin: GPIO%d", *this->rx_pin_);
ESP_LOGCONFIG(TAG, " RX Buffer Size: %u", this->rx_buffer_size_); // NOLINT
}
ESP_LOGCONFIG(TAG, " Baud Rate: %u baud", this->baud_rate_);
ESP_LOGCONFIG(TAG, " Bits: %u", this->nr_bits_);
Expand Down Expand Up @@ -210,8 +215,9 @@ void ESP8266SoftwareSerial::begin() {
// this->gpio_rx_pin_->attach_interrupt(ESP8266SoftwareSerial::gpio_intr, this, FALLING);
}
void ESP8266SoftwareSerial::setup(int8_t tx_pin, int8_t rx_pin, uint32_t baud_rate, uint8_t stop_bits, uint32_t nr_bits,
UARTParityOptions parity) {
UARTParityOptions parity, size_t rx_buffer_size) {
this->bit_time_ = F_CPU / baud_rate;
this->rx_buffer_size_ = rx_buffer_size;
this->stop_bits_ = stop_bits;
this->nr_bits_ = nr_bits;
this->parity_ = parity;
Expand Down
1 change: 1 addition & 0 deletions esphome/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@
CONF_RUN_CYCLES = 'run_cycles'
CONF_RUN_DURATION = 'run_duration'
CONF_RW_PIN = 'rw_pin'
CONF_RX_BUFFER_SIZE = 'rx_buffer_size'
CONF_RX_ONLY = 'rx_only'
CONF_RX_PIN = 'rx_pin'
CONF_SAFE_MODE = 'safe_mode'
Expand Down
1 change: 1 addition & 0 deletions tests/test1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ uart:
parity: NONE
data_bits: 8
stop_bits: 1
rx_buffer_size: 512

ota:
safe_mode: True
Expand Down

0 comments on commit cd7af19

Please sign in to comment.