Skip to content

Commit 4c4048c

Browse files
bors[bot]burrbull
andauthored
Merge #454
454: fix PwmExt r=therealprof a=burrbull Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents 2ad748e + 8507004 commit 4c4048c

File tree

7 files changed

+80
-77
lines changed

7 files changed

+80
-77
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Changed
1111

12+
- Make `Monotonic` implementation generic for `u32` timers, fix `PwmExt` trait [#454]
1213
- Rename `Delay<SYST>` to SysDelay, remove old `Delay<TIM2>`
1314
- Extend timers to 32bit on `Delay`
1415
- Move `MonoTimer` from `timer` to dwt mode [#448]
@@ -65,6 +66,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
6566
[#447]: https://github.com/stm32-rs/stm32f4xx-hal/pull/447
6667
[#448]: https://github.com/stm32-rs/stm32f4xx-hal/pull/448
6768
[#449]: https://github.com/stm32-rs/stm32f4xx-hal/pull/449
69+
[#454]: https://github.com/stm32-rs/stm32f4xx-hal/pull/454
6870

6971
### Changed
7072

examples/pwm-sinus.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn main() -> ! {
2020
let gpioa = dp.GPIOA.split();
2121
let channels = (gpioa.pa8.into_alternate(), gpioa.pa9.into_alternate());
2222

23-
let mut pwm = dp.TIM1.pwm_us(&clocks, channels, 100.micros());
23+
let mut pwm = dp.TIM1.pwm_us(channels, 100.micros(), &clocks);
2424
let mut counter = dp.TIM2.counter_us(&clocks);
2525
let max_duty = pwm.get_max_duty();
2626

examples/pwm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn main() -> ! {
1818
let gpioa = dp.GPIOA.split();
1919
let channels = (gpioa.pa8.into_alternate(), gpioa.pa9.into_alternate());
2020

21-
let pwm = dp.TIM1.pwm_hz(&clocks, channels, 20.kHz()).split();
21+
let pwm = dp.TIM1.pwm_hz(channels, 20.kHz(), &clocks).split();
2222
let (mut ch1, _ch2) = pwm;
2323
let max_duty = ch1.get_max_duty();
2424
ch1.set_duty(max_duty / 2);

src/prelude.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ pub use crate::spi::SpiExt as _stm32f4xx_hal_spi_SpiExt;
7373
pub use crate::syscfg::SysCfgExt as _stm32f4xx_hal_syscfg_SysCfgExt;
7474
pub use crate::time::U32Ext as _stm32f4xx_hal_time_U32Ext;
7575
#[cfg(feature = "rtic")]
76-
#[cfg(not(feature = "stm32f410"))]
7776
pub use crate::timer::MonoTimerExt as _stm32f4xx_hal_timer_MonoTimerExt;
7877
pub use crate::timer::PwmExt as _stm32f4xx_hal_timer_PwmExt;
7978
pub use crate::timer::SysTimerExt as _stm32f4xx_hal_timer_SysCounterExt;

src/timer.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,11 @@ mod pins;
2222
pub use pins::*;
2323
pub mod pwm;
2424
pub use pwm::*;
25-
#[cfg(not(feature = "stm32f410"))]
2625
pub mod pwm_input;
27-
#[cfg(not(feature = "stm32f410"))]
2826
pub use pwm_input::PwmInput;
2927
#[cfg(feature = "rtic")]
30-
#[cfg(not(feature = "stm32f410"))]
3128
pub mod monotonic;
3229
#[cfg(feature = "rtic")]
33-
#[cfg(not(feature = "stm32f410"))]
3430
pub use monotonic::*;
3531

3632
mod hal_02;
@@ -233,6 +229,7 @@ mod sealed {
233229
fn get_interrupt_flag(&self) -> Event;
234230
fn read_count(&self) -> Self::Width;
235231
fn start_one_pulse(&mut self);
232+
fn start_no_update(&mut self);
236233
fn cr1_reset(&mut self);
237234
}
238235

@@ -353,6 +350,10 @@ macro_rules! hal {
353350
self.cr1.write(|w| unsafe { w.bits(1 << 3) }.cen().set_bit());
354351
}
355352
#[inline(always)]
353+
fn start_no_update(&mut self) {
354+
self.cr1.write(|w| w.cen().set_bit().udis().set_bit());
355+
}
356+
#[inline(always)]
356357
fn cr1_reset(&mut self) {
357358
self.cr1.reset();
358359
}
@@ -726,7 +727,6 @@ impl<TIM: Instance + MasterTimer, const FREQ: u32> FTimer<TIM, FREQ> {
726727
pub(crate) const fn compute_arr_presc(freq: u32, clock: u32) -> (u16, u32) {
727728
let ticks = clock / freq;
728729
let psc = (ticks - 1) / (1 << 16);
729-
assert!(psc <= u16::MAX as u32);
730730
let arr = ticks / (psc + 1) - 1;
731731
(psc as u16, arr)
732732
}

src/timer/monotonic.rs

Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RTIC Monotonic impl for the 32-bit timers
2-
use super::{FTimer, Instance};
2+
use super::{Channel, Event, FTimer, General, Instance, WithPwm};
33
use crate::rcc::Clocks;
44
use core::ops::{Deref, DerefMut};
55
pub use fugit::{self, ExtU32};
@@ -39,64 +39,55 @@ pub trait MonoTimerExt: Sized {
3939
}
4040
}
4141

42-
macro_rules! mono {
43-
($($TIM:ty,)+) => {
44-
$(
45-
impl MonoTimerExt for $TIM {
46-
fn monotonic<const FREQ: u32>(self, clocks: &Clocks) -> MonoTimer<Self, FREQ> {
47-
FTimer::new(self, clocks).monotonic()
48-
}
49-
}
50-
51-
impl<const FREQ: u32> FTimer<$TIM, FREQ> {
52-
pub fn monotonic(self) -> MonoTimer<$TIM, FREQ> {
53-
MonoTimer::<$TIM, FREQ>::_new(self)
54-
}
55-
}
42+
impl<TIM> MonoTimerExt for TIM
43+
where
44+
Self: Instance + General<Width = u32> + WithPwm,
45+
{
46+
fn monotonic<const FREQ: u32>(self, clocks: &Clocks) -> MonoTimer<Self, FREQ> {
47+
FTimer::new(self, clocks).monotonic()
48+
}
49+
}
5650

57-
impl<const FREQ: u32> MonoTimer<$TIM, FREQ> {
58-
fn _new(timer: FTimer<$TIM, FREQ>) -> Self {
59-
timer.tim.arr.write(|w| unsafe { w.bits(u32::MAX) });
60-
timer.tim.egr.write(|w| w.ug().set_bit());
61-
timer.tim.sr.modify(|_, w| w.uif().clear_bit());
62-
timer.tim.cr1.modify(|_, w| w.cen().set_bit().udis().set_bit());
63-
Self(timer)
64-
}
65-
}
51+
impl<TIM, const FREQ: u32> FTimer<TIM, FREQ>
52+
where
53+
TIM: Instance + General<Width = u32> + WithPwm,
54+
{
55+
pub fn monotonic(mut self) -> MonoTimer<TIM, FREQ> {
56+
unsafe {
57+
self.tim.set_auto_reload_unchecked(TIM::max_auto_reload());
58+
}
59+
self.tim.trigger_update();
60+
self.tim.start_no_update();
61+
MonoTimer(self)
62+
}
63+
}
6664

67-
impl<const FREQ: u32> Monotonic for MonoTimer<$TIM, FREQ> {
68-
type Instant = fugit::TimerInstantU32<FREQ>;
69-
type Duration = fugit::TimerDurationU32<FREQ>;
65+
impl<TIM, const FREQ: u32> Monotonic for MonoTimer<TIM, FREQ>
66+
where
67+
TIM: Instance + General<Width = u32> + WithPwm,
68+
{
69+
type Instant = fugit::TimerInstantU32<FREQ>;
70+
type Duration = fugit::TimerDurationU32<FREQ>;
7071

71-
unsafe fn reset(&mut self) {
72-
self.tim.dier.modify(|_, w| w.cc1ie().set_bit());
73-
}
72+
unsafe fn reset(&mut self) {
73+
self.tim.listen_interrupt(Event::C1, true);
74+
}
7475

75-
#[inline(always)]
76-
fn now(&mut self) -> Self::Instant {
77-
Self::Instant::from_ticks(self.tim.cnt.read().cnt().bits())
78-
}
76+
#[inline(always)]
77+
fn now(&mut self) -> Self::Instant {
78+
Self::Instant::from_ticks(self.tim.read_count())
79+
}
7980

80-
fn set_compare(&mut self, instant: Self::Instant) {
81-
self.tim
82-
.ccr1
83-
.write(|w| w.ccr().bits(instant.duration_since_epoch().ticks()));
84-
}
81+
fn set_compare(&mut self, instant: Self::Instant) {
82+
TIM::set_cc_value(Channel::C1 as u8, instant.duration_since_epoch().ticks());
83+
}
8584

86-
fn clear_compare_flag(&mut self) {
87-
self.tim.sr.modify(|_, w| w.cc1if().clear_bit());
88-
}
85+
fn clear_compare_flag(&mut self) {
86+
self.tim.clear_interrupt_flag(Event::C1);
87+
}
8988

90-
#[inline(always)]
91-
fn zero() -> Self::Instant {
92-
Self::Instant::from_ticks(0)
93-
}
94-
}
95-
)+
89+
#[inline(always)]
90+
fn zero() -> Self::Instant {
91+
Self::Instant::from_ticks(0)
9692
}
9793
}
98-
99-
mono!(crate::pac::TIM5,);
100-
101-
#[cfg(feature = "tim2")]
102-
mono!(crate::pac::TIM2,);

src/timer/pwm.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,45 +90,56 @@ where
9090
{
9191
}
9292

93-
pub trait PwmExt<P, PINS>
93+
pub trait PwmExt
9494
where
9595
Self: Sized + Instance + WithPwm,
96-
PINS: Pins<Self, P>,
9796
{
98-
fn pwm<const FREQ: u32>(
97+
fn pwm<P, PINS, const FREQ: u32>(
9998
self,
100-
clocks: &Clocks,
10199
pins: PINS,
102100
time: TimerDurationU32<FREQ>,
103-
) -> Pwm<Self, P, PINS, FREQ>;
101+
clocks: &Clocks,
102+
) -> Pwm<Self, P, PINS, FREQ>
103+
where
104+
PINS: Pins<Self, P>;
104105

105-
fn pwm_hz(self, clocks: &Clocks, pins: PINS, freq: Hertz) -> PwmHz<Self, P, PINS>;
106+
fn pwm_hz<P, PINS>(self, pins: PINS, freq: Hertz, clocks: &Clocks) -> PwmHz<Self, P, PINS>
107+
where
108+
PINS: Pins<Self, P>;
106109

107-
fn pwm_us(
110+
fn pwm_us<P, PINS>(
108111
self,
109-
clocks: &Clocks,
110112
pins: PINS,
111113
time: TimerDurationU32<1_000_000>,
112-
) -> Pwm<Self, P, PINS, 1_000_000> {
113-
self.pwm::<1_000_000>(clocks, pins, time)
114+
clocks: &Clocks,
115+
) -> Pwm<Self, P, PINS, 1_000_000>
116+
where
117+
PINS: Pins<Self, P>,
118+
{
119+
self.pwm::<_, _, 1_000_000>(pins, time, clocks)
114120
}
115121
}
116122

117-
impl<TIM, P, PINS> PwmExt<P, PINS> for TIM
123+
impl<TIM> PwmExt for TIM
118124
where
119125
Self: Sized + Instance + WithPwm,
120-
PINS: Pins<Self, P>,
121126
{
122-
fn pwm<const FREQ: u32>(
127+
fn pwm<P, PINS, const FREQ: u32>(
123128
self,
124-
clocks: &Clocks,
125129
pins: PINS,
126130
time: TimerDurationU32<FREQ>,
127-
) -> Pwm<TIM, P, PINS, FREQ> {
131+
clocks: &Clocks,
132+
) -> Pwm<TIM, P, PINS, FREQ>
133+
where
134+
PINS: Pins<Self, P>,
135+
{
128136
FTimer::<Self, FREQ>::new(self, clocks).pwm(pins, time)
129137
}
130138

131-
fn pwm_hz(self, clocks: &Clocks, pins: PINS, time: Hertz) -> PwmHz<TIM, P, PINS> {
139+
fn pwm_hz<P, PINS>(self, pins: PINS, time: Hertz, clocks: &Clocks) -> PwmHz<TIM, P, PINS>
140+
where
141+
PINS: Pins<Self, P>,
142+
{
132143
Timer::new(self, clocks).pwm_hz(pins, time)
133144
}
134145
}

0 commit comments

Comments
 (0)