diff --git a/CHANGELOG.rst b/CHANGELOG.rst index ea892dd..749e32e 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,3 +1,17 @@ +0.2.1 +---- +* Pull request #12 fixes: + - Fixed indent in the i2c-1 dts + - Removed import dependencies in the SPI and PWM overlays + - Re-enabled building of the dtbo on setup.py install + +0.2.0 +---- +* Added the ability to load DTB Overlays from within CHIP_IO + - Support for PWM0, SPI2, and I2C-1 (which comes back as i2c-3 on the 4.4 CHIP + - Support for a custom DTB Overlay +* Fixes to the pwm unit test, all but 2 now pass :) + 0.1.2 ---- * SoftPWM Fix by aninternetof diff --git a/CHIP_IO/OverlayManager.py b/CHIP_IO/OverlayManager.py new file mode 100644 index 0000000..7b8f37c --- /dev/null +++ b/CHIP_IO/OverlayManager.py @@ -0,0 +1,202 @@ +import os +import shutil +import time + +DEBUG = False + +OVERLAYINSTALLPATH = "/lib/firmware/chip_io" +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/" + +# LOADED VARIABLES +# 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" +} + +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 + """ + global _LOADED + return _LOADED["SPI2"] + +def get_pwm_loaded(): + """ + get_pwm_loaded - Returns True/False based upon if the pwm0 Overlay is loaded + """ + global _LOADED + return _LOADED["PWM0"] + +def get_custom_loaded(): + """ + get_custom_loaded - Returns True/False based upon if a Custom Overlay is loaded + """ + global _LOADED + return _LOADED["CUST"] + +def _set_overlay_verify(name, overlay_path, config_path): + """ + _set_overlay_verify - Function to load the overlay and verify it was setup properly + """ + global DEBUG + # VERIFY PATH IS NOT THERE + if os.path.exists(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) + + # CAT THE OVERLAY INTO THE CONFIG FILESYSTEM + with open(config_path + "/dtbo", 'wb') as outfile: + with open(overlay_path, 'rb') as infile: + shutil.copyfileobj(infile, outfile) + + # SLEEP TO ENABLE THE KERNEL TO DO ITS JOB + time.sleep(0.2) + + # VERIFY + if name == "CUST": + # BLINDLY ACCEPT THAT IT LOADED + return 0 + elif name == "PWM0": + if os.path.exists(PWMSYSFSPATH): + if DEBUG: + print("PWM IS LOADED!") + return 0 + else: + 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: + print("SPI2 IS LOADED!") + return 0 + else: + if DEBUG: + print("ERROR LOADING SPI2") + return 0 + +def load(overlay, path=""): + """ + load - Load a DTB Overlay + + Inputs: + overlay - Overlay Key: I2C1, SPI2, PWM0, CUST + path - Full Path to where the custom overlay is stored + + Returns: + 0 - Successful Load + 1 - Unsuccessful Load + 2 - Overlay was previously set + """ + global DEBUG + global _LOADED + if DEBUG: + print("LOAD OVERLAY: {0} @ {1}".format(overlay,path)) + # SEE IF OUR OVERLAY NAME IS IN THE KEYS + if overlay.upper() in _OVERLAYS.keys(): + cpath = OVERLAYCONFIGPATH + "/" + _FOLDERS[overlay.upper()] + if DEBUG: + print("VALID OVERLAY") + print("CONFIG PATH: {0}".format(cpath)) + # CHECK TO SEE IF WE HAVE A PATH FOR CUSTOM OVERLAY + if overlay.upper() == "CUST" and path == "": + raise ValueError("Path must be specified for Custom Overlay Choice") + elif overlay.upper() == "CUST" and _LOADED[overlay.upper()]: + print("Custom Overlay already loaded") + return 2 + elif overlay.upper() == "CUST" and not os.path.exists(path): + print("Custom Overlay path does not exist") + return 1 + + # SET UP THE OVERLAY PATH FOR OUR USE + if overlay.upper() != "CUST": + opath = OVERLAYINSTALLPATH + "/" + _OVERLAYS[overlay.upper()] + else: + opath = path + if DEBUG: + print("OVERLAY PATH: {0}".format(opath)) + + if overlay.upper() == "PWM0" and _LOADED[overlay.upper()]: + 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 + + # LOAD THE OVERLAY + errc = _set_overlay_verify(overlay.upper(), opath, cpath) + if DEBUG: + print("_SET_OVERLAY_VERIFY ERRC: {0}".format(errc)) + if errc == 0: + _LOADED[overlay.upper()] = True + + else: + raise ValueError("Invalid Overlay name specified! Choose between: I2C1, SPI2, PWM0, CUST") + +def unload(overlay): + global DEBUG + global _LOADED + if DEBUG: + print("UNLOAD OVERLAY: {0}".format(overlay)) + # SEE IF OUR OVERLAY NAME IS IN THE KEYS + if overlay.upper() in _OVERLAYS.keys(): + # BRUTE FORCE REMOVE AS THE DIRECTORY CONTAINS FILES + 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") + + diff --git a/README.rst b/README.rst index b7a7d54..5e76d9d 100644 --- a/README.rst +++ b/README.rst @@ -2,11 +2,17 @@ CHIP_IO ============================ A CHIP GPIO library +NOTE: Now requires the custom DTC to install the library + Manual:: - sudo ntpdate pool.ntp.org sudo apt-get update - sudo apt-get install git build-essential python-dev python-pip -y + sudo apt-get install git build-essential python-dev python-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 python setup.py install @@ -17,7 +23,7 @@ Manual:: Using the library is very similar to the excellent RPi.GPIO library used on the Raspberry Pi. Below are some examples. -All scripts that require GPIO and PWM (HW and/or SW) access need to be run with super user permissions! +All scripts that require GPIO, PWM (HW and/or SW), and Overlay Manager need to be run with super user permissions! **Allowable Pin Names for the Library** @@ -208,22 +214,24 @@ Hardware PWM requires a DTB Overlay loaded on the CHIP to allow the kernel to kn **SOFTPWM**:: - import CHIP_IO.SOFTPWM as PWM - #PWM.start(channel, duty, freq=2000, polarity=0) + import CHIP_IO.SOFTPWM as SPWM + #SPWM.start(channel, duty, freq=2000, polarity=0) #duty values are valid 0 (off) to 100 (on) #you can choose any pin - PWM.start("XIO-P7", 50) - PWM.set_duty_cycle("XIO-P7", 25.5) - PWM.set_frequency("XIO-P7", 10) + SPWM.start("XIO-P7", 50) + SPWM.set_duty_cycle("XIO-P7", 25.5) + SPWM.set_frequency("XIO-P7", 10) - PWM.stop("XIO-P7") - PWM.cleanup() + SPWM.stop("XIO-P7") + SPWM.cleanup() #set polarity to 1 on start: - PWM.start("XIO-P7", 50, 2000, 1) + 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**:: Not Implemented yet @@ -232,6 +240,38 @@ Use SOFTPWM at low speeds (hundreds of Hz) for the best results. Do not use for SPI requires a DTB Overlay to access. CHIP_IO does not contain any SPI specific code as the Python spidev module works when it can see the SPI bus. +**Overlay Manager**:: + +The Overlay Manager enables you to quickly load simple Device Tree Overlays. The options for loading are: +PWM0, SPI2, 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() + # To load an overlay, feed in the name to load() + OM.load("PWM0") + # To verify the overlay was properly loaded, the get_ functions return booleans + OM.get_pwm_loaded() + OM.get_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") + # You can check for loading like above, but it's really just there for sameness + OM.get_custom_loaded() + # To unload, just call unload() + OM.unload("CUST") + +Note that this requires the 4.4 kernel with the CONFIG_OF_CONFIGFS option enabled in the kernel config. + **Running tests** Install py.test to run the tests. You'll also need the python compiler package for py.test.:: diff --git a/overlays/__init__.py b/overlays/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/overlays/builder.py b/overlays/builder.py new file mode 100644 index 0000000..4355f96 --- /dev/null +++ b/overlays/builder.py @@ -0,0 +1,24 @@ +from subprocess import call +import os +import shutil +import glob + +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(): + 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/") + print("Removing old DTBO files (if applicable)") + for fl in glob.glob("/lib/firmware/chip_io/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") + diff --git a/overlays/chip-i2c1.dts b/overlays/chip-i2c1.dts new file mode 100644 index 0000000..09aa600 --- /dev/null +++ b/overlays/chip-i2c1.dts @@ -0,0 +1,72 @@ +/* + * Copyright 2016 Free Electrons + * Copyright 2016 NextThing Co + * + * Maxime Ripard + * + * 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"; + }; + }; +}; + diff --git a/overlays/chip-pwm0.dts b/overlays/chip-pwm0.dts new file mode 100644 index 0000000..a25abb0 --- /dev/null +++ b/overlays/chip-pwm0.dts @@ -0,0 +1,61 @@ +/* + * Copyright 2016 Free Electrons + * Copyright 2016 NextThing Co + * + * Maxime Ripard + * + * 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"; + + /* Enable the PWM */ + fragment@0 { + target = <&pwm>; + + __overlay__ { + pinctrl-names = "default"; + pinctrl-0 = <&pwm0_pins>; + status = "okay"; + }; + }; +}; diff --git a/overlays/chip-spi2.dts b/overlays/chip-spi2.dts new file mode 100644 index 0000000..7ecd79b --- /dev/null +++ b/overlays/chip-spi2.dts @@ -0,0 +1,93 @@ +/* + * Copyright 2016, Robert Wolterman + * This file is an amalgamation of stuff from Kolja Windeler, Maxime Ripard, and Renzo. + * + * 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"; + + /* activate the gpio for interrupt */ + fragment@0 { + target-path = <&pio>; + + __overlay__ { + chip_spi2_pins: spi2@0 { + allwinner,pins = "PE1", "PE2", "PE3"; + allwinner,function = "spi2"; + allwinner,drive = "0"; //; + allwinner,pull = "0"; //; + }; + + chip_spi2_cs0_pins: spi2_cs0@0 { + allwinner,pins = "PE0"; + allwinner,function = "spi2"; + allwinner,drive = "0"; //; + allwinner,pull = "0"; //; + }; + }; + }; + + /* + * Enable our SPI device, with an spidev device connected + * to it + */ + fragment@1 { + target = <&spi2>; + + __overlay__ { + #address-cells = <1>; + #size-cells = <0>; + pinctrl-names = "default"; + pinctrl-0 = <&chip_spi2_pins>, <&chip_spi2_cs0_pins>; + status = "okay"; + + spi2@0 { + compatible = "rohm,dh2228fv"; + reg = <0>; + spi-max-frequency = <24000000>; + }; + }; + }; +}; + diff --git a/setup.py b/setup.py index 0e9e334..4cb737a 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ classifiers = ['Development Status :: 3 - Alpha', 'Topic :: System :: Hardware'] setup(name = 'CHIP_IO', - version = '0.1.2', + version = '0.2.1', author = 'Robert Wolterman', author_email = 'robert.wolterman@gmail.com', description = 'A module to control CHIP IO channels', diff --git a/source/constants.c b/source/constants.c index 7a86c53..e7a481e 100644 --- a/source/constants.c +++ b/source/constants.c @@ -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.1.2"); + version = Py_BuildValue("s", "0.2.1"); PyModule_AddObject(module, "VERSION", version); } diff --git a/test/omtest.py b/test/omtest.py new file mode 100755 index 0000000..c680ecb --- /dev/null +++ b/test/omtest.py @@ -0,0 +1,49 @@ +#!/usr/bin/python + +import CHIP_IO.OverlayManager as OM +import os + +# ENABLE DEBUG +print("ENABLING OVERLAY MANAGER DEBUG") +OM.enable_debug() + +# **************** PWM ******************* +print("\nIS PWM ENABLED: {0}".format(OM.get_pwm_loaded())) +OM.load("PWM0") +print("IS PWM ENABLED: {0}".format(OM.get_pwm_loaded())) +# VERIFY PWM0 EXISTS +if os.path.exists('/sys/class/pwm/pwmchip0'): + print("PWM DEVICE EXISTS") +else: + print("PWM DEVICE DID NOT LOAD PROPERLY") +print("UNLOADING PWM0") +OM.unload("PWM0") +print("IS PWM ENABLED: {0}".format(OM.get_pwm_loaded())) + +# **************** I2C-1 ******************* +print("\nIS I2C ENABLED: {0}".format(OM.get_i2c_loaded())) +OM.load("I2C1") +print("IS I2C ENABLED: {0}".format(OM.get_i2c_loaded())) +# VERIFY I2C-1 EXISTS +if os.path.exists('/sys/class/i2c-dev/i2c-1'): + print("I2C1 DEVICE EXISTS") +else: + print("I2C1 DEVICE DID NOT LOAD PROPERLY") +print("UNLOADING I2C1") +OM.unload("I2C1") +print("IS I2C ENABLED: {0}".format(OM.get_i2c_loaded())) + +# **************** SPI2 ******************* +print("\nIS SPI ENABLED: {0}".format(OM.get_spi_loaded())) +OM.load("SPI2") +print("IS SPI ENABLED: {0}".format(OM.get_spi_loaded())) +# VERIFY SPI2 EXISTS +if os.listdir('/sys/class/spi_master') != "": + print("SPI DEVICE EXISTS") +else: + print("SPI DEVICE DID NOT LOAD PROPERLY") +print("UNLOADING SPI") +OM.unload("SPI2") +print("IS SPI ENABLED: {0}".format(OM.get_spi_loaded())) + + diff --git a/test/test_pwm_setup.py b/test/test_pwm_setup.py index e5944dd..48e0e88 100644 --- a/test/test_pwm_setup.py +++ b/test/test_pwm_setup.py @@ -13,8 +13,8 @@ class TestPwmSetup: pwm_test = '/sys/class/pwm/pwmchip0/pwm0' assert os.path.exists(pwm_test) - duty = open(pwm_test + '/duty_cycle').read() - period = open(pwm_test + '/period').read() + 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() @@ -25,12 +25,12 @@ class TestPwmSetup: pwm_test = '/sys/class/pwm/pwmchip0/pwm0' assert os.path.exists(pwm_test) - duty = open(pwm_test + '/duty_cycle').read() - period = open(pwm_test + '/period').read() - polarity = open(pwm_test + '/polarity').read() + 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 string(polarity) == "inverted" + assert str(polarity) == "inverted" PWM.cleanup() def test_start_pwm_with_polarity_default(self): @@ -39,12 +39,12 @@ class TestPwmSetup: pwm_test = '/sys/class/pwm/pwmchip0/pwm0' assert os.path.exists(pwm_test) - duty = open(pwm_test + '/duty_cycle').read() - period = open(pwm_test + '/period').read() - polarity = open(pwm_test + '/polarity').read() + 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 string(polarity) == "normal" + assert str(polarity) == "normal" PWM.cleanup() def test_start_pwm_with_polarity_zero(self): @@ -53,12 +53,12 @@ class TestPwmSetup: pwm_test = '/sys/class/pwm/pwmchip0/pwm0' assert os.path.exists(pwm_test) - duty = open(pwm_test + '/duty_cycle').read() - period = open(pwm_test + '/period').read() - polarity = open(pwm_test + '/polarity').read() + 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 string(polarity) == "normal" + assert str(polarity) == "normal" PWM.cleanup() def test_pwm_start_invalid_pwm_key(self): @@ -113,14 +113,14 @@ class TestPwmSetup: pwm_test = '/sys/class/pwm/pwmchip0/pwm0' assert os.path.exists(pwm_test) - duty = open(pwm_test + '/duty_cycle').read() - period = open(pwm_test + '/period').read() + 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').read() - period = open(pwm_test + '/period').read() + 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() @@ -180,6 +180,6 @@ class TestPwmSetup: #PWM.start("PWM0", 1) #PWM.stop("PWM0") #assert os.path.exists('/sys/class/gpio/gpio68') - #direction = open('/sys/class/gpio/gpio68/direction').read() + #direction = open('/sys/class/gpio/gpio68/direction').readline().strip() #assert direction == 'out\n' #PWM.cleanup()