Skip to content

Commit 0b8f467

Browse files
author
Cruz Monrreal
authored
Merge pull request #7220 from melvinvdb/fix_nrf5x_interrupt_pull_mode
Fix mbed::InterruptIn.mode() in NRF5x targets
2 parents b80490d + 4986daa commit 0b8f467

File tree

1 file changed

+38
-44
lines changed

1 file changed

+38
-44
lines changed

targets/TARGET_NORDIC/TARGET_NRF5x/gpio_api.c

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121

2222

2323
#if defined(TARGET_MCU_NRF51822)
24-
#define GPIO_PIN_COUNT 31
24+
#define GPIO_PIN_COUNT 31
2525
#elif defined(TARGET_MCU_NRF52832)
26-
#define GPIO_PIN_COUNT 32
26+
#define GPIO_PIN_COUNT 32
2727
#elif defined(TARGET_MCU_NRF52840)
28-
#define GPIO_PIN_COUNT 48
28+
#define GPIO_PIN_COUNT 48
2929
#else
30-
#error not recognized gpio count for mcu
30+
#error not recognized gpio count for mcu
3131
#endif
3232

3333
typedef struct {
@@ -40,9 +40,9 @@ typedef struct {
4040
} gpio_cfg_t;
4141

4242
#if GPIO_PIN_COUNT > 32
43-
typedef uint64_t gpio_mask_t;
43+
typedef uint64_t gpio_mask_t;
4444
#else
45-
typedef uint32_t gpio_mask_t;
45+
typedef uint32_t gpio_mask_t;
4646
#endif
4747

4848
static gpio_mask_t m_gpio_initialized;
@@ -62,12 +62,12 @@ static void gpiote_irq_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t a
6262
{
6363
nrf_gpio_pin_sense_t sense = nrf_gpio_pin_sense_get(pin);
6464
gpio_irq_event event = (sense == NRF_GPIO_PIN_SENSE_LOW) ? IRQ_RISE : IRQ_FALL;
65-
65+
6666
if (m_gpio_irq_enabled & ((gpio_mask_t)1 << pin)) {
6767
if (((event == IRQ_RISE) && m_gpio_cfg[pin].irq_rise)
68-
|| ((event == IRQ_FALL) && m_gpio_cfg[pin].irq_fall)) {
69-
m_irq_handler(m_channel_ids[pin], event);
70-
}
68+
|| ((event == IRQ_FALL) && m_gpio_cfg[pin].irq_fall)) {
69+
m_irq_handler(m_channel_ids[pin], event);
70+
}
7171
}
7272
}
7373

@@ -80,9 +80,9 @@ void gpio_init(gpio_t *obj, PinName pin)
8080
return;
8181
}
8282
MBED_ASSERT((uint32_t)pin < GPIO_PIN_COUNT);
83-
83+
8484
NVIC_SetVector(GPIOTE_IRQn, (uint32_t) GPIOTE_IRQHandler);
85-
85+
8686
(void) nrf_drv_gpiote_init();
8787

8888
m_gpio_cfg[obj->pin].used_as_gpio = true;
@@ -111,8 +111,7 @@ static void gpiote_pin_uninit(uint8_t pin)
111111
if (m_gpio_initialized & ((gpio_mask_t)1UL << pin)) {
112112
if ((m_gpio_cfg[pin].direction == PIN_OUTPUT) && (!m_gpio_cfg[pin].used_as_irq)) {
113113
nrf_drv_gpiote_out_uninit(pin);
114-
}
115-
else if (m_gpio_cfg[pin].used_as_irq) {
114+
} else if (m_gpio_cfg[pin].used_as_irq) {
116115
nrf_drv_gpiote_in_uninit(pin);
117116
}
118117
}
@@ -122,45 +121,40 @@ static void gpio_apply_config(uint8_t pin)
122121
{
123122
if (m_gpio_cfg[pin].used_as_gpio || m_gpio_cfg[pin].used_as_irq) {
124123
if ((m_gpio_cfg[pin].direction == PIN_INPUT)
125-
|| (m_gpio_cfg[pin].used_as_irq)) {
124+
|| (m_gpio_cfg[pin].used_as_irq)) {
126125
//Configure as input.
127126
nrf_drv_gpiote_in_config_t cfg;
128127

129128
cfg.hi_accuracy = false;
130129
cfg.is_watcher = false;
131130
cfg.sense = NRF_GPIOTE_POLARITY_TOGGLE;
131+
switch (m_gpio_cfg[pin].pull) {
132+
case PullUp:
133+
cfg.pull = NRF_GPIO_PIN_PULLUP;
134+
break;
135+
case PullDown:
136+
cfg.pull = NRF_GPIO_PIN_PULLDOWN;
137+
break;
138+
default:
139+
cfg.pull = NRF_GPIO_PIN_NOPULL;
140+
break;
141+
}
132142
if (m_gpio_cfg[pin].used_as_irq) {
133-
cfg.pull = NRF_GPIO_PIN_PULLUP;
134143
nrf_drv_gpiote_in_init(pin, &cfg, gpiote_irq_handler);
135144
if ((m_gpio_irq_enabled & ((gpio_mask_t)1 << pin))
136-
&& (m_gpio_cfg[pin].irq_rise || m_gpio_cfg[pin].irq_fall))
137-
{
145+
&& (m_gpio_cfg[pin].irq_rise || m_gpio_cfg[pin].irq_fall)) {
138146
nrf_drv_gpiote_in_event_enable(pin, true);
139147
}
148+
} else {
149+
nrf_gpio_cfg_input(pin, cfg.pull);
140150
}
141-
else {
142-
switch(m_gpio_cfg[pin].pull) {
143-
case PullUp:
144-
cfg.pull = NRF_GPIO_PIN_PULLUP;
145-
break;
146-
case PullDown:
147-
cfg.pull = NRF_GPIO_PIN_PULLDOWN;
148-
break;
149-
default:
150-
cfg.pull = NRF_GPIO_PIN_NOPULL;
151-
break;
152-
}
153-
nrf_gpio_cfg_input(pin,cfg.pull);
154-
}
155-
}
156-
else {
151+
} else {
157152
// Configure as output.
158153
nrf_drv_gpiote_out_config_t cfg = GPIOTE_CONFIG_OUT_SIMPLE(nrf_gpio_pin_out_read(pin));
159154
nrf_drv_gpiote_out_init(pin, &cfg);
160155
}
161156
m_gpio_initialized |= ((gpio_mask_t)1UL << pin);
162-
}
163-
else {
157+
} else {
164158
m_gpio_initialized &= ~((gpio_mask_t)1UL << pin);
165159
}
166160
}
@@ -169,9 +163,9 @@ static void gpio_apply_config(uint8_t pin)
169163
void gpio_mode(gpio_t *obj, PinMode mode)
170164
{
171165
MBED_ASSERT(obj->pin != (PinName)NC);
172-
166+
173167
gpiote_pin_uninit(obj->pin); // try to uninitialize gpio before a change.
174-
168+
175169
m_gpio_cfg[obj->pin].pull = mode;
176170
gpio_apply_config(obj->pin);
177171
}
@@ -180,9 +174,9 @@ void gpio_mode(gpio_t *obj, PinMode mode)
180174
void gpio_dir(gpio_t *obj, PinDirection direction)
181175
{
182176
MBED_ASSERT(obj->pin != (PinName)NC);
183-
177+
184178
gpiote_pin_uninit(obj->pin); // try to uninitialize gpio before a change.
185-
179+
186180
m_gpio_cfg[obj->pin].direction = direction;
187181
gpio_apply_config(obj->pin);
188182
}
@@ -201,8 +195,9 @@ int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32
201195
(void) nrf_drv_gpiote_init();
202196

203197
gpiote_pin_uninit(pin); // try to uninitialize gpio before a change.
204-
198+
205199
m_gpio_cfg[pin].used_as_irq = true;
200+
m_gpio_cfg[pin].pull = PullNone;
206201
m_channel_ids[pin] = id;
207202
obj->ch = pin;
208203
m_irq_handler = handler;
@@ -225,15 +220,14 @@ void gpio_irq_free(gpio_irq_t *obj)
225220

226221
void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
227222
{
228-
gpio_cfg_t* cfg = &m_gpio_cfg[obj->ch];
223+
gpio_cfg_t *cfg = &m_gpio_cfg[obj->ch];
229224
bool irq_enabled_before =
230225
(m_gpio_irq_enabled & ((gpio_mask_t)1 << obj->ch)) &&
231226
(cfg->irq_rise || cfg->irq_fall);
232227

233228
if (event == IRQ_RISE) {
234229
cfg->irq_rise = enable ? true : false;
235-
}
236-
else if (event == IRQ_FALL) {
230+
} else if (event == IRQ_FALL) {
237231
cfg->irq_fall = enable ? true : false;
238232
}
239233

0 commit comments

Comments
 (0)