Skip to content

Commit 0a43527

Browse files
Fix multicore SoftwareSerial IRQ handling (#3336)
The _handleIRQ callback for SerialPIO would be called for both cores if both had SerialPIO objects running. This would cause both cores to execute reads and queue updates, in parallel, at the same time, from the same PIO. This is a very bad thing and causes random data corruption. Now, store the core we started on and only handleIRQ if the current core matches the one we're running on.
1 parent 90fe158 commit 0a43527

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

cores/rp2040/SerialPIO.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static void __not_in_flash_func(_fifoIRQ)() {
9090
}
9191

9292
void __not_in_flash_func(SerialPIO::_handleIRQ)() {
93-
if (_rx == NOPIN) {
93+
if ((_rx == NOPIN) || (_onCore != get_core_num())) {
9494
return;
9595
}
9696
while (!pio_sm_is_rx_fifo_empty(_rxPIO, _rxSM)) {
@@ -149,6 +149,7 @@ static int pio_irq_0(PIO p) {
149149
}
150150

151151
void SerialPIO::begin(unsigned long baud, uint16_t config) {
152+
_onCore = get_core_num();
152153
_overflow = false;
153154
_baud = baud;
154155
switch (config & SERIAL_PARITY_MASK) {

cores/rp2040/SerialPIO.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ class SerialPIO : public arduino::HardwareSerial {
9393

9494
LocklessQueue<uint8_t> *_queue;
9595
size_t _fifoSize;
96+
97+
uint _onCore;
9698
};
9799

98100
#ifdef ARDUINO_NANO_RP2040_CONNECT

0 commit comments

Comments
 (0)