Skip to content

Commit 9c7742c

Browse files
committed
Enhance GPIO hogs for application control and shell commands
Related to #55617 Enhance the GPIO hogs driver to allow application control over GPIO configuration and add a shell command for managing GPIOs. * Add `gpio_hogs_configure()` function in `drivers/gpio/gpio_hogs.c` to allow application-controlled GPIO configuration. * Implement logic to filter GPIOs based on the provided port and mask in `gpio_hogs_configure()`. * Modify `gpio_hogs_init()` in `drivers/gpio/gpio_hogs.c` to check the `GPIO_HOGS_INITIALIZE_BY_APPLICATION` Kconfig option. * Update shell commands in `drivers/gpio/gpio_shell.c` to use the `line-names` property for referencing GPIOs by their names. * Add tab completion for GPIO names in the shell commands in `drivers/gpio/gpio_shell.c`. * Add a new Kconfig option `GPIO_HOGS_INITIALIZE_BY_APPLICATION` in `drivers/gpio/Kconfig` to enable application-controlled GPIO initialization. Signed-off-by: Vishwanath Martur <[email protected]>
1 parent c50777a commit 9c7742c

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

drivers/gpio/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ config GPIO_HOGS_INIT_PRIORITY
8585
GPIO hogs initialization priority. GPIO hogs must be initialized after the
8686
GPIO controller drivers.
8787

88+
config GPIO_HOGS_INITIALIZE_BY_APPLICATION
89+
bool "Application-controlled GPIO hog initialization"
90+
default n
91+
depends on GPIO_HOGS
92+
help
93+
Enable application-controlled GPIO hog initialization. When enabled,
94+
the application must call gpio_hogs_configure() to initialize GPIO hogs.
95+
8896
config GPIO_ENABLE_DISABLE_INTERRUPT
8997
bool "Support for enable/disable interrupt without re-config [EXPERIMENTAL]"
9098
select EXPERIMENTAL

drivers/gpio/gpio_hogs.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,21 @@ static const struct gpio_hogs gpio_hogs[] = {
9090
DT_FOREACH_STATUS_OKAY_NODE(GPIO_HOGS_COND_INIT)
9191
};
9292

93-
static int gpio_hogs_init(void)
93+
static int gpio_hogs_configure(const struct device *port, gpio_flags_t mask)
9494
{
9595
const struct gpio_hogs *hogs;
9696
const struct gpio_hog_dt_spec *spec;
9797
int err;
9898
int i;
9999
int j;
100100

101-
102101
for (i = 0; i < ARRAY_SIZE(gpio_hogs); i++) {
103102
hogs = &gpio_hogs[i];
104103

104+
if (port != NULL && hogs->port != port) {
105+
continue;
106+
}
107+
105108
if (!device_is_ready(hogs->port)) {
106109
LOG_ERR("GPIO port %s not ready", hogs->port->name);
107110
return -ENODEV;
@@ -110,7 +113,7 @@ static int gpio_hogs_init(void)
110113
for (j = 0; j < hogs->num_specs; j++) {
111114
spec = &hogs->specs[j];
112115

113-
err = gpio_pin_configure(hogs->port, spec->pin, spec->flags);
116+
err = gpio_pin_configure(hogs->port, spec->pin, spec->flags & mask);
114117
if (err < 0) {
115118
LOG_ERR("failed to configure GPIO hog for port %s pin %u (err %d)",
116119
hogs->port->name, spec->pin, err);
@@ -122,4 +125,13 @@ static int gpio_hogs_init(void)
122125
return 0;
123126
}
124127

128+
static int gpio_hogs_init(void)
129+
{
130+
#if !defined(CONFIG_GPIO_HOGS_INITIALIZE_BY_APPLICATION)
131+
return gpio_hogs_configure(NULL, GPIO_FLAGS_ALL);
132+
#else
133+
return 0;
134+
#endif
135+
}
136+
125137
SYS_INIT(gpio_hogs_init, POST_KERNEL, CONFIG_GPIO_HOGS_INIT_PRIORITY);

0 commit comments

Comments
 (0)