Skip to content

Commit

Permalink
Add Serial.swap
Browse files Browse the repository at this point in the history
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
  • Loading branch information
MCUdude committed Feb 23, 2019
1 parent 8b114b6 commit 72c52f0
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 11 deletions.
39 changes: 38 additions & 1 deletion avr/cores/arduino/UART.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion avr/cores/arduino/UART.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion avr/cores/arduino/UART0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion avr/cores/arduino/UART1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion avr/cores/arduino/UART2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion avr/cores/arduino/UART3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion avr/cores/arduino/UART_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
20 changes: 16 additions & 4 deletions avr/variants/uno_wifi/pins_arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
#include <avr/pgmspace.h>
#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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 72c52f0

Please sign in to comment.