Skip to content
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

Need to change PWM frequency of LedcDriver #294

Closed
Angelo13C opened this issue Aug 28, 2023 · 1 comment
Closed

Need to change PWM frequency of LedcDriver #294

Angelo13C opened this issue Aug 28, 2023 · 1 comment

Comments

@Angelo13C
Copy link

Hi!

I need to change the PWM frequency of LedcDriver after it has been created because the TMC2209 motor driver needs a clock signal on the STEP pin that moves the motor one step for each rising edge.

The problem now is that after creating a LedcDriver there's no way to change its frequency so there's no way to change the speed of the motor.

I thought about adding this:

/// Set the frequency of the timer.
pub fn set_frequency(&mut self, frequency: Hertz) -> Result<(), EspError> {
    esp!(unsafe {
        ledc_set_freq(self.speed_mode.into(), self.timer.into(), frequency.into())
    })?;
    Ok(())
}

in LedcDriver but I don't know if it's safe

@Vollbrecht
Copy link
Collaborator

The LEDC hardware is somewhat limited if you want to change the frequency at runtime. That is duo to the fact that each of the 4 timers is shared with multiple channels. So if you are using timer 0 and channel 0 and 1, you will change the frequency of both channels 0 and 1 at the same time because they are bound to timer 0. So what they recommend is :

  • live with that limitation and call it anyway
  • swap out the underlying timer with one that is setup with another frequency to the currently used channel
  • swap out the channel itself that is using another timer

We don't implement the swapping the channels currently in the wrapper, if i am not mistaken, so you would need to go with method one currently.

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

No branches or pull requests

2 participants