adafruit-beaglebone-io-python/test/test_pwm_setup.py
Drew Fustini a7dd48c3f4 update PWM test for 4.14 kernel udev paths
pytest now passes all tests OK on 4.14.0-rc7-ti-r6 as non-root user:

debian@beaglebone:~/ssh/adafruit-beaglebone-io-python/test$ python -m pytest
======== test session starts =============
platform linux2 -- Python 2.7.13, pytest-3.2.3, py-1.4.34, pluggy-0.4.0
rootdir: /home/debian/ssh/adafruit-beaglebone-io-python, inifile:
collected 58 items

test_adc.py ......
test_gpio_input.py ..
test_gpio_output.py ......
test_gpio_setup.py ..........
test_led.py ..
test_pwm_setup.py ...........................
test_spi.py ..
test_uart.py ...

======== 58 passed in 13.30 seconds ========

debian@beaglebone:~$ sudo /opt/scripts/tools/version.sh
git:/opt/scripts/:[6c2688b0be448b7bb9ca18849b430d496a84acb4]
eeprom:[A335BNLT000C3014BBBK1316]
model:[TI_AM335x_BeagleBone_Black]
dogtag:[BeagleBoard.org Debian Image 2017-10-17]
bootloader:[microSD-(push-button)]:[/dev/mmcblk0]:[U-Boot 2017.09-00002-g0f3f1c7907]
bootloader:[eMMC-(default)]:[/dev/mmcblk1]:[U-Boot 2017.09-rc2-00002-g84a7f2]
kernel:[4.14.0-rc7-ti-r6]
nodejs:[v6.11.5]
uboot_overlay_options:[enable_uboot_overlays=1]
uboot_overlay_options:[uboot_overlay_pru=/lib/firmware/AM335X-PRU-RPROC-4-4-TI-00A0.dtbo]
uboot_overlay_options:[enable_uboot_cape_universal=1]
pkg:[bb-cape-overlays]:[4.4.20171027.1-0rcnee1~stretch+20171027]
pkg:[bb-wl18xx-firmware]:[1.20170829-0rcnee1~stretch+20170829]
pkg:[firmware-ti-connectivity]:[20170823-1rcnee0~stretch+20170830]
2017-11-01 09:05:23 +00:00

311 lines
9.7 KiB
Python

import glob
import os
import platform
import Adafruit_BBIO.PWM as PWM
import pytest
kernel = platform.release()
def teardown_module(module):
PWM.cleanup()
def get_pwm_dir():
if kernel >= '4.1.0':
# On 4.1+, the pwm subdirectory sometimes takes different names:
# .pwm or .ehrpwm, etc.
results = glob.glob(
"/sys/devices/platform/ocp/48302000.*/" +
"48302200.*/pwm/pwmchip?/pwm*")
# We expect that there will be a result (a directory fitting
# our path exists) so test that with an assertion.
assert len(results) > 0
# Continue with the pwm_dir found
return results[0]
else:
files = os.listdir('/sys/devices')
ocp = '/sys/devices/' + [s for s in files if s.startswith('ocp')][0]
files = os.listdir(ocp)
return ocp + '/' + [s for s in files if s.startswith('pwm_test_P9_14')][0]
class TestPwmSetup:
def test_start_pwm(self):
PWM.cleanup()
PWM.start("P9_14", 0)
pwm_dir = get_pwm_dir()
assert os.path.exists(pwm_dir)
if kernel >= '4.1.0':
duty = open(pwm_dir + '/duty_cycle').read()
else:
duty = open(pwm_dir + '/duty').read()
period = open(pwm_dir + '/period').read()
assert int(duty) == 0
assert int(period) == 500000
PWM.cleanup()
def test_start_pwm_ecap0(self):
print("test_start_pwm_ecap0\n");
PWM.cleanup()
PWM.start("P9_42", 0)
pwm_dir = get_pwm_dir()
assert os.path.exists(pwm_dir)
if kernel >= '4.1.0':
duty = open(pwm_dir + '/duty_cycle').read()
else:
duty = open(pwm_dir + '/duty').read()
period = open(pwm_dir + '/period').read()
assert int(duty) == 0
assert int(period) == 500000
PWM.cleanup()
# test not enabled as default as
# cape-universala overlay required
#def test_start_pwm_ecap2(self):
#print("test_start_pwm_ecap2\n");
#PWM.cleanup()
#PWM.start("P9_28", 0)
#pwm_dir = get_pwm_dir()
#assert os.path.exists(pwm_dir)
#if kernel >= '4.1.0':
#duty = open(pwm_dir + '/duty_cycle').read()
#else:
#duty = open(pwm_dir + '/duty').read()
#period = open(pwm_dir + '/period').read()
#assert int(duty) == 0
#assert int(period) == 500000
#PWM.cleanup()
def test_start_pwm_with_polarity_one(self):
PWM.cleanup()
PWM.start("P9_14", 0, 2000, 1)
pwm_dir = get_pwm_dir()
assert os.path.exists(pwm_dir)
if kernel >= '4.1.0':
duty = open(pwm_dir + '/duty_cycle').read()
else:
duty = open(pwm_dir + '/duty').read()
period = open(pwm_dir + '/period').read()
polarity = open(pwm_dir + '/polarity').read()
assert int(duty) == 0
assert int(period) == 500000
# TEMPORARY FIX: disable polarity check
# due to issue in 4.9.x+ kernels
# refer to issue #170:
# https://github.com/adafruit/adafruit-beaglebone-io-python/issues/170
# and commit c35e4cb from pull request #173:
# "source/c_pwm.c: disable pwm_set_polarity (broken in v4.9.x/v4.14.x)"
# https://github.com/adafruit/adafruit-beaglebone-io-python/pull/173/commits/c35e4cb98a1f14c85aca7259132bcc97e93d78f8
#if kernel >= '4.1.0':
# assert polarity == "inversed\n"
#else:
# assert int(polarity) == 1
PWM.cleanup()
def test_start_pwm_with_polarity_default(self):
PWM.cleanup()
PWM.start("P9_14", 0, 2000)
pwm_dir = get_pwm_dir()
assert os.path.exists(pwm_dir)
if kernel >= '4.1.0':
duty = open(pwm_dir + '/duty_cycle').read()
else:
duty = open(pwm_dir + '/duty').read()
period = open(pwm_dir + '/period').read()
polarity = open(pwm_dir + '/polarity').read()
assert int(duty) == 0
assert int(period) == 500000
# TEMPORARY FIX: disable polarity check
# due to issue in 4.9.x+ kernels
# refer to issue #170:
# https://github.com/adafruit/adafruit-beaglebone-io-python/issues/170
# and commit c35e4cb from pull request #173:
# "source/c_pwm.c: disable pwm_set_polarity (broken in v4.9.x/v4.14.x)"
# https://github.com/adafruit/adafruit-beaglebone-io-python/pull/173/commits/c35e4cb98a1f14c85aca7259132bcc97e93d78f8
#if kernel >= '4.1.0':
# assert polarity == 'normal\n'
#else:
# assert int(polarity) == 0
PWM.cleanup()
def test_start_pwm_with_polarity_zero(self):
PWM.cleanup()
PWM.start("P9_14", 0, 2000, 0)
pwm_dir = get_pwm_dir()
assert os.path.exists(pwm_dir)
if kernel >= '4.1.0':
duty = open(pwm_dir + '/duty_cycle').read()
else:
duty = open(pwm_dir + '/duty').read()
period = open(pwm_dir + '/period').read()
polarity = open(pwm_dir + '/polarity').read()
assert int(duty) == 0
assert int(period) == 500000
if kernel >= '4.1.0':
assert polarity == 'normal\n'
else:
assert int(polarity) == 0
PWM.cleanup()
def test_pwm_start_invalid_pwm_key(self):
PWM.cleanup()
with pytest.raises(ValueError):
PWM.start("P8_25", -1)
def test_pwm_start_invalid_duty_cycle_negative(self):
PWM.cleanup()
with pytest.raises(ValueError):
PWM.start("P9_14", -1)
def test_pwm_start_valid_duty_cycle_min(self):
PWM.cleanup()
# testing an exception isn't thrown
PWM.start("P9_14", 0)
PWM.cleanup()
def test_pwm_start_valid_duty_cycle_max(self):
PWM.cleanup()
# testing an exception isn't thrown
PWM.start("P9_14", 100)
PWM.cleanup()
def test_pwm_start_invalid_duty_cycle_high(self):
PWM.cleanup()
with pytest.raises(ValueError):
PWM.start("P9_14", 101)
def test_pwm_start_invalid_duty_cycle_string(self):
PWM.cleanup()
with pytest.raises(TypeError):
PWM.start("P9_14", "1")
def test_pwm_start_invalid_frequency_negative(self):
PWM.cleanup()
with pytest.raises(ValueError):
PWM.start("P9_14", 0, -1)
def test_pwm_start_invalid_frequency_string(self):
PWM.cleanup()
with pytest.raises(TypeError):
PWM.start("P9_14", 0, "1")
def test_pwm_start_negative_polarity(self):
PWM.cleanup()
with pytest.raises(ValueError):
PWM.start("P9_14", 0, 100, -1)
def test_pwm_start_invalid_positive_polarity(self):
PWM.cleanup()
with pytest.raises(ValueError):
PWM.start("P9_14", 0, 100, 2)
def test_pwm_start_invalid_polarity_type(self):
PWM.cleanup()
with pytest.raises(TypeError):
PWM.start("P9_14", 0, 100, "1")
def test_pwm_duty_modified(self):
PWM.cleanup()
PWM.start("P9_14", 0)
pwm_dir = get_pwm_dir()
assert os.path.exists(pwm_dir)
if kernel >= '4.1.0':
duty = open(pwm_dir + '/duty_cycle').read()
else:
duty = open(pwm_dir + '/duty').read()
period = open(pwm_dir + '/period').read()
assert int(duty) == 0
assert int(period) == 500000
PWM.set_duty_cycle("P9_14", 100)
if kernel >= '4.1.0':
duty = open(pwm_dir + '/duty_cycle').read()
else:
duty = open(pwm_dir + '/duty').read()
period = open(pwm_dir + '/period').read()
assert int(duty) == 500000
assert int(period) == 500000
PWM.cleanup()
def test_pwm_duty_cycle_non_setup_key(self):
PWM.cleanup()
with pytest.raises(RuntimeError):
PWM.set_duty_cycle("P9_14", 100)
PWM.cleanup()
def test_pwm_duty_cycle_invalid_key(self):
PWM.cleanup()
with pytest.raises(ValueError):
PWM.set_duty_cycle("P9_15", 100)
PWM.cleanup()
def test_pwm_duty_cycle_invalid_value_high(self):
PWM.cleanup()
PWM.start("P9_14", 0)
with pytest.raises(ValueError):
PWM.set_duty_cycle("P9_14", 101)
PWM.cleanup()
def test_pwm_duty_cycle_invalid_value_negative(self):
PWM.cleanup()
PWM.start("P9_14", 0)
with pytest.raises(ValueError):
PWM.set_duty_cycle("P9_14", -1)
PWM.cleanup()
def test_pwm_duty_cycle_invalid_value_string(self):
PWM.cleanup()
PWM.start("P9_14", 0)
with pytest.raises(TypeError):
PWM.set_duty_cycle("P9_14", "a")
PWM.cleanup()
def test_pwm_frequency_invalid_value_negative(self):
PWM.cleanup()
PWM.start("P9_14", 0)
with pytest.raises(ValueError):
PWM.set_frequency("P9_14", -1)
PWM.cleanup()
def test_pwm_frequency_invalid_value_string(self):
PWM.cleanup()
PWM.start("P9_14", 0)
with pytest.raises(TypeError):
PWM.set_frequency("P9_14", "11")
PWM.cleanup()
def test_pwm_freq_non_setup_key(self):
PWM.cleanup()
with pytest.raises(RuntimeError):
PWM.set_frequency("P9_14", 100)
PWM.cleanup()
def test_pwm_freq_non_setup_invalid_key(self):
PWM.cleanup()
with pytest.raises(ValueError):
PWM.set_frequency("P9_15", 100)
PWM.cleanup()
def test_stop_pwm(self):
pass
# PWM.start("P9_14", 1)
# PWM.stop("P9_14")
# assert os.path.exists('/sys/class/gpio/gpio68')
# direction = open('/sys/class/gpio/gpio68/direction').read()
# assert direction == 'out\n'
# PWM.cleanup()