Skip to content

Commit fba5ca2

Browse files
committed
...just kidding. However now it is compiling!! But now quite running
1 parent eb08474 commit fba5ca2

File tree

6 files changed

+144
-77
lines changed

6 files changed

+144
-77
lines changed

examples/mcpwm-simple.rs

Lines changed: 12 additions & 7 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::{Mcpwm, Operator, OperatorConfig};
64+
use esp_idf_hal::mcpwm::{Timer, TimerConfig, Operator, OperatorConfig};
6565
use esp_idf_hal::prelude::Peripherals;
6666
use esp_idf_hal::units::FromValueType;
6767

@@ -72,27 +72,30 @@ fn main() -> anyhow::Result<()> {
7272
let peripherals = Peripherals::take().unwrap();
7373
let timer_config = TimerConfig::default().frequency(25.kHz());
7474
let operator_config = OperatorConfig::default();
75-
let timer = Mcpwm::new(peripherals.mcpwm0.timer, timer_config)?;
75+
let timer = Timer::new(peripherals.mcpwm0.timer0, timer_config);
7676

77-
let timer = timer.into_connection()
77+
let mut timer = timer.into_connection()
7878
.attatch_operator0(
7979
peripherals.mcpwm0.operator0,
8080
operator_config,
8181
peripherals.pins.gpio4,
8282
peripherals.pins.gpio5,
83-
)?;
83+
);
8484

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

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

89-
for duty in (0..timer.get_top_value()).cycle() {
89+
let period_ticks = timer.get_period_peak();
90+
91+
// TODO: Will this work as expected in UP_DOWN counter mode?
92+
for duty in (0..period_ticks).cycle() {
9093
if duty % 100 == 0 {
91-
println!("Duty {}%", x / 100);
94+
println!("Duty {}%", 100 * duty / period_ticks);
9295
}
9396

9497
operator.set_duty_a(duty)?;
95-
operator.set_duty_b(100.0 - duty)?;
98+
operator.set_duty_b(period_ticks - duty)?;
9699
FreeRtos.delay_ms(10)?;
97100
}
98101

@@ -102,4 +105,6 @@ fn main() -> anyhow::Result<()> {
102105
#[cfg(not(any(esp32, esp32s3)))]
103106
fn main() {
104107
esp_idf_sys::link_patches();
108+
109+
println!("Sorry MCPWM is only supported on ESP32 and ESP32-S3");
105110
}

src/mcpwm/mod.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,21 @@ mod timer_connection;
5757

5858
use core::ffi;
5959

60-
use self::{operator::OPERATOR, timer::TIMER};
60+
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+
}
74+
};
6175

6276
// MCPWM clock source frequency for ESP32 and ESP32-s3
6377
const MCPWM_CLOCK_SOURCE_FREQUENCY: u32 = 160_000_000;
@@ -70,9 +84,9 @@ const MAX_PWM_TIMER_PERIOD: u32 = 0x1_00_00;
7084

7185
/// The Motor Control Pulse Width Modulator peripheral
7286
pub struct MCPWM<G: Group> {
73-
timer0: TIMER<0, G>,
74-
timer1: TIMER<1, G>,
75-
timer2: TIMER<2, G>,
87+
pub timer0: TIMER<0, G>,
88+
pub timer1: TIMER<1, G>,
89+
pub timer2: TIMER<2, G>,
7690

7791
pub operator0: OPERATOR<0, G>,
7892
pub operator1: OPERATOR<1, G>,

src/mcpwm/operator.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
use esp_idf_sys::{EspError, mcpwm_oper_handle_t};
1+
use esp_idf_sys::{mcpwm_oper_handle_t, EspError};
22

3-
use crate::{mcpwm::{Group, Group0, Group1}, gpio::OutputPin};
3+
use crate::{gpio::OutputPin, mcpwm::Group};
44

5-
use super::{Duty, timer_connection::OptionalOutputPin};
5+
use super::{timer_connection::OptionalOutputPin, Duty};
66

77
use core::{ffi, marker::PhantomData};
88

9-
pub struct OPERATOR<const N: u8, G: Group>{
9+
pub struct OPERATOR<const N: u8, G: Group> {
1010
_ptr: PhantomData<*const ()>,
11-
_group: PhantomData<G>
11+
_group: PhantomData<G>,
1212
}
1313

1414
impl<const N: u8, G: Group> OPERATOR<N, G> {
@@ -19,7 +19,7 @@ impl<const N: u8, G: Group> OPERATOR<N, G> {
1919
pub unsafe fn new() -> Self {
2020
Self {
2121
_ptr: PhantomData,
22-
_group: PhantomData
22+
_group: PhantomData,
2323
}
2424
}
2525
}
@@ -52,7 +52,6 @@ pub struct Operator<const N: u8, G: Group, PA: OptionalOutputPin, PB: OptionalOu
5252

5353
_pin_a: PA,
5454
_pin_b: PB,
55-
5655
//deadtime: D
5756
}
5857

@@ -67,7 +66,8 @@ where
6766
todo!()
6867
}
6968

70-
/// Set duty as percentage between 0.0 and 100.0 for output A
69+
// TODO: make sure the peak related description is accurate
70+
/// Set duty as in the range 0 to timers peak value
7171
pub fn set_duty_a(&mut self, duty: Duty) -> Result<(), EspError> {
7272
todo!()
7373
}
@@ -96,7 +96,6 @@ pub struct OperatorConfig {
9696
duty_b: Duty,
9797

9898
duty_mode: DutyMode,
99-
10099
//deadtime: Option<DeadtimeConfig>,
101100
}
102101

@@ -136,7 +135,6 @@ impl Default for OperatorConfig {
136135
duty_b: 0,
137136

138137
duty_mode: DutyMode::ActiveHigh,
139-
140138
//deadtime: None,
141139
}
142140
}
@@ -159,12 +157,14 @@ pub enum DutyMode {
159157
}
160158

161159
pub trait OptionalOperator<const N: u8, G: Group> {}
162-
impl<const N: u8, G: Group> OptionalOperator<N, G> for OPERATOR<N, G> {}
160+
impl<const N: u8, G: Group, PA: OptionalOutputPin, PB: OptionalOutputPin> OptionalOperator<N, G>
161+
for Operator<N, G, PA, PB>
162+
{
163+
}
163164

164165
pub struct NoOperator;
165166
impl<const N: u8, G: Group> OptionalOperator<N, G> for NoOperator {}
166167

167-
168168
/*
169169
170170
#[derive(Default, Clone)]
@@ -215,9 +215,9 @@ impl DutyMode {
215215
} else {
216216
duty_config.on_matches_cmp_b.counting_up = GeneratorAction::SetHigh;
217217
}
218-
218+
219219
duty_config
220220
},
221221
}
222222
}
223-
} */
223+
} */

0 commit comments

Comments
 (0)