From 72c52f0c261b6e0e67d2743a9a104940092d15f1 Mon Sep 17 00:00:00 2001 From: MCUdude Date: Sat, 23 Feb 2019 20:54:43 +0100 Subject: [PATCH] Add Serial.swap This will be very useful when working on other hardware where the serial pins may or may not be swapped. Default state and swap state defined in pins_arduino.h Call Serial.swap() or Serial.swap(true) to swap pins, and call Serial.swap(false) to return to the default state --- avr/cores/arduino/UART.cpp | 39 +++++++++++++++++++++++++++- avr/cores/arduino/UART.h | 6 ++++- avr/cores/arduino/UART0.cpp | 2 +- avr/cores/arduino/UART1.cpp | 2 +- avr/cores/arduino/UART2.cpp | 2 +- avr/cores/arduino/UART3.cpp | 2 +- avr/cores/arduino/UART_private.h | 8 +++++- avr/variants/uno_wifi/pins_arduino.h | 20 +++++++++++--- 8 files changed, 70 insertions(+), 11 deletions(-) diff --git a/avr/cores/arduino/UART.cpp b/avr/cores/arduino/UART.cpp index fa8098c..3f4234c 100644 --- a/avr/cores/arduino/UART.cpp +++ b/avr/cores/arduino/UART.cpp @@ -131,7 +131,7 @@ void UartClass::begin(unsigned long baud, uint16_t config) this->end(); } - // Setup port mux + // Setup default port mux PORTMUX.USARTROUTEA |= _uart_mux; int32_t baud_setting = 0; @@ -173,6 +173,43 @@ void UartClass::begin(unsigned long baud, uint16_t config) SREG = oldSREG; } +void UartClass::swap(uint8_t shouldSwap) +{ + if(shouldSwap == true) + { + // Let PORTMUX pont to alternative UART pins + if(_uart_mux_swap > 0) + PORTMUX.USARTROUTEA |= _uart_mux; + else + PORTMUX.USARTROUTEA &= ~_uart_mux; + + // Set pin state for alternative UART pins + pinMode(_hwserial_rx_pin_swap, INPUT_PULLUP); + digitalWrite(_hwserial_tx_pin_swap, HIGH); + pinMode(_hwserial_tx_pin_swap, OUTPUT); + + // Set previous pins to high Z + pinMode(_hwserial_rx_pin, INPUT); + pinMode(_hwserial_tx_pin, INPUT); + } + else //if(shouldSwap == false) + { + if(_uart_mux > 0) + PORTMUX.USARTROUTEA |= _uart_mux; + else + PORTMUX.USARTROUTEA &= ~_uart_mux; + + // Set pin state for default UART pins + pinMode(_hwserial_rx_pin, INPUT_PULLUP); + digitalWrite(_hwserial_tx_pin, HIGH); + pinMode(_hwserial_tx_pin, OUTPUT); + + // Set previous pins to high Z + pinMode(_hwserial_rx_pin_swap, INPUT); + pinMode(_hwserial_tx_pin_swap, INPUT); + } +} + void UartClass::end() { // wait for transmission of outgoing data diff --git a/avr/cores/arduino/UART.h b/avr/cores/arduino/UART.h index ef966e9..4ae9c06 100644 --- a/avr/cores/arduino/UART.h +++ b/avr/cores/arduino/UART.h @@ -127,8 +127,11 @@ class UartClass : public HardwareSerial volatile uint8_t const _hwserial_rx_pin; volatile uint8_t const _hwserial_tx_pin; + volatile uint8_t const _hwserial_rx_pin_swap; + volatile uint8_t const _hwserial_tx_pin_swap; volatile uint8_t const _uart_mux; + volatile uint8_t const _uart_mux_swap; // Has any byte been written to the UART since begin() bool _written; @@ -149,9 +152,10 @@ class UartClass : public HardwareSerial unsigned char _tx_buffer[SERIAL_TX_BUFFER_SIZE]; public: - inline UartClass(volatile USART_t *hwserial_module, uint8_t hwserial_rx_pin, uint8_t hwserial_tx_pin, uint8_t dre_vect_num, uint8_t uart_mux); + inline UartClass(volatile USART_t *hwserial_module, uint8_t hwserial_rx_pin, uint8_t hwserial_tx_pin, uint8_t hwserial_rx_pin_swap, uint8_t hwserial_tx_pin_swap, uint8_t dre_vect_num, uint8_t uart_mux, uint8_t uart_mux_swap); void begin(unsigned long baud) { begin(baud, SERIAL_8N1); } void begin(unsigned long, uint16_t); + void swap(uint8_t shouldSwap = true); void end(); virtual int available(void); virtual int peek(void); diff --git a/avr/cores/arduino/UART0.cpp b/avr/cores/arduino/UART0.cpp index b8f1f34..2db3d28 100644 --- a/avr/cores/arduino/UART0.cpp +++ b/avr/cores/arduino/UART0.cpp @@ -55,7 +55,7 @@ ISR(HWSERIAL0_DRE_VECTOR) #endif #if defined(HWSERIAL0) - UartClass Serial(HWSERIAL0, PIN_WIRE_HWSERIAL0_RX, PIN_WIRE_HWSERIAL0_TX, HWSERIAL0_DRE_VECTOR_NUM, HWSERIAL0_MUX); + UartClass Serial(HWSERIAL0, PIN_WIRE_HWSERIAL0_RX, PIN_WIRE_HWSERIAL0_TX, PIN_WIRE_HWSERIAL0_RX_PINSWAP, PIN_WIRE_HWSERIAL0_TX_PINSWAP, HWSERIAL0_DRE_VECTOR_NUM, HWSERIAL0_MUX_DEFAULT, HWSERIAL0_MUX_PINSWAP); #endif // Function that can be weakly referenced by serialEventRun to prevent diff --git a/avr/cores/arduino/UART1.cpp b/avr/cores/arduino/UART1.cpp index aae79ca..ea43dd4 100644 --- a/avr/cores/arduino/UART1.cpp +++ b/avr/cores/arduino/UART1.cpp @@ -55,7 +55,7 @@ ISR(HWSERIAL1_DRE_VECTOR) #endif #if defined(HWSERIAL1) - UartClass Serial1(HWSERIAL1, PIN_WIRE_HWSERIAL1_RX, PIN_WIRE_HWSERIAL1_TX, HWSERIAL1_DRE_VECTOR_NUM, HWSERIAL1_MUX); + UartClass Serial1(HWSERIAL1, PIN_WIRE_HWSERIAL1_RX, PIN_WIRE_HWSERIAL1_TX, PIN_WIRE_HWSERIAL1_RX_PINSWAP, PIN_WIRE_HWSERIAL1_TX_PINSWAP, HWSERIAL1_DRE_VECTOR_NUM, HWSERIAL1_MUX_DEFAULT, HWSERIAL1_MUX_PINSWAP); #endif // Function that can be weakly referenced by serialEventRun to prevent diff --git a/avr/cores/arduino/UART2.cpp b/avr/cores/arduino/UART2.cpp index 671f534..4f0a72d 100644 --- a/avr/cores/arduino/UART2.cpp +++ b/avr/cores/arduino/UART2.cpp @@ -55,7 +55,7 @@ ISR(HWSERIAL2_DRE_VECTOR) #endif #if defined(HWSERIAL2) - UartClass Serial2(HWSERIAL2, PIN_WIRE_HWSERIAL2_RX, PIN_WIRE_HWSERIAL2_TX, HWSERIAL2_DRE_VECTOR_NUM, HWSERIAL2_MUX); + UartClass Serial2(HWSERIAL2, PIN_WIRE_HWSERIAL2_RX, PIN_WIRE_HWSERIAL2_TX, PIN_WIRE_HWSERIAL2_RX_PINSWAP, PIN_WIRE_HWSERIAL2_TX_PINSWAP, HWSERIAL2_DRE_VECTOR_NUM, HWSERIAL2_MUX_DEFAULT, HWSERIAL2_MUX_PINSWAP); #endif // Function that can be weakly referenced by serialEventRun to prevent diff --git a/avr/cores/arduino/UART3.cpp b/avr/cores/arduino/UART3.cpp index c73a2c4..f6d69a8 100644 --- a/avr/cores/arduino/UART3.cpp +++ b/avr/cores/arduino/UART3.cpp @@ -55,7 +55,7 @@ ISR(HWSERIAL3_DRE_VECTOR) #endif #if defined(HWSERIAL3) - UartClass Serial3(HWSERIAL3, PIN_WIRE_HWSERIAL3_RX, PIN_WIRE_HWSERIAL3_TX, HWSERIAL3_DRE_VECTOR_NUM, HWSERIAL3_MUX); + UartClass Serial3(HWSERIAL3, PIN_WIRE_HWSERIAL3_RX, PIN_WIRE_HWSERIAL3_TX, PIN_WIRE_HWSERIAL3_RX_PINSWAP, PIN_WIRE_HWSERIAL3_TX_PINSWAP, HWSERIAL3_DRE_VECTOR_NUM, HWSERIAL3_MUX_DEFAULT, HWSERIAL3_MUX_PINSWAP); #endif // Function that can be weakly referenced by serialEventRun to prevent diff --git a/avr/cores/arduino/UART_private.h b/avr/cores/arduino/UART_private.h index 0bca2d3..a7b6664 100644 --- a/avr/cores/arduino/UART_private.h +++ b/avr/cores/arduino/UART_private.h @@ -33,12 +33,18 @@ UartClass::UartClass( volatile USART_t *hwserial_module, volatile uint8_t hwserial_rx_pin, volatile uint8_t hwserial_tx_pin, + volatile uint8_t hwserial_rx_pin_swap, + volatile uint8_t hwserial_tx_pin_swap, volatile uint8_t hwserial_dre_interrupt_vect_num, - volatile uint8_t uart_mux) : + volatile uint8_t uart_mux, + volatile uint8_t uart_mux_swap) : _hwserial_module(hwserial_module), _hwserial_rx_pin(hwserial_rx_pin), _hwserial_tx_pin(hwserial_tx_pin), + _hwserial_rx_pin_swap(hwserial_rx_pin_swap), + _hwserial_tx_pin_swap(hwserial_tx_pin_swap), _uart_mux(uart_mux), + _uart_mux_swap(uart_mux_swap), _rx_buffer_head(0), _rx_buffer_tail(0), _tx_buffer_head(0), _tx_buffer_tail(0), _hwserial_dre_interrupt_vect_num(hwserial_dre_interrupt_vect_num), diff --git a/avr/variants/uno_wifi/pins_arduino.h b/avr/variants/uno_wifi/pins_arduino.h index 0d4676e..682165b 100644 --- a/avr/variants/uno_wifi/pins_arduino.h +++ b/avr/variants/uno_wifi/pins_arduino.h @@ -26,6 +26,9 @@ #include #include "timers.h" +#define UNO_WIFI_REV2_PINOUT +#define UNO2018_PINOUT + #define NUM_DIGITAL_PINS 20 // (14 on digital headers + 6 on analog headers) #define NUM_ANALOG_INPUTS 6 #define NUM_RESERVED_PINS 6 // (TOSC1/2, VREF, RESET, DEBUG USART Rx/Tx) @@ -65,9 +68,12 @@ static const uint8_t SCL = PIN_WIRE_SCL; #define HWSERIAL1_DRE_VECTOR (USART1_DRE_vect) #define HWSERIAL1_DRE_VECTOR_NUM (USART1_DRE_vect_num) #define HWSERIAL1_RXC_VECTOR (USART1_RXC_vect) -#define HWSERIAL1_MUX (PORTMUX_USART1_ALT1_gc) +#define HWSERIAL1_MUX_DEFAULT (PORTMUX_USART1_ALT1_gc) +#define HWSERIAL1_MUX_PINSWAP (PORTMUX_USART1_DEFAULT_gc) #define PIN_WIRE_HWSERIAL1_RX (0) #define PIN_WIRE_HWSERIAL1_TX (1) +#define PIN_WIRE_HWSERIAL1_RX_PINSWAP (33) +#define PIN_WIRE_HWSERIAL1_TX_PINSWAP (32) // Uno2 Debug USART (not available on headers, only via the EDGB virtual COM port) // USART3 on mega4809 (alternative pins) @@ -76,9 +82,12 @@ static const uint8_t SCL = PIN_WIRE_SCL; #define HWSERIAL0_DRE_VECTOR (USART3_DRE_vect) #define HWSERIAL0_DRE_VECTOR_NUM (USART3_DRE_vect_num) #define HWSERIAL0_RXC_VECTOR (USART3_RXC_vect) -#define HWSERIAL0_MUX (PORTMUX_USART3_ALT1_gc) +#define HWSERIAL0_MUX_DEFAULT (PORTMUX_USART3_ALT1_gc) +#define HWSERIAL0_MUX_PINSWAP (PORTMUX_USART3_DEFAULT_gc) #define PIN_WIRE_HWSERIAL0_RX (26) #define PIN_WIRE_HWSERIAL0_TX (27) +#define PIN_WIRE_HWSERIAL0_RX_PINSWAP (10) +#define PIN_WIRE_HWSERIAL0_TX_PINSWAP (9) // Uno2 Spare USART available on testpoints // USART0 on mega4809 (alternative pins) @@ -87,11 +96,14 @@ static const uint8_t SCL = PIN_WIRE_SCL; #define HWSERIAL2_DRE_VECTOR (USART0_DRE_vect) #define HWSERIAL2_DRE_VECTOR_NUM (USART0_DRE_vect_num) #define HWSERIAL2_RXC_VECTOR (USART0_RXC_vect) -#define HWSERIAL2_MUX (PORTMUX_USART0_ALT1_gc) +#define HWSERIAL2_MUX_DEFAULT (PORTMUX_USART0_ALT1_gc) +#define HWSERIAL2_MUX_PINSWAP (PORTMUX_USART0_DEFAULT_gc) #define PIN_WIRE_HWSERIAL2_RX (23) #define PIN_WIRE_HWSERIAL2_TX (24) +#define PIN_WIRE_HWSERIAL2_RX_PINSWAP (7) +#define PIN_WIRE_HWSERIAL2_TX_PINSWAP (2) -#define HWSERIAL3_MUX (PORTMUX_USART2_NONE_gc) +#define HWSERIAL3_MUX_DEFAULT (PORTMUX_USART2_NONE_gc) #define TWI_MUX (PORTMUX_TWI0_DEFAULT_gc) //PORTMUX_TWI0_ALT1_gc #define MUX_SPI (SPI_MUX)