@@ -101,11 +101,12 @@ void uart_disarm_tx_interrupt(uart_t* uart);
101101void uart_set_baudrate (uart_t * uart, int baud_rate);
102102int 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 );
105105void uart_finish_init (uart_t * uart);
106106void 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
110111void uart_ignore_char (char c);
111112void 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
587613void HardwareSerial::setDebugOutput (bool en) {
0 commit comments