Skip to content

Commit e97bc80

Browse files
committed
Allow setting alternate TX for UART 0, so GPIO1 is available as SPI_CS1
1 parent 87c59b1 commit e97bc80

File tree

2 files changed

+79
-28
lines changed

2 files changed

+79
-28
lines changed

cores/esp8266/HardwareSerial.cpp

+64-24
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,11 @@ 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, bool alternate_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, bool alternate_tx);
108+
void uart_set_tx(uart_t* uart, bool alternate_tx);
108109

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

277-
uart_t* uart_start_init(int uart_nr, int baudrate, byte config, byte mode) {
278+
uart_t* uart_start_init(int uart_nr, int baudrate, byte config, byte mode, bool alternate_tx) {
278279

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

@@ -289,8 +290,15 @@ uart_t* uart_start_init(int uart_nr, int baudrate, byte config, byte mode) {
289290
uart->rxEnabled = (mode != SERIAL_TX_ONLY);
290291
uart->txEnabled = (mode != SERIAL_RX_ONLY);
291292
uart->rxPin = (uart->rxEnabled)?3:255;
292-
uart->txPin = (uart->txEnabled)?1:255;
293-
if(uart->rxEnabled) pinMode(uart->rxPin, SPECIAL);
293+
if(uart->rxEnabled) {
294+
if (alternate_tx) {
295+
uart->txPin = 2;
296+
pinMode(uart->rxPin, FUNCTION_4);
297+
} else {
298+
uart->txPin = 1;
299+
pinMode(uart->rxPin, SPECIAL);
300+
}
301+
} else uart->txPin = 255;
294302
if(uart->txEnabled) pinMode(uart->txPin, SPECIAL);
295303
IOSWAP &= ~(1 << IOSWAPU0);
296304
break;
@@ -299,7 +307,7 @@ uart_t* uart_start_init(int uart_nr, int baudrate, byte config, byte mode) {
299307
uart->rxEnabled = false;
300308
uart->txEnabled = (mode != SERIAL_RX_ONLY);
301309
uart->rxPin = 255;
302-
uart->txPin = (uart->txEnabled)?2:255;
310+
uart->txPin = (uart->txEnabled)?2:255; // GPIO7 as TX not possible! See GPIO pins used by UART
303311
if(uart->txEnabled) pinMode(uart->txPin, SPECIAL);
304312
break;
305313
case UART_NO:
@@ -360,40 +368,66 @@ void uart_uninit(uart_t* uart) {
360368
os_free(uart);
361369
}
362370

363-
void uart_swap(uart_t* uart) {
371+
void uart_swap(uart_t* uart, bool alternate_tx) {
364372
if(uart == 0)
365373
return;
366374
switch(uart->uart_nr) {
367375
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);
376+
if(((uart->txPin == 1 || uart->txPin == 2) && uart->txEnabled) || (uart->rxPin == 3 && uart->rxEnabled)) {
372377
if(uart->txEnabled){ //TX
373-
pinMode(1, INPUT);
378+
pinMode(uart->txPin, INPUT);
374379
uart->txPin = 15;
375380
}
376381
if(uart->rxEnabled){ //RX
377-
pinMode(3, INPUT);
382+
pinMode(uart->rxPin, INPUT);
378383
uart->rxPin = 13;
379384
}
385+
if(uart->txEnabled) pinMode(uart->txPin, FUNCTION_4); //TX
386+
if(uart->rxEnabled) pinMode(uart->rxPin, FUNCTION_4); //RX
387+
IOSWAP |= (1 << IOSWAPU0);
380388
} else {
381-
if(uart->txEnabled) pinMode(1, SPECIAL); //TX
382-
if(uart->rxEnabled) pinMode(3, SPECIAL); //RX
383-
IOSWAP &= ~(1 << IOSWAPU0);
384389
if(uart->txEnabled){ //TX
385-
pinMode(15, INPUT);
386-
uart->txPin = 1;
390+
pinMode(uart->txPin, INPUT);
391+
uart->txPin = (alternate_tx)?2:1;
387392
}
388393
if(uart->rxEnabled){ //RX
389-
pinMode(13, INPUT); //RX
394+
pinMode(uart->rxPin, INPUT);
390395
uart->rxPin = 3;
391396
}
397+
if(uart->txEnabled) pinMode(uart->txPin, (alternate_tx)?FUNCTION_4:SPECIAL); //TX
398+
if(uart->rxEnabled) pinMode(3, SPECIAL); //RX
399+
IOSWAP &= ~(1 << IOSWAPU0);
392400
}
393401

394402
break;
395403
case UART1:
396-
// current no swap possible! see GPIO pins used by UART
404+
// Currently no swap possible! See GPIO pins used by UART
405+
break;
406+
default:
407+
break;
408+
}
409+
}
410+
411+
void uart_set_tx(uart_t* uart, bool alternate_tx) {
412+
if(uart == 0)
413+
return;
414+
switch(uart->uart_nr) {
415+
case UART0:
416+
if(uart->txEnabled) {
417+
if (uart->txPin == 1 && alternate_tx) {
418+
pinMode(uart->txPin, INPUT);
419+
uart->txPin = 2;
420+
pinMode(uart->txPin, FUNCTION_4);
421+
} else if (uart->txPin == 2 && !alternate_tx) {
422+
pinMode(uart->txPin, INPUT);
423+
uart->txPin = 1;
424+
pinMode(uart->txPin, SPECIAL);
425+
}
426+
}
427+
428+
break;
429+
case UART1:
430+
// GPIO7 as TX not possible! See GPIO pins used by UART
397431
break;
398432
default:
399433
break;
@@ -488,7 +522,7 @@ HardwareSerial::HardwareSerial(int uart_nr) :
488522
_uart_nr(uart_nr), _uart(0), _tx_buffer(0), _rx_buffer(0) {
489523
}
490524

491-
void HardwareSerial::begin(unsigned long baud, byte config, byte mode) {
525+
void HardwareSerial::begin(unsigned long baud, byte config, byte mode, bool alternate_tx) {
492526
InterruptLock il;
493527

494528
// disable debug for this interface
@@ -499,7 +533,7 @@ void HardwareSerial::begin(unsigned long baud, byte config, byte mode) {
499533
if (_uart) {
500534
os_free(_uart);
501535
}
502-
_uart = uart_start_init(_uart_nr, baud, config, mode);
536+
_uart = uart_start_init(_uart_nr, baud, config, mode, alternate_tx);
503537

504538
if(_uart == 0) {
505539
return;
@@ -538,10 +572,16 @@ void HardwareSerial::end() {
538572
_tx_buffer = 0;
539573
}
540574

541-
void HardwareSerial::swap() {
575+
void HardwareSerial::swap(bool alternate_tx) {
576+
if(_uart == 0)
577+
return;
578+
uart_swap(_uart, alternate_tx);
579+
}
580+
581+
void HardwareSerial::set_tx(bool alternate_tx) {
542582
if(_uart == 0)
543583
return;
544-
uart_swap(_uart);
584+
uart_set_tx(_uart, alternate_tx);
545585
}
546586

547587
void HardwareSerial::setDebugOutput(bool en) {

cores/esp8266/HardwareSerial.h

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

0 commit comments

Comments
 (0)