1
0
mirror of https://github.com/xtacocorex/CHIP_IO synced 2025-07-20 12:53:22 +00:00

36 Commits

Author SHA1 Message Date
d0ed4665aa Fixing issue with the enable 1.8v pin code. Updating version to 0.2.7 2017-01-03 00:07:40 +00:00
1d641ecdff evidently the README format doesn't like what I did for the earlier change 2017-01-02 21:01:40 +00:00
8241f54891 Update to the README to make the PocketCHIP specific names more easily recognizable. Update to the Makefile clean function to remove the byte compiled python stuff that wasn't cleaned properly 2017-01-02 20:49:09 +00:00
abbc4968c6 version update to 0.2.6 that brings in the fix for #34 2016-12-31 21:52:21 +00:00
9a2e81f093 fix to close #34, gpio value file is now opened as read/write instead of read only 2016-12-31 21:49:52 +00:00
7cce06b472 fixing error in the overlay builder, forgot to remove the i2c-1 stuff in there. added code to common to allow denotation if a pin supports software pwm 2016-12-31 20:25:12 +00:00
7489fd21b4 Removing i2c-1 custom support in OverlayManager as it's back in 4.4.13, this is to close #31. Update to version 0.2.5 2016-12-31 05:10:26 +00:00
87303b6083 fixed the issue with the softpwm pytest code for #22 2016-12-31 04:52:57 +00:00
8aa402fbd5 mods to test_pwm_setup.py to mark some tests that are failing as expected to fail (#22). the issue appears to be the pwm cleanup stuff. added some code to help with future work 2016-12-31 04:37:24 +00:00
e82f62133a Closing out #17 and #18, version upped to 0.2.4 2016-12-31 02:19:57 +00:00
993ea29213 added the unexport_all() function to CHIP_IO.Utilities to address #18 2016-12-31 02:09:30 +00:00
7d91bbc47e fixes for #17, hardware pwm works now, still can't change polarity, but i think that's a kernel driver/dtb thing. 2016-12-31 01:43:42 +00:00
b12c0ad2fe Merge branch 'master' into hotfix/hwpwmfix 2016-12-30 17:16:17 +00:00
5d8c0f6aaa removing time step in the makefile as the CHIP doesn't have ntpdate installed by default (at least in headless OS) 2016-12-30 16:43:14 +00:00
4aaff04375 removing ropeproject file in test directory 2016-12-30 16:42:10 +00:00
eeb98d468f Merge pull request #39 from blueSolder/master
working
2016-12-26 07:43:41 -06:00
51c38843d9 Add files via upload 2016-12-26 16:34:47 +11:00
5aa08351c2 Merge pull request #37 from zerotri/patch-1
Add 'TARGET_DIR' prefix env var to builder.py
2016-12-16 18:24:42 -06:00
9d7fb634a3 Add 'TARGET_DIR' prefix env var to builder.py
This allows CHIP_IO to build and install successfully in buildroot.
2016-12-16 15:32:02 -08:00
3cd69cfe93 Updating the Makefile to aid in the package and publish to pypi 2016-12-02 01:15:01 +00:00
cf9916018b Merge pull request #33 from WereCatf/master
Improve the code for setting the 1V8-pin, including get-function
2016-12-01 10:31:40 -06:00
e0c65de9a8 Update README.rst 2016-12-01 18:18:17 +02:00
794ce98ed2 Improve the code for setting the 1V8-pin, including get-function 2016-12-01 17:47:11 +02:00
617f562b1c Merge pull request #30 from xtacocorex/feature/lradc
LRADC support
2016-12-01 08:25:09 -06:00
89cbea038e LRADC support to close out feature #15. Added Utilities to enable and disable the 1.8v pin on U13. Updated README. Updated version to 2.3 2016-12-01 05:55:26 +00:00
82d6fef173 checking in debug stuff for the hw pwm issue #17 2016-09-24 14:22:05 -05:00
e2173f0235 Merge pull request #23 from streamnsight/streamnsight-patch-1-3
update readme indent / code
2016-09-22 10:49:46 -05:00
30593306a3 update readme indent / code 2016-09-22 08:45:03 -07:00
a9902d1267 Updating the readme to add Python3 installation process 2016-09-18 19:15:44 -05:00
fcd5afcdc8 Merge pull request #20 from fabien-gigante/master
Minor enhancement to C library (only)
2016-08-26 10:58:38 -05:00
d66032ed93 Merge pull request #1 from fabien-gigante/patch-1
Patch 1
2016-08-26 16:29:18 +02:00
9a83cf765a Update py_gpio.c
allow passing data arguments to c callbacks
2016-08-26 16:06:43 +02:00
d95847eb5a Update event_gpio.c
allow passing data arguments to c callbacks
2016-08-26 16:05:25 +02:00
12bc9dec67 Update event_gpio.h
allow passing data arguments to c callbacks
2016-08-26 16:03:09 +02:00
29eb018e78 Update common.c
unnecessary include of Python
2016-08-26 15:57:07 +02:00
ee0e27cb22 Update c_softpwm.c
unnecessary include of Python
2016-08-26 15:55:43 +02:00
31 changed files with 1139 additions and 546 deletions

View File

@ -1,3 +1,32 @@
0.2.7
----
* Fix to the Enable 1.8V Pin code as it wasn't working due to bit shifting isn't allowed on a float.
* Updated README to denote the PocketCHIP Pin names better
0.2.6
----
* Fix to keep the GPIO value file open until the pin is unexported (issue #34)
0.2.5
----
* Updates to the pytest code for HWPWM and SoftPWM
* Removed the i2c-1 load/unload support in OverlayManager as CHIP Kernel 4.4.13 has that bus brought back by default
0.2.4
----
* HW PWM Fixed
- Start/Stop/Duty Cycle/Frequency settings work
- Polarity cannot be changed, so don't bother setting it to 1 in start()
* Added the unexport_all() function to Utilites
0.2.3
----
* LRADC Support
* Added Utilities
- Enable/Disable the 1.8V Pin
- Change 1.8V Pin to output either 2.0V, 2.6V, or 3.3V
(Current limited to 50mA)
0.2.2
----
* Fixes for Issue #16

228
CHIP_IO/LRADC.py Normal file
View File

@ -0,0 +1,228 @@
# Copyright (c) 2016 Robert Wolterman
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import time
# Global Variables
DEBUG = False
DEVICE_EXIST = True
# Default Sample Rate Variables
SAMPLE_RATE_32P25 = 32.25
SAMPLE_RATE_62O5 = 62.5
SAMPLE_RATE_125 = 125
SAMPLE_RATE_250 = 250
SAMPLE_RATES = []
# Scale Factor
SCALE_FACTOR = 31.25
# File Locations
LRADC_BASE_DEVICE_FILE = "/sys/bus/iio/devices/iio:device0"
AVAILABLE_SAMPLE_RATE_FILE = "/sampling_frequency_available"
SCALE_FACTOR_FILE = "/in_voltage_scale"
CURRENT_SAMPLE_RATE_FILE = "/in_voltage_sampling_frequency"
RAW_VOLTAGE_CHAN0_FILE = "/in_voltage0_raw"
RAW_VOLTAGE_CHAN1_FILE = "/in_voltage1_raw"
def enable_debug():
global DEBUG
DEBUG = True
def setup(rate=250):
# First we determine if the device exists
if not os.path.exists(LRADC_BASE_DEVICE_FILE):
global DEVICE_EXIST
DEVICE_EXIST = False
raise Exception("LRADC Device does not exist")
else:
# Set the Sample Rate
set_sample_rate(rate)
def get_device_exist():
return DEVICE_EXIST
def get_scale_factor():
# If we do not have a device, lets throw an exception
if not DEVICE_EXIST:
raise Exception("LRADC Device does not exist")
# Get the data from the file
f = open(LRADC_BASE_DEVICE_FILE+SCALE_FACTOR_FILE,"r")
dat = f.readline()
f.close()
# Set the Scale Factor
global SCALE_FACTOR
SCALE_FACTOR = float(dat.strip())
# Debug
if DEBUG:
print("Current LRADC Scaling Factor: {0}").format(SCALE_FACTOR)
return SCALE_FACTOR
def get_allowable_sample_rates():
# If we do not have a device, lets throw an exception
if not DEVICE_EXIST:
raise Exception("LRADC Device does not exist")
# Get the data from the file
f = open(LRADC_BASE_DEVICE_FILE+AVAILABLE_SAMPLE_RATE_FILE,"r")
dat = f.readline()
f.close()
global SAMPLE_RATES
tmp = dat.strip().split(" ")
for i in xrange(len(tmp)):
if "." in tmp[i]:
tmp[i] = float(tmp[i])
else:
tmp[i] = int(tmp[i])
SAMPLE_RATES = tmp
# Debug
if DEBUG:
print("Allowable Sampling Rates:")
for rate in SAMPLE_RATES:
print("{0}").format(rate)
return tuple(SAMPLE_RATES)
def set_sample_rate(rate):
# If we do not have a device, lets throw an exception
if not DEVICE_EXIST:
raise Exception("LRADC Device does not exist")
# Debug
if DEBUG:
print("Setting Sample Rate to: {0}").format(rate)
# Check to see if the rates were gathered already
global SAMPLE_RATES
if SAMPLE_RATES == []:
tmp = get_allowable_sample_rates()
# Range check the input rate
if rate not in SAMPLE_RATES:
raise ValueError("Input Rate an Acceptable Value")
# Write the rate
f = open(LRADC_BASE_DEVICE_FILE+CURRENT_SAMPLE_RATE_FILE,"w")
mystr = "%.2f" % rate
f.write(mystr)
f.close()
# Verify write went well
crate = get_sample_rate()
if crate != rate:
raise Exception("Unable to write new Sampling Rate")
def get_sample_rate():
# If we do not have a device, lets throw an exception
if not DEVICE_EXIST:
raise Exception("LRADC Device does not exist")
# Get the data from the file
f = open(LRADC_BASE_DEVICE_FILE+CURRENT_SAMPLE_RATE_FILE,"r")
dat = f.read()
f.close()
dat = dat.strip()
if "." in dat:
dat = float(dat)
else:
dat = int(dat)
# Debug
if DEBUG:
print("Current Sampling Rate: {0}").format(dat)
return dat
def get_chan0_raw():
# If we do not have a device, lets throw an exception
if not DEVICE_EXIST:
raise Exception("LRADC Device does not exist")
# Get the data from the file
f = open(LRADC_BASE_DEVICE_FILE+RAW_VOLTAGE_CHAN0_FILE,"r")
dat = f.readline()
f.close()
dat = float(dat.strip())
# Debug
if DEBUG:
print("CHAN0 RAW: {0}").format(dat)
return dat
def get_chan1_raw():
# If we do not have a device, lets throw an exception
if not DEVICE_EXIST:
raise Exception("LRADC Device does not exist")
# Get the data from the file
f = open(LRADC_BASE_DEVICE_FILE+RAW_VOLTAGE_CHAN1_FILE,"r")
dat = f.readline()
f.close()
dat = float(dat.strip())
# Debug
if DEBUG:
print("CHAN1 RAW: {0}").format(dat)
return dat
def get_chan0():
# If we do not have a device, lets throw an exception
if not DEVICE_EXIST:
raise Exception("LRADC Device does not exist")
# Get the raw data first
dat = get_chan0_raw()
# Apply scale factor
dat *= SCALE_FACTOR
# Debug
if DEBUG:
print("CHAN0: {0}").format(dat)
return dat
def get_chan1():
# If we do not have a device, lets throw an exception
if not DEVICE_EXIST:
raise Exception("LRADC Device does not exist")
# Get the raw data first
dat = get_chan1_raw()
# Apply scale factor
dat *= SCALE_FACTOR
# Debug
if DEBUG:
print("CHAN1: {0}").format(dat)
return dat

View File

@ -1,3 +1,22 @@
# Copyright (c) 2016 Robert Wolterman
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import shutil
import time
@ -9,7 +28,6 @@ OVERLAYCONFIGPATH = "/sys/kernel/config/device-tree/overlays"
CUSTOMOVERLAYFILEPATH = ""
PWMSYSFSPATH = "/sys/class/pwm/pwmchip0"
I2C1SYSFSPATH = "/sys/class/i2c-dev/i2c-1"
# USING THE BASE DIRECTORY FOR SPI AS THE DEVICE NUMBER CHANGES ON LOAD/UNLOAD
SPI2SYSFSPATH = "/sys/class/spi_master/"
@ -17,21 +35,18 @@ SPI2SYSFSPATH = "/sys/class/spi_master/"
# DO NOT MODIFY BY HAND WHEN USING
# AS IT COULD BREAK FUNCTIONALITY
_LOADED = {
"I2C1" : False,
"SPI2" : False,
"PWM0" : False,
"CUST" : False
}
_OVERLAYS = {
"I2C1" : "chip-i2c1.dtbo",
"SPI2" : "chip-spi2.dtbo",
"PWM0" : "chip-pwm0.dtbo",
"CUST" : ""
}
_FOLDERS = {
"I2C1" : "chip-i2c",
"SPI2" : "chip-spi",
"PWM0" : "chip-pwm",
"CUST" : "chip-cust"
@ -41,13 +56,6 @@ def enable_debug():
global DEBUG
DEBUG = True
def get_i2c_loaded():
"""
get_i2c_loaded - Returns True/False based upon if the i2c-1 Overlay is loaded
"""
global _LOADED
return _LOADED["I2C1"]
def get_spi_loaded():
"""
get_spi_loaded - Returns True/False based upon if the spi2 Overlay is loaded
@ -79,7 +87,7 @@ def _set_overlay_verify(name, overlay_path, config_path):
print("Config path already exists! Not moving forward")
print("config_path: {0}".format(config_path))
return -1
# MAKE THE CONFIGURATION PATH
os.makedirs(config_path)
@ -104,15 +112,6 @@ def _set_overlay_verify(name, overlay_path, config_path):
if DEBUG:
print("ERROR LOAIDNG PWM0")
return 1
elif name == "I2C1":
if os.path.exists(I2C1SYSFSPATH):
if DEBUG:
print("I2C-1 IS LOADED!")
return 0
else:
if DEBUG:
print("ERROR LOADING I2C-1")
return 1
elif name == "SPI2":
if os.listdir(SPI2SYSFSPATH) != "":
if DEBUG:
@ -128,7 +127,7 @@ def load(overlay, path=""):
load - Load a DTB Overlay
Inputs:
overlay - Overlay Key: I2C1, SPI2, PWM0, CUST
overlay - Overlay Key: SPI2, PWM0, CUST
path - Full Path to where the custom overlay is stored
Returns:
@ -168,10 +167,6 @@ def load(overlay, path=""):
print("PWM0 Overlay already loaded")
return 2
if overlay.upper() == "I2C1" and _LOADED[overlay.upper()]:
print("I2C1 Overlay already loaded")
return 2
if overlay.upper() == "SPI2" and _LOADED[overlay.upper()]:
print("SPI2 Overlay already loaded")
return 2
@ -184,7 +179,7 @@ def load(overlay, path=""):
_LOADED[overlay.upper()] = True
else:
raise ValueError("Invalid Overlay name specified! Choose between: I2C1, SPI2, PWM0, CUST")
raise ValueError("Invalid Overlay name specified! Choose between: SPI2, PWM0, CUST")
def unload(overlay):
global DEBUG
@ -197,6 +192,6 @@ def unload(overlay):
os.system('rmdir \"{}\"'.format(OVERLAYCONFIGPATH + "/" + _FOLDERS[overlay.upper()]))
_LOADED[overlay.upper()] = False
else:
raise ValueError("Invalid Overlay name specified! Choose between: I2C1, SPI2, PWM0, CUST")
raise ValueError("Invalid Overlay name specified! Choose between: SPI2, PWM0, CUST")

79
CHIP_IO/Utilities.py Normal file
View File

@ -0,0 +1,79 @@
# Copyright (c) 2016 Robert Wolterman
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# CHIP_IO Utilities
# Random functions to enable fun stuff on the CHIP!
# Credit goes to nonokuono (https://bbs.nextthing.co/users/nonokunono)
# for gathering the i2cset commands from the AXP-209 datasheet for 2.0, 2.6, and 3.3V output
# and for figuring out the ADC setup on the AXP-209
import subprocess
import glob
import re
# Set the 1.8V-pin on the CHIP U13-header to given voltage
# Return False on error
def set_1v8_pin_voltage(voltage):
if not isinstance(voltage, int) and not isinstance(voltage, float):
return False
if voltage < 1.8 or voltage > 3.3:
return False
voltage=int(round((voltage - 1.8) / 0.1)) << 4
if subprocess.call(["/usr/sbin/i2cset", "-f", "-y" ,"0", "0x34", "0x90", "0x03"]):
return False
if subprocess.call(["/usr/sbin/i2cset", "-f", "-y", "0", "0x34", "0x91", str(voltage)]):
return False
return True
# Get the voltage the 1.8V-pin on the CHIP U13-header has been configured as
# Return False on error
def get_1v8_pin_voltage():
p=subprocess.Popen(["/usr/sbin/i2cget", "-f", "-y", "0", "0x34", "0x90"], stdout=subprocess.PIPE)
output=p.communicate()[0].decode("utf-8").strip()
#Not configured as an output
if output != "0x03":
return False
p=subprocess.Popen(["/usr/sbin/i2cget", "-f", "-y", "0", "0x34", "0x91"], stdout=subprocess.PIPE)
output=p.communicate()[0].decode("utf-8").strip()
voltage=round((int(output, 16) >> 4) * 0.1 + 1.8, 1)
return voltage
# Enable 1.8V Pin on CHIP U13 Header
def enable_1v8_pin():
set_1v8_pin_voltage(1.8)
# Disable 1.8V Pin on CHIP U13 Header
def disable_1v8_pin():
# CANNOT USE I2C LIB AS WE NEED TO FORCE THE COMMAND DUE TO THE KERNEL OWNING THE DEVICE
# First we have to write 0x05 to AXP-209 Register 0x91
subprocess.call('/usr/sbin/i2cset -f -y 0 0x34 0x91 0x05', shell=True)
# Then we have to write 0x07 to AXP-209 Register 0x90
subprocess.call('/usr/sbin/i2cset -f -y 0 0x34 0x90 0x07', shell=True)
# Unexport All
def unexport_all():
gpios = glob.glob("/sys/class/gpio/gpio[0-9]*")
for g in gpios:
tmp = g.split("/")
gpio = tmp[4]
num = re.sub("[a-z]","",gpio)
cmd = "echo " + num + " > /sys/class/gpio/unexport"
subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE)

View File

@ -1,12 +1,12 @@
time:
/usr/bin/ntpdate -b -s -u pool.ntp.org
package: clean
python setup.py sdist
publish: clean
python setup.py sdist upload
publish: package
twine upload dist/*
clean:
rm -rf CHIP_IO.* build dist
rm -f *.pyo
rm -f *.pyo *.pyc
rm -f *.egg
rm -f overlays/*.pyo overlays/*.pyc
tests:

View File

@ -6,6 +6,8 @@ NOTE: Now requires the custom DTC to install the library
Manual::
For Python2.7::
sudo apt-get update
sudo apt-get install git build-essential python-dev python-pip flex bison -y
git clone https://github.com/atenart/dtc
@ -19,6 +21,21 @@ Manual::
cd ..
sudo rm -rf CHIP_IO
For Python3::
sudo apt-get update
sudo apt-get install git build-essential python3-dev python3-pip flex bison -y
git clone https://github.com/atenart/dtc
cd dtc
make
sudo make install PREFIX=/usr
cd ..
git clone git://github.com/xtacocorex/CHIP_IO.git
cd CHIP_IO
sudo python3 setup.py install
cd ..
sudo rm -rf CHIP_IO
**Usage**
Using the library is very similar to the excellent RPi.GPIO library used on the Raspberry Pi. Below are some examples.
@ -27,115 +44,115 @@ All scripts that require GPIO, PWM (HW and/or SW), and Overlay Manager need to b
**Allowable Pin Names for the Library**
The following "table" is the allowable pin names that are able to be used by 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.
+-----------+-------------+--------+
| Name | Alt Name | Key |
+-----------+-------------+--------+
| TWI1-SDA | KPD-I2C-SDA | U13_9 |
+-----------+-------------+--------+
| TWI1-SCK | KPD-I2C-SCL | U13_11 |
+-----------+-------------+--------+
| LCD-D2 | LCD-D2 | U13_17 |
+-----------+-------------+--------+
| PWM0 | PWM0 | U13_18 |
+-----------+-------------+--------+
| LCD-D4 | LCD-D4 | U13_19 |
+-----------+-------------+--------+
| LCD-D3 | LCD-D3 | U13_20 |
+-----------+-------------+--------+
| LCD-D6 | LCD-D6 | U13_21 |
+-----------+-------------+--------+
| LCD-D5 | LCD-D5 | U13_22 |
+-----------+-------------+--------+
| LCD-D10 | LCD-D10 | U13_23 |
+-----------+-------------+--------+
| LCD-D7 | LCD-D7 | U13_24 |
+-----------+-------------+--------+
| LCD-D12 | LCD-D12 | U13_25 |
+-----------+-------------+--------+
| LCD-D11 | LCD-D11 | U13_26 |
+-----------+-------------+--------+
| LCD-D14 | LCD-D14 | U13_27 |
+-----------+-------------+--------+
| LCD-D13 | LCD-D13 | U13_28 |
+-----------+-------------+--------+
| LCD-D18 | LCD-D18 | U13_29 |
+-----------+-------------+--------+
| LCD-D15 | LCD-D15 | U13_30 |
+-----------+-------------+--------+
| LCD-D20 | LCD-D20 | U13_31 |
+-----------+-------------+--------+
| LCD-D19 | LCD-D19 | U13_32 |
+-----------+-------------+--------+
| LCD-D22 | LCD-D22 | U13_33 |
+-----------+-------------+--------+
| LCD-D21 | LCD-D21 | U13_34 |
+-----------+-------------+--------+
| LCD-CLK | LCD-CLK | U13_35 |
+-----------+-------------+--------+
| LCD-D23 | LCD-D23 | U13_36 |
+-----------+-------------+--------+
| LCD-VSYNC | LCD-VSYNC | U13_37 |
+-----------+-------------+--------+
| LCD-HSYNC | LCD-HSYNC | U13_38 |
+-----------+-------------+--------+
| LCD-DE | LCD-DE | U13_40 |
+-----------+-------------+--------+
| UART1-TX | UART-TX | U14_3 |
+-----------+-------------+--------+
| UART1-RX | UART-RX | U14_5 |
+-----------+-------------+--------+
| LRADC | ADC | U14_11 |
+-----------+-------------+--------+
| XIO-P0 | XIO-P0 | U14_13 |
+-----------+-------------+--------+
| XIO-P1 | XIO-P1 | U14_14 |
+-----------+-------------+--------+
| XIO-P2 | GPIO1 | U14_15 |
+-----------+-------------+--------+
| XIO-P3 | GPIO2 | U14_16 |
+-----------+-------------+--------+
| XIO-P4 | GPIO3 | U14_17 |
+-----------+-------------+--------+
| XIO-P5 | GPIO4 | U14_18 |
+-----------+-------------+--------+
| XIO-P6 | GPIO5 | U14_19 |
+-----------+-------------+--------+
| XIO-P7 | GPIO6 | U14_20 |
+-----------+-------------+--------+
| AP-EINT1 | KPD-INT | U14_23 |
+-----------+-------------+--------+
| AP-EINT3 | AP-INT3 | U14_24 |
+-----------+-------------+--------+
| TWI2-SDA | I2C-SDA | U14_25 |
+-----------+-------------+--------+
| TWI2-SCK | I2C-SCL | U14_26 |
+-----------+-------------+--------+
| CSIPCK | SPI-SEL | U14_27 |
+-----------+-------------+--------+
| CSICK | SPI-CLK | U14_28 |
+-----------+-------------+--------+
| CSIHSYNC | SPI-MOSI | U14_29 |
+-----------+-------------+--------+
| CSIVSYNC | SPI-MISO | U14_30 |
+-----------+-------------+--------+
| CSID0 | CSID0 | U14_31 |
+-----------+-------------+--------+
| CSID1 | CSID1 | U14_32 |
+-----------+-------------+--------+
| CSID2 | CSID2 | U14_33 |
+-----------+-------------+--------+
| CSID3 | CSID3 | U14_34 |
+-----------+-------------+--------+
| CSID4 | CSID4 | U14_35 |
+-----------+-------------+--------+
| CSID5 | CSID5 | U14_36 |
+-----------+-------------+--------+
| CSID6 | CSID6 | U14_37 |
+-----------+-------------+--------+
| CSID7 | CSID7 | U14_38 |
+-----------+-------------+--------+
+------------------+--------------------------+--------+
| CHIP (Name) | PocketCHIP (Alt Name) | Key |
+------------------+--------------------------+--------+
| TWI1-SDA | KPD-I2C-SDA | U13_9 |
+------------------+--------------------------+--------+
| TWI1-SCK | KPD-I2C-SCL | U13_11 |
+------------------+--------------------------+--------+
| LCD-D2 | LCD-D2 | U13_17 |
+------------------+--------------------------+--------+
| PWM0 | PWM0 | U13_18 |
+------------------+--------------------------+--------+
| LCD-D4 | LCD-D4 | U13_19 |
+------------------+--------------------------+--------+
| LCD-D3 | LCD-D3 | U13_20 |
+------------------+--------------------------+--------+
| LCD-D6 | LCD-D6 | U13_21 |
+------------------+--------------------------+--------+
| LCD-D5 | LCD-D5 | U13_22 |
+------------------+--------------------------+--------+
| LCD-D10 | LCD-D10 | U13_23 |
+------------------+--------------------------+--------+
| LCD-D7 | LCD-D7 | U13_24 |
+------------------+--------------------------+--------+
| LCD-D12 | LCD-D12 | U13_25 |
+------------------+--------------------------+--------+
| LCD-D11 | LCD-D11 | U13_26 |
+------------------+--------------------------+--------+
| LCD-D14 | LCD-D14 | U13_27 |
+------------------+--------------------------+--------+
| LCD-D13 | LCD-D13 | U13_28 |
+------------------+--------------------------+--------+
| LCD-D18 | LCD-D18 | U13_29 |
+------------------+--------------------------+--------+
| LCD-D15 | LCD-D15 | U13_30 |
+------------------+--------------------------+--------+
| LCD-D20 | LCD-D20 | U13_31 |
+------------------+--------------------------+--------+
| LCD-D19 | LCD-D19 | U13_32 |
+------------------+--------------------------+--------+
| LCD-D22 | LCD-D22 | U13_33 |
+------------------+--------------------------+--------+
| LCD-D21 | LCD-D21 | U13_34 |
+------------------+--------------------------+--------+
| LCD-CLK | LCD-CLK | U13_35 |
+------------------+--------------------------+--------+
| LCD-D23 | LCD-D23 | U13_36 |
+------------------+--------------------------+--------+
| LCD-VSYNC | LCD-VSYNC | U13_37 |
+------------------+--------------------------+--------+
| LCD-HSYNC | LCD-HSYNC | U13_38 |
+------------------+--------------------------+--------+
| LCD-DE | LCD-DE | U13_40 |
+------------------+--------------------------+--------+
| UART1-TX | UART-TX | U14_3 |
+------------------+--------------------------+--------+
| UART1-RX | UART-RX | U14_5 |
+------------------+--------------------------+--------+
| LRADC | ADC | U14_11 |
+------------------+--------------------------+--------+
| XIO-P0 | XIO-P0 | U14_13 |
+------------------+--------------------------+--------+
| XIO-P1 | XIO-P1 | U14_14 |
+------------------+--------------------------+--------+
| XIO-P2 | GPIO1 | U14_15 |
+------------------+--------------------------+--------+
| XIO-P3 | GPIO2 | U14_16 |
+------------------+--------------------------+--------+
| XIO-P4 | GPIO3 | U14_17 |
+------------------+--------------------------+--------+
| XIO-P5 | GPIO4 | U14_18 |
+------------------+--------------------------+--------+
| XIO-P6 | GPIO5 | U14_19 |
+------------------+--------------------------+--------+
| XIO-P7 | GPIO6 | U14_20 |
+------------------+--------------------------+--------+
| AP-EINT1 | KPD-INT | U14_23 |
+------------------+--------------------------+--------+
| AP-EINT3 | AP-INT3 | U14_24 |
+------------------+--------------------------+--------+
| TWI2-SDA | I2C-SDA | U14_25 |
+------------------+--------------------------+--------+
| TWI2-SCK | I2C-SCL | U14_26 |
+------------------+--------------------------+--------+
| CSIPCK | SPI-SEL | U14_27 |
+------------------+--------------------------+--------+
| CSICK | SPI-CLK | U14_28 |
+------------------+--------------------------+--------+
| CSIHSYNC | SPI-MOSI | U14_29 |
+------------------+--------------------------+--------+
| CSIVSYNC | SPI-MISO | U14_30 |
+------------------+--------------------------+--------+
| CSID0 | CSID0 | U14_31 |
+------------------+--------------------------+--------+
| CSID1 | CSID1 | U14_32 |
+------------------+--------------------------+--------+
| CSID2 | CSID2 | U14_33 |
+------------------+--------------------------+--------+
| CSID3 | CSID3 | U14_34 |
+------------------+--------------------------+--------+
| CSID4 | CSID4 | U14_35 |
+------------------+--------------------------+--------+
| CSID5 | CSID5 | U14_36 |
+------------------+--------------------------+--------+
| CSID6 | CSID6 | U14_37 |
+------------------+--------------------------+--------+
| CSID7 | CSID7 | U14_38 |
+------------------+--------------------------+--------+
**GPIO Setup**
@ -170,16 +187,16 @@ Inputs work similarly to outputs.::
Polling inputs::
if GPIO.input("CSID0"):
print("HIGH")
print("HIGH")
else:
print("LOW")
print("LOW")
Waiting for an edge (GPIO.RISING, GPIO.FALLING, or GPIO.BOTH::
This only works for the AP-EINT1, AP-EINT3, and XPO Pins on the CHIP
GPIO.wait_for_edge(channel, GPIO.RISING)
This only works for the AP-EINT1, AP-EINT3, and XPO Pins on the CHIP
Detecting events::
GPIO.setup("XIO-P0", GPIO.IN)
@ -187,7 +204,7 @@ Detecting events::
#your amazing code here
#detect wherever:
if GPIO.event_detected("XIO-P0"):
print "event detected!"
print "event detected!"
**GPIO Cleanup**
@ -198,18 +215,17 @@ To clean up the GPIO when done, do the following::
**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
#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()
#set polarity to 1 on start:
#For specific polarity: this example sets polarity to 1 on start:
PWM.start("PWM0", 50, 2000, 1)
**SOFTPWM**::
@ -221,20 +237,44 @@ Hardware PWM requires a DTB Overlay loaded on the CHIP to allow the kernel to kn
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")
SPWM.cleanup()
#set polarity to 1 on start:
#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.
**ADC**::
**LRADC**::
Not Implemented yet
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 Debug
ADC.enable_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**::
@ -246,7 +286,7 @@ The Overlay Manager enables you to quickly load simple Device Tree Overlays. Th
PWM0, SPI2, I2C1, CUST
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 and I2C1 without PWM0, but you cannot have SPI2 loaded twice.
::
import CHIP_IO.OverlayManager as OM
# The enable_debug() function turns on debug printing
#OM.enable_debug()
@ -254,14 +294,13 @@ Only one of each type of overlay can be loaded at a time, but all three options
OM.load("PWM0")
# To verify the overlay was properly loaded, the get_ functions return booleans
OM.get_pwm_loaded()
OM.get_i2c_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")
@ -270,7 +309,29 @@ There is no verification that the Custom Overlay is setup properly, it's fire an
# To unload, just call unload()
OM.unload("CUST")
Note that this requires the 4.4 kernel with the CONFIG_OF_CONFIGFS option enabled in the kernel config.
**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 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()
**Running tests**

View File

@ -2,23 +2,23 @@ from subprocess import call
import os
import shutil
import glob
import sys
def compile():
print("Compiling DTS Files")
call(["dtc", "-O", "dtb", "-o", "overlays/chip-spi2.dtbo", "-b", "o", "-@", "overlays/chip-spi2.dts"])
call(["dtc", "-O", "dtb", "-o", "overlays/chip-i2c1.dtbo", "-b", "o", "-@", "overlays/chip-i2c1.dts"])
call(["dtc", "-O", "dtb", "-o", "overlays/chip-pwm0.dtbo", "-b", "o", "-@", "overlays/chip-pwm0.dts"])
def copy():
target_dir = os.environ.get('TARGET_DIR', '')
overlay_path = target_dir + "/lib/firmware/chip_io"
print("Checking for DTBO Install Path")
if not os.path.exists("/lib/firmware/chip_io/"):
print("Path not found, creating /lib/firmware/chip_io/")
os.makedirs("/lib/firmware/chip_io/")
if not os.path.exists(overlay_path):
print("Path not found, creating "+overlay_path)
os.makedirs(overlay_path)
print("Removing old DTBO files (if applicable)")
for fl in glob.glob("/lib/firmware/chip_io/chip-*-.dtbo"):
for fl in glob.glob(overlay_path+"/chip-*-.dtbo"):
os.remove(fl)
print("Moving DTBO files to /lib/firmware/chip_io/")
shutil.move("overlays/chip-spi2.dtbo", "/lib/firmware/chip_io/chip-spi2.dtbo")
shutil.move("overlays/chip-i2c1.dtbo", "/lib/firmware/chip_io/chip-i2c1.dtbo")
shutil.move("overlays/chip-pwm0.dtbo", "/lib/firmware/chip_io/chip-pwm0.dtbo")
print("Moving DTBO files to "+overlay_path)
shutil.move("overlays/chip-spi2.dtbo", overlay_path+"/chip-spi2.dtbo")
shutil.move("overlays/chip-pwm0.dtbo", overlay_path+"/chip-pwm0.dtbo")

View File

@ -1,72 +0,0 @@
/*
* Copyright 2016 Free Electrons
* Copyright 2016 NextThing Co
*
* Maxime Ripard <maxime.ripard@free-electrons.com>
*
* This file is dual-licensed: you can use it either under the terms
* of the GPL or the X11 license, at your option. Note that this dual
* licensing only applies to this file, and not this project as a
* whole.
*
* a) This file is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Or, alternatively,
*
* b) Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/dts-v1/;
/plugin/;
/ {
compatible = "nextthing,chip", "allwinner,sun5i-r8";
fragment@0 {
target-path = "/aliases";
__overlay__ {
i2c1 = "/soc@01c00000/i2c@01c2b000";
};
};
/* Enable the I2C1 bus and the keyboard */
fragment@1 {
target = <&i2c1>;
__overlay__ {
#address-cells = <1>;
#size-cells = <0>;
pinctrl-names = "default";
pinctrl-0 = <&i2c1_pins_a>;
status = "okay";
};
};
};

View File

@ -20,7 +20,7 @@ classifiers = ['Development Status :: 3 - Alpha',
'Topic :: System :: Hardware']
setup(name = 'CHIP_IO',
version = '0.2.2',
version = '0.2.7',
author = 'Robert Wolterman',
author_email = 'robert.wolterman@gmail.com',
description = 'A module to control CHIP IO channels',

View File

@ -42,8 +42,37 @@ SOFTWARE.
#define PERIOD 0
#define DUTY 1
#define ENABLE 1
#define DISABLE 0
// Global variables
int pwm_initialized = 0;
int DEBUG = 0;
//int ENABLE = 1;
//int DISABLE = 0;
// pwm devices (future chip pro use)
struct pwm_dev
{
char key[KEYLEN+1]; /* leave room for terminating NUL byte */
int gpio;
int initialized;
struct pwm_dev *next;
};
struct pwm_dev *initialized_pwms = NULL;
struct pwm_dev *lookup_initialized_pwm(const char *key)
{
struct pwm_dev *dev = initialized_pwms;
while (dev != NULL)
{
if (strcmp(dev->key, key) == 0) {
return dev;
}
dev = dev->next;
}
return NULL;
}
// pwm exports
struct pwm_exp
@ -71,25 +100,28 @@ struct pwm_exp *lookup_exported_pwm(const char *key)
}
pwm = pwm->next;
}
return NULL; /* standard for pointers */
}
int initialize_pwm(void)
{
if (!pwm_initialized) {
if (!pwm_initialized) {
int fd, len;
char str_gpio[80];
// Per https://github.com/NextThingCo/CHIP-linux/pull/4
// we need to export 0 here to enable pwm0
int gpio = 0;
if (DEBUG)
printf(" ** EXPORTING PWM0 **\n");
if ((fd = open("/sys/class/pwm/pwmchip0/export", O_WRONLY)) < 0)
{
return -1;
}
len = snprintf(str_gpio, sizeof(str_gpio), "%d", gpio); BUF2SMALL(str_gpio);
ssize_t s = write(fd, str_gpio, len); ASSRT(s == len);
if (DEBUG)
printf(" ** IN initialize_pwm: s = %d, len = %d\n", s, len);
close(fd);
pwm_initialized = 1;
@ -101,101 +133,171 @@ int initialize_pwm(void)
int pwm_set_frequency(const char *key, float freq) {
int len;
int rtnval = -1;
char buffer[80];
unsigned long period_ns;
struct pwm_exp *pwm;
if (freq <= 0.0)
return -1;
return rtnval;
pwm = lookup_exported_pwm(key);
if (pwm == NULL) {
return -1;
return rtnval;
}
period_ns = (unsigned long)(1e9 / freq);
if (period_ns != pwm->period_ns) {
pwm->period_ns = period_ns;
if (pwm->enable) {
if (DEBUG)
printf(" ** IN pwm_set_frequency: pwm_initialized = %d\n", pwm_initialized);
if (period_ns != pwm->period_ns) {
pwm->period_ns = period_ns;
len = snprintf(buffer, sizeof(buffer), "%lu", period_ns); BUF2SMALL(buffer);
ssize_t s = write(pwm->period_fd, buffer, len); ASSRT(s == len);
len = snprintf(buffer, sizeof(buffer), "%lu", period_ns); BUF2SMALL(buffer);
if (DEBUG)
printf(" ** pwm_set_frequency: buffer: %s\n", buffer);
ssize_t s = write(pwm->period_fd, buffer, len); //ASSRT(s == len);
if (DEBUG)
printf(" ** IN pwm_set_frequency: s = %d, len = %d\n", s, len);
if (s != len) {
rtnval = -1;
} else {
rtnval = 1;
}
} else {
rtnval = 0;
}
} else {
rtnval = 0;
}
return 1;
return rtnval;
}
int pwm_set_polarity(const char *key, int polarity) {
int len;
int rtnval = -1;
char buffer[80];
struct pwm_exp *pwm;
pwm = lookup_exported_pwm(key);
if (pwm == NULL) {
return -1;
return rtnval;
}
if (polarity < 0 || polarity > 1) {
return -1;
if (polarity != 0 && polarity != 1) {
return rtnval;
}
if (polarity == 0) {
len = snprintf(buffer, sizeof(buffer), "%s", "normal"); BUF2SMALL(buffer);
if (pwm->enable) {
if (DEBUG)
printf(" ** IN pwm_set_polarity: pwm_initialized = %d\n", pwm_initialized);
if (polarity == 0) {
len = snprintf(buffer, sizeof(buffer), "%s", "normal"); BUF2SMALL(buffer);
}
else
{
len = snprintf(buffer, sizeof(buffer), "%s", "inverted"); BUF2SMALL(buffer);
}
if (DEBUG)
printf(" ** pwm_set_poliarity: buffer: %s\n", buffer);
ssize_t s = write(pwm->polarity_fd, buffer, len); //ASSRT(s == len);
if (DEBUG)
printf(" ** IN pwm_set_polarity: s = %d, len = %d\n", s, len);
if (s != len) {
rtnval = -1;
} else {
rtnval = 1;
}
} else {
rtnval = 0;
}
else
{
len = snprintf(buffer, sizeof(buffer), "%s", "inverted"); BUF2SMALL(buffer);
}
ssize_t s = write(pwm->polarity_fd, buffer, len); ASSRT(s == len);
return 0;
return rtnval;
}
int pwm_set_duty_cycle(const char *key, float duty) {
int len;
int rtnval = -1;
char buffer[80];
struct pwm_exp *pwm;
if (duty < 0.0 || duty > 100.0)
return -1;
if (duty < 0.0 || duty > 100.0) {
return rtnval;
}
pwm = lookup_exported_pwm(key);
if (pwm == NULL) {
return -1;
return rtnval;
}
pwm->duty = (unsigned long)(pwm->period_ns * (duty / 100.0));
len = snprintf(buffer, sizeof(buffer), "%lu", pwm->duty); BUF2SMALL(buffer);
ssize_t s = write(pwm->duty_fd, buffer, len); ASSRT(s == len);
if (pwm->enable) {
if (DEBUG)
printf(" ** IN pwm_set_duty_cycle: pwm_initialized = %d\n", pwm_initialized);
len = snprintf(buffer, sizeof(buffer), "%lu", pwm->duty); BUF2SMALL(buffer);
if (DEBUG)
printf(" ** pwm_set_duty_cycle: buffer: %s\n", buffer);
ssize_t s = write(pwm->duty_fd, buffer, len); //ASSRT(s == len);
if (DEBUG)
printf(" ** IN pwm_set_duty_cycle: s = %d, len = %d\n", s, len);
if (s != len) {
rtnval = -1;
} else {
rtnval = 1;
}
} else {
rtnval = 0;
}
return 0;
return rtnval;
}
int pwm_set_enable(const char *key, int enable)
{
int len;
int rtnval = -1;
char buffer[80];
struct pwm_exp *pwm;
if (enable != 0 || enable != 1)
return -1;
if (enable != 0 && enable != 1) {
if (DEBUG)
printf(" ** INVALID ENABLE OPTION, NEEDS TO BE EITHER 0 OR 1! **\n");
return rtnval;
}
pwm = lookup_exported_pwm(key);
if (pwm == NULL) {
return -1;
if (DEBUG)
printf(" ** SOMETHING BAD HAPPEND IN pwm_set_enable, WE'RE EXITING WITH -1 NOW! **\n");
return rtnval;
}
pwm->enable = enable;
len = snprintf(buffer, sizeof(buffer), "%d", pwm->enable); BUF2SMALL(buffer);
ssize_t s = write(pwm->enable_fd, buffer, len); ASSRT(s == len);
len = snprintf(buffer, sizeof(buffer), "%d", enable); BUF2SMALL(buffer);
ssize_t s = write(pwm->enable_fd, buffer, len); //ASSRT(s == len);
if (DEBUG) {
printf(" ** IN pwm_set_enable: pwm_initialized = %d\n", pwm_initialized);
printf(" ** pwm_set_enable: buffer: %s\n", buffer);
printf(" ** IN pwm_set_enable: s = %d, len = %d\n", s, len);
}
return 0;
if (s == len)
{
if (DEBUG)
printf(" ** SETTING pwm->enable to %d\n", enable);
pwm->enable = enable;
rtnval = 0;
} else {
rtnval = -1;
}
return rtnval;
}
int pwm_start(const char *key, float duty, float freq, int polarity)
@ -208,9 +310,17 @@ int pwm_start(const char *key, float duty, float freq, int polarity)
int period_fd, duty_fd, polarity_fd, enable_fd;
struct pwm_exp *new_pwm, *pwm;
if (DEBUG)
printf(" ** IN pwm_start: pwm_initialized = %d\n", pwm_initialized);
if(!pwm_initialized) {
initialize_pwm();
} else {
if (DEBUG)
printf(" ** ALREADY INITIALIZED, CALLING CLEANUP TO START FRESH **");
pwm_cleanup();
}
if (DEBUG)
printf(" ** IN pwm_start: pwm_initialized = %d\n", pwm_initialized);
//setup the pwm base path, the chip only has one pwm
snprintf(pwm_base_path, sizeof(pwm_base_path), "/sys/class/pwm/pwmchip0/pwm%d", 0); BUF2SMALL(pwm_base_path);
@ -221,24 +331,31 @@ int pwm_start(const char *key, float duty, float freq, int polarity)
snprintf(duty_path, sizeof(duty_path), "%s/duty_cycle", pwm_base_path); BUF2SMALL(duty_path);
snprintf(polarity_path, sizeof(polarity_path), "%s/polarity", pwm_base_path); BUF2SMALL(polarity_path);
if (DEBUG) {
printf(" ** IN pwm_start: pwm_base_path: %s\n", pwm_base_path);
printf(" ** IN pwm_start: enable_path: %s\n", enable_path);
printf(" ** IN pwm_start: period_path: %s\n", period_path);
printf(" ** IN pwm_start: duty_path: %s\n", duty_path);
printf(" **IN pwm_start: polarity_path: %s\n", polarity_path);
}
//add period and duty fd to pwm list
if ((enable_fd = open(enable_path, O_RDWR)) < 0)
if ((enable_fd = open(enable_path, O_WRONLY)) < 0)
return -1;
if ((period_fd = open(period_path, O_RDWR)) < 0) {
if ((period_fd = open(period_path, O_WRONLY)) < 0) {
close(enable_fd);
return -1;
}
if ((duty_fd = open(duty_path, O_RDWR)) < 0) {
if ((duty_fd = open(duty_path, O_WRONLY)) < 0) {
//error, close already opened period_fd.
close(enable_fd);
close(period_fd);
return -1;
}
if ((polarity_fd = open(polarity_path, O_RDWR)) < 0) {
if ((polarity_fd = open(polarity_path, O_WRONLY)) < 0) {
//error, close already opened period_fd and duty_fd.
close(enable_fd);
close(period_fd);
@ -252,6 +369,8 @@ int pwm_start(const char *key, float duty, float freq, int polarity)
return -1; // out of memory
}
if (DEBUG)
printf(" ** IN pwm_start: IF WE MAKE IT HERE, THE FILES WERE SUCCESSFULLY OPEN **\n");
strncpy(new_pwm->key, key, KEYLEN); /* can leave string unterminated */
new_pwm->key[KEYLEN] = '\0'; /* terminate string */
new_pwm->period_fd = period_fd;
@ -272,12 +391,16 @@ int pwm_start(const char *key, float duty, float freq, int polarity)
pwm->next = new_pwm;
}
pwm_set_frequency(key, freq);
pwm_set_polarity(key, polarity);
pwm_set_enable(key, 1);
pwm_set_duty_cycle(key, duty);
int rtnval = 0;
rtnval = pwm_set_enable(key, ENABLE);
rtnval = 0;
rtnval = pwm_set_frequency(key, freq);
rtnval = 0;
//rtnval = pwm_set_polarity(key, polarity);
//rtnval = 0;
rtnval = pwm_set_duty_cycle(key, duty);
return 1;
return rtnval;
}
int pwm_disable(const char *key)
@ -292,9 +415,9 @@ int pwm_disable(const char *key)
// Disable the PWM
pwm_set_frequency(key, 0);
pwm_set_polarity(key, 0);
pwm_set_enable(key, 0);
//pwm_set_polarity(key, 0);
pwm_set_duty_cycle(key, 0);
pwm_set_enable(key, DISABLE);
if ((fd = open("/sys/class/pwm/pwmchip0/unexport", O_WRONLY)) < 0)
{
@ -315,7 +438,7 @@ int pwm_disable(const char *key)
close(pwm->period_fd);
close(pwm->duty_fd);
close(pwm->polarity_fd);
if (prev_pwm == NULL)
{
exported_pwms = pwm->next;
@ -323,7 +446,6 @@ int pwm_disable(const char *key)
} else {
prev_pwm->next = pwm->next;
}
temp = pwm;
pwm = pwm->next;
free(temp);
@ -341,3 +463,12 @@ void pwm_cleanup(void)
pwm_disable(exported_pwms->key);
}
}
void pwm_toggle_debug(void)
{
if (DEBUG) {
DEBUG = 0;
} else {
DEBUG = 1;
}
}

View File

@ -35,3 +35,4 @@ int pwm_set_frequency(const char *key, float freq);
int pwm_set_duty_cycle(const char *key, float duty);
int pwm_set_enable(const char *key, int enable);
void pwm_cleanup(void);
void pwm_toggle_debug(void);

View File

@ -41,7 +41,6 @@ SOFTWARE.
#include "c_pwm.h"
#include "common.h"
#include "event_gpio.h"
#include "Python.h"
#define KEYLEN 7

View File

@ -36,7 +36,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "Python.h"
#include <dirent.h>
#include <time.h>
#include "common.h"
@ -51,87 +50,87 @@ int setup_error = 0;
int module_setup = 0;
pins_t pins_info[] = {
{ "GND", "GND", "U13_1", -1, BASE_METHOD_AS_IS, -1, -1},
{ "CHG-IN", "CHG-IN", "U13_2", -1, BASE_METHOD_AS_IS, -1, -1},
{ "VCC-5V", "VCC-5V", "U13_3", -1, BASE_METHOD_AS_IS, -1, -1},
{ "GND", "GND", "U13_4", -1, BASE_METHOD_AS_IS, -1, -1},
{ "VCC-3V3", "VCC-3V3", "U13_5", -1, BASE_METHOD_AS_IS, -1, -1},
{ "TS", "TS", "U13_6", -1, BASE_METHOD_AS_IS, -1, -1},
{ "VCC-1V8", "VCC-1V8", "U13_7", -1, BASE_METHOD_AS_IS, -1, -1},
{ "BAT", "BAT", "U13_8", -1, BASE_METHOD_AS_IS, -1, -1},
{ "TWI1-SDA", "KPD-I2C-SDA", "U13_9", 48, BASE_METHOD_AS_IS, -1, -1},
{ "PWRON", "PWRON", "U13_10", -1, BASE_METHOD_AS_IS, -1, -1},
{ "TWI1-SCK", "KPD-I2C-SCL", "U13_11", 47, BASE_METHOD_AS_IS, -1, -1},
{ "GND", "GND", "U13_12", -1, BASE_METHOD_AS_IS, -1, -1},
{ "X1", "X1", "U13_13", -1, BASE_METHOD_AS_IS, -1, -1},
{ "X2", "X2", "U13_14", -1, BASE_METHOD_AS_IS, -1, -1},
{ "Y1", "Y1", "U13_15", -1, BASE_METHOD_AS_IS, -1, -1},
{ "Y2", "Y2", "U13_16", -1, BASE_METHOD_AS_IS, -1, -1},
{ "LCD-D2", "LCD-D2", "U13_17", 98, BASE_METHOD_AS_IS, -1, -1},
{ "PWM0", "PWM0", "U13_18", 34, BASE_METHOD_AS_IS, 0, -1},
{ "LCD-D4", "LCD-D4", "U13_19", 100, BASE_METHOD_AS_IS, -1, -1},
{ "LCD-D3", "LCD-D3", "U13_20", 99, BASE_METHOD_AS_IS, -1, -1},
{ "LCD-D6", "LCD-D6", "U13_21", 102, BASE_METHOD_AS_IS, -1, -1},
{ "LCD-D5", "LCD-D5", "U13_22", 101, BASE_METHOD_AS_IS, -1, -1},
{ "LCD-D10", "LCD-D10", "U13_23", 106, BASE_METHOD_AS_IS, -1, -1},
{ "LCD-D7", "LCD-D7", "U13_24", 103, BASE_METHOD_AS_IS, -1, -1},
{ "LCD-D12", "LCD-D12", "U13_25", 108, BASE_METHOD_AS_IS, -1, -1},
{ "LCD-D11", "LCD-D11", "U13_26", 107, BASE_METHOD_AS_IS, -1, -1},
{ "LCD-D14", "LCD-D14", "U13_27", 110, BASE_METHOD_AS_IS, -1, -1},
{ "LCD-D13", "LCD-D13", "U13_28", 109, BASE_METHOD_AS_IS, -1, -1},
{ "LCD-D18", "LCD-D18", "U13_29", 114, BASE_METHOD_AS_IS, -1, -1},
{ "LCD-D15", "LCD-D15", "U13_30", 111, BASE_METHOD_AS_IS, -1, -1},
{ "LCD-D20", "LCD-D20", "U13_31", 116, BASE_METHOD_AS_IS, -1, -1},
{ "LCD-D19", "LCD-D19", "U13_32", 115, BASE_METHOD_AS_IS, -1, -1},
{ "LCD-D22", "LCD-D22", "U13_33", 118, BASE_METHOD_AS_IS, -1, -1},
{ "LCD-D21", "LCD-D21", "U13_34", 117, BASE_METHOD_AS_IS, -1, -1},
{ "LCD-CLK", "LCD-CLK", "U13_35", 120, BASE_METHOD_AS_IS, -1, -1},
{ "LCD-D23", "LCD-D23", "U13_36", 119, BASE_METHOD_AS_IS, -1, -1},
{ "LCD-VSYNC", "LCD-VSYNC", "U13_37", 123, BASE_METHOD_AS_IS, -1, -1},
{ "LCD-HSYNC", "LCD-HSYNC", "U13_38", 122, BASE_METHOD_AS_IS, -1, -1},
{ "GND", "GND", "U13_39", -1, BASE_METHOD_AS_IS, -1, -1},
{ "LCD-DE", "LCD-DE", "U13_40", 121, BASE_METHOD_AS_IS, -1, -1},
{ "GND", "GND", "U14_1", -1, BASE_METHOD_AS_IS, -1, -1},
{ "VCC-5V", "VCC-5V", "U14_2", -1, BASE_METHOD_AS_IS, -1, -1},
{ "UART1-TX", "UART-TX", "U14_3", 195, BASE_METHOD_AS_IS, -1, -1},
{ "HPL", "HPL", "U14_4", -1, BASE_METHOD_AS_IS, -1, -1},
{ "UART1-RX", "UART-RX", "U14_5", 196, BASE_METHOD_AS_IS, -1, -1},
{ "HPCOM", "HPCOM", "U14_6", -1, BASE_METHOD_AS_IS, -1, -1},
{ "FEL", "FEL", "U14_7", -1, BASE_METHOD_AS_IS, -1, -1},
{ "HPR", "HPR", "U14_8", -1, BASE_METHOD_AS_IS, -1, -1},
{ "VCC-3V3", "VCC-3V3", "U14_9", -1, BASE_METHOD_AS_IS, -1, -1},
{ "MICM", "MICM", "U14_10", -1, BASE_METHOD_AS_IS, -1, -1},
{ "LRADC", "ADC", "U14_11", -1, BASE_METHOD_AS_IS, -1, 0},
{ "MICIN1", "MICIN1", "U14_12", -1, BASE_METHOD_AS_IS, -1, -1},
{ "XIO-P0", "XIO-P0", "U14_13", 0, BASE_METHOD_XIO, -1, -1},
{ "XIO-P1", "XIO-P1", "U14_14", 1, BASE_METHOD_XIO, -1, -1},
{ "XIO-P2", "GPIO1", "U14_15", 2, BASE_METHOD_XIO, -1, -1},
{ "XIO-P3", "GPIO2", "U14_16", 3, BASE_METHOD_XIO, -1, -1},
{ "XIO-P4", "GPIO3", "U14_17", 4, BASE_METHOD_XIO, -1, -1},
{ "XIO-P5", "GPIO4", "U14_18", 5, BASE_METHOD_XIO, -1, -1},
{ "XIO-P6", "GPIO5", "U14_19", 6, BASE_METHOD_XIO, -1, -1},
{ "XIO-P7", "GPIO6", "U14_20", 7, BASE_METHOD_XIO, -1, -1},
{ "GND", "GND", "U14_21", -1, BASE_METHOD_AS_IS, -1, -1},
{ "GND", "GND", "U14_22", -1, BASE_METHOD_AS_IS, -1, -1},
{ "AP-EINT1", "KPD-INT", "U14_23", 193, BASE_METHOD_AS_IS, -1, -1},
{ "AP-EINT3", "AP-INT3", "U14_24", 35, BASE_METHOD_AS_IS, -1, -1},
{ "TWI2-SDA", "I2C-SDA", "U14_25", 50, BASE_METHOD_AS_IS, -1, -1},
{ "TWI2-SCK", "I2C-SCL", "U14_26", 49, BASE_METHOD_AS_IS, -1, -1},
{ "CSIPCK", "SPI-SEL", "U14_27", 128, BASE_METHOD_AS_IS, -1, -1},
{ "CSICK", "SPI-CLK", "U14_28", 129, BASE_METHOD_AS_IS, -1, -1},
{ "CSIHSYNC", "SPI-MOSI", "U14_29", 130, BASE_METHOD_AS_IS, 1, -1},
{ "CSIVSYNC", "SPI-MISO", "U14_30", 131, BASE_METHOD_AS_IS, -1, -1},
{ "CSID0", "CSID0", "U14_31", 132, BASE_METHOD_AS_IS, 1, -1},
{ "CSID1", "CSID1", "U14_32", 133, BASE_METHOD_AS_IS, -1, -1},
{ "CSID2", "CSID2", "U14_33", 134, BASE_METHOD_AS_IS, -1, -1},
{ "CSID3", "CSID3", "U14_34", 135, BASE_METHOD_AS_IS, -1, -1},
{ "CSID4", "CSID4", "U14_35", 136, BASE_METHOD_AS_IS, -1, -1},
{ "CSID5", "CSID5", "U14_36", 137, BASE_METHOD_AS_IS, -1, -1},
{ "CSID6", "CSID6", "U14_37", 138, BASE_METHOD_AS_IS, -1, -1},
{ "CSID7", "CSID7", "U14_38", 139, BASE_METHOD_AS_IS, -1, -1},
{ "GND", "GND", "U14_39", -1, BASE_METHOD_AS_IS, -1, -1},
{ "GND", "GND", "U14_40", -1, BASE_METHOD_AS_IS, -1, -1},
{ NULL, NULL, NULL, -1, 0, -1, -1}
{ "GND", "GND", "U13_1", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "CHG-IN", "CHG-IN", "U13_2", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "VCC-5V", "VCC-5V", "U13_3", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "GND", "GND", "U13_4", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "VCC-3V3", "VCC-3V3", "U13_5", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "TS", "TS", "U13_6", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "VCC-1V8", "VCC-1V8", "U13_7", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "BAT", "BAT", "U13_8", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "TWI1-SDA", "KPD-I2C-SDA", "U13_9", 48, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "PWRON", "PWRON", "U13_10", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "TWI1-SCK", "KPD-I2C-SCL", "U13_11", 47, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "GND", "GND", "U13_12", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "X1", "X1", "U13_13", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "X2", "X2", "U13_14", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "Y1", "Y1", "U13_15", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "Y2", "Y2", "U13_16", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "LCD-D2", "LCD-D2", "U13_17", 98, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "PWM0", "PWM0", "U13_18", 34, BASE_METHOD_AS_IS, 0, -1, SPWM_DISABLED},
{ "LCD-D4", "LCD-D4", "U13_19", 100, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "LCD-D3", "LCD-D3", "U13_20", 99, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "LCD-D6", "LCD-D6", "U13_21", 102, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "LCD-D5", "LCD-D5", "U13_22", 101, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "LCD-D10", "LCD-D10", "U13_23", 106, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "LCD-D7", "LCD-D7", "U13_24", 103, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "LCD-D12", "LCD-D12", "U13_25", 108, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "LCD-D11", "LCD-D11", "U13_26", 107, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "LCD-D14", "LCD-D14", "U13_27", 110, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "LCD-D13", "LCD-D13", "U13_28", 109, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "LCD-D18", "LCD-D18", "U13_29", 114, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "LCD-D15", "LCD-D15", "U13_30", 111, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "LCD-D20", "LCD-D20", "U13_31", 116, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "LCD-D19", "LCD-D19", "U13_32", 115, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "LCD-D22", "LCD-D22", "U13_33", 118, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "LCD-D21", "LCD-D21", "U13_34", 117, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "LCD-CLK", "LCD-CLK", "U13_35", 120, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "LCD-D23", "LCD-D23", "U13_36", 119, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "LCD-VSYNC", "LCD-VSYNC", "U13_37", 123, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "LCD-HSYNC", "LCD-HSYNC", "U13_38", 122, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "GND", "GND", "U13_39", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "LCD-DE", "LCD-DE", "U13_40", 121, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "GND", "GND", "U14_1", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "VCC-5V", "VCC-5V", "U14_2", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "UART1-TX", "UART-TX", "U14_3", 195, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "HPL", "HPL", "U14_4", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "UART1-RX", "UART-RX", "U14_5", 196, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "HPCOM", "HPCOM", "U14_6", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "FEL", "FEL", "U14_7", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "HPR", "HPR", "U14_8", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "VCC-3V3", "VCC-3V3", "U14_9", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "MICM", "MICM", "U14_10", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "LRADC", "ADC", "U14_11", -1, BASE_METHOD_AS_IS, -1, 0, SPWM_DISABLED},
{ "MICIN1", "MICIN1", "U14_12", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "XIO-P0", "XIO-P0", "U14_13", 0, BASE_METHOD_XIO, -1, -1, SPWM_ENABLED},
{ "XIO-P1", "XIO-P1", "U14_14", 1, BASE_METHOD_XIO, -1, -1, SPWM_ENABLED},
{ "XIO-P2", "GPIO1", "U14_15", 2, BASE_METHOD_XIO, -1, -1, SPWM_ENABLED},
{ "XIO-P3", "GPIO2", "U14_16", 3, BASE_METHOD_XIO, -1, -1, SPWM_ENABLED},
{ "XIO-P4", "GPIO3", "U14_17", 4, BASE_METHOD_XIO, -1, -1, SPWM_ENABLED},
{ "XIO-P5", "GPIO4", "U14_18", 5, BASE_METHOD_XIO, -1, -1, SPWM_ENABLED},
{ "XIO-P6", "GPIO5", "U14_19", 6, BASE_METHOD_XIO, -1, -1, SPWM_ENABLED},
{ "XIO-P7", "GPIO6", "U14_20", 7, BASE_METHOD_XIO, -1, -1, SPWM_ENABLED},
{ "GND", "GND", "U14_21", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "GND", "GND", "U14_22", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "AP-EINT1", "KPD-INT", "U14_23", 193, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "AP-EINT3", "AP-INT3", "U14_24", 35, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "TWI2-SDA", "I2C-SDA", "U14_25", 50, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "TWI2-SCK", "I2C-SCL", "U14_26", 49, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "CSIPCK", "SPI-SEL", "U14_27", 128, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "CSICK", "SPI-CLK", "U14_28", 129, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "CSIHSYNC", "SPI-MOSI", "U14_29", 130, BASE_METHOD_AS_IS, 1, -1, SPWM_DISABLED},
{ "CSIVSYNC", "SPI-MISO", "U14_30", 131, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "CSID0", "CSID0", "U14_31", 132, BASE_METHOD_AS_IS, 1, -1, SPWM_DISABLED},
{ "CSID1", "CSID1", "U14_32", 133, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "CSID2", "CSID2", "U14_33", 134, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "CSID3", "CSID3", "U14_34", 135, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "CSID4", "CSID4", "U14_35", 136, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "CSID5", "CSID5", "U14_36", 137, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "CSID6", "CSID6", "U14_37", 138, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "CSID7", "CSID7", "U14_38", 139, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "GND", "GND", "U14_39", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ "GND", "GND", "U14_40", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
{ NULL, NULL, NULL, -1, 0, -1, -1, -1}
};
@ -352,10 +351,12 @@ int get_pwm_key_by_name(const char *name, char *key)
pins_t *p;
for (p = pins_info; p->name != NULL; ++p) {
if (strcmp(p->name, name) == 0) {
printf("## FOUND PWM KEY, VALIDATING MUX MODE ##\n");
//validate it's a valid pwm pin
if (p->pwm_mux_mode == -1)
return 0;
printf("## PWM KEY IS VALID ##\n");
strncpy(key, p->key, 7);
key[7] = '\0';
return 1;

View File

@ -52,13 +52,15 @@ SOFTWARE.
} while (0)
#define MODE_UNKNOWN -1
#define BOARD 10
#define BCM 11
#define CHIP 0
#define CHIPPRO 1
// In the pins_t structure, the "base_method" field tells how
// the "gpio" field should be interpreted.
#define BASE_METHOD_AS_IS 1 /* use the gpio value directly */
#define BASE_METHOD_XIO 2 /* add the gpio value to the XIO base */
#define SPWM_ENABLED 1 /* pin able to be used by software pwm */
#define SPWM_DISABLED 0 /* pin unable to be used by software pwm */
typedef struct pins_t {
const char *name;
@ -66,8 +68,9 @@ typedef struct pins_t {
const char *key;
int gpio; /* port number to use under /sys/class/gpio */
int base_method; /* modifier for port number; see BASE_METHOD_... */
int pwm_mux_mode;
int ain;
int pwm_mux_mode; /* pwm pin */
int ain; /* analog pin */
int spwm_allow; /* pin allowed for software pwm */
} pins_t;
@ -77,7 +80,6 @@ struct dyn_int_array_s {
};
typedef struct dyn_int_array_s dyn_int_array_t;
#define FILENAME_BUFFER_SIZE 128
int setup_error;

View File

@ -76,6 +76,6 @@ void define_constants(PyObject *module)
both_edge = Py_BuildValue("i", BOTH_EDGE);
PyModule_AddObject(module, "BOTH", both_edge);
version = Py_BuildValue("s", "0.2.2");
version = Py_BuildValue("s", "0.2.7");
PyModule_AddObject(module, "VERSION", version);
}

View File

@ -66,7 +66,8 @@ struct callback
int fde;
int gpio;
int edge;
void (*func)(int gpio);
void* data;
void (*func)(int gpio, void* data);
struct callback *next;
};
struct callback *callbacks = NULL;
@ -214,7 +215,9 @@ int open_value_file(int gpio)
// create file descriptor of value file
snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/value", gpio); BUF2SMALL(filename);
if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) < 0) {
// Changed this to open Read/Write to prevent a ton of file open/closes from happening when using
// the GPIO for SOFTPWM
if ((fd = open(filename, O_RDWR | O_NONBLOCK)) < 0) {
char err[256];
snprintf(err, sizeof(err), "open_value_file: could not open '%s' (%s)", filename, strerror(errno));
add_error_msg(err);
@ -375,17 +378,23 @@ int gpio_get_direction(int gpio, unsigned int *value)
int gpio_set_value(int gpio, unsigned int value)
{
int fd, e_no;
// This now uses the value file descriptor that is set in the other struct
// in an effort to minimize opening/closing this
int fd = fd_lookup(gpio);
int e_no;
char filename[MAX_FILENAME];
char vstr[16];
snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/value", gpio); BUF2SMALL(filename);
if ((fd = open(filename, O_WRONLY)) < 0) {
char err[256];
snprintf(err, sizeof(err), "gpio_set_value: could not open '%s' (%s)", filename, strerror(errno));
add_error_msg(err);
return -1;
if (!fd)
{
if ((fd = open_value_file(gpio)) == -1) {
char err[256];
snprintf(err, sizeof(err), "gpio_get_value: could not open GPIO %d value file", gpio);
add_error_msg(err);
return -1;
}
}
if (value) {
@ -395,7 +404,6 @@ int gpio_set_value(int gpio, unsigned int value)
}
ssize_t s = write(fd, vstr, strlen(vstr)); e_no = errno;
close(fd);
if (s != strlen(vstr)) {
char err[256];
@ -549,7 +557,7 @@ void exports_cleanup(void)
gpio_unexport(exported_gpios->gpio);
}
int add_edge_callback(int gpio, int edge, void (*func)(int gpio))
int add_edge_callback(int gpio, int edge, void (*func)(int gpio, void* data), void* data)
{
struct callback *cb = callbacks;
struct callback *new_cb;
@ -559,6 +567,7 @@ int add_edge_callback(int gpio, int edge, void (*func)(int gpio))
new_cb->fde = open_edge_file(gpio);
new_cb->gpio = gpio;
new_cb->edge = edge;
new_cb->data = data;
new_cb->func = func;
new_cb->next = NULL;
@ -602,7 +611,7 @@ void run_callbacks(int gpio)
// Only run if we are allowed
if (canrun)
{
cb->func(cb->gpio);
cb->func(cb->gpio, cb->data);
}
}

View File

@ -70,7 +70,7 @@ int gpio_set_edge(int gpio, unsigned int edge);
int gpio_get_edge(int gpio);
int add_edge_detect(int gpio, unsigned int edge);
void remove_edge_detect(int gpio);
int add_edge_callback(int gpio, int edge, void (*func)(int gpio));
int add_edge_callback(int gpio, int edge, void (*func)(int gpio, void* data), void* data);
int event_detected(int gpio);
int gpio_event_add(int gpio);
int gpio_event_remove(int gpio);

View File

@ -225,7 +225,7 @@ static PyObject *py_input_gpio(PyObject *self, PyObject *args)
return py_value;
}
static void run_py_callbacks(int gpio)
static void run_py_callbacks(int gpio, void* data)
{
PyObject *result;
PyGILState_STATE gstate;
@ -294,7 +294,7 @@ static int add_py_callback(char *channel, int gpio, int edge, unsigned int bounc
cb = cb->next;
cb->next = new_py_cb;
}
add_edge_callback(gpio, edge, run_py_callbacks);
add_edge_callback(gpio, edge, run_py_callbacks, NULL);
return 0;
}
@ -749,6 +749,54 @@ static PyObject *py_selftest(PyObject *self, PyObject *args)
static const char moduledocstring[] = "GPIO functionality of a CHIP using Python";
/*
mine for changing pin directipn
*/
static PyObject *py_set_direction(PyObject *self, PyObject *args, PyObject *kwargs)
{
int gpio;
char *channel;
int direction;
static char *kwlist[] = { "channel", "direction", NULL };
clear_error_msg();
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "si|ii", kwlist, &channel, &direction))
return NULL;
if (!module_setup) {
init_module();
}
if (direction != INPUT && direction != OUTPUT)
{
PyErr_SetString(PyExc_ValueError, "An invalid direction was passed to setup()");
return NULL;
}
if (get_gpio_number(channel, &gpio) < 0) {
char err[2000];
snprintf(err, sizeof(err), "Invalid channel %s. (%s)", channel, get_error_msg());
PyErr_SetString(PyExc_ValueError, err);
return NULL;
}
if (gpio_set_direction(gpio, direction) < 0) {
char err[2000];
snprintf(err, sizeof(err), "Error setting direction %d on channel %s. (%s)", direction, channel, get_error_msg());
PyErr_SetString(PyExc_RuntimeError, err);
return NULL;
}
remember_gpio_direction(gpio, direction);
Py_RETURN_NONE;
}
PyMethodDef gpio_methods[] = {
{"setup", (PyCFunction)py_setup_channel, METH_VARARGS | METH_KEYWORDS, "Set up the GPIO channel, direction and (optional) pull/up down control\nchannel - Either: CHIP board pin number (not R8 GPIO 00..nn number). Pins start from 1\n or : CHIP GPIO name\ndirection - INPUT or OUTPUT\n[pull_up_down] - PUD_OFF (default), PUD_UP or PUD_DOWN\n[initial] - Initial value for an output channel"},
{"cleanup", py_cleanup, METH_VARARGS, "Clean up by resetting all GPIO channels that have been used by this program to INPUT with no pullup/pulldown and no event detection"},
@ -763,9 +811,11 @@ PyMethodDef gpio_methods[] = {
{"setwarnings", py_setwarnings, METH_VARARGS, "Enable or disable warning messages"},
{"get_gpio_base", py_gpio_base, METH_VARARGS, "Get the XIO base number for sysfs"},
{"selftest", py_selftest, METH_VARARGS, "Internal unit tests"},
{ "direction", (PyCFunction)py_set_direction, METH_VARARGS, "Change direction of gpio channel. Either INPUT or OUTPUT\n" },
{NULL, NULL, 0, NULL}
};
#if PY_MAJOR_VERSION > 2
static struct PyModuleDef rpigpiomodule = {
PyModuleDef_HEAD_INIT,
@ -815,4 +865,4 @@ PyMODINIT_FUNC initGPIO(void)
#else
return;
#endif
}
}

View File

@ -43,6 +43,15 @@ static PyObject *py_cleanup(PyObject *self, PyObject *args)
Py_RETURN_NONE;
}
// python function toggle_debug()
static PyObject *py_toggle_debug(PyObject *self, PyObject *args)
{
// toggle debug printing
pwm_toggle_debug();
Py_RETURN_NONE;
}
// python function start(channel, duty_cycle, freq)
static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwargs)
{
@ -170,9 +179,10 @@ static const char moduledocstring[] = "Hardware PWM functionality of a CHIP usin
PyMethodDef pwm_methods[] = {
{"start", (PyCFunction)py_start_channel, METH_VARARGS | METH_KEYWORDS, "Set up and start the PWM channel. channel can be in the form of 'PWM0', or 'U13_18'"},
{"stop", (PyCFunction)py_stop_channel, METH_VARARGS | METH_KEYWORDS, "Stop the PWM channel. channel can be in the form of 'PWM0', or 'U13_18'"},
{ "set_duty_cycle", (PyCFunction)py_set_duty_cycle, METH_VARARGS, "Change the duty cycle\ndutycycle - between 0.0 and 100.0" },
{ "set_frequency", (PyCFunction)py_set_frequency, METH_VARARGS, "Change the frequency\nfrequency - frequency in Hz (freq > 0.0)" },
{"set_duty_cycle", (PyCFunction)py_set_duty_cycle, METH_VARARGS, "Change the duty cycle\ndutycycle - between 0.0 and 100.0" },
{"set_frequency", (PyCFunction)py_set_frequency, METH_VARARGS, "Change the frequency\nfrequency - frequency in Hz (freq > 0.0)" },
{"cleanup", py_cleanup, METH_VARARGS, "Clean up by resetting all GPIO channels that have been used by this program to INPUT with no pullup/pulldown and no event detection"},
{"toggle_debug", py_toggle_debug, METH_VARARGS, "Toggles the enabling/disabling of Debug print output"},
//{"setwarnings", py_setwarnings, METH_VARARGS, "Enable or disable warning messages"},
{NULL, NULL, 0, NULL}
};

View File

@ -1,96 +0,0 @@
# The default ``config.py``
def set_prefs(prefs):
"""This function is called before opening the project"""
# Specify which files and folders to ignore in the project.
# Changes to ignored resources are not added to the history and
# VCSs. Also they are not returned in `Project.get_files()`.
# Note that ``?`` and ``*`` match all characters but slashes.
# '*.pyc': matches 'test.pyc' and 'pkg/test.pyc'
# 'mod*.pyc': matches 'test/mod1.pyc' but not 'mod/1.pyc'
# '.svn': matches 'pkg/.svn' and all of its children
# 'build/*.o': matches 'build/lib.o' but not 'build/sub/lib.o'
# 'build//*.o': matches 'build/lib.o' and 'build/sub/lib.o'
prefs['ignored_resources'] = [
'*.pyc', '*~', '.ropeproject', '.hg', '.svn', '_svn', '.git',
'.tox', '.env', 'node_modules', 'bower_components']
# Specifies which files should be considered python files. It is
# useful when you have scripts inside your project. Only files
# ending with ``.py`` are considered to be python files by
# default.
#prefs['python_files'] = ['*.py']
# Custom source folders: By default rope searches the project
# for finding source folders (folders that should be searched
# for finding modules). You can add paths to that list. Note
# that rope guesses project source folders correctly most of the
# time; use this if you have any problems.
# The folders should be relative to project root and use '/' for
# separating folders regardless of the platform rope is running on.
# 'src/my_source_folder' for instance.
#prefs.add('source_folders', 'src')
# You can extend python path for looking up modules
#prefs.add('python_path', '~/python/')
# Should rope save object information or not.
prefs['save_objectdb'] = True
prefs['compress_objectdb'] = False
# If `True`, rope analyzes each module when it is being saved.
prefs['automatic_soa'] = True
# The depth of calls to follow in static object analysis
prefs['soa_followed_calls'] = 0
# If `False` when running modules or unit tests "dynamic object
# analysis" is turned off. This makes them much faster.
prefs['perform_doa'] = True
# Rope can check the validity of its object DB when running.
prefs['validate_objectdb'] = True
# How many undos to hold?
prefs['max_history_items'] = 32
# Shows whether to save history across sessions.
prefs['save_history'] = True
prefs['compress_history'] = False
# Set the number spaces used for indenting. According to
# :PEP:`8`, it is best to use 4 spaces. Since most of rope's
# unit-tests use 4 spaces it is more reliable, too.
prefs['indent_size'] = 4
# Builtin and c-extension modules that are allowed to be imported
# and inspected by rope.
prefs['extension_modules'] = []
# Add all standard c-extensions to extension_modules list.
prefs['import_dynload_stdmods'] = True
# If `True` modules with syntax errors are considered to be empty.
# The default value is `False`; When `False` syntax errors raise
# `rope.base.exceptions.ModuleSyntaxError` exception.
prefs['ignore_syntax_errors'] = False
# If `True`, rope ignores unresolvable imports. Otherwise, they
# appear in the importing namespace.
prefs['ignore_bad_imports'] = False
# If `True`, rope will transform a comma list of imports into
# multiple separate import statements when organizing
# imports.
prefs['split_imports'] = False
# If `True`, rope will sort imports alphabetically by module name
# instead of alphabetically by import statement, with from imports
# after normal imports.
prefs['sort_imports_alphabetically'] = False
def project_opened(project):
"""This function is called after opening the project"""
# Do whatever you like here!

View File

@ -1,2 +0,0 @@
<EFBFBD>}q(Utest_softpwm_setup]q(Uteardown_moduleqUTestSoftpwmSetupqeUtest_gpio_output]q(hUTestGPIOOutputqeUtest_pwm_setup]q(hU TestPwmSetupqeUtest_gpio_setup]q (hU TestSetupq
eUtest_gpio_input]q (hU

View File

@ -1 +0,0 @@
<EFBFBD>]q(]q]qe.

View File

@ -1 +0,0 @@
<EFBFBD>}q.

0
test/gptest.py Executable file → Normal file
View File

67
test/lradc_test.py Normal file
View File

@ -0,0 +1,67 @@
#!/usr/bin/python
import CHIP_IO.LRADC as ADC
# == ENABLE DEBUG ==
print("ENABLING LRADC DEBUG OUTPUT")
ADC.enable_debug()
# == SETUP ==
print("LRADC SETUP WITH SAMPLE RATE OF 125")
ADC.setup(125)
# == SCALE FACTOR ==
print("GETTING SCALE FACTOR")
scalefactor = ADC.get_scale_factor()
print(scalefactor)
print("")
# == ALLOWABLE SAMPLING RATES ==
print("GETTING ALLOWABLE SAMPLE RATES")
rates = ADC.get_allowable_sample_rates()
print(rates)
print("IS 32.25 IN RATE TUPLE")
print(ADC.SAMPLE_RATE_32P25 in rates)
print("")
# == CURRENT SAMPLE RATE ==
print("CURRENT SAMPLING RATE")
crate = ADC.get_sample_rate()
print(crate)
print("")
# == SET SAMPLE RATE ==
print("SETTING SAMPLE RATE TO 62.5")
ADC.set_sample_rate(62.5)
crate = ADC.get_sample_rate()
print(crate)
print("")
# == CHAN 0 RAW ==
print("READING LRADC CHAN0 RAW")
raw0 = ADC.get_chan0_raw()
print(raw0)
print("")
# == CHAN 1 RAW ==
print("READING LRADC CHAN1 RAW")
raw1 = ADC.get_chan1_raw()
print(raw1)
print("")
# == CHAN 0 ==
print("READING LRADC CHAN0 WITH SCALE APPLIED")
full0 = ADC.get_chan0()
print(full0)
print("")
# == CHAN 1 ==
print("READING LRADC CHAN1 WITH SCALE APPLIED")
full1 = ADC.get_chan1()
print(full1)
print("")
# == RESET ==
print("RESETING SAMPLE RATE TO 250")
ADC.set_sample_rate(250)

0
test/omtest.py Executable file → Normal file
View File

99
test/pwmtest.py Normal file
View File

@ -0,0 +1,99 @@
#!/usr/bin/python
import CHIP_IO.PWM as PWM
import CHIP_IO.GPIO as GPIO
import CHIP_IO.OverlayManager as OM
import time
import datetime
import threading
class PWMReceiver(threading.Thread):
def __init__(self,gpio,key,maxcount=20,sleeptime=0.5):
self.gpio = gpio
self.key = key
self.counter = 0
self.maxcount = maxcount
self.sleeptime = sleeptime
threading.Thread.__init__(self)
def run(self):
print("SETTING UP RECEIVER GPIO")
self.gpio.cleanup()
self.gpio.setup(self.key, self.gpio.IN)
print("STARTING RECEIVE LOOP")
try:
while self.counter < self.maxcount:
pwmval = self.gpio.input(self.key)
print("PWM VALUE: {0} @ {1}".format(pwmval, datetime.datetime.now()))
time.sleep(self.sleeptime)
self.counter += 1
except KeyboardInterrupt:
self.gpio.cleanup(self.key)
def PrintPwmData():
print("PRINTING PWM SYSFS DATA")
f = open("/sys/class/pwm/pwmchip0/pwm0/enable")
print("PWM0 ENABLE:\t{}".format(f.readline()))
f.close()
f = open("/sys/class/pwm/pwmchip0/pwm0/period")
print("PWM0 PERIOD:\t{}".format(f.readline()))
f.close()
f = open("/sys/class/pwm/pwmchip0/pwm0/duty_cycle")
print("PWM0 DUTY CYCLE:\t{}".format(f.readline()))
f.close()
f = open("/sys/class/pwm/pwmchip0/pwm0/polarity")
print("PWM0 POLARITY:\t{}".format(f.readline()))
f.close()
if __name__ == "__main__":
# SETUP VARIABLES
PWMGPIO = "PWM0"
RECEIVERGPIO = "CSID0"
COUNT = 150
SLEEPTIME = 0.01
# LOAD THE PWM OVERLAY
print("LOADING PWM OVERLAY")
OM.load("PWM0")
time.sleep(1)
# CLEANUP THE GPIO
#GPIO.cleanup()
#PWM.cleanup()
# SETUP PWM
try:
print("PWM START")
PWM.toggle_debug()
PWM.start(PWMGPIO, 15, 50, 1)
PrintPwmData()
# UNCOMMENT FOR CRASH
#print("PWM SET FREQUENCY")
#PWM.set_frequency(PWMGPIO, 200)
#PrintPwmData()
# UNCOMMENT FOR CRASH
#print("PWM SET DUTY CYCLE")
#PWM.set_duty_cycle(PWMGPIO, 25)
#PrintPwmData()
# SETUP PWM RECEIVER
#rcvr = PWMReceiver(GPIO, RECEIVERGPIO, COUNT, SLEEPTIME)
#rcvr.start()
#time.sleep(COUNT*SLEEPTIME + 1)
raw_input("PRESS ENTER WHEN DONE")
except:
raise
finally:
# CLEANUP
print("CLEANUP")
PWM.stop(PWMGPIO)
PWM.cleanup()
#OM.unload("PWM0")
#GPIO.cleanup()

0
test/spwmtest.py Executable file → Normal file
View File

0
test/test_gpio_input.py Executable file → Normal file
View File

View File

@ -1,65 +1,84 @@
import pytest
import os
import time
import CHIP_IO.PWM as PWM
import CHIP_IO.OverlayManager as OM
def setup_module(module):
OM.load("PWM0")
def teardown_module(module):
PWM.cleanup()
OM.unload("PWM0")
class TestPwmSetup:
def setup_method(self, test_method):
time.sleep(0.5)
#def teardown_method(self, test_method):
# PWM.cleanup()
#OM.unload("PWM0")
#def setup_module(self, module):
# OM.load("PWM0")
#def teardown_module(self, module):
# OM.unload("PWM0")
def test_start_pwm(self):
PWM.start("PWM0", 0)
pwm_test = '/sys/class/pwm/pwmchip0/pwm0'
pwm_test = '/sys/class/pwm/pwmchip0/pwm0/'
assert os.path.exists(pwm_test)
duty = open(pwm_test + '/duty_cycle').readline().strip()
period = open(pwm_test + '/period').readline().strip()
assert os.path.exists(pwm_test) == True
duty = open(pwm_test + 'duty_cycle').readline().strip()
period = open(pwm_test + 'period').readline().strip()
assert int(duty) == 0
assert int(period) == 500000
PWM.cleanup()
@pytest.mark.xfail(reason="pwm cleanup is doing weirdness for this test")
def test_start_pwm_with_polarity_one(self):
PWM.start("PWM0", 0, 2000, 1)
pwm_test = '/sys/class/pwm/pwmchip0/pwm0'
pwm_test = '/sys/class/pwm/pwmchip0/pwm0/'
assert os.path.exists(pwm_test)
duty = open(pwm_test + '/duty_cycle').readline().strip()
period = open(pwm_test + '/period').readline().strip()
polarity = open(pwm_test + '/polarity').readline().strip()
#assert os.path.exists(pwm_test) == True
duty = open(pwm_test + 'duty_cycle').readline().strip()
period = open(pwm_test + 'period').readline().strip()
polarity = open(pwm_test + 'polarity').readline().strip()
assert int(duty) == 0
assert int(period) == 500000
assert str(polarity) == "inverted"
PWM.cleanup()
@pytest.mark.xfail(reason="pwm cleanup is doing weirdness for this test")
def test_start_pwm_with_polarity_default(self):
PWM.start("PWM0", 0, 2000, 0)
pwm_test = '/sys/class/pwm/pwmchip0/pwm0'
pwm_test = '/sys/class/pwm/pwmchip0/pwm0/'
assert os.path.exists(pwm_test)
duty = open(pwm_test + '/duty_cycle').readline().strip()
period = open(pwm_test + '/period').readline().strip()
polarity = open(pwm_test + '/polarity').readline().strip()
#assert os.path.exists(pwm_test) == True
duty = open(pwm_test + 'duty_cycle').readline().strip()
period = open(pwm_test + 'period').readline().strip()
polarity = open(pwm_test + 'polarity').readline().strip()
assert int(duty) == 0
assert int(period) == 500000
assert str(polarity) == "normal"
PWM.cleanup()
@pytest.mark.xfail(reason="pwm cleanup is doing weirdness for this test")
def test_start_pwm_with_polarity_zero(self):
PWM.start("PWM0", 0, 2000, 0)
pwm_test = '/sys/class/pwm/pwmchip0/pwm0'
pwm_test = '/sys/class/pwm/pwmchip0/pwm0/'
assert os.path.exists(pwm_test)
duty = open(pwm_test + '/duty_cycle').readline().strip()
period = open(pwm_test + '/period').readline().strip()
polarity = open(pwm_test + '/polarity').readline().strip()
#assert os.path.exists(pwm_test) == True
duty = open(pwm_test + 'duty_cycle').readline().strip()
period = open(pwm_test + 'period').readline().strip()
polarity = open(pwm_test + 'polarity').readline().strip()
assert int(duty) == 0
assert int(period) == 500000
assert str(polarity) == "normal"
PWM.cleanup()
def test_pwm_start_invalid_pwm_key(self):
with pytest.raises(ValueError):
@ -72,12 +91,10 @@ class TestPwmSetup:
def test_pwm_start_valid_duty_cycle_min(self):
#testing an exception isn't thrown
PWM.start("PWM0", 0)
PWM.cleanup()
def test_pwm_start_valid_duty_cycle_max(self):
#testing an exception isn't thrown
PWM.start("PWM0", 100)
PWM.cleanup()
def test_pwm_start_invalid_duty_cycle_high(self):
with pytest.raises(ValueError):
@ -107,79 +124,62 @@ class TestPwmSetup:
with pytest.raises(TypeError):
PWM.start("PWM0", 0, 100, "1")
@pytest.mark.xfail(reason="pwm cleanup is doing weirdness for this test")
def test_pwm_duty_modified(self):
PWM.start("PWM0", 0)
pwm_test = '/sys/class/pwm/pwmchip0/pwm0'
pwm_test = '/sys/class/pwm/pwmchip0/pwm0/'
assert os.path.exists(pwm_test)
duty = open(pwm_test + '/duty_cycle').readline().strip()
period = open(pwm_test + '/period').readline().strip()
assert os.path.exists(pwm_test) == True
duty = open(pwm_test + 'duty_cycle').readline().strip()
period = open(pwm_test + 'period').readline().strip()
assert int(duty) == 0
assert int(period) == 500000
PWM.set_duty_cycle("PWM0", 100)
duty = open(pwm_test + '/duty_cycle').readline().strip()
period = open(pwm_test + '/period').readline().strip()
duty = open(pwm_test + 'duty_cycle').readline().strip()
period = open(pwm_test + 'period').readline().strip()
assert int(duty) == 500000
assert int(period) == 500000
PWM.cleanup()
def test_pwm_duty_cycle_non_setup_key(self):
with pytest.raises(RuntimeError):
PWM.set_duty_cycle("PWM0", 100)
PWM.cleanup()
def test_pwm_duty_cycle_invalid_key(self):
with pytest.raises(ValueError):
PWM.set_duty_cycle("P9_15", 100)
PWM.cleanup()
def test_pwm_duty_cycle_invalid_value_high(self):
PWM.start("PWM0", 0)
with pytest.raises(ValueError):
PWM.set_duty_cycle("PWM0", 101)
PWM.cleanup()
def test_pwm_duty_cycle_invalid_value_negative(self):
PWM.start("PWM0", 0)
with pytest.raises(ValueError):
PWM.set_duty_cycle("PWM0", -1)
PWM.cleanup()
def test_pwm_duty_cycle_invalid_value_string(self):
PWM.start("PWM0", 0)
with pytest.raises(TypeError):
PWM.set_duty_cycle("PWM0", "a")
PWM.cleanup()
def test_pwm_frequency_invalid_value_negative(self):
PWM.start("PWM0", 0)
with pytest.raises(ValueError):
PWM.set_frequency("PWM0", -1)
PWM.cleanup()
def test_pwm_frequency_invalid_value_string(self):
PWM.start("PWM0", 0)
with pytest.raises(TypeError):
PWM.set_frequency("PWM0", "11")
PWM.cleanup()
def test_pwm_freq_non_setup_key(self):
with pytest.raises(RuntimeError):
PWM.set_frequency("PWM0", 100)
PWM.cleanup()
def test_pwm_freq_non_setup_key(self):
with pytest.raises(ValueError):
PWM.set_frequency("P9_15", 100)
PWM.cleanup()
def test_stop_pwm(self):
pass
#PWM.start("PWM0", 1)
#PWM.stop("PWM0")
#assert os.path.exists('/sys/class/gpio/gpio68')
#direction = open('/sys/class/gpio/gpio68/direction').readline().strip()
#assert direction == 'out\n'
#PWM.cleanup()

View File

@ -10,6 +10,10 @@ def teardown_module(module):
class TestSoftpwmSetup:
def setup_method(self, test_method):
PWM.cleanup()
def test_start_pwm(self):
PWM.start("XIO-P7", 50, 10)
base = GPIO.get_gpio_base() + 7