Skip to content

Commit f120915

Browse files
committed
opm dynamic period change
1 parent 9347fb6 commit f120915

File tree

2 files changed

+42
-39
lines changed

2 files changed

+42
-39
lines changed

examples/opm.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,19 @@ const APP: () = {
4040
let opm = ctx.device.TIM3.opm(4.ms(), &mut rcc);
4141

4242
let mut opm_ch1 = opm.bind_pin(gpioa.pa6);
43-
opm_ch1.enable();
44-
4543
let mut opm_ch2 = opm.bind_pin(gpioa.pa7);
46-
opm_ch2.set_delay(1.ms());
47-
opm_ch2.enable();
48-
4944
let mut opm_ch3 = opm.bind_pin(gpiob.pb0);
50-
opm_ch3.set_delay(2.ms());
51-
opm_ch3.enable();
52-
5345
let mut opm_ch4 = opm.bind_pin(gpiob.pb1);
54-
opm_ch4.set_delay(3.ms());
46+
47+
let max_delay = opm_ch2.get_max_delay();
48+
49+
opm_ch2.set_delay(max_delay / 2);
50+
opm_ch3.set_delay(max_delay / 4);
51+
opm_ch4.set_delay(max_delay / 8);
52+
53+
opm_ch1.enable();
54+
opm_ch2.enable();
55+
opm_ch3.enable();
5556
opm_ch4.enable();
5657

5758
init::LateResources { opm, exti, led }

src/timer/opm.rs

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ pub trait OpmExt: Sized {
1414
pub struct OpmPin<TIM, CH> {
1515
tim: PhantomData<TIM>,
1616
channel: PhantomData<CH>,
17-
clk: Hertz,
18-
delay: MicroSecond,
17+
delay: u32,
1918
}
2019

2120
pub struct Opm<TIM> {
@@ -32,8 +31,7 @@ impl<TIM> Opm<TIM> {
3231
OpmPin {
3332
tim: PhantomData,
3433
channel: PhantomData,
35-
clk: self.clk,
36-
delay: 0.ms(),
34+
delay: 1,
3735
}
3836
}
3937
}
@@ -42,34 +40,39 @@ macro_rules! opm {
4240
($($TIMX:ident: ($timX:ident, $arr:ident $(,$arr_h:ident)*),)+) => {
4341
$(
4442
impl OpmExt for $TIMX {
45-
fn opm(self, period: MicroSecond, rcc: &mut Rcc) -> Opm<Self> {
46-
$timX(self, period, rcc)
43+
fn opm(self, pulse: MicroSecond, rcc: &mut Rcc) -> Opm<Self> {
44+
$timX(self, pulse, rcc)
4745
}
4846
}
4947

50-
fn $timX(tim: $TIMX, period: MicroSecond, rcc: &mut Rcc) -> Opm<$TIMX> {
48+
fn $timX(_tim: $TIMX, pulse: MicroSecond, rcc: &mut Rcc) -> Opm<$TIMX> {
5149
$TIMX::enable(rcc);
5250
$TIMX::reset(rcc);
5351

54-
let cycles_per_period = rcc.clocks.apb_tim_clk / period.into();
55-
let psc = (cycles_per_period - 1) / 0xffff;
56-
tim.psc.write(|w| unsafe { w.psc().bits(psc as u16) });
57-
58-
let freq = (rcc.clocks.apb_tim_clk.0 / (psc + 1)).hz();
59-
let reload = period.cycles(freq);
60-
unsafe {
61-
tim.arr.write(|w| w.$arr().bits(reload as u16));
62-
$(
63-
tim.arr.modify(|_, w| w.$arr_h().bits((reload >> 16) as u16));
64-
)*
65-
}
66-
Opm {
67-
clk: freq,
52+
let mut opm = Opm::<$TIMX> {
53+
clk: rcc.clocks.apb_tim_clk,
6854
tim: PhantomData,
69-
}
55+
};
56+
opm.set_pulse(pulse);
57+
opm
7058
}
7159

7260
impl Opm<$TIMX> {
61+
pub fn set_pulse(&mut self, pulse: MicroSecond) {
62+
let cycles_per_period = self.clk / pulse.into();
63+
let psc = (cycles_per_period - 1) / 0xffff;
64+
let freq = (self.clk.0 / (psc + 1)).hz();
65+
let reload = pulse.cycles(freq);
66+
unsafe {
67+
let tim = &*$TIMX::ptr();
68+
tim.psc.write(|w| w.psc().bits(psc as u16));
69+
tim.arr.write(|w| w.$arr().bits(reload as u16));
70+
$(
71+
tim.arr.modify(|_, w| w.$arr_h().bits((reload >> 16) as u16));
72+
)*
73+
}
74+
}
75+
7376
pub fn generate(&mut self) {
7477
let tim = unsafe {&*$TIMX::ptr()};
7578
tim.cr1.write(|w| w.opm().set_bit().cen().set_bit());
@@ -96,20 +99,19 @@ macro_rules! opm_hal {
9699
tim.ccer.modify(|_, w| w.$ccxe().clear_bit());
97100
}
98101

99-
pub fn set_delay(&mut self, delay: MicroSecond) {
102+
pub fn get_max_delay(&mut self) -> u32 {
103+
unsafe { (*$TIMX::ptr()).arr.read().bits() }
104+
}
105+
106+
pub fn set_delay(&mut self, delay: u32) {
100107
self.delay = delay;
101108
self.setup();
102109
}
103110

104111
fn setup(&mut self) {
105-
let tim = unsafe {&*$TIMX::ptr()};
106-
let compare = if self.delay.0 > 0 {
107-
self.delay.cycles(self.clk)
108-
} else {
109-
1
110-
};
111112
unsafe {
112-
tim.$ccrx.write(|w| w.bits(compare));
113+
let tim = &*$TIMX::ptr();
114+
tim.$ccrx.write(|w| w.bits(self.delay));
113115
tim.$ccmrx_output().modify(|_, w| w.$ocxm().bits(7).$ocxfe().set_bit());
114116
}
115117
}

0 commit comments

Comments
 (0)