Uart improvments (#1024)

* 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>
This commit is contained in:
0hax
2020-05-24 23:59:07 +02:00
committed by GitHub
parent 4de44a99eb
commit fb2b7ade41
8 changed files with 572 additions and 343 deletions
+29 -2
View File
@@ -7,10 +7,19 @@
namespace esphome {
namespace uart {
enum UARTParityOptions {
UART_CONFIG_PARITY_NONE,
UART_CONFIG_PARITY_EVEN,
UART_CONFIG_PARITY_ODD,
};
const char *parity_to_str(UARTParityOptions parity);
#ifdef ARDUINO_ARCH_ESP8266
class ESP8266SoftwareSerial {
public:
void setup(int8_t tx_pin, int8_t rx_pin, uint32_t baud_rate, uint8_t stop_bits);
void setup(int8_t tx_pin, int8_t rx_pin, uint32_t baud_rate, uint8_t stop_bits, uint32_t nr_bits,
UARTParityOptions parity);
uint8_t read_byte();
uint8_t peek_byte();
@@ -21,6 +30,11 @@ class ESP8266SoftwareSerial {
int available();
void begin();
void end();
GPIOPin *gpio_tx_pin_{nullptr};
GPIOPin *gpio_rx_pin_{nullptr};
protected:
static void gpio_intr(ESP8266SoftwareSerial *arg);
@@ -34,6 +48,8 @@ class ESP8266SoftwareSerial {
volatile size_t rx_in_pos_{0};
size_t rx_out_pos_{0};
uint8_t stop_bits_;
uint8_t nr_bits_;
UARTParityOptions parity_;
ISRInternalGPIOPin *tx_pin_{nullptr};
ISRInternalGPIOPin *rx_pin_{nullptr};
};
@@ -43,6 +59,8 @@ class UARTComponent : public Component, public Stream {
public:
void set_baud_rate(uint32_t baud_rate) { baud_rate_ = baud_rate; }
uint32_t get_config();
void setup() override;
void dump_config() override;
@@ -53,6 +71,8 @@ class UARTComponent : public Component, public Stream {
void write_array(const std::vector<uint8_t> &data) { this->write_array(&data[0], data.size()); }
void write_str(const char *str);
void end();
void begin();
bool peek_byte(uint8_t *data);
@@ -74,6 +94,8 @@ 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_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; }
protected:
void check_logger_conflict_();
@@ -88,6 +110,8 @@ class UARTComponent : public Component, public Stream {
optional<uint8_t> rx_pin_;
uint32_t baud_rate_;
uint8_t stop_bits_;
uint8_t nr_bits_;
UARTParityOptions parity_;
};
#ifdef ARDUINO_ARCH_ESP32
@@ -130,9 +154,12 @@ class UARTDevice : public Stream {
size_t write(uint8_t data) override { return this->parent_->write(data); }
int read() override { return this->parent_->read(); }
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
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);
protected:
UARTComponent *parent_{nullptr};