JetPack6 GPIO IOCTL problem

static void gpio_write(const char *dev_name, int offset, uint8_t value)
{
    struct gpiohandle_request rq;
    struct gpiohandle_data data;
    int fd, ret;
    printf("Write value %d to GPIO at offset %d (OUTPUT mode) on chip %s\n", value, offset, dev_name);
    fd = open(dev_name, O_RDONLY);
    if (fd < 0)
    {
        printf("Unabled to open %s: %s", dev_name, strerror(errno));
        return;
    }
    data.values[0] = value;
    rq.lineoffsets[0] = offset;
    rq.flags = GPIOHANDLE_REQUEST_OUTPUT;
    rq.lines = 1;
    memcpy(rq.default_values,&data,1);
    strcpy(rq.consumer_label,"CCGLED");
    ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &rq);
    close(fd);
    if (ret == -1)
    {
        printf("Unable to line handle from ioctl : %s", strerror(errno));
        return;
    }

    ret = ioctl(rq.fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
    if (ret == -1)
    {
        printf("Unable to set line value using ioctl : %s", strerror(errno));
    }
    else
    {
         usleep(2000000);
    }
   
    close(rq.fd);
}

Hi!
Can you give an example of using ioctl to control GPIO on jetpack6, after using the above code to set the IO state, it will not be maintained, and the IO state will return to the default state after the program exits, which is very inconvenient

This is the expected behavior even when using libgpiod.
You need to add some busy waiting/sleep functions to prevent the process from returing immediately.

thanks for your reply

Can you give an example, at the moment I need to set the IO state even if I exit the program, I need the IO to remain in the state after I set it

If the behavior of ioctl() and libgpiod are the same where the effect is gone after the process/function returns, then that means there are some changes in more low-level drivers, and you can only live with it.

Then I don’t think this is possible.

Just like the previous Jetpack version was controlled by sysfs, the IO status can be changed at any time, and the set state can be maintained after setting

Do you understand what I’m talking about here?

I totally understand and just wanted to see if there is another way to solve this problem I’m having on Jetpack 6 right now.

Then try something like this.

I’ve tried to delay waiting, but this method is not suitable for multiple GPIOs and multiple programs to set at any time, is there no other way?

Then I think there is no.

Or you can manually enable required kernel config for GPIO sysfs and re-build the kernel, and see if sysfs still works as expected.

I’ve tried it, and when I try to open GPIO sysfs, it compiles normally, but it doesn’t boot normally after flashing

That means you are doing it incorrectly.

https://docs.nvidia.com/jetson/archives/r36.3/DeveloperGuide/SD/Kernel/KernelCustomization.html
Make sure you follow every single step mentioned here.
You need to

  1. Build kernel image
  2. Build in-tree kernel modules
  3. Build OOT kernel modules
  4. Update initrd to include newly built kernel modules.

Is there a suggested process or a case study that can be referenced?

I think our document is very clear on this.
Just ask for what you don’t understand.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.