Skip to content

Commit 1944065

Browse files
committed
Compiling again, still some work left to do before working
1 parent a715adb commit 1944065

File tree

4 files changed

+200
-166
lines changed

4 files changed

+200
-166
lines changed

src/mcpwm/comparator.rs

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
use core::ptr;
22

3-
use esp_idf_sys::{mcpwm_cmpr_handle_t, mcpwm_gen_t, mcpwm_generator_set_actions_on_compare_event, mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_UP, mcpwm_generator_action_t_MCPWM_GEN_ACTION_KEEP, mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_DOWN, mcpwm_comparator_config_t__bindgen_ty_1, esp, mcpwm_comparator_config_t, mcpwm_new_comparator, mcpwm_oper_handle_t};
3+
use esp_idf_sys::{
4+
esp, mcpwm_cmpr_handle_t, mcpwm_comparator_config_t, mcpwm_comparator_config_t__bindgen_ty_1,
5+
mcpwm_gen_t, mcpwm_generator_set_actions_on_compare_event, mcpwm_new_comparator,
6+
mcpwm_oper_handle_t, mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_DOWN,
7+
mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_UP,
8+
};
49

5-
use super::generator::{CountingDirection, OnMatchCfg, NoCmpMatchConfig};
10+
use super::generator::{CountingDirection, NoCmpMatchConfig, OnMatchCfg};
611

7-
8-
trait ComparatorChannel{}
12+
trait ComparatorChannel {}
913

1014
pub struct CmpX;
1115
impl ComparatorChannel for CmpX {}
@@ -15,22 +19,17 @@ impl ComparatorChannel for CmpY {}
1519

1620
pub trait OptionalCmp {
1721
type OnMatchCfg: OnMatchCfg;
18-
type Cfg: OptionalCmpCfg<Cmp=Self>;
19-
unsafe fn _configure(&mut self, gen: &mut mcpwm_gen_t, cfg: Self::OnMatchCfg);
22+
type Cfg: OptionalCmpCfg<Cmp = Self>;
23+
24+
fn get_comparator_mut(&mut self) -> Option<&mut Comparator>;
2025
}
2126

2227
impl OptionalCmp for Comparator {
2328
type OnMatchCfg = super::generator::CountingDirection;
2429
type Cfg = ComparatorConfig;
2530

26-
unsafe fn _configure(&mut self, gen: &mut mcpwm_gen_t, cfg: Self::OnMatchCfg) {
27-
// TODO: "must be terminated by MCPWM_GEN_TIMER_EVENT_ACTION_END()"
28-
mcpwm_generator_set_actions_on_compare_event(gen, esp_idf_sys::mcpwm_gen_compare_event_action_t {
29-
direction: mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_UP, comparator: self.0, action: cfg.counting_up.into()
30-
});
31-
mcpwm_generator_set_actions_on_compare_event(gen, esp_idf_sys::mcpwm_gen_compare_event_action_t {
32-
direction: mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_DOWN, comparator: self.0, action: cfg.counting_down.into()
33-
});
31+
fn get_comparator_mut(&mut self) -> Option<&mut Comparator> {
32+
Some(self)
3433
}
3534
}
3635

@@ -39,12 +38,34 @@ impl OptionalCmp for NoCmp {
3938
type OnMatchCfg = super::generator::NoCmpMatchConfig;
4039
type Cfg = NoCmpCfg;
4140

42-
unsafe fn _configure(&mut self, gen: &mut mcpwm_gen_t, cfg: Self::OnMatchCfg) {
43-
// Do nothing
41+
fn get_comparator_mut(&mut self) -> Option<&mut Comparator> {
42+
None
4443
}
4544
}
4645

47-
pub struct Comparator(pub(crate)mcpwm_cmpr_handle_t);
46+
pub struct Comparator(pub(crate) mcpwm_cmpr_handle_t);
47+
48+
impl Comparator {
49+
pub(crate) unsafe fn configure(&mut self, gen: &mut mcpwm_gen_t, cfg: CountingDirection) {
50+
// TODO: "must be terminated by MCPWM_GEN_TIMER_EVENT_ACTION_END()"
51+
mcpwm_generator_set_actions_on_compare_event(
52+
gen,
53+
esp_idf_sys::mcpwm_gen_compare_event_action_t {
54+
direction: mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_UP,
55+
comparator: self.0,
56+
action: cfg.counting_up.into(),
57+
},
58+
);
59+
mcpwm_generator_set_actions_on_compare_event(
60+
gen,
61+
esp_idf_sys::mcpwm_gen_compare_event_action_t {
62+
direction: mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_DOWN,
63+
comparator: self.0,
64+
action: cfg.counting_down.into(),
65+
},
66+
);
67+
}
68+
}
4869

4970
#[derive(Debug, Clone, Copy)]
5071
pub struct ComparatorConfig {
@@ -63,7 +84,7 @@ impl Default for ComparatorConfig {
6384

6485
pub struct NoCmpCfg;
6586

66-
pub trait OptionalCmpCfg{
87+
pub trait OptionalCmpCfg {
6788
type OnMatchCfg: OnMatchCfg;
6889
type Cmp: OptionalCmp;
6990
unsafe fn init(self, operator_handle: mcpwm_oper_handle_t) -> Self::Cmp;
@@ -77,7 +98,7 @@ impl OptionalCmpCfg for NoCmpCfg {
7798
NoCmp
7899
}
79100
}
80-
impl OptionalCmpCfg for ComparatorConfig{
101+
impl OptionalCmpCfg for ComparatorConfig {
81102
type OnMatchCfg = CountingDirection;
82103
type Cmp = Comparator;
83104

@@ -88,7 +109,7 @@ impl OptionalCmpCfg for ComparatorConfig{
88109
unsafe {
89110
esp!(mcpwm_new_comparator(operator_handle, &cfg, &mut cmp)).unwrap();
90111
}
91-
112+
92113
Comparator(cmp)
93114
}
94-
}
115+
}

src/mcpwm/generator.rs

Lines changed: 84 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -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::{
1414
use crate::gpio::OutputPin;
1515

1616
use super::{
17-
comparator::OptionalCmp,
18-
timer_connection::{NoPin, OptionalOutputPin},
17+
comparator::{Comparator, OptionalCmp},
1918
};
2019

2120
pub 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

4236
pub 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

7269
pub 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+
179191
impl<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

260280
pub trait OnMatchCfg {
261281
fn empty() -> Self;
282+
fn to_counting_direction(self) -> CountingDirection;
262283
}
263284

264285
impl Into<mcpwm_generator_action_t> for GeneratorAction {

0 commit comments

Comments
 (0)