Skip to content

Commit 3931249

Browse files
committed
HardwareSerial: fix issue with swapping pins twice
Previously calling HardwareSerial::swap twice would set the pins back to the original setting (1, 3). This feature was lost when swap function got an optional argument for the TX pin. This change reverts that, restoring ability to swap pins back. To use TX pin 2, HardwareSerial::pins or HardwareSerial::set_tx methods can be used. This change also simplifies UART pin configuration code.
1 parent 1c4a48d commit 3931249

File tree

5 files changed

+155
-183
lines changed

5 files changed

+155
-183
lines changed

cores/esp8266/HardwareSerial.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ HardwareSerial::HardwareSerial(int uart_nr)
3535
: _uart_nr(uart_nr), _rx_size(256)
3636
{}
3737

38-
void HardwareSerial::begin(unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin)
38+
void HardwareSerial::begin(unsigned long baud, SerialConfig config, SerialMode mode, int tx_pin, int rx_pin)
3939
{
4040
end();
41-
_uart = uart_init(_uart_nr, baud, (int) config, (int) mode, tx_pin, _rx_size);
41+
_uart = uart_init(_uart_nr, baud, (int) config, (int) mode, tx_pin, rx_pin, _rx_size);
4242
}
4343

4444
void HardwareSerial::end()
@@ -62,23 +62,23 @@ size_t HardwareSerial::setRxBufferSize(size_t size){
6262
return _rx_size;
6363
}
6464

65-
void HardwareSerial::swap(uint8_t tx_pin)
65+
void HardwareSerial::swap()
6666
{
6767
if(!_uart) {
6868
return;
6969
}
70-
uart_swap(_uart, tx_pin);
70+
uart_swap(_uart);
7171
}
7272

73-
void HardwareSerial::set_tx(uint8_t tx_pin)
73+
void HardwareSerial::set_tx(int tx_pin)
7474
{
7575
if(!_uart) {
7676
return;
7777
}
7878
uart_set_tx(_uart, tx_pin);
7979
}
8080

81-
void HardwareSerial::pins(uint8_t tx, uint8_t rx)
81+
void HardwareSerial::pins(int tx, int rx)
8282
{
8383
if(!_uart) {
8484
return;

cores/esp8266/HardwareSerial.h

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,40 +72,36 @@ class HardwareSerial: public Stream
7272

7373
void begin(unsigned long baud)
7474
{
75-
begin(baud, SERIAL_8N1, SERIAL_FULL, 1);
75+
begin(baud, SERIAL_8N1, SERIAL_FULL, -1, -1);
7676
}
7777
void begin(unsigned long baud, SerialConfig config)
7878
{
79-
begin(baud, config, SERIAL_FULL, 1);
79+
begin(baud, config, SERIAL_FULL, -1, -1);
8080
}
8181
void begin(unsigned long baud, SerialConfig config, SerialMode mode)
8282
{
83-
begin(baud, config, mode, 1);
83+
begin(baud, config, mode, -1, -1);
8484
}
8585

86-
void begin(unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin);
86+
void begin(unsigned long baud, SerialConfig config, SerialMode mode, int tx_pin, int rx_pin);
8787

8888
void end();
8989

9090
size_t setRxBufferSize(size_t size);
9191

92-
void swap()
93-
{
94-
swap(1);
95-
}
96-
void swap(uint8_t tx_pin); //toggle between use of GPIO13/GPIO15 or GPIO3/GPIO(1/2) as RX and TX
92+
void swap(); //toggle between use of GPIO13/GPIO15 or GPIO3/GPIO1 as RX and TX
9793

9894
/*
99-
* Toggle between use of GPIO1 and GPIO2 as TX on UART 0.
95+
* Set TX pin. On UART0, either GPIO1 or GPIO2 can be used.
10096
* Note: UART 1 can't be used if GPIO2 is used with UART 0!
10197
*/
102-
void set_tx(uint8_t tx_pin);
98+
void set_tx(int tx_pin);
10399

104100
/*
105101
* UART 0 possible options are (1, 3), (2, 3) or (15, 13)
106102
* UART 1 allows only TX on 2 if UART 0 is not (2, 3)
107103
*/
108-
void pins(uint8_t tx, uint8_t rx);
104+
void pins(int tx, int rx);
109105

110106
int available(void) override;
111107
int peek(void) override;

cores/esp8266/core_esp8266_main.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,19 @@ extern "C" void __gdb_do_break(){}
147147
extern "C" void gdb_do_break(void) __attribute__ ((weak, alias("__gdb_do_break")));
148148

149149
void init_done() {
150-
system_set_os_print(1);
151150
gdb_init();
152151
do_global_ctors();
153-
printf("\n%08x\n", core_version);
154152
esp_schedule();
155153
}
156154

155+
extern "C" void uart0_wait_tx_empty();
157156

158157
extern "C" void user_init(void) {
159158
struct rst_info *rtc_info_ptr = system_get_rst_info();
160159
memcpy((void *) &resetInfo, (void *) rtc_info_ptr, sizeof(resetInfo));
161-
160+
uart0_wait_tx_empty();
162161
uart_div_modify(0, UART_CLK_FREQ / (115200));
163-
162+
164163
init();
165164

166165
initVariant();

0 commit comments

Comments
 (0)