Skip to content

Commit cb36793

Browse files
Toggle SoftwareSerial interrupts when starting / stopping to listen
This prevents interrupts from triggering when the SoftwareSerial instance is not even listening. Additionally, this removes the need to disable interrupts in SoftwareSerial::listen, since no interrupts are active while it touches the variables.
1 parent 3f02bcb commit cb36793

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

libraries/SoftwareSerial/SoftwareSerial.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,14 @@ bool SoftwareSerial::listen()
183183

184184
if (active_object != this)
185185
{
186+
if (active_object)
187+
active_object->stopListening();
188+
186189
_buffer_overflow = false;
187-
uint8_t oldSREG = SREG;
188-
cli();
189190
_receive_buffer_head = _receive_buffer_tail = 0;
190191
active_object = this;
191-
SREG = oldSREG;
192+
193+
setRxIntMsk(true);
192194
return true;
193195
}
194196

@@ -200,6 +202,7 @@ bool SoftwareSerial::stopListening()
200202
{
201203
if (active_object == this)
202204
{
205+
setRxIntMsk(false);
203206
active_object = NULL;
204207
return true;
205208
}
@@ -418,7 +421,6 @@ void SoftwareSerial::begin(long speed)
418421
// (others might also need it, so we disable the interrupt by using
419422
// the per-pin PCMSK register).
420423
*digitalPinToPCICR(_receivePin) |= _BV(digitalPinToPCICRbit(_receivePin));
421-
setRxIntMsk(true);
422424
tunedDelay(_tx_delay); // if we were low this establishes the end
423425
}
424426

0 commit comments

Comments
 (0)