SlashDevin / NeoSWSerial

Efficient alternative to SoftwareSerial with attachInterrupt for RX chars, simultaneous RX & TX
173 stars 42 forks source link

NeoSWSerial with PinChangeInterrupt #31

Open geologic opened 5 years ago

geologic commented 5 years ago

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?

SRGDamia1 commented 5 years ago

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.

geologic commented 5 years ago

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'

SRGDamia1 commented 5 years ago

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);

geologic commented 5 years ago

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'

albi90 commented 4 years ago

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?

geologic commented 4 years ago

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.