Skip to content

Commit

Permalink
Compiling again, still some work left to do before working
Browse files Browse the repository at this point in the history
  • Loading branch information
usbalbin committed Nov 28, 2022
1 parent a715adb commit 1944065
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 166 deletions.
63 changes: 42 additions & 21 deletions src/mcpwm/comparator.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use core::ptr;

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};
use esp_idf_sys::{
esp, mcpwm_cmpr_handle_t, mcpwm_comparator_config_t, mcpwm_comparator_config_t__bindgen_ty_1,
mcpwm_gen_t, mcpwm_generator_set_actions_on_compare_event, mcpwm_new_comparator,
mcpwm_oper_handle_t, mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_DOWN,
mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_UP,
};

use super::generator::{CountingDirection, OnMatchCfg, NoCmpMatchConfig};
use super::generator::{CountingDirection, NoCmpMatchConfig, OnMatchCfg};


trait ComparatorChannel{}
trait ComparatorChannel {}

pub struct CmpX;
impl ComparatorChannel for CmpX {}
Expand All @@ -15,22 +19,17 @@ impl ComparatorChannel for CmpY {}

pub trait OptionalCmp {
type OnMatchCfg: OnMatchCfg;
type Cfg: OptionalCmpCfg<Cmp=Self>;
unsafe fn _configure(&mut self, gen: &mut mcpwm_gen_t, cfg: Self::OnMatchCfg);
type Cfg: OptionalCmpCfg<Cmp = Self>;

fn get_comparator_mut(&mut self) -> Option<&mut Comparator>;
}

impl OptionalCmp for Comparator {
type OnMatchCfg = super::generator::CountingDirection;
type Cfg = ComparatorConfig;

unsafe fn _configure(&mut self, gen: &mut mcpwm_gen_t, cfg: Self::OnMatchCfg) {
// TODO: "must be terminated by MCPWM_GEN_TIMER_EVENT_ACTION_END()"
mcpwm_generator_set_actions_on_compare_event(gen, esp_idf_sys::mcpwm_gen_compare_event_action_t {
direction: mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_UP, comparator: self.0, action: cfg.counting_up.into()
});
mcpwm_generator_set_actions_on_compare_event(gen, esp_idf_sys::mcpwm_gen_compare_event_action_t {
direction: mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_DOWN, comparator: self.0, action: cfg.counting_down.into()
});
fn get_comparator_mut(&mut self) -> Option<&mut Comparator> {
Some(self)
}
}

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

unsafe fn _configure(&mut self, gen: &mut mcpwm_gen_t, cfg: Self::OnMatchCfg) {
// Do nothing
fn get_comparator_mut(&mut self) -> Option<&mut Comparator> {
None
}
}

pub struct Comparator(pub(crate)mcpwm_cmpr_handle_t);
pub struct Comparator(pub(crate) mcpwm_cmpr_handle_t);

impl Comparator {
pub(crate) unsafe fn configure(&mut self, gen: &mut mcpwm_gen_t, cfg: CountingDirection) {
// TODO: "must be terminated by MCPWM_GEN_TIMER_EVENT_ACTION_END()"
mcpwm_generator_set_actions_on_compare_event(
gen,
esp_idf_sys::mcpwm_gen_compare_event_action_t {
direction: mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_UP,
comparator: self.0,
action: cfg.counting_up.into(),
},
);
mcpwm_generator_set_actions_on_compare_event(
gen,
esp_idf_sys::mcpwm_gen_compare_event_action_t {
direction: mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_DOWN,
comparator: self.0,
action: cfg.counting_down.into(),
},
);
}
}

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

pub struct NoCmpCfg;

pub trait OptionalCmpCfg{
pub trait OptionalCmpCfg {
type OnMatchCfg: OnMatchCfg;
type Cmp: OptionalCmp;
unsafe fn init(self, operator_handle: mcpwm_oper_handle_t) -> Self::Cmp;
Expand All @@ -77,7 +98,7 @@ impl OptionalCmpCfg for NoCmpCfg {
NoCmp
}
}
impl OptionalCmpCfg for ComparatorConfig{
impl OptionalCmpCfg for ComparatorConfig {
type OnMatchCfg = CountingDirection;
type Cmp = Comparator;

Expand All @@ -88,7 +109,7 @@ impl OptionalCmpCfg for ComparatorConfig{
unsafe {
esp!(mcpwm_new_comparator(operator_handle, &cfg, &mut cmp)).unwrap();
}

Comparator(cmp)
}
}
}
147 changes: 84 additions & 63 deletions src/mcpwm/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use esp_idf_sys::{
mcpwm_generator_action_t_MCPWM_GEN_ACTION_HIGH, mcpwm_generator_action_t_MCPWM_GEN_ACTION_KEEP,
mcpwm_generator_action_t_MCPWM_GEN_ACTION_LOW,
mcpwm_generator_action_t_MCPWM_GEN_ACTION_TOGGLE, mcpwm_generator_config_t,
mcpwm_generator_config_t__bindgen_ty_1, mcpwm_generator_set_actions_on_timer_event,
mcpwm_generator_set_actions_on_timer_event,
mcpwm_new_generator, mcpwm_oper_handle_t, mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_DOWN,
mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_UP, mcpwm_timer_event_t_MCPWM_TIMER_EVENT_EMPTY,
mcpwm_timer_event_t_MCPWM_TIMER_EVENT_FULL,
Expand All @@ -14,29 +14,23 @@ use esp_idf_sys::{
use crate::gpio::OutputPin;

use super::{
comparator::OptionalCmp,
timer_connection::{NoPin, OptionalOutputPin},
comparator::{Comparator, OptionalCmp},
};

pub struct NoGen;

impl OptionalGen for NoGen {}
pub trait OptionalGen {}

impl<P: OutputPin> OptionalGen for Generator<P> {}

pub trait ToGenCfg {
type Cfg: OptionalGenCfg;
impl OptionalGen for NoGen {
}

impl<G: GeneratorChannel, CMP_X: OnMatchCfg, CMP_Y: OnMatchCfg, P: OutputPin> ToGenCfg
for (CMP_X, CMP_Y, G, Generator<P>)
{
type Cfg = GeneratorConfig<G, CMP_X, CMP_Y, P>;
pub trait OptionalGen {
}

impl<G: GeneratorChannel, CMP_X: OnMatchCfg, CMP_Y: OnMatchCfg> ToGenCfg for (CMP_X, CMP_Y, G, NoGenCfg) {
type Cfg = NoGenCfg;
impl<G, CMP_X, CMP_Y, P> OptionalGen for Generator<G, CMP_X, CMP_Y, P>
where
G: GeneratorChannel,
CMP_X: OnMatchCfg,
CMP_Y: OnMatchCfg,
P: OutputPin,
{
}

pub trait GeneratorChannel {
Expand All @@ -54,7 +48,10 @@ impl GeneratorChannel for GenB {
}

// TODO: Allow OptionalOutputPin?
pub struct Generator<P: OutputPin> {
pub struct Generator<G, CMP_X, CMP_Y, P: OutputPin> {
channel: PhantomData<G>,
cmp_x: PhantomData<CMP_X>,
cmp_y: PhantomData<CMP_Y>,
pub(crate) handle: mcpwm_gen_handle_t,
pub(crate) pin: P,
}
Expand All @@ -71,57 +68,45 @@ pub struct GeneratorConfig<G: GeneratorChannel, CMP_X, CMP_Y, P> {

pub struct NoGenCfg;

pub trait OptionalGenCfg {}

impl OptionalGenCfg for NoGenCfg {}

impl<G: GeneratorChannel, CMP_X: OnMatchCfg, CMP_Y: OnMatchCfg, P> OptionalGenCfg
for GeneratorConfig<G, CMP_X, CMP_Y, P>
{
}

pub trait GenInit {
pub trait OptionalGenCfg {
type Gen: OptionalGen;

/// This is only to be used internally by esp-idf-hal
unsafe fn init(self, operator_handle: mcpwm_oper_handle_t) -> Self::Gen;
unsafe fn init(
self,
operator_handle: mcpwm_oper_handle_t,
cmp_x: Option<&mut Comparator>,
cmp_y: Option<&mut Comparator>,
) -> Self::Gen;
}

impl<CMP_X, CMP_Y> GenInit
for (
&mut CMP_X,
&mut CMP_Y,
NoGenCfg,
)
where
CMP_X: OptionalCmp,
CMP_Y: OptionalCmp,
{
impl OptionalGenCfg for NoGenCfg {
type Gen = NoGen;

unsafe fn init(self, operator_handle: mcpwm_oper_handle_t) -> Self::Gen {
unsafe fn init(
self,
_operator_handle: mcpwm_oper_handle_t,
_cmp_x: Option<&mut Comparator>,
_cmp_y: Option<&mut Comparator>,
) -> NoGen {
NoGen
}
}

impl<G: GeneratorChannel, CMP_X, CMP_Y, P: OutputPin> GenInit
for (
&mut CMP_X,
&mut CMP_Y,
GeneratorConfig<G, CMP_X::OnMatchCfg, CMP_Y::OnMatchCfg, P>,
)
where
CMP_X: OptionalCmp,
CMP_Y: OptionalCmp,
impl<G: GeneratorChannel, CMP_X: OnMatchCfg, CMP_Y: OnMatchCfg, P: OutputPin> OptionalGenCfg
for GeneratorConfig<G, CMP_X, CMP_Y, P>
{
type Gen = Generator<P>;

unsafe fn init(mut self, operator_handle: mcpwm_oper_handle_t) -> Generator<P> {
let (cmp_x, cmp_y, generator_config) = self;

type Gen = Generator<G, CMP_X, CMP_Y, P>;

unsafe fn init(
self,
operator_handle: mcpwm_oper_handle_t,
cmp_x: Option<&mut Comparator>,
cmp_y: Option<&mut Comparator>,
) -> Self::Gen {
let cfg = mcpwm_generator_config_t {
gen_gpio_num: generator_config.pin.pin(),
flags: todo!(),//generator_config.flags,
gen_gpio_num: self.pin.pin(),
flags: todo!(), //generator_config.flags,
};
let mut gen = ptr::null_mut();
unsafe {
Expand All @@ -133,7 +118,7 @@ where
mcpwm_gen_timer_event_action_t {
direction: mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_UP,
event: mcpwm_timer_event_t_MCPWM_TIMER_EVENT_EMPTY,
action: generator_config.on_is_empty.counting_up.into(),
action: self.on_is_empty.counting_up.into(),
}
))
.unwrap();
Expand All @@ -142,7 +127,7 @@ where
mcpwm_gen_timer_event_action_t {
direction: mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_DOWN,
event: mcpwm_timer_event_t_MCPWM_TIMER_EVENT_EMPTY,
action: generator_config.on_is_empty.counting_down.into(),
action: self.on_is_empty.counting_down.into(),
}
))
.unwrap();
Expand All @@ -151,7 +136,7 @@ where
mcpwm_gen_timer_event_action_t {
direction: mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_UP,
event: mcpwm_timer_event_t_MCPWM_TIMER_EVENT_FULL,
action: generator_config.on_is_full.counting_up.into(),
action: self.on_is_full.counting_up.into(),
}
))
.unwrap();
Expand All @@ -160,22 +145,49 @@ where
mcpwm_gen_timer_event_action_t {
direction: mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_DOWN,
event: mcpwm_timer_event_t_MCPWM_TIMER_EVENT_FULL,
action: generator_config.on_is_full.counting_down.into(),
action: self.on_is_full.counting_down.into(),
}
))
.unwrap();

cmp_x._configure(&mut *gen, generator_config.on_matches_cmp_x);
cmp_y._configure(&mut *gen, generator_config.on_matches_cmp_y);
if let Some(cmp_x) = cmp_x {
cmp_x.configure(&mut *gen, self.on_matches_cmp_x.to_counting_direction());
}

if let Some(cmp_y) = cmp_y {
cmp_y.configure(&mut *gen, self.on_matches_cmp_y.to_counting_direction());
}
}

Generator {
channel: PhantomData,
cmp_x: PhantomData,
cmp_y: PhantomData,
handle: gen,
pin: generator_config.pin,
pin: self.pin,
}
}
}

pub trait GenInit {
type Gen: OptionalGen;

/// This is only to be used internally by esp-idf-hal
unsafe fn init(self, operator_handle: mcpwm_oper_handle_t) -> Self::Gen;
}

impl<CMP_X, CMP_Y> GenInit for (&mut CMP_X, &mut CMP_Y, NoGenCfg)
where
CMP_X: OptionalCmp,
CMP_Y: OptionalCmp,
{
type Gen = NoGen;

unsafe fn init(self, operator_handle: mcpwm_oper_handle_t) -> Self::Gen {
NoGen
}
}

impl<G: GeneratorChannel, P> GeneratorConfig<G, CountingDirection, CountingDirection, P> {
pub fn active_high(pin: P) -> Self {
let mut result: Self = GeneratorConfig::empty(pin);
Expand Down Expand Up @@ -233,6 +245,10 @@ impl OnMatchCfg for NoCmpMatchConfig {
fn empty() -> Self {
NoCmpMatchConfig
}

fn to_counting_direction(self) -> CountingDirection {
CountingDirection::empty()
}
}

// TODO: Come up with better name
Expand All @@ -255,10 +271,15 @@ impl OnMatchCfg for CountingDirection {
fn empty() -> Self {
CountingDirection::empty()
}

fn to_counting_direction(self) -> CountingDirection {
self
}
}

pub trait OnMatchCfg {
fn empty() -> Self;
fn to_counting_direction(self) -> CountingDirection;
}

impl Into<mcpwm_generator_action_t> for GeneratorAction {
Expand Down
Loading

0 comments on commit 1944065

Please sign in to comment.