Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid path for USR LED when setup as input #310

Closed
pdp7 opened this issue Apr 1, 2019 · 2 comments
Closed

Invalid path for USR LED when setup as input #310

pdp7 opened this issue Apr 1, 2019 · 2 comments
Assignees
Labels

Comments

@pdp7
Copy link
Collaborator

pdp7 commented Apr 1, 2019

This error occurs for built-in LEDs like USR1. It currently preventing the built-in LEDs from being used in the CircuitPython Blinka library: adafruit/Adafruit_Blinka#96

The error is can be triggered by calling GPIO.setup("USR1", GPIO.IN) instead of GPIO.setup("USR1", GPIO.OUT):

import Adafruit_BBIO.GPIO as GPIO
GPIO.setup("USR1", GPIO.IN)

results in error:

Traceback (most recent call last):
  File "/home/debian/test-usr.py", line 2, in <module>
    GPIO.setup("USR1", GPIO.IN)
ValueError: Set gpio mode failed, missing file or invalid permissions.

and strace shows the bad path:

open("/sys/devices/platform/ocp/ocp:USR01_pinmux/state", O_WRONLY|O_CREAT|O_TRUNC, 0666) = -1 ENOENT (No such file or directory)

While it is the case that INPUT is not valid state for built-in LEDs, this error is caused by accessing the incorrect path for the LED.

The correct path is:

/sys/class/leds/beaglebone:green:usr0/brightness
@pdp7 pdp7 self-assigned this Apr 1, 2019
@pdp7 pdp7 added the bug label Apr 1, 2019
@pdp7
Copy link
Collaborator Author

pdp7 commented Apr 1, 2019

For reference, the Linux kernel has a LED subsystem which is different than the GPIO subsystem. Therefore, the built-in LED devices show up under /sys/class/leds/ instead of /sys/class/gpio.

This requires a special case in Adafruit_BBIO to handle the built-in LEDs:
https://github.com/adafruit/adafruit-beaglebone-io-python/blob/master/source/event_gpio.c#L220

    // create file descriptor of value file
    if ((gpio >= USR_LED_GPIO_MIN) && (gpio <=  USR_LED_GPIO_MAX)) {
        snprintf(filename, sizeof(filename), "/sys/class/leds/beaglebone:green:usr%d/brightness", gpio -  USR_LED_GPIO_MIN);
    } else if (beaglebone_blue()) {
        //syslog(LOG_DEBUG, "Adafruit_BBIO: gpio open_value_file: beaglebone_blue() is true\n");
        switch(gpio) {
            case USR_LED_RED:
                snprintf(filename, sizeof(filename), "/sys/class/leds/red/brightness");
                break;
            case USR_LED_GREEN:
                snprintf(filename, sizeof(filename), "/sys/class/leds/green/brightness");
                break;
            case BAT25:
                snprintf(filename, sizeof(filename), "/sys/class/leds/bat25/brightness");
                break;
            case BAT50:
                snprintf(filename, sizeof(filename), "/sys/class/leds/bat50/brightness");
                break;
            case BAT75:
                snprintf(filename, sizeof(filename), "/sys/class/leds/bat75/brightness");
                break;
            case BAT100:
                snprintf(filename, sizeof(filename), "/sys/class/leds/bat100/brightness");
                break;
            case WIFI:
                snprintf(filename, sizeof(filename), "/sys/class/leds/wifi/brightness");
                break;
            default:
                snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/value", gpio);
                break;
        }
    } else {
        //syslog(LOG_DEBUG, "Adafruit_BBIO: gpio open_value_file: default gpio path\n");
        snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/value", gpio);
    }

pdp7 added a commit that referenced this issue Apr 1, 2019
This fix resolves Adafruit_BBIO issue #310 and adafruit/Adafruit_Blinka#96 which was preventing the USR LEDs from being used by the CircuitPython Blinka library
@pdp7
Copy link
Collaborator Author

pdp7 commented Apr 1, 2019

The above commit fixes adafruit/Adafruit_Blinka#96

This Blinka example now runs OK:

import board
import digitalio
import busio
import time

led = digitalio.DigitalInOut(board.LED_USR0)
led.direction = digitalio.Direction.OUTPUT


while True:
    led.value = True
    time.sleep(0.5)
    led.value = False
    time.sleep(0.5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant