Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BeagleBone Black Adafruit IO Python library GPIO user permissions

I have a problem with GPIO in Adafruit Python IO library on Debian (from armhf.org). It works as root, but not as regular user.

I've added udev rule;

KERNEL=="gpio*", SUBSYSTEM=="gpio", ACTION=="add", PROGRAM="/bin/sh -c 'chown -R debian:gpio /sys/class/gpio'"
KERNEL=="gpio*", SUBSYSTEM=="gpio", ACTION=="add", PROGRAM="/bin/sh -c 'chown -R debian:gpio /sys/devices/virtual/gpio/'"

My user "debian" is in group "gpio". Now I can control gpio using echo... > /sys... in user's shell. But not with python. It just does not work without errors.

However, if I run manually "echo 45 > /sys/class/gpio/export" and then start python on this gpio it will work.

I can see gpio45 in /sys/class/gpio after starting the python script, but it does not work.It will only work after manual export. I tried to compile function gpio_export() from source code and start it manually. It actually works. But in real script it only creates gpio file and doesn't work.

Here is my test script:

import Adafruit_BBIO.GPIO as GPIO                                                                    
import time                                                                                          

P = "P8_11"                                                                                          

GPIO.setup(P, GPIO.OUT)                                                                              
for i in xrange(100):                                                                                
        if i % 2 == 0:                                                                               
                GPIO.output(P, GPIO.HIGH)                                                            
        else:                                                                                        
                GPIO.output(P, GPIO.LOW)                                                             
        time.sleep(0.5)                                                                              
GPIO.cleanup() 

Thanks

-=UPDATED=- It's a bug. I posted it on project's github page along with brutal patch which solves the problem for me https://github.com/adafruit/adafruit-beaglebone-io-python/issues/36

--- adafruit-beaglebone-io-python/source/py_gpio.c  2013-09-17 20:10:08.000000000 +0300
+++ adafruit-beaglebone-io-python/source/py_gpio.c  2013-09-21 02:54:43.000000000 +0300
@@ -105,10 +105,26 @@

    if (get_gpio_number(channel, &gpio))
        return NULL;
-
-   gpio_export(gpio);
-   gpio_set_direction(gpio, direction);
-   gpio_set_value(gpio, pud);
+   
+   unsigned int count = 1000000;
+   int res = -1;
+   do {
+       res = gpio_export(gpio);
+   } while(res != 0 && count-- > 0);
+   if(count == 0)
+      return NULL;
+   count = 1000000;
+   do {
+       res = gpio_set_direction(gpio, direction);
+   } while(res != 0 && count-- > 0);
+   if(count == 0)
+      return NULL;
+   count = 1000000;
+   do {
+   res = gpio_set_value(gpio, pud);
+   } while(res != 0 && count-- > 0);
+   if(count == 0)
+      return NULL;

    gpio_direction[gpio] = direction;

Basically, you have to check return values. In my case open() cannot open newly created by exporting gpio file because it's not yet appeared on a filesystem.

like image 830
Oleg Antonyan Avatar asked Nov 26 '22 08:11

Oleg Antonyan


1 Answers

solved this patch is already accepted

like image 78
Oleg Antonyan Avatar answered Nov 30 '22 23:11

Oleg Antonyan