-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
docs/machine: Specify new class machine.PWM. #4237
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
Conversation
CircuitPython API is here: https://circuitpython.readthedocs.io/en/latest/shared-bindings/pulseio/PWMOut.html. I don't expect you'll copy it but we provide similar functionality. One thing we include in the constructor is whether the frequency will vary or not. On SAMD chips (and maybe others) we can use multiple output channels of a single timer as long as they share the same frequency. We can vary the duty cycle but not the frequency per channel. So we can create more Interestingly the nRF52 has separate PWM peripherals from its timers. |
Thanks, I missed that (found analogOut and just assumed that was PWM). Since CircuitPython uses properties it makes it hard to copy the class verbatim. But copying the functionality to use methods would give something like: pwm = PWM(pin, *, frequency, duty_cycle, variable_frequency)
pwm.frequency([value]) # get or set current freq in Hz
pwm.duty_cycle([value]) # get or set current duty cycle as ratio out of 65535 That's simple. But I think it's important to be able to set the period as well as frequency (eg what about 1.5Hz?), and for things like servo motors to be able to specify the duty as an absolute time (eg microseconds) not just a fraction of the cycle period. |
Hi, Assuming that the source clock is 19.2 MHz,
sets the clock prescaler to 20.0 and results 1 KHz 50% duty.
Raspberry Pi PWM hardware has:
The API seems fairly good for me but in some cases I may want to set the prescaler value and additional parameters explicitly. How should I design the |
Some MCUs have more than one "hardware unit" that can generate PWM on any given pin. The idea of the
In general such parameters should be additional keywords to the init method and/or constructor. |
Ah, I see. I could pass a timer object as block arg, for example, to specify which timer to be used for generating PWM signal ( in the case of STM32).
OK, I'll use kw arguments for PWM optional parameters and add a clock manager class for specifying clock-related settings (clock source, prescaler, etc.). Then another question: |
It's the latter: the idea of But keep in mind that this here is just a proposal, it's not yet concrete! Probably it will change. |
Thanks, now I can start implementing my PWM class. I got that this is not final, but anyway I need some interface specification and this is a very good start point. |
I think specifying freq AND period WITHOUT tick_hz could be useful. |
I have implemented the PWM class for Raspberry Pi, and I think tick_hz parameter may not be necessary (if the API will not include |
Signed-off-by: Damien George <damien@micropython.org>
acaa9a6
to
2f61244
Compare
Signed-off-by: Damien George <damien@micropython.org>
I've updated this to simplify it, to remove the |
This looks good as long as STM32 implementation allows |
This adds an initial specification of the machine.PWM class, to provide a way to generate PWM output that is portable across the different ports. Such functionality may already be available in one way or another (eg through a Timer object), but because configuring PWM via a Timer is very port-specific, and because it's a common thing to do, it's beneficial to have a top-level construct for it. The specification in this commit aims to provide core functionality in a minimal way. It also somewhat matches most existing ad-hoc implementations of machine.PWM. See discussion in #2283 and #4237. Signed-off-by: Damien George <damien@micropython.org>
This was squashed and merged in 9e1b25a |
There is currently no |
This adds an initial specification of the machine.PWM class, to provide a way to generate PWM output that is portable across the different ports. Such functionality may already be available in one way or another (eg through a Timer object), but because configuring PWM via a Timer is very port-specific, and because it's a common thing to do, it's beneficial to have a top-level construct for it. The specification in this commit aims to provide core functionality in a minimal way. It also somewhat matches most existing ad-hoc implementations of machine.PWM. See discussion in micropython#2283 and micropython#4237. Signed-off-by: Damien George <damien@micropython.org>
As discussed previously in #2283, it would be good to have a simple PWM class in the machine module to allow for generating PWM output in a way that is portable across the different ports. Such functionality may already be available in one way or another (eg through a Timer object), but because configuring PWM via a Timer is very port-specific, and because it's a common thing to do, it's beneficial to have a top-level construct for it.
The proposal here aims to provide all required functionality in a minimal way. Some points about it:
pwm.init(freq=..., duty_u16=...)
; counter to this, there's no way to just change the frequency/period.It might be interesting to compare this with the proposal for ADC, #4213, because in a way they are related. With the PWM class I didn't go the route of adding a PWMBlock for further configuration, but instead showed an alternative to this, by having a
block
parameter to the constructor which would allow to have finer control over which underlying hardware generator was used for the PWM object.