* [libgpiod] Either a bug or a misunderstanding regarding bulk operations
@ 2022-09-29 13:13 Mark Locascio
2022-09-30 2:53 ` Kent Gibson
0 siblings, 1 reply; 2+ messages in thread
From: Mark Locascio @ 2022-09-29 13:13 UTC (permalink / raw)
To: linux-gpio
[-- Attachment #1: Type: text/plain, Size: 2597 bytes --]
Hi,
I was working with libgpiod and I noticed some unintuitive behavior. I
can't say for sure that it's a bug, but at the very least, it's
confusing, and I want to get clarification. Sample code is attached.
Let's say I have four lines that I want to use as outputs. I can
request them all at once like this:
struct gpiod_chip* chip = gpiod_chip_open("/dev/gpiochip0");
struct gpiod_line_bulk lines;
uint32_t offsets[NUMOUTPUTS] = {6, 13, 19, 26};
int get_lines_result = gpiod_chip_get_lines(chip, offsets, NUMOUTPUTS, &lines);
int32_t values[NUMOUTPUTS] = {0, 0, 0, 0};
int set_dir_result = gpiod_line_request_bulk_output(&lines, "testgpio", values);
And then when I want to set new values, I can update the "values"
array and then do:
int set_result = gpiod_line_set_value_bulk(&lines, values);
This works exactly as expected. However, I need to maintain an array
of the current values of all the output lines. If I didn't want to do
that, perhaps because I was wrapping this in a function, I thought it
could make sense to get a single line object from the bulk object, and
then just set that one value. For example:
void set_line_high(struct gpiod_line_bulk* all_lines, int line_index)
{
struct gpiod_line* single_line =
gpiod_line_bulk_get_line(&all_lines, line_index);
int set_result = gpiod_line_set_value(single_line, 1);
}
This only works for line_index == 0. A more illuminating test case is
to have a loop that iterates over all of my outputs and toggles them
all every second:
while (keep_going)
{
for (int i = 0; i < NUMOUTPUTS ; i++)
{
values[i] = counter % 2;
struct gpiod_line* single_line = gpiod_line_bulk_get_line(&lines, i);
int set_result = gpiod_line_set_value(single_line, values[i]);
}
counter++;
usleep(1000000);
}
When I run this, the first line in the bulk object is the only one
that toggles. The other three are always off. If I replace it with
for (int i = 0; i < NUMOUTPUTS; i++)
{
values[i] = counter % 2;
}
int set_result = gpiod_line_set_value_bulk(&lines, values);
counter++;
usleep(1000000);
it works fine, and all lines toggle as expected. I have attached two
test programs that build & run on a raspberry pi (I just attached the
GPIOs to LEDs to see what's happening). One works, the other doesn't,
using the approaches above.
Is this expected behavior? I took a look at the code and didn't see an
immediate reason this wouldn't work. Let me know, and I'm happy to
help either troubleshoot or help update the docs to make it more clear
why this isn't intended to work.
Thanks!
-Mark
[-- Attachment #2: test-working.c --]
[-- Type: text/plain, Size: 2095 bytes --]
#include <gpiod.h>
#include <error.h>
#include <fcntl.h> // for open()
#include <linux/spi/spidev.h> // for struct spi_ioc_transfer
#include <signal.h> // for signal handling
#include <stdio.h> // fprintf, printf, stderr
#include <string.h> // for memset
#include <unistd.h> // for close()
#include <sys/ioctl.h> // for ioctl()
#include <sys/time.h>
#include <stdint.h> // for uint32_t
#define NUMOUTPUTS 4
volatile bool keep_going;
void termination_handler(int signum)
{
keep_going = false;
}
int main(int argc, char *argv[])
{
struct sigaction term_handler;
struct sigaction prev_handler;
struct gpiod_chip* chip = NULL;
struct gpiod_line_bulk lines;
term_handler.sa_handler = termination_handler;
sigemptyset(&term_handler.sa_mask);
term_handler.sa_flags = 0;
sigaction(SIGINT, &term_handler, &prev_handler);
printf("Setting up GPIO...\n");
chip = gpiod_chip_open("/dev/gpiochip0");
if (!chip)
{
perror("gpiod_chip_open failed");
return 1;
}
uint32_t offsets[NUMOUTPUTS] = {6, 13, 19, 26};
int get_lines_result = gpiod_chip_get_lines(chip, offsets, NUMOUTPUTS, &lines);
if (get_lines_result != 0)
{
perror("gpiod_chip_get_lines failed");
return 1;
}
int32_t values[NUMOUTPUTS] = {0, 0, 0, 0};
int set_dir_result = gpiod_line_request_bulk_output(&lines, "testgpio", values);
if (set_dir_result != 0)
{
printf("gpiod_line_request_output() failed\n");
return 1;
}
keep_going = true;
int counter = 0;
while (keep_going)
{
for (int i = 0; i < NUMOUTPUTS; i++)
{
values[i] = counter % 2;
}
int set_result = gpiod_line_set_value_bulk(&lines, values);
if (set_result != 0)
{
printf("gpiod_line_set_value() failed!\n");
keep_going = false;
}
counter++;
usleep(1000000);
}
printf("Stopping.\n");
gpiod_line_release_bulk(&lines);
gpiod_chip_close(chip);
printf("Stopped.\n");
return 0;
}
[-- Attachment #3: test-not-working.c --]
[-- Type: text/plain, Size: 2204 bytes --]
#include <gpiod.h>
#include <error.h>
#include <fcntl.h> // for open()
#include <linux/spi/spidev.h> // for struct spi_ioc_transfer
#include <signal.h> // for signal handling
#include <stdio.h> // fprintf, printf, stderr
#include <string.h> // for memset
#include <unistd.h> // for close()
#include <sys/ioctl.h> // for ioctl()
#include <sys/time.h>
#include <stdint.h> // for uint32_t
#define NUMOUTPUTS 4
volatile bool keep_going;
void termination_handler(int signum)
{
keep_going = false;
}
int main(int argc, char *argv[])
{
struct sigaction term_handler;
struct sigaction prev_handler;
struct gpiod_chip* chip = NULL;
struct gpiod_line_bulk lines;
term_handler.sa_handler = termination_handler;
sigemptyset(&term_handler.sa_mask);
term_handler.sa_flags = 0;
sigaction(SIGINT, &term_handler, &prev_handler);
printf("Setting up GPIO...\n");
chip = gpiod_chip_open("/dev/gpiochip0");
if (!chip)
{
perror("gpiod_chip_open failed");
return 1;
}
uint32_t offsets[NUMOUTPUTS] = {6, 13, 19, 26};
int get_lines_result = gpiod_chip_get_lines(chip, offsets, NUMOUTPUTS, &lines);
if (get_lines_result != 0)
{
perror("gpiod_chip_get_lines failed");
return 1;
}
int32_t values[NUMOUTPUTS] = {0, 0, 0, 0};
int set_dir_result = gpiod_line_request_bulk_output(&lines, "testgpio", values);
if (set_dir_result != 0)
{
printf("gpiod_line_request_output() failed\n");
return 1;
}
keep_going = true;
int counter = 0;
while (keep_going)
{
for (int i = 0; i < NUMOUTPUTS; i++)
{
values[i] = counter % 2;
struct gpiod_line* single_line = gpiod_line_bulk_get_line(&lines, i);
int set_result = gpiod_line_set_value(single_line, values[i]);
if (set_result != 0)
{
printf("gpiod_line_set_value() failed!\n");
keep_going = false;
}
}
counter++;
usleep(1000000);
}
printf("Stopping.\n");
gpiod_line_release_bulk(&lines);
gpiod_chip_close(chip);
printf("Stopped.\n");
return 0;
}
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [libgpiod] Either a bug or a misunderstanding regarding bulk operations
2022-09-29 13:13 [libgpiod] Either a bug or a misunderstanding regarding bulk operations Mark Locascio
@ 2022-09-30 2:53 ` Kent Gibson
0 siblings, 0 replies; 2+ messages in thread
From: Kent Gibson @ 2022-09-30 2:53 UTC (permalink / raw)
To: Mark Locascio; +Cc: linux-gpio, Bartosz Golaszewski
On Thu, Sep 29, 2022 at 08:13:36AM -0500, Mark Locascio wrote:
> Hi,
>
> I was working with libgpiod and I noticed some unintuitive behavior. I
> can't say for sure that it's a bug, but at the very least, it's
> confusing, and I want to get clarification. Sample code is attached.
>
> Let's say I have four lines that I want to use as outputs. I can
> request them all at once like this:
>
> struct gpiod_chip* chip = gpiod_chip_open("/dev/gpiochip0");
> struct gpiod_line_bulk lines;
> uint32_t offsets[NUMOUTPUTS] = {6, 13, 19, 26};
> int get_lines_result = gpiod_chip_get_lines(chip, offsets, NUMOUTPUTS, &lines);
> int32_t values[NUMOUTPUTS] = {0, 0, 0, 0};
> int set_dir_result = gpiod_line_request_bulk_output(&lines, "testgpio", values);
>
> And then when I want to set new values, I can update the "values"
> array and then do:
>
> int set_result = gpiod_line_set_value_bulk(&lines, values);
>
> This works exactly as expected. However, I need to maintain an array
> of the current values of all the output lines. If I didn't want to do
> that, perhaps because I was wrapping this in a function, I thought it
> could make sense to get a single line object from the bulk object, and
> then just set that one value.
>
The line bulk are "Convenience data structures and helper functions for
storing and operating on multiple lines at once."
They are intended to operate on all the requested lines at once, e.g. so
the lines on a bus can be switched at the same time (or as close as
the kernel can manage).
If you would rather treat the lines separately then request them
separately.
AIUI, the purpose of gpiod_line_bulk_get_line() is to allow the user to
access the results of a bulk operation on a per-line basis, not to allow
them to perform per-line operations on the bulk. The kernel API requires
that the operations for a bulk are on the bulk, and libgpiod v1 doesn't
keep track of the values of the other lines, so when you perform
operations on the single line returned by gpiod_line_bulk_get_line()
libgpiod assumes that it is a single line operation and only sets one
value - which the kernel sees as being for the first line in the bulk.
The remaining lines are zeroed.
Not sure if that is a bug or a feature, but that is my understanding.
libgpiod v2 [1](a WIP) changes this part of the API completely, and IIRC
does allow sets on individual lines in a bulk (now referred to as a line
request), by tracking the requested values for all lines in the bulk.
I'm sure Bart would consider any patches for v1 that you would care to
offer, though at this stage I would lean towards documentation
clarifications over code changes.
Cheers,
Kent.
[1] https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git -
next/libgpiod-2.0 branch
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-09-30 2:53 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-29 13:13 [libgpiod] Either a bug or a misunderstanding regarding bulk operations Mark Locascio
2022-09-30 2:53 ` Kent Gibson
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).