Giter VIP home page Giter VIP logo

chip_io's Introduction

CHIP_IO

A CHIP GPIO library

Debian File Installation:

There are now pre-compiled binary deb files for the CHIP that do not require any build tools on a CHIP/CHIP Pro.

Go to this page: https://github.com/xtacocorex/CHIP_IO/releases/latest Or Go to this page: https://xtacocorex.github.io/chip_io_releases/index.html

Download the .deb file for the version of Python you are running. Then install with dpkg, like the following example:

sudo dpkg -i python-chip-io_0.5.9-1_armhf.deb

Manual Installation:

For Python2.7:

sudo apt-get update
sudo apt-get install git build-essential python-dev python-pip flex bison chip-dt-overlays -y
git clone git://github.com/xtacocorex/CHIP_IO.git
cd CHIP_IO
sudo python setup.py install
cd ..

For Python3:

sudo apt-get update
sudo apt-get install git build-essential python3-dev python3-pip flex bison chip-dt-overlays -y
git clone git://github.com/xtacocorex/CHIP_IO.git
cd CHIP_IO
sudo python3 setup.py install
cd ..

PyPi Installation:

For Python2.7:

sudo apt-get update
sudo apt-get install git build-essential python-dev python-pip flex bison chip-dt-overlays -y
sudo pip install CHIP-IO

For Python3:

sudo apt-get update
sudo apt-get install git build-essential python3-dev python3-pip flex bison chip-dt-overlays -y
sudo pip3 install CHIP-IO

Usage

Using the library is very similar to the excellent RPi.GPIO library used on the Raspberry Pi. Below are some examples.

All scripts that require GPIO, PWM (HW and/or SW), and Overlay Manager need to be run with super user permissions!

Allowable Pin Names for the Library

The following "table" is the allowable pin names that are able to be used by the library. The Name column is the normal name used on the CHIP Headers, the Alt Name column is the value used by the PocketCHIP header (if it's broken out), and the Key is the Header and Pin Number the the Pin is physically located. Either of these 3 means is able to specify a pin in CHIP_IO.

CHIP (Main Name) PocketCHIP/CHIP Pro Name Key (Alt Name) HW Support Edge Detect
TWI1-SDA KPD-I2C-SDA U13_9 CHIP/CHIP PRO NO
TWI1-SCK KPD-I2C-SCL U13_11 CHIP/CHIP PRO NO
LCD-D2 UART2-TX U13_17 CHIP/CHIP PRO NO
PWM0 PWM0 U13_18 CHIP/CHIP PRO NO
PWM1 PWM1 EINT13 CHIP PRO YES
LCD-D4 UART2-CTS U13_19 CHIP/CHIP PRO NO
LCD-D3 UART2-RX U13_20 CHIP/CHIP PRO NO
LCD-D6 LCD-D6 U13_21 CHIP NO
LCD-D5 UART2-RTS U13_22 CHIP/CHIP PRO NO
LCD-D10 LCD-D10 U13_23 CHIP NO
LCD-D7 LCD-D7 U13_24 CHIP NO
LCD-D12 LCD-D12 U13_25 CHIP NO
LCD-D11 LCD-D11 U13_26 CHIP NO
LCD-D14 LCD-D14 U13_27 CHIP NO
LCD-D13 LCD-D13 U13_28 CHIP NO
LCD-D18 LCD-D18 U13_29 CHIP NO
LCD-D15 LCD-D15 U13_30 CHIP NO
LCD-D20 LCD-D20 U13_31 CHIP NO
LCD-D19 LCD-D19 U13_32 CHIP NO
LCD-D22 LCD-D22 U13_33 CHIP NO
LCD-D21 LCD-D21 U13_34 CHIP NO
LCD-CLK LCD-CLK U13_35 CHIP NO
LCD-D23 LCD-D23 U13_36 CHIP NO
LCD-VSYNC LCD-VSYNC U13_37 CHIP NO
LCD-HSYNC LCD-HSYNC U13_38 CHIP NO
LCD-DE LCD-DE U13_40 CHIP NO
UART1-TX UART-TX U14_3 CHIP/CHIP PRO NO
UART1-RX UART-RX U14_5 CHIP/CHIP PRO NO
LRADC ADC U14_11 CHIP/CHIP PRO NO
XIO-P0 XIO-P0 U14_13 CHIP YES
XIO-P1 XIO-P1 U14_14 CHIP YES
XIO-P2 GPIO1 U14_15 CHIP YES
XIO-P3 GPIO2 U14_16 CHIP YES
XIO-P4 GPIO3 U14_17 CHIP YES
XIO-P5 GPIO4 U14_18 CHIP YES
XIO-P6 GPIO5 U14_19 CHIP YES
XIO-P7 GPIO6 U14_20 CHIP YES
AP-EINT1 KPD-INT U14_23 CHIP/CHIP PRO YES
AP-EINT3 AP-INT3 U14_24 CHIP/CHIP PRO YES
TWI2-SDA I2C-SDA U14_25 CHIP/CHIP PRO NO
TWI2-SCK I2C-SCL U14_26 CHIP/CHIP PRO NO
CSIPCK SPI-SEL U14_27 CHIP/CHIP PRO NO
CSICK SPI-CLK U14_28 CHIP/CHIP PRO NO
CSIHSYNC SPI-MOSI U14_29 CHIP/CHIP PRO NO
CSIVSYNC SPI-MISO U14_30 CHIP/CHIP PRO NO
CSID0 D0 U14_31 CHIP/CHIP PRO NO
CSID1 D1 U14_32 CHIP/CHIP PRO NO
CSID2 D2 U14_33 CHIP/CHIP PRO NO
CSID3 D3 U14_34 CHIP/CHIP PRO NO
CSID4 D4 U14_35 CHIP/CHIP PRO NO
CSID5 D5 U14_36 CHIP/CHIP PRO NO
CSID6 D6 U14_37 CHIP/CHIP PRO NO
CSID7 D7 U14_38 CHIP/CHIP PRO NO
I2S-MCLK EINT19 21 CHIP PRO YES
I2S-BCLK I2S-BCLK 22 CHIP PRO NO
I2S-LCLK I2S-LCLK 23 CHIP PRO NO
I2S-DO EINT19 24 CHIP PRO NO
I2S-DI EINT24 25 CHIP PRO YES

GPIO Setup

Import the library, and setup as GPIO.OUT or GPIO.IN:

import CHIP_IO.GPIO as GPIO
GPIO.setup("CSID0", GPIO.OUT)

You can also refer to the pin number:

GPIO.setup("U14_31", GPIO.OUT)

You can also refer to the bin based upon its alternate name:

GPIO.setup("GPIO1", GPIO.IN)

GPIO Miscellaneous

Debug can be enabled/disabled by the following command:

# Enable Debug
GPIO.toggle_debug()

You can determine if the hardware is a CHIP/CHIP Pro using the following:

# Determine hardware
# 0 For CHIP
# 1 For CHIP Pro
GPIO.is_chip_pro()

GPIO Output

Setup the pin for output, and write GPIO.HIGH or GPIO.LOW. Or you can use 1 or 0.:

import CHIP_IO.GPIO as GPIO
GPIO.setup("CSID0", GPIO.OUT)
GPIO.output("CSID0", GPIO.HIGH)

GPIO Input

Inputs work similarly to outputs.:

import CHIP_IO.GPIO as GPIO
GPIO.setup("CSID0", GPIO.IN)

Other options when setting up pins:

# Specify pull up/pull down settings on a pin
GPIO.setup("CSID0", GPIO.IN, pull_up_down=GPIO.PUD_UP)

# Specify initial value for an output
GPIO.setup("CSID0", GPIO.OUT, initial=1)

Pull Up/Down values are only for pins that are provided by the R8, the XIO are not capable of this. The allowable values are: PUD_OFF, PUD_UP, and PUD_DOWN.

Polling inputs:

if GPIO.input("CSID0"):
    print("HIGH")
else:
    print("LOW")

Read lots of data:

# Get 8 bits of data in one shot
mybyte = GPIO.read_byte("LCD-D3")

# Get 16 bits of data in one shot
myword = GPIO.read_word("XIO-P4")

This code was initially added by brettcvz and I cleaned it up and expanded it.

You can quickly change a pins direction:

GPIO.direction("XIO-P3", GPIO.OUT)
GPIO.direction("XIO-P3", GPIO.IN)

You can also re-setup a pin in order to change direction, note that this is a slower operation:

GPIO.setup("XIO-P3", GPIO.OUT)
GPIO.setup("XIO-P3", GPIO.IN)

The edge detection code below only works for the AP-EINT1, AP-EINT3, and XPO Pins on the CHIP.

Waiting for an edge (GPIO.RISING, GPIO.FALLING, or GPIO.BOTH:

GPIO.wait_for_edge(channel, GPIO.RISING)

Detecting events:

GPIO.setup("XIO-P0", GPIO.IN)
GPIO.add_event_detect("XIO-P0", GPIO.FALLING)
#your amazing code here

#detect wherever:
if GPIO.event_detected("XIO-P0"):
    print "event detected!"

CHIP_IO can also handle adding callback functions on any pin that supports edge detection. Note that only one callback function can be specified per Pin, if you try to set more, an exception will be thrown.:

def mycallback(channel):
    print("we hit the edge we want")

GPIO.setup("GPIO3", GPIO.IN)

# Add Event Detect and Callback Separately for Falling Edge
GPIO.add_event_detect("GPIO3", GPIO.FALLING)
GPIO.add_event_callback("GPIO3", mycallback)

# Add Event Detect and Callback Separately for Rising Edge
GPIO.add_event_detect("GPIO3", GPIO.RISING)
GPIO.add_event_callback("GPIO3", mycallback)

# Add Callback for Both Edges using the add_event_detect() method
GPIO.add_event_detect("GPIO3", GPIO.BOTH, mycallback)

# Remove callback with the following
GPIO.remove_event_detect("GPIO3")

# bouncetime is also able to be set for both GPIO.add_event_detect() and GPIO.add_event_callback()
GPIO.add_event_detect("GPIO3", GPIO.FALLING, bouncetime=300)
GPIO.add_event_callback("GPIO3", GPIO.RISING, mycallback, bouncetime=300)

GPIO Cleanup

To clean up the GPIO when done, do the following:

# Clean up every exported GPIO Pin
GPIO.cleanup()

# Clean up a single pin (keeping everything else intact)
GPIO.cleanup("XIO-P0")

PWM:

Hardware PWM requires a DTB Overlay loaded on the CHIP to allow the kernel to know there is a PWM device available to use. :: import CHIP_IO.PWM as PWM # Determine hardware # 0 For CHIP # 1 For CHIP Pro PWM.is_chip_pro()

# Enable/Disable Debug PWM.toggle_debug()

#PWM.start(channel, duty, freq=2000, polarity=0) #duty values are valid 0 (off) to 100 (on) PWM.start("PWM0", 50) PWM.set_duty_cycle("PWM0", 25.5) PWM.set_frequency("PWM0", 10)

# To stop PWM PWM.stop("PWM0") PWM.cleanup()

#For specific polarity: this example sets polarity to 1 on start: PWM.start("PWM0", 50, 2000, 1)

SOFTPWM:

import CHIP_IO.SOFTPWM as SPWM
# Determine hardware
# 0 For CHIP
# 1 For CHIP Pro
SPWM.is_chip_pro()

# Enable/Disable Debug
SPWM.toggle_debug()

#SPWM.start(channel, duty, freq=2000, polarity=0)
#duty values are valid 0 (off) to 100 (on)
#you can choose any pin
SPWM.start("XIO-P7", 50)
SPWM.set_duty_cycle("XIO-P7", 25.5)
SPWM.set_frequency("XIO-P7", 10)

# To Stop SPWM
SPWM.stop("XIO-P7")

# Cleanup
SPWM.cleanup()

#For specific polarity: this example sets polarity to 1 on start:
SPWM.start("XIO-P7", 50, 2000, 1)

Use SOFTPWM at low speeds (hundreds of Hz) for the best results. Do not use for anything that needs high precision or reliability.

If using SOFTPWM and PWM at the same time, import CHIP_IO.SOFTPWM as SPWM or something different than PWM as to not confuse the library.

SERVO:

import CHIP_IO.SERVO as SERVO
# Determine hardware
# 0 For CHIP
# 1 For CHIP Pro
SERVO.is_chip_pro()

# Enable/Disable Debug
SERVO.toggle_debug()

#SPWM.start(channel, angle=0, range=180)
#angle values are between +/- range/2)
#you can choose any pin except the XIO's
SERVO.start("CSID4", 50)
SERVO.set_angle("CSID4", 25.5)
SERVO.set_range("CSID4", 90)

# To Stop Servo
SERVO.stop("CSID4")

# Cleanup
SERVO.cleanup()

The Software Servo control only works on the LCD and CSI pins. The XIO is too slow to control.

LRADC:

The LRADC was enabled in the 4.4.13-ntc-mlc. This is a 6 bit ADC that is 2 Volt tolerant. Sample code below details how to talk to the LRADC.:

import CHIP_IO.LRADC as ADC
# Enable/Disable Debug
ADC.toggle_debug()

# Check to see if the LRADC Device exists
# Returns True/False
ADC.get_device_exists()

# Setup the LRADC
# Specify a sampling rate if needed
ADC.setup(rate)

# Get the Scale Factor
factor = ADC.get_scale_factor()

# Get the allowable Sampling Rates
sampleratestuple = ADC.get_allowable_sample_rates()

# Set the sampling rate
ADC.set_sample_rate(rate)

# Get the current sampling rate
currentrate = ADC.get_sample_rate()

# Get the Raw Channel 0 or 1 data
raw = ADC.get_chan0_raw()
raw = ADC.get_chan1_raw()

# Get the factored ADC Channel data
fulldata = ADC.get_chan0()
fulldata = ADC.get_chan1()

SPI:

SPI requires a DTB Overlay to access. CHIP_IO does not contain any SPI specific code as the Python spidev module works when it can see the SPI bus.

Overlay Manager:

The Overlay Manager enables you to quickly load simple Device Tree Overlays. The options for loading are: PWM0, SPI2, CUST. The Overlay Manager is smart enough to determine if you are trying to load PWM on a CHIP Pro and will fail due to the base DTB for the CHIP Pro supporting PWM0/1 out of the box.

Only one of each type of overlay can be loaded at a time, but all three options can be loaded simultaneously. So you can have SPI2 without PWM0, but you cannot have SPI2 loaded twice. :: import CHIP_IO.OverlayManager as OM # The toggle_debug() function turns on/off debug printing OM.toggle_debug()

# To load an overlay, feed in the name to load() OM.load("PWM0")

# To verify the overlay was properly loaded, the get functions return booleans OM.get_pwm_loaded() OM.get_spi_loaded()

# To unload an overlay, feed in the name to unload() OM.unload("PWM0")

To use a custom overlay, you must build and compile it properly per the DIP Docs: http://docs.getchip.com/dip.html#development-by-example There is no verification that the Custom Overlay is setup properly, it's fire and forget :: import CHIP_IO.OverlayManager as OM # The full path to the dtbo file needs to be specified OM.load("CUST","/home/chip/projects/myfunproject/overlays/mycustomoverlay.dtbo")

# You can check for loading like above, but it's really just there for sameness OM.get_custom_loaded()

# To unload, just call unload() OM.unload("CUST")

OverlayManager requires a 4.4 kernel with the CONFIG_OF_CONFIGFS option enabled in the kernel config.

Utilties:

CHIP_IO now supports the ability to enable and disable the 1.8V port on U13. This voltage rail isn't enabled during boot.

To use the utilities, here is sample code:

import CHIP_IO.Utilities as UT
# Enable/Disable Debug
UT.toggle_debug()

# Enable 1.8V Output
UT.enable_1v8_pin()

# Set 2.0V Output
UT.set_1v8_pin_voltage(2.0)

# Set 2.6V Output
UT.set_1v8_pin_voltage(2.6)

# Set 3.3V Output
UT.set_1v8_pin_voltage(3.3)

# Disable 1.8V Output
UT.disable_1v8_pin()

# Get currently-configured voltage (returns False if the pin is not enabled as output)
UT.get_1v8_pin_voltage()

# Unexport Everything
UT.unexport_all()

# Determine if you are running a CHIP/CHIP Pro
# This returns True if the computer is a CHIP Pro and False if it is a CHIP
UT.is_chip_pro()

Running tests

Install py.test to run the tests. You'll also need the python compiler package for py.test.:

# Python 2.7
sudo apt-get install python-pytest
# Python 3
sudo apt-get install python3-pytest

To run the tests, do the following.:

# If only one version of Python is installed
# Python 2
sudo make pytest2
# Python 3
sudo make pytest3
# If more than one version of Python, run through both
sudo make test

Credits

The CHIP IO Python library was originally forked from the Adafruit Beaglebone IO Python Library. The BeagleBone IO Python library was originally forked from the excellent MIT Licensed [RPi.GPIO](https://code.google.com/p/raspberry-gpio-python) library written by Ben Croston.

License

CHIP IO port by Robert Wolterman, released under the MIT License. Beaglebone IO Library Written by Justin Cooper, Adafruit Industries. BeagleBone IO Python library is released under the MIT License.

chip_io's People

Contributors

bluesolder avatar fabien-gigante avatar fordsfords avatar hbradio avatar meitelwein avatar mzealey avatar streamnsight avatar tryonlinux avatar werecatf avatar xtacocorex avatar zerotri avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

chip_io's Issues

Add Support for Loading DTB Overlays

Prepare for the future by adding a feature to enable a user to load/unload a DTB Overlay.

Example:
Load:
sudo mkdir /sys/kernel/config/device-tree/overlays/DIPexample
su -c 'cat irtempi2c.dtbo > /sys/kernel/config/device-tree/overlays/DIPexample/dtbo'

Unload:
sudo rmdir /sys/kernel/config/device-tree/overlays/DIPexample/

LRADC Support

With the proper kernel and DTB, the LRADC is enabled. Get this working. Probably implement purely in Python as register setting was causing segfaults in my test code.

SPI fails to load - sun4i-spi 1c17000.spi: prop pinctrl-0 index 0 invalid phandle

I am using CHIP version 4.4.13-ntc-mlc #1 SMP Tue Dec 6 21:38:00 UTC 2016 armv7l GNU/Linux
when running

import CHIP_IO.OverlayManager as OM
OM.load("SPI2")

in the console I am getting
sun4i-spi 1c17000.spi: prop pinctrl-0 index 0 invalid phandle
sun4i-spi 1c17000.spi: chipselect 0 already in use
spi_master spi32766: spi_device register error /soc@01c00000/spi@01c17000/spi2@0
of_spi_notify: failed to create for '/soc@01c00000/spi@01c17000/spi2@0'
__of_changeset_entry_notify: notifier error @/soc@01c00000/spi@01c17000/spi2@0

Any ideas how to resolve it?

tests failing

Running sudo py.test on a fresh install results in 9 failures:

============================= test session starts ==============================
platform linux2 -- Python 2.7.9 -- py-1.4.25 -- pytest-2.6.3
collected 63 items

test/test_gpio_input.py ..
test/test_gpio_output.py ......
test/test_gpio_setup.py ..........
test/test_pwm_setup.py FFFF...........F.........
test/test_softpwm_setup.py ...............softpwm_start failedFsoftpwm_start failedFsoftpwm_start failedFsoftpwm_start failedF.

=================================== FAILURES ===================================
_________________________ TestPwmSetup.test_start_pwm __________________________

self = <test_pwm_setup.TestPwmSetup instance at 0xb668fb70>

    def test_start_pwm(self):
        PWM.start("PWM0", 0)

        pwm_test = '/sys/class/pwm/pwmchip0/pwm0'

>       assert os.path.exists(pwm_test)
E       assert <function exists at 0xb6d80d30>('/sys/class/pwm/pwmchip0/pwm0')
E        +  where <function exists at 0xb6d80d30> = <module 'posixpath' from '/usr/lib/python2.7/posixpath.pyc'>.exists
E        +    where <module 'posixpath' from '/usr/lib/python2.7/posixpath.pyc'> = os.path

test/test_pwm_setup.py:15: AssertionError
________________ TestPwmSetup.test_start_pwm_with_polarity_one _________________

self = <test_pwm_setup.TestPwmSetup instance at 0xb67804b8>

    def test_start_pwm_with_polarity_one(self):
        PWM.start("PWM0", 0, 2000, 1)

        pwm_test = '/sys/class/pwm/pwmchip0/pwm0'

>       assert os.path.exists(pwm_test)
E       assert <function exists at 0xb6d80d30>('/sys/class/pwm/pwmchip0/pwm0')
E        +  where <function exists at 0xb6d80d30> = <module 'posixpath' from '/usr/lib/python2.7/posixpath.pyc'>.exists
E        +    where <module 'posixpath' from '/usr/lib/python2.7/posixpath.pyc'> = os.path

test/test_pwm_setup.py:27: AssertionError
______________ TestPwmSetup.test_start_pwm_with_polarity_default _______________

self = <test_pwm_setup.TestPwmSetup instance at 0xb6780be8>

    def test_start_pwm_with_polarity_default(self):
        PWM.start("PWM0", 0, 2000, 0)

        pwm_test = '/sys/class/pwm/pwmchip0/pwm0'

>       assert os.path.exists(pwm_test)
E       assert <function exists at 0xb6d80d30>('/sys/class/pwm/pwmchip0/pwm0')
E        +  where <function exists at 0xb6d80d30> = <module 'posixpath' from '/usr/lib/python2.7/posixpath.pyc'>.exists
E        +    where <module 'posixpath' from '/usr/lib/python2.7/posixpath.pyc'> = os.path

test/test_pwm_setup.py:41: AssertionError
________________ TestPwmSetup.test_start_pwm_with_polarity_zero ________________

self = <test_pwm_setup.TestPwmSetup instance at 0xb66bd698>

    def test_start_pwm_with_polarity_zero(self):
        PWM.start("PWM0", 0, 2000, 0)

        pwm_test = '/sys/class/pwm/pwmchip0/pwm0'

>       assert os.path.exists(pwm_test)
E       assert <function exists at 0xb6d80d30>('/sys/class/pwm/pwmchip0/pwm0')
E        +  where <function exists at 0xb6d80d30> = <module 'posixpath' from '/usr/lib/python2.7/posixpath.pyc'>.exists
E        +    where <module 'posixpath' from '/usr/lib/python2.7/posixpath.pyc'> = os.path

test/test_pwm_setup.py:55: AssertionError
_____________________ TestPwmSetup.test_pwm_duty_modified ______________________

self = <test_pwm_setup.TestPwmSetup instance at 0xb66bdaf8>

    def test_pwm_duty_modified(self):
        PWM.start("PWM0", 0)

        pwm_test = '/sys/class/pwm/pwmchip0/pwm0'

>       assert os.path.exists(pwm_test)
E       assert <function exists at 0xb6d80d30>('/sys/class/pwm/pwmchip0/pwm0')
E        +  where <function exists at 0xb6d80d30> = <module 'posixpath' from '/usr/lib/python2.7/posixpath.pyc'>.exists
E        +    where <module 'posixpath' from '/usr/lib/python2.7/posixpath.pyc'> = os.path

test/test_pwm_setup.py:115: AssertionError
_________ TestSoftpwmSetup.test_pwm_duty_cycle_invalid_value_negative __________

self = <test_softpwm_setup.TestSoftpwmSetup instance at 0xb66a4238>

    def test_pwm_duty_cycle_invalid_value_negative(self):
>       PWM.start("XIO-P7", 0)
E       RuntimeError: Error starting softpwm on pin U14_20 (gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy)
E       Error setting up softpwm on pin 1023, maybe already exported? (gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy)))

test/test_softpwm_setup.py:85: RuntimeError
__________ TestSoftpwmSetup.test_pwm_duty_cycle_invalid_value_string ___________

self = <test_softpwm_setup.TestSoftpwmSetup instance at 0xb66a48a0>

    def test_pwm_duty_cycle_invalid_value_string(self):
>       PWM.start("XIO-P7", 0)
E       RuntimeError: Error starting softpwm on pin U14_20 (gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy)
E       Error setting up softpwm on pin 1023, maybe already exported? (gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy))
E       gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy)
E       Error setting up softpwm on pin 1023, maybe already exported? (gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy)
E       Error setting up softpwm on pin 1023, maybe already exported? (gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy))
E       gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy)))

test/test_softpwm_setup.py:91: RuntimeError
__________ TestSoftpwmSetup.test_pwm_frequency_invalid_value_negative __________

self = <test_softpwm_setup.TestSoftpwmSetup instance at 0xb66b6080>

    def test_pwm_frequency_invalid_value_negative(self):
>       PWM.start("XIO-P7", 0)
E       RuntimeError: Error starting softpwm on pin U14_20 (gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy)
E       Error setting up softpwm on pin 1023, maybe already exported? (gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy))
E       gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy)
E       Error setting up softpwm on pin 1023, maybe already exported? (gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy)
E       Error setting up softpwm on pin 1023, maybe already exported? (gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy))
E       gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy))
E       gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy)
E       Error setting up softpwm on pin 1023, maybe already exported? (gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy)
E       Error setting up softpwm on pin 1023, maybe already exported? (g)

test/test_softpwm_setup.py:97: RuntimeError
___________ TestSoftpwmSetup.test_pwm_frequency_invalid_value_string ___________

self = <test_softpwm_setup.TestSoftpwmSetup instance at 0xb66551e8>

    def test_pwm_frequency_invalid_value_string(self):
>       PWM.start("XIO-P7", 0)
E       RuntimeError: Error starting softpwm on pin U14_20 (gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy)
E       Error setting up softpwm on pin 1023, maybe already exported? (gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy))
E       gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy)
E       Error setting up softpwm on pin 1023, maybe already exported? (gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy)
E       Error setting up softpwm on pin 1023, maybe already exported? (gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy))
E       gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy))
E       gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy)
E       Error setting up softpwm on pin 1023, maybe already exported? (gpio_export: could not write '1023' to /sys/class/gpio/export (Device or resource busy)
E       Error setting up softpwm on pin 1023, maybe already exported? (g)

test/test_softpwm_setup.py:103: RuntimeError
===================== 9 failed, 54 passed in 2.12 seconds ======================

Maintain Python3 Support

This is a blanket issue to ensure Python3 support remains working for all CHIP_IO features. I don't actively use Python3, so it's sometimes left to the wayside.

LRADC definitely has issues in Python3 per #42.

I have a feeling this will be closed when @jdcc does work on his fork.

Handle CHIP Pro

The NTC CHIP Pro is a smaller CHIP with the GR8 SOC that is designed for embedded applications.

The IO of the CHIP Pro is slightly different from the CHIP.

Need to come up with an elegant way to handle IO on the CHIP Pro while maintaining compatibility with the CHIP/PocketCHIP.

Remove i2c-1 DTB Overlay?

With the 4.4.13-ntc-mlc OS drop from NTC, i2c-1 is back in the main dtb in /boot.

Do we want to remove this capability from OverlayManager?

Add pin aliases

As discussed in #6 , "GPIO0" is not an allowed pin name.

The allowable names are what are defined by NTC on their documentation or the header and pin number.

The PocketCHIP does have a breakout of some pins at the top and there are indeed pins labeled "GPIO1" to "GPIO6". So it would be cool if these alternate names would be allowed.

PWM lines

just a question here:

I just realize there only seem to be one PWM pin, (PWM0)
Can other pins be used for HW PWM, or is that it?

'coz I was looking to control a few motors, but if there is only one pin that's not going to work! :-/

GPIO.cleanup() is broken

I introduced a weird corner case when using GPIO.cleanup() in implementing #43. In the scenario when you just import CHIP_IO.GPIO and immediately do a cleanup on all pins, thing's break.

Test Code:

import CHIP_IO.GPIO as GPIO
GPIO.cleanup()

If you actually export a pin from inside CHIP_IO and then do a GPIO.cleanup(), it's happy.

@fordsfords I think the issue is with the C code and the Python API. I know you know nothing about the Python API part, but could you look at: 8ecec67
(py_cleanup in py_gpio.c) and see if I'm doing that strcmp right?

My initial debugging had me put an if statement around the close(epfd) in event_cleanup() inside event_gpio.c. That didn't help the situation.

internal pull up/down resistor

Does chip have internal pull up or down resistors that I can enable? I would prefer this over using a bread board. I have tried using
GPIO.setup("CSID3", GPIO.IN, pull_up_down=GPIO.PUD_UP)
I have also tried "XIO-P0".
I don't get any errors but the pin appears to be floating.

Make Issue Python 3

New to GitHub, so I apologize if this is the wrong place for this, but I was having issues compiling when getting to the below step:

sudo python3 setup.py instal

The issue was something about rpigpiomodule not being defined (forgot to copy it) in py_gpio.c. So I looked at the adafruit-beagalbone repository and saw how they defined it there (https://github.com/adafruit/adafruit-beaglebone-io-python/blob/master/source/py_gpio.c) and added it to my local copy of py_gpio.c at line 862 (Code I added is below). It now compiles without issues. When I figure out how to commit changes back to the repository I will. Everything works perfect now. Thanks for your hard work porting this!!

#if PY_MAJOR_VERSION > 2
static struct PyModuleDef rpigpiomodule = {
   PyModuleDef_HEAD_INIT,
   "GPIO",       // name of module
   moduledocstring,  // module documentation, may be NULL
   -1,               // size of per-interpreter state of the module, or -1 if the module keeps state in global variables.
   gpio_methods
};
#endif

aREST support

More of a feature request than an issue.

Any way you can add aREST support for this? I am not the greatest at programming, but I feel it should be possible as its very close to a RPi.

Touchscreen ADC-s

Hello, as far as I know there are 4 ADC-s (X1, X2, Y1, Y2) for the touchscreen. Would it be possible for you to expose these via the CHIP-IO library?

new_xio: Channel in use when running as non-root

If you're not root (or probably not in the group that is allowed), and try to set up a pin, the following happens:

>>> import CHIP_IO.GPIO as GPIO
>>> GPIO.setup("GPIO0", GPIO.OUT)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Channel in use (already exported)

Even though I am just not allowed to and the channel is actually not exported.

py.test fail on softpwm: segmentation fault

trying this stuff out...I tried the py.test, with these results:

test/test_gpio_input.py FF
test/test_gpio_output.py FFFF..
test/test_gpio_setup.py FFFFFFF..F
test/test_pwm_setup.py FFFF...........F.........
test/test_softpwm_setup.py F...Segmentation fault

running tests fails with python2.7 and python3

  1. The instructions given for running tests are only relevant for python2.7.
  2. sudo py.test fails, and sudo python2 -m pytest fails too:
========================================================================== test session starts ==========================================================================
platform linux2 -- Python 2.7.9 -- py-1.4.25 -- pytest-2.6.3
collected 0 items / 5 errors 

================================================================================ ERRORS =================================================================================
_______________________________________________________________ ERROR collecting test/test_gpio_input.py ________________________________________________________________
test/test_gpio_input.py:5: in <module>
    import CHIP_IO.GPIO as GPIO
E   ImportError: No module named GPIO
_______________________________________________________________ ERROR collecting test/test_gpio_output.py _______________________________________________________________
test/test_gpio_output.py:3: in <module>
    import CHIP_IO.GPIO as GPIO
E   ImportError: No module named GPIO
_______________________________________________________________ ERROR collecting test/test_gpio_setup.py ________________________________________________________________
test/test_gpio_setup.py:4: in <module>
    import CHIP_IO.GPIO as GPIO
E   ImportError: No module named GPIO
________________________________________________________________ ERROR collecting test/test_pwm_setup.py ________________________________________________________________
test/test_pwm_setup.py:5: in <module>
    import CHIP_IO.PWM as PWM
E   ImportError: No module named PWM
______________________________________________________________ ERROR collecting test/test_softpwm_setup.py ______________________________________________________________
test/test_softpwm_setup.py:4: in <module>
    import CHIP_IO.SOFTPWM as PWM
E   ImportError: No module named SOFTPWM
======================================================================== 5 error in 1.23 seconds ========================================================================
  1. installing pytest with pip3/python3 and running sudo python3 -m pytest fails too... :(
========================================================================== test session starts ==========================================================================
platform linux -- Python 3.4.2, pytest-3.0.5, py-1.4.32, pluggy-0.4.0
rootdir: /home/chip/CHIP_IO, inifile: 
collected 0 items / 6 errors 

================================================================================ ERRORS =================================================================================
__________________________________________________________________ ERROR collecting test/lradc_test.py __________________________________________________________________
test/lradc_test.py:11: in <module>
    ADC.setup(125)
CHIP_IO/LRADC.py:57: in setup
    set_sample_rate(rate)
CHIP_IO/LRADC.py:116: in set_sample_rate
    print("Setting Sample Rate to: {0}").format(rate)
E   AttributeError: 'NoneType' object has no attribute 'format'
---------------------------------------------------------------------------- Captured stdout ----------------------------------------------------------------------------
ENABLING LRADC DEBUG OUTPUT
LRADC SETUP WITH SAMPLE RATE OF 125
Setting Sample Rate to: {0}
_______________________________________________________________ ERROR collecting test/test_gpio_input.py ________________________________________________________________
ImportError while importing test module '/home/chip/CHIP_IO/test/test_gpio_input.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
test/test_gpio_input.py:5: in <module>
    import CHIP_IO.GPIO as GPIO
E   ImportError: No module named 'CHIP_IO.GPIO'
_______________________________________________________________ ERROR collecting test/test_gpio_output.py _______________________________________________________________
ImportError while importing test module '/home/chip/CHIP_IO/test/test_gpio_output.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
test/test_gpio_output.py:3: in <module>
    import CHIP_IO.GPIO as GPIO
E   ImportError: No module named 'CHIP_IO.GPIO'
_______________________________________________________________ ERROR collecting test/test_gpio_setup.py ________________________________________________________________
ImportError while importing test module '/home/chip/CHIP_IO/test/test_gpio_setup.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
test/test_gpio_setup.py:4: in <module>
    import CHIP_IO.GPIO as GPIO
E   ImportError: No module named 'CHIP_IO.GPIO'
________________________________________________________________ ERROR collecting test/test_pwm_setup.py ________________________________________________________________
ImportError while importing test module '/home/chip/CHIP_IO/test/test_pwm_setup.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
test/test_pwm_setup.py:5: in <module>
    import CHIP_IO.PWM as PWM
E   ImportError: No module named 'CHIP_IO.PWM'
______________________________________________________________ ERROR collecting test/test_softpwm_setup.py ______________________________________________________________
ImportError while importing test module '/home/chip/CHIP_IO/test/test_softpwm_setup.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
test/test_softpwm_setup.py:4: in <module>
    import CHIP_IO.SOFTPWM as PWM
E   ImportError: No module named 'CHIP_IO.SOFTPWM'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 6 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
======================================================================== 6 error in 1.89 seconds ========================================================================

Error setting up channel X

Hello,

Once a python script using CHIP_IO has exited, when I try to run it again, even with using GPIO.cleanup() before setting up the IO, I'm getting an error:

GPIO.setup(set, GPIO.OUT)
RuntimeError: Error setting up channel CSID2, maybe already exported? (gpio_export: could not write '134' to /sys/class/gpio/export (Device or resource busy))

Is there any workaround to be able to run my script again, without rebooting?
Thanks.

Overlay Manager fails

I'm unable to get the overlay manager to work.

When i type the following in the REPL:

>>> import CHIP_IO.OverlayManager as OM
>>> OM.enable_debug()
>>> OM.load("PWM0")

I get the following error:

LOAD OVERLAY: PWM0 @ 
VALID OVERLAY
CONFIG PATH:  /sys/kernel/config/device-tree/overlays/chip-pwm
OVERLAY PATH: /lib/firmware/chip_io/chip-pwm0.dtbo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/dist-packages/CHIP_IO-0.2.2-py3.4-linux-armv7l.egg/CHIP_IO/OverlayManager.py", line 180, in load
    errc = _set_overlay_verify(overlay.upper(), opath, cpath)
  File "/usr/local/lib/python3.4/dist-packages/CHIP_IO-0.2.2-py3.4-linux-armv7l.egg/CHIP_IO/OverlayManager.py", line 84, in _set_overlay_verify
    os.makedirs(config_path)
  File "/usr/lib/python3.4/os.py", line 227, in makedirs
    makedirs(head, mode, exist_ok)
  File "/usr/lib/python3.4/os.py", line 227, in makedirs
    makedirs(head, mode, exist_ok)
  File "/usr/lib/python3.4/os.py", line 237, in makedirs
    mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/sys/kernel/config/device-tree'

Any feedback would be appreciated. Thanks.

SOFTPWM does not do anything

I have CHIP v1.0 and firmware official build headless 4.4 (Python 2.7.9).

Simply toggling gpio works using this library, but soft pwm does nothing. I have measured both with multimeter and ostilloscope and detected no change in output. I did not get any error messages either.

import time
import CHIP_IO.SOFTPWM as PWM
PWM.start("XIO-P7", 50)
time.sleep(5)
PWM.stop("XIO-P7")
PWM.cleanup()

HW PWM Issues

I just confirmed issues with the HW PWM code.

@fordsfords, looks like your imporovements might have broken it. :(

Will commit my test code for the HW PWM. Need to be running 4.4 with a custom kernel enable CONFIG_OF_CONFIGFS.

Install failed

I'm relatively new to python, and I got chip working, but after reboot its not working anymore:

chip@chip ~/dev> sudo python3 test.py 
[sudo] password for chip: 
Error processing line 1 of /usr/local/lib/python3.4/dist-packages/easy-install.pth:

  Traceback (most recent call last):
    File "/usr/lib/python3.4/site.py", line 177, in addpackage
      if not dircase in known_paths and os.path.exists(dir):
    File "/usr/lib/python3.4/genericpath.py", line 19, in exists
      os.stat(path)
  TypeError: embedded NUL character

Remainder of file ignored
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    import CHIP_IO.GPIO as GPIO
ImportError: No module named 'CHIP_IO'

I'm wondering why chip_io isn't available by a package manager?

Edge Detection Oddities

Edge Detection on the XIO is goofy as it double hits for both falling and rising regardless of user setting.

Add ability to clean up a single pin

In the underlying C code, there is the ability to cleanup a single pin. Expose this feature to the Python layer. This way, you don't need to blast all the pins away if you just want to clean up a single pin.

Minor issue building dtc

Perhaps it's a missing dependency upstream, but the make of dtc fails as version_gen.h is not provided in the dtc repo. A 'make version_gen.h' is required before the make of dtc itself. Thanx!

no luck with HW PWM0

I got SOFTPWM working fine, except it doesn't seem to be very precise if I can trust my oscilloscope: asking for 50hz I get something more like 45Hz, and can never set duty cycle down lower than ~5%

So I wanted to try HW PWM, and hooked things up on PWM0, but no response.
Using only PWM.start("PWM0", 5, 50), no error but no response.

So I thought maybe I need to activate PWM0 overlay.

I ran

>>> import CHIP_IO.OverlayManager as OM
>>> OM.load("PWM0")

and I am getting:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.linux-armv7l/egg/CHIP_IO/OverlayManager.py", line 180, in load
  File "build/bdist.linux-armv7l/egg/CHIP_IO/OverlayManager.py", line 84, in _set_overlay_verify
  File "/usr/lib/python2.7/os.py", line 150, in makedirs
    makedirs(head, mode)
  File "/usr/lib/python2.7/os.py", line 150, in makedirs
    makedirs(head, mode)
  File "/usr/lib/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 1] Operation not permitted: '/sys/kernel/config/device-tree'

I'm running python with sudo so not sure what else I need to do.
I installed the dtb as per the instructions.

Any ideas?
Thanks for help.

Create dumb function setmode() to mimic RPi.GPIO

Tons of people are porting Raspberry Pi code to the CHIP and have gotten hung up on the RPi.GPIO setmode function.

Make this a dummy function for the time being. It might be able to be used for #32 if GPIO is drastically different on the CHIP Pro.

new_xio: Error when Pin-Name not correct

If you set-up a pin via string name (I think there should be constants available as well, but that's just me), one gets the following error:

>>> import CHIP_IO.GPIO as GPIO
>>> GPIO.setup("GPIO0", GPIO.OUT)
ASSRT failed at source/event_gpio.c:248 (s == strlen(direction))

While "XIO-P0" works fine, "GPIO0" throws this c-error.

pwm problem

i use 4.4 or 4.3 firmware:
I follow your sample will found error:

import CHIP_IO.SOFTPWM as SPWM
SPWM.start("XIO-P7", 50)
SPWM.set_duty_cycle("XIO-P7", 25.5)
Traceback (most recent call last):
File "", line 1, in
RuntimeError: You must start() the PWM channel first

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.