mirror of
https://github.com/xtacocorex/CHIP_IO
synced 2025-07-20 21:03:22 +00:00
Compare commits
33 Commits
Author | SHA1 | Date | |
---|---|---|---|
abbc4968c6 | |||
9a2e81f093 | |||
7cce06b472 | |||
7489fd21b4 | |||
87303b6083 | |||
8aa402fbd5 | |||
e82f62133a | |||
993ea29213 | |||
7d91bbc47e | |||
b12c0ad2fe | |||
5d8c0f6aaa | |||
4aaff04375 | |||
eeb98d468f | |||
51c38843d9 | |||
5aa08351c2 | |||
9d7fb634a3 | |||
3cd69cfe93 | |||
cf9916018b | |||
e0c65de9a8 | |||
794ce98ed2 | |||
617f562b1c | |||
89cbea038e | |||
82d6fef173 | |||
e2173f0235 | |||
30593306a3 | |||
a9902d1267 | |||
fcd5afcdc8 | |||
d66032ed93 | |||
9a83cf765a | |||
d95847eb5a | |||
12bc9dec67 | |||
29eb018e78 | |||
ee0e27cb22 |
@ -1,3 +1,27 @@
|
||||
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
228
CHIP_IO/LRADC.py
Normal 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
|
||||
|
||||
|
@ -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
|
||||
@ -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
79
CHIP_IO/Utilities.py
Normal 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=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)
|
||||
|
8
Makefile
8
Makefile
@ -1,8 +1,8 @@
|
||||
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
|
||||
|
91
README.rst
91
README.rst
@ -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.
|
||||
@ -176,10 +193,10 @@ Polling inputs::
|
||||
|
||||
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)
|
||||
@ -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**
|
||||
|
||||
|
@ -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")
|
||||
|
@ -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";
|
||||
};
|
||||
};
|
||||
};
|
||||
|
2
setup.py
2
setup.py
@ -20,7 +20,7 @@ classifiers = ['Development Status :: 3 - Alpha',
|
||||
'Topic :: System :: Hardware']
|
||||
|
||||
setup(name = 'CHIP_IO',
|
||||
version = '0.2.2',
|
||||
version = '0.2.6',
|
||||
author = 'Robert Wolterman',
|
||||
author_email = 'robert.wolterman@gmail.com',
|
||||
description = 'A module to control CHIP IO channels',
|
||||
|
203
source/c_pwm.c
203
source/c_pwm.c
@ -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,7 +100,6 @@ struct pwm_exp *lookup_exported_pwm(const char *key)
|
||||
}
|
||||
pwm = pwm->next;
|
||||
}
|
||||
|
||||
return NULL; /* standard for pointers */
|
||||
}
|
||||
|
||||
@ -84,12 +112,16 @@ int initialize_pwm(void)
|
||||
// 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,46 +133,68 @@ 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 (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);
|
||||
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 (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);
|
||||
}
|
||||
@ -148,54 +202,102 @@ int pwm_set_polarity(const char *key, int polarity) {
|
||||
{
|
||||
len = snprintf(buffer, sizeof(buffer), "%s", "inverted"); BUF2SMALL(buffer);
|
||||
}
|
||||
ssize_t s = write(pwm->polarity_fd, buffer, len); ASSRT(s == len);
|
||||
|
||||
return 0;
|
||||
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;
|
||||
}
|
||||
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));
|
||||
|
||||
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);
|
||||
ssize_t s = write(pwm->duty_fd, buffer, len); ASSRT(s == len);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
if (s == len)
|
||||
{
|
||||
if (DEBUG)
|
||||
printf(" ** SETTING pwm->enable to %d\n", enable);
|
||||
pwm->enable = enable;
|
||||
rtnval = 0;
|
||||
} else {
|
||||
rtnval = -1;
|
||||
}
|
||||
|
||||
len = snprintf(buffer, sizeof(buffer), "%d", pwm->enable); BUF2SMALL(buffer);
|
||||
ssize_t s = write(pwm->enable_fd, buffer, len); ASSRT(s == len);
|
||||
|
||||
return 0;
|
||||
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)
|
||||
{
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -41,7 +41,6 @@ SOFTWARE.
|
||||
#include "c_pwm.h"
|
||||
#include "common.h"
|
||||
#include "event_gpio.h"
|
||||
#include "Python.h"
|
||||
|
||||
#define KEYLEN 7
|
||||
|
||||
|
165
source/common.c
165
source/common.c
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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.6");
|
||||
PyModule_AddObject(module, "VERSION", version);
|
||||
}
|
||||
|
@ -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,18 +378,24 @@ 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) {
|
||||
if (!fd)
|
||||
{
|
||||
if ((fd = open_value_file(gpio)) == -1) {
|
||||
char err[256];
|
||||
snprintf(err, sizeof(err), "gpio_set_value: could not open '%s' (%s)", filename, strerror(errno));
|
||||
snprintf(err, sizeof(err), "gpio_get_value: could not open GPIO %d value file", gpio);
|
||||
add_error_msg(err);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (value) {
|
||||
strncpy(vstr, "1", ARRAY_SIZE(vstr) - 1);
|
||||
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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,
|
||||
|
@ -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}
|
||||
};
|
||||
|
@ -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!
|
@ -1,2 +0,0 @@
|
||||
<EFBFBD>}q(Utest_softpwm_setup]q(Uteardown_moduleqUTestSoftpwmSetupqeUtest_gpio_output]q(hUTestGPIOOutputqeUtest_pwm_setup]q(hUTestPwmSetupqeUtest_gpio_setup]q (hU TestSetupq
|
||||
eUtest_gpio_input]q(hU
|
@ -1 +0,0 @@
|
||||
<EFBFBD>]q(]q]qe.
|
@ -1 +0,0 @@
|
||||
<EFBFBD>}q.
|
0
test/gptest.py
Executable file → Normal file
0
test/gptest.py
Executable file → Normal file
67
test/lradc_test.py
Normal file
67
test/lradc_test.py
Normal 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
0
test/omtest.py
Executable file → Normal file
99
test/pwmtest.py
Normal file
99
test/pwmtest.py
Normal 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
0
test/spwmtest.py
Executable file → Normal file
0
test/test_gpio_input.py
Executable file → Normal file
0
test/test_gpio_input.py
Executable file → Normal 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()
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user