Skip to content

Commit ad3552a

Browse files
Gadgetoiddpgeorge
authored andcommitted
rp2/rp2_pio: Make PIO IRQ handlers have lazy initialisation.
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>
1 parent e7ff0b8 commit ad3552a

1 file changed

Lines changed: 27 additions & 5 deletions

File tree

ports/rp2/rp2_pio.c

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,23 @@ static void pio1_irq0(void) {
104104
pio_irq0(pio1);
105105
}
106106

107+
// Returns the correct irq0 handler wrapper for a given pio
108+
static inline irq_handler_t rp2_pio_get_irq_handler(PIO pio) {
109+
return pio == pio0 ? pio0_irq0 : pio1_irq0;
110+
}
111+
112+
// Add the irq0 handler if it's not added, and if no other handler is present
113+
void rp2_pio_irq_set_exclusive_handler(PIO pio, uint irq) {
114+
irq_handler_t current = irq_get_exclusive_handler(irq);
115+
// If the IRQ is set and isn't our handler, or a shared handler is set, then raise an error
116+
if ((current && current != rp2_pio_get_irq_handler(pio)) || irq_has_shared_handler(irq)) {
117+
mp_raise_ValueError("irq claimed by external resource");
118+
// If the IRQ is not set, add our handler
119+
} else if (!current) {
120+
irq_set_exclusive_handler(irq, rp2_pio_get_irq_handler(pio));
121+
}
122+
}
123+
107124
// Calls pio_add_program() and keeps track of used instruction memory.
108125
static uint rp2_pio_add_managed_program(PIO pio, struct pio_program *pio_program) {
109126
uint offset = pio_add_program(pio, pio_program);
@@ -143,15 +160,18 @@ void rp2_pio_init(void) {
143160
// Set up interrupts.
144161
memset(MP_STATE_PORT(rp2_pio_irq_obj), 0, sizeof(MP_STATE_PORT(rp2_pio_irq_obj)));
145162
memset(MP_STATE_PORT(rp2_state_machine_irq_obj), 0, sizeof(MP_STATE_PORT(rp2_state_machine_irq_obj)));
146-
irq_set_exclusive_handler(PIO0_IRQ_0, pio0_irq0);
147-
irq_set_exclusive_handler(PIO1_IRQ_0, pio1_irq0);
148163
}
149164

150165
void rp2_pio_deinit(void) {
151166
// Disable and clear interrupts.
152-
irq_set_mask_enabled((1u << PIO0_IRQ_0) | (1u << PIO1_IRQ_0), false);
153-
irq_remove_handler(PIO0_IRQ_0, pio0_irq0);
154-
irq_remove_handler(PIO1_IRQ_0, pio1_irq0);
167+
if (irq_get_exclusive_handler(PIO0_IRQ_0) == pio0_irq0) {
168+
irq_set_enabled(PIO0_IRQ_0, false);
169+
irq_remove_handler(PIO0_IRQ_0, pio0_irq0);
170+
}
171+
if (irq_get_exclusive_handler(PIO1_IRQ_0) == pio1_irq0) {
172+
irq_set_enabled(PIO1_IRQ_0, false);
173+
irq_remove_handler(PIO1_IRQ_0, pio1_irq0);
174+
}
155175

156176
rp2_state_machine_reset_all();
157177

@@ -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
379399

380400
// Enable IRQ if a handler is given.
381401
if (args[ARG_handler].u_obj != mp_const_none) {
402+
rp2_pio_irq_set_exclusive_handler(self->pio, self->irq);
382403
self->pio->inte0 = irq->trigger;
383404
irq_set_enabled(self->irq, true);
384405
}
@@ -872,6 +893,7 @@ static mp_obj_t rp2_state_machine_irq(size_t n_args, const mp_obj_t *pos_args, m
872893
}
873894

874895
if (self->pio->inte0) {
896+
rp2_pio_irq_set_exclusive_handler(self->pio, self->irq);
875897
irq_set_enabled(self->irq, true);
876898
}
877899
}

0 commit comments

Comments
 (0)