You can create an instance of HardwareSerial
referencing a hardware
port that depends on a function parameter, as in
void initPort(uint8_t port) {
switch (port) {
case 0: {
HardwareSerial serial(&UBRR0H, &UBRR0L,
&UCSR0A, &UCSR0B, &UCSR0C, &UDR0);
serial.begin(9600);
break;
}
case 1:
// etc...
}
}
However, since only a local variable can be created in this manner, it
is not of much use. Ideally, you would want to have a global variable
for the serial port, and initialize it in initPort()
, as
HardwareSerial serial;
void initPort(uint8_t port) {
switch (port) {
case 0:
// Invoke fictional init() method.
serial.init(&UBRR0H, &UBRR0L,
&UCSR0A, &UCSR0B, &UCSR0C, &UDR0);
break;
case 1:
// etc...
}
serial.begin(9600);
}
Alas, this is not possible, as the HardwareSerial
class has no default
constructor. This means that binding the object to the hardware port can
only be done when it is constructed.
Short of modifying the Arduino core (adding a default constructor and an
init()
method), the only option I can see is to have the global
variable be a pointer. Then, in initPort()
the object is
instantiated while initializing the pointer. I tried this approach in
the example program below. There was a slight technical difficulty
though: since the object files from the core defining Serial
,
Serail1
, etc. will hopefully not be linked to the program, the
interrupt service routines defined therein must be also defined in the
sketch itself. And since an ISR cannot be bound to an interrupt at run
time, the ISRs have to be defined for all the ports, even though only
one ends up being actually used.
Here is my test program:
#include <HardwareSerial_private.h>
HardwareSerial *serial;
ISR(USART0_RX_vect) { serial->_rx_complete_irq(); }
ISR(USART1_RX_vect, ISR_ALIASOF(USART0_RX_vect));
ISR(USART2_RX_vect, ISR_ALIASOF(USART0_RX_vect));
ISR(USART3_RX_vect, ISR_ALIASOF(USART0_RX_vect));
ISR(USART0_UDRE_vect) { serial->_tx_udr_empty_irq(); }
ISR(USART1_UDRE_vect, ISR_ALIASOF(USART0_UDRE_vect));
ISR(USART2_UDRE_vect, ISR_ALIASOF(USART0_UDRE_vect));
ISR(USART3_UDRE_vect, ISR_ALIASOF(USART0_UDRE_vect));
void initPort(uint8_t port)
{
switch (port) {
case 0:
serial = new HardwareSerial(&UBRR0H, &UBRR0L,
&UCSR0A, &UCSR0B, &UCSR0C, &UDR0);
break;
case 1:
serial = new HardwareSerial(&UBRR1H, &UBRR1L,
&UCSR1A, &UCSR1B, &UCSR1C, &UDR1);
break;
case 2:
serial = new HardwareSerial(&UBRR2H, &UBRR2L,
&UCSR2A, &UCSR2B, &UCSR2C, &UDR2);
break;
case 3:
serial = new HardwareSerial(&UBRR3H, &UBRR3L,
&UCSR3A, &UCSR3B, &UCSR3C, &UDR3);
break;
}
serial->begin(9600);
}
void setup() {
initPort(0);
serial->println("Hello, World!");
}
void loop(){}
It should be noted that the ISRs do not take that mush flash space.
There are actually only two of them, even though each is referenced four
times in the interrupt vector table.
HardwareSerial Serial(&UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, &UDR);