-
Notifications
You must be signed in to change notification settings - Fork 840
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
stm32: Add the ability to center-align timers #1991
Conversation
I would prefer separate parameters, since you need the edge/center aligned mode for frequency calculation. I also have a toy project, that requires center aligned mode. In edge aligned mode a cycle corresponds to the reload value, while in center aligned mode each cycle counts up and down, i.e. you need to half the reload value for the same frequency. I chose a generic parameter that contains the configuration and frequency multiplier as const to configure center vs. edge aligned mode which avoids storage and runtime costs. Disclaimer: I haven't run any unit tests nor have I fully understood all the trait dependencies, and so my solution might also be invalid. Usage: let mut buzzer_pwm = simple_pwm::SimplePwm::<'_, _, simple_pwm::CmsCenterAlignedMode1>::new(
p.TIM1,
Some(PwmPin::new_ch1(p.PE9, OutputType::PushPull)),
None,
Some(PwmPin::new_ch3(p.PE13, OutputType::PushPull)),
None,
Hertz::hz(3300),
); |
Ah good point, yeah it changes the frequency of the PWM. The frequency of the timer clock remains the same. This feels like something the PWM should fix, not the underlying timer. I like your solution too with it being a typestate param. Though it makes the |
Yes, we should avoid typestates if the feature can be implemented with plain old enums. |
I am also fine with enums. Just wondering, which is the right approach to include the frequency factor.
If the frequency is only set initially, the latter approach is a good solution, as the configuration is already known. |
Yeah, do we sacrifice a byte of ram or a runtime memory read? Although thinking about it, reading the enum from ram is pretty much the same as reading it from a register. So I guess keeping a ZST is still fine. Btw, personally, I'm changing the frequency multiple times per second :P |
Hmmm, what I dislike is that the basic timer doesn't have a center-aligned mode, but the frequency function is on the basic timer trait and so is not able account for the alignment. And Rust doesn't give us the ability to override a default fn with another one. So the frequency as defined on the trait will always be the frequency from one extreme to the other extreme, instead of the frequency from 0 to 0. That might be fine idk... Maybe putting a doc comment on the function explaining this is enough? |
8462ee7
to
a9dc887
Compare
Confirmed working on my STM32G0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!
Draft because I still need to test it on hardware.
Added it as a nice enum type in place of just the direction.
This is the most straightforward implementation I think.
The reason for adding this is because it enables more fancy PWM usages using the existing simple and complementary pwm implementations.
I've made a variant for each of the interrupt settings for the center-aligned mode. Should that be separated? Or is this combination fine?