1+ use std:: borrow:: Borrow ;
2+
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 } ;
10+
11+ use super :: { Duty , timer_connection:: OptionalOutputPin } ;
12+
113// The hardware for ESP32 and ESP32-S3 can associate any operator(within the mcpwm module) with any
214// timer(within the mcpwm module) for example allowing using the same timer for all three operators.
3- // However at least as of IDF v4.4 timer0 is hardcoded to operator0 and timer1 to operator1 and so on...
4- pub trait HwOperator < U : Unit > : Into < Operator < U , Self > > {
5- fn signal_a ( ) -> mcpwm_io_signals_t ;
6- fn signal_b ( ) -> mcpwm_io_signals_t ;
7- fn unit ( ) -> mcpwm_unit_t {
8- U :: unit ( )
9- }
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 ;
1019}
1120
1221macro_rules! impl_operator_helper {
1322 ( $instance: ident: $timer: expr, $signal_a: expr, $signal_b: expr, $unit: ty) => {
1423 impl HwOperator <$unit> for $instance<$unit> {
15- fn signal_a( ) -> mcpwm_io_signals_t {
16- $signal_a
17- }
18-
19- fn signal_b( ) -> mcpwm_io_signals_t {
20- $signal_b
21- }
24+ const SIGNAL_A : mcpwm_io_signals_t = $signal_a;
25+ const SIGNAL_B : mcpwm_io_signals_t = $signal_b;
2226 }
2327 } ;
2428}
@@ -40,16 +44,6 @@ macro_rules! impl_operator {
4044 }
4145 }
4246
43- impl <U : Unit > Into <Operator <U >> for $instance<U > {
44- fn into( self ) -> Operator <U > {
45- Operator {
46- _instance: self ,
47- pin_a: NoPin ,
48- pin_b: NoPin ,
49- }
50- }
51- }
52-
5347 impl_operator_helper!( $instance: $timer, $signal_a, $signal_b, UnitZero ) ;
5448 impl_operator_helper!( $instance: $timer, $signal_a, $signal_b, UnitOne ) ;
5549 } ;
@@ -79,21 +73,20 @@ impl_operator!(
7973///
8074/// Every Motor Control module has three operators. Every operator can generate two output signals called A and B.
8175/// A and B share the same timer and thus frequency and phase but can have induvidual duty set.
82- pub struct Operator < U : Unit , O : HwOperator < U > , M : Borrow < Mcpwm < U > > , PA : OptionalPin , PB : OptionalPin , D > {
76+ pub struct Operator < U : Unit , O : HwOperator < U > , PA : OptionalOutputPin , PB : OptionalOutputPin > {
8377 handle : mcpwm_operator_t ,
8478 _instance : O ,
8579
8680 _pin_a : PA ,
8781 _pin_b : PB ,
8882
89- deadtime : D
83+ // deadtime: D
9084}
9185
92- impl < U , O , M , PA , PB > Operator < U , O , M , PA , PB >
86+ impl < U , O , PA , PB > Operator < U , O , PA , PB >
9387where
9488 U : Unit ,
9589 O : HwOperator < U > ,
96- M : Borrow < Mcpwm < U > > ,
9790 PA : OutputPin ,
9891 PB : OptionalOutputPin ,
9992{
@@ -108,11 +101,10 @@ where
108101 }
109102}
110103
111- impl < U , O , M , PA , PB > Operator < U , O , M , PA , PB >
104+ impl < U , O , PA , PB > Operator < U , O , PA , PB >
112105where
113106 U : Unit ,
114107 O : HwOperator < U > ,
115- M : Borrow < Mcpwm < U > > ,
116108 PA : OptionalOutputPin ,
117109 PB : OutputPin ,
118110{
@@ -134,7 +126,7 @@ pub struct OperatorConfig {
134126
135127 duty_mode : DutyMode ,
136128
137- deadtime : Option < DeadtimeConfig > ,
129+ // deadtime: Option<DeadtimeConfig>,
138130}
139131
140132impl OperatorConfig {
@@ -159,26 +151,22 @@ impl OperatorConfig {
159151 self
160152 }
161153
162- #[ must_use]
154+ /* #[must_use]
163155 pub fn deadtime(mut self, deadtime: impl Into<Option<DeadtimeConfig>>) -> Self {
164156 self.deadtime = deadtime.into();
165157 self
166- }
158+ }*/
167159}
168160
169161impl Default for OperatorConfig {
170162 fn default ( ) -> Self {
171163 Self {
172- frequency : 1000 . Hz ( ) ,
173- duty_a : 50.0 ,
174- duty_b : 50.0 ,
175-
176- #[ cfg( not( esp_idf_version = "4.3" ) ) ]
177- lowest_frequency : 16 . Hz ( ) ,
164+ duty_a : 0.0 ,
165+ duty_b : 0.0 ,
178166
179167 duty_mode : DutyMode :: ActiveHigh ,
180168
181- deadtime : None ,
169+ // deadtime: None,
182170 }
183171 }
184172}
@@ -199,6 +187,13 @@ pub enum DutyMode {
199187 ActiveLow ,
200188}
201189
190+ pub trait OptionalOperator < U : Unit , O : HwOperator < U > > { }
191+
192+ pub struct NoOperator ;
193+ impl < U : Unit , O : HwOperator < U > > OptionalOperator < U , O > for NoOperator { }
194+
195+ /*
196+
202197#[derive(Default, Clone)]
203198struct DutyConfig {
204199 on_matches_cmp_a: CountingDirection,
@@ -226,23 +221,30 @@ impl Default for GeneratorAction {
226221 }
227222}
228223
229- impl From < DutyMode > for DutyConfig {
230- fn from ( val : DutyMode ) -> Self {
224+ impl DutyMode {
225+ fn into_duty_cfg<G: Generator>(self ) -> DutyConfig<G> {
231226 match val {
232227 DutyMode::ActiveHigh => {
233228 let mut duty_config: DutyConfig = Default::default();
234229 duty_config.on_is_empty.counting_up = GeneratorAction::SetHigh;
235- duty_config. on_matches_cmp_a . counting_up = GeneratorAction :: SetLow ;
236-
230+ if G::IS_A {
231+ duty_config.on_matches_cmp_a.counting_up = GeneratorAction::SetLow;
232+ } else {
233+ duty_config.on_matches_cmp_b.counting_up = GeneratorAction::SetLow;
234+ }
237235 duty_config
238236 },
239237 DutyMode::ActiveLow => {
240238 let mut duty_config: DutyConfig = Default::default();
241239 duty_config.on_is_empty.counting_up = GeneratorAction::SetLow;
242- duty_config. on_matches_cmp_a . counting_up = GeneratorAction :: SetHigh ;
240+ if G::IS_A {
241+ duty_config.on_matches_cmp_a.counting_up = GeneratorAction::SetHigh;
242+ } else {
243+ duty_config.on_matches_cmp_b.counting_up = GeneratorAction::SetHigh;
244+ }
243245
244246 duty_config
245247 },
246248 }
247249 }
248- }
250+ } */
0 commit comments