Skip to content

Commit e9cebde

Browse files
authored
Merge pull request #2372 from anangl/gpio_irq_fix
Fixed irq handling in gpio_api.
2 parents 0edef2d + 41c9d1d commit e9cebde

File tree

1 file changed

+15
-29
lines changed
  • hal/targets/hal/TARGET_NORDIC/TARGET_NRF5

1 file changed

+15
-29
lines changed

hal/targets/hal/TARGET_NORDIC/TARGET_NRF5/gpio_api.c

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@
2222

2323
#define GPIO_PIN_COUNT 31
2424

25-
typedef enum {
26-
GPIO_NOT_USED = 0,
27-
GPIO_USED = 1,
28-
GPIO_USED_IRQ = 2,
29-
GPIO_USED_IRQ_DISABLED = 3
30-
} gpio_usage_t;
31-
3225
typedef struct {
3326
bool used_as_gpio : 1;
3427
PinDirection direction : 1;
@@ -100,13 +93,13 @@ static void gpio_apply_config(uint8_t pin)
10093
nrf_drv_gpiote_in_uninit(pin);
10194
}
10295
}
103-
96+
10497
if (m_gpio_cfg[pin].used_as_gpio || m_gpio_cfg[pin].used_as_irq) {
10598
if ((m_gpio_cfg[pin].direction == PIN_INPUT)
10699
|| (m_gpio_cfg[pin].used_as_irq)) {
107100
//Configure as input.
108101
nrf_drv_gpiote_in_config_t cfg;
109-
102+
110103
cfg.hi_accuracy = false;
111104
cfg.is_watcher = false;
112105
cfg.sense = NRF_GPIOTE_POLARITY_TOGGLE;
@@ -116,7 +109,7 @@ static void gpio_apply_config(uint8_t pin)
116109
if ((m_gpio_irq_enabled & (1 << pin))
117110
&& (m_gpio_cfg[pin].irq_rise || m_gpio_cfg[pin].irq_fall))
118111
{
119-
nrf_drv_gpiote_in_event_enable(pin, false);
112+
nrf_drv_gpiote_in_event_enable(pin, true);
120113
}
121114
}
122115
else {
@@ -198,30 +191,25 @@ void gpio_irq_free(gpio_irq_t *obj)
198191

199192
void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
200193
{
201-
bool event_enabled_before = false;
202-
if ((m_gpio_irq_enabled & (1 << obj->ch))
203-
&& (m_gpio_cfg[obj->ch].irq_rise || m_gpio_cfg[obj->ch].irq_fall)) {
204-
event_enabled_before = true;
205-
}
194+
gpio_cfg_t* cfg = &m_gpio_cfg[obj->ch];
195+
bool irq_enabled_before =
196+
(m_gpio_irq_enabled & (1 << obj->ch)) &&
197+
(cfg->irq_rise || cfg->irq_fall);
206198

207199
if (event == IRQ_RISE) {
208-
m_gpio_cfg[obj->ch].irq_rise = enable ? true : false;
200+
cfg->irq_rise = enable ? true : false;
209201
}
210202
else if (event == IRQ_FALL) {
211-
m_gpio_cfg[obj->ch].irq_fall = enable ? true : false;
203+
cfg->irq_fall = enable ? true : false;
212204
}
213205

214-
bool event_enabled_after = false;
215-
if ((m_gpio_irq_enabled & (1 << obj->ch))
216-
&& (m_gpio_cfg[obj->ch].irq_rise || m_gpio_cfg[obj->ch].irq_fall)) {
217-
event_enabled_after = true;
218-
}
206+
bool irq_enabled_after = cfg->irq_rise || cfg->irq_fall;
219207

220-
if (event_enabled_before != event_enabled_after) {
221-
if (event_enabled_after) {
222-
nrf_drv_gpiote_in_event_enable(obj->ch,false);
208+
if (irq_enabled_before != irq_enabled_after) {
209+
if (irq_enabled_after) {
210+
gpio_irq_enable(obj);
223211
} else {
224-
nrf_drv_gpiote_in_event_disable(obj->ch);
212+
gpio_irq_disable(obj);
225213
}
226214
}
227215
}
@@ -239,7 +227,5 @@ void gpio_irq_enable(gpio_irq_t *obj)
239227
void gpio_irq_disable(gpio_irq_t *obj)
240228
{
241229
m_gpio_irq_enabled &= ~(1 << obj->ch);
242-
if (m_gpio_cfg[obj->ch].irq_rise || m_gpio_cfg[obj->ch].irq_fall) {
243-
nrf_drv_gpiote_in_event_enable(obj->ch, false);
244-
}
230+
nrf_drv_gpiote_in_event_disable(obj->ch);
245231
}

0 commit comments

Comments
 (0)