[libgpiod] Either a bug or a misunderstanding regarding bulk operations

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



  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
#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;
}
#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;
}

[Index of Archives]     [Linux SPI]     [Linux Kernel]     [Linux ARM (vger)]     [Linux ARM MSM]     [Linux Omap]     [Linux Arm]     [Linux Tegra]     [Fedora ARM]     [Linux for Samsung SOC]     [eCos]     [Linux Fastboot]     [Gcc Help]     [Git]     [DCCP]     [IETF Announce]     [Security]     [Linux MIPS]     [Yosemite Campsites]

  Powered by Linux