Skip to content

Commit 185b723

Browse files
Merge pull request #4676 from LMESTM/issue_2959
Correctly enable / disable InterruptIn Edges detection
2 parents a75f935 + 61648a5 commit 185b723

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

targets/TARGET_STM/gpio_irq_api.c

+19-2
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,21 @@ static void handle_interrupt_in(uint32_t irq_index, uint32_t max_num_pin_line)
9090
if (__HAL_GPIO_EXTI_GET_FLAG(pin) != RESET) {
9191
__HAL_GPIO_EXTI_CLEAR_FLAG(pin);
9292

93-
if (gpio_channel->channel_ids[gpio_idx] == 0)
93+
if (gpio_channel->channel_ids[gpio_idx] == 0) {
9494
continue;
95+
}
9596

9697
// Check which edge has generated the irq
9798
if ((gpio->IDR & pin) == 0) {
9899
irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_FALL);
99100
} else {
100101
irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_RISE);
101102
}
103+
return;
102104
}
103105
}
104106
}
107+
error("Unexpected Spurious interrupt, index %d\r\n", irq_index);
105108
}
106109

107110

@@ -254,18 +257,23 @@ void gpio_irq_free(gpio_irq_t *obj)
254257

255258
void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
256259
{
260+
/* Enable / Disable Edge triggered interrupt and store event */
257261
if (event == IRQ_RISE) {
258262
if (enable) {
259263
LL_EXTI_EnableRisingTrig_0_31(1 << STM_PIN(obj->pin));
264+
obj->event |= IRQ_RISE;
260265
} else {
261266
LL_EXTI_DisableRisingTrig_0_31(1 << STM_PIN(obj->pin));
267+
obj->event &= ~IRQ_RISE;
262268
}
263269
}
264270
if (event == IRQ_FALL) {
265271
if (enable) {
266272
LL_EXTI_EnableFallingTrig_0_31(1 << STM_PIN(obj->pin));
273+
obj->event |= IRQ_FALL;
267274
} else {
268275
LL_EXTI_DisableFallingTrig_0_31(1 << STM_PIN(obj->pin));
276+
obj->event &= ~IRQ_FALL;
269277
}
270278
}
271279
}
@@ -284,14 +292,23 @@ void gpio_irq_enable(gpio_irq_t *obj)
284292

285293
LL_EXTI_EnableIT_0_31(1 << pin_index);
286294

295+
/* Restore previous edge interrupt configuration if applicable */
296+
if (obj->event & IRQ_RISE) {
297+
LL_EXTI_EnableRisingTrig_0_31(1 << STM_PIN(obj->pin));
298+
}
299+
if (obj->event & IRQ_FALL) {
300+
LL_EXTI_EnableFallingTrig_0_31(1 << STM_PIN(obj->pin));
301+
}
302+
287303
NVIC_EnableIRQ(obj->irq_n);
288304
}
289305

290306
void gpio_irq_disable(gpio_irq_t *obj)
291307
{
292308
/* Clear EXTI line configuration */
309+
LL_EXTI_DisableRisingTrig_0_31(1 << STM_PIN(obj->pin));
310+
LL_EXTI_DisableFallingTrig_0_31(1 << STM_PIN(obj->pin));
293311
LL_EXTI_DisableIT_0_31(1 << STM_PIN(obj->pin));
294312
NVIC_DisableIRQ(obj->irq_n);
295313
NVIC_ClearPendingIRQ(obj->irq_n);
296-
obj->event = EDGE_NONE;
297314
}

0 commit comments

Comments
 (0)