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.
This commit is contained in:
Matthijs Kooijman 2014-04-23 19:16:21 +02:00
parent 5f2f0ef4c8
commit 08c3bfdc9f
2 changed files with 8 additions and 1 deletions

View File

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

View File

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