adafruit-beaglebone-io-python
adafruit-beaglebone-io-python copied to clipboard
Support new GPIO character device interface
Linux kernel gpio subsystem maintainer Linus Walleij presented GPIO descriptors in this presentation:
- Video: GPIO for Engineers and Makers
- Slides: GPIO for Engineers and Makers
Linus W. details kernel-external API changes:
-
/sys/class/gpio
sysfs interface is marked as deprecated - new character device
/sys/bus/gpiochipN
or/dev/gpiochipN
Linus W. emphasized these points:
The Rules of Linux Userspace GPIO
- You do not access GPIOs from userspace
- YOU DO NOT ACCESS GPIOS FROM USERSPACE
- Read Documentation/gpio/drivers-on-gpio.txt
- Use the character device
Features the Character Device Has
- Discovery mechanism (not magic numbers)
- Cleanup of resources on closing or crashing
- Open Drain / Open Source
- Get and set multiple lines at once
- Good examples: tools/gpio/*Features the Character Device Has
- Discovery mechanism (not magic numbers)
- Cleanup of resources on closing or crashing
- Open Drain / Open Source
- Get and set multiple lines at once
- Good examples: tools/gpio/*
Information on when these gpiolib features were merged into the kernel:
gpio: add a userspace chardev ABI for GPIOs
- commit `3c702e9987e26
-
[GIT PULL] GPIO bulk changes for kernel v4.6
(Mar 17, 2016) - ioctl
- GPIO_GET_CHIPINFO_IOCTL
gpio: add userspace ABI for GPIO line information
- commit
521a2ad6f862a
-
[GIT PULL] GPIO bulk changes for kernel v4.6
(Mar 17, 2016) - ioctl
- GPIO_GET_LINEINFO_IOCTL
gpio: userspace ABI for reading/writing GPIO lines
- commit:
d7c51b47ac11e
-
[GIT PULL] bulk GPIO changes for v4.8
(Jul 25, 2016) - ioctl
- GPIOHANDLE_GET_LINE_VALUES_IOCTL
- GPIOHANDLE_SET_LINE_VALUES_IOCTL
- GPIO_GET_LINEHANDLE_IOCTL
gpio: userspace ABI for reading GPIO line events
- commit
61f922db72216
-
[GIT PULL] bulk GPIO changes for v4.8
(Jul 25, 2016) - ioctl
- GPIOHANDLE_GET_LINE_VALUES_IOCTL
- GPIO_GET_LINEEVENT_IOCTL
lsgpio utlitiy uses these new ioctl()'s to list all lines for each gpiochip:
debian@beaglebone:~/gpio$ sudo ./lsgpio
Linus W GPIO chip: gpiochip3, "gpio", 32 GPIO lines
line 0: unnamed unused
line 1: unnamed unused
<snip>
line 30: unnamed "P9_11" [kernel]
line 31: unnamed "P9_13" [kernel]
system calls that lsgpio uses:
debian@beaglebone:~/gpio$ sudo strace -e open,stat,read,write ./lsgpio >/dev/null
open("/dev", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_CLOEXEC) = 3
open("/dev/gpiochip3", O_RDONLY) = 4
open("/dev/gpiochip2", O_RDONLY) = 4
open("/dev/gpiochip1", O_RDONLY) = 4
open("/dev/gpiochip0", O_RDONLY) = 4
debian@beaglebone:~/gpio$ sudo strace -e open,ioctl ./lsgpio >/dev/null
open("/dev", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_CLOEXEC) = 3
open("/dev/gpiochip3", O_RDONLY) = 4
ioctl(4, GPIO_GET_CHIPINFO_IOCTL, 0xbeff6618) = 0
ioctl(4, GPIO_GET_LINEINFO_IOCTL, 0xbeff65cc) = 0
<snip>
Bartosz Golaszewski created libgpiod to make it easier to use the new kernel GPIO API: New GPIO interface for user space
The repo for libgpiod is hosted here: https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/
Examples of the libgpiod API:
- Learn More About Linux’s New GPIO User Space Subsystem & Libgpiod
- core: add non-closing gpiochip iterator function and foreach macro #11
bindings: python #5 @brgl writes:
Seems like ctypes is the way to go and it should be quite easy to implement.
Update on Python bindings for libgpiod by @brgl:
Yes, I was recently contacted by @sgjava who showed me his automatically generated ctypes bindings.
You can find them here.
However I plan to write object oriented python 3 bindings (using C extensions) right after I finish the C++ bindings.
There were some issues raised on the linux-gpio mailing list about the safety of ctypes for public structures with different memory layout on 32 and 64 bit architectures.
Please note that I just released v1.0 of libgpiod with an improved API which shall remain stable from now on.
libgpiod-extra by @sgjava
Automated build, Python and Java bindings for libgpiod
Python bindings ctypesgen is used to create a Python bindings from libgpiod's gpiod.h C header file. I've added an insert file to add the missing ctypes Structures.
From https://github.com/brgl/libgpiod/issues/5#issuecomment-367170393 @sgjava has created userspaceio with Python 3 bindings using CFFI:
Python and Java bindings for GPIO, SPI, I2C and Serial interfaces in Linux user space
Library to operate GPIO pins via character device
GPIOHandle for character device
New GPIO interface has been introduced recently.
It exposes GPIO interface as
/dev/gpiochip0
character device and provides several ioctl syscalls for bulk operations on sets of GPIO pins.In gpiodev/src/gpioctl.c we wrap the ioctl calls into C-functions suitable for later use.
In gpiodev/gpio.py the ctypes bindings created and then used to define the main GPIOHandle class.
Check examples for usage.
Hello Sir,
I am a novice w/ all this info. but if you need support, I will take time out to look over things and test stuff.
Seth
P.S. If you want to direct me as to what is important right now versus what can wait, I can make some notes and get back to you.
@pdp7 ,
Hello Sir...I have been reading and trying to catch up to what you may already know about these instances listed above.
...
https://github.com/torvalds/linux/blob/master/Documentation/driver-api/gpio/drivers-on-gpio.rst.
This is what I found recently in Linux. I have been watching the YouTube.com video on gpio and how we are not supposed to call it from UserSpace. I am almost done w/ the video.
Seth
P.S. I will continue my search on these subjects. Are you still wanting assistance for any particular subject matter?
Hello,
I still have the /sys/class/gpio (legacy) system and the new character device system for gpio at /dev.
Seth
P.S. I will work on it. Be back later in the year or hopefully, sooner.
Okay and Hello,
I found the userspaceio GitHub.com page. This is good motion in action. I found the fellow that did a presentation on Linux Embedded Conference, too. He has a GitHub.com page for libgpiod.
Seth
P.S. I was able to get my LED lit and turned off by the character device system.
@pdp7
Hello Again Sir...Look here: https://www.hackster.io/silver2row/the-new-character-device-idea-0968d6. I made a "funny" set up of making a LED blink on and after the return button has been pressed, the LED turns off when the pin is high w/ gpioset --mode=wait gpiochip1 28=0
for P9_12.
Seth
P.S. I thought this was fascinating. Now, I need to read more as usual before I can get more into things. So, let me know if you need support. I am actually building a CNC for fun, just for posting, but I will have time to read more. Just show me the way and I can start on ideas.
@silver2row thanks, that is very interesting
@pdp7 ,
No issue, sir. I thought you would have liked that idea.
Seth
P.S. I know you may not know everything on this subject but neither do I. If I can ever catch up to what you have read already, I may be able to help. I will keep searching online for ideas and issues pertaining to character device support. So, just let me know if you have some nice docs. on this subject.