Skip to content

Commit 03a656e

Browse files
committed
fmt
1 parent 1a8eea1 commit 03a656e

File tree

7 files changed

+153
-48
lines changed

7 files changed

+153
-48
lines changed

examples/mcpwm-deadtime.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// TODO: Update those graphs with measured results
2+
3+
/// # x = 10
4+
///
5+
/// . .
6+
/// . .
7+
/// .------. .------.
8+
/// | | | |
9+
/// pin4 | | | |
10+
/// | | | |
11+
/// ----- -------------------------- --------------------------
12+
/// . .
13+
/// . .
14+
/// .------------------------. .------------------------.
15+
/// | | | |
16+
/// pin5 | | | |
17+
/// | | | |
18+
/// ----- -------- --------
19+
/// . .
20+
///
21+
///
22+
/// # x = 50
23+
/// . .
24+
/// . .
25+
/// .---------------. .---------------.
26+
/// | | | |
27+
/// pin4 | | | |
28+
/// | | | |
29+
/// ----- ----------------- -----------------
30+
/// . .
31+
/// . .
32+
/// .---------------. .---------------.
33+
/// | | | |
34+
/// pin5 | | | |
35+
/// | | | |
36+
/// ----- ----------------- -----------------
37+
/// . .
38+
///
39+
///
40+
/// # x = 90
41+
/// . .
42+
/// . .
43+
/// .------------------------. .------------------------.
44+
/// | | | |
45+
/// pin4 | | | |
46+
/// | | | |
47+
/// ----- -------- --------
48+
/// . .
49+
/// . .
50+
/// .------. .------.
51+
/// | | | |
52+
/// pin5 | | | |
53+
/// | | | |
54+
/// ----- -------------------------- --------------------------
55+
/// . .
56+
57+
#[cfg(any(esp32, esp32s3))]
58+
fn main() -> anyhow::Result<()> {
59+
use embedded_hal::delay::blocking::DelayUs;
60+
61+
use esp_idf_hal::delay::FreeRtos;
62+
use esp_idf_hal::mcpwm::{DeadtimeConfig, Mcpwm, Operator, OperatorConfig};
63+
use esp_idf_hal::prelude::Peripherals;
64+
use esp_idf_hal::units::FromValueType;
65+
66+
esp_idf_sys::link_patches();
67+
68+
println!("Configuring MCPWM");
69+
70+
let peripherals = Peripherals::take().unwrap();
71+
let config = OperatorConfig::default().frequency(1.kHz()).deadtime(
72+
DeadtimeConfig::ActiveHighComplement {
73+
rising_edge_delay: 1500, // 1500*100ns=150us or 15% of the period
74+
falling_edge_delay: 3000, // 3000*100ns=300us or 30% of the period
75+
},
76+
);
77+
let mcpwm = Mcpwm::new(peripherals.mcpwm0.mcpwm)?;
78+
let mut operator = Operator::new(
79+
peripherals.mcpwm0.operator0,
80+
&mcpwm,
81+
&config,
82+
peripherals.pins.gpio4,
83+
peripherals.pins.gpio5,
84+
)?;
85+
86+
println!("Starting duty-cycle loop");
87+
88+
for x in (0..10000u16).cycle() {
89+
let duty = f32::from(x) * 0.01;
90+
91+
if x % 100 == 0 {
92+
println!("Duty {}%", duty);
93+
}
94+
95+
operator.set_duty_a(duty)?;
96+
operator.set_duty_b(0.0)?;
97+
FreeRtos.delay_ms(10)?;
98+
}
99+
100+
unreachable!()
101+
}
102+
103+
#[cfg(not(any(esp32, esp32s3)))]
104+
fn main() {
105+
esp_idf_sys::link_patches();
106+
}

examples/mcpwm-simple.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fn main() -> anyhow::Result<()> {
6161
use embedded_hal::delay::blocking::DelayUs;
6262

6363
use esp_idf_hal::delay::FreeRtos;
64-
use esp_idf_hal::mcpwm::{Timer, TimerConfig, Operator, OperatorConfig};
64+
use esp_idf_hal::mcpwm::{Operator, OperatorConfig, Timer, TimerConfig};
6565
use esp_idf_hal::prelude::Peripherals;
6666
use esp_idf_hal::units::FromValueType;
6767

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

77-
let mut timer = timer.into_connection()
78-
.attatch_operator0(
79-
peripherals.mcpwm0.operator0,
80-
operator_config,
81-
peripherals.pins.gpio4,
82-
peripherals.pins.gpio5,
83-
);
77+
let mut timer = timer.into_connection().attatch_operator0(
78+
peripherals.mcpwm0.operator0,
79+
operator_config,
80+
peripherals.pins.gpio4,
81+
peripherals.pins.gpio5,
82+
);
8483

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

src/mcpwm/mod.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@
2828
//! let timer_config = TimerConfig::default().frequency(25.kHz());
2929
//! let operator_config = OperatorConfig::default();
3030
//! let timer = Mcpwm::new(peripherals.mcpwm0.timer, timer_config)?;
31-
//!
31+
//!
3232
//! let timer = timer.into_connection()
3333
//! .attatch_operator0(
3434
//! peripherals.mcpwm0.operator0,
3535
//! operator_config,
3636
//! peripherals.pins.gpio4,
3737
//! peripherals.pins.gpio5,
3838
//! )?;
39-
//!
39+
//!
4040
//! let (timer, operator, _, _) = timer.split();
4141
//!
4242
//! println!("Starting duty-cycle loop");
@@ -51,26 +51,16 @@
5151
//!
5252
//! See the `examples/` folder of this repository for more.
5353
54-
mod timer;
5554
mod operator;
55+
mod timer;
5656
mod timer_connection;
5757

5858
use core::ffi;
5959

6060
pub use self::{
61-
operator::{
62-
OPERATOR,
63-
Operator,
64-
OperatorConfig,
65-
},
66-
timer::{
67-
TIMER,
68-
Timer,
69-
TimerConfig
70-
},
71-
timer_connection::{
72-
TimerConnection
73-
}
61+
operator::{Operator, OperatorConfig, OPERATOR},
62+
timer::{Timer, TimerConfig, TIMER},
63+
timer_connection::TimerConnection,
7464
};
7565

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

129119
impl Group for Group1 {
130120
const ID: ffi::c_int = 1;
131-
}
121+
}

src/mcpwm/operator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ where
6767
}
6868

6969
/// Set duty as in the range 0 to timers peak value
70-
///
70+
///
7171
/// NOTE: The compare value shouldn’t exceed timer’s count peak, otherwise, the compare event will never got triggered.
7272
pub fn set_duty_a(&mut self, duty: Duty) -> Result<(), EspError> {
7373
todo!()

src/mcpwm/timer.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ pub struct Timer<const N: u8, G: Group> {
7070
handle: mcpwm_timer_handle_t,
7171
_timer: TIMER<N, G>,
7272
/// Number of ticks within a period
73-
///
73+
///
7474
/// See `Self::get_period_ticks` for more info
7575
period_ticks: u32,
7676

7777
/// This is the maximum value that the comparator will see
78-
///
78+
///
7979
/// See `Self::get_period_peak` for more info
8080
period_peak: u16,
8181
}
@@ -111,15 +111,15 @@ impl<const N: u8, G: Group> Timer<N, G> {
111111
handle,
112112
_timer: timer,
113113
period_ticks: cfg.period_ticks,
114-
period_peak
114+
period_peak,
115115
}
116116
}
117117

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

130130
// TODO: make sure this description is accurate
131131
/// This is the maximum value that the comparator will see
132-
///
132+
///
133133
/// Use this working with the duty
134-
///
134+
///
135135
/// NOTE: This will not be the same as `Self::get_period_ticks` when using `CounterMode::UpDown`
136136
/// See `Self::get_period_ticks` for more info
137137
pub fn get_period_peak(&self) -> u16 {

src/mcpwm/timer_connection.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use crate::mcpwm::Group;
22

33
use super::{
4-
operator::{
5-
NoOperator, OperatorConfig, OptionalOperator, OPERATOR,
6-
},
7-
timer::{Timer, TIMER}, Operator,
4+
operator::{NoOperator, OperatorConfig, OptionalOperator, OPERATOR},
5+
timer::{Timer, TIMER},
6+
Operator,
87
};
98

109
// TODO: How do we want fault module to fit into this?
@@ -37,8 +36,13 @@ impl<const N: u8, G: Group> TimerConnection<N, G, NoOperator, NoOperator, NoOper
3736
//
3837
// Thus we know that after split is called nothing can be added/removed while still having access to
3938
// the individual objects. We also garantuee that the operators wont live longer than the timer
40-
impl<const N: u8, G: Group, O0: OptionalOperator<0, G>, O1: OptionalOperator<1, G>, O2: OptionalOperator<2, G>>
41-
TimerConnection<N, G, O0, O1, O2>
39+
impl<
40+
const N: u8,
41+
G: Group,
42+
O0: OptionalOperator<0, G>,
43+
O1: OptionalOperator<1, G>,
44+
O2: OptionalOperator<2, G>,
45+
> TimerConnection<N, G, O0, O1, O2>
4246
{
4347
pub fn split(&mut self) -> (&mut Timer<N, G>, &mut O0, &mut O1, &mut O2) {
4448
(
@@ -50,15 +54,17 @@ impl<const N: u8, G: Group, O0: OptionalOperator<0, G>, O1: OptionalOperator<1,
5054
}
5155
}
5256
// TODO: Do something more builder-pattern like for making the operator?
53-
impl<const N: u8, G: Group, O1: OptionalOperator<1, G>, O2: OptionalOperator<2, G>> TimerConnection<N, G, NoOperator, O1, O2> {
57+
impl<const N: u8, G: Group, O1: OptionalOperator<1, G>, O2: OptionalOperator<2, G>>
58+
TimerConnection<N, G, NoOperator, O1, O2>
59+
{
5460
pub fn attatch_operator0<PA: OptionalOutputPin, PB: OptionalOutputPin>(
5561
self,
5662
operator_handle: OPERATOR<0, G>,
5763
operator_cfg: OperatorConfig,
5864
pin_a: PA,
5965
pin_b: PB,
6066
) -> TimerConnection<N, G, Operator<0, G, PA, PB>, O1, O2> {
61-
let operator = todo!();//self.init_and_attach_operator(operator_cfg, pin_a, pin_b);
67+
let operator = todo!(); //self.init_and_attach_operator(operator_cfg, pin_a, pin_b);
6268
TimerConnection {
6369
timer: self.timer,
6470
operator0: operator,
@@ -68,15 +74,17 @@ impl<const N: u8, G: Group, O1: OptionalOperator<1, G>, O2: OptionalOperator<2,
6874
}
6975
}
7076

71-
impl<const N: u8, G: Group, O0: OptionalOperator<0, G>, O2: OptionalOperator<2, G>> TimerConnection<N, G, O0, NoOperator, O2> {
77+
impl<const N: u8, G: Group, O0: OptionalOperator<0, G>, O2: OptionalOperator<2, G>>
78+
TimerConnection<N, G, O0, NoOperator, O2>
79+
{
7280
pub fn attatch_operator1<PA: OptionalOutputPin, PB: OptionalOutputPin>(
7381
self,
7482
operator_handle: OPERATOR<1, G>,
7583
operator_cfg: OperatorConfig,
7684
pin_a: PA,
7785
pin_b: PB,
7886
) -> TimerConnection<N, G, O0, Operator<1, G, PA, PB>, O2> {
79-
let operator = todo!();//self.init_and_attach_operator(operator_cfg, pin_a, pin_b);
87+
let operator = todo!(); //self.init_and_attach_operator(operator_cfg, pin_a, pin_b);
8088
TimerConnection {
8189
timer: self.timer,
8290
operator0: self.operator0,
@@ -86,15 +94,17 @@ impl<const N: u8, G: Group, O0: OptionalOperator<0, G>, O2: OptionalOperator<2,
8694
}
8795
}
8896

89-
impl<const N: u8, G: Group, O0: OptionalOperator<0, G>, O1: OptionalOperator<1, G>> TimerConnection<N, G, O0, O1, NoOperator> {
97+
impl<const N: u8, G: Group, O0: OptionalOperator<0, G>, O1: OptionalOperator<1, G>>
98+
TimerConnection<N, G, O0, O1, NoOperator>
99+
{
90100
pub fn attatch_operator2<PA: OptionalOutputPin, PB: OptionalOutputPin>(
91101
self,
92102
operator_handle: OPERATOR<2, G>,
93103
operator_cfg: OperatorConfig,
94104
pin_a: PA,
95105
pin_b: PB,
96106
) -> TimerConnection<N, G, O0, O1, Operator<2, G, PA, PB>> {
97-
let operator = todo!();//self.init_and_attach_operator(operator_cfg, pin_a, pin_b);
107+
let operator = todo!(); //self.init_and_attach_operator(operator_cfg, pin_a, pin_b);
98108
TimerConnection {
99109
timer: self.timer,
100110
operator0: self.operator0,
@@ -108,4 +118,4 @@ pub struct NoPin;
108118

109119
pub trait OptionalOutputPin {}
110120

111-
impl<P: crate::gpio::OutputPin> OptionalOutputPin for P {}
121+
impl<P: crate::gpio::OutputPin> OptionalOutputPin for P {}

src/peripherals.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ pub struct Peripherals {
5757
#[cfg(not(feature = "riscv-ulp-hal"))]
5858
pub ledc: ledc::LEDC,
5959
#[cfg(all(any(esp32, esp32s3), not(feature = "riscv-ulp-hal")))]
60-
pub mcpwm0: mcpwm::MCPWM::<mcpwm::Group0>,
60+
pub mcpwm0: mcpwm::MCPWM<mcpwm::Group0>,
6161
#[cfg(all(any(esp32, esp32s3), not(feature = "riscv-ulp-hal")))]
62-
pub mcpwm1: mcpwm::MCPWM::<mcpwm::Group1>,
62+
pub mcpwm1: mcpwm::MCPWM<mcpwm::Group1>,
6363
#[cfg(not(feature = "riscv-ulp-hal"))]
6464
pub rmt: rmt::RMT,
6565
#[cfg(all(

0 commit comments

Comments
 (0)