Skip to content

Commit 6f793fb

Browse files
committed
M2351: Fix GPIO rising/falling edge interrupts cannot exist simultaneously
1 parent a2c9ae6 commit 6f793fb

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

targets/TARGET_NUVOTON/TARGET_M2351/gpio_irq_api.c

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "pinmap.h"
2424
#include "PeripheralPins.h"
2525
#include "nu_bitutil.h"
26+
#include "mbed_assert.h"
2627

2728
#define NU_MAX_PIN_PER_PORT 16
2829

@@ -89,6 +90,7 @@ int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32
8990
}
9091

9192
obj->pin = pin;
93+
obj->irq_types = 0;
9294
obj->irq_handler = (uint32_t) handler;
9395
obj->irq_id = id;
9496

@@ -156,27 +158,32 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
156158
uint32_t port_index = NU_PINNAME_TO_PORT(obj->pin);
157159
GPIO_T *gpio_base = NU_PORT_BASE(port_index);
158160

161+
/* We assume BSP has such coding so that we can easily add/remove either irq type. */
162+
MBED_STATIC_ASSERT(GPIO_INT_BOTH_EDGE == (GPIO_INT_RISING | GPIO_INT_FALLING),
163+
"GPIO_INT_BOTH_EDGE must be bitwise OR of GPIO_INT_RISING and GPIO_INT_FALLING");
164+
uint32_t irq_type;
159165
switch (event) {
160-
case IRQ_RISE:
161-
if (enable) {
162-
GPIO_EnableInt(gpio_base, pin_index, GPIO_INT_RISING);
163-
} else {
164-
gpio_base->INTEN &= ~(GPIO_INT_RISING << pin_index);
165-
}
166-
break;
166+
case IRQ_RISE:
167+
irq_type = GPIO_INT_RISING;
168+
break;
167169

168-
case IRQ_FALL:
169-
if (enable) {
170-
GPIO_EnableInt(gpio_base, pin_index, GPIO_INT_FALLING);
171-
} else {
172-
gpio_base->INTEN &= ~(GPIO_INT_FALLING << pin_index);
173-
}
174-
break;
170+
case IRQ_FALL:
171+
irq_type = GPIO_INT_FALLING;
172+
break;
175173

176-
case IRQ_NONE:
177-
default:
178-
break;
174+
default:
175+
irq_type = 0;
179176
}
177+
178+
/* We can handle invalid/null irq type. */
179+
if (enable) {
180+
obj->irq_types |= irq_type;
181+
} else {
182+
obj->irq_types &= ~irq_type;
183+
}
184+
185+
/* Update irq types */
186+
GPIO_EnableInt(gpio_base, pin_index, obj->irq_types);
180187
}
181188

182189
void gpio_irq_enable(gpio_irq_t *obj)

targets/TARGET_NUVOTON/TARGET_M2351/objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ extern "C" {
2929

3030
struct gpio_irq_s {
3131
PinName pin;
32+
uint32_t irq_types;
3233
uint32_t irq_handler;
3334
uint32_t irq_id;
3435
struct gpio_irq_s *next;

0 commit comments

Comments
 (0)