@@ -5,7 +5,7 @@ use esp_idf_sys::{
55 mcpwm_generator_action_t_MCPWM_GEN_ACTION_HIGH, mcpwm_generator_action_t_MCPWM_GEN_ACTION_KEEP,
66 mcpwm_generator_action_t_MCPWM_GEN_ACTION_LOW,
77 mcpwm_generator_action_t_MCPWM_GEN_ACTION_TOGGLE, mcpwm_generator_config_t,
8- mcpwm_generator_config_t__bindgen_ty_1 , mcpwm_generator_set_actions_on_timer_event,
8+ mcpwm_generator_set_actions_on_timer_event,
99 mcpwm_new_generator, mcpwm_oper_handle_t, mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_DOWN,
1010 mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_UP, mcpwm_timer_event_t_MCPWM_TIMER_EVENT_EMPTY,
1111 mcpwm_timer_event_t_MCPWM_TIMER_EVENT_FULL,
@@ -14,29 +14,23 @@ use esp_idf_sys::{
1414use crate :: gpio:: OutputPin ;
1515
1616use super :: {
17- comparator:: OptionalCmp ,
18- timer_connection:: { NoPin , OptionalOutputPin } ,
17+ comparator:: { Comparator , OptionalCmp } ,
1918} ;
2019
2120pub struct NoGen ;
2221
23- impl OptionalGen for NoGen { }
24- pub trait OptionalGen { }
25-
26- impl < P : OutputPin > OptionalGen for Generator < P > { }
27-
28- pub trait ToGenCfg {
29- type Cfg : OptionalGenCfg ;
22+ impl OptionalGen for NoGen {
3023}
31-
32- impl < G : GeneratorChannel , CMP_X : OnMatchCfg , CMP_Y : OnMatchCfg , P : OutputPin > ToGenCfg
33- for ( CMP_X , CMP_Y , G , Generator < P > )
34- {
35- type Cfg = GeneratorConfig < G , CMP_X , CMP_Y , P > ;
24+ pub trait OptionalGen {
3625}
3726
38- impl < G : GeneratorChannel , CMP_X : OnMatchCfg , CMP_Y : OnMatchCfg > ToGenCfg for ( CMP_X , CMP_Y , G , NoGenCfg ) {
39- type Cfg = NoGenCfg ;
27+ impl < G , CMP_X , CMP_Y , P > OptionalGen for Generator < G , CMP_X , CMP_Y , P >
28+ where
29+ G : GeneratorChannel ,
30+ CMP_X : OnMatchCfg ,
31+ CMP_Y : OnMatchCfg ,
32+ P : OutputPin ,
33+ {
4034}
4135
4236pub trait GeneratorChannel {
@@ -54,7 +48,10 @@ impl GeneratorChannel for GenB {
5448}
5549
5650// TODO: Allow OptionalOutputPin?
57- pub struct Generator < P : OutputPin > {
51+ pub struct Generator < G , CMP_X , CMP_Y , P : OutputPin > {
52+ channel : PhantomData < G > ,
53+ cmp_x : PhantomData < CMP_X > ,
54+ cmp_y : PhantomData < CMP_Y > ,
5855 pub ( crate ) handle : mcpwm_gen_handle_t ,
5956 pub ( crate ) pin : P ,
6057}
@@ -71,57 +68,45 @@ pub struct GeneratorConfig<G: GeneratorChannel, CMP_X, CMP_Y, P> {
7168
7269pub struct NoGenCfg ;
7370
74- pub trait OptionalGenCfg { }
75-
76- impl OptionalGenCfg for NoGenCfg { }
77-
78- impl < G : GeneratorChannel , CMP_X : OnMatchCfg , CMP_Y : OnMatchCfg , P > OptionalGenCfg
79- for GeneratorConfig < G , CMP_X , CMP_Y , P >
80- {
81- }
82-
83- pub trait GenInit {
71+ pub trait OptionalGenCfg {
8472 type Gen : OptionalGen ;
8573
8674 /// This is only to be used internally by esp-idf-hal
87- unsafe fn init ( self , operator_handle : mcpwm_oper_handle_t ) -> Self :: Gen ;
75+ unsafe fn init (
76+ self ,
77+ operator_handle : mcpwm_oper_handle_t ,
78+ cmp_x : Option < & mut Comparator > ,
79+ cmp_y : Option < & mut Comparator > ,
80+ ) -> Self :: Gen ;
8881}
8982
90- impl < CMP_X , CMP_Y > GenInit
91- for (
92- & mut CMP_X ,
93- & mut CMP_Y ,
94- NoGenCfg ,
95- )
96- where
97- CMP_X : OptionalCmp ,
98- CMP_Y : OptionalCmp ,
99- {
83+ impl OptionalGenCfg for NoGenCfg {
10084 type Gen = NoGen ;
10185
102- unsafe fn init ( self , operator_handle : mcpwm_oper_handle_t ) -> Self :: Gen {
86+ unsafe fn init (
87+ self ,
88+ _operator_handle : mcpwm_oper_handle_t ,
89+ _cmp_x : Option < & mut Comparator > ,
90+ _cmp_y : Option < & mut Comparator > ,
91+ ) -> NoGen {
10392 NoGen
10493 }
10594}
10695
107- impl < G : GeneratorChannel , CMP_X , CMP_Y , P : OutputPin > GenInit
108- for (
109- & mut CMP_X ,
110- & mut CMP_Y ,
111- GeneratorConfig < G , CMP_X :: OnMatchCfg , CMP_Y :: OnMatchCfg , P > ,
112- )
113- where
114- CMP_X : OptionalCmp ,
115- CMP_Y : OptionalCmp ,
96+ impl < G : GeneratorChannel , CMP_X : OnMatchCfg , CMP_Y : OnMatchCfg , P : OutputPin > OptionalGenCfg
97+ for GeneratorConfig < G , CMP_X , CMP_Y , P >
11698{
117- type Gen = Generator < P > ;
118-
119- unsafe fn init ( mut self , operator_handle : mcpwm_oper_handle_t ) -> Generator < P > {
120- let ( cmp_x, cmp_y, generator_config) = self ;
121-
99+ type Gen = Generator < G , CMP_X , CMP_Y , P > ;
100+
101+ unsafe fn init (
102+ self ,
103+ operator_handle : mcpwm_oper_handle_t ,
104+ cmp_x : Option < & mut Comparator > ,
105+ cmp_y : Option < & mut Comparator > ,
106+ ) -> Self :: Gen {
122107 let cfg = mcpwm_generator_config_t {
123- gen_gpio_num : generator_config . pin . pin ( ) ,
124- flags : todo ! ( ) , //generator_config.flags,
108+ gen_gpio_num : self . pin . pin ( ) ,
109+ flags : todo ! ( ) , //generator_config.flags,
125110 } ;
126111 let mut gen = ptr:: null_mut ( ) ;
127112 unsafe {
@@ -133,7 +118,7 @@ where
133118 mcpwm_gen_timer_event_action_t {
134119 direction: mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_UP,
135120 event: mcpwm_timer_event_t_MCPWM_TIMER_EVENT_EMPTY,
136- action: generator_config . on_is_empty. counting_up. into( ) ,
121+ action: self . on_is_empty. counting_up. into( ) ,
137122 }
138123 ) )
139124 . unwrap ( ) ;
@@ -142,7 +127,7 @@ where
142127 mcpwm_gen_timer_event_action_t {
143128 direction: mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_DOWN,
144129 event: mcpwm_timer_event_t_MCPWM_TIMER_EVENT_EMPTY,
145- action: generator_config . on_is_empty. counting_down. into( ) ,
130+ action: self . on_is_empty. counting_down. into( ) ,
146131 }
147132 ) )
148133 . unwrap ( ) ;
@@ -151,7 +136,7 @@ where
151136 mcpwm_gen_timer_event_action_t {
152137 direction: mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_UP,
153138 event: mcpwm_timer_event_t_MCPWM_TIMER_EVENT_FULL,
154- action: generator_config . on_is_full. counting_up. into( ) ,
139+ action: self . on_is_full. counting_up. into( ) ,
155140 }
156141 ) )
157142 . unwrap ( ) ;
@@ -160,22 +145,49 @@ where
160145 mcpwm_gen_timer_event_action_t {
161146 direction: mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_DOWN,
162147 event: mcpwm_timer_event_t_MCPWM_TIMER_EVENT_FULL,
163- action: generator_config . on_is_full. counting_down. into( ) ,
148+ action: self . on_is_full. counting_down. into( ) ,
164149 }
165150 ) )
166151 . unwrap ( ) ;
167152
168- cmp_x. _configure ( & mut * gen, generator_config. on_matches_cmp_x ) ;
169- cmp_y. _configure ( & mut * gen, generator_config. on_matches_cmp_y ) ;
153+ if let Some ( cmp_x) = cmp_x {
154+ cmp_x. configure ( & mut * gen, self . on_matches_cmp_x . to_counting_direction ( ) ) ;
155+ }
156+
157+ if let Some ( cmp_y) = cmp_y {
158+ cmp_y. configure ( & mut * gen, self . on_matches_cmp_y . to_counting_direction ( ) ) ;
159+ }
170160 }
171161
172162 Generator {
163+ channel : PhantomData ,
164+ cmp_x : PhantomData ,
165+ cmp_y : PhantomData ,
173166 handle : gen,
174- pin : generator_config . pin ,
167+ pin : self . pin ,
175168 }
176169 }
177170}
178171
172+ pub trait GenInit {
173+ type Gen : OptionalGen ;
174+
175+ /// This is only to be used internally by esp-idf-hal
176+ unsafe fn init ( self , operator_handle : mcpwm_oper_handle_t ) -> Self :: Gen ;
177+ }
178+
179+ impl < CMP_X , CMP_Y > GenInit for ( & mut CMP_X , & mut CMP_Y , NoGenCfg )
180+ where
181+ CMP_X : OptionalCmp ,
182+ CMP_Y : OptionalCmp ,
183+ {
184+ type Gen = NoGen ;
185+
186+ unsafe fn init ( self , operator_handle : mcpwm_oper_handle_t ) -> Self :: Gen {
187+ NoGen
188+ }
189+ }
190+
179191impl < G : GeneratorChannel , P > GeneratorConfig < G , CountingDirection , CountingDirection , P > {
180192 pub fn active_high ( pin : P ) -> Self {
181193 let mut result: Self = GeneratorConfig :: empty ( pin) ;
@@ -233,6 +245,10 @@ impl OnMatchCfg for NoCmpMatchConfig {
233245 fn empty ( ) -> Self {
234246 NoCmpMatchConfig
235247 }
248+
249+ fn to_counting_direction ( self ) -> CountingDirection {
250+ CountingDirection :: empty ( )
251+ }
236252}
237253
238254// TODO: Come up with better name
@@ -255,10 +271,15 @@ impl OnMatchCfg for CountingDirection {
255271 fn empty ( ) -> Self {
256272 CountingDirection :: empty ( )
257273 }
274+
275+ fn to_counting_direction ( self ) -> CountingDirection {
276+ self
277+ }
258278}
259279
260280pub trait OnMatchCfg {
261281 fn empty ( ) -> Self ;
282+ fn to_counting_direction ( self ) -> CountingDirection ;
262283}
263284
264285impl Into < mcpwm_generator_action_t > for GeneratorAction {
0 commit comments