From 22b559f8c09f0a28b01e1d2adb5aae42dae6e63d Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Tue, 24 May 2022 09:02:43 -0700 Subject: [PATCH] Deallocate resources on SerialPIO::end 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. --- cores/rp2040/SerialPIO.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/cores/rp2040/SerialPIO.cpp b/cores/rp2040/SerialPIO.cpp index 820eb2b0..ac15ca26 100644 --- a/cores/rp2040/SerialPIO.cpp +++ b/cores/rp2040/SerialPIO.cpp @@ -257,8 +257,23 @@ void SerialPIO::end() { if (!_running) { return; } - // TODO: Deallocate PIO resources, stop them - _pioSP[pio_get_index(_rxPIO)][_rxSM] = nullptr; + if (_tx != NOPIN) { + pio_sm_set_enabled(_txPIO, _txSM, false); + } + if (_rx != NOPIN) { + pio_sm_set_enabled(_rxPIO, _rxSM, false); + _pioSP[pio_get_index(_rxPIO)][_rxSM] = nullptr; + // If no more active, disable the IRQ + auto pioNum = pio_get_index(_rxPIO); + bool used = false; + for (int i = 0; i < 4; i++) { + used = used || !!_pioSP[pioNum][i]; + } + if (!used) { + auto irqno = pioNum == 0 ? PIO0_IRQ_0 : PIO1_IRQ_0; + irq_set_enabled(irqno, false); + } + } _running = false; } -- 2.54.0