Skip to content

Commit 007e495

Browse files
dok-netearlephilhower
authored andcommitted
Implement invert for HardwareSerial (#6816)
* Simple i/f to turn on inverted logic on UART0. * Refactor invert from HardwareSerial to uart * Final refactoring of invert bits into config bitmap. * Overload instead of default arg for subclassing. * Prevent unwanted effects if setting invert on other than UART0 - only that has these flags defined and documented.
1 parent b478429 commit 007e495

File tree

5 files changed

+19
-9
lines changed

5 files changed

+19
-9
lines changed

cores/esp8266/HardwareSerial.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ HardwareSerial::HardwareSerial(int uart_nr)
3636
: _uart_nr(uart_nr), _rx_size(256)
3737
{}
3838

39-
void HardwareSerial::begin(unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin)
39+
void HardwareSerial::begin(unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin, bool invert)
4040
{
4141
end();
42-
_uart = uart_init(_uart_nr, baud, (int) config, (int) mode, tx_pin, _rx_size);
42+
_uart = uart_init(_uart_nr, baud, (int) config, (int) mode, tx_pin, _rx_size, invert);
4343
#if defined(DEBUG_ESP_PORT) && !defined(NDEBUG)
4444
if (static_cast<void*>(this) == static_cast<void*>(&DEBUG_ESP_PORT))
4545
{

cores/esp8266/HardwareSerial.h

+9-4
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,23 @@ class HardwareSerial: public Stream
7373

7474
void begin(unsigned long baud)
7575
{
76-
begin(baud, SERIAL_8N1, SERIAL_FULL, 1);
76+
begin(baud, SERIAL_8N1, SERIAL_FULL, 1, false);
7777
}
7878
void begin(unsigned long baud, SerialConfig config)
7979
{
80-
begin(baud, config, SERIAL_FULL, 1);
80+
begin(baud, config, SERIAL_FULL, 1, false);
8181
}
8282
void begin(unsigned long baud, SerialConfig config, SerialMode mode)
8383
{
84-
begin(baud, config, mode, 1);
84+
begin(baud, config, mode, 1, false);
8585
}
8686

87-
void begin(unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin);
87+
void begin(unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin)
88+
{
89+
begin(baud, config, mode, tx_pin, false);
90+
}
91+
92+
void begin(unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin, bool invert);
8893

8994
void end();
9095

cores/esp8266/uart.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ uart_get_baudrate(uart_t* uart)
577577
}
578578

579579
uart_t*
580-
uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx_size)
580+
uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx_size, bool invert)
581581
{
582582
uart_t* uart = (uart_t*) malloc(sizeof(uart_t));
583583
if(uart == NULL)
@@ -657,6 +657,10 @@ uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx
657657
}
658658

659659
uart_set_baudrate(uart, baudrate);
660+
if(uart->uart_nr == UART0 && invert)
661+
{
662+
config |= BIT(UCDTRI) | BIT(UCRTSI) | BIT(UCTXI) | BIT(UCDSRI) | BIT(UCCTSI) | BIT(UCRXI);
663+
}
660664
USC0(uart->uart_nr) = config;
661665

662666
if(!gdbstub_has_uart_isr_control() || uart->uart_nr != UART0) {

cores/esp8266/uart.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ extern "C" {
113113
struct uart_;
114114
typedef struct uart_ uart_t;
115115

116-
uart_t* uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx_size);
116+
uart_t* uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx_size, bool invert);
117117
void uart_uninit(uart_t* uart);
118118

119119
void uart_swap(uart_t* uart, int tx_pin);

tests/host/common/MockUART.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,11 @@ uart_get_bit_length(const int uart_nr)
323323
}
324324

325325
uart_t*
326-
uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx_size)
326+
uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx_size, bool invert)
327327
{
328328
(void) config;
329329
(void) tx_pin;
330+
(void) invert;
330331
uart_t* uart = (uart_t*) malloc(sizeof(uart_t));
331332
if(uart == NULL)
332333
return NULL;

0 commit comments

Comments
 (0)