Skip to content

Commit e81c679

Browse files
mkapala-nordicjfischer-no
authored andcommitted
lib: dk_buttons_and_leds: Add no interrupts mode for buttons
Add no interrupts mode for buttons (CONFIG_DK_LIBRARY_BUTTON_NO_ISR). This option avoids relying on GPIO interrupts at all and unconditionally periodically scan all of the buttons. It can be used on the nRF54L15 PDK revision 0.2.X, where buttons 3 and 4 are connected to GPIO PORT2 which does not support GPIOTE. Because of that, the PDK cannot rely on GPIO interrupts for the mentioned buttons. Jira: NCSDK-23210 Signed-off-by: Mateusz Kapala <mateusz.kapala@nordicsemi.no>
1 parent aea89ec commit e81c679

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

lib/dk_buttons_and_leds/Kconfig

+22
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,34 @@ if DK_LIBRARY
1212

1313
config DK_LIBRARY_BUTTON_SCAN_INTERVAL
1414
int "Scanning interval of buttons in milliseconds"
15+
default 50 if DK_LIBRARY_BUTTON_NO_ISR
1516
default 10
1617

1718
config DK_LIBRARY_DYNAMIC_BUTTON_HANDLERS
1819
bool "Enable the runtime assignable button handler API"
1920
default y
2021

22+
config DK_LIBRARY_BUTTON_NO_ISR
23+
bool "Poll buttons unconditionally (no interrupts) [EXPERIMENTAL]"
24+
select EXPERIMENTAL
25+
help
26+
With this option disabled, the module periodically scans all the
27+
available buttons until no button is pressed. If no button is
28+
pressed, the module uses GPIO interrupts to detect the first button
29+
press. On the first button press, the module switches back to
30+
periodically scanning buttons.
31+
32+
Enable this option to avoid relying on GPIO interrupts at all and
33+
to unconditionally scan all buttons periodically. Please note that
34+
the constant scanning activity increases the overall power
35+
consumption of the system.
36+
37+
For example, in case the application uses Button 3 or 4, the option
38+
must be set for the nRF54L15 PDK (PCA10156) revisions
39+
v0.2.0 AA0-ES2, v0.2.0 AA0-ES3, and v0.2.1 AB0-ES5.
40+
These versions of the PDK have Buttons 3 and 4 connected to
41+
the GPIO port which does not support interrupts.
42+
2143
module = DK_LIBRARY
2244
module-str = DK library
2345
source "${ZEPHYR_BASE}/subsys/logging/Kconfig.template.log_config"

lib/dk_buttons_and_leds/dk_buttons_and_leds.c

+25-21
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ static void buttons_scan_fn(struct k_work *work)
129129
static bool initial_run = true;
130130
uint32_t button_scan;
131131

132-
if (irq_enabled) {
132+
__ASSERT_NO_MSG(!IS_ENABLED(CONFIG_DK_LIBRARY_BUTTON_NO_ISR) || !irq_enabled);
133+
134+
if (!IS_ENABLED(CONFIG_DK_LIBRARY_BUTTON_NO_ISR) && irq_enabled) {
133135
/* Disable GPIO interrupts for edge triggered devices.
134136
* Devices that are configured with active high interrupts are already disabled.
135137
*/
@@ -157,7 +159,7 @@ static void buttons_scan_fn(struct k_work *work)
157159

158160
last_button_scan = button_scan;
159161

160-
if (button_scan != 0) {
162+
if (IS_ENABLED(CONFIG_DK_LIBRARY_BUTTON_NO_ISR) || (button_scan != 0)) {
161163
k_work_reschedule(&buttons_scan,
162164
K_MSEC(CONFIG_DK_LIBRARY_BUTTON_SCAN_INTERVAL));
163165
} else {
@@ -257,29 +259,31 @@ int dk_buttons_init(button_handler_t button_handler)
257259
}
258260
}
259261

260-
uint32_t pin_mask = 0;
262+
if (!IS_ENABLED(CONFIG_DK_LIBRARY_BUTTON_NO_ISR)) {
263+
uint32_t pin_mask = 0;
261264

262-
for (size_t i = 0; i < ARRAY_SIZE(buttons); i++) {
263-
/* Module starts in scanning mode and will switch to
264-
* callback mode if no button is pressed.
265-
*/
266-
err = gpio_pin_interrupt_configure_dt(&buttons[i],
267-
GPIO_INT_DISABLE);
268-
if (err) {
269-
LOG_ERR("Cannot disable callbacks()");
270-
return err;
271-
}
265+
for (size_t i = 0; i < ARRAY_SIZE(buttons); i++) {
266+
/* Module starts in scanning mode and will switch to
267+
* callback mode if no button is pressed.
268+
*/
269+
err = gpio_pin_interrupt_configure_dt(&buttons[i],
270+
GPIO_INT_DISABLE);
271+
if (err) {
272+
LOG_ERR("Cannot disable callbacks()");
273+
return err;
274+
}
272275

273-
pin_mask |= BIT(buttons[i].pin);
274-
}
276+
pin_mask |= BIT(buttons[i].pin);
277+
}
275278

276-
gpio_init_callback(&gpio_cb, button_pressed, pin_mask);
279+
gpio_init_callback(&gpio_cb, button_pressed, pin_mask);
277280

278-
for (size_t i = 0; i < ARRAY_SIZE(buttons); i++) {
279-
err = gpio_add_callback(buttons[i].port, &gpio_cb);
280-
if (err) {
281-
LOG_ERR("Cannot add callback");
282-
return err;
281+
for (size_t i = 0; i < ARRAY_SIZE(buttons); i++) {
282+
err = gpio_add_callback(buttons[i].port, &gpio_cb);
283+
if (err) {
284+
LOG_ERR("Cannot add callback");
285+
return err;
286+
}
283287
}
284288
}
285289

0 commit comments

Comments
 (0)