* [PATCH] cmd: gpio: Add `gpio read` subcommand
@ 2022-03-22 17:14 Diego Rondini
2022-04-08 23:43 ` Tom Rini
0 siblings, 1 reply; 4+ messages in thread
From: Diego Rondini @ 2022-03-22 17:14 UTC (permalink / raw)
To: u-boot; +Cc: Diego Rondini
As explained in commit 4af2a33ee5b9 ("cmd: gpio: Make `gpio input`
return pin value again") the `gpio input` is used in scripts to obtain
the value of a pin, despite the fact that CMD_RET_FAILURE is
indistinguishable from a valid pin value.
To be able to detect failures and properly use the value of a GPIO in
scripts we introduce the `gpio read` command that sets the variable
`name` to the value of the pin. Return code of the `gpio read` command
can be used to check for CMD_RET_SUCCESS or CMD_RET_FAILURE.
Signed-off-by: Diego Rondini <diego.rondini@kynetics.com>
---
cmd/gpio.c | 24 ++++++++++++++++++++----
test/py/tests/test_gpio.py | 15 +++++++++++++++
2 files changed, 35 insertions(+), 4 deletions(-)
diff --git a/cmd/gpio.c b/cmd/gpio.c
index 4150024e628..150c77cbcf9 100644
--- a/cmd/gpio.c
+++ b/cmd/gpio.c
@@ -12,6 +12,7 @@
#include <dm.h>
#include <log.h>
#include <malloc.h>
+#include <env.h>
#include <asm/gpio.h>
#include <linux/err.h>
@@ -25,6 +26,7 @@ enum gpio_cmd {
GPIOC_SET,
GPIOC_CLEAR,
GPIOC_TOGGLE,
+ GPIOC_READ,
};
#if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
@@ -124,7 +126,7 @@ static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,
unsigned int gpio;
enum gpio_cmd sub_cmd;
int value;
- const char *str_cmd, *str_gpio = NULL;
+ const char *str_cmd, *str_gpio, *str_var = NULL;
int ret;
#ifdef CONFIG_DM_GPIO
bool all = false;
@@ -137,12 +139,19 @@ static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,
argc -= 2;
argv += 2;
#ifdef CONFIG_DM_GPIO
- if (argc > 0 && !strcmp(*argv, "-a")) {
+ if (argc > 0 && !strncmp(str_cmd, "status", 2) && !strcmp(*argv, "-a")) {
all = true;
argc--;
argv++;
}
#endif
+ if (argc > 0 && !strncmp(str_cmd, "read", 2)) {
+ if (argc < 2)
+ goto show_usage;
+ str_var = *argv;
+ argc--;
+ argv++;
+ }
if (argc > 0)
str_gpio = *argv;
if (!strncmp(str_cmd, "status", 2)) {
@@ -174,6 +183,9 @@ static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,
case 't':
sub_cmd = GPIOC_TOGGLE;
break;
+ case 'r':
+ sub_cmd = GPIOC_READ;
+ break;
default:
goto show_usage;
}
@@ -205,7 +217,7 @@ static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,
}
/* finally, let's do it: set direction and exec command */
- if (sub_cmd == GPIOC_INPUT) {
+ if (sub_cmd == GPIOC_INPUT || sub_cmd == GPIOC_READ) {
gpio_direction_input(gpio);
value = gpio_get_value(gpio);
} else {
@@ -233,9 +245,11 @@ static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,
goto err;
} else {
printf("%d\n", value);
+ if (sub_cmd == GPIOC_READ)
+ env_set_ulong(str_var, (ulong)value);
}
- if (sub_cmd != GPIOC_INPUT && !IS_ERR_VALUE(value)) {
+ if (sub_cmd != GPIOC_INPUT && sub_cmd != GPIOC_READ && !IS_ERR_VALUE(value)) {
int nval = gpio_get_value(gpio);
if (IS_ERR_VALUE(nval)) {
@@ -267,4 +281,6 @@ U_BOOT_CMD(gpio, 4, 0, do_gpio,
"query and control gpio pins",
"<input|set|clear|toggle> <pin>\n"
" - input/set/clear/toggle the specified pin\n"
+ "gpio read <name> <pin>\n"
+ " - set environment variable 'name' to the specified pin\n"
"gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs");
diff --git a/test/py/tests/test_gpio.py b/test/py/tests/test_gpio.py
index 8c64f686b0b..52bc8502d3e 100644
--- a/test/py/tests/test_gpio.py
+++ b/test/py/tests/test_gpio.py
@@ -35,3 +35,18 @@ def test_gpio_exit_statuses(u_boot_console):
assert(expected_response in response)
response = u_boot_console.run_command('gpio input 200; echo rc:$?')
assert(expected_response in response)
+
+@pytest.mark.boardspec('sandbox')
+@pytest.mark.buildconfigspec('cmd_gpio')
+def test_gpio_read(u_boot_console):
+ """Test that gpio read correctly sets the variable to the value of a gpio pin."""
+
+ response = u_boot_console.run_command('gpio read var 0; echo val:$var')
+ expected_response = 'val:0'
+ assert(expected_response in response)
+ response = u_boot_console.run_command('gpio toggle 0; gpio read var 0; echo val:$var')
+ expected_response = 'val:1'
+ assert(expected_response in response)
+ response = u_boot_console.run_command('setenv var; gpio read var nonexistent-gpio; echo val:$var')
+ expected_response = 'val:'
+ assert(expected_response in response)
--
2.34.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] cmd: gpio: Add `gpio read` subcommand
2022-03-22 17:14 [PATCH] cmd: gpio: Add `gpio read` subcommand Diego Rondini
@ 2022-04-08 23:43 ` Tom Rini
2022-04-11 10:02 ` [PATCH v2] " Diego Rondini
0 siblings, 1 reply; 4+ messages in thread
From: Tom Rini @ 2022-04-08 23:43 UTC (permalink / raw)
To: Diego Rondini; +Cc: u-boot
[-- Attachment #1: Type: text/plain, Size: 1004 bytes --]
On Tue, Mar 22, 2022 at 06:14:35PM +0100, Diego Rondini wrote:
> As explained in commit 4af2a33ee5b9 ("cmd: gpio: Make `gpio input`
> return pin value again") the `gpio input` is used in scripts to obtain
> the value of a pin, despite the fact that CMD_RET_FAILURE is
> indistinguishable from a valid pin value.
> To be able to detect failures and properly use the value of a GPIO in
> scripts we introduce the `gpio read` command that sets the variable
> `name` to the value of the pin. Return code of the `gpio read` command
> can be used to check for CMD_RET_SUCCESS or CMD_RET_FAILURE.
>
> Signed-off-by: Diego Rondini <diego.rondini@kynetics.com>
> ---
> cmd/gpio.c | 24 ++++++++++++++++++++----
> test/py/tests/test_gpio.py | 15 +++++++++++++++
> 2 files changed, 35 insertions(+), 4 deletions(-)
Please rework this to add CONFIG_CMD_GPIO_READ, and enable it on sandbox
as it causes a good bit of growth on a large number of platforms,
thanks.
--
Tom
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2] cmd: gpio: Add `gpio read` subcommand
2022-04-08 23:43 ` Tom Rini
@ 2022-04-11 10:02 ` Diego Rondini
2022-04-20 19:24 ` Tom Rini
0 siblings, 1 reply; 4+ messages in thread
From: Diego Rondini @ 2022-04-11 10:02 UTC (permalink / raw)
To: u-boot; +Cc: Tom Rini, Diego Rondini
As explained in commit 4af2a33ee5b9 ("cmd: gpio: Make `gpio input`
return pin value again") the `gpio input` is used in scripts to obtain
the value of a pin, despite the fact that CMD_RET_FAILURE is
indistinguishable from a valid pin value.
To be able to detect failures and properly use the value of a GPIO in
scripts we introduce the `gpio read` command that sets the variable
`name` to the value of the pin. Return code of the `gpio read` command
can be used to check for CMD_RET_SUCCESS or CMD_RET_FAILURE.
CONFIG_CMD_GPIO_READ is used to enable the `gpio read` command.
Signed-off-by: Diego Rondini <diego.rondini@kynetics.com>
---
V2:
- enable only when CONFIG_CMD_GPIO_READ is set
- in test_gpio.py check for return code as well
---
cmd/Kconfig | 7 ++++++
cmd/gpio.c | 45 +++++++++++++++++++++++++++++++++++---
configs/sandbox_defconfig | 1 +
test/py/tests/test_gpio.py | 14 ++++++++++++
4 files changed, 64 insertions(+), 3 deletions(-)
diff --git a/cmd/Kconfig b/cmd/Kconfig
index d3abe3a06b..f580797d2d 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -996,6 +996,13 @@ config CMD_GPIO
help
GPIO support.
+config CMD_GPIO_READ
+ bool "gpio read - save GPIO value to variable"
+ depends on CMD_GPIO
+ help
+ Enables the 'gpio read' command that saves the value
+ of a GPIO pin to a variable.
+
config CMD_PWM
bool "pwm"
depends on DM_PWM
diff --git a/cmd/gpio.c b/cmd/gpio.c
index 4150024e62..53e9ce666f 100644
--- a/cmd/gpio.c
+++ b/cmd/gpio.c
@@ -12,6 +12,9 @@
#include <dm.h>
#include <log.h>
#include <malloc.h>
+#ifdef CONFIG_CMD_GPIO_READ
+#include <env.h>
+#endif
#include <asm/gpio.h>
#include <linux/err.h>
@@ -25,6 +28,9 @@ enum gpio_cmd {
GPIOC_SET,
GPIOC_CLEAR,
GPIOC_TOGGLE,
+#ifdef CONFIG_CMD_GPIO_READ
+ GPIOC_READ,
+#endif
};
#if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
@@ -125,6 +131,9 @@ static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,
enum gpio_cmd sub_cmd;
int value;
const char *str_cmd, *str_gpio = NULL;
+#ifdef CONFIG_CMD_GPIO_READ
+ const char *str_var = NULL;
+#endif
int ret;
#ifdef CONFIG_DM_GPIO
bool all = false;
@@ -137,11 +146,20 @@ static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,
argc -= 2;
argv += 2;
#ifdef CONFIG_DM_GPIO
- if (argc > 0 && !strcmp(*argv, "-a")) {
+ if (argc > 0 && !strncmp(str_cmd, "status", 2) && !strcmp(*argv, "-a")) {
all = true;
argc--;
argv++;
}
+#endif
+#ifdef CONFIG_CMD_GPIO_READ
+ if (argc > 0 && !strncmp(str_cmd, "read", 2)) {
+ if (argc < 2)
+ goto show_usage;
+ str_var = *argv;
+ argc--;
+ argv++;
+ }
#endif
if (argc > 0)
str_gpio = *argv;
@@ -174,6 +192,11 @@ static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,
case 't':
sub_cmd = GPIOC_TOGGLE;
break;
+#ifdef CONFIG_CMD_GPIO_READ
+ case 'r':
+ sub_cmd = GPIOC_READ;
+ break;
+#endif
default:
goto show_usage;
}
@@ -205,7 +228,11 @@ static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,
}
/* finally, let's do it: set direction and exec command */
- if (sub_cmd == GPIOC_INPUT) {
+ if (sub_cmd == GPIOC_INPUT
+#ifdef CONFIG_CMD_GPIO_READ
+ || sub_cmd == GPIOC_READ
+#endif
+ ) {
gpio_direction_input(gpio);
value = gpio_get_value(gpio);
} else {
@@ -233,9 +260,17 @@ static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,
goto err;
} else {
printf("%d\n", value);
+#ifdef CONFIG_CMD_GPIO_READ
+ if (sub_cmd == GPIOC_READ)
+ env_set_ulong(str_var, (ulong)value);
+#endif
}
- if (sub_cmd != GPIOC_INPUT && !IS_ERR_VALUE(value)) {
+ if (sub_cmd != GPIOC_INPUT && !IS_ERR_VALUE(value)
+#ifdef CONFIG_CMD_GPIO_READ
+ && sub_cmd != GPIOC_READ
+#endif
+ ) {
int nval = gpio_get_value(gpio);
if (IS_ERR_VALUE(nval)) {
@@ -267,4 +302,8 @@ U_BOOT_CMD(gpio, 4, 0, do_gpio,
"query and control gpio pins",
"<input|set|clear|toggle> <pin>\n"
" - input/set/clear/toggle the specified pin\n"
+#ifdef CONFIG_CMD_GPIO_READ
+ "gpio read <name> <pin>\n"
+ " - set environment variable 'name' to the specified pin\n"
+#endif
"gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs");
diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig
index cb8d590eb6..a65527003d 100644
--- a/configs/sandbox_defconfig
+++ b/configs/sandbox_defconfig
@@ -61,6 +61,7 @@ CONFIG_CMD_UNZIP=y
CONFIG_CMD_BIND=y
CONFIG_CMD_DEMO=y
CONFIG_CMD_GPIO=y
+CONFIG_CMD_GPIO_READ=y
CONFIG_CMD_PWM=y
CONFIG_CMD_GPT=y
CONFIG_CMD_GPT_RENAME=y
diff --git a/test/py/tests/test_gpio.py b/test/py/tests/test_gpio.py
index 109649e2c7..fa0af5f82b 100644
--- a/test/py/tests/test_gpio.py
+++ b/test/py/tests/test_gpio.py
@@ -46,6 +46,20 @@ def test_gpio_exit_statuses(u_boot_console):
response = u_boot_console.run_command('gpio input 200; echo rc:$?')
assert(expected_response in response)
+@pytest.mark.boardspec('sandbox')
+@pytest.mark.buildconfigspec('cmd_gpio')
+def test_gpio_read(u_boot_console):
+ """Test that gpio read correctly sets the variable to the value of a gpio pin."""
+
+ response = u_boot_console.run_command('gpio read var 0; echo val:$var,rc:$?')
+ expected_response = 'val:0,rc:0'
+ assert(expected_response in response)
+ response = u_boot_console.run_command('gpio toggle 0; gpio read var 0; echo val:$var,rc:$?')
+ expected_response = 'val:1,rc:0'
+ assert(expected_response in response)
+ response = u_boot_console.run_command('setenv var; gpio read var nonexistent-gpio; echo val:$var,rc:$?')
+ expected_response = 'val:,rc:1'
+ assert(expected_response in response)
"""
Generic Tests for 'gpio' command on sandbox and real hardware.
--
2.34.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v2] cmd: gpio: Add `gpio read` subcommand
2022-04-11 10:02 ` [PATCH v2] " Diego Rondini
@ 2022-04-20 19:24 ` Tom Rini
0 siblings, 0 replies; 4+ messages in thread
From: Tom Rini @ 2022-04-20 19:24 UTC (permalink / raw)
To: Diego Rondini; +Cc: u-boot
[-- Attachment #1: Type: text/plain, Size: 782 bytes --]
On Mon, Apr 11, 2022 at 12:02:09PM +0200, Diego Rondini wrote:
> As explained in commit 4af2a33ee5b9 ("cmd: gpio: Make `gpio input`
> return pin value again") the `gpio input` is used in scripts to obtain
> the value of a pin, despite the fact that CMD_RET_FAILURE is
> indistinguishable from a valid pin value.
> To be able to detect failures and properly use the value of a GPIO in
> scripts we introduce the `gpio read` command that sets the variable
> `name` to the value of the pin. Return code of the `gpio read` command
> can be used to check for CMD_RET_SUCCESS or CMD_RET_FAILURE.
> CONFIG_CMD_GPIO_READ is used to enable the `gpio read` command.
>
> Signed-off-by: Diego Rondini <diego.rondini@kynetics.com>
Applied to u-boot/master, thanks!
--
Tom
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-04-20 19:24 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-22 17:14 [PATCH] cmd: gpio: Add `gpio read` subcommand Diego Rondini
2022-04-08 23:43 ` Tom Rini
2022-04-11 10:02 ` [PATCH v2] " Diego Rondini
2022-04-20 19:24 ` Tom Rini
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.