Skip to content

Commit 9e659b0

Browse files
Deallocate resources on SerialPIO::end (#596)
Fixes #593 When SerialPIO::end is called, stop the PIO SMs and potentially disable the IRQ handler if this is the last RX port on that PIO.
1 parent 7d30e6d commit 9e659b0

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

cores/rp2040/SerialPIO.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,23 @@ void SerialPIO::end() {
257257
if (!_running) {
258258
return;
259259
}
260-
// TODO: Deallocate PIO resources, stop them
261-
_pioSP[pio_get_index(_rxPIO)][_rxSM] = nullptr;
260+
if (_tx != NOPIN) {
261+
pio_sm_set_enabled(_txPIO, _txSM, false);
262+
}
263+
if (_rx != NOPIN) {
264+
pio_sm_set_enabled(_rxPIO, _rxSM, false);
265+
_pioSP[pio_get_index(_rxPIO)][_rxSM] = nullptr;
266+
// If no more active, disable the IRQ
267+
auto pioNum = pio_get_index(_rxPIO);
268+
bool used = false;
269+
for (int i = 0; i < 4; i++) {
270+
used = used || !!_pioSP[pioNum][i];
271+
}
272+
if (!used) {
273+
auto irqno = pioNum == 0 ? PIO0_IRQ_0 : PIO1_IRQ_0;
274+
irq_set_enabled(irqno, false);
275+
}
276+
}
262277
_running = false;
263278
}
264279

0 commit comments

Comments
 (0)