rp2/rp2_pio: Make PIO IRQ handlers have lazy initialisation.
Some checks failed
JavaScript code lint and formatting with Biome / eslint (push) Has been cancelled
Check code formatting / code-formatting (push) Has been cancelled
Check code size / build (push) Has been cancelled
Check spelling with codespell / codespell (push) Has been cancelled
Check commit message formatting / build (push) Has been cancelled
Build docs / build (push) Has been cancelled
Check examples / embedding (push) Has been cancelled
Package mpremote / build (push) Has been cancelled
.mpy file format and tools / test (push) Has been cancelled
Build ports metadata / build (push) Has been cancelled
cc3200 port / build (push) Has been cancelled
esp32 port / build_idf (esp32_build_cmod_spiram_s2) (push) Has been cancelled
esp32 port / build_idf (esp32_build_s3_c3) (push) Has been cancelled
esp8266 port / build (push) Has been cancelled
mimxrt port / build (push) Has been cancelled
nrf port / build (push) Has been cancelled
powerpc port / build (push) Has been cancelled
qemu-arm port / build_and_test (push) Has been cancelled
qemu-riscv port / build_and_test (push) Has been cancelled
renesas-ra port / build_renesas_ra_board (push) Has been cancelled
rp2 port / build (push) Has been cancelled
samd port / build (push) Has been cancelled
stm32 port / build_stm32 (stm32_misc_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_nucleo_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_pyb_build) (push) Has been cancelled
unix port / minimal (push) Has been cancelled
unix port / reproducible (push) Has been cancelled
unix port / standard (push) Has been cancelled
unix port / standard_v2 (push) Has been cancelled
unix port / coverage (push) Has been cancelled
unix port / coverage_32bit (push) Has been cancelled
unix port / nanbox (push) Has been cancelled
unix port / float (push) Has been cancelled
unix port / stackless_clang (push) Has been cancelled
unix port / float_clang (push) Has been cancelled
unix port / settrace (push) Has been cancelled
unix port / settrace_stackless (push) Has been cancelled
unix port / macos (push) Has been cancelled
unix port / qemu_mips (push) Has been cancelled
unix port / qemu_arm (push) Has been cancelled
unix port / qemu_riscv64 (push) Has been cancelled
webassembly port / build (push) Has been cancelled
windows port / build-vs (Debug, x64, windows-2022, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Debug, x64, windows-latest, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, x86, windows-2022, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Debug, x86, windows-latest, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-2019, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-2019, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-2022, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-2022, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-latest, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-latest, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-2019, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-2019, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-2022, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-2022, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-latest, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-latest, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, dev) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, standard) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, dev) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, standard) (push) Has been cancelled
windows port / cross-build-on-linux (push) Has been cancelled
zephyr port / build (push) Has been cancelled
Python code lint and formatting with ruff / ruff (push) Has been cancelled

This change has no impact on vanilla MicroPython builds, but is intended to
avoid RP2's PIO implementation from trampling PIO usage in USER_C_MODULES.

This is consistent with PIOs tracking of used state machines and managed
programs, and makes working with PIO in USER_C_MODULES much less of an
uphill battle.

Since PIO deinit runs before gc_sweep_all it's impossible to work around
this wrinkle otherwise.  A module finalizer does not get the opportunity to
put the PIOs back into a state which wont crash rp2_pio_deinit.

Changes are:
- init: Avoid exclusive handlers being added to all PIOs and add them only
  when needed.
- deinit: Only remove handlers we have set.
- rp2_pio_irq: Add the exlusive handler if needed.
- rp2_state_machine_irq: Add the exclusive handler if needed.

Signed-off-by: Phil Howard <phil@gadgetoid.com>
This commit is contained in:
Phil Howard
2024-07-08 15:34:18 +01:00
committed by Damien George
parent e7ff0b8a31
commit ad3552ae2c

View File

@@ -104,6 +104,23 @@ static void pio1_irq0(void) {
pio_irq0(pio1);
}
// Returns the correct irq0 handler wrapper for a given pio
static inline irq_handler_t rp2_pio_get_irq_handler(PIO pio) {
return pio == pio0 ? pio0_irq0 : pio1_irq0;
}
// Add the irq0 handler if it's not added, and if no other handler is present
void rp2_pio_irq_set_exclusive_handler(PIO pio, uint irq) {
irq_handler_t current = irq_get_exclusive_handler(irq);
// If the IRQ is set and isn't our handler, or a shared handler is set, then raise an error
if ((current && current != rp2_pio_get_irq_handler(pio)) || irq_has_shared_handler(irq)) {
mp_raise_ValueError("irq claimed by external resource");
// If the IRQ is not set, add our handler
} else if (!current) {
irq_set_exclusive_handler(irq, rp2_pio_get_irq_handler(pio));
}
}
// Calls pio_add_program() and keeps track of used instruction memory.
static uint rp2_pio_add_managed_program(PIO pio, struct pio_program *pio_program) {
uint offset = pio_add_program(pio, pio_program);
@@ -143,15 +160,18 @@ void rp2_pio_init(void) {
// Set up interrupts.
memset(MP_STATE_PORT(rp2_pio_irq_obj), 0, sizeof(MP_STATE_PORT(rp2_pio_irq_obj)));
memset(MP_STATE_PORT(rp2_state_machine_irq_obj), 0, sizeof(MP_STATE_PORT(rp2_state_machine_irq_obj)));
irq_set_exclusive_handler(PIO0_IRQ_0, pio0_irq0);
irq_set_exclusive_handler(PIO1_IRQ_0, pio1_irq0);
}
void rp2_pio_deinit(void) {
// Disable and clear interrupts.
irq_set_mask_enabled((1u << PIO0_IRQ_0) | (1u << PIO1_IRQ_0), false);
irq_remove_handler(PIO0_IRQ_0, pio0_irq0);
irq_remove_handler(PIO1_IRQ_0, pio1_irq0);
if (irq_get_exclusive_handler(PIO0_IRQ_0) == pio0_irq0) {
irq_set_enabled(PIO0_IRQ_0, false);
irq_remove_handler(PIO0_IRQ_0, pio0_irq0);
}
if (irq_get_exclusive_handler(PIO1_IRQ_0) == pio1_irq0) {
irq_set_enabled(PIO1_IRQ_0, false);
irq_remove_handler(PIO1_IRQ_0, pio1_irq0);
}
rp2_state_machine_reset_all();
@@ -379,6 +399,7 @@ static mp_obj_t rp2_pio_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k
// Enable IRQ if a handler is given.
if (args[ARG_handler].u_obj != mp_const_none) {
rp2_pio_irq_set_exclusive_handler(self->pio, self->irq);
self->pio->inte0 = irq->trigger;
irq_set_enabled(self->irq, true);
}
@@ -872,6 +893,7 @@ static mp_obj_t rp2_state_machine_irq(size_t n_args, const mp_obj_t *pos_args, m
}
if (self->pio->inte0) {
rp2_pio_irq_set_exclusive_handler(self->pio, self->irq);
irq_set_enabled(self->irq, true);
}
}