Debian 10 (Buster) has gcc 8.2 which warns about:
source/c_pwm.c:459:65: error: argument to ‘sizeof’ in ‘strncpy’ call is the same expression as the source; did you mean to use the size of the destination? [-Werror=sizeof-pointer-memaccess]
source/c_pwm.c:396:43: error: ‘%s’ directive output may be truncated writing up to 199 bytes into a region of size 100 [-Werror=format-truncation=]
Issue #197: JesseMcL raised the issue that running
PWM.start() and PWM.start() in a loop will eventually
exhaust the number of open file descriptors and
PWM.start() will raise the error:
RuntimeError: problem with sysfs file.
The file descriptor for the enable file should
be closed in pwm_disable() to avoid the leak
Signed-off-by: Drew Fustini <drew@pdp7.com>
sleep 100 ms after export to avoid race condition as udev
needs the opportunity to set group ownership and permission
Test instructions:
==================
reboot, log back in, wait for udev to set group ownership and permission:
$ ls -la /sys/devices/platform/ocp/48304000.epwmss/48304200.pwm/pwm/pwmchip6/export
-rw-rw---- 1 root pwm 4096 Nov 2 04:02 /sys/devices/platform/ocp/48304000.epwmss/48304200.pwm/pwm/pwmchip6/export
run test program:
python ~/issue185.py
contents of test program:
import Adafruit_BBIO.PWM as PWM
PWM.start("P8_13", 50, 1000, 0)
PWM.stop("P8_13")
PWM.cleanup()
Example of pwm and ecap paths for pwm outputs in the 4.14 kernel:
/sys/devices/platform/ocp/48300000.epwmss/48300100.ecap/pwm/pwmchip0/pwm-0:0/duty_cycle
/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip1/pwm-1:0/duty_cycle
/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip1/pwm-1:1/duty_cycle
/sys/devices/platform/ocp/48302000.epwmss/48302200.pwm/pwm/pwmchip3/pwm-3:0/duty_cycle
/sys/devices/platform/ocp/48302000.epwmss/48302200.pwm/pwm/pwmchip3/pwm-3:1/duty_cycle
/sys/devices/platform/ocp/48304000.epwmss/48304100.ecap/pwm/pwmchip5/pwm-5:0/duty_cycle
Note that the ecap path is longer than pwm path by 1 character
In definition of struct pwm_exp, extended length of key by 1 to ensure room for a terminating byte. Currently, the code doesn't check this, expecting strncpy in pwm_start to trim and NUL-terminate the string.
In lookup_exported_pwm, return "not-found" condition as NULL, not 0, per general pointer usage recommendations.
In pwm_start, added code to ensure NUL-termination of the key.
In pwm_disable, there's a path that doesn't set prev_pwm and can leave it NULL. It being NULL seems to indicate a "first-time" flag, for when the first item in the list is matched. If the search should be terminated after finding device "key", should break be used? Reasonably, a device named "key" should only be found once in the tree, but as coded, it seems that this search loop might find it more than once.
In pwm_set_polarity, the original length of the buffer as 5 will cover positive 16-bit integers, but not room for a trailing NUL byte. If accidently negative, a size of 7 will include room for the sign without breaking.