Skip to content

Conversation

will-v-pi
Copy link
Contributor

When disabling, if you get an interrupt after gpio_set_irq_callback before gpio_set_irq_enabled then you get an unhandled_user_irq

Fix is to change where you gpio_set_irq_callback depending on whether you're enabling or disabling

The order depends on whether you're enabling or disabling
@will-v-pi will-v-pi added this to the 2.2.1 milestone Aug 22, 2025
@will-v-pi will-v-pi requested a review from kilograham August 22, 2025 11:41
Comment on lines +199 to 204
// when enabling, first set callback, then enable the interrupt
// when disabling, first disable the interrupt, then clear callback
if (enabled) gpio_set_irq_callback(callback);
gpio_set_irq_enabled(gpio, events, enabled);
if (!enabled) gpio_set_irq_callback(callback);
if (enabled) irq_set_enabled(IO_IRQ_BANK0, true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function likely to get used in performance-critical code? If it is, perhaps it's worth reducing the branches? 🤔 (or is one of those cases where the compiler is able to do this optimisation automatically?)

Suggested change
// when enabling, first set callback, then enable the interrupt
// when disabling, first disable the interrupt, then clear callback
if (enabled) gpio_set_irq_callback(callback);
gpio_set_irq_enabled(gpio, events, enabled);
if (!enabled) gpio_set_irq_callback(callback);
if (enabled) irq_set_enabled(IO_IRQ_BANK0, true);
// when enabling, first set callback, then enable the interrupt
// when disabling, first disable the interrupt, then clear callback
if (enabled) {
gpio_set_irq_callback(callback);
gpio_set_irq_enabled(gpio, events, enabled);
irq_set_enabled(IO_IRQ_BANK0, true);
} else {
gpio_set_irq_enabled(gpio, events, enabled);
gpio_set_irq_callback(callback);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants