Skip to content

Alternative for #176 #177

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

Closed
wants to merge 2 commits into from
Closed

Alternative for #176 #177

wants to merge 2 commits into from

Conversation

burrbull
Copy link
Member

No description provided.

Copy link
Member

@TheZoq2 TheZoq2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from the comments I left, I like the implementation here.

As discussed in #176, we may want to discuss using a different time unit than Hertz.

src/pwm.rs Outdated
Some(Channel::C2) => unsafe { (*$TIMX::ptr()).ccr2.read().ccr().bits() },
Some(Channel::C3) => unsafe { (*$TIMX::ptr()).ccr3.read().ccr().bits() },
Some(Channel::C4) => unsafe { (*$TIMX::ptr()).ccr4.read().ccr().bits() },
None => 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is 0 always equivalent to a disabled channel? Or can the channel be in some sort of floating state if disabled?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about to panic here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would really like to avoid panics as far as possible. I don't see a great way to accomplish that though :/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By experience, disable is different to duty of 0

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it should be OK to give the duty whatever it is enabled or disabled, according to enabling the channel would give you this duty.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that seems like the best solution

src/pwm.rs Outdated
Some(Channel::C2) => unsafe { (*$TIMX::ptr()).ccr2.write(|w| w.ccr().bits(duty)) },
Some(Channel::C3) => unsafe { (*$TIMX::ptr()).ccr3.write(|w| w.ccr().bits(duty)) },
Some(Channel::C4) => unsafe { (*$TIMX::ptr()).ccr4.write(|w| w.ccr().bits(duty)) },
None => {},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this code:

pwm.disable(C1);
pwm.set_duty(C1, 0.5);
pwm.enable(C1);

I would expect it to get the updated duty after being enabled, but this seems to not be the case here. As far as I can tell,

match channel {
    Channel::C1 => unsafe{ /*ccr1.write...*/ }
    Channel::C2 => unsafe{ /*ccr2.write...*/ }
    Channel::C3 => unsafe{ /*ccr3.write...*/ }
    Channel::C4 => unsafe{ /*ccr3.write...*/ }
}

would result in that behaviour

src/pwm.rs Outdated

// Length in ms of an internal clock pulse
(clk.0 / u32(psc * arr)).hz()
// (((psc as u32) / clk.0) * (1_000_000 as u32) * (arr as u32)).ms()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be removed

src/pwm.rs Outdated
T: Into<Self::Time> {
let clk = self.clk;

// let freq = u16(1 / period.into());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be removed as well

@burrbull
Copy link
Member Author

I've renamed function to check_used and added panic.
Now you cannot enable/disable channel if it is not used (pin not specified).

Comment on lines +85 to +94
fn check_used(c: Channel) -> Channel {
if (c == Channel::C1 && Self::C1)
|| (c == Channel::C2 && Self::C2)
|| (c == Channel::C3 && Self::C3)
|| (c == Channel::C4 && Self::C4)
{
c
} else {
panic!("Unused channel")
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheZoq2 @TeXitoi
In fact this code must be optimized by compiler. So no panic will be in code if user specify right channel.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally I want to avoid panics if the user specifies the wrong channel as well :) But I don't see a great way to do that at the moment

Copy link
Contributor

@TeXitoi TeXitoi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK for me.

I'm not a fan of this check_used(), but that's not a blocker

//// Operations affecting all defined channels on the Timer

// Adjust period to 0.5 seconds
pwm.set_period(500.us());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still the error ;-)

@justacec
Copy link
Contributor

I just integrated this into my branch

@burrbull
Copy link
Member Author

I just integrated this into my branch

Ok. I close PR, but branch will be available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants