mirror of
https://github.com/esphome/esphome.git
synced 2025-10-31 23:21:54 +00:00
Fix the LiberTiny bug with UART pin setup (#11518)
This commit is contained in:
@@ -46,40 +46,58 @@ uint16_t LibreTinyUARTComponent::get_config() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void LibreTinyUARTComponent::setup() {
|
void LibreTinyUARTComponent::setup() {
|
||||||
if (this->rx_pin_) {
|
|
||||||
this->rx_pin_->setup();
|
|
||||||
}
|
|
||||||
if (this->tx_pin_ && this->rx_pin_ != this->tx_pin_) {
|
|
||||||
this->tx_pin_->setup();
|
|
||||||
}
|
|
||||||
|
|
||||||
int8_t tx_pin = tx_pin_ == nullptr ? -1 : tx_pin_->get_pin();
|
int8_t tx_pin = tx_pin_ == nullptr ? -1 : tx_pin_->get_pin();
|
||||||
int8_t rx_pin = rx_pin_ == nullptr ? -1 : rx_pin_->get_pin();
|
int8_t rx_pin = rx_pin_ == nullptr ? -1 : rx_pin_->get_pin();
|
||||||
bool tx_inverted = tx_pin_ != nullptr && tx_pin_->is_inverted();
|
bool tx_inverted = tx_pin_ != nullptr && tx_pin_->is_inverted();
|
||||||
bool rx_inverted = rx_pin_ != nullptr && rx_pin_->is_inverted();
|
bool rx_inverted = rx_pin_ != nullptr && rx_pin_->is_inverted();
|
||||||
|
|
||||||
|
auto shouldFallbackToSoftwareSerial = [&]() -> bool {
|
||||||
|
auto hasFlags = [](InternalGPIOPin *pin, const gpio::Flags mask) -> bool {
|
||||||
|
return pin && pin->get_flags() & mask != gpio::Flags::FLAG_NONE;
|
||||||
|
};
|
||||||
|
if (hasFlags(this->tx_pin_, gpio::Flags::FLAG_OPEN_DRAIN | gpio::Flags::FLAG_PULLUP | gpio::Flags::FLAG_PULLDOWN) ||
|
||||||
|
hasFlags(this->rx_pin_, gpio::Flags::FLAG_OPEN_DRAIN | gpio::Flags::FLAG_PULLUP | gpio::Flags::FLAG_PULLDOWN)) {
|
||||||
|
#if LT_ARD_HAS_SOFTSERIAL
|
||||||
|
ESP_LOGI(TAG, "Pins has flags set. Using Software Serial");
|
||||||
|
return true;
|
||||||
|
#else
|
||||||
|
ESP_LOGW(TAG, "Pin flags are set but not supported for hardware serial. Ignoring");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
if (false)
|
if (false)
|
||||||
return;
|
return;
|
||||||
#if LT_HW_UART0
|
#if LT_HW_UART0
|
||||||
else if ((tx_pin == -1 || tx_pin == PIN_SERIAL0_TX) && (rx_pin == -1 || rx_pin == PIN_SERIAL0_RX)) {
|
else if ((tx_pin == -1 || tx_pin == PIN_SERIAL0_TX) && (rx_pin == -1 || rx_pin == PIN_SERIAL0_RX) &&
|
||||||
|
!shouldFallbackToSoftwareSerial()) {
|
||||||
this->serial_ = &Serial0;
|
this->serial_ = &Serial0;
|
||||||
this->hardware_idx_ = 0;
|
this->hardware_idx_ = 0;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#if LT_HW_UART1
|
#if LT_HW_UART1
|
||||||
else if ((tx_pin == -1 || tx_pin == PIN_SERIAL1_TX) && (rx_pin == -1 || rx_pin == PIN_SERIAL1_RX)) {
|
else if ((tx_pin == -1 || tx_pin == PIN_SERIAL1_TX) && (rx_pin == -1 || rx_pin == PIN_SERIAL1_RX) &&
|
||||||
|
!shouldFallbackToSoftwareSerial()) {
|
||||||
this->serial_ = &Serial1;
|
this->serial_ = &Serial1;
|
||||||
this->hardware_idx_ = 1;
|
this->hardware_idx_ = 1;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#if LT_HW_UART2
|
#if LT_HW_UART2
|
||||||
else if ((tx_pin == -1 || tx_pin == PIN_SERIAL2_TX) && (rx_pin == -1 || rx_pin == PIN_SERIAL2_RX)) {
|
else if ((tx_pin == -1 || tx_pin == PIN_SERIAL2_TX) && (rx_pin == -1 || rx_pin == PIN_SERIAL2_RX) &&
|
||||||
|
!shouldFallbackToSoftwareSerial()) {
|
||||||
this->serial_ = &Serial2;
|
this->serial_ = &Serial2;
|
||||||
this->hardware_idx_ = 2;
|
this->hardware_idx_ = 2;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
else {
|
else {
|
||||||
#if LT_ARD_HAS_SOFTSERIAL
|
#if LT_ARD_HAS_SOFTSERIAL
|
||||||
|
if (this->rx_pin_) {
|
||||||
|
this->rx_pin_->setup();
|
||||||
|
}
|
||||||
|
if (this->tx_pin_ && this->rx_pin_ != this->tx_pin_) {
|
||||||
|
this->tx_pin_->setup();
|
||||||
|
}
|
||||||
this->serial_ = new SoftwareSerial(rx_pin, tx_pin, rx_inverted || tx_inverted);
|
this->serial_ = new SoftwareSerial(rx_pin, tx_pin, rx_inverted || tx_inverted);
|
||||||
#else
|
#else
|
||||||
this->serial_ = &Serial;
|
this->serial_ = &Serial;
|
||||||
|
|||||||
Reference in New Issue
Block a user