From 937cc51c693241cf6b7450ef5c61287bf73ee30e Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Tue, 4 Jun 2013 11:04:30 +0200 Subject: [PATCH] Clear SoftwareSerial rx delay if no interrupt register is found Before enabling interupts, begin would see if the given receive pin actually has an associated PCINT register. If not, the interrupts would not be enabled. Now, the same check is done, but when no register is available, the rx parameters are not loaded at all (which in turn prevents the interrupt from being enabled). This allows all code to use the same "is rx enabled" (which will be added next). --- .../libraries/SoftwareSerial/SoftwareSerial.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/hardware/arduino/avr/libraries/SoftwareSerial/SoftwareSerial.cpp b/hardware/arduino/avr/libraries/SoftwareSerial/SoftwareSerial.cpp index d1f6c9256..8476deb8a 100644 --- a/hardware/arduino/avr/libraries/SoftwareSerial/SoftwareSerial.cpp +++ b/hardware/arduino/avr/libraries/SoftwareSerial/SoftwareSerial.cpp @@ -385,9 +385,13 @@ void SoftwareSerial::begin(long speed) long baud = pgm_read_dword(&table[i].baud); if (baud == speed) { - _rx_delay_centering = pgm_read_word(&table[i].rx_delay_centering); - _rx_delay_intrabit = pgm_read_word(&table[i].rx_delay_intrabit); - _rx_delay_stopbit = pgm_read_word(&table[i].rx_delay_stopbit); + if (digitalPinToPCICR(_receivePin)) + { + // Only setup rx when we have a valid PCINT for this pin + _rx_delay_centering = pgm_read_word(&table[i].rx_delay_centering); + _rx_delay_intrabit = pgm_read_word(&table[i].rx_delay_intrabit); + _rx_delay_stopbit = pgm_read_word(&table[i].rx_delay_stopbit); + } _tx_delay = pgm_read_word(&table[i].tx_delay); break; } @@ -396,11 +400,8 @@ void SoftwareSerial::begin(long speed) // Set up RX interrupts, but only if we have a valid RX baud rate if (_rx_delay_stopbit) { - if (digitalPinToPCICR(_receivePin)) - { - *digitalPinToPCICR(_receivePin) |= _BV(digitalPinToPCICRbit(_receivePin)); - *digitalPinToPCMSK(_receivePin) |= _BV(digitalPinToPCMSKbit(_receivePin)); - } + *digitalPinToPCICR(_receivePin) |= _BV(digitalPinToPCICRbit(_receivePin)); + *digitalPinToPCMSK(_receivePin) |= _BV(digitalPinToPCMSKbit(_receivePin)); tunedDelay(_tx_delay); // if we were low this establishes the end }