Skip to content

Commit d68b971

Browse files
committed
Use explicit TX pin number and add pins setting method
1 parent e97bc80 commit d68b971

File tree

2 files changed

+58
-24
lines changed

2 files changed

+58
-24
lines changed

cores/esp8266/HardwareSerial.cpp

+43-17
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,12 @@ void uart_disarm_tx_interrupt(uart_t* uart);
101101
void uart_set_baudrate(uart_t* uart, int baud_rate);
102102
int uart_get_baudrate(uart_t* uart);
103103

104-
uart_t* uart_start_init(int uart_nr, int baudrate, byte config, bool alternate_tx);
104+
uart_t* uart_start_init(int uart_nr, int baudrate, byte config, uint8_t use_tx);
105105
void uart_finish_init(uart_t* uart);
106106
void uart_uninit(uart_t* uart);
107-
void uart_swap(uart_t* uart, bool alternate_tx);
108-
void uart_set_tx(uart_t* uart, bool alternate_tx);
107+
void uart_swap(uart_t* uart, uint8_t use_tx);
108+
void uart_set_tx(uart_t* uart, uint8_t use_tx);
109+
void uart_set_pins(uart_t* uart, uint8_t tx, uint8_t rx);
109110

110111
void uart_ignore_char(char c);
111112
void uart0_write_char(char c);
@@ -275,7 +276,7 @@ int uart_get_baudrate(uart_t* uart) {
275276
return uart->baud_rate;
276277
}
277278

278-
uart_t* uart_start_init(int uart_nr, int baudrate, byte config, byte mode, bool alternate_tx) {
279+
uart_t* uart_start_init(int uart_nr, int baudrate, byte config, byte mode, uint8_t use_tx) {
279280

280281
uart_t* uart = (uart_t*) os_malloc(sizeof(uart_t));
281282

@@ -291,7 +292,7 @@ uart_t* uart_start_init(int uart_nr, int baudrate, byte config, byte mode, bool
291292
uart->txEnabled = (mode != SERIAL_RX_ONLY);
292293
uart->rxPin = (uart->rxEnabled)?3:255;
293294
if(uart->rxEnabled) {
294-
if (alternate_tx) {
295+
if (use_tx == 2) {
295296
uart->txPin = 2;
296297
pinMode(uart->rxPin, FUNCTION_4);
297298
} else {
@@ -368,7 +369,7 @@ void uart_uninit(uart_t* uart) {
368369
os_free(uart);
369370
}
370371

371-
void uart_swap(uart_t* uart, bool alternate_tx) {
372+
void uart_swap(uart_t* uart, uint8_t use_tx) {
372373
if(uart == 0)
373374
return;
374375
switch(uart->uart_nr) {
@@ -388,13 +389,13 @@ void uart_swap(uart_t* uart, bool alternate_tx) {
388389
} else {
389390
if(uart->txEnabled){ //TX
390391
pinMode(uart->txPin, INPUT);
391-
uart->txPin = (alternate_tx)?2:1;
392+
uart->txPin = (use_tx == 2)?2:1;
392393
}
393394
if(uart->rxEnabled){ //RX
394395
pinMode(uart->rxPin, INPUT);
395396
uart->rxPin = 3;
396397
}
397-
if(uart->txEnabled) pinMode(uart->txPin, (alternate_tx)?FUNCTION_4:SPECIAL); //TX
398+
if(uart->txEnabled) pinMode(uart->txPin, (use_tx == 2)?FUNCTION_4:SPECIAL); //TX
398399
if(uart->rxEnabled) pinMode(3, SPECIAL); //RX
399400
IOSWAP &= ~(1 << IOSWAPU0);
400401
}
@@ -408,17 +409,17 @@ void uart_swap(uart_t* uart, bool alternate_tx) {
408409
}
409410
}
410411

411-
void uart_set_tx(uart_t* uart, bool alternate_tx) {
412+
void uart_set_tx(uart_t* uart, uint8_t use_tx) {
412413
if(uart == 0)
413414
return;
414415
switch(uart->uart_nr) {
415416
case UART0:
416417
if(uart->txEnabled) {
417-
if (uart->txPin == 1 && alternate_tx) {
418+
if (uart->txPin == 1 && use_tx == 2) {
418419
pinMode(uart->txPin, INPUT);
419420
uart->txPin = 2;
420421
pinMode(uart->txPin, FUNCTION_4);
421-
} else if (uart->txPin == 2 && !alternate_tx) {
422+
} else if (uart->txPin == 2 && use_tx != 2) {
422423
pinMode(uart->txPin, INPUT);
423424
uart->txPin = 1;
424425
pinMode(uart->txPin, SPECIAL);
@@ -434,6 +435,25 @@ void uart_set_tx(uart_t* uart, bool alternate_tx) {
434435
}
435436
}
436437

438+
void uart_set_pins(uart_t* uart, uint8_t tx, uint8_t rx) {
439+
if(uart == 0)
440+
return;
441+
442+
if(uart->uart_nr == UART0) { // Only UART0 allows pin changes
443+
if(uart->txEnabled && uart->txPin != tx) {
444+
if( rx == 13 && tx == 15) {
445+
uart_swap(uart, 15);
446+
} else if (rx == 3 && (tx == 1 || tx == 2)) {
447+
if (uart->rxPin != rx) uart_swap(uart, tx);
448+
else uart_set_tx(uart, tx);
449+
}
450+
}
451+
if(uart->rxEnabled && uart->rxPin != rx && rx == 13 && tx == 15) {
452+
uart_swap(uart, 15);
453+
}
454+
}
455+
}
456+
437457
// ####################################################################################################
438458
// ####################################################################################################
439459
// ####################################################################################################
@@ -522,7 +542,7 @@ HardwareSerial::HardwareSerial(int uart_nr) :
522542
_uart_nr(uart_nr), _uart(0), _tx_buffer(0), _rx_buffer(0) {
523543
}
524544

525-
void HardwareSerial::begin(unsigned long baud, byte config, byte mode, bool alternate_tx) {
545+
void HardwareSerial::begin(unsigned long baud, byte config, byte mode, uint8_t use_tx) {
526546
InterruptLock il;
527547

528548
// disable debug for this interface
@@ -533,7 +553,7 @@ void HardwareSerial::begin(unsigned long baud, byte config, byte mode, bool alte
533553
if (_uart) {
534554
os_free(_uart);
535555
}
536-
_uart = uart_start_init(_uart_nr, baud, config, mode, alternate_tx);
556+
_uart = uart_start_init(_uart_nr, baud, config, mode, use_tx);
537557

538558
if(_uart == 0) {
539559
return;
@@ -572,16 +592,22 @@ void HardwareSerial::end() {
572592
_tx_buffer = 0;
573593
}
574594

575-
void HardwareSerial::swap(bool alternate_tx) {
595+
void HardwareSerial::swap(uint8_t use_tx) {
596+
if(_uart == 0)
597+
return;
598+
uart_swap(_uart, use_tx);
599+
}
600+
601+
void HardwareSerial::set_tx(uint8_t use_tx) {
576602
if(_uart == 0)
577603
return;
578-
uart_swap(_uart, alternate_tx);
604+
uart_set_tx(_uart, use_tx);
579605
}
580606

581-
void HardwareSerial::set_tx(bool alternate_tx) {
607+
void HardwareSerial::pins(uint8_t tx, uint8_t rx) {
582608
if(_uart == 0)
583609
return;
584-
uart_set_tx(_uart, alternate_tx);
610+
uart_set_pins(_uart, tx, rx);
585611
}
586612

587613
void HardwareSerial::setDebugOutput(bool en) {

cores/esp8266/HardwareSerial.h

+15-7
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,33 @@ class HardwareSerial: public Stream {
7474
HardwareSerial(int uart_nr);
7575

7676
void begin(unsigned long baud) {
77-
begin(baud, SERIAL_8N1, SERIAL_FULL, false);
77+
begin(baud, SERIAL_8N1, SERIAL_FULL, 1);
7878
}
7979
void begin(unsigned long baud, uint8_t config) {
80-
begin(baud, config, SERIAL_FULL, false);
80+
begin(baud, config, SERIAL_FULL, 1);
8181
}
8282
void begin(unsigned long baud, uint8_t config, uint8_t mode) {
83-
begin(baud, config, mode, false);
83+
begin(baud, config, mode, 1);
8484
}
85-
void begin(unsigned long, uint8_t, uint8_t, bool);
85+
void begin(unsigned long, uint8_t, uint8_t, uint8_t);
8686
void end();
8787
void swap() {
88-
swap(false);
88+
swap(1);
8989
}
90-
void swap(bool alternate_tx); //toggle between use of GPIO13/GPIO15 or GPIO3/GPIO(1/2) as RX and TX
90+
void swap(uint8_t use_tx); //toggle between use of GPIO13/GPIO15 or GPIO3/GPIO(1/2) as RX and TX
91+
9192
/*
9293
* Toggle between use of GPIO1 and GPIO2 as TX on UART 0.
9394
* Note: UART 1 can't be used if GPIO2 is used with UART 0!
9495
*/
95-
void set_tx(bool alternate_tx);
96+
void set_tx(uint8_t use_tx);
97+
98+
/*
99+
* UART 0 possible options are (1, 3), (2, 3) or (15, 13)
100+
* UART 1 allows only TX on 2 if UART 0 is not (2, 3)
101+
*/
102+
void pins(uint8_t tx, uint8_t rx);
103+
96104
int available(void) override;
97105
int peek(void) override;
98106
int read(void) override;

0 commit comments

Comments
 (0)