@@ -101,11 +101,12 @@ void uart_disarm_tx_interrupt(uart_t* uart);
101
101
void uart_set_baudrate (uart_t * uart, int baud_rate);
102
102
int uart_get_baudrate (uart_t * uart);
103
103
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 );
105
105
void uart_finish_init (uart_t * uart);
106
106
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);
109
110
110
111
void uart_ignore_char (char c);
111
112
void uart0_write_char (char c);
@@ -275,7 +276,7 @@ int uart_get_baudrate(uart_t* uart) {
275
276
return uart->baud_rate ;
276
277
}
277
278
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 ) {
279
280
280
281
uart_t * uart = (uart_t *) os_malloc (sizeof (uart_t ));
281
282
@@ -291,7 +292,7 @@ uart_t* uart_start_init(int uart_nr, int baudrate, byte config, byte mode, bool
291
292
uart->txEnabled = (mode != SERIAL_RX_ONLY);
292
293
uart->rxPin = (uart->rxEnabled )?3 :255 ;
293
294
if (uart->rxEnabled ) {
294
- if (alternate_tx ) {
295
+ if (use_tx == 2 ) {
295
296
uart->txPin = 2 ;
296
297
pinMode (uart->rxPin , FUNCTION_4);
297
298
} else {
@@ -368,7 +369,7 @@ void uart_uninit(uart_t* uart) {
368
369
os_free (uart);
369
370
}
370
371
371
- void uart_swap (uart_t * uart, bool alternate_tx ) {
372
+ void uart_swap (uart_t * uart, uint8_t use_tx ) {
372
373
if (uart == 0 )
373
374
return ;
374
375
switch (uart->uart_nr ) {
@@ -388,13 +389,13 @@ void uart_swap(uart_t* uart, bool alternate_tx) {
388
389
} else {
389
390
if (uart->txEnabled ){ // TX
390
391
pinMode (uart->txPin , INPUT);
391
- uart->txPin = (alternate_tx )?2 :1 ;
392
+ uart->txPin = (use_tx == 2 )?2 :1 ;
392
393
}
393
394
if (uart->rxEnabled ){ // RX
394
395
pinMode (uart->rxPin , INPUT);
395
396
uart->rxPin = 3 ;
396
397
}
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
398
399
if (uart->rxEnabled ) pinMode (3 , SPECIAL); // RX
399
400
IOSWAP &= ~(1 << IOSWAPU0);
400
401
}
@@ -408,17 +409,17 @@ void uart_swap(uart_t* uart, bool alternate_tx) {
408
409
}
409
410
}
410
411
411
- void uart_set_tx (uart_t * uart, bool alternate_tx ) {
412
+ void uart_set_tx (uart_t * uart, uint8_t use_tx ) {
412
413
if (uart == 0 )
413
414
return ;
414
415
switch (uart->uart_nr ) {
415
416
case UART0:
416
417
if (uart->txEnabled ) {
417
- if (uart->txPin == 1 && alternate_tx ) {
418
+ if (uart->txPin == 1 && use_tx == 2 ) {
418
419
pinMode (uart->txPin , INPUT);
419
420
uart->txPin = 2 ;
420
421
pinMode (uart->txPin , FUNCTION_4);
421
- } else if (uart->txPin == 2 && !alternate_tx ) {
422
+ } else if (uart->txPin == 2 && use_tx != 2 ) {
422
423
pinMode (uart->txPin , INPUT);
423
424
uart->txPin = 1 ;
424
425
pinMode (uart->txPin , SPECIAL);
@@ -434,6 +435,25 @@ void uart_set_tx(uart_t* uart, bool alternate_tx) {
434
435
}
435
436
}
436
437
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
+
437
457
// ####################################################################################################
438
458
// ####################################################################################################
439
459
// ####################################################################################################
@@ -522,7 +542,7 @@ HardwareSerial::HardwareSerial(int uart_nr) :
522
542
_uart_nr(uart_nr), _uart(0 ), _tx_buffer(0 ), _rx_buffer(0 ) {
523
543
}
524
544
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 ) {
526
546
InterruptLock il;
527
547
528
548
// disable debug for this interface
@@ -533,7 +553,7 @@ void HardwareSerial::begin(unsigned long baud, byte config, byte mode, bool alte
533
553
if (_uart) {
534
554
os_free (_uart);
535
555
}
536
- _uart = uart_start_init (_uart_nr, baud, config, mode, alternate_tx );
556
+ _uart = uart_start_init (_uart_nr, baud, config, mode, use_tx );
537
557
538
558
if (_uart == 0 ) {
539
559
return ;
@@ -572,16 +592,22 @@ void HardwareSerial::end() {
572
592
_tx_buffer = 0 ;
573
593
}
574
594
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) {
576
602
if (_uart == 0 )
577
603
return ;
578
- uart_swap (_uart, alternate_tx );
604
+ uart_set_tx (_uart, use_tx );
579
605
}
580
606
581
- void HardwareSerial::set_tx ( bool alternate_tx ) {
607
+ void HardwareSerial::pins ( uint8_t tx, uint8_t rx ) {
582
608
if (_uart == 0 )
583
609
return ;
584
- uart_set_tx (_uart, alternate_tx );
610
+ uart_set_pins (_uart, tx, rx );
585
611
}
586
612
587
613
void HardwareSerial::setDebugOutput (bool en) {
0 commit comments