Skip to content

Commit 5ac64ff

Browse files
authored
Merge pull request #7949 from dok-net/hwserial
HW Serial swap and pin setting work only on a few pins
2 parents eedb009 + 8430a09 commit 5ac64ff

File tree

4 files changed

+60
-31
lines changed

4 files changed

+60
-31
lines changed

cores/esp8266/HardwareSerial.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -101,31 +101,31 @@ class HardwareSerial: public Stream
101101
return uart_get_rx_buffer_size(_uart);
102102
}
103103

104-
void swap()
104+
bool swap()
105105
{
106-
swap(1);
106+
return swap(1);
107107
}
108-
void swap(uint8_t tx_pin) //toggle between use of GPIO13/GPIO15 or GPIO3/GPIO(1/2) as RX and TX
108+
bool swap(uint8_t tx_pin) //toggle between use of GPIO13/GPIO15 or GPIO3/GPIO(1/2) as RX and TX
109109
{
110-
uart_swap(_uart, tx_pin);
110+
return uart_swap(_uart, tx_pin);
111111
}
112112

113113
/*
114114
* Toggle between use of GPIO1 and GPIO2 as TX on UART 0.
115115
* Note: UART 1 can't be used if GPIO2 is used with UART 0!
116116
*/
117-
void set_tx(uint8_t tx_pin)
117+
bool set_tx(uint8_t tx_pin)
118118
{
119-
uart_set_tx(_uart, tx_pin);
119+
return uart_set_tx(_uart, tx_pin);
120120
}
121121

122122
/*
123123
* UART 0 possible options are (1, 3), (2, 3) or (15, 13)
124124
* UART 1 allows only TX on 2 if UART 0 is not (2, 3)
125125
*/
126-
void pins(uint8_t tx, uint8_t rx)
126+
bool pins(uint8_t tx, uint8_t rx)
127127
{
128-
uart_set_pins(_uart, tx, rx);
128+
return uart_set_pins(_uart, tx, rx);
129129
}
130130

131131
int available(void) override;

cores/esp8266/uart.cpp

+43-17
Original file line numberDiff line numberDiff line change
@@ -760,11 +760,11 @@ uart_uninit(uart_t* uart)
760760
free(uart);
761761
}
762762

763-
void
763+
bool
764764
uart_swap(uart_t* uart, int tx_pin)
765765
{
766766
if(uart == NULL)
767-
return;
767+
return false;
768768

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

790790
IOSWAP |= (1 << IOSWAPU0);
791+
return true;
791792
}
792793
else
793794
{
@@ -808,6 +809,7 @@ uart_swap(uart_t* uart, int tx_pin)
808809
pinMode(3, SPECIAL); //RX
809810

810811
IOSWAP &= ~(1 << IOSWAPU0);
812+
return true;
811813
}
812814
break;
813815
case UART1:
@@ -816,13 +818,14 @@ uart_swap(uart_t* uart, int tx_pin)
816818
default:
817819
break;
818820
}
821+
return false;
819822
}
820823

821-
void
824+
bool
822825
uart_set_tx(uart_t* uart, int tx_pin)
823826
{
824827
if(uart == NULL)
825-
return;
828+
return false;
826829

827830
switch(uart->uart_nr)
828831
{
@@ -834,12 +837,14 @@ uart_set_tx(uart_t* uart, int tx_pin)
834837
pinMode(uart->tx_pin, INPUT);
835838
uart->tx_pin = 2;
836839
pinMode(uart->tx_pin, FUNCTION_4);
840+
return true;
837841
}
838842
else if (uart->tx_pin == 2 && tx_pin != 2)
839843
{
840844
pinMode(uart->tx_pin, INPUT);
841845
uart->tx_pin = 1;
842846
pinMode(uart->tx_pin, SPECIAL);
847+
return true;
843848
}
844849
}
845850

@@ -850,33 +855,54 @@ uart_set_tx(uart_t* uart, int tx_pin)
850855
default:
851856
break;
852857
}
858+
return false;
853859
}
854860

855-
void
861+
bool
856862
uart_set_pins(uart_t* uart, int tx, int rx)
857863
{
858864
if(uart == NULL)
859-
return;
865+
return false;
866+
867+
if(uart->uart_nr != UART0) // Only UART0 allows pin changes
868+
return false;
860869

861-
if(uart->uart_nr == UART0) // Only UART0 allows pin changes
870+
if(uart->tx_enabled && uart->tx_pin != tx)
862871
{
863-
if(uart->tx_enabled && uart->tx_pin != tx)
872+
if( rx == 13 && tx == 15)
873+
{
874+
if (!uart_swap(uart, 15))
875+
return false;
876+
}
877+
else if (rx == 3 && (tx == 1 || tx == 2))
864878
{
865-
if( rx == 13 && tx == 15)
879+
if (uart->rx_pin != rx)
866880
{
867-
uart_swap(uart, 15);
881+
if (!uart_swap(uart, tx))
882+
return false;
868883
}
869-
else if (rx == 3 && (tx == 1 || tx == 2))
884+
else
870885
{
871-
if (uart->rx_pin != rx)
872-
uart_swap(uart, tx);
873-
else
874-
uart_set_tx(uart, tx);
886+
if (!uart_set_tx(uart, tx))
887+
return false;
875888
}
876889
}
877-
if(uart->rx_enabled && uart->rx_pin != rx && rx == 13 && tx == 15)
878-
uart_swap(uart, 15);
890+
else
891+
return false;
879892
}
893+
894+
if (uart->rx_enabled && uart->rx_pin != rx)
895+
{
896+
if (rx == 13 && tx == 15)
897+
{
898+
if (!uart_swap(uart, 15))
899+
return false;
900+
}
901+
else
902+
return false;
903+
}
904+
905+
return true;
880906
}
881907

882908

cores/esp8266/uart.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ typedef struct uart_ uart_t;
116116
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

119-
void uart_swap(uart_t* uart, int tx_pin);
120-
void uart_set_tx(uart_t* uart, int tx_pin);
121-
void uart_set_pins(uart_t* uart, int tx, int rx);
119+
bool uart_swap(uart_t* uart, int tx_pin);
120+
bool uart_set_tx(uart_t* uart, int tx_pin);
121+
bool uart_set_pins(uart_t* uart, int tx, int rx);
122122
bool uart_tx_enabled(uart_t* uart);
123123
bool uart_rx_enabled(uart_t* uart);
124124

tests/host/common/MockUART.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -407,26 +407,29 @@ uart_uninit(uart_t* uart)
407407
free(uart);
408408
}
409409

410-
void
410+
bool
411411
uart_swap(uart_t* uart, int tx_pin)
412412
{
413413
(void) uart;
414414
(void) tx_pin;
415+
return true;
415416
}
416417

417-
void
418+
bool
418419
uart_set_tx(uart_t* uart, int tx_pin)
419420
{
420421
(void) uart;
421422
(void) tx_pin;
423+
return true;
422424
}
423425

424-
void
426+
bool
425427
uart_set_pins(uart_t* uart, int tx, int rx)
426428
{
427429
(void) uart;
428430
(void) tx;
429431
(void) rx;
432+
return true;
430433
}
431434

432435
bool

0 commit comments

Comments
 (0)