mirror of
https://github.com/esphome/esphome.git
synced 2024-11-22 23:18:10 +01:00
Clean up UART Improvements code (#1190)
This commit is contained in:
parent
43d5e7a8cc
commit
62468198d6
4 changed files with 22 additions and 57 deletions
|
@ -43,7 +43,8 @@ void UARTComponent::check_logger_conflict_() {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void UARTDevice::check_uart_settings(uint32_t baud_rate, uint8_t stop_bits, UARTParityOptions parity, uint8_t nr_bits) {
|
void UARTDevice::check_uart_settings(uint32_t baud_rate, uint8_t stop_bits, UARTParityOptions parity,
|
||||||
|
uint8_t data_bits) {
|
||||||
if (this->parent_->baud_rate_ != baud_rate) {
|
if (this->parent_->baud_rate_ != baud_rate) {
|
||||||
ESP_LOGE(TAG, " Invalid baud_rate: Integration requested baud_rate %u but you have %u!", baud_rate,
|
ESP_LOGE(TAG, " Invalid baud_rate: Integration requested baud_rate %u but you have %u!", baud_rate,
|
||||||
this->parent_->baud_rate_);
|
this->parent_->baud_rate_);
|
||||||
|
@ -52,9 +53,9 @@ void UARTDevice::check_uart_settings(uint32_t baud_rate, uint8_t stop_bits, UART
|
||||||
ESP_LOGE(TAG, " Invalid stop bits: Integration requested stop_bits %u but you have %u!", stop_bits,
|
ESP_LOGE(TAG, " Invalid stop bits: Integration requested stop_bits %u but you have %u!", stop_bits,
|
||||||
this->parent_->stop_bits_);
|
this->parent_->stop_bits_);
|
||||||
}
|
}
|
||||||
if (this->parent_->nr_bits_ != nr_bits) {
|
if (this->parent_->data_bits_ != data_bits) {
|
||||||
ESP_LOGE(TAG, " Invalid number of data bits: Integration requested %u data bits but you have %u!", nr_bits,
|
ESP_LOGE(TAG, " Invalid number of data bits: Integration requested %u data bits but you have %u!", data_bits,
|
||||||
this->parent_->nr_bits_);
|
this->parent_->data_bits_);
|
||||||
}
|
}
|
||||||
if (this->parent_->parity_ != parity) {
|
if (this->parent_->parity_ != parity) {
|
||||||
ESP_LOGE(TAG, " Invalid parity: Integration requested parity %s but you have %s!", parity_to_str(parity),
|
ESP_LOGE(TAG, " Invalid parity: Integration requested parity %s but you have %s!", parity_to_str(parity),
|
||||||
|
|
|
@ -18,7 +18,7 @@ const char *parity_to_str(UARTParityOptions parity);
|
||||||
#ifdef ARDUINO_ARCH_ESP8266
|
#ifdef ARDUINO_ARCH_ESP8266
|
||||||
class ESP8266SoftwareSerial {
|
class ESP8266SoftwareSerial {
|
||||||
public:
|
public:
|
||||||
void setup(int8_t tx_pin, int8_t rx_pin, uint32_t baud_rate, uint8_t stop_bits, uint32_t nr_bits,
|
void setup(int8_t tx_pin, int8_t rx_pin, uint32_t baud_rate, uint8_t stop_bits, uint32_t data_bits,
|
||||||
UARTParityOptions parity, size_t rx_buffer_size);
|
UARTParityOptions parity, size_t rx_buffer_size);
|
||||||
|
|
||||||
uint8_t read_byte();
|
uint8_t read_byte();
|
||||||
|
@ -30,8 +30,6 @@ class ESP8266SoftwareSerial {
|
||||||
|
|
||||||
int available();
|
int available();
|
||||||
|
|
||||||
void begin();
|
|
||||||
void end();
|
|
||||||
GPIOPin *gpio_tx_pin_{nullptr};
|
GPIOPin *gpio_tx_pin_{nullptr};
|
||||||
GPIOPin *gpio_rx_pin_{nullptr};
|
GPIOPin *gpio_rx_pin_{nullptr};
|
||||||
|
|
||||||
|
@ -48,7 +46,7 @@ class ESP8266SoftwareSerial {
|
||||||
volatile size_t rx_in_pos_{0};
|
volatile size_t rx_in_pos_{0};
|
||||||
size_t rx_out_pos_{0};
|
size_t rx_out_pos_{0};
|
||||||
uint8_t stop_bits_;
|
uint8_t stop_bits_;
|
||||||
uint8_t nr_bits_;
|
uint8_t data_bits_;
|
||||||
UARTParityOptions parity_;
|
UARTParityOptions parity_;
|
||||||
ISRInternalGPIOPin *tx_pin_{nullptr};
|
ISRInternalGPIOPin *tx_pin_{nullptr};
|
||||||
ISRInternalGPIOPin *rx_pin_{nullptr};
|
ISRInternalGPIOPin *rx_pin_{nullptr};
|
||||||
|
@ -71,8 +69,6 @@ class UARTComponent : public Component, public Stream {
|
||||||
void write_array(const std::vector<uint8_t> &data) { this->write_array(&data[0], data.size()); }
|
void write_array(const std::vector<uint8_t> &data) { this->write_array(&data[0], data.size()); }
|
||||||
|
|
||||||
void write_str(const char *str);
|
void write_str(const char *str);
|
||||||
void end();
|
|
||||||
void begin();
|
|
||||||
|
|
||||||
bool peek_byte(uint8_t *data);
|
bool peek_byte(uint8_t *data);
|
||||||
|
|
||||||
|
@ -95,7 +91,7 @@ class UARTComponent : public Component, public Stream {
|
||||||
void set_rx_pin(uint8_t rx_pin) { this->rx_pin_ = rx_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_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_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_data_bits(uint8_t data_bits) { this->data_bits_ = data_bits; }
|
||||||
void set_parity(UARTParityOptions parity) { this->parity_ = parity; }
|
void set_parity(UARTParityOptions parity) { this->parity_ = parity; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -112,7 +108,7 @@ class UARTComponent : public Component, public Stream {
|
||||||
size_t rx_buffer_size_;
|
size_t rx_buffer_size_;
|
||||||
uint32_t baud_rate_;
|
uint32_t baud_rate_;
|
||||||
uint8_t stop_bits_;
|
uint8_t stop_bits_;
|
||||||
uint8_t nr_bits_;
|
uint8_t data_bits_;
|
||||||
UARTParityOptions parity_;
|
UARTParityOptions parity_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -156,12 +152,10 @@ class UARTDevice : public Stream {
|
||||||
size_t write(uint8_t data) override { return this->parent_->write(data); }
|
size_t write(uint8_t data) override { return this->parent_->write(data); }
|
||||||
int read() override { return this->parent_->read(); }
|
int read() override { return this->parent_->read(); }
|
||||||
int peek() override { return this->parent_->peek(); }
|
int peek() override { return this->parent_->peek(); }
|
||||||
void end() { this->parent_->end(); }
|
|
||||||
void begin() { this->parent_->begin(); }
|
|
||||||
|
|
||||||
/// Check that the configuration of the UART bus matches the provided values and otherwise print a warning
|
/// Check that the configuration of the UART bus matches the provided values and otherwise print a warning
|
||||||
void check_uart_settings(uint32_t baud_rate, uint8_t stop_bits = 1,
|
void check_uart_settings(uint32_t baud_rate, uint8_t stop_bits = 1,
|
||||||
UARTParityOptions parity = UART_CONFIG_PARITY_NONE, uint8_t nr_bits = 8);
|
UARTParityOptions parity = UART_CONFIG_PARITY_NONE, uint8_t data_bits = 8);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
UARTComponent *parent_{nullptr};
|
UARTComponent *parent_{nullptr};
|
||||||
|
|
|
@ -43,7 +43,7 @@ uint32_t UARTComponent::get_config() {
|
||||||
else if (this->parity_ == UART_CONFIG_PARITY_ODD)
|
else if (this->parity_ == UART_CONFIG_PARITY_ODD)
|
||||||
config |= UART_PARITY_ODD | UART_PARITY_EN;
|
config |= UART_PARITY_ODD | UART_PARITY_EN;
|
||||||
|
|
||||||
switch (this->nr_bits_) {
|
switch (this->data_bits_) {
|
||||||
case 5:
|
case 5:
|
||||||
config |= UART_NB_BIT_5;
|
config |= UART_NB_BIT_5;
|
||||||
break;
|
break;
|
||||||
|
@ -94,7 +94,7 @@ void UARTComponent::dump_config() {
|
||||||
ESP_LOGCONFIG(TAG, " RX Buffer Size: %u", this->rx_buffer_size_);
|
ESP_LOGCONFIG(TAG, " RX Buffer Size: %u", this->rx_buffer_size_);
|
||||||
}
|
}
|
||||||
ESP_LOGCONFIG(TAG, " Baud Rate: %u baud", this->baud_rate_);
|
ESP_LOGCONFIG(TAG, " Baud Rate: %u baud", this->baud_rate_);
|
||||||
ESP_LOGCONFIG(TAG, " Bits: %u", this->nr_bits_);
|
ESP_LOGCONFIG(TAG, " Data Bits: %u", this->data_bits_);
|
||||||
ESP_LOGCONFIG(TAG, " Parity: %s", parity_to_str(this->parity_));
|
ESP_LOGCONFIG(TAG, " Parity: %s", parity_to_str(this->parity_));
|
||||||
ESP_LOGCONFIG(TAG, " Stop bits: %u", this->stop_bits_);
|
ESP_LOGCONFIG(TAG, " Stop bits: %u", this->stop_bits_);
|
||||||
this->check_logger_conflict_();
|
this->check_logger_conflict_();
|
||||||
|
@ -114,8 +114,6 @@ void UARTComponent::write_str(const char *str) {
|
||||||
this->hw_serial_->write(str);
|
this->hw_serial_->write(str);
|
||||||
ESP_LOGVV(TAG, " Wrote \"%s\"", str);
|
ESP_LOGVV(TAG, " Wrote \"%s\"", str);
|
||||||
}
|
}
|
||||||
void UARTComponent::end() { this->hw_serial_->end(); }
|
|
||||||
void UARTComponent::begin() { this->hw_serial_->begin(this->baud_rate_, get_config()); }
|
|
||||||
bool UARTComponent::read_byte(uint8_t *data) {
|
bool UARTComponent::read_byte(uint8_t *data) {
|
||||||
if (!this->check_read_timeout_())
|
if (!this->check_read_timeout_())
|
||||||
return false;
|
return false;
|
||||||
|
@ -161,4 +159,4 @@ void UARTComponent::flush() {
|
||||||
|
|
||||||
} // namespace uart
|
} // namespace uart
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
#endif // ESP32
|
#endif // ARDUINO_ARCH_ESP32
|
||||||
|
|
|
@ -19,7 +19,7 @@ uint32_t UARTComponent::get_config() {
|
||||||
else if (this->parity_ == UART_CONFIG_PARITY_ODD)
|
else if (this->parity_ == UART_CONFIG_PARITY_ODD)
|
||||||
config |= UART_PARITY_ODD;
|
config |= UART_PARITY_ODD;
|
||||||
|
|
||||||
switch (this->nr_bits_) {
|
switch (this->data_bits_) {
|
||||||
case 5:
|
case 5:
|
||||||
config |= UART_NB_BIT_5;
|
config |= UART_NB_BIT_5;
|
||||||
break;
|
break;
|
||||||
|
@ -66,7 +66,7 @@ void UARTComponent::setup() {
|
||||||
this->sw_serial_ = new ESP8266SoftwareSerial();
|
this->sw_serial_ = new ESP8266SoftwareSerial();
|
||||||
int8_t tx = this->tx_pin_.has_value() ? *this->tx_pin_ : -1;
|
int8_t tx = this->tx_pin_.has_value() ? *this->tx_pin_ : -1;
|
||||||
int8_t rx = this->rx_pin_.has_value() ? *this->rx_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->data_bits_, this->parity_,
|
||||||
this->rx_buffer_size_);
|
this->rx_buffer_size_);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,7 +81,7 @@ void UARTComponent::dump_config() {
|
||||||
ESP_LOGCONFIG(TAG, " RX Buffer Size: %u", this->rx_buffer_size_); // NOLINT
|
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, " Baud Rate: %u baud", this->baud_rate_);
|
||||||
ESP_LOGCONFIG(TAG, " Bits: %u", this->nr_bits_);
|
ESP_LOGCONFIG(TAG, " Data Bits: %u", this->data_bits_);
|
||||||
ESP_LOGCONFIG(TAG, " Parity: %s", parity_to_str(this->parity_));
|
ESP_LOGCONFIG(TAG, " Parity: %s", parity_to_str(this->parity_));
|
||||||
ESP_LOGCONFIG(TAG, " Stop bits: %u", this->stop_bits_);
|
ESP_LOGCONFIG(TAG, " Stop bits: %u", this->stop_bits_);
|
||||||
if (this->hw_serial_ != nullptr) {
|
if (this->hw_serial_ != nullptr) {
|
||||||
|
@ -121,18 +121,6 @@ void UARTComponent::write_str(const char *str) {
|
||||||
}
|
}
|
||||||
ESP_LOGVV(TAG, " Wrote \"%s\"", str);
|
ESP_LOGVV(TAG, " Wrote \"%s\"", str);
|
||||||
}
|
}
|
||||||
void UARTComponent::end() {
|
|
||||||
if (this->hw_serial_ != nullptr)
|
|
||||||
this->hw_serial_->end();
|
|
||||||
else if (this->sw_serial_ != nullptr)
|
|
||||||
this->sw_serial_->end();
|
|
||||||
}
|
|
||||||
void UARTComponent::begin() {
|
|
||||||
if (this->hw_serial_ != nullptr)
|
|
||||||
this->hw_serial_->begin(this->baud_rate_, static_cast<SerialConfig>(get_config()));
|
|
||||||
else if (this->sw_serial_ != nullptr)
|
|
||||||
this->sw_serial_->begin();
|
|
||||||
}
|
|
||||||
bool UARTComponent::read_byte(uint8_t *data) {
|
bool UARTComponent::read_byte(uint8_t *data) {
|
||||||
if (!this->check_read_timeout_())
|
if (!this->check_read_timeout_())
|
||||||
return false;
|
return false;
|
||||||
|
@ -198,28 +186,12 @@ void UARTComponent::flush() {
|
||||||
this->sw_serial_->flush();
|
this->sw_serial_->flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void ESP8266SoftwareSerial::end() {
|
void ESP8266SoftwareSerial::setup(int8_t tx_pin, int8_t rx_pin, uint32_t baud_rate, uint8_t stop_bits,
|
||||||
/* Because of this bug: https://github.com/esp8266/Arduino/issues/6049
|
uint32_t data_bits, UARTParityOptions parity, size_t rx_buffer_size) {
|
||||||
* detach_interrupt can't called.
|
|
||||||
* So simply reset rx_in_pos and rx_out_pos even if it's totally racy with
|
|
||||||
* the interrupt.
|
|
||||||
*/
|
|
||||||
// this->gpio_rx_pin_->detach_interrupt();
|
|
||||||
this->rx_in_pos_ = 0;
|
|
||||||
this->rx_out_pos_ = 0;
|
|
||||||
}
|
|
||||||
void ESP8266SoftwareSerial::begin() {
|
|
||||||
/* attach_interrupt() is also not safe because gpio_intr() may
|
|
||||||
* endup with arg == nullptr.
|
|
||||||
*/
|
|
||||||
// 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, size_t rx_buffer_size) {
|
|
||||||
this->bit_time_ = F_CPU / baud_rate;
|
this->bit_time_ = F_CPU / baud_rate;
|
||||||
this->rx_buffer_size_ = rx_buffer_size;
|
this->rx_buffer_size_ = rx_buffer_size;
|
||||||
this->stop_bits_ = stop_bits;
|
this->stop_bits_ = stop_bits;
|
||||||
this->nr_bits_ = nr_bits;
|
this->data_bits_ = data_bits;
|
||||||
this->parity_ = parity;
|
this->parity_ = parity;
|
||||||
if (tx_pin != -1) {
|
if (tx_pin != -1) {
|
||||||
auto pin = GPIOPin(tx_pin, OUTPUT);
|
auto pin = GPIOPin(tx_pin, OUTPUT);
|
||||||
|
@ -242,7 +214,7 @@ void ICACHE_RAM_ATTR ESP8266SoftwareSerial::gpio_intr(ESP8266SoftwareSerial *arg
|
||||||
const uint32_t start = ESP.getCycleCount();
|
const uint32_t start = ESP.getCycleCount();
|
||||||
uint8_t rec = 0;
|
uint8_t rec = 0;
|
||||||
// Manually unroll the loop
|
// Manually unroll the loop
|
||||||
for (int i = 0; i < arg->nr_bits_; i++)
|
for (int i = 0; i < arg->data_bits_; i++)
|
||||||
rec |= arg->read_bit_(&wait, start) << i;
|
rec |= arg->read_bit_(&wait, start) << i;
|
||||||
|
|
||||||
/* If parity is enabled, just read it and ignore it. */
|
/* If parity is enabled, just read it and ignore it. */
|
||||||
|
@ -282,7 +254,7 @@ void ICACHE_RAM_ATTR HOT ESP8266SoftwareSerial::write_byte(uint8_t data) {
|
||||||
const uint32_t start = ESP.getCycleCount();
|
const uint32_t start = ESP.getCycleCount();
|
||||||
// Start bit
|
// Start bit
|
||||||
this->write_bit_(false, &wait, start);
|
this->write_bit_(false, &wait, start);
|
||||||
for (int i = 0; i < this->nr_bits_; i++) {
|
for (int i = 0; i < this->data_bits_; i++) {
|
||||||
bool bit = data & (1 << i);
|
bool bit = data & (1 << i);
|
||||||
this->write_bit_(bit, &wait, start);
|
this->write_bit_(bit, &wait, start);
|
||||||
if (need_parity_bit)
|
if (need_parity_bit)
|
||||||
|
@ -333,4 +305,4 @@ int ESP8266SoftwareSerial::available() {
|
||||||
|
|
||||||
} // namespace uart
|
} // namespace uart
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
#endif // ESP8266
|
#endif // ARDUINO_ARCH_ESP8266
|
||||||
|
|
Loading…
Reference in a new issue