Skip to content

Commit f14ecdb

Browse files
committed
Merge pull request #1424 from KaloNK/master
Allow setting alternate TX for UART 0, so GPIO1 is available as SPI_CS1
2 parents e443b0c + 8bd0b2a commit f14ecdb

File tree

2 files changed

+113
-28
lines changed

2 files changed

+113
-28
lines changed

cores/esp8266/HardwareSerial.cpp

+90-24
Original file line numberDiff line numberDiff line change
@@ -101,10 +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);
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);
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);
108110

109111
void uart_ignore_char(char c);
110112
void uart0_write_char(char c);
@@ -274,7 +276,7 @@ int uart_get_baudrate(uart_t* uart) {
274276
return uart->baud_rate;
275277
}
276278

277-
uart_t* uart_start_init(int uart_nr, int baudrate, byte config, byte mode) {
279+
uart_t* uart_start_init(int uart_nr, int baudrate, byte config, byte mode, uint8_t use_tx) {
278280

279281
uart_t* uart = (uart_t*) os_malloc(sizeof(uart_t));
280282

@@ -289,8 +291,15 @@ uart_t* uart_start_init(int uart_nr, int baudrate, byte config, byte mode) {
289291
uart->rxEnabled = (mode != SERIAL_TX_ONLY);
290292
uart->txEnabled = (mode != SERIAL_RX_ONLY);
291293
uart->rxPin = (uart->rxEnabled)?3:255;
292-
uart->txPin = (uart->txEnabled)?1:255;
293-
if(uart->rxEnabled) pinMode(uart->rxPin, SPECIAL);
294+
if(uart->rxEnabled) {
295+
if (use_tx == 2) {
296+
uart->txPin = 2;
297+
pinMode(uart->rxPin, FUNCTION_4);
298+
} else {
299+
uart->txPin = 1;
300+
pinMode(uart->rxPin, SPECIAL);
301+
}
302+
} else uart->txPin = 255;
294303
if(uart->txEnabled) pinMode(uart->txPin, SPECIAL);
295304
IOSWAP &= ~(1 << IOSWAPU0);
296305
break;
@@ -299,7 +308,7 @@ uart_t* uart_start_init(int uart_nr, int baudrate, byte config, byte mode) {
299308
uart->rxEnabled = false;
300309
uart->txEnabled = (mode != SERIAL_RX_ONLY);
301310
uart->rxPin = 255;
302-
uart->txPin = (uart->txEnabled)?2:255;
311+
uart->txPin = (uart->txEnabled)?2:255; // GPIO7 as TX not possible! See GPIO pins used by UART
303312
if(uart->txEnabled) pinMode(uart->txPin, SPECIAL);
304313
break;
305314
case UART_NO:
@@ -360,46 +369,91 @@ void uart_uninit(uart_t* uart) {
360369
os_free(uart);
361370
}
362371

363-
void uart_swap(uart_t* uart) {
372+
void uart_swap(uart_t* uart, uint8_t use_tx) {
364373
if(uart == 0)
365374
return;
366375
switch(uart->uart_nr) {
367376
case UART0:
368-
if((uart->txPin == 1 && uart->txEnabled) || (uart->rxPin == 3 && uart->rxEnabled)) {
369-
if(uart->txEnabled) pinMode(15, FUNCTION_4); //TX
370-
if(uart->rxEnabled) pinMode(13, FUNCTION_4); //RX
371-
IOSWAP |= (1 << IOSWAPU0);
377+
if(((uart->txPin == 1 || uart->txPin == 2) && uart->txEnabled) || (uart->rxPin == 3 && uart->rxEnabled)) {
372378
if(uart->txEnabled){ //TX
373-
pinMode(1, INPUT);
379+
pinMode(uart->txPin, INPUT);
374380
uart->txPin = 15;
375381
}
376382
if(uart->rxEnabled){ //RX
377-
pinMode(3, INPUT);
383+
pinMode(uart->rxPin, INPUT);
378384
uart->rxPin = 13;
379385
}
386+
if(uart->txEnabled) pinMode(uart->txPin, FUNCTION_4); //TX
387+
if(uart->rxEnabled) pinMode(uart->rxPin, FUNCTION_4); //RX
388+
IOSWAP |= (1 << IOSWAPU0);
380389
} else {
381-
if(uart->txEnabled) pinMode(1, SPECIAL); //TX
382-
if(uart->rxEnabled) pinMode(3, SPECIAL); //RX
383-
IOSWAP &= ~(1 << IOSWAPU0);
384390
if(uart->txEnabled){ //TX
385-
pinMode(15, INPUT);
386-
uart->txPin = 1;
391+
pinMode(uart->txPin, INPUT);
392+
uart->txPin = (use_tx == 2)?2:1;
387393
}
388394
if(uart->rxEnabled){ //RX
389-
pinMode(13, INPUT); //RX
395+
pinMode(uart->rxPin, INPUT);
390396
uart->rxPin = 3;
391397
}
398+
if(uart->txEnabled) pinMode(uart->txPin, (use_tx == 2)?FUNCTION_4:SPECIAL); //TX
399+
if(uart->rxEnabled) pinMode(3, SPECIAL); //RX
400+
IOSWAP &= ~(1 << IOSWAPU0);
401+
}
402+
403+
break;
404+
case UART1:
405+
// Currently no swap possible! See GPIO pins used by UART
406+
break;
407+
default:
408+
break;
409+
}
410+
}
411+
412+
void uart_set_tx(uart_t* uart, uint8_t use_tx) {
413+
if(uart == 0)
414+
return;
415+
switch(uart->uart_nr) {
416+
case UART0:
417+
if(uart->txEnabled) {
418+
if (uart->txPin == 1 && use_tx == 2) {
419+
pinMode(uart->txPin, INPUT);
420+
uart->txPin = 2;
421+
pinMode(uart->txPin, FUNCTION_4);
422+
} else if (uart->txPin == 2 && use_tx != 2) {
423+
pinMode(uart->txPin, INPUT);
424+
uart->txPin = 1;
425+
pinMode(uart->txPin, SPECIAL);
426+
}
392427
}
393428

394429
break;
395430
case UART1:
396-
// current no swap possible! see GPIO pins used by UART
431+
// GPIO7 as TX not possible! See GPIO pins used by UART
397432
break;
398433
default:
399434
break;
400435
}
401436
}
402437

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+
403457
// ####################################################################################################
404458
// ####################################################################################################
405459
// ####################################################################################################
@@ -488,7 +542,7 @@ HardwareSerial::HardwareSerial(int uart_nr) :
488542
_uart_nr(uart_nr), _uart(0), _tx_buffer(0), _rx_buffer(0) {
489543
}
490544

491-
void HardwareSerial::begin(unsigned long baud, byte config, byte mode) {
545+
void HardwareSerial::begin(unsigned long baud, byte config, byte mode, uint8_t use_tx) {
492546
InterruptLock il;
493547

494548
// disable debug for this interface
@@ -499,7 +553,7 @@ void HardwareSerial::begin(unsigned long baud, byte config, byte mode) {
499553
if (_uart) {
500554
os_free(_uart);
501555
}
502-
_uart = uart_start_init(_uart_nr, baud, config, mode);
556+
_uart = uart_start_init(_uart_nr, baud, config, mode, use_tx);
503557

504558
if(_uart == 0) {
505559
return;
@@ -538,10 +592,22 @@ void HardwareSerial::end() {
538592
_tx_buffer = 0;
539593
}
540594

541-
void HardwareSerial::swap() {
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) {
602+
if(_uart == 0)
603+
return;
604+
uart_set_tx(_uart, use_tx);
605+
}
606+
607+
void HardwareSerial::pins(uint8_t tx, uint8_t rx) {
542608
if(_uart == 0)
543609
return;
544-
uart_swap(_uart);
610+
uart_set_pins(_uart, tx, rx);
545611
}
546612

547613
void HardwareSerial::setDebugOutput(bool en) {

cores/esp8266/HardwareSerial.h

+23-4
Original file line numberDiff line numberDiff line change
@@ -74,14 +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);
77+
begin(baud, SERIAL_8N1, SERIAL_FULL, 1);
7878
}
7979
void begin(unsigned long baud, uint8_t config) {
80-
begin(baud, config, SERIAL_FULL);
80+
begin(baud, config, SERIAL_FULL, 1);
8181
}
82-
void begin(unsigned long, uint8_t, uint8_t);
82+
void begin(unsigned long baud, uint8_t config, uint8_t mode) {
83+
begin(baud, config, mode, 1);
84+
}
85+
void begin(unsigned long, uint8_t, uint8_t, uint8_t);
8386
void end();
84-
void swap(); //toggle between use of GPIO13/GPIO15 or GPIO3/GPIO1 as RX and TX
87+
void swap() {
88+
swap(1);
89+
}
90+
void swap(uint8_t use_tx); //toggle between use of GPIO13/GPIO15 or GPIO3/GPIO(1/2) as RX and TX
91+
92+
/*
93+
* Toggle between use of GPIO1 and GPIO2 as TX on UART 0.
94+
* Note: UART 1 can't be used if GPIO2 is used with UART 0!
95+
*/
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+
85104
int available(void) override;
86105
int peek(void) override;
87106
int read(void) override;

0 commit comments

Comments
 (0)