Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
usbalbin committed Oct 13, 2022
1 parent 1a8eea1 commit 03a656e
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 48 deletions.
106 changes: 106 additions & 0 deletions examples/mcpwm-deadtime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// TODO: Update those graphs with measured results

/// # x = 10
///
/// . .
/// . .
/// .------. .------.
/// | | | |
/// pin4 | | | |
/// | | | |
/// ----- -------------------------- --------------------------
/// . .
/// . .
/// .------------------------. .------------------------.
/// | | | |
/// pin5 | | | |
/// | | | |
/// ----- -------- --------
/// . .
///
///
/// # x = 50
/// . .
/// . .
/// .---------------. .---------------.
/// | | | |
/// pin4 | | | |
/// | | | |
/// ----- ----------------- -----------------
/// . .
/// . .
/// .---------------. .---------------.
/// | | | |
/// pin5 | | | |
/// | | | |
/// ----- ----------------- -----------------
/// . .
///
///
/// # x = 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::{DeadtimeConfig, 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(1.kHz()).deadtime(
DeadtimeConfig::ActiveHighComplement {
rising_edge_delay: 1500, // 1500*100ns=150us or 15% of the period
falling_edge_delay: 3000, // 3000*100ns=300us or 30% of the period
},
);
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 {}%", duty);
}

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

unreachable!()
}

#[cfg(not(any(esp32, esp32s3)))]
fn main() {
esp_idf_sys::link_patches();
}
15 changes: 7 additions & 8 deletions examples/mcpwm-simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn main() -> anyhow::Result<()> {
use embedded_hal::delay::blocking::DelayUs;

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

Expand All @@ -74,13 +74,12 @@ fn main() -> anyhow::Result<()> {
let operator_config = OperatorConfig::default();
let timer = Timer::new(peripherals.mcpwm0.timer0, timer_config);

let mut timer = timer.into_connection()
.attatch_operator0(
peripherals.mcpwm0.operator0,
operator_config,
peripherals.pins.gpio4,
peripherals.pins.gpio5,
);
let mut timer = timer.into_connection().attatch_operator0(
peripherals.mcpwm0.operator0,
operator_config,
peripherals.pins.gpio4,
peripherals.pins.gpio5,
);

let (timer, operator, _, _) = timer.split();

Expand Down
24 changes: 7 additions & 17 deletions src/mcpwm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
//! let timer_config = TimerConfig::default().frequency(25.kHz());
//! let operator_config = OperatorConfig::default();
//! let timer = Mcpwm::new(peripherals.mcpwm0.timer, timer_config)?;
//!
//!
//! let timer = timer.into_connection()
//! .attatch_operator0(
//! peripherals.mcpwm0.operator0,
//! operator_config,
//! peripherals.pins.gpio4,
//! peripherals.pins.gpio5,
//! )?;
//!
//!
//! let (timer, operator, _, _) = timer.split();
//!
//! println!("Starting duty-cycle loop");
Expand All @@ -51,26 +51,16 @@
//!
//! See the `examples/` folder of this repository for more.
mod timer;
mod operator;
mod timer;
mod timer_connection;

use core::ffi;

pub use self::{
operator::{
OPERATOR,
Operator,
OperatorConfig,
},
timer::{
TIMER,
Timer,
TimerConfig
},
timer_connection::{
TimerConnection
}
operator::{Operator, OperatorConfig, OPERATOR},
timer::{Timer, TimerConfig, TIMER},
timer_connection::TimerConnection,
};

// MCPWM clock source frequency for ESP32 and ESP32-s3
Expand Down Expand Up @@ -128,4 +118,4 @@ impl Group for Group0 {

impl Group for Group1 {
const ID: ffi::c_int = 1;
}
}
2 changes: 1 addition & 1 deletion src/mcpwm/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ where
}

/// Set duty as in the range 0 to timers peak value
///
///
/// NOTE: The compare value shouldn’t exceed timer’s count peak, otherwise, the compare event will never got triggered.
pub fn set_duty_a(&mut self, duty: Duty) -> Result<(), EspError> {
todo!()
Expand Down
14 changes: 7 additions & 7 deletions src/mcpwm/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ pub struct Timer<const N: u8, G: Group> {
handle: mcpwm_timer_handle_t,
_timer: TIMER<N, G>,
/// Number of ticks within a period
///
///
/// See `Self::get_period_ticks` for more info
period_ticks: u32,

/// This is the maximum value that the comparator will see
///
///
/// See `Self::get_period_peak` for more info
period_peak: u16,
}
Expand Down Expand Up @@ -111,15 +111,15 @@ impl<const N: u8, G: Group> Timer<N, G> {
handle,
_timer: timer,
period_ticks: cfg.period_ticks,
period_peak
period_peak,
}
}

// TODO: make sure this description is accurate
/// Get number of ticks per period
///
///
/// Use this when working with the frequency or the period
///
///
/// NOTE: This will be the same as `Self::get_period_peak` for all `CounterMode` except for
/// `CounterMode::UpDown` where the period will be twice as large as the peak value since
/// the timer will count from zero to peak and then down to zero again
Expand All @@ -129,9 +129,9 @@ impl<const N: u8, G: Group> Timer<N, G> {

// TODO: make sure this description is accurate
/// This is the maximum value that the comparator will see
///
///
/// Use this working with the duty
///
///
/// NOTE: This will not be the same as `Self::get_period_ticks` when using `CounterMode::UpDown`
/// See `Self::get_period_ticks` for more info
pub fn get_period_peak(&self) -> u16 {
Expand Down
36 changes: 23 additions & 13 deletions src/mcpwm/timer_connection.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::mcpwm::Group;

use super::{
operator::{
NoOperator, OperatorConfig, OptionalOperator, OPERATOR,
},
timer::{Timer, TIMER}, Operator,
operator::{NoOperator, OperatorConfig, OptionalOperator, OPERATOR},
timer::{Timer, TIMER},
Operator,
};

// TODO: How do we want fault module to fit into this?
Expand Down Expand Up @@ -37,8 +36,13 @@ impl<const N: u8, G: Group> TimerConnection<N, G, NoOperator, NoOperator, NoOper
//
// Thus we know that after split is called nothing can be added/removed while still having access to
// the individual objects. We also garantuee that the operators wont live longer than the timer
impl<const N: u8, G: Group, O0: OptionalOperator<0, G>, O1: OptionalOperator<1, G>, O2: OptionalOperator<2, G>>
TimerConnection<N, G, O0, O1, O2>
impl<
const N: u8,
G: Group,
O0: OptionalOperator<0, G>,
O1: OptionalOperator<1, G>,
O2: OptionalOperator<2, G>,
> TimerConnection<N, G, O0, O1, O2>
{
pub fn split(&mut self) -> (&mut Timer<N, G>, &mut O0, &mut O1, &mut O2) {
(
Expand All @@ -50,15 +54,17 @@ impl<const N: u8, G: Group, O0: OptionalOperator<0, G>, O1: OptionalOperator<1,
}
}
// TODO: Do something more builder-pattern like for making the operator?
impl<const N: u8, G: Group, O1: OptionalOperator<1, G>, O2: OptionalOperator<2, G>> TimerConnection<N, G, NoOperator, O1, O2> {
impl<const N: u8, G: Group, O1: OptionalOperator<1, G>, O2: OptionalOperator<2, G>>
TimerConnection<N, G, NoOperator, O1, O2>
{
pub fn attatch_operator0<PA: OptionalOutputPin, PB: OptionalOutputPin>(
self,
operator_handle: OPERATOR<0, G>,
operator_cfg: OperatorConfig,
pin_a: PA,
pin_b: PB,
) -> TimerConnection<N, G, Operator<0, G, PA, PB>, O1, O2> {
let operator = todo!();//self.init_and_attach_operator(operator_cfg, pin_a, pin_b);
let operator = todo!(); //self.init_and_attach_operator(operator_cfg, pin_a, pin_b);
TimerConnection {
timer: self.timer,
operator0: operator,
Expand All @@ -68,15 +74,17 @@ impl<const N: u8, G: Group, O1: OptionalOperator<1, G>, O2: OptionalOperator<2,
}
}

impl<const N: u8, G: Group, O0: OptionalOperator<0, G>, O2: OptionalOperator<2, G>> TimerConnection<N, G, O0, NoOperator, O2> {
impl<const N: u8, G: Group, O0: OptionalOperator<0, G>, O2: OptionalOperator<2, G>>
TimerConnection<N, G, O0, NoOperator, O2>
{
pub fn attatch_operator1<PA: OptionalOutputPin, PB: OptionalOutputPin>(
self,
operator_handle: OPERATOR<1, G>,
operator_cfg: OperatorConfig,
pin_a: PA,
pin_b: PB,
) -> TimerConnection<N, G, O0, Operator<1, G, PA, PB>, O2> {
let operator = todo!();//self.init_and_attach_operator(operator_cfg, pin_a, pin_b);
let operator = todo!(); //self.init_and_attach_operator(operator_cfg, pin_a, pin_b);
TimerConnection {
timer: self.timer,
operator0: self.operator0,
Expand All @@ -86,15 +94,17 @@ impl<const N: u8, G: Group, O0: OptionalOperator<0, G>, O2: OptionalOperator<2,
}
}

impl<const N: u8, G: Group, O0: OptionalOperator<0, G>, O1: OptionalOperator<1, G>> TimerConnection<N, G, O0, O1, NoOperator> {
impl<const N: u8, G: Group, O0: OptionalOperator<0, G>, O1: OptionalOperator<1, G>>
TimerConnection<N, G, O0, O1, NoOperator>
{
pub fn attatch_operator2<PA: OptionalOutputPin, PB: OptionalOutputPin>(
self,
operator_handle: OPERATOR<2, G>,
operator_cfg: OperatorConfig,
pin_a: PA,
pin_b: PB,
) -> TimerConnection<N, G, O0, O1, Operator<2, G, PA, PB>> {
let operator = todo!();//self.init_and_attach_operator(operator_cfg, pin_a, pin_b);
let operator = todo!(); //self.init_and_attach_operator(operator_cfg, pin_a, pin_b);
TimerConnection {
timer: self.timer,
operator0: self.operator0,
Expand All @@ -108,4 +118,4 @@ pub struct NoPin;

pub trait OptionalOutputPin {}

impl<P: crate::gpio::OutputPin> OptionalOutputPin for P {}
impl<P: crate::gpio::OutputPin> OptionalOutputPin for P {}
4 changes: 2 additions & 2 deletions src/peripherals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ pub struct Peripherals {
#[cfg(not(feature = "riscv-ulp-hal"))]
pub ledc: ledc::LEDC,
#[cfg(all(any(esp32, esp32s3), not(feature = "riscv-ulp-hal")))]
pub mcpwm0: mcpwm::MCPWM::<mcpwm::Group0>,
pub mcpwm0: mcpwm::MCPWM<mcpwm::Group0>,
#[cfg(all(any(esp32, esp32s3), not(feature = "riscv-ulp-hal")))]
pub mcpwm1: mcpwm::MCPWM::<mcpwm::Group1>,
pub mcpwm1: mcpwm::MCPWM<mcpwm::Group1>,
#[cfg(not(feature = "riscv-ulp-hal"))]
pub rmt: rmt::RMT,
#[cfg(all(
Expand Down

0 comments on commit 03a656e

Please sign in to comment.