Skip to content

Commit 5f2f0ef

Browse files
Optimize SoftwareSerial::setRxIntMsk()
This precalculates the mask register and value, making setRxIntMask considerably less complicated. Right now, this is not a big deal, but simplifying it allows using it inside the ISR next.
1 parent b6ba4b6 commit 5f2f0ef

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

Diff for: libraries/SoftwareSerial/SoftwareSerial.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,11 @@ void SoftwareSerial::begin(long speed)
403403
// (others might also need it, so we disable the interrupt by using
404404
// the per-pin PCMSK register).
405405
*digitalPinToPCICR(_receivePin) |= _BV(digitalPinToPCICRbit(_receivePin));
406+
// Precalculate the pcint mask register and value, so setRxIntMask
407+
// can be used inside the ISR without costing too much time.
408+
_pcint_maskreg = digitalPinToPCMSK(_receivePin);
409+
_pcint_maskvalue = _BV(digitalPinToPCMSKbit(_receivePin));
410+
406411
tunedDelay(_tx_delay); // if we were low this establishes the end
407412
}
408413

@@ -417,9 +422,9 @@ void SoftwareSerial::begin(long speed)
417422
void SoftwareSerial::setRxIntMsk(bool enable)
418423
{
419424
if (enable)
420-
*digitalPinToPCMSK(_receivePin) |= _BV(digitalPinToPCMSKbit(_receivePin));
425+
*_pcint_maskreg |= _pcint_maskvalue;
421426
else
422-
*digitalPinToPCMSK(_receivePin) &= ~_BV(digitalPinToPCMSKbit(_receivePin));
427+
*_pcint_maskreg &= ~_pcint_maskvalue;
423428
}
424429

425430
void SoftwareSerial::end()

Diff for: libraries/SoftwareSerial/SoftwareSerial.h

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class SoftwareSerial : public Stream
5353
volatile uint8_t *_receivePortRegister;
5454
uint8_t _transmitBitMask;
5555
volatile uint8_t *_transmitPortRegister;
56+
volatile uint8_t *_pcint_maskreg;
57+
uint8_t _pcint_maskvalue;
5658

5759
uint16_t _rx_delay_centering;
5860
uint16_t _rx_delay_intrabit;

0 commit comments

Comments
 (0)