IOError: [Errno 13] Permission denied: '/sys/class/gpio/gpio298/direction'

Hello, need to run my python program without sudo. But I have an error !

Could anyone help ?

python /home/nvidia/catkin_ws/src/alfred_eyes/src/RandomOledEyes.py
Traceback (most recent call last):
  File "/home/nvidia/catkin_ws/src/alfred_eyes/src/RandomOledEyes.py", line 32, in <module>
    serial = spi(port=3, device=0, bus_speed_hz=8000000, transfer_size=2048, gpio=GPIO, gpio_DC=1, gpio_RST=2)
  File "/usr/local/lib/python2.7/dist-packages/luma/core/interface/serial.py", line 253, in __init__
    bitbang.__init__(self, gpio, transfer_size, DC=gpio_DC, RST=gpio_RST)
  File "/usr/local/lib/python2.7/dist-packages/luma/core/interface/serial.py", line 153, in __init__
    self._DC = self._configure(kwargs.get("DC"))
  File "/usr/local/lib/python2.7/dist-packages/luma/core/interface/serial.py", line 164, in _configure
    self._gpio.setup(pin, self._gpio.OUT)
  File "/usr/local/lib/python2.7/dist-packages/OPi/GPIO.py", line 366, in setup
    sysfs.direction(pin, direction)
  File "/usr/local/lib/python2.7/dist-packages/OPi/sysfs.py", line 32, in direction
    with open(path, "w") as fp:
IOError: [Errno 13] Permission denied: '/sys/class/gpio/gpio298/direction'

Thanks All

Was your program run as root (with sudo)? A regular user can’t do that.

I did : sudo gedit /etc/udev/rules.d/99-com.rules

sudo usermod -aG gpio nvidia
SUBSYSTEM==“gpio*”, PROGRAM=“/bin/sh -c ‘chown -R root:gpio /sys/class/gpio && chmod -R 770 /sys/class/gpio; chown -R root:gpio /sys/devices/virtual/gpio && chmod -R 770 /sys/devices/virtual/gpio; chown -R root:gpio’”

and

sudo echo 1 > /sys/class/gpio/gpio298/direction
bash: /sys/class/gpio/gpio298/direction: Permission denied

Don’t know what to do !!

Yes, It works when I sudo python
But I need to make it work in a ROS environment, so without “sudo”. An idea ?

Sure your code is for Jetson ? Not for Raspberry Pi ? (noticed ‘OPi’)
I’m running R28.2-DP, and see no gpio298:

ls -al /sys/class/gpio/
total 0
drwxr-xr-x  2 root root    0 Jan 21 21:13 .
drwxr-xr-x 73 root root    0 Jan 21 21:13 ..
--w-------  1 root root 4096 Jan 21 21:13 export
lrwxrwxrwx  1 root root    0 Jan 21 21:13 gpiochip216 -> ../../devices/bpmp_i2c/i2c-4/4-003c/max77620-gpio/gpio/gpiochip216
lrwxrwxrwx  1 root root    0 Jan 21 21:13 gpiochip224 -> ../../devices/3160000.i2c/i2c-0/0-0077/gpio/gpiochip224
lrwxrwxrwx  1 root root    0 Jan 21 21:13 gpiochip240 -> ../../devices/3160000.i2c/i2c-0/0-0074/gpio/gpiochip240
lrwxrwxrwx  1 root root    0 Jan 21 21:13 gpiochip256 -> ../../devices/c2f0000.gpio/gpio/gpiochip256
lrwxrwxrwx  1 root root    0 Jan 21 21:13 gpiochip320 -> ../../devices/2200000.gpio/gpio/gpiochip320
--w-------  1 root root 4096 Jan 21 21:13 unexport

Sure. My board is TX2 !
Using Luma.oled and Luma.OPI for interfacing Gpio, for a SSD1351 RGB Oled screen.

my board :

nvidia@tegra-ubuntu:~$ ls -al /sys/class/gpio/
total 0
drwxrwx---  2 root gpio    0 Jan 21 21:15 .
drwxr-xr-x 72 root root    0 Jan 21 20:57 ..
-rwxrwx---  1 root gpio 4096 Jan 21 21:15 export
lrwxrwxrwx  1 root gpio    0 Jan 21 21:15 gpio298 -> ../../devices/c2f0000.gpio/gpio/gpio298
lrwxrwxrwx  1 root gpio    0 Jan 21 20:57 gpiochip216 -> ../../devices/bpmp_i2c/i2c-4/4-003c/max77620-gpio/gpio/gpiochip216
lrwxrwxrwx  1 root gpio    0 Jan 21 20:57 gpiochip224 -> ../../devices/3160000.i2c/i2c-0/0-0077/gpio/gpiochip224
lrwxrwxrwx  1 root gpio    0 Jan 21 20:57 gpiochip240 -> ../../devices/3160000.i2c/i2c-0/0-0074/gpio/gpiochip240
lrwxrwxrwx  1 root gpio    0 Jan 21 20:57 gpiochip256 -> ../../devices/c2f0000.gpio/gpio/gpiochip256
lrwxrwxrwx  1 root gpio    0 Jan 21 20:57 gpiochip320 -> ../../devices/2200000.gpio/gpio/gpiochip320
-rwxrwx---  1 root gpio 4096 Jan 21 21:15 unexport

I personnaly added the 298 myself, to command OLED DC pin

Just need to be able to run without sudo ?!

hello elpimous12,

there are APIs to control GPIO pins.
you’re able to control them from your driver side.

please check below code snippet as an example.

if (pw->reset_gpio) {
    gpio_set_value(pw->reset_gpio, 0);
    usleep_range(30, 50);
    gpio_set_value(pw->reset_gpio, 1);
    usleep_range(30, 50);
}

Hello JerryChang,
Yes, it would be easier for me to use the python API from nvidia, but I can’t find documentation !!!
Some answers, in this forum helped me with doc names, but couldn’t find them !

Do you know where i could find TX2 GPIO python API doc ?
Thanks a lot (And thanks NVIDIA for this marvellous board ! → kisses from Alfred, my robot !)

Hello again.
Could you help a bit more ?
what library should I import in python file, to work with GPIO ? Do you have an example ?
Thanks

Hi all,
Small UP ?!

Hi elpimous12,

We have no experience for using Python API to work with GPIO, hope other developer could share their case with you.

Thanks

Hello. Bad news for me…
So, I’ll wait…
Thanks for answer