TX2 GPIO controls without root permission

Hi. I’m Mingu.

I try control gpio of jetson tx2 using C++ code.

Now, My code can control the gpio. But it needs the sudo command for root permission.

How can I use gpio without root permission?

hello mk.kang,

suppose you’re have implementation as below steps to control gpio.

Generate gpioXXX name
$ echo XXX > export
Enable the gpio
$ cd gpioXXX 
$ echo out > direction && echo 1 > value

you might also check below sources, there’re general APIs to control gpio pin.

<i>$l4t-r32.2/kernel_src/kernel/kernel-4.9/include/linux/gpio.h</i>

static inline int gpio_get_value(unsigned int gpio)
{
        return __gpio_get_value(gpio);
}

static inline void gpio_set_value(unsigned int gpio, int value)
{
        __gpio_set_value(gpio, value);
}

in addition, please also check the kernel driver for the reference to control GPIO

<i>$l4t-r32.2/kernel_src/kernel/nvidia/drivers/media/i2c/imx185.c</i>

        if (pw->reset_gpio) {
                gpio_set_value(pw->reset_gpio, 0);

Below is part of the code I am using.

int gpioExport ( jetsonGPIO gpio )
{
    int fileDescriptor, length;
    char commandBuffer[MAX_BUF];

    fileDescriptor = open("/sys/class/gpio/export", O_WRONLY);
    if (fileDescriptor < 0) {
        char errorBuffer[128] ;
        snprintf(errorBuffer,sizeof(errorBuffer), "gpioExport unable to open gpio%d",gpio) ;
        perror(errorBuffer);
        return fileDescriptor;
    }

    length = snprintf(commandBuffer, sizeof(commandBuffer), "%d", gpio);
    if (write(fileDescriptor, commandBuffer, length) != length) {
        perror("gpioExport");
        return fileDescriptor ;

    }
    close(fileDescriptor);

    return 0;
}

For access the file’/sys/class/gpio/export’, i needs the root permission.

But i want to use gpio without root permission.

hello mk.kang,

yap, you’ll need root permission to access sysnodes.
you may check the APIs in comment #2 to implement low-level drivers to control GPIOs.
thanks

hello JerryChang.

I understand your answer.

To check low-level drivers to control GPIOs, Where can I find some examples or documentation?

that’s also in comment #2, please also check the kernel driver for the reference to control GPIO