Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ fugit = "0.3.7"
stm32-usbd = { version = "0.7.0", optional = true }
fixed = { version = "1.28.0", optional = true }
embedded-io = "0.6"
stm32-hrtim = { version = "0.1.0", optional = true }
#stm32-hrtim = { version = "0.1.0", optional = true }
stm32-hrtim = { git = "https://github.com/usbalbin/stm32-hrtim", branch = "simplify", optional = true }
rand_core = { version = "0.9", default-features = false }

[dependencies.cortex-m]
Expand Down
5 changes: 3 additions & 2 deletions examples/hrtim/adc-trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ fn main() -> ! {
mut cr1,
mut cr3,
mut cr4,
out: (mut out1, mut out2),
mut out1,
mut out2,
..
} = dp
.HRTIM_TIMA
.pwm_advanced((pin_a, pin_b))
.pwm_advanced(pin_a, pin_b)
.prescaler(prescaler)
.period(period)
.finalize(&mut hr_control);
Expand Down
6 changes: 3 additions & 3 deletions examples/hrtim/capture-dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use stm32_hrtim::{
capture,
compare_register::HrCompareRegister,
external_event::{self, ToExternalEventSource},
output::HrOutput,
output::{self, HrOutput},
timer::{HrSlaveTimerCpt, HrTimer, TimerSplitCapture},
HrParts, HrPwmAdvExt, Pscl128,
};
Expand Down Expand Up @@ -86,12 +86,12 @@ fn main() -> ! {
let HrParts {
timer,
mut cr1,
out: mut out1,
mut out1,
dma_channel,
..
} = dp
.HRTIM_TIMA
.pwm_advanced(pin_a)
.pwm_advanced(pin_a, output::NoPin)
.prescaler(prescaler)
.period(period)
.finalize(&mut hr_control);
Expand Down
12 changes: 6 additions & 6 deletions examples/hrtim/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use stm32_hrtim::{
capture::HrCapture,
compare_register::HrCompareRegister,
external_event::{self, ToExternalEventSource},
output::HrOutput,
output::{self, HrOutput},
timer::{HrSlaveTimerCpt, HrTimer},
HrParts, HrPwmAdvExt, Pscl128,
};
Expand Down Expand Up @@ -81,21 +81,21 @@ fn main() -> ! {
let HrParts {
mut timer,
mut cr1,
mut out,
mut out1,
..
} = dp
.HRTIM_TIMA
.pwm_advanced(pin_a)
.pwm_advanced(pin_a, output::NoPin)
.prescaler(prescaler)
.period(period)
.finalize(&mut hr_control);

out.enable_rst_event(&cr1); // Set low on compare match with cr1
out.enable_set_event(&timer); // Set high at new period
out1.enable_rst_event(&cr1); // Set low on compare match with cr1
out1.enable_set_event(&timer); // Set high at new period

cr1.set_duty(period / 2);
timer.start(&mut hr_control.control);
out.enable();
out1.enable();

let capture = timer.capture_ch1();
capture.enable_interrupt(true, &mut hr_control);
Expand Down
5 changes: 3 additions & 2 deletions examples/hrtim/deadtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ fn main() -> ! {
let HrParts {
mut timer,
mut cr1,
out: (mut out1, mut out2),
mut out1,
mut out2,
..
} = dp
.HRTIM_TIMA
.pwm_advanced((pin_a, pin_b))
.pwm_advanced(pin_a, pin_b)
.prescaler(prescaler)
.period(period)
.deadtime(deadtime)
Expand Down
14 changes: 7 additions & 7 deletions examples/hrtim/eev-comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use cortex_m_rt::entry;
use stm32_hrtim::{
compare_register::HrCompareRegister,
external_event::{self, ToExternalEventSource},
output::HrOutput,
output::{self, HrOutput},
timer::HrTimer,
timer_eev_cfg::{EevCfg, EevCfgs},
HrParts, HrPwmAdvExt, Polarity, Pscl4,
Expand Down Expand Up @@ -109,22 +109,22 @@ fn main() -> ! {
let HrParts {
mut timer,
mut cr1,
mut out,
mut out1,
..
} = dp
.HRTIM_TIMA
.pwm_advanced(pin_a)
.pwm_advanced(pin_a, output::NoPin)
.prescaler(prescaler)
.eev_cfg(EevCfgs::default().eev4(EevCfg::default()))
.period(0xFFFF)
.finalize(&mut hr_control);

out.enable_rst_event(&cr1); // Set low on compare match with cr1
out.enable_rst_event(&eev_input4);
out.enable_set_event(&timer); // Set high at new period
out1.enable_rst_event(&cr1); // Set low on compare match with cr1
out1.enable_rst_event(&eev_input4);
out1.enable_set_event(&timer); // Set high at new period
cr1.set_duty(timer.get_period() / 3);

out.enable();
out1.enable();
timer.start(&mut hr_control.control);

info!("Started");
Expand Down
14 changes: 7 additions & 7 deletions examples/hrtim/eev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use cortex_m_rt::entry;
use stm32_hrtim::{
compare_register::HrCompareRegister,
external_event::{self, ToExternalEventSource},
output::HrOutput,
output::{self, HrOutput},
timer::HrTimer,
timer_eev_cfg::EevCfgs,
HrParts, HrPwmAdvExt, Polarity, Pscl4,
Expand Down Expand Up @@ -81,22 +81,22 @@ fn main() -> ! {
let HrParts {
mut timer,
mut cr1,
mut out,
mut out1,
..
} = dp
.HRTIM_TIMA
.pwm_advanced(pin_a)
.pwm_advanced(pin_a, output::NoPin)
.prescaler(prescaler)
.eev_cfg(EevCfgs::default())
.period(0xFFFF)
.finalize(&mut hr_control);

out.enable_rst_event(&cr1); // Set low on compare match with cr1
out.enable_rst_event(&eev_input3);
out.enable_set_event(&timer); // Set high at new period
out1.enable_rst_event(&cr1); // Set low on compare match with cr1
out1.enable_rst_event(&eev_input3);
out1.enable_set_event(&timer); // Set high at new period
cr1.set_duty(timer.get_period() / 3);

out.enable();
out1.enable();
timer.start(&mut hr_control.control);

info!("Started");
Expand Down
6 changes: 3 additions & 3 deletions examples/hrtim/flt-comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use fugit::ExtU32 as _;
use stm32_hrtim::{
compare_register::HrCompareRegister,
fault::{FaultAction, FaultMonitor},
output::HrOutput,
output::{self, HrOutput},
timer::HrTimer,
HrParts, HrPwmAdvExt, Polarity, Pscl4,
};
Expand Down Expand Up @@ -111,11 +111,11 @@ fn main() -> ! {
let HrParts {
mut timer,
mut cr1,
out: mut out1,
mut out1,
..
} = dp
.HRTIM_TIMA
.pwm_advanced(pin_a)
.pwm_advanced(pin_a, output::NoPin)
.prescaler(prescaler)
.period(0xFFFF)
.with_fault_source(fault_source5) // Set fault source
Expand Down
16 changes: 8 additions & 8 deletions examples/hrtim/flt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use cortex_m_rt::entry;
use stm32_hrtim::{
compare_register::HrCompareRegister,
fault::{FaultAction, FaultMonitor},
output::HrOutput,
output::{self, HrOutput},
timer::HrTimer,
HrParts, HrPwmAdvExt, Polarity, Pscl4,
};
Expand Down Expand Up @@ -80,35 +80,35 @@ fn main() -> ! {
let HrParts {
mut timer,
mut cr1,
mut out,
mut out1,
..
} = dp
.HRTIM_TIMA
.pwm_advanced(pin_a)
.pwm_advanced(pin_a, output::NoPin)
.prescaler(prescaler)
.period(0xFFFF)
.with_fault_source(fault_source3)
.fault_action1(FaultAction::ForceInactive)
.fault_action2(FaultAction::ForceInactive)
.finalize(&mut hr_control);

out.enable_rst_event(&cr1); // Set low on compare match with cr1
out.enable_set_event(&timer); // Set high at new period
out1.enable_rst_event(&cr1); // Set low on compare match with cr1
out1.enable_set_event(&timer); // Set high at new period
cr1.set_duty(timer.get_period() / 3);

out.enable();
out1.enable();
timer.start(&mut hr_control.control);

info!("Started");

loop {
for _ in 0..5 {
delay.delay(500_u32.millis());
info!("State: {:?}", out.get_state());
info!("State: {:?}", out1.get_state());
}
if hr_control.fault_3.is_fault_active() {
hr_control.fault_3.clear_fault(); // Clear fault every 5s
out.enable();
out1.enable();
info!("failt cleared, and output reenabled");
}
}
Expand Down
5 changes: 3 additions & 2 deletions examples/hrtim/hrtim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ fn main() -> ! {
let HrParts {
mut timer,
mut cr1,
out: (mut out1, mut out2),
mut out1,
mut out2,
..
} = dp
.HRTIM_TIMA
.pwm_advanced((pin_a, pin_b))
.pwm_advanced(pin_a, pin_b)
.prescaler(prescaler)
.period(0xFFFF)
.push_pull_mode(true) // Set push pull mode, out1 and out2 are
Expand Down
9 changes: 5 additions & 4 deletions examples/hrtim/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use utils::logger::info;
use cortex_m_rt::entry;
use stm32_hrtim::{
compare_register::HrCompareRegister,
output::HrOutput,
output::{self, HrOutput},
timer::{HrSlaveTimer, HrTimer},
HrParts, HrPwmAdvExt, HrTimerMode, MasterPreloadSource, PreloadSource, Pscl4,
};
Expand Down Expand Up @@ -69,11 +69,12 @@ fn main() -> ! {
let HrParts {
mut timer,
mut cr1,
out: (mut out1, mut out2),
mut out1,
mut out2,
..
} = dp
.HRTIM_TIMA
.pwm_advanced((pin_a, pin_b))
.pwm_advanced(pin_a, pin_b)
.prescaler(prescaler)
.push_pull_mode(true) // Set push pull mode, out1 and out2 are
// alternated every period with one being
Expand All @@ -89,7 +90,7 @@ fn main() -> ! {
..
} = dp
.HRTIM_MASTER
.pwm_advanced(())
.pwm_advanced(output::NoPin, output::NoPin)
.prescaler(prescaler)
.preload(MasterPreloadSource::OnMasterRepetitionUpdate)
.period(0xFFFF)
Expand Down
56 changes: 29 additions & 27 deletions src/hrtim/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
};
use stm32_hrtim::{
control::{HrPwmControl, HrTimOngoingCalibration},
output::{HrOut1, HrOut2, ToHrOut},
output::{self, Output1Pin, Output2Pin},
HrParts, HrPwmBuilder,
};

Expand All @@ -33,20 +33,21 @@ impl HrControltExt for crate::stm32::HRTIM_COMMON {
}
}

pub trait HrPwmBuilderExt<TIM, PSCL, PINS: ToHrOut<TIM>> {
fn finalize(self, control: &mut HrPwmControl) -> HrParts<TIM, PSCL, PINS::Out<PSCL>>;
pub trait HrPwmBuilderExt<TIM, PSCL, P1: Output1Pin<TIM>, P2: Output2Pin<TIM>> {
fn finalize(self, control: &mut HrPwmControl) -> HrParts<TIM, PSCL>;
}
macro_rules! impl_finalize {
($($TIMX:ident),+) => {$(
impl<PSCL: stm32_hrtim::HrtimPrescaler, PINS: HrtimPin<$TIMX>> HrPwmBuilderExt<$TIMX, PSCL, PINS>
for HrPwmBuilder<$TIMX, PSCL, stm32_hrtim::PreloadSource, PINS>
impl<PSCL: stm32_hrtim::HrtimPrescaler, P1: Out1Pin<$TIMX>, P2: Out2Pin<$TIMX>> HrPwmBuilderExt<$TIMX, PSCL, P1, P2>
for HrPwmBuilder<$TIMX, PSCL, stm32_hrtim::PreloadSource, P1, P2>
{
fn finalize(
self,
control: &mut HrPwmControl,
) -> HrParts<$TIMX, PSCL, <PINS as ToHrOut<$TIMX>>::Out<PSCL>> {
let pins = self._init(control);
pins.connect_to_hrtim();
) -> HrParts<$TIMX, PSCL> {
let (pin1, pin2) = self._init(control);
pin1.connect_to_hrtim();
pin2.connect_to_hrtim();
unsafe { MaybeUninit::uninit().assume_init() }
}
}
Expand Down Expand Up @@ -74,30 +75,31 @@ use gpio::{

use gpio::gpioc::{PC6, PC7};

pub trait HrtimPin<TIM>: ToHrOut<TIM> {
/// Implemented for types that can be used as output1 for HRTIM timer instances
pub trait Out1Pin<TIM>: Output1Pin<TIM> {
/// Connect pin to hrtim timer
fn connect_to_hrtim(self);
}

impl<TIM, PA, PB> HrtimPin<TIM> for (PA, PB)
where
PA: HrtimPin<TIM>,
PB: HrtimPin<TIM>,
{
fn connect_to_hrtim(self) {
self.0.connect_to_hrtim();
self.1.connect_to_hrtim();
}
/// Implemented for types that can be used as output2 for HRTIM timer instances
pub trait Out2Pin<TIM>: Output2Pin<TIM> {
/// Connect pin to hrtim timer
fn connect_to_hrtim(self);
}

macro_rules! pins_helper {
($TIMX:ty, $HrOutY:ident, $CHY:ident<$CHY_AF:literal>) => {
//impl sealed::Sealed<$TIMX> for $CHY<GpioInputMode> {}
impl<TIM> Out1Pin<TIM> for output::NoPin {
fn connect_to_hrtim(self) {}
}

unsafe impl ToHrOut<$TIMX> for $CHY<gpio::DefaultMode> {
type Out<PSCL> = $HrOutY<$TIMX, PSCL>;
}
impl<TIM> Out2Pin<TIM> for output::NoPin {
fn connect_to_hrtim(self) {}
}

macro_rules! pins_helper {
($TIMX:ty, $OutYPin:ident, $OutputYPin:ident, $HrOutY:ident, $CHY:ident<$CHY_AF:literal>) => {
unsafe impl $OutputYPin<$TIMX> for $CHY<gpio::DefaultMode> {}

impl HrtimPin<$TIMX> for $CHY<gpio::DefaultMode> {
impl $OutYPin<$TIMX> for $CHY<gpio::DefaultMode> {
// Pin<Gpio, Index, Alternate<PushPull, AF>>
fn connect_to_hrtim(self) {
let _: $CHY<gpio::Alternate<{ $CHY_AF }>> = self.into_alternate();
Expand All @@ -108,8 +110,8 @@ macro_rules! pins_helper {

macro_rules! pins {
($($TIMX:ty: CH1: $CH1:ident<$CH1_AF:literal>, CH2: $CH2:ident<$CH2_AF:literal>),+) => {$(
pins_helper!($TIMX, HrOut1, $CH1<$CH1_AF>);
pins_helper!($TIMX, HrOut2, $CH2<$CH2_AF>);
pins_helper!($TIMX, Out1Pin, Output1Pin, HrOut1, $CH1<$CH1_AF>);
pins_helper!($TIMX, Out2Pin, Output2Pin, HrOut2, $CH2<$CH2_AF>);
)+};
}

Expand Down
Loading
Loading