tools/gpio: add gpio util to get or set gpio lines
PATCH 1/1 tools/gpio: add gpio util to get or set gpio lines
Add a new tool to set or get the value of one or more gpio lines, which can as either chip offsets or line names. This makes it easier to control gpio lines from user space without using the cumbersome sysfs interface. It also demostrates how gpio-line-names can be used to decouple software from hardware layout.
--- tools/gpio/.gitignore | 2 +- tools/gpio/Build | 1 + tools/gpio/Makefile | 11 ++- tools/gpio/gpio.c | 261 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 273 insertions(+), 2 deletions(-) create mode 100644 tools/gpio/gpio.c
tools/gpio/.gitignore +1 -1
@@ -1,4 +1,4 @@ gpio-event-mon gpio-hammer lsgpio - +gpio
tools/gpio/Build +1 -0
@@ -1,3 +1,4 @@ +gpio-y += gpio.o gpio-utils.o lsgpio-y += lsgpio.o gpio-utils.o gpio-hammer-y += gpio-hammer.o gpio-utils.o gpio-event-mon-y += gpio-event-mon.o gpio-utils.o
tools/gpio/Makefile +10 -1
@@ -15,7 +15,7 @@ CC = $(CROSS_COMPILE)gcc LD = $(CROSS_COMPILE)ld CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include -ALL_TARGETS := lsgpio gpio-hammer gpio-event-mon +ALL_TARGETS := gpio lsgpio gpio-hammer gpio-event-mon ALL_PROGRAMS := $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS)) all: $(ALL_PROGRAMS)
@@ -33,6 +33,15 @@ $(OUTPUT)include/linux/gpio.h: ../../include/uapi/linux/gpio.h prepare: $(OUTPUT)include/linux/gpio.h # +# gpio +# +GPIO_IN := $(OUTPUT)gpio-in.o +$(GPIO_IN): prepare FORCE + $(Q)$(MAKE) $(build)=gpio +$(OUTPUT)gpio: $(GPIO_IN) + $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@ + +# # lsgpio # LSGPIO_IN := $(OUTPUT)lsgpio-in.o
tools/gpio/gpio.c new file +261 -0
261 lines not on this page · raw message
Signature · 1 line
2.13.3
Quoted 7 lines · Martin Hundebøll
Add a new tool to set or get the value of one or more gpio lines, which can as either chip offsets or line names. This makes it easier to control gpio lines from user space without using the cumbersome sysfs interface. It also demostrates how gpio-line-names can be used to decouple software from hardware layout.
Signed-off-by Martin Hundebøll <[email protected]>
How does this relate to Bartosz userspace library libgpiod? Have you checked it and considered using it?
https://github.com/brgl/libgpiod
Quoted 1 line
tools/gpio/gpio.c | 261 ++++++++++++++++++++++++++++++++++++++++++++++++++
We can't have a tool simply named "gpio" it is too unspecific.
Come up with a better name for this, such as "gpiog-get-set" or so.
Maybe it should be two tools? One fo setting and one for getting a line?
Quoted 10 lines
+ * lsgpio - example on how to list the GPIO lines on a system
+ *
+ * Copyright (C) 2015 Linus Walleij
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published by
+ * the Free Software Foundation.
+ *
+ * Usage:
+ * lsgpio <-n device-name>
There is a lot of copy-paste going on here. Please fix it up.
Quoted 113 lines
tools/gpio/gpio.c new file +113
-0+int print_offsets (const char *device_name,unsigned int *lines,int nlines) +{ +struct gpiohandle_data data; +int i, line, value, ret; + + ret =gpiotools_gets (device_name, lines, nlines, &data); +if (ret) +return ret; + +/* print value for each configured line */ +printf ("GPIO chip: %s\n" , device_name); +for (i =0 ; i < nlines; i++) { + line = lines[i]; + value = data.values[line]; +printf ("\tline %i: %i\n" , lines[i], value); + } + +return ret; +} + +int print_labels (const char *device_name,unsigned int *lines, +const char *labels[],int nlabels) +{ +struct gpiohandle_data data; +int i, value, ret; +const char *label; + + ret =gpiotools_gets (device_name, lines, nlabels, &data); +if (ret) +return ret; + +/* print value for each configured label */ +printf ("GPIO chip: %s\n" , device_name); +for (i =0 ; i < nlabels; i++) { + label = labels[i]; + value = data.values[i]; +printf ("\t%s: %i\n" , label, value); + } + +return ret; +} + +int control_offsets (const char *device_name,unsigned int *lines,int nlines, +int flags,int value) +{ +struct gpiohandle_data data = {{ value }}; + +if (flags &GPIOHANDLE_REQUEST_INPUT ) +return print_offsets (device_name, lines, nlines); +else +return gpiotools_sets (device_name, lines, nlines, &data); +} + +int control_labels (const char *device_name,const char *labels[],int nlabels, +int flags,int value) +{ +struct gpiochip_info cinfo = {0 }; +char *chrdev_name =NULL ; +unsigned int lines[GPIOHANDLES_MAX ]; +int nlines =0 ; +int ret, fd, i, j; + + ret =asprintf (&chrdev_name,"/dev/%s" , device_name); +if (ret <0 ) +return -ENOMEM ; + + fd =open (chrdev_name,0 ); +if (fd ==-1 ) { + ret = -errno; +fprintf (stderr,"Failed to open %s\n" , chrdev_name); + goto exit_error; + } + +/* Inspect this GPIO chip */ + ret =ioctl (fd,GPIO_GET_CHIPINFO_IOCTL , &cinfo); +if (ret ==-1 ) { + ret = -errno; +perror ("Failed to issue CHIPINFO IOCTL\n" ); + goto exit_close_error; + } + +/* Loop over the lines and match info */ +for (i =0 ; i < cinfo.lines; i++) { +struct gpioline_info linfo; + +memset (&linfo,0 ,sizeof (linfo)); + linfo.line_offset = i; + + ret =ioctl (fd,GPIO_GET_LINEINFO_IOCTL , &linfo); +if (ret ==-1 ) { + ret = -errno; +perror ("Failed to issue LINEINFO IOCTL\n" ); + goto exit_close_error; + } + +/* Check if line name is included requested names */ +for (j =0 ; j < nlabels; j++) { +if (strcmp (linfo.name, labels[j]) ==0 ) { + lines[nlines++] = i; +break ; + } + } + } + +if (nlines ==0 ) + goto exit_close_error; + +if (flags &GPIOHANDLE_REQUEST_INPUT ) { +print_labels (device_name, lines, labels, nlabels); + }else { +struct gpiohandle_data data = {{ value }}; + ret =gpiotools_sets (device_name, lines, nlines, &data); + }
tools/gpio/gpio.c line 151
Quoted 10 lines
tools/gpio/gpio.c new file +10
-0+ +exit_close_error: +if (fd >=0 &&close (fd) ==-1 ) +perror ("Failed to close GPIO character device file" ); + +exit_error: +free (chrdev_name); + +return ret; +}
All of this also looks copypasted.
Please go through your program and make something very specific and to the point that does exactly what you want instead of randomly reusing lsgpio.
Yours,
Linus Walleij
Quoted 10 lines · Linus Walleij
Quoted 1 line · Martin Hundebøll
Add a new tool to set or get the value of one or more gpio lines,
which
Quoted 5 lines
can as either chip offsets or line names. This makes it easier to control gpio lines from user space without using the cumbersome sysfs interface. It also demostrates how gpio-line-names can be used to decouple software from hardware layout.
Quoted 5 lines
Quoted 1 line
Signed-off-by Martin Hundebøll <[email protected]>How does this relate to Bartosz userspace library libgpiod? Have you checked it and considered using it?
https://github.com/brgl/libgpiod
I tried with the best of my Google Foo, but "gpio" is a bit too generic. libgpiod is exactly what I need, thanks.
Quoted 10 lines
Quoted 2 lines
tools/gpio/gpio.c | 261++++++++++++++++++++++++++++++++++++++++++++++++++
We can't have a tool simply named "gpio" it is too unspecific.
Come up with a better name for this, such as "gpiog-get-set" or so.
Maybe it should be two tools? One fo setting and one for getting a line?
Correct. I can update the patch into two utils, but with libgpiod in the mix, we can just drop this?
// Martin
Quoted 153 lines
Quoted 6 lines
+ * lsgpio - example on how to list the GPIO lines on a system
+ *
+ * Copyright (C) 2015 Linus Walleij
+ *
+ * This program is free software; you can redistribute it and/ormodify it
Quoted 1 line
+ * under the terms of the GNU General Public License version 2 as
published by
Quoted 4 lines
+ * the Free Software Foundation.
+ *
+ * Usage:
+ * lsgpio <-n device-name>There is a lot of copy-paste going on here. Please fix it up.
Quoted 1 line
+int print_offsets(const char
*device_name, unsigned int *lines, intnlines)
Quoted 32 lines
tools/gpio/gpio.c new file +32
-0+{ +struct gpiohandle_data data; +int i, line, value, ret; + + ret =gpiotools_gets (device_name, lines, nlines, &data); +if (ret) +return ret; + +/* print value for each configured line */ +printf ("GPIO chip: %s\n" , device_name); +for (i =0 ; i < nlines; i++) { + line = lines[i]; + value = data.values[line]; +printf ("\tline %i: %i\n" , lines[i], value); + } + +return ret; +} + +int print_labels (const char *device_name,unsigned int *lines, +const char *labels[],int nlabels) +{ +struct gpiohandle_data data; +int i, value, ret; +const char *label; + + ret =gpiotools_gets (device_name, lines, nlabels, &data); +if (ret) +return ret; + +/* print value for each configured label */ +printf ("GPIO chip: %s\n" , device_name);tools/gpio/gpio.c
Quoted 10 lines
tools/gpio/gpio.c new file +10
-0+for (i =0 ; i < nlabels; i++) { + label = labels[i]; + value = data.values[i]; +printf ("\t%s: %i\n" , label, value); + } + +return ret; +} + +int control_offsets(const char *device_name, unsigned int *lines,int nlines,
tools/gpio/gpio.c
Quoted 8 lines
tools/gpio/gpio.c new file +8
-0+ int flags, int value) +{ +struct gpiohandle_data data = {{ value }}; + +if (flags &GPIOHANDLE_REQUEST_INPUT ) +return print_offsets (device_name, lines, nlines); +else + return gpiotools_sets(device_name, lines, nlines,&data);
tools/gpio/gpio.c
Quoted 3 lines
tools/gpio/gpio.c new file +3
-0+} + +int control_labels(const char *device_name, const char *labels[],int nlabels,
Quoted 48 lines
tools/gpio/gpio.c new file +48
-0+ int flags, int value) +{ +struct gpiochip_info cinfo = {0 }; +char *chrdev_name =NULL ; +unsigned int lines[GPIOHANDLES_MAX ]; +int nlines =0 ; +int ret, fd, i, j; + + ret =asprintf (&chrdev_name,"/dev/%s" , device_name); +if (ret <0 ) +return -ENOMEM ; + + fd =open (chrdev_name,0 ); +if (fd ==-1 ) { + ret = -errno; +fprintf (stderr,"Failed to open %s\n" , chrdev_name); + goto exit_error; + } + +/* Inspect this GPIO chip */ + ret =ioctl (fd,GPIO_GET_CHIPINFO_IOCTL , &cinfo); +if (ret ==-1 ) { + ret = -errno; +perror ("Failed to issue CHIPINFO IOCTL\n" ); + goto exit_close_error; + } + +/* Loop over the lines and match info */ +for (i =0 ; i < cinfo.lines; i++) { +struct gpioline_info linfo; + +memset (&linfo,0 ,sizeof (linfo)); + linfo.line_offset = i; + + ret =ioctl (fd,GPIO_GET_LINEINFO_IOCTL , &linfo); +if (ret ==-1 ) { + ret = -errno; +perror ("Failed to issue LINEINFO IOCTL\n" ); + goto exit_close_error; + } + +/* Check if line name is included requested names */ +for (j =0 ; j < nlabels; j++) { +if (strcmp (linfo.name, labels[j]) ==0 ) { + lines[nlines++] = i; +break ; + } + }tools/gpio/gpio.c
Quoted 10 lines
tools/gpio/gpio.c new file +10
-0+ } + +if (nlines ==0 ) + goto exit_close_error; + +if (flags &GPIOHANDLE_REQUEST_INPUT ) { +print_labels (device_name, lines, labels, nlabels); + }else { +struct gpiohandle_data data = {{ value }}; + ret = gpiotools_sets(device_name, lines, nlines,&data);
tools/gpio/gpio.c line 151
Quoted 11 lines
tools/gpio/gpio.c new file +11
-0+ } + +exit_close_error: +if (fd >=0 &&close (fd) ==-1 ) +perror ("Failed to close GPIO character device file" ); + +exit_error: +free (chrdev_name); + +return ret; +}All of this also looks copypasted.
Please go through your program and make something very specific and to the point that does exactly what you want instead of randomly reusing lsgpio.
Yours,
Linus Walleij
Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
Quoted 4 lines · Martin Hundebøll
Quoted 2 lines
Maybe it should be two tools? One fo setting and one for getting a line?
Correct. I can update the patch into two utils, but with libgpiod in the mix, we can just drop this?
I would like Bartosz library to be used widely so let's drop this.
Yours,
Linus Walleij
Quoted 9 lines · Linus Walleij
Quoted 4 lines · Martin Hundebøll
Quoted 2 lines
Maybe it should be two tools? One fo setting and one for getting a line?
Correct. I can update the patch into two utils, but with libgpiod in the mix, we can just drop this?
I would like Bartosz library to be used widely so let's drop this.
Would you accept a patch updating the documentation with a hint about tools/gpio and libgpiod?
Signature · 8 lines
MARTIN HUNDEBØLL, Prevas A/S Software Developer Hedeager 3, DK-8200 Aarhus N Phone +45 87438070 Mobile +45 25562438 [email protected] www.prevas.com
Quoted 2 lines · Martin Hundebøll
Would you accept a patch updating the documentation with a hint about
tools/gpioand libgpiod?
Easily, it would be much appreciated!
Yours,
Linus Walleij