|
23 | 23 | #include "pinmap.h"
|
24 | 24 | #include "PeripheralPins.h"
|
25 | 25 | #include "nu_bitutil.h"
|
| 26 | +#include "mbed_assert.h" |
26 | 27 |
|
27 | 28 | #define NU_MAX_PIN_PER_PORT 16
|
28 | 29 |
|
@@ -89,6 +90,7 @@ int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32
|
89 | 90 | }
|
90 | 91 |
|
91 | 92 | obj->pin = pin;
|
| 93 | + obj->irq_types = 0; |
92 | 94 | obj->irq_handler = (uint32_t) handler;
|
93 | 95 | obj->irq_id = id;
|
94 | 96 |
|
@@ -156,27 +158,32 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
|
156 | 158 | uint32_t port_index = NU_PINNAME_TO_PORT(obj->pin);
|
157 | 159 | GPIO_T *gpio_base = NU_PORT_BASE(port_index);
|
158 | 160 |
|
| 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; |
159 | 165 | 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; |
167 | 169 |
|
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; |
175 | 173 |
|
176 |
| - case IRQ_NONE: |
177 |
| - default: |
178 |
| - break; |
| 174 | + default: |
| 175 | + irq_type = 0; |
179 | 176 | }
|
| 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); |
180 | 187 | }
|
181 | 188 |
|
182 | 189 | void gpio_irq_enable(gpio_irq_t *obj)
|
|
0 commit comments