NeoSWSerial icon indicating copy to clipboard operation
NeoSWSerial copied to clipboard

NeoSWSerial with PinChangeInterrupt

Open geologic opened this issue 6 years ago • 6 comments
trafficstars

Hi

I'm using your library to talk to a GSM modem:

NeoSWSerial modemSerial (GSM_TXPIN,GSM_RXPIN); // Pins 7,8

I also use avr/wdt.h for watchdog functions, and 2 external interrupts (pin d2 and d3). Everything is working ok.

I need to check a 3rd pin connected to a pushbutton, so i made a new sketch just with PinChangeInterrupt library where i check for a button press on XTAL2 pin (i don't use xtal):

attachPCINT( 7, gotPressP1, FALLING); // Check PCINT=7 -> PB7 = XTAL2

That also works ok. When i try to merge everything, i got linking errors, so i try to disable NeoSWSerial ISRs: on .h file i uncomented

#define NEOSWSERIAL_EXTERNAL_PCINT // uncomment to use your own PCINT ISRs

and added ISR to manage RX:

attachPCINT(7, gotPressP1, FALLING);
attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(GSM_RXPIN), ISRrx, CHANGE);

void ISRrx() {
  NeoSWSerial::rxISR( *portInputRegister( digitalPinToPort( GSM_RXPIN ) ) );
}

Now it compiles ok but my board is always reeboting, and i can see it is a watchdog reset.

Any idea why?

geologic avatar Feb 01 '19 19:02 geologic

The problem is with using the XTAL pin as input, nothing caused by this library. I strongly suspect that you have to do some tweaking to pins_arduino.h before that would work.

Otherwise this library works very nicely when you attach the pin change interrupts with some other library, just like you're already doing.

SRGDamia1 avatar Feb 01 '19 23:02 SRGDamia1

Well, i'm prety sure the problem is not XTAL pin as input, as i have a standalone working sketch using that pin and PinChangeInterrupt library.

If i try to use EnableInterrupt instead of PinChangeInterrupt i got compile errors: multiple definition of __vector_1' and multiple definition of __vector_2'

geologic avatar Feb 04 '19 12:02 geologic

I've never used PinChangeInterrupt, but I have NeoSWSerial working well with EnableInterrupt. EnableInterrupt won't use the XTAL pin though. Do you really need that pin? Have you tried with another?

All I did to get it working with EnableInterrupt: Un-comment the line: (or compile with build flag -DNEOSWSERIAL_EXTERNAL_PCINT) #define NEOSWSERIAL_EXTERNAL_PCINT // uncomment to use your own PCINT ISRs

Created an ISR helper fxn:

void neoSSerial1ISR()
{
    NeoSWSerial::rxISR(*portInputRegister(digitalPinToPort(neoSSerial1Rx)));
}

And called the ISR in my setup fxn: enableInterrupt(neoSSerial1Rx, neoSSerial1ISR, CHANGE);

SRGDamia1 avatar Feb 04 '19 16:02 SRGDamia1

After trial and error i manage to put everything together... My original code works ok with NeoSWSerial and a GSM modem. Adding PinChangeInterrupt works ok and my pin responds to events, but GSM no longer work. The problem seems to be in ISR registration, since GSM no longer receive or send AT commands.

So, i made another try: used my original code with EnableInterrupt library (instead of PinChangeInterrupt), and done the suggested changes to ISR function, but code does not compile: multiple definition of '__vector_1' and multiple definition of '__vector_2'

geologic avatar Feb 05 '19 11:02 geologic

After trial and error i manage to put everything together... My original code works ok with NeoSWSerial and a GSM modem. Adding PinChangeInterrupt works ok and my pin responds to events, but GSM no longer work. The problem seems to be in ISR registration, since GSM no longer receive or send AT commands.

So, i made another try: used my original code with EnableInterrupt library (instead of PinChangeInterrupt), and done the suggested changes to ISR function, but code does not compile: multiple definition of '__vector_1' and multiple definition of '__vector_2'

Im also seeing a similar issue with a Lora Chip & NeoSWSerial + a pin change interrupt the interrupt fires then i loose connection to my Lora chip (using pins 8 and 9 so not XTAL) did you work out the issue?

albi90 avatar Feb 02 '20 12:02 albi90

Im also seeing a similar issue with a Lora Chip & NeoSWSerial + a pin change interrupt the interrupt fires then i loose connection to my Lora chip (using pins 8 and 9 so not XTAL) did you work out the issue?

No, i give up using NeoSWSerial and changed my 328P for the 328PB version, since 328PB has 2 UARTS. Careful, 328P and 328PB are not pin compatible, you have to check your design.

geologic avatar Feb 05 '20 10:02 geologic