I've found that using software PWM intensively (calling start/stop for several hundreds) cause that PWM stop working. No error on start call but also it won't change GPIO pin status anymore. After application restart everything works as expected. I have a small setup for reproducing this issue. On RPi #26 GPIO pin is connected LED with resistor. This code in testled.py (yeah, it's very stupid for production but it can reproduce this issue):
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(26, GPIO.OUT)
pwm = GPIO.PWM(26, 1)
def blink_led(cycles):
for i in xrange(cycles):
pwm.ChangeFrequency(25)
pwm.start(50)
time.sleep(1)
pwm.stop()
Now when I launch python interpreter, import testled and run blink_led(400). Led starts blinking but after some time LED turns off and is never lightened again. Even when blink_led finish and is runned again it won't turn on LED again. You have to stop python interpreter, run it again and then it works... for a while.
I have some code which only turning GPIO status (without using PWM in this library) and there everything looks OK - even after long time GPIO pins are working properly. So it seems that this is related only to PWM.
I have looked a bit into this issue and main problem seems to be related to Linux threads. Library creates/destroys posix thread at every start/stop pwm. On my RPi I am able to create and destroy only 380 threads within single process. After that threshold pthread_create always returns error code 11. See test code in attachment.
I suggest solution to create thread per GPIO just once and then sleep it with mutex or something to prevent too many create/destroy cycles.
I created a github repo (https://github.com/wuestkamp/raspberry-gpio-python) which solves this issue and describes how to solve this.
Fix added to source code library. Will be included in next release.