mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-19 12:43:28 +02:00
fb2b7ade41
* uart: Add support for specifying the number of bits and parity. ESP8266SwSerial doesn't really check parity but just read the parity bit and ignore it when receiving data. Signed-off-by: 0hax <0hax@protonmail.com> * uart: support begin and end methods. A component may need to reset uart buffer/status by using begin() and end() methods. This is useful for example when a component needs to be sure it is not reading garbage from previously received data over uart. For end() methods with software serial, disabling interrupt is currently impossible because of a bug in esp8266 Core: https://github.com/esp8266/Arduino/issues/6049 Signed-off-by: 0hax <0hax@protonmail.com> * esphal: add support for detaching an interrupt. That's needed when a component needs to enable/disable interrupt on a gpio. Signed-off-by: 0hax <0hax@protonmail.com> * uart: rename CONF_NR_BITS to CONF_DATA_BITS_NUMBER. Signed-off-by: 0hax <0hax@protonmail.com> * uart: use static const uint32_t instead of #define. Signed-off-by: 0hax <0hax@protonmail.com> * uart: use an enum to handle parity. Signed-off-by: 0hax <0hax@protonmail.com> * uart: split between esp32 and esp8266. Signed-off-by: 0hax <0hax@protonmail.com> * uart: check_uart_settings for parity and number of data bits. Signed-off-by: 0hax <0hax@protonmail.com> * name param data_bits * add new params to test Co-authored-by: Guillermo Ruffino <glm.net@gmail.com>
80 lines
2.2 KiB
C++
80 lines
2.2 KiB
C++
#include "uart.h"
|
|
#include "esphome/core/log.h"
|
|
#include "esphome/core/helpers.h"
|
|
#include "esphome/core/application.h"
|
|
#include "esphome/core/defines.h"
|
|
|
|
#ifdef USE_LOGGER
|
|
#include "esphome/components/logger/logger.h"
|
|
#endif
|
|
|
|
namespace esphome {
|
|
namespace uart {
|
|
|
|
static const char *TAG = "uart";
|
|
|
|
size_t UARTComponent::write(uint8_t data) {
|
|
this->write_byte(data);
|
|
return 1;
|
|
}
|
|
int UARTComponent::read() {
|
|
uint8_t data;
|
|
if (!this->read_byte(&data))
|
|
return -1;
|
|
return data;
|
|
}
|
|
int UARTComponent::peek() {
|
|
uint8_t data;
|
|
if (!this->peek_byte(&data))
|
|
return -1;
|
|
return data;
|
|
}
|
|
|
|
void UARTComponent::check_logger_conflict_() {
|
|
#ifdef USE_LOGGER
|
|
if (this->hw_serial_ == nullptr || logger::global_logger->get_baud_rate() == 0) {
|
|
return;
|
|
}
|
|
|
|
if (this->hw_serial_ == logger::global_logger->get_hw_serial()) {
|
|
ESP_LOGW(TAG, " You're using the same serial port for logging and the UART component. Please "
|
|
"disable logging over the serial port by setting logger->baud_rate to 0.");
|
|
}
|
|
#endif
|
|
}
|
|
|
|
void UARTDevice::check_uart_settings(uint32_t baud_rate, uint8_t stop_bits, UARTParityOptions parity, uint8_t nr_bits) {
|
|
if (this->parent_->baud_rate_ != baud_rate) {
|
|
ESP_LOGE(TAG, " Invalid baud_rate: Integration requested baud_rate %u but you have %u!", baud_rate,
|
|
this->parent_->baud_rate_);
|
|
}
|
|
if (this->parent_->stop_bits_ != stop_bits) {
|
|
ESP_LOGE(TAG, " Invalid stop bits: Integration requested stop_bits %u but you have %u!", stop_bits,
|
|
this->parent_->stop_bits_);
|
|
}
|
|
if (this->parent_->nr_bits_ != nr_bits) {
|
|
ESP_LOGE(TAG, " Invalid number of data bits: Integration requested %u data bits but you have %u!", nr_bits,
|
|
this->parent_->nr_bits_);
|
|
}
|
|
if (this->parent_->parity_ != parity) {
|
|
ESP_LOGE(TAG, " Invalid parity: Integration requested parity %s but you have %s!", parity_to_str(parity),
|
|
parity_to_str(this->parent_->parity_));
|
|
}
|
|
}
|
|
|
|
const char *parity_to_str(UARTParityOptions parity) {
|
|
switch (parity) {
|
|
case UART_CONFIG_PARITY_NONE:
|
|
return "NONE";
|
|
case UART_CONFIG_PARITY_EVEN:
|
|
return "EVEN";
|
|
case UART_CONFIG_PARITY_ODD:
|
|
return "ODD";
|
|
default:
|
|
return "UNKNOWN";
|
|
}
|
|
}
|
|
|
|
} // namespace uart
|
|
} // namespace esphome
|