Disable hwSerial library at compile-time

Hi All

Is there a way to set a compiler flag to disable serial ports ?

Suppose i want to KEEP Serial0, but i want to make my own serial interrupt for S1, S2, and S3 Or have my own serial interrupt for S0, but use S1 as a debug port, with serial libray active

At different times it will be Mega2560, uno, w 168, or 328, or maybe even my own board with a 644 processor. So, it will be 'random' combinations of serial libray active at different projects.

kind regards

Create your own board variant(s) which only defines the serial ports you want the hardwareSerial to handle.

You will have to check, but I think ISR's using 'ISR()' should have the 'weak' attribute set by default. If its not set, you could add 'attribute((weak))' to the core versions, and implement your own versions in the sketch.

This might not work, but is how the AVR core defines its interrupts so they all point to the bad_vector ISR, unless you provide your own version.

So far i have resolved it by editing hardwareserial.cpp it has the drawback, that i MUST remember to use the proper hardwareserial and copy the correct one into the libfolder

majenko: what about board variants - can i set it in boards.txt ? what will happen if i need s2, and s3 by hardwareserial - but s0 and s1 must be controlled by my own interrupt ?

pYro_65 I would rather have some kind of /#define disable_serial0 ... kind of thing

This does work, even without any ‘disable_serial0’

applying weak to the HWserial cpp I can now use the code below. Instead of the serial implementation, sending data will toggle the pin 13 led.

    volatile unsigned char c = UDR0;
    digitalWrite( 13, !digitalRead( 13 ) );

void setup(){
  pinMode( 13, OUTPUT );
void loop(){}

Just to make sure i have it right :

in hardwareserial.cpp change


repeat for S1,S2, and S3

Will allow me to override all built-in serial handling, when i want to ?

or did i get it completely wrong ? as a non-professional c-programmer, i am not totally conversant in the finer nuances of the language ;-)

It's a hint to the linker, but it sounds to my like pYro_65 is giving you good advice. Try it and see.

Yes Yes My comment on being conversant was a hint that i need to know what exactly to put in

Hmm, in 1.0.5 they used ISR(), but 1.5.1 uses SIGNAL() which is deprecated.

All I added above the isr definition was this ( weak must be on declaration, not definition ).

  ISR(USART_RX_vect) __attribute__((weak));

I just tried 1.5.2 with the SIGNAL macro and it worked fine too. It will use the default implementation if you do not define your own version.

Clearly i am doing something wrong

my code:

void USART_setup()
  // USART1 initialization
  // USART1 Communication Parameters: 8 Data, 1 Stop, No Parity
  // USART1 Receiver: On
  // USART1 Transmitter: On
  // USART1 Mode: Asynchronous
  // USART1 Baud Rate: 38400 (16 MHz X-Tal)
  // USART1 RX interrupt enable

  ReceivedByte = UDR1;    // read byte from rx register
  if (ReceivedByte >=0 && ReceivedByte <=14 ) {USART_TX(ReceivedByte); }
  g_mpx_led_state = !g_mpx_led_state;
  rx_enable(); // enable serial interrupt

static void rx_enable()
  byte t;
  t=UDR1;t=UDR1;  // empty buffer
  sbi(UCSR1B,RXEN1);  // enable RX
  sbi(UCSR1A,RXC1);   // clear RX interrupt
  sbi(UCSR1B,RXCIE1); // enable RX interrupts

static void rx_disable()

my hardwareserial.cpp - and i haven’t changed anything
It might be wise to mention that it is in sanguino/core-tree, and my compiler are 1.0.5

#if defined(USART1_RX_vect)
  void serialEvent1() __attribute__((weak));
  void serialEvent1() {}
  #define serialEvent1_implemented
    unsigned char c = UDR1;
    store_char(c, &rx_buffer1);
#elif defined(SIG_USART1_RECV)
  #error SIG_USART1_RECV