Skip to content

Commit 486b78e

Browse files
authored
Fix race in gpio_set_irq_enabled_with_callback (#2635)
The order depends on whether you're enabling or disabling
1 parent 36f1df6 commit 486b78e

File tree

1 file changed

+4
-2
lines changed
  • src/rp2_common/hardware_gpio

1 file changed

+4
-2
lines changed

src/rp2_common/hardware_gpio/gpio.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,11 @@ void gpio_set_irq_enabled(uint gpio, uint32_t events, bool enabled) {
196196
}
197197

198198
void gpio_set_irq_enabled_with_callback(uint gpio, uint32_t events, bool enabled, gpio_irq_callback_t callback) {
199-
// first set callback, then enable the interrupt
200-
gpio_set_irq_callback(callback);
199+
// when enabling, first set callback, then enable the interrupt
200+
// when disabling, first disable the interrupt, then clear callback
201+
if (enabled) gpio_set_irq_callback(callback);
201202
gpio_set_irq_enabled(gpio, events, enabled);
203+
if (!enabled) gpio_set_irq_callback(callback);
202204
if (enabled) irq_set_enabled(IO_IRQ_BANK0, true);
203205
}
204206

0 commit comments

Comments
 (0)