diff --git a/firmware/Cargo.toml b/firmware/Cargo.toml index 2ab961b..d419a3d 100644 --- a/firmware/Cargo.toml +++ b/firmware/Cargo.toml @@ -30,6 +30,7 @@ bleps = { git = "https://github.com/bjoernQ/bleps", package = "bleps", rev = "a5 "async", ] } esp-alloc = "0.4.0" +fugit = "0.3.7" [[bin]] name = "firmware" diff --git a/firmware/src/main.rs b/firmware/src/main.rs index 67c79af..f5e7282 100644 --- a/firmware/src/main.rs +++ b/firmware/src/main.rs @@ -5,10 +5,13 @@ use embassy_executor::Spawner; use embassy_time::Timer; use esp_backtrace as _; use esp_hal::{ - gpio::{Io, Level, Output}, - timer::timg::TimerGroup, + clock::Clocks, gpio::{Io, Level, Output}, mcpwm::PeripheralClockConfig, timer::timg::TimerGroup }; use esp_println::println; +use fugit::RateExtU32; +use esp_hal::mcpwm::McPwm; +use esp_hal::mcpwm::timer::PwmWorkingMode; +use esp_hal::mcpwm::operator::PwmPinConfig; #[esp_hal_embassy::main] async fn main(_spawner: Spawner) { @@ -22,8 +25,26 @@ async fn main(_spawner: Spawner) { let mut led = Output::new(io.pins.gpio18, Level::Low); // add these - let motor_hi = Output::new(io.pins.gpio13, Level::High); - let motor_lo = Output::new(io.pins.gpio14, Level::Low); + let motor_hi_pin = io.pins.gpio13; + let motor_lo_pin = io.pins.gpio14; + + let clocks = Clocks::get(); + //println!("src clock: {}", clocks.crypto_pwm_clock); + let clock_cfg = PeripheralClockConfig::with_frequency(32.MHz()).unwrap(); //let mut mcpwm = McPwm::new(peripherals.MCPWM0, clock_cfg); + let mut mcpwm = McPwm::new(peripherals.MCPWM0, clock_cfg); + mcpwm.operator0.set_timer(&mcpwm.timer0); + let timer_clock_cfg = clock_cfg.timer_clock_with_frequency(u8::MAX as u16, PwmWorkingMode::Increase, 20.kHz()).unwrap(); + mcpwm.timer0.start(timer_clock_cfg); + + let (mut motor_hi, mut motor_lo) = mcpwm.operator0.with_pins( + motor_hi_pin, + PwmPinConfig::UP_ACTIVE_HIGH, + motor_lo_pin, + PwmPinConfig::UP_ACTIVE_HIGH, + ); + + // motor_hi.set_timestamp(180); + // motor_lo.set_timestamp(0); loop { println!("Hello, World!");