Skip to content

Commit cdd3f54

Browse files
committed
applications: nrf5340_audio: Button handler for different kits
Currently the button handler expects kits to have five buttons - which is true for the Audio DK, but not for the nRF5340 DK. To have the audio application running on the nRF5340 DK (and other kits in the future), the handler must be refactored to not fail compilation when fewer buttons are present, and reduce the application's overall dependency on button availability. Signed-off-by: Graham Wacey <graham.wacey@nordicsemi.no>
1 parent ace34ce commit cdd3f54

File tree

6 files changed

+113
-97
lines changed

6 files changed

+113
-97
lines changed

applications/nrf5340_audio/src/modules/button_assignments.h

+34-12
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,43 @@
1616

1717
#include <zephyr/drivers/gpio.h>
1818

19-
#define BUTTON_NOT_ASSIGNED 0xFF
19+
#define BUTTON_NOT_ASSIGNED 0xFFFF
2020

2121
/** @brief List of buttons and associated metadata
2222
*/
23-
enum button_pin_names {
24-
BUTTON_VOLUME_DOWN = DT_GPIO_PIN(DT_ALIAS(sw0), gpios),
25-
BUTTON_VOLUME_UP = DT_GPIO_PIN(DT_ALIAS(sw1), gpios),
26-
BUTTON_PLAY_PAUSE = DT_GPIO_PIN(DT_ALIAS(sw2), gpios),
27-
#if defined(CONFIG_BOARD_NRF5340_AUDIO_DK_NRF5340_CPUAPP)
28-
BUTTON_4 = DT_GPIO_PIN(DT_ALIAS(sw3), gpios),
29-
BUTTON_5 = DT_GPIO_PIN(DT_ALIAS(sw4), gpios),
23+
#if DT_NODE_EXISTS(DT_ALIAS(sw0))
24+
#define BUTTON_VOLUME_DOWN DT_GPIO_PIN(DT_ALIAS(sw0), gpios)
3025
#else
31-
BUTTON_5 = DT_GPIO_PIN(DT_ALIAS(sw3), gpios),
32-
BUTTON_4 = BUTTON_NOT_ASSIGNED,
33-
#endif /* CONFIG_BOARD_NRF5340_AUDIO_DK_NRF5340_CPUAPP */
34-
};
26+
#define BUTTON_VOLUME_DOWN BUTTON_NOT_ASSIGNED
27+
28+
#endif
29+
30+
#if DT_NODE_EXISTS(DT_ALIAS(sw1))
31+
#define BUTTON_VOLUME_UP DT_GPIO_PIN(DT_ALIAS(sw1), gpios)
32+
#else
33+
#define BUTTON_VOLUME_UP BUTTON_NOT_ASSIGNED
34+
35+
#endif
36+
37+
#if DT_NODE_EXISTS(DT_ALIAS(sw2))
38+
#define BUTTON_PLAY_PAUSE DT_GPIO_PIN(DT_ALIAS(sw2), gpios)
39+
#else
40+
#define BUTTON_PLAY_PAUSE BUTTON_NOT_ASSIGNED
41+
#endif
42+
43+
#if DT_NODE_EXISTS(DT_ALIAS(sw4))
44+
#define BUTTON_5 DT_GPIO_PIN(DT_ALIAS(sw4), gpios)
45+
#if DT_NODE_EXISTS(DT_ALIAS(sw3))
46+
#define BUTTON_4 DT_GPIO_PIN(DT_ALIAS(sw3), gpios)
47+
#else
48+
#define BUTTON_4 BUTTON_NOT_ASSIGNED
49+
#endif
50+
#elif DT_NODE_EXISTS(DT_ALIAS(sw3))
51+
#define BUTTON_4 BUTTON_NOT_ASSIGNED
52+
#define BUTTON_5 DT_GPIO_PIN(DT_ALIAS(sw3), gpios)
53+
#else
54+
#define BUTTON_4 BUTTON_NOT_ASSIGNED
55+
#define BUTTON_5 BUTTON_NOT_ASSIGNED
56+
#endif
3557

3658
#endif /* _BUTTON_ASSIGNMENTS_H_ */

applications/nrf5340_audio/src/modules/button_handler.c

+67-79
Original file line numberDiff line numberDiff line change
@@ -25,59 +25,43 @@ LOG_MODULE_REGISTER(button_handler, CONFIG_MODULE_BUTTON_HANDLER_LOG_LEVEL);
2525
ZBUS_CHAN_DEFINE(button_chan, struct button_msg, NULL, NULL, ZBUS_OBSERVERS_EMPTY,
2626
ZBUS_MSG_INIT(0));
2727

28-
/* How many buttons does the module support. Increase at memory cost */
29-
#define BUTTONS_MAX 5
30-
#define BASE_10 10
28+
/* Button configuration structure. */
29+
struct btn_cfg_data {
30+
const char *name;
31+
const struct gpio_dt_spec gpio;
32+
void *user_cfg;
33+
};
34+
35+
struct btn_unit_cfg {
36+
uint32_t num_buttons;
37+
struct btn_cfg_data *buttons;
38+
};
39+
40+
#define BASE_10 10
3141

3242
/* Only allow one button msg at a time, as a mean of debounce */
3343
K_MSGQ_DEFINE(button_queue, sizeof(struct button_msg), 1, 4);
3444

3545
static bool debounce_is_ongoing;
36-
static struct gpio_callback btn_callback[BUTTONS_MAX];
37-
38-
/* clang-format off */
39-
const static struct btn_config btn_cfg[] = {
40-
{
41-
.btn_name = STRINGIFY(BUTTON_VOLUME_DOWN),
42-
.btn_pin = BUTTON_VOLUME_DOWN,
43-
.btn_cfg_mask = DT_GPIO_FLAGS(DT_ALIAS(sw0), gpios),
44-
},
45-
{
46-
.btn_name = STRINGIFY(BUTTON_VOLUME_UP),
47-
.btn_pin = BUTTON_VOLUME_UP,
48-
.btn_cfg_mask = DT_GPIO_FLAGS(DT_ALIAS(sw1), gpios),
49-
},
50-
{
51-
.btn_name = STRINGIFY(BUTTON_PLAY_PAUSE),
52-
.btn_pin = BUTTON_PLAY_PAUSE,
53-
.btn_cfg_mask = DT_GPIO_FLAGS(DT_ALIAS(sw2), gpios),
54-
},
55-
#if defined(CONFIG_BOARD_NRF5340_AUDIO_DK_NRF5340_CPUAPP)
56-
{
57-
.btn_name = STRINGIFY(BUTTON_4),
58-
.btn_pin = BUTTON_4,
59-
.btn_cfg_mask = DT_GPIO_FLAGS(DT_ALIAS(sw3), gpios),
60-
},
61-
{
62-
.btn_name = STRINGIFY(BUTTON_5),
63-
.btn_pin = BUTTON_5,
64-
.btn_cfg_mask = DT_GPIO_FLAGS(DT_ALIAS(sw4), gpios),
65-
}
46+
47+
#if DT_NODE_EXISTS(DT_PATH(buttons))
48+
#define BUTTON DT_PATH(buttons)
6649
#else
67-
{
68-
.btn_name = STRINGIFY(BUTTON_4),
69-
.btn_pin = BUTTON_4,
70-
},
71-
{
72-
.btn_name = STRINGIFY(BUTTON_5),
73-
.btn_pin = BUTTON_5,
74-
.btn_cfg_mask = DT_GPIO_FLAGS(DT_ALIAS(sw3), gpios),
75-
}
76-
#endif /* CONFIG_BOARD_NRF5340_AUDIO_DK_NRF5340_CPUAPP */
77-
};
78-
/* clang-format on */
50+
#pragma error("No buttons node found")
51+
#endif
52+
53+
#define BUTTON_GPIO(button_node_id) \
54+
{ \
55+
.name = STRINGIFY(BUTTON_##button_node_id), \
56+
.gpio = GPIO_DT_SPEC_GET(button_node_id, gpios), \
57+
},
7958

80-
static const struct device *gpio_53_dev;
59+
static struct btn_cfg_data btn_config_data[] = {DT_FOREACH_CHILD(BUTTON, BUTTON_GPIO)};
60+
61+
static const struct btn_unit_cfg btn_config = {.num_buttons = ARRAY_SIZE(btn_config_data),
62+
.buttons = btn_config_data};
63+
64+
static struct gpio_callback btn_callback[ARRAY_SIZE(btn_config_data)];
8165

8266
/**@brief Simple debouncer for buttons
8367
*
@@ -95,8 +79,8 @@ K_TIMER_DEFINE(button_debounce_timer, on_button_debounce_timeout, NULL);
9579
*/
9680
static int pin_to_btn_idx(uint8_t btn_pin, uint32_t *pin_idx)
9781
{
98-
for (uint8_t i = 0; i < ARRAY_SIZE(btn_cfg); i++) {
99-
if (btn_pin == btn_cfg[i].btn_pin) {
82+
for (uint8_t i = 0; i < btn_config.num_buttons; i++) {
83+
if (btn_pin == btn_config.buttons[i].gpio.pin) {
10084
*pin_idx = i;
10185
return 0;
10286
}
@@ -174,7 +158,7 @@ static void button_isr(const struct device *port, struct gpio_callback *cb, uint
174158
ERR_CHK(ret);
175159

176160
LOG_DBG("Pushed button idx: %d pin: %d name: %s", btn_idx, btn_pin,
177-
btn_cfg[btn_idx].btn_name);
161+
btn_config.buttons[btn_idx].name);
178162

179163
msg.button_pin = btn_pin;
180164
msg.button_action = BUTTON_PRESS;
@@ -191,16 +175,22 @@ static void button_isr(const struct device *port, struct gpio_callback *cb, uint
191175
int button_pressed(gpio_pin_t button_pin, bool *button_pressed)
192176
{
193177
int ret;
178+
uint32_t btn_idx;
194179

195-
if (!device_is_ready(gpio_53_dev)) {
196-
return -ENODEV;
180+
if (button_pin == BUTTON_NOT_ASSIGNED || button_pressed == NULL) {
181+
return -EINVAL;
197182
}
198183

199-
if (button_pressed == NULL) {
184+
ret = pin_to_btn_idx(button_pin, &btn_idx);
185+
if (ret) {
186+
return -EINVAL;
187+
}
188+
189+
if (!device_is_ready(btn_config.buttons[btn_idx].gpio.port)) {
200190
return -EINVAL;
201191
}
202192

203-
ret = gpio_pin_get(gpio_53_dev, button_pin);
193+
ret = gpio_pin_get(btn_config.buttons[btn_idx].gpio.port, button_pin);
204194
switch (ret) {
205195
case 0:
206196
*button_pressed = false;
@@ -219,37 +209,34 @@ int button_handler_init(void)
219209
{
220210
int ret;
221211

222-
if (ARRAY_SIZE(btn_cfg) == 0) {
212+
if (btn_config.num_buttons == 0) {
223213
LOG_WRN("No buttons assigned");
224214
return -EINVAL;
225215
}
226216

227-
gpio_53_dev = DEVICE_DT_GET(DT_NODELABEL(gpio0));
228-
229-
if (!device_is_ready(gpio_53_dev)) {
230-
LOG_ERR("Device driver not ready.");
231-
return -ENODEV;
232-
}
233-
234-
for (uint8_t i = 0; i < ARRAY_SIZE(btn_cfg); i++) {
235-
if (btn_cfg[i].btn_pin == BUTTON_NOT_ASSIGNED) {
236-
continue;
237-
}
238-
239-
ret = gpio_pin_configure(gpio_53_dev, btn_cfg[i].btn_pin,
240-
GPIO_INPUT | btn_cfg[i].btn_cfg_mask);
241-
if (ret) {
242-
return ret;
217+
for (size_t i = 0; i < btn_config.num_buttons; i++) {
218+
struct btn_cfg_data *button = &btn_config.buttons[i];
219+
220+
if (device_is_ready(button->gpio.port)) {
221+
ret = gpio_pin_configure(button->gpio.port, button->gpio.pin,
222+
GPIO_INPUT | button->gpio.dt_flags);
223+
if (ret) {
224+
LOG_ERR("Cannot configure GPIO (ret %d)", ret);
225+
return ret;
226+
}
227+
} else {
228+
LOG_ERR("GPIO device not ready");
229+
return -ENODEV;
243230
}
244231

245-
gpio_init_callback(&btn_callback[i], button_isr, BIT(btn_cfg[i].btn_pin));
232+
gpio_init_callback(&btn_callback[i], button_isr, BIT(button->gpio.pin));
246233

247-
ret = gpio_add_callback(gpio_53_dev, &btn_callback[i]);
234+
ret = gpio_add_callback(button->gpio.port, &btn_callback[i]);
248235
if (ret) {
249236
return ret;
250237
}
251238

252-
ret = gpio_pin_interrupt_configure(gpio_53_dev, btn_cfg[i].btn_pin,
239+
ret = gpio_pin_interrupt_configure(button->gpio.port, button->gpio.pin,
253240
GPIO_INT_EDGE_TO_INACTIVE);
254241
if (ret) {
255242
return ret;
@@ -265,8 +252,9 @@ static int cmd_print_all_btns(const struct shell *shell, size_t argc, char **arg
265252
ARG_UNUSED(argc);
266253
ARG_UNUSED(argv);
267254

268-
for (uint8_t i = 0; i < ARRAY_SIZE(btn_cfg); i++) {
269-
shell_print(shell, "Id %d: pin: %d %s", i, btn_cfg[i].btn_pin, btn_cfg[i].btn_name);
255+
for (uint8_t i = 0; i < btn_config.num_buttons; i++) {
256+
shell_print(shell, "Id %d: pin: %d %s", i, btn_config.buttons[i].gpio.pin,
257+
btn_config.buttons[i].name);
270258
}
271259

272260
return 0;
@@ -291,21 +279,21 @@ static int cmd_push_btn(const struct shell *shell, size_t argc, char **argv)
291279

292280
btn_idx = strtoul(argv[1], NULL, BASE_10);
293281

294-
if (btn_idx >= ARRAY_SIZE(btn_cfg)) {
282+
if (btn_idx >= ARRAY_SIZE(btn_config_data)) {
295283
shell_error(shell, "Selected button ID out of range");
296284
return -EINVAL;
297285
}
298286

299-
msg.button_pin = btn_cfg[btn_idx].btn_pin;
287+
msg.button_pin = btn_config.buttons[btn_idx].gpio.pin;
300288
msg.button_action = BUTTON_PRESS;
301289

302290
ret = zbus_chan_pub(&button_chan, &msg, K_NO_WAIT);
303291
if (ret) {
304292
LOG_ERR("Failed to publish button msg, ret: %d", ret);
305293
}
306294

307-
shell_print(shell, "Pushed button idx: %d pin: %d : %s", btn_idx, btn_cfg[btn_idx].btn_pin,
308-
btn_cfg[btn_idx].btn_name);
295+
shell_print(shell, "Pushed button idx: %d pin: %d : %s", btn_idx,
296+
btn_config.buttons[btn_idx].gpio.pin, btn_config.buttons[btn_idx].name);
309297

310298
return 0;
311299
}

applications/nrf5340_audio/src/modules/button_handler.h

-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@
1010
#include <stdint.h>
1111
#include <zephyr/drivers/gpio.h>
1212

13-
struct btn_config {
14-
const char *btn_name;
15-
uint8_t btn_pin;
16-
uint32_t btn_cfg_mask;
17-
};
18-
1913
/** @brief Initialize button handler, with buttons defined in button_assignments.h.
2014
*
2115
* @note This function may only be called once - there is no reinitialize.

applications/nrf5340_audio/src/modules/led_assignments.h

+3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
*/
66

7+
<<<<<<< HEAD
78
/** @file
89
* @brief LED assignments
910
*
1011
* LED mappings are listed here.
1112
*/
1213

14+
=======
15+
>>>>>>> 1fbc5f5ef0 (applications: nrf5340_audio: LED module for kits with other LED setup)
1316
#ifndef _LED_ASSIGNMENTS_H_
1417
#define _LED_ASSIGNMENTS_H_
1518

applications/nrf5340_audio/src/utils/nrf5340_dk_init.h

+3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
*/
66

7+
<<<<<<< HEAD
78
/** @file
89
* @brief Board initialiszation function
910
*
1011
*/
1112

13+
=======
14+
>>>>>>> 1fbc5f5ef0 (applications: nrf5340_audio: LED module for kits with other LED setup)
1215
#ifndef _NRF5340_BOARD_H_
1316
#define _NRF5340_BOARD_H_
1417

applications/nrf5340_audio/src/utils/nrf5340_dk_version.h

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
*/
66

7+
<<<<<<< HEAD
78
/** @file
89
* @brief nRF5340 Audio DK specific functions
910
*
@@ -13,6 +14,11 @@
1314
#ifndef _NRF5340_BOARD_VERSION_H_
1415
#define _NRF5340_BOARD_VERSION_H_
1516

17+
=======
18+
#ifndef _NRF5340_BOARD_VERSION_H_
19+
#define _NRF5340_BOARD_VERSION_H_
20+
21+
>>>>>>> 1fbc5f5ef0 (applications: nrf5340_audio: LED module for kits with other LED setup)
1622
#include "nrf5340_dk.h"
1723

1824
/**@brief Get the board/HW version

0 commit comments

Comments
 (0)