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
+
1
13
// The hardware for ESP32 and ESP32-S3 can associate any operator(within the mcpwm module) with any
2
14
// 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 ;
10
19
}
11
20
12
21
macro_rules! impl_operator_helper {
13
22
( $instance: ident: $timer: expr, $signal_a: expr, $signal_b: expr, $unit: ty) => {
14
23
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;
22
26
}
23
27
} ;
24
28
}
@@ -40,16 +44,6 @@ macro_rules! impl_operator {
40
44
}
41
45
}
42
46
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
-
53
47
impl_operator_helper!( $instance: $timer, $signal_a, $signal_b, UnitZero ) ;
54
48
impl_operator_helper!( $instance: $timer, $signal_a, $signal_b, UnitOne ) ;
55
49
} ;
@@ -79,21 +73,20 @@ impl_operator!(
79
73
///
80
74
/// Every Motor Control module has three operators. Every operator can generate two output signals called A and B.
81
75
/// 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 > {
83
77
handle : mcpwm_operator_t ,
84
78
_instance : O ,
85
79
86
80
_pin_a : PA ,
87
81
_pin_b : PB ,
88
82
89
- deadtime : D
83
+ // deadtime: D
90
84
}
91
85
92
- impl < U , O , M , PA , PB > Operator < U , O , M , PA , PB >
86
+ impl < U , O , PA , PB > Operator < U , O , PA , PB >
93
87
where
94
88
U : Unit ,
95
89
O : HwOperator < U > ,
96
- M : Borrow < Mcpwm < U > > ,
97
90
PA : OutputPin ,
98
91
PB : OptionalOutputPin ,
99
92
{
@@ -108,11 +101,10 @@ where
108
101
}
109
102
}
110
103
111
- impl < U , O , M , PA , PB > Operator < U , O , M , PA , PB >
104
+ impl < U , O , PA , PB > Operator < U , O , PA , PB >
112
105
where
113
106
U : Unit ,
114
107
O : HwOperator < U > ,
115
- M : Borrow < Mcpwm < U > > ,
116
108
PA : OptionalOutputPin ,
117
109
PB : OutputPin ,
118
110
{
@@ -134,7 +126,7 @@ pub struct OperatorConfig {
134
126
135
127
duty_mode : DutyMode ,
136
128
137
- deadtime : Option < DeadtimeConfig > ,
129
+ // deadtime: Option<DeadtimeConfig>,
138
130
}
139
131
140
132
impl OperatorConfig {
@@ -159,26 +151,22 @@ impl OperatorConfig {
159
151
self
160
152
}
161
153
162
- #[ must_use]
154
+ /* #[must_use]
163
155
pub fn deadtime(mut self, deadtime: impl Into<Option<DeadtimeConfig>>) -> Self {
164
156
self.deadtime = deadtime.into();
165
157
self
166
- }
158
+ }*/
167
159
}
168
160
169
161
impl Default for OperatorConfig {
170
162
fn default ( ) -> Self {
171
163
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 ,
178
166
179
167
duty_mode : DutyMode :: ActiveHigh ,
180
168
181
- deadtime : None ,
169
+ // deadtime: None,
182
170
}
183
171
}
184
172
}
@@ -199,6 +187,13 @@ pub enum DutyMode {
199
187
ActiveLow ,
200
188
}
201
189
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
+
202
197
#[derive(Default, Clone)]
203
198
struct DutyConfig {
204
199
on_matches_cmp_a: CountingDirection,
@@ -226,23 +221,30 @@ impl Default for GeneratorAction {
226
221
}
227
222
}
228
223
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> {
231
226
match val {
232
227
DutyMode::ActiveHigh => {
233
228
let mut duty_config: DutyConfig = Default::default();
234
229
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
+ }
237
235
duty_config
238
236
},
239
237
DutyMode::ActiveLow => {
240
238
let mut duty_config: DutyConfig = Default::default();
241
239
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
+ }
243
245
244
246
duty_config
245
247
},
246
248
}
247
249
}
248
- }
250
+ } */
0 commit comments