Skip to content

Commit ae32455

Browse files
jema-nordicnordic-piks
authored andcommitted
tests: samples: bluetooth: io_adapter: Refactor GPIO pin configuration
- Added macros for enabling and disabling GPIO pins. - Removed redundant initial configuration code from the main function. Signed-off-by: Jerzy Marecik <[email protected]>
1 parent 2fdf6a6 commit ae32455

File tree

1 file changed

+36
-44
lines changed
  • tests/samples/bluetooth/io_adapter/src

1 file changed

+36
-44
lines changed

tests/samples/bluetooth/io_adapter/src/main.c

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ LOG_MODULE_REGISTER(main, CONFIG_LOG_DEFAULT_LEVEL);
2929
#define PRINT_STATUS(status, fmt, ...) \
3030
shell_print(shell, "{\"status\": \"%s\", \"msg\": \"" fmt "\"}", status, ##__VA_ARGS__)
3131

32+
#define DISABLE_PIN(gpio, pin) gpio_pin_configure(gpio, pin, GPIO_DISCONNECTED)
33+
#define ENABLE_BUTTON_PIN(gpio, pin) gpio_pin_configure(gpio, pin, GPIO_OUTPUT_INACTIVE)
34+
#define ENABLE_LED_PIN(gpio, pin) gpio_pin_configure(gpio, pin, GPIO_INPUT)
35+
3236
/* IO Adapter channel to port/pin mapping */
3337
typedef struct {
3438
uint8_t gpio_port;
@@ -133,7 +137,7 @@ static int cmd_button_push(const struct shell *shell, size_t argc, char **argv)
133137

134138
for (int i = 0; i < repeats; i++) {
135139
/* Press button (set to LOW) */
136-
int ret = gpio_pin_set(gpio, btn->gpio_pin, 0);
140+
int ret = ENABLE_BUTTON_PIN(gpio, btn->gpio_pin);
137141

138142
if (ret < 0) {
139143
PRINT_STATUS("error", "Failed to press button on port %d pin %d",
@@ -144,8 +148,8 @@ static int cmd_button_push(const struct shell *shell, size_t argc, char **argv)
144148
/* Wait for specified time */
145149
k_sleep(K_MSEC(time_ms));
146150

147-
/* Release button (set to HIGH) */
148-
ret = gpio_pin_set(gpio, btn->gpio_pin, 1);
151+
/* Release button (disconnect pin) */
152+
ret = DISABLE_PIN(gpio, btn->gpio_pin);
149153
if (ret < 0) {
150154
PRINT_STATUS("error", "Failed to release button on port %d pin %d",
151155
btn->gpio_port, btn->gpio_pin);
@@ -201,7 +205,7 @@ static int cmd_button_hold(const struct shell *shell, size_t argc, char **argv)
201205
}
202206

203207
/* Press button (set to LOW) */
204-
int ret = gpio_pin_set(gpio, btn->gpio_pin, 0);
208+
int ret = ENABLE_BUTTON_PIN(gpio, btn->gpio_pin);
205209

206210
if (ret < 0) {
207211
PRINT_STATUS("error", "Failed to press button on port %d pin %d",
@@ -250,8 +254,8 @@ static int cmd_button_release(const struct shell *shell, size_t argc, char **arg
250254
return -ENODEV;
251255
}
252256

253-
/* Release button (set to HIGH) */
254-
int ret = gpio_pin_set(gpio, btn->gpio_pin, 1);
257+
/* Release button (disconnect pin) */
258+
int ret = DISABLE_PIN(gpio, btn->gpio_pin);
255259

256260
if (ret < 0) {
257261
PRINT_STATUS("error", "Failed to release button on port %d pin %d",
@@ -303,14 +307,26 @@ static int cmd_led_state(const struct shell *shell, size_t argc, char **argv)
303307
}
304308

305309
int state;
306-
int ret = gpio_pin_get(gpio, led_map_entry->gpio_pin);
310+
int ret = ENABLE_LED_PIN(gpio, led_map_entry->gpio_pin);
311+
312+
if (ret < 0) {
313+
PRINT_STATUS("error", "Failed to enable LED pin");
314+
return ret;
315+
}
307316

317+
ret = gpio_pin_get(gpio, led_map_entry->gpio_pin);
308318
if (ret < 0) {
309319
PRINT_STATUS("error", "Failed to read LED state");
310320
return ret;
311321
}
312322
state = ret;
313323

324+
ret = DISABLE_PIN(gpio, led_map_entry->gpio_pin);
325+
if (ret < 0) {
326+
PRINT_STATUS("error", "Failed to disable LED pin");
327+
return ret;
328+
}
329+
314330
shell_print(shell, "{\"status\": \"success\", "
315331
"\"result\": \"%s\", "
316332
"\"msg\": \"LED %d %s (channel: %d, gpio: %d_%d)\"}",
@@ -404,6 +420,13 @@ static int cmd_blink(const struct shell *shell, size_t argc, char **argv)
404420
int64_t start_time = k_uptime_get();
405421
int64_t end_time = start_time + time_ms;
406422

423+
int ret = ENABLE_LED_PIN(gpio, led_map_entry->gpio_pin);
424+
425+
if (ret < 0) {
426+
PRINT_STATUS("error", "Failed to enable LED pin");
427+
return ret;
428+
}
429+
407430
while (k_uptime_get() < end_time) {
408431
int current_state = gpio_pin_get(gpio, led_map_entry->gpio_pin);
409432

@@ -421,6 +444,12 @@ static int cmd_blink(const struct shell *shell, size_t argc, char **argv)
421444
k_sleep(K_MSEC(5));
422445
}
423446

447+
ret = DISABLE_PIN(gpio, led_map_entry->gpio_pin);
448+
if (ret < 0) {
449+
PRINT_STATUS("error", "Failed to disable LED pin");
450+
return ret;
451+
}
452+
424453
shell_print(shell,
425454
"{\"status\": \"success\", "
426455
"\"result\": \"%d\", "
@@ -484,8 +513,6 @@ SHELL_CMD_REGISTER(blink, NULL,
484513

485514
int main(void)
486515
{
487-
int ret;
488-
489516
/* Initialize GPIO devices */
490517
gpio_devs[0] = DEVICE_DT_GET(DT_NODELABEL(gpio0));
491518
gpio_devs[1] = DEVICE_DT_GET(DT_NODELABEL(gpio1));
@@ -495,40 +522,5 @@ int main(void)
495522
return -ENODEV;
496523
}
497524

498-
/* Configure all button pins as outputs with initial state HIGH (released) */
499-
for (int channel = 0; channel < IOADAPTER_CHANNELS_NUMBER; channel++) {
500-
for (int btn = 0; btn < BUTTONS_NUMBER_PER_CHANNEL; btn++) {
501-
const io_map *btn_map = &button_map[channel][btn];
502-
const struct device *gpio = gpio_devs[btn_map->gpio_port];
503-
504-
ret = gpio_pin_configure(gpio, btn_map->gpio_pin,
505-
GPIO_OUTPUT_ACTIVE | GPIO_PULL_UP);
506-
if (ret < 0) {
507-
LOG_ERR("Error: Failed to configure button pin "
508-
"(channel %d, button %d)",
509-
channel, btn + 1);
510-
return ret;
511-
}
512-
/* Set initial state to HIGH (released) */
513-
gpio_pin_set(gpio, btn_map->gpio_pin, 1);
514-
}
515-
}
516-
517-
/* Configure all LED pins as inputs */
518-
for (int channel = 0; channel < IOADAPTER_CHANNELS_NUMBER; channel++) {
519-
for (int led = 0; led < LEDS_NUMBER_PER_CHANNEL; led++) {
520-
const io_map *led_map_entry = &led_map[channel][led];
521-
const struct device *gpio = gpio_devs[led_map_entry->gpio_port];
522-
523-
ret = gpio_pin_configure(gpio, led_map_entry->gpio_pin,
524-
GPIO_INPUT | GPIO_PULL_UP);
525-
if (ret < 0) {
526-
LOG_ERR("Error: Failed to configure LED pin (channel %d, LED %d)",
527-
channel, led + 1);
528-
return ret;
529-
}
530-
}
531-
}
532-
533525
return 0;
534526
}

0 commit comments

Comments
 (0)