From 08c3bfdc9fd5da81932ec9e4df0b645c93f2ebdc Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Wed, 23 Apr 2014 19:16:21 +0200 Subject: Disable the RX PCINT inside SoftwareSerial::recv Before, the interrupt would remain enabled during reception, which would re-set the PCINT flag because of the level changes inside the received byte. Because interrupts are globally disabled, this would not immediately trigger an interrupt, but the flag would be remembered to trigger another PCINT interrupt immediately after the first one is processed. Typically this was not a problem, because the second interrupt would see the stop bit, or an idle line, and decide that the interrupt triggered for someone else. However, at high baud rates, this could cause the next interrupt for the real start bit to be delayed so much that the byte got corrupted. By clearing the interrupt mask bit for just the RX pin (as opposed to the PCINT mask bit for the entire port), any PCINT events on other bits can still set the PCINT flag and be processed as normal. In this case, it's likely that there will be corruption, but that's inevitable when (other) interrupts happen during SoftwareSerial reception. --- libraries/SoftwareSerial/SoftwareSerial.cpp | 7 +++++++ libraries/SoftwareSerial/SoftwareSerial.h | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) (limited to 'libraries/SoftwareSerial') diff --git a/libraries/SoftwareSerial/SoftwareSerial.cpp b/libraries/SoftwareSerial/SoftwareSerial.cpp index bee1107..d1d2008 100644 --- a/libraries/SoftwareSerial/SoftwareSerial.cpp +++ b/libraries/SoftwareSerial/SoftwareSerial.cpp @@ -237,6 +237,11 @@ void SoftwareSerial::recv() // so interrupt is probably not for us if (_inverse_logic ? rx_pin_read() : !rx_pin_read()) { + // Disable further interrupts during reception, this prevents + // triggering another interrupt directly after we return, which can + // cause problems at higher baudrates. + setRxIntMsk(false); + // Wait approximately 1/2 of a bit width to "center" the sample tunedDelay(_rx_delay_centering); DebugPulse(_DEBUG_PIN2, 1); @@ -255,6 +260,8 @@ void SoftwareSerial::recv() tunedDelay(_rx_delay_stopbit); DebugPulse(_DEBUG_PIN2, 1); + // Re-enable interrupts when we're sure to be inside the stop bit + setRxIntMsk(true); if (_inverse_logic) d = ~d; diff --git a/libraries/SoftwareSerial/SoftwareSerial.h b/libraries/SoftwareSerial/SoftwareSerial.h index 302a23b..2b12598 100644 --- a/libraries/SoftwareSerial/SoftwareSerial.h +++ b/libraries/SoftwareSerial/SoftwareSerial.h @@ -76,7 +76,7 @@ private: void tx_pin_write(uint8_t pin_state) __attribute__((__always_inline__)); void setTX(uint8_t transmitPin); void setRX(uint8_t receivePin); - void setRxIntMsk(bool enable); + void setRxIntMsk(bool enable) __attribute__((__always_inline__)); // private static method for timing static inline void tunedDelay(uint16_t delay); -- cgit v1.2.3-18-g5258