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

Motor Control Pulse Width Modulator (MCPWM) #93

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions examples/mcpwm-simple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#[cfg(any(esp32, esp32s3))]
fn main() -> anyhow::Result<()> {
use embedded_hal::delay::blocking::DelayUs;

use esp_idf_hal::delay::FreeRtos;
use esp_idf_hal::mcpwm::{Mcpwm, Operator, OperatorConfig};
use esp_idf_hal::prelude::Peripherals;
use esp_idf_hal::units::FromValueType;

esp_idf_sys::link_patches();

println!("Configuring MCPWM");

let peripherals = Peripherals::take().unwrap();
let config = OperatorConfig::default().frequency(25.kHz().into());
let mcpwm = Mcpwm::new(peripherals.mcpwm0.mcpwm)?;
let mut operator = Operator::new(
peripherals.mcpwm0.operator0,
&mcpwm,
&config,
peripherals.pins.gpio4,
peripherals.pins.gpio5,
)?;

println!("Starting duty-cycle loop");

for &duty in [0.0, 20.0, 40.0, 60.0, 80.0, 100.0].iter().cycle() {
println!("Duty {}%", duty);
operator.set_duty_a(duty)?;
operator.set_duty_b(100.0 - duty)?;
FreeRtos.delay_ms(2000)?;
}

loop {
FreeRtos.delay_ms(1000)?;
}
}

#[cfg(not(any(esp32, esp32s3)))]
fn main() {
esp_idf_sys::link_patches();
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub mod i2c;
#[cfg(not(feature = "riscv-ulp-hal"))]
pub mod interrupt;
pub mod ledc;
#[cfg(all(any(esp32, esp32s3), not(feature = "riscv-ulp-hal")))]
pub mod mcpwm;
#[cfg(not(feature = "riscv-ulp-hal"))]
pub mod mutex;
pub mod peripherals;
Expand Down
Loading