Adafruit GPIO Library for CHIP


#1

Since I’m planning on using some of the Adafruit Python libraries for my CHIP project, I decided to add support for the CHIP in their GPIO library.

I forked their repo and added support for GPIO (CSI0-7 and XIOP0-7) and I2C; SPI and PWM are not. I don’t have any devices that use SPI or PWM, nor do they show up in CHIPs Debian GUI image.

Edge detection and GPIO callbacks are not implemented at this moment.

I verified that the GPIO functions work for setup, input, and cleanup. I don’t have anything hooked up to write too, but it should work. I haven’t tested the I2C code, but all I did was add support for getting the default I2C bus.

Repo is here:

You also need to install CHIP_IO: https://github.com/xtacocorex/CHIP_IO

I plan on submitting a pull request to get this merged into Adafruits’ repo.

Scripts created with this library that control the GPIO pins need to be run with root permissions.

  • GPIO: Total control of GPIO through CHIP_IO
  • I2C: Control of i2c devices through python-smbus (in the future it will be with Adafruits native python i2c library)
    The default I2C bus is now i2c-2 as i2c-1 was removed in OS 1.1 (4.4 Kernel)
  • SPI: Control of SPI devices through python-spidev (with appropriate DTB setup)
  • PWM: Control of the Hardware PWM (with appropriate DTB setup) and Software PWM through CHIP_IO

[SOLVED] Problems with GPIO
Looking for some advice
C.h.i.p nixie type clock VFD
How do i create an aplicación that is able to use chip i/o
My webcam finally works with old built-root kernel customization, Thanks JKW
Can I make a GPIO controlled video player with C.H.I.P. SDK?
Installing LIRC on C.H.I.P
Programming with GPIO
CHIP IO: Library for interfacing with all the things (v0.7.1)
I wrote a GPIO "library" for Python you guys can use!
Will windows app running under wine on CHIP be able to use the pin inputs and outputs?
Starting point for a complete newb?
Help Connecting 4 digit 7 segment via i2c
Custom Weather Station with Adafruit.IO
Newbie to CHIP, oldie to RPi Zero. GPIO question
Some questions about GPIO and C.H.I.P. console emulators
Walkthrough for Beginners! Adafruit Sensors+CHIP GPIO
C.H.I.P. based S0 logger for digital power meters
Second serial port
GPIO to User Input
#2

@xtacocorex Great idea and effort! I am a bit curious as to why you set C.H.I.P. to platform number 5 instead of 4 - is it the belief in some cultures that 4 is an unlucky number? Yeah, some of us actually do read all of the code Out There :grinning:

in Platform.py:

 RASPBERRY_PI     = 1
 BEAGLEBONE_BLACK = 2
 MINNOWBOARD      = 3
 CHIP             = 5

#3

@Jim_Manley I was wondering if I’d get a comment about this. :slight_smile:

There is currently a pull request in waiting at Adafruits’s repo that adds the Intel Edison as number 4, I didn’t want to cause a merge issue in the future.

Thanks for looking over the code, I appreciate the second set of eyes on it for sanity sake.


#4

Ah, of course, good ol’ Thomas Edison trying to usurp the limelight from the grave (pun fully intended, since he ran the lab that developed the first long-lived incandescent electric light) … with a little help from the 800-pound marketing gorilla, Intel :wink:

That’s interesting that they have a pull request in for an update to the Python GPIO library for the Edison - I wonder what took them so long, given how many engineers work there and the supposed educational purpose of the Edison product line.

As for as a second set of eyes, they’re winding up crossed from reading so much code, and I’m not sure whether anything I had to say would be considered sane by some :stuck_out_tongue_winking_eye:

I will be trying the GPIO library out, though, as I’m using C.H.I.P.s to teach computing principles to kids to whom I will be giving them away given their low cost … once they’re available in volume (heavy sigh).


#5

Thank you for this!

Did you have any issues installing? I did the instructions from the readme, and Python is not finding the module. See below. Perhaps I need to change a location in easy-install.pth?

sudo apt-get update
sudo apt-get install build-essential python-pip python-dev python-smbus git
git clone https://github.com/xtacocorex/Adafruit_Python_GPIO.git
cd Adafruit_Python_GPIO
sudo python setup.py install

sudo python -c "import Adafruit_BBIO.GPIO as GPIO; print GPIO"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named Adafruit_BBIO.GPIO

#6

@aninternetof you have a typo in your command, it should be:

sudo python -c "import Adafruit_GPIO.GPIO as GPIO; print GPIO"

#7

@xtacocorex, thank you! That fixed it. I had pulled the name from Adafruit’s tutorial.

Do you also have a sec for a quick usage example? The code below is giving me “AttributeError: ‘module’ object has no attribute ‘start’”

import Adafruit_GPIO.PWM as PWM
import time

print "Starting motor"
PWM.start("XIOP1", 50);

print "Sleeping"
time.sleep(2)

print "Stopping motor"
PWM.stop("XIOP1")
PWM.cleanup()

#8

@xtacocorex. Oh, I see in your initial post that PWM is not implemented.


#9

But, clearly I’m still importing incorrectly, because the following code gives me “AttributeError: ‘module’ object has no attribute ‘setup’”

import Adafruit_GPIO.GPIO as GPIO

GPIO.setup("XIOP1", GPIO.OUT);
print "setting GPIO"
GPIO.output("XIOP1", GPIO.HIGH)

GPIO.cleanup();

#10

You need to create a CHIP GPIO object:

gpio = GPIO.CHIPGPIOAdapter()
gpio.setup("XIOP1,GPIO.OUT)
print "setting GPIO"
gpio.output("XIOP1",GPIO.HIGH)
gpio.cleanup()

Or you could do the following which auto-detects the type of computer:

gpio = GPIO.get_platform_gpio()
gpio.setup("XIOP1,GPIO.OUT)
print "setting GPIO"
gpio.output("XIOP1",GPIO.HIGH)
gpio.cleanup()

I wasn’t able to fully test the GPIO portion of library when I ported it, so there may be bugs. I’m currently away from all of my gear as I’m on work travel at the moment so I can’t do any live testing.


#11

Dude thx for great work, but we need spi, for spi tft’s


#12

Thank you, thank you. Looking through the code, I see the structure now: Adafruit_GPIO imports Adafruit_BBIO if the board happens to be a Beaglebone Black.


#13

@pyro0139 I was searching these forums looking for a way to enable spi in the Debian+GUI install on the CHIP, but I wasn’t successful. If you know if a guide in the forums that enables SPI without a kernel update, let me know. I plan on building a custom kernel soon, so if it’s something that has to be built in, I’ll try to add it and then add support for SPI in the library.

I have a feeling PWM will be a kernel update too…


#14

Awesome work! @xtacocorex


#15

Update:
I added PWM, I think. I have no way to test currently and I’m not sure it’s fully supported in Debian for the CHIP at the moment. If someone has a buildroot OS installed that has working PWM can you please test?

I’m assuming the PWM stuff in sysfs is

/sys/class/pwm/pwm0

I also looked over the SPI code and there are no hardware specifics for either the RPi or Beagleboard Black; it uses the Python spidev module. This may work on CHIPs that have proper SPI support. For those looking to use this with SPI, I would say try it out and if it doesn’t work, it’s most likely the computer not being setup to deal with SPI (Debian).


#16

Great work, haven’t tested yet but I’m hoping to do it tomorrow. Did a quick review and looks good, saw an astray print on https://github.com/adafruit/Adafruit_Python_GPIO/compare/master...xtacocorex:master#diff-0cdd0e84cb93118182360ce6cb55a2c6R447 but looks :thumbsup:


#17

Thanks for the heads up! I thought I had removed all of my debug print statements. Fix pushed up to the repo.


#18

Thanks for this! I can confirm that this simple example works (updated for more recent version of the library:

import time
import CHIP_IO.GPIO as GPIO

# Pin configuration.
pin = "XIO-P0"
   
# Setup input for pin
GPIO.setup(pin,GPIO.IN)

# Poll the pin in a loop
try:
while True:
    # read the pin every half a second
    btn = GPIO.input(pin)
    print btn
    time.sleep(0.5)

except KeyboardInterrupt:
GPIO.cleanup()       # clean up GPIO on CTRL+C exit
GPIO.cleanup()           # clean up GPIO on normal exit

#19

@pnyboer Thanks!

I think I’m going to port over the Beaglebone Black GPIO library (which is itself a port of RPi.GPIO) to the CHIP. That should bring about the edge detection support.


#20

Update!

This library now requires CHIP_IO, which can be found here: https://github.com/xtacocorex/CHIP_IO.

@pnyboer: The CHIP_IO requirement allows for event_detection, I haven’t tested it, but I ported the code from the Beaglebone Black, so it should work.