From efa35a8809bb2abd61a8903b5a502430b1ce34a5 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Wed, 20 Nov 2019 12:24:19 +0100 Subject: [PATCH 1/5] Simple i/f to turn on inverted logic on UART0. --- cores/esp8266/HardwareSerial.cpp | 6 +++++- cores/esp8266/HardwareSerial.h | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/cores/esp8266/HardwareSerial.cpp b/cores/esp8266/HardwareSerial.cpp index 129b55eda3..1c4470e0f1 100644 --- a/cores/esp8266/HardwareSerial.cpp +++ b/cores/esp8266/HardwareSerial.cpp @@ -36,10 +36,14 @@ HardwareSerial::HardwareSerial(int uart_nr) : _uart_nr(uart_nr), _rx_size(256) {} -void HardwareSerial::begin(unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin) +void HardwareSerial::begin(unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin, bool invert) { end(); _uart = uart_init(_uart_nr, baud, (int) config, (int) mode, tx_pin, _rx_size); + if (0 == _uart_nr && invert) + { + U0C0 |= BIT(UCDTRI) | BIT(UCRTSI) | BIT(UCTXI) | BIT(UCDSRI) | BIT(UCCTSI) | BIT(UCRXI); + } #if defined(DEBUG_ESP_PORT) && !defined(NDEBUG) if (static_cast(this) == static_cast(&DEBUG_ESP_PORT)) { diff --git a/cores/esp8266/HardwareSerial.h b/cores/esp8266/HardwareSerial.h index dd36e6c281..67b041e6d1 100644 --- a/cores/esp8266/HardwareSerial.h +++ b/cores/esp8266/HardwareSerial.h @@ -84,7 +84,7 @@ class HardwareSerial: public Stream begin(baud, config, mode, 1); } - void begin(unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin); + void begin(unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin, bool invert=false); void end(); From 3c754c24594545996cebf7d992fd042d3c61295a Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Wed, 20 Nov 2019 12:57:50 +0100 Subject: [PATCH 2/5] Refactor invert from HardwareSerial to uart --- cores/esp8266/HardwareSerial.cpp | 6 +----- cores/esp8266/uart.cpp | 6 +++++- cores/esp8266/uart.h | 2 +- tests/host/common/MockUART.cpp | 3 ++- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/cores/esp8266/HardwareSerial.cpp b/cores/esp8266/HardwareSerial.cpp index 1c4470e0f1..194a0553e5 100644 --- a/cores/esp8266/HardwareSerial.cpp +++ b/cores/esp8266/HardwareSerial.cpp @@ -39,11 +39,7 @@ HardwareSerial::HardwareSerial(int uart_nr) void HardwareSerial::begin(unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin, bool invert) { end(); - _uart = uart_init(_uart_nr, baud, (int) config, (int) mode, tx_pin, _rx_size); - if (0 == _uart_nr && invert) - { - U0C0 |= BIT(UCDTRI) | BIT(UCRTSI) | BIT(UCTXI) | BIT(UCDSRI) | BIT(UCCTSI) | BIT(UCRXI); - } + _uart = uart_init(_uart_nr, baud, (int) config, (int) mode, tx_pin, _rx_size, invert); #if defined(DEBUG_ESP_PORT) && !defined(NDEBUG) if (static_cast(this) == static_cast(&DEBUG_ESP_PORT)) { diff --git a/cores/esp8266/uart.cpp b/cores/esp8266/uart.cpp index 05a425c9c7..b271b77b61 100644 --- a/cores/esp8266/uart.cpp +++ b/cores/esp8266/uart.cpp @@ -577,7 +577,7 @@ uart_get_baudrate(uart_t* uart) } uart_t* -uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx_size) +uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx_size, bool invert) { uart_t* uart = (uart_t*) malloc(sizeof(uart_t)); if(uart == NULL) @@ -669,6 +669,10 @@ uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx if(uart->rx_enabled) { uart_start_isr(uart); } + if(invert) + { + U0C0 |= BIT(UCDTRI) | BIT(UCRTSI) | BIT(UCTXI) | BIT(UCDSRI) | BIT(UCCTSI) | BIT(UCRXI); + } if(gdbstub_has_uart_isr_control()) { ETS_UART_INTR_ENABLE(); // Undo the disable in the switch() above } diff --git a/cores/esp8266/uart.h b/cores/esp8266/uart.h index fd61585689..4871b5e93a 100644 --- a/cores/esp8266/uart.h +++ b/cores/esp8266/uart.h @@ -113,7 +113,7 @@ extern "C" { struct uart_; typedef struct uart_ uart_t; -uart_t* uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx_size); +uart_t* uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx_size, bool invert); void uart_uninit(uart_t* uart); void uart_swap(uart_t* uart, int tx_pin); diff --git a/tests/host/common/MockUART.cpp b/tests/host/common/MockUART.cpp index db0d8c8894..d86e0418ff 100644 --- a/tests/host/common/MockUART.cpp +++ b/tests/host/common/MockUART.cpp @@ -323,10 +323,11 @@ uart_get_bit_length(const int uart_nr) } uart_t* -uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx_size) +uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx_size, bool invert) { (void) config; (void) tx_pin; + (void) invert; uart_t* uart = (uart_t*) malloc(sizeof(uart_t)); if(uart == NULL) return NULL; From dccc005ac5d1bfb09d64edbf8472e2fec08426f5 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Wed, 20 Nov 2019 13:14:33 +0100 Subject: [PATCH 3/5] Final refactoring of invert bits into config bitmap. --- cores/esp8266/uart.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cores/esp8266/uart.cpp b/cores/esp8266/uart.cpp index b271b77b61..411ef637a6 100644 --- a/cores/esp8266/uart.cpp +++ b/cores/esp8266/uart.cpp @@ -657,6 +657,10 @@ uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx } uart_set_baudrate(uart, baudrate); + if(invert) + { + config |= BIT(UCDTRI) | BIT(UCRTSI) | BIT(UCTXI) | BIT(UCDSRI) | BIT(UCCTSI) | BIT(UCRXI); + } USC0(uart->uart_nr) = config; if(!gdbstub_has_uart_isr_control() || uart->uart_nr != UART0) { @@ -669,10 +673,6 @@ uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx if(uart->rx_enabled) { uart_start_isr(uart); } - if(invert) - { - U0C0 |= BIT(UCDTRI) | BIT(UCRTSI) | BIT(UCTXI) | BIT(UCDSRI) | BIT(UCCTSI) | BIT(UCRXI); - } if(gdbstub_has_uart_isr_control()) { ETS_UART_INTR_ENABLE(); // Undo the disable in the switch() above } From 7be11ce7d83a54d0df5229c21cf9842181e2093f Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Wed, 20 Nov 2019 13:39:18 +0100 Subject: [PATCH 4/5] Overload instead of default arg for subclassing. --- cores/esp8266/HardwareSerial.h | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/cores/esp8266/HardwareSerial.h b/cores/esp8266/HardwareSerial.h index 67b041e6d1..dd12e96d25 100644 --- a/cores/esp8266/HardwareSerial.h +++ b/cores/esp8266/HardwareSerial.h @@ -73,18 +73,23 @@ class HardwareSerial: public Stream void begin(unsigned long baud) { - begin(baud, SERIAL_8N1, SERIAL_FULL, 1); + begin(baud, SERIAL_8N1, SERIAL_FULL, 1, false); } void begin(unsigned long baud, SerialConfig config) { - begin(baud, config, SERIAL_FULL, 1); + begin(baud, config, SERIAL_FULL, 1, false); } void begin(unsigned long baud, SerialConfig config, SerialMode mode) { - begin(baud, config, mode, 1); + begin(baud, config, mode, 1, false); } - void begin(unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin, bool invert=false); + void begin(unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin) + { + begin(baud, config, mode, tx_pin, false); + } + + void begin(unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin, bool invert); void end(); From ae75059ec4d263e4dcf6213f983d557de0ee43de Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Wed, 20 Nov 2019 13:54:04 +0100 Subject: [PATCH 5/5] Prevent unwanted effects if setting invert on other than UART0 - only that has these flags defined and documented. --- cores/esp8266/uart.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp8266/uart.cpp b/cores/esp8266/uart.cpp index 411ef637a6..fd8170b586 100644 --- a/cores/esp8266/uart.cpp +++ b/cores/esp8266/uart.cpp @@ -657,7 +657,7 @@ uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx } uart_set_baudrate(uart, baudrate); - if(invert) + if(uart->uart_nr == UART0 && invert) { config |= BIT(UCDTRI) | BIT(UCRTSI) | BIT(UCTXI) | BIT(UCDSRI) | BIT(UCCTSI) | BIT(UCRXI); }