#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>
GPIOC_SET,
GPIOC_CLEAR,
GPIOC_TOGGLE,
+#ifdef CONFIG_CMD_GPIO_READ
+ GPIOC_READ,
+#endif
};
#if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
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;
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;
case 't':
sub_cmd = GPIOC_TOGGLE;
break;
+#ifdef CONFIG_CMD_GPIO_READ
+ case 'r':
+ sub_cmd = GPIOC_READ;
+ break;
+#endif
default:
goto show_usage;
}
}
/* 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 {
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)) {
"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");
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.