Skip to content

[BREAKING] HW Serial swap and pin setting work only on a few pins #7949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions cores/esp8266/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,31 +101,31 @@ class HardwareSerial: public Stream
return uart_get_rx_buffer_size(_uart);
}

void swap()
bool swap()
{
swap(1);
return swap(1);
}
void swap(uint8_t tx_pin) //toggle between use of GPIO13/GPIO15 or GPIO3/GPIO(1/2) as RX and TX
bool swap(uint8_t tx_pin) //toggle between use of GPIO13/GPIO15 or GPIO3/GPIO(1/2) as RX and TX
{
uart_swap(_uart, tx_pin);
return uart_swap(_uart, tx_pin);
}

/*
* Toggle between use of GPIO1 and GPIO2 as TX on UART 0.
* Note: UART 1 can't be used if GPIO2 is used with UART 0!
*/
void set_tx(uint8_t tx_pin)
bool set_tx(uint8_t tx_pin)
{
uart_set_tx(_uart, tx_pin);
return uart_set_tx(_uart, tx_pin);
}

/*
* UART 0 possible options are (1, 3), (2, 3) or (15, 13)
* UART 1 allows only TX on 2 if UART 0 is not (2, 3)
*/
void pins(uint8_t tx, uint8_t rx)
bool pins(uint8_t tx, uint8_t rx)
{
uart_set_pins(_uart, tx, rx);
return uart_set_pins(_uart, tx, rx);
}

int available(void) override;
Expand Down
60 changes: 43 additions & 17 deletions cores/esp8266/uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,11 +760,11 @@ uart_uninit(uart_t* uart)
free(uart);
}

void
bool
uart_swap(uart_t* uart, int tx_pin)
{
if(uart == NULL)
return;
return false;

switch(uart->uart_nr)
{
Expand All @@ -788,6 +788,7 @@ uart_swap(uart_t* uart, int tx_pin)
pinMode(uart->rx_pin, FUNCTION_4); //RX

IOSWAP |= (1 << IOSWAPU0);
return true;
}
else
{
Expand All @@ -808,6 +809,7 @@ uart_swap(uart_t* uart, int tx_pin)
pinMode(3, SPECIAL); //RX

IOSWAP &= ~(1 << IOSWAPU0);
return true;
}
break;
case UART1:
Expand All @@ -816,13 +818,14 @@ uart_swap(uart_t* uart, int tx_pin)
default:
break;
}
return false;
}

void
bool
uart_set_tx(uart_t* uart, int tx_pin)
{
if(uart == NULL)
return;
return false;

switch(uart->uart_nr)
{
Expand All @@ -834,12 +837,14 @@ uart_set_tx(uart_t* uart, int tx_pin)
pinMode(uart->tx_pin, INPUT);
uart->tx_pin = 2;
pinMode(uart->tx_pin, FUNCTION_4);
return true;
}
else if (uart->tx_pin == 2 && tx_pin != 2)
{
pinMode(uart->tx_pin, INPUT);
uart->tx_pin = 1;
pinMode(uart->tx_pin, SPECIAL);
return true;
}
}

Expand All @@ -850,33 +855,54 @@ uart_set_tx(uart_t* uart, int tx_pin)
default:
break;
}
return false;
}

void
bool
uart_set_pins(uart_t* uart, int tx, int rx)
{
if(uart == NULL)
return;
return false;

if(uart->uart_nr != UART0) // Only UART0 allows pin changes
return false;

if(uart->uart_nr == UART0) // Only UART0 allows pin changes
if(uart->tx_enabled && uart->tx_pin != tx)
{
if(uart->tx_enabled && uart->tx_pin != tx)
if( rx == 13 && tx == 15)
{
if (!uart_swap(uart, 15))
return false;
}
else if (rx == 3 && (tx == 1 || tx == 2))
{
if( rx == 13 && tx == 15)
if (uart->rx_pin != rx)
{
uart_swap(uart, 15);
if (!uart_swap(uart, tx))
return false;
}
else if (rx == 3 && (tx == 1 || tx == 2))
else
{
if (uart->rx_pin != rx)
uart_swap(uart, tx);
else
uart_set_tx(uart, tx);
if (!uart_set_tx(uart, tx))
return false;
}
}
if(uart->rx_enabled && uart->rx_pin != rx && rx == 13 && tx == 15)
uart_swap(uart, 15);
else
return false;
}

if (uart->rx_enabled && uart->rx_pin != rx)
{
if (rx == 13 && tx == 15)
{
if (!uart_swap(uart, 15))
return false;
}
else
return false;
}

return true;
}


Expand Down
6 changes: 3 additions & 3 deletions cores/esp8266/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ 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, bool invert);
void uart_uninit(uart_t* uart);

void uart_swap(uart_t* uart, int tx_pin);
void uart_set_tx(uart_t* uart, int tx_pin);
void uart_set_pins(uart_t* uart, int tx, int rx);
bool uart_swap(uart_t* uart, int tx_pin);
bool uart_set_tx(uart_t* uart, int tx_pin);
bool uart_set_pins(uart_t* uart, int tx, int rx);
bool uart_tx_enabled(uart_t* uart);
bool uart_rx_enabled(uart_t* uart);

Expand Down
9 changes: 6 additions & 3 deletions tests/host/common/MockUART.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,26 +407,29 @@ uart_uninit(uart_t* uart)
free(uart);
}

void
bool
uart_swap(uart_t* uart, int tx_pin)
{
(void) uart;
(void) tx_pin;
return true;
}

void
bool
uart_set_tx(uart_t* uart, int tx_pin)
{
(void) uart;
(void) tx_pin;
return true;
}

void
bool
uart_set_pins(uart_t* uart, int tx, int rx)
{
(void) uart;
(void) tx;
(void) rx;
return true;
}

bool
Expand Down