Interrupt conflict with SoftwareSerial

Hi,
I have the following code in a project:

ISR (PORTE_PORT_vect)
{
  if ( PORTE.INTFLAGS & PIN3_bm ) {     
  
...

    PORTE.INTFLAGS &= PIN3_bm ;                 //reset flag bit in register
  }
}

ISR (PORTB_PORT_vect)
{
  if ( PORTB.INTFLAGS & PIN1_bm ) {
    
...

    PORTB.INTFLAGS &= PIN1_bm ;
  }
}

ISR (PORTD_PORT_vect)
{
  if ( PORTD.INTFLAGS & PIN3_bm ) {
   
...
    
    PORTD.INTFLAGS &= PIN3_bm ;
  }
}

It works perfectly. Now I need SoftwareSerial lib. When I include it, I get the following error:

WInterrupts.cpp.o (symbol from plugin): In function `attachInterruptParam':
(.text+0x0): multiple definition of `__vector_34'
sketch/rph_encoder.cpp.o (symbol from plugin):(.text+0x0): first defined here
WInterrupts.cpp.o (symbol from plugin): In function `attachInterruptParam':
(.text+0x0): multiple definition of `__vector_20'
sketch/rph_encoder.cpp.o (symbol from plugin):(.text+0x0): first defined here
WInterrupts.cpp.o (symbol from plugin): In function `attachInterruptParam':
(.text+0x0): multiple definition of `__vector_35'
sketch/rph_encoder.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
exit status 1
Erreur de compilation pour la carte Arduino Nano Every

Is it possible to solve that?

Looks like you're using a Mega. If that's the case, is there any reason why you don't use one of the other hardware serial ports (Serial1..Serial3)?

You can hack the SoftwareSerial library. Maybe one of the alternatives to SoftwareSerial will work

  1. GitHub - SlashDevin/NeoSWSerial: Efficient alternative to SoftwareSerial with attachInterrupt for RX chars, simultaneous RX & TX
  2. GitHub - PaulStoffregen/AltSoftSerial: Software emulated serial using hardware timers for improved compatibility

They might be in library manager.

I rather guess you're using an Arduino Nano Every but that model also supports up to 4 hardware serial ports. Please specify why those cannot be used but a bad software emulation should be used.

I would guess that the OP does not know about the additional hardware serial ports on the Nano Every.

right, sorry I forgot to mention...

I'm also trying to get hardware Serial working:

I use SoftwareSerial basically because Critters lib uses it. I wrote my own code, but the lib generates obviously the same error.
I can try to hack SoftwareSerial, but the error message mentions WInterrupts.cpp, and I don't know how to manage vectors directly. I guess ISR on ports B, D and E are declared by WInterrupts, so I should maybe put my if code directly there...

Again, why would you do any of that when there are perfectly good hardware UART options available to you?

again because the only lib available for my module is based on it and because hardware serial was not working. Now it does!! (I was using Serial instead of Serial1)
The other reason is to understand why a lib prevents the use of interrupts. I'm still interested to a solution, in case...

Post a GitHub link to that library.

Pretty simple, the same interrupt vector can't be defined more than once in the same code.

ooops, forgot to paste:

The lib doesn't. It simply tries to use an interrupt vector that has already been claimed. For completely obvious reasons that is not permitted.

Use hardware serial and the problem will not only go away, the code will work as well or better than the original, and even be smaller.

There's not much to that library. I'd rewrite it so that the class constructor accepts a reference to an object of the Stream class. Then you could pass it a HardwareSerial or SoftwareSerial object depending on the board in use. You could even then post a GitHub Pull request for the author to include that improvement.

MP3FLASH16P_RPH.cpp (2.7 KB)
MP3FLASH16P_RPH.h (838 Bytes)
I modded the lib to use only Serial1, and added a fadeOut() method. (initiate with fadeOutStart([duration in ms]), then put fadeOut() in the main loop).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.