Skip to content

Commit cc0e98e

Browse files
committed
Almost compiling
1 parent ff2624b commit cc0e98e

File tree

6 files changed

+191
-148
lines changed

6 files changed

+191
-148
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ heapless = "0.7"
3333
embassy-sync = { version = "0.1", optional = true, git = "https://github.com/ivmarkov/embassy" }
3434
embassy-time = { version = "0.1", optional = true, features = ["tick-hz-1_000_000"], git = "https://github.com/ivmarkov/embassy" }
3535
edge-executor = { version = "0.2", optional = true, default-features = false }
36+
libc = "0.2.134"
37+
3638

3739
[build-dependencies]
3840
embuild = "0.30.3"

src/mcpwm/mod.rs

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,18 @@ mod timer;
5555
mod operator;
5656
mod timer_connection;
5757

58-
use core::borrow::Borrow;
58+
use core::ffi;
5959

60-
use crate::gpio::OutputPin;
61-
use crate::units::{FromValueType, Hertz};
62-
use crate::mcpwm::operator::{HwOperator, OPERATOR0, OPERATOR1, OPERATOR2};
63-
use esp_idf_sys::*;
60+
use crate::mcpwm::operator::{
61+
HwOperator0, HwOperator1, HwOperator2,
62+
OPERATOR00, OPERATOR01, OPERATOR02,
63+
OPERATOR10, OPERATOR11, OPERATOR12
64+
};
65+
66+
use crate::mcpwm::timer::{
67+
TIMER00, TIMER01, TIMER02,
68+
TIMER10, TIMER11, TIMER12
69+
};
6470

6571
// MCPWM clock source frequency for ESP32 and ESP32-s3
6672
const MCPWM_CLOCK_SOURCE_FREQUENCY: u32 = 160_000_000;
@@ -72,58 +78,75 @@ const MAX_PWM_TIMER_PRESCALE: u32 = 0x1_00;
7278
const MAX_PWM_TIMER_PERIOD: u32 = 0x1_00_00;
7379

7480
/// The Motor Control Pulse Width Modulator peripheral
75-
pub struct Peripheral<U: Unit> {
76-
pub mcpwm: MCPWM<U>,
77-
pub operator0: OPERATOR0<U>,
78-
pub operator1: OPERATOR1<U>,
79-
pub operator2: OPERATOR2<U>,
81+
pub struct MCPWM0 {
82+
timer0: TIMER00,
83+
timer1: TIMER01,
84+
timer2: TIMER02,
85+
86+
pub operator0: OPERATOR00,
87+
pub operator1: OPERATOR01,
88+
pub operator2: OPERATOR02,
8089
}
8190

82-
impl<U: Unit> Peripheral<U> {
91+
impl MCPWM0 {
8392
/// # Safety
8493
///
85-
/// It is safe to instantiate this exactly one time per `Unit`.
94+
/// It is safe to instantiate this exactly one time per `Group`.
8695
pub unsafe fn new() -> Self {
8796
Self {
88-
mcpwm: MCPWM::new(),
89-
operator0: OPERATOR0::new(),
90-
operator1: OPERATOR1::new(),
91-
operator2: OPERATOR2::new(),
97+
timer0: TIMER00::new(),
98+
timer1: TIMER01::new(),
99+
timer2: TIMER02::new(),
100+
operator0: OPERATOR00::new(),
101+
operator1: OPERATOR01::new(),
102+
operator2: OPERATOR02::new(),
92103
}
93104
}
94105
}
95106

96-
#[derive(Default)]
97-
pub struct UnitZero;
98-
99-
#[derive(Default)]
100-
pub struct UnitOne;
107+
pub struct MCPWM1 {
108+
timer0: TIMER10,
109+
timer1: TIMER11,
110+
timer2: TIMER12,
101111

102-
pub type Duty = u16;
103-
104-
pub struct MCPWM<U: Unit> {
105-
_unit: U,
112+
pub operator0: OPERATOR10,
113+
pub operator1: OPERATOR11,
114+
pub operator2: OPERATOR12,
106115
}
107116

108-
impl<U: Unit> MCPWM<U> {
117+
impl MCPWM1 {
109118
/// # Safety
110119
///
111-
/// It is safe to instantiate this exactly one time per `Unit`.
112-
unsafe fn new() -> Self {
120+
/// It is safe to instantiate this exactly one time per `Group`.
121+
pub unsafe fn new() -> Self {
113122
Self {
114-
_unit: U::default(),
123+
timer0: TIMER10::new(),
124+
timer1: TIMER11::new(),
125+
timer2: TIMER12::new(),
126+
operator0: OPERATOR10::new(),
127+
operator1: OPERATOR11::new(),
128+
operator2: OPERATOR12::new(),
115129
}
116130
}
117131
}
118132

119-
pub trait Unit: Default {
120-
const ID: mcpwm_unit_t;
133+
#[derive(Default)]
134+
pub struct Group0;
135+
136+
#[derive(Default)]
137+
pub struct Group1;
138+
139+
pub type Duty = u16;
140+
141+
// Note this was called `Unit` in IDF < 5.0
142+
pub trait Group: Default {
143+
const ID: ffi::c_int;
121144
}
122145

123-
impl Unit for UnitZero {
124-
const ID: mcpwm_unit_t = mcpwm_unit_t_MCPWM_UNIT_0;
146+
impl Group for Group0 {
147+
const ID: ffi::c_int = 0;
125148
}
126149

127-
impl Unit for UnitOne {
128-
const ID: mcpwm_unit_t = mcpwm_unit_t_MCPWM_UNIT_1;
150+
impl Group for Group1 {
151+
const ID: ffi::c_int = 1;
129152
}

src/mcpwm/operator.rs

Lines changed: 36 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,47 @@
1-
use std::borrow::Borrow;
1+
use esp_idf_sys::{EspError, mcpwm_oper_handle_t};
22

3-
use esp_idf_sys::{mcpwm_io_signals_t, mcpwm_unit_t, mcpwm_operator_t,
4-
mcpwm_io_signals_t_MCPWM0A, mcpwm_io_signals_t_MCPWM0B,
5-
mcpwm_io_signals_t_MCPWM1A, mcpwm_io_signals_t_MCPWM1B,
6-
mcpwm_io_signals_t_MCPWM2A, mcpwm_io_signals_t_MCPWM2B, EspError
7-
};
8-
9-
use crate::{mcpwm::{Unit, UnitZero, UnitOne}, gpio::OutputPin};
3+
use crate::{mcpwm::{Group, Group0, Group1}, gpio::OutputPin};
104

115
use super::{Duty, timer_connection::OptionalOutputPin};
126

7+
use core::ffi;
8+
139
// The hardware for ESP32 and ESP32-S3 can associate any operator(within the mcpwm module) with any
1410
// timer(within the mcpwm module) for example allowing using the same timer for all three operators.
15-
pub trait HwOperator<U: Unit> {
16-
const SIGNAL_A: mcpwm_io_signals_t;
17-
const SIGNAL_B: mcpwm_io_signals_t;
18-
const UNIT_ID: mcpwm_unit_t = U::ID;
11+
pub trait HwOperator<U: Group> {
12+
const GROUP_ID: ffi::c_int = U::ID;
1913
}
2014

21-
macro_rules! impl_operator_helper {
22-
($instance:ident: $timer:expr, $signal_a:expr, $signal_b:expr, $unit:ty) => {
23-
impl HwOperator<$unit> for $instance<$unit> {
24-
const SIGNAL_A: mcpwm_io_signals_t = $signal_a;
25-
const SIGNAL_B: mcpwm_io_signals_t = $signal_b;
26-
}
15+
macro_rules! impl_operator {
16+
($t:ident, $g:ty) => {
17+
crate::impl_peripheral!($t);
18+
19+
impl HwOperator<$g> for $t {}
2720
};
2821
}
2922

30-
macro_rules! impl_operator {
31-
($instance:ident: $timer:expr, $signal_a:expr, $signal_b:expr) => {
32-
pub struct $instance<U: Unit> {
33-
_unit: U,
34-
}
23+
pub trait HwOperator0<G: Group>: HwOperator<G> {}
24+
pub trait HwOperator1<G: Group>: HwOperator<G> {}
25+
pub trait HwOperator2<G: Group>: HwOperator<G> {}
3526

36-
impl<U: Unit> $instance<U> {
37-
/// # Safety
38-
///
39-
/// It is safe to instantiate this operator exactly one time per Unit.
40-
pub unsafe fn new() -> Self {
41-
$instance {
42-
_unit: U::default(),
43-
}
44-
}
45-
}
27+
// Group 0
28+
impl_operator!(OPERATOR00, Group0);
29+
impl_operator!(OPERATOR01, Group0);
30+
impl_operator!(OPERATOR02, Group0);
4631

47-
impl_operator_helper!($instance: $timer, $signal_a, $signal_b, UnitZero);
48-
impl_operator_helper!($instance: $timer, $signal_a, $signal_b, UnitOne);
49-
};
50-
}
32+
// Group 1
33+
impl_operator!(OPERATOR10, Group1);
34+
impl_operator!(OPERATOR11, Group1);
35+
impl_operator!(OPERATOR12, Group1);
36+
37+
impl HwOperator0<Group0> for OPERATOR00 {}
38+
impl HwOperator0<Group1> for OPERATOR10 {}
39+
40+
impl HwOperator1<Group0> for OPERATOR01 {}
41+
impl HwOperator1<Group1> for OPERATOR11 {}
5142

52-
impl_operator!(
53-
OPERATOR0: mcpwm_timer_t_MCPWM_TIMER_0,
54-
mcpwm_io_signals_t_MCPWM0A,
55-
mcpwm_io_signals_t_MCPWM0B
56-
);
57-
impl_operator!(
58-
OPERATOR1: mcpwm_timer_t_MCPWM_TIMER_1,
59-
mcpwm_io_signals_t_MCPWM1A,
60-
mcpwm_io_signals_t_MCPWM1B
61-
);
62-
impl_operator!(
63-
OPERATOR2: mcpwm_timer_t_MCPWM_TIMER_2,
64-
mcpwm_io_signals_t_MCPWM2A,
65-
mcpwm_io_signals_t_MCPWM2B
66-
);
43+
impl HwOperator2<Group0> for OPERATOR02 {}
44+
impl HwOperator2<Group1> for OPERATOR12 {}
6745

6846
// TODO: How do we want syncing to fit in to this?
6947
// TODO: How do we want carrier to fit into this?
@@ -73,8 +51,8 @@ impl_operator!(
7351
///
7452
/// Every Motor Control module has three operators. Every operator can generate two output signals called A and B.
7553
/// A and B share the same timer and thus frequency and phase but can have induvidual duty set.
76-
pub struct Operator<U: Unit, O: HwOperator<U>, PA: OptionalOutputPin, PB: OptionalOutputPin> {
77-
handle: mcpwm_operator_t,
54+
pub struct Operator<U: Group, O: HwOperator<U>, PA: OptionalOutputPin, PB: OptionalOutputPin> {
55+
handle: mcpwm_oper_handle_t,
7856
_instance: O,
7957

8058
_pin_a: PA,
@@ -85,7 +63,7 @@ pub struct Operator<U: Unit, O: HwOperator<U>, PA: OptionalOutputPin, PB: Option
8563

8664
impl<U, O, PA, PB> Operator<U, O, PA, PB>
8765
where
88-
U: Unit,
66+
U: Group,
8967
O: HwOperator<U>,
9068
PA: OutputPin,
9169
PB: OptionalOutputPin,
@@ -103,7 +81,7 @@ where
10381

10482
impl<U, O, PA, PB> Operator<U, O, PA, PB>
10583
where
106-
U: Unit,
84+
U: Group,
10785
O: HwOperator<U>,
10886
PA: OptionalOutputPin,
10987
PB: OutputPin,
@@ -187,10 +165,10 @@ pub enum DutyMode {
187165
ActiveLow,
188166
}
189167

190-
pub trait OptionalOperator<U: Unit, O: HwOperator<U>> {}
168+
pub trait OptionalOperator<U: Group, O: HwOperator<U>> {}
191169

192170
pub struct NoOperator;
193-
impl<U: Unit, O: HwOperator<U>> OptionalOperator<U, O> for NoOperator {}
171+
impl<U: Group, O: HwOperator<U>> OptionalOperator<U, O> for NoOperator {}
194172

195173
/*
196174

0 commit comments

Comments
 (0)