Skip to content

Commit 9317bea

Browse files
committed
STM32 GPIO INIT in critical section
critical section added in gpio_irq init and free functions to protect shared code structures Note that other functions are protected in API level in InterruptIn
1 parent 8d246d8 commit 9317bea

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

targets/TARGET_STM/gpio_irq_api.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "pinmap.h"
3838
#include "mbed_error.h"
3939
#include "gpio_irq_device.h"
40+
#include "platform/mbed_critical.h"
4041

4142
#define EDGE_NONE (0)
4243
#define EDGE_RISE (1)
@@ -194,6 +195,8 @@ int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32
194195
return -1;
195196
}
196197

198+
core_util_critical_section_enter();
199+
197200
/* Enable SYSCFG Clock */
198201
#if !defined(TARGET_STM32WB)
199202
__HAL_RCC_SYSCFG_CLK_ENABLE();
@@ -266,19 +269,25 @@ int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32
266269
NVIC_SetVector(obj->irq_n, vector);
267270
gpio_irq_enable(obj);
268271

272+
core_util_critical_section_exit();
269273
return 0;
270274
}
271275

272276
void gpio_irq_free(gpio_irq_t *obj)
273277
{
278+
core_util_critical_section_enter();
279+
274280
uint32_t gpio_idx = pin_lines_desc[STM_PIN(obj->pin)].gpio_idx;
275281
gpio_channel_t *gpio_channel = &channels[obj->irq_index];
276282

283+
gpio_irq_disable(obj);
284+
277285
gpio_channel->pin_mask &= ~(1 << gpio_idx);
278286
gpio_channel->channel_ids[gpio_idx] = 0;
279287
gpio_channel->channel_gpio[gpio_idx] = 0;
280288
gpio_channel->channel_pin[gpio_idx] = 0;
281-
gpio_irq_disable(obj);
289+
290+
core_util_critical_section_exit();
282291
}
283292

284293
void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
@@ -342,6 +351,13 @@ void gpio_irq_disable(gpio_irq_t *obj)
342351
LL_EXTI_DisableFallingTrig_0_31(1 << pin_index);
343352
LL_EXTI_DisableIT_0_31(1 << pin_index);
344353

354+
uint32_t pin = (uint32_t)(1 << (gpio_channel->channel_pin[gpio_idx]));
355+
356+
// Clear interrupt flag
357+
if (__HAL_GPIO_EXTI_GET_FLAG(pin) != RESET) {
358+
__HAL_GPIO_EXTI_CLEAR_FLAG(pin);
359+
}
360+
345361
const bool no_more_pins_on_vector = (gpio_channel->pin_mask & ~pin_mask) == 0;
346362
if (no_more_pins_on_vector) {
347363
NVIC_DisableIRQ(obj->irq_n);

0 commit comments

Comments
 (0)