From: Kent Gibson <warthog618@gmail.com>
To: andy pugh <bodgesoc@gmail.com>
Cc: linux-gpio@vger.kernel.org
Subject: Re: [libgpiod] gpiod_line_get_value_bulk may be broken?
Date: Fri, 28 Jul 2023 04:53:30 +0800 [thread overview]
Message-ID: <ZMLZSqRwrPfKEbX3@sol> (raw)
In-Reply-To: <CAN1+YZU95GyPdE0fDcQNweMCHq0FXQ5RxR5dqmKdmMUaoiMsfw@mail.gmail.com>
On Thu, Jul 27, 2023 at 04:14:38PM +0100, andy pugh wrote:
> I am using v1.2 on a Raspberry Pi under Buster (as that is the
> installed version)
>
> However, the code appears to be the same in v1.6.
>
> As far as I can see gpiod_line_get_value_bulk() always fails if the
> bulk contains more than one line. The problem(I think) is that
> gpiod_line_same_chip() always returns -EINVAL
>
> Test code output:
>
> Chips line 0 0x3f1280 gpiochip0
> Chips line 1 0x3f2af0 gpiochip0
> Chips line 2 0x3f4350 gpiochip0
>
> ie, all the same chip _name_ but the result of gpiod_line_get_chip (if
> displayed as a pointer with %p) differs.
>
> Test code
>
> ````
> #include <gpiod.h>
> #include <stdio.h>
> #include <unistd.h>
> #include <assert.h>
> #include <errno.h>
> #include <string.h>
>
> int main(int argc, char **argv)
> {
> struct gpiod_chip *chip;
> struct gpiod_line *line0, *line1, *line2;
> struct gpiod_line_bulk bulk;
> int retval;
> int val[4] = {0};
>
> // Open GPIO chip
> chip = gpiod_chip_open_by_name("gpiochip0");
>
> // Open GPIO lines
> line0 = gpiod_line_find("GPIO17");
> line1 = gpiod_line_find("GPIO18");
> line2 = gpiod_line_find("GPIO19");
>
> gpiod_line_request_input(line0, "test");
> gpiod_line_request_input(line1, "test");
> gpiod_line_request_input(line2, "test");
>
> gpiod_line_bulk_init(&bulk);
> gpiod_line_bulk_add(&bulk, line0);
> gpiod_line_bulk_add(&bulk, line1);
> gpiod_line_bulk_add(&bulk, line2);
>
> // Compare the return value of gpiod_line_get_chip() for each line
> printf("Chips line 0 %p %s\n", gpiod_line_get_chip(line0),
> gpiod_chip_name(gpiod_line_get_chip(line0)));
> printf("Chips line 1 %p %s\n", gpiod_line_get_chip(line1),
> gpiod_chip_name(gpiod_line_get_chip(line1)));
> printf("Chips line 2 %p %s\n", gpiod_line_get_chip(line2),
> gpiod_chip_name(gpiod_line_get_chip(line2)));
>
> // try to read the IO lines
> retval = gpiod_line_get_value_bulk(&bulk, val);
> assert (retval == 0);
>
That is not how the line_bulk API is used.
You don't request the lines separately and then add them to the bulk,
you add them to the bulk then request them with
gpiod_line_request_bulk_input(), or one of the other
gpiod_line_request_bulk_XXX() functions.
e.g.
gpiod_line_bulk_init(&bulk);
gpiod_line_bulk_add(&bulk, line0);
gpiod_line_bulk_add(&bulk, line1);
gpiod_line_bulk_add(&bulk, line2);
gpiod_line_request_bulk_input(&bulk, "test");
// try to read the IO lines
retval = gpiod_line_get_value_bulk(&bulk, val);
assert (retval == 0);
Refer to the test cases in libgpiod/tests/tests-line.c.
e.g. the request_bulk_output test case.
Btw, the primary use case for the bulk is for when you need to perform
operations on a set of lines as simultaneously as possible. The
downside is that they have to be requested at the same time.
If you don't require the simultaneity, it may be simpler to request them
separately, and operate on them separately.
You might also want to consider looking at libgpiod v2, which is the
latest release and has a new API that is hopefully less confusing.
The get_multiple_line_values[1] example is similar to what you are doing
in your example. You can use gpiod_chip_get_line_offset_from_name() to
perform the name to offset mapping, if necessary.
OTOH, libgpiod v2 hasn't made its way to many distros yet, and will never
get to Raspbian Buster, so you would need to download and build it
yourself, and your kernel may not even support it (libgpiod v2 requires
kernel 5.10 or later).
Cheers,
Kent.
[1] https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/examples/get_multiple_line_values.c
next prev parent reply other threads:[~2023-07-27 20:53 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-27 15:14 [libgpiod] gpiod_line_get_value_bulk may be broken? andy pugh
2023-07-27 20:53 ` Kent Gibson [this message]
2023-07-27 21:17 ` andy pugh
2023-07-27 21:55 ` Kent Gibson
2023-07-27 22:10 ` andy pugh
2023-07-27 22:36 ` Kent Gibson
2023-07-28 0:39 ` andy pugh
2023-07-28 1:07 ` andy pugh
2023-07-28 5:57 ` Kent Gibson
2023-07-28 19:01 ` andy pugh
2023-07-29 2:03 ` Kent Gibson
2023-08-05 22:55 ` andy pugh
2023-08-06 1:02 ` Kent Gibson
2023-08-06 9:13 ` andy pugh
2023-08-06 9:29 ` Kent Gibson
2023-08-10 0:17 ` andy pugh
2023-08-10 0:46 ` Kent Gibson
2023-08-10 22:07 ` andy pugh
2023-08-11 0:59 ` Kent Gibson
2023-08-11 1:26 ` andy pugh
2023-08-11 1:36 ` Kent Gibson
2023-08-14 22:25 ` How to use gpiod_line_set_flags andy pugh
2023-08-15 0:49 ` Kent Gibson
2023-08-15 18:03 ` andy pugh
2023-08-11 12:19 ` [libgpiod] gpiod_line_get_value_bulk may be broken? Bartosz Golaszewski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZMLZSqRwrPfKEbX3@sol \
--to=warthog618@gmail.com \
--cc=bodgesoc@gmail.com \
--cc=linux-gpio@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).