Skip to content

Commit 281ea45

Browse files
MirkoCovizzieivindj-nordic
authored andcommitted
lib: bm_buttons: use nrfx critical section API
Replaces the custom crticial section implementation with the nrfx ones. These are implemented in `nrfx_glue.h` and use Zephyr's `irq_lock` and `irq_unlock`. These, by default, do not disable zero latency interrupts, such as the ones registered for the SoftDevice's time-sensitive operations (e.g. radio) that should never be disabled. Signed-off-by: Mirko Covizzi <[email protected]>
1 parent ea272d3 commit 281ea45

File tree

1 file changed

+9
-29
lines changed

1 file changed

+9
-29
lines changed

lib/bm_buttons/bm_buttons.c

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,13 @@
77
#include <zephyr/logging/log.h>
88
#include <zephyr/irq.h>
99
#include <nrfx_gpiote.h>
10+
#include <nrfx_glue.h>
1011

1112
#include <bm_timer.h>
1213
#include <bm_buttons.h>
1314

1415
LOG_MODULE_REGISTER(bm_buttons, CONFIG_BM_BUTTONS_LOG_LEVEL);
1516

16-
/* Local implementation of critical section enter/exit support. */
17-
18-
#include <stdint.h>
19-
#include <nrf.h>
20-
21-
static uint32_t bm_irq_disable(void)
22-
{
23-
uint32_t pm = __get_PRIMASK();
24-
__disable_irq();
25-
return pm;
26-
}
27-
28-
static void bm_irq_enable(uint32_t pm)
29-
{
30-
if (!pm) {
31-
__enable_irq();
32-
}
33-
}
34-
35-
/* End of local implementation of critical section enter/exit support. */
36-
3717
#define IRQ_PRIO 3
3818
#define BITS_PER_PIN 4
3919
#define NUM_PINS CONFIG_BM_BUTTONS_NUM_PINS
@@ -235,9 +215,9 @@ static void evt_handle(uint8_t pin, uint8_t index, bool is_active)
235215
state_set(index, BUTTON_PRESS_ARMED);
236216
LOG_DBG("Pin %d %s -> %s", pin, STRINGIFY(BUTTON_IDLE),
237217
STRINGIFY(BUTTON_PRESS_ARMED));
238-
uint32_t pm = bm_irq_disable();
218+
NRFX_CRITICAL_SECTION_ENTER();
239219
global.pin_active |= 1ULL << index;
240-
bm_irq_enable(pm);
220+
NRFX_CRITICAL_SECTION_EXIT();
241221
} else {
242222
/* Stay in BUTTON_IDLE. */
243223
}
@@ -250,9 +230,9 @@ static void evt_handle(uint8_t pin, uint8_t index, bool is_active)
250230
user_event(pin, BM_BUTTONS_PRESS);
251231
} else {
252232
state_set(index, BUTTON_IDLE);
253-
uint32_t pm = bm_irq_disable();
233+
NRFX_CRITICAL_SECTION_ENTER();
254234
global.pin_active &= ~(1ULL << index);
255-
bm_irq_enable(pm);
235+
NRFX_CRITICAL_SECTION_EXIT();
256236
}
257237
break;
258238
case BUTTON_PRESSED:
@@ -272,9 +252,9 @@ static void evt_handle(uint8_t pin, uint8_t index, bool is_active)
272252
} else {
273253
state_set(index, BUTTON_IDLE);
274254
user_event(pin, BM_BUTTONS_RELEASE);
275-
uint32_t pm = bm_irq_disable();
255+
NRFX_CRITICAL_SECTION_ENTER();
276256
global.pin_active &= ~(1ULL << index);
277-
bm_irq_enable(pm);
257+
NRFX_CRITICAL_SECTION_EXIT();
278258
}
279259
break;
280260
}
@@ -305,9 +285,9 @@ static int buttons_disable(void)
305285
gpiote_trigger_enable(global.configs[i].pin_number, false);
306286
}
307287

308-
uint32_t pm = bm_irq_disable();
288+
NRFX_CRITICAL_SECTION_ENTER();
309289
global.pin_active = 0;
310-
bm_irq_enable(pm);
290+
NRFX_CRITICAL_SECTION_EXIT();
311291

312292
return 0;
313293
}

0 commit comments

Comments
 (0)