Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
103 changes: 103 additions & 0 deletions examples/mcpwm-simple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/// Simple example showing how MCPWM may be used to generate two synchronized pwm signals with varying duty.
/// The duty on the pin4 will increase from 0% all the way up to 100% and repeat. At the same time the duty
/// on pin 5 will go from 100% down to 0%
///
/// # duty = 10
///
/// . .
/// . .
/// .------. .------.
/// | | | |
/// pin4 | | | |
/// | | | |
/// ----- -------------------------- --------------------------
/// . .
/// . .
/// .------------------------. .------------------------.
/// | | | |
/// pin5 | | | |
/// | | | |
/// ----- -------- --------
/// . .
///
///
/// # duty = 50
/// . .
/// . .
/// .---------------. .---------------.
/// | | | |
/// pin4 | | | |
/// | | | |
/// ----- ----------------- -----------------
/// . .
/// . .
/// .---------------. .---------------.
/// | | | |
/// pin5 | | | |
/// | | | |
/// ----- ----------------- -----------------
/// . .
///
///
/// # duty = 90
/// . .
/// . .
/// .------------------------. .------------------------.
/// | | | |
/// pin4 | | | |
/// | | | |
/// ----- -------- --------
/// . .
/// . .
/// .------. .------.
/// | | | |
/// pin5 | | | |
/// | | | |
/// ----- -------------------------- --------------------------
/// . .

#[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());
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 x in (0..10000u16).cycle() {
let duty = f32::from(x) * 0.01;

if x % 100 == 0 {
println!("Duty {}%", x / 100);
}

operator.set_duty_a(duty)?;
operator.set_duty_b(100.0 - duty)?;
FreeRtos.delay_ms(10)?;
}

unreachable!()
}

#[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 @@ -42,6 +42,8 @@ pub mod ledc;
not(feature = "riscv-ulp-hal")
))]
pub mod mac;
#[cfg(all(any(esp32, esp32s3), not(feature = "riscv-ulp-hal")))]
pub mod mcpwm;
#[cfg(not(feature = "riscv-ulp-hal"))]
pub mod modem;
pub mod peripheral;
Expand Down
Loading