cmd: gpio: Add `gpio read` subcommand
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>
diff --git a/test/py/tests/test_gpio.py b/test/py/tests/test_gpio.py
index 109649e..fa0af5f 100644
--- a/test/py/tests/test_gpio.py
+++ b/test/py/tests/test_gpio.py
@@ -46,6 +46,20 @@
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.