adafruit / adafruit-beaglebone-io-python

Adafruit's BeagleBone IO Python Library
http://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black
476 stars 218 forks source link

Support new GPIO character device interface #157

Open pdp7 opened 7 years ago

pdp7 commented 7 years ago

Linux kernel gpio subsystem maintainer Linus Walleij presented GPIO descriptors in this presentation:

Linus W. details kernel-external API changes:

Linus W. emphasized these points:

The Rules of Linux Userspace GPIO

  1. You do not access GPIOs from userspace
  2. YOU DO NOT ACCESS GPIOS FROM USERSPACE
  3. Read Documentation/gpio/drivers-on-gpio.txt
  4. Use the character device

Features the Character Device Has

pdp7 commented 6 years ago

Information on when these gpiolib features were merged into the kernel:

gpio: add a userspace chardev ABI for GPIOs

gpio: add userspace ABI for GPIO line information

gpio: userspace ABI for reading/writing GPIO lines

gpio: userspace ABI for reading GPIO line events

pdp7 commented 6 years ago

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>
pdp7 commented 6 years ago

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:

bindings: python #5 @brgl writes:

Seems like ctypes is the way to go and it should be quite easy to implement.

pdp7 commented 6 years ago

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.

pdp7 commented 6 years ago

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

pdp7 commented 6 years ago

bookwar/python-gpiodev

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.

silver2row commented 6 years ago

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.

silver2row commented 6 years ago

@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?

silver2row commented 6 years ago

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.

silver2row commented 6 years ago

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 commented 6 years ago

@silver2row check out libgpiod repo and bindings: python #5.

There are comments in there by @sgjava

silver2row commented 6 years ago

@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.

pdp7 commented 5 years ago

@silver2row thanks, that is very interesting

silver2row commented 5 years ago

@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.