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 +++++++ 1 file changed, 7 insertions(+) (limited to 'libraries/SoftwareSerial/SoftwareSerial.cpp') 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; -- cgit v1.2.3-18-g5258