@@ -29,6 +29,10 @@ LOG_MODULE_REGISTER(main, CONFIG_LOG_DEFAULT_LEVEL);
29
29
#define PRINT_STATUS (status , fmt , ...) \
30
30
shell_print(shell, "{\"status\": \"%s\", \"msg\": \"" fmt "\"}", status, ##__VA_ARGS__)
31
31
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
+
32
36
/* IO Adapter channel to port/pin mapping */
33
37
typedef struct {
34
38
uint8_t gpio_port ;
@@ -133,7 +137,7 @@ static int cmd_button_push(const struct shell *shell, size_t argc, char **argv)
133
137
134
138
for (int i = 0 ; i < repeats ; i ++ ) {
135
139
/* 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 );
137
141
138
142
if (ret < 0 ) {
139
143
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)
144
148
/* Wait for specified time */
145
149
k_sleep (K_MSEC (time_ms ));
146
150
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 );
149
153
if (ret < 0 ) {
150
154
PRINT_STATUS ("error" , "Failed to release button on port %d pin %d" ,
151
155
btn -> gpio_port , btn -> gpio_pin );
@@ -201,7 +205,7 @@ static int cmd_button_hold(const struct shell *shell, size_t argc, char **argv)
201
205
}
202
206
203
207
/* 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 );
205
209
206
210
if (ret < 0 ) {
207
211
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
250
254
return - ENODEV ;
251
255
}
252
256
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 );
255
259
256
260
if (ret < 0 ) {
257
261
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)
303
307
}
304
308
305
309
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
+ }
307
316
317
+ ret = gpio_pin_get (gpio , led_map_entry -> gpio_pin );
308
318
if (ret < 0 ) {
309
319
PRINT_STATUS ("error" , "Failed to read LED state" );
310
320
return ret ;
311
321
}
312
322
state = ret ;
313
323
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
+
314
330
shell_print (shell , "{\"status\": \"success\", "
315
331
"\"result\": \"%s\", "
316
332
"\"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)
404
420
int64_t start_time = k_uptime_get ();
405
421
int64_t end_time = start_time + time_ms ;
406
422
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
+
407
430
while (k_uptime_get () < end_time ) {
408
431
int current_state = gpio_pin_get (gpio , led_map_entry -> gpio_pin );
409
432
@@ -421,6 +444,12 @@ static int cmd_blink(const struct shell *shell, size_t argc, char **argv)
421
444
k_sleep (K_MSEC (5 ));
422
445
}
423
446
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
+
424
453
shell_print (shell ,
425
454
"{\"status\": \"success\", "
426
455
"\"result\": \"%d\", "
@@ -484,8 +513,6 @@ SHELL_CMD_REGISTER(blink, NULL,
484
513
485
514
int main (void )
486
515
{
487
- int ret ;
488
-
489
516
/* Initialize GPIO devices */
490
517
gpio_devs [0 ] = DEVICE_DT_GET (DT_NODELABEL (gpio0 ));
491
518
gpio_devs [1 ] = DEVICE_DT_GET (DT_NODELABEL (gpio1 ));
@@ -495,40 +522,5 @@ int main(void)
495
522
return - ENODEV ;
496
523
}
497
524
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
-
533
525
return 0 ;
534
526
}
0 commit comments