gpiod installed but not allowed to use it or something?

Hello,

Old school board here and so on…

  1. Can I use gpiod in python3 just by importing it?
  2. uname -r: 6.1.80-ti-r34
  3. cat /etc/dogtag: BeagleBoard.org Debian Bookworm Minimal Image 2024-05-29

I figured I would try an updated version of the images from a known source…

Anyway, when I type:

import time
import gpiod

...

# Code below

I receive an error about gpiod not being installed but when I check with apt, it is installed along with libgpiod-dev.

Seth

P.S. Please send guidance.

I might be completely off target here, but isn’t libgpiod a C binding?

Are you looking for the python equivalent, perchance?

Try to search for python install gpiod and see if something interesting pops up.

1 Like

gpiod is for python3 and libgpiod-dev is the C versioning…yes.

Seth

P.S. I changed my set up again by changing the image and now I have kernel 5.10.x. I am goin’ to use sysfs and see how far I get.

Sorry if I didn’t express myself clearly enough.

I don’t think you can see installed python library packages with apt.
The apt gpiod is a daemon; hence why I wanted you to see if pip says installed…

Again, python isn’t my first language, so I’m sorry if I’m adding noise,
but it sounds like some intermixing is going on here.

1 Like

Oh…

I thought I could with apt since I could install it with apt.

$gpioinfo
1 Like

gpioinfo shows what is available but I have yet to use GPIO with that specific kernel in Python3.

Seth

P.S. P8_11 and P8_12 are both input and not used. This gives me ideas on if or how to use those specific pins yet I cannot for some reason. I am controlling a driver with STEP and DIR only, i.e. well…along with GND too. I seem to be missing something…anyway. I will keep trying. I will have more time later today.

I am back on kernel 5.10.x in Bookworm along with sysfs

You can certainly install some Python modules with apt. There are usually not the latest version though. You have to be careful mixing apt and pip package if you are not installing into a venv though.

I have noticed this also…

apt and pip modules tend to place their binaries or working directories in separate locations where the files sometimes do not always know what to use or when…

I have installed a venv just in case, i.e. esp. when working with apt and pip on the same system.

Seth

Then it is a python issue, it would not have responded with the pin assignments if that was faulty.

I think for those on 6.1.x kernels, the python3 binding was not ported…

Odd.

Anyway, no issue. I already went back in time to 5.10.x.

Seth

1 Like
#!/usr/bin/env python
import time
import os
import signal
import sys

# Motor is attached here
# controller = ["P9_11", "P9_13", "P9_15", "P9_17"]; 
# controller = ["30", "31", "48", "5"]
# controller = ["P9_14", "P9_16", "P9_18", "P9_22"]; 
controller = ["50", "51", "4", "2"]
states = [[1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1]]
statesHiTorque = [[1,1,0,0], [0,1,1,0], [0,0,1,1], [1,0,0,1]]
statesHalfStep = [[1,0,0,0], [1,1,0,0], [0,1,0,0], [0,1,1,0],
                      [0,0,1,0], [0,0,1,1], [0,0,0,1], [1,0,0,1]]

curState = 0    # Current state
ms = 100        # Time between steps, in ms
maxStep = 22    # Number of steps to turn before turning around
minStep = 0     # minimum step to turn back around on

CW  =  1       # Clockwise
CCW = -1
pos =  0       # current position and direction
direction = CW
GPIOPATH="/sys/class/gpio"

def signal_handler(sig, frame):
    print('Got SIGINT, turning motor off')
    for i in range(len(controller)) :
        f = open(GPIOPATH+"/gpio"+controller[i]+"/value", "w")
        f.write('0')
        f.close()
    sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
print('Hit ^C to stop')

def move():
    global pos
    global direction
    global minStep
    global maxStep
    pos += direction
    print("pos: " + str(pos))
    # Switch directions if at end.
    if (pos >= maxStep or pos <= minStep) :
        direction *= -1
    rotate(direction)

# This is the general rotate
def rotate(direction) :
    global curState
    global states
	# print("rotate(%d)", direction);
    # Rotate the state according to the direction of rotation
    curState +=  direction
    if(curState >= len(states)) :
        curState = 0;
    elif(curState<0) :
        curState = len(states)-1
    updateState(states[curState])

# Write the current input state to the controller
def updateState(state) :
    global controller
    print(state)
    for i in range(len(controller)) :
        f = open(GPIOPATH+"/gpio"+controller[i]+"/value", "w")
        f.write(str(state[i]))
        f.close()

# Initialize motor control pins to be OUTPUTs
for i in range(len(controller)) :
    # Make sure pin is exported
    if (not os.path.exists(GPIOPATH+"/gpio"+controller[i])):
        f = open(GPIOPATH+"/export", "w")
        f.write(pin)
        f.close()
    # Make it an output pin
    f = open(GPIOPATH+"/gpio"+controller[i]+"/direction", "w")
    f.write("out")
    f.close()

# Put the motor into a known state
updateState(states[0])
rotate(direction)

# Rotate
while True:
    move()
    time.sleep(ms/1000)

This is some source from the docs.beagleboard.org pages that I found for a L298 driver. I switched things up a bit and made the source run…

To my surprise, I got it to work but nothing happened. I get it. Some Python3 source in the land of Debian these days.

Too many apt styled library reckonings. Anyway, thank you for trying to make me understand.

Seth

I think you might be closer than you think…

for i in range(len(controller)) :
    # Make sure pin is exported
    if (not os.path.exists(GPIOPATH+"/gpio"+controller[i])):
        f = open(GPIOPATH+"/export", "w")
        f.write(pin)

What is the pin variable equal to?
Should that not be i instead?

On my BBGW, I can do:

echo 53 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio53/direction
echo 1 > /sys/class/gpio/gpio53/value
echo 0 > /sys/class/gpio/gpio53/value

to wiggle the USR0 led.

Just make sure to do a

echo none > /sys/class/leds/beaglebone\:green\:usr0/trigger

in case the kernel is controlling the leds in the first place… :smiling_face:

Note! Your leds might be named differently, so adapt the path.

1 Like

with the sysfs GPIO pins, I am NOT supposed to export the pins in question. It makes them remove themselves somehow.

Seth

P.S. For some reason, my other source I am using is a bit troubled too. I tried with sysfs and gpiod. Neither make this motor move. I probably need a decent PSU for the BBBW. Hmm. That may be it. I need to check…

I will return service once I get it hooked up and tested.

You’re right; if the gpio is already exported, you’ll get a write error: Operation not permitted,
but the line

if (not os.path.exists(GPIOPATH+"/gpio"+controller[i])):

is protecting against this condition, so I’m still pretty sure that you want to use
the i varlable instead of pin.

You didn’t mention if you tried to wiggle the USR0 led, so if you didn’t, could you please try?

While you’re at it, could you include the output of:
ls /sys/class/gpio
ls /sys/class/leds
and
gpioinfo
It’ll give a clearer picture of what you’re dealing with…

1 Like

Sure,

I will get to the ls commands soon and the gpioinfo command too.

Please be patient.

Seth

ls /sys/class/leds

beaglebone:green:usr0  beaglebone:green:usr1  beaglebone:green:usr2
beaglebone:green:usr3  mmc0::  mmc2::

and… ls /sys/class/gpio

export   gpio113  gpio13  gpio23  gpio32  gpio38  gpio47  gpio60  gpio67  gpio72  gpio78  gpio87      gpiochip64
gpio10   gpio114  gpio14  gpio26  gpio33  gpio39  gpio48  gpio61  gpio68  gpio73  gpio79  gpio88      gpiochip96
gpio11   gpio115  gpio15  gpio27  gpio34  gpio4   gpio49  gpio62  gpio69  gpio74  gpio8   gpio89      unexport
gpio110  gpio116  gpio2   gpio3   gpio35  gpio44  gpio5   gpio63  gpio7   gpio75  gpio80  gpio9
gpio111  gpio117  gpio20  gpio30  gpio36  gpio45  gpio50  gpio65  gpio70  gpio76  gpio81  gpiochip0
gpio112  gpio12   gpio22  gpio31  gpio37  gpio46  gpio51  gpio66  gpio71  gpio77  gpio86  gpiochip32

and gpioinfo

gpiochip0 - 32 lines:
        line   0: "[mdio_data]" unused input active-high
        line   1: "[mdio_clk]"       unused   input  active-high
        line   2: "P9_22 [spi0_sclk]" "P9_22" input active-high [used]
        line   3: "P9_21 [spi0_d0]" "P9_21" input active-high [used]
        line   4: "P9_18 [spi0_d1]" "P9_18" input active-high [used]
        line   5: "P9_17 [spi0_cs0]" "P9_17" input active-high [used]
        line   6:  "[mmc0_cd]"         "cd"   input   active-low [used]
        line   7: "P8_42A [ecappwm0]" "P9_42" input active-high [used]
        line   8: "P8_35 [lcd d12]" "P8_35" input active-high [used]
        line   9: "P8_33 [lcd d13]" "P8_33" input active-high [used]
        line  10: "P8_31 [lcd d14]" "P8_31" input active-high [used]
        line  11: "P8_32 [lcd d15]" "P8_32" input active-high [used]
        line  12: "P9_20 [i2c2_sda]" "P9_20" input active-high [used]
        line  13: "P9_19 [i2c2_scl]" "P9_19" input active-high [used]
        line  14: "P9_26 [uart1_rxd]" "P9_26" input active-high [used]
        line  15: "P9_24 [uart1_txd]" "P9_24" input active-high [used]
        line  16: "[rmii1_txd3]" unused input active-high
        line  17: "[rmii1_txd2]" unused input active-high
        line  18: "[usb0_drvvbus]" unused input active-high
        line  19: "[hdmi cec]"       unused   input  active-high
        line  20:     "P9_41B"      "P9_41"   input  active-high [used]
        line  21: "[rmii1_txd1]" unused input active-high
        line  22: "P8_19 [ehrpwm2a]" "P8_19" input active-high [used]
        line  23: "P8_13 [ehrpwm2b]" "P8_13" input active-high [used]
        line  24:         "NC"       unused   input  active-high
        line  25:         "NC"       unused   input  active-high
        line  26:      "P8_14"      "P8_14"   input  active-high [used]
        line  27:      "P8_17"      "P8_17"   input  active-high [used]
        line  28: "[rmii1_txd0]" "enable" output active-high [used]
        line  29: "[rmii1_refclk]" "interrupt" input active-high [used]
        line  30: "P9_11 [uart4_rxd]" "P9_11" input active-high [used]
        line  31: "P9_13 [uart4_txd]" "P9_13" input active-high [used]
gpiochip1 - 32 lines:
        line   0: "P8_25 [mmc1_dat0]" "P8_25" input active-high [used]
        line   1: "[mmc1_dat1]" "P8_24" input active-high [used]
        line   2: "P8_5 [mmc1_dat2]" "P8_05" input active-high [used]
        line   3: "P8_6 [mmc1_dat3]" "P8_06" input active-high [used]
        line   4: "P8_23 [mmc1_dat4]" "P8_23" input active-high [used]
        line   5: "P8_22 [mmc1_dat5]" "P8_22" input active-high [used]
        line   6: "P8_3 [mmc1_dat6]" "P8_03" input active-high [used]
        line   7: "P8_4 [mmc1_dat7]" "P8_04" input active-high [used]
        line   8:         "NC"       unused   input  active-high
        line   9:         "NC"       unused   input  active-high
        line  10:         "NC"       unused   input  active-high
        line  11:         "NC"       unused   input  active-high
        line  12:      "P8_12"      "P8_12"   input  active-high [used]
        line  13:      "P8_11"      "P8_11"   input  active-high [used]
        line  14:      "P8_16"      "P8_16"   input  active-high [used]
        line  15:      "P8_15"      "P8_15"   input  active-high [used]
        line  16:     "P9_15A"      "P9_15"   input  active-high [used]
        line  17:      "P9_23"      "P9_23"   input  active-high [used]
        line  18: "P9_14 [ehrpwm1a]" "P9_14" input active-high [used]
        line  19: "P9_16 [ehrpwm1b]" "P9_16" input active-high [used]
        line  20: "[emmc rst]"       unused   input  active-high
        line  21: "[usr0 led]" "beaglebone:green:usr0" output active-high [used]
        line  22: "[usr1 led]" "beaglebone:green:usr1" output active-high [used]
        line  23: "[usr2 led]" "beaglebone:green:usr2" output active-high [used]
        line  24: "[usr3 led]" "beaglebone:green:usr3" output active-high [used]
        line  25: "[hdmi irq]"       unused   input  active-high
        line  26: "[usb vbus oc]" unused input active-high
        line  27: "[hdmi audio]" unused input active-high
        line  28:      "P9_12"      "P9_12"   input  active-high [used]
        line  29:      "P8_26"      "P8_26"   input  active-high [used]
        line  30: "P8_21 [emmc]" "P8_21" input active-high [used]
        line  31: "P8_20 [emmc]" "P8_20" input active-high [used]
gpiochip2 - 32 lines:
        line   0:     "P9_15B"       unused   input  active-high
        line   1:      "P8_18"      "P8_18"   input  active-high [used]
        line   2:       "P8_7"      "P8_07"   input  active-high [used]
        line   3:       "P8_8"      "P8_08"   input  active-high [used]
        line   4:      "P8_10"      "P8_10"   input  active-high [used]
        line   5:       "P8_9"      "P8_09"   input  active-high [used]
        line   6: "P8_45 [hdmi]" "P8_45" input active-high [used]
        line   7: "P8_46 [hdmi]" "P8_46" input active-high [used]
        line   8: "P8_43 [hdmi]" "P8_43" input active-high [used]
        line   9: "P8_44 [hdmi]" "P8_44" input active-high [used]
        line  10: "P8_41 [hdmi]" "P8_41" input active-high [used]
        line  11: "P8_42 [hdmi]" "P8_42" input active-high [used]
        line  12: "P8_39 [hdmi]" "P8_39" input active-high [used]
        line  13: "P8_40 [hdmi]" "P8_40" input active-high [used]
        line  14: "P8_37 [hdmi]" "P8_37" input active-high [used]
        line  15: "P8_38 [hdmi]" "P8_38" input active-high [used]
        line  16: "P8_36 [hdmi]" "P8_36" input active-high [used]
        line  17: "P8_34 [hdmi]" "P8_34" input active-high [used]
        line  18: "[rmii1_rxd3]" unused input active-high
        line  19: "[rmii1_rxd2]" unused input active-high
        line  20: "[rmii1_rxd1]" unused input active-high
        line  21: "[rmii1_rxd0]" unused input active-high
        line  22: "P8_27 [hdmi]" "P8_27" input active-high [used]
        line  23: "P8_29 [hdmi]" "P8_29" input active-high [used]
        line  24: "P8_28 [hdmi]" "P8_28" input active-high [used]
        line  25: "P8_30 [hdmi]" "P8_30" input active-high [used]
        line  26: "[mmc0_dat3]" unused input active-high
        line  27: "[mmc0_dat2]" unused input active-high
        line  28: "[mmc0_dat1]" unused input active-high
        line  29: "[mmc0_dat0]" unused input active-high
        line  30: "[mmc0_clk]"       unused   input  active-high
        line  31: "[mmc0_cmd]"       unused   input  active-high
gpiochip3 - 32 lines:
        line   0:  "[mii col]"       unused   input  active-high
        line   1:  "[mii crs]"       unused   input  active-high
        line   2: "[mii rx err]" unused input active-high
        line   3: "[mii tx en]" unused input active-high
        line   4: "[mii rx dv]" unused input active-high
        line   5: "[i2c0 sda]"       unused   input  active-high
        line   6: "[i2c0 scl]"       unused   input  active-high
        line   7: "[jtag emu0]" unused input active-high
        line   8: "[jtag emu1]" unused input active-high
        line   9: "[mii tx clk]" "fixedregulator@2" output active-high [used]
        line  10: "[mii rx clk]" "LS_BUF_EN" output active-high [used]
        line  11:         "NC"       unused   input  active-high
        line  12:         "NC"       unused   input  active-high
        line  13: "[usb vbus en]" unused input active-high
        line  14: "P9_31 [spi1_sclk]" "P9_31" input active-high [used]
        line  15: "P9_29 [spi1_d0]" "P9_29" input active-high [used]
        line  16: "P9_30 [spi1_d1]" "P9_30" input active-high [used]
        line  17: "P9_28 [spi1_cs0]" "P9_28" input active-high [used]
        line  18: "P9_42B [ecappwm0]" "P9_92" input active-high [used]
        line  19:      "P9_27"      "P9_27"   input  active-high [used]
        line  20:     "P9_41A"      "P9_91"   input  active-high [used]
        line  21:      "P9_25"      "P9_25"   input  active-high [used]
        line  22:         "NC"       unused   input  active-high
        line  23:         "NC"       unused   input  active-high
        line  24:         "NC"       unused   input  active-high
        line  25:         "NC"       unused   input  active-high
        line  26:         "NC"       unused   input  active-high
        line  27:         "NC"       unused   input  active-high
        line  28:         "NC"       unused   input  active-high
        line  29:         "NC"       unused   input  active-high
        line  30:         "NC"       unused   input  active-high
        line  31:         "NC"       unused   input  active-high

So, currently, I am using P8_11, P8_12, and P9_11.

P8_11 is GPIO45
P8_12 is GPIO44

and…

P9_11 is GPIO30

@lranders ,

Also…something I noticed. When I use gpiod, some of my used pins, GPIO30 in this case, are removed for when I use sysfs again.

Seth

P.S. I understand that this is supposed to be this way but I just wanted to inform you.

Yea, I don’t want to be a spoilsport but all the pins you have in mind are already claimed by
some device in Linux; note the little [used] tag at the end of the line.

That means that they have most likely been pin-muxed to one of the built-in hardware devices
inside the am335x processor and unavailable to the sysfs files.

You will have to study the schematic and the device-tree to see what pins can safely be reclaimed.

Derek Molloy has written extensively about this exact topic and can save you hours of heartburn.

The reason I wanted you to play with gpio 53 to 56 was because they are safe to repurpose
for a quick-n-dirty-lets-see-if-this-works kind of scenario.
You just need to do a

echo none > /sys/class/leds/beaglebone\:green\:usr[0-3]/trigger

to get the Linux led driver out of your hair.

If you change your program to use 53 to 56, you might even see the leds flash if you tune
up the ms variable to say, 500.

2 Likes

Thank you,

I will see.

Seth

Perhaps this pinmuxing link could be of some interest to you as well…

I can’t vouch for the applicability, but it sounded like on topic.

1 Like