Skip to content

Commit

Permalink
Implement invert for HardwareSerial (esp8266#6816)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
dok-net authored and earlephilhower committed Nov 20, 2019
1 parent b478429 commit 007e495
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions cores/esp8266/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ 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);
_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<void*>(this) == static_cast<void*>(&DEBUG_ESP_PORT))
{
Expand Down
13 changes: 9 additions & 4 deletions cores/esp8266/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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();

Expand Down
6 changes: 5 additions & 1 deletion cores/esp8266/uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(uart->uart_nr == UART0 && 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) {
Expand Down
2 changes: 1 addition & 1 deletion cores/esp8266/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion tests/host/common/MockUART.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 007e495

Please sign in to comment.