-
Notifications
You must be signed in to change notification settings - Fork 223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PWM no work on all gpio pins? #20
Comments
This is how it is limited by hardware implementation, see BCM2835-ARM-Peripherals - section 6.2 Your python library probably uses only software implementation for pwm on other pins. This means it actually uses regular output mode and some software timers. You can easily implement this by yourself in Go. Problem is you can reliably achieve at most few kHz with software implementation. Which is like 10-1000× slower compared to hw pwm/clock mode. And the accuracy of software timer is only about 5 to 50 microseconds at best. So even for slower frequences, if you need very precise output, yo have to use real pwm mode. |
Thanks for reply. But I look into python library RPi.GPIO, it is use /dev/gpiomem as same as go-rpio. And not found any software implementation for pwm on other pins. Other library pi-blaster enable pins as go-rpio. But it can be enable other pins by arguments (and not found any software implementation for pwm). |
From the project description of rpi.gpio:
|
@PeerXu You are kind of right with pi-blaster. I went through the source code and it is interesting how it works. In my eyes it lays somewhere between pure hw implementation as in go-rpio or wiringPi and sw based implementation as in RPi.GPIO and similar libs. If I understand it well: it uses If you need to use more pwm pins than go-rpio supports I'd suggest you to use pi-blaster, or if you don't need high accuracy and high frequences, you can implement custom sw pwm by yourself. @stianeikeland Maybe there should be also software implementation of pwm in go-rpio? What do you think, does it makes sense, or should go-rpio be only low-level based as it is now? |
Thanks again! I am using pi-blaster now! |
@Drahoslav7 If someone is willing to put in the time for a decent and easy to use software PWM, then by all means, I'm open for it. The API just needs to make it very clear that this is software PWM, and the other one is hardware PWM :) |
I just want to share my experience with software PWM, in case it can help somebody else. I am running a Raspberry PI 3 with a DAC hat and wanted to remote control my AMP with an IR LED. As @drahoslove mentioned, software timers are slow and extremely unreliable. So with inspiration from a Rust library (spin_sleep), I created a more precise sleep for Go: powernap. To get the fastest and most reliable result, I build in a scheduler (Plan), that can be used to add a function with a pre-calculated sleep durations. This way, I'm able to pre-calculate a RC5(x) signal and build a plan for switching the GPIO pin on or off at the correct intervals. I would expect it to get too imprecise before getting to 1MHz, but for the 144kHz signal through an LED, it works quite good. One thing to notice, is that the "safe" zone for using native time.Sleep is way up in the millisecond zone (below 1kHz), which causes CPU cycles to be traded for precision constantly. Maybe it's possible to build an actual, decent soft PWM from this? Hope these findings can help others. |
Interestingly enough, there's already a PR, #24 , for this. The author just hasn't addressed the review comments. @morphar interesting work! Software PWM would be a good addition to this library. If you'd like to have a go :) at it you could start with an implementation I did in another project. A super minor comment. In the
to this
Anyway, I'm going to keep powermap in the back of my mind as it looks very useful. |
Thanks @youngkin :) When I get some time, I will probably have a go :) at the soft PWM. This was just an initial PoC to see if it was possible and not a general purpose PWM. I think that the implementation you did, will probably "just work", if you replace the sleep with powernap.Sleep. |
go-rpio/rpio.go
Line 245 in f6236e5
I try to use python with gpio in other pins (not only in above pins).
Why limit pins in the code?
Thanks.
The text was updated successfully, but these errors were encountered: