Skip to content

Commit 8ab1f8f

Browse files
bors[bot]burrbull
andauthored
Merge #439
439: inherent Pwm r=therealprof a=burrbull Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents 77a6700 + b83ea56 commit 8ab1f8f

File tree

5 files changed

+106
-32
lines changed

5 files changed

+106
-32
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+
- Add inherent impl of `embedded_hal::Pwm` methods on `Pwm`s [#439]
1213
- Use `embedded-dma` v0.2 [#440]
1314
- Add LSI support for `Rtc` [#438]
1415
- Use `time` for `Rtc` instead of `rtcc`, add `rtc` example [#436]
@@ -45,6 +46,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
4546
[#434]: https://github.com/stm32-rs/stm32f4xx-hal/pull/434
4647
[#436]: https://github.com/stm32-rs/stm32f4xx-hal/pull/436
4748
[#438]: https://github.com/stm32-rs/stm32f4xx-hal/pull/438
49+
[#439]: https://github.com/stm32-rs/stm32f4xx-hal/pull/439
4850
[#440]: https://github.com/stm32-rs/stm32f4xx-hal/pull/440
4951

5052
### Changed

src/fugit/hal_02.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{Counter, Delay, Error, Instance, SysCounter};
1+
use super::{Channel, Counter, Delay, Error, Instance, Pins, Pwm, SysCounter, WithPwm};
22

33
use embedded_hal::blocking::delay::{DelayMs, DelayUs};
44
use fugit::{ExtU32, MicrosDurationU32, TimerDurationU32};
@@ -86,3 +86,45 @@ impl Cancel for SysCounter {
8686
self.cancel()
8787
}
8888
}
89+
90+
impl<TIM, P, PINS, const FREQ: u32> embedded_hal::Pwm for Pwm<TIM, P, PINS, FREQ>
91+
where
92+
TIM: Instance + WithPwm,
93+
PINS: Pins<TIM, P>,
94+
{
95+
type Channel = Channel;
96+
type Duty = u16;
97+
type Time = TimerDurationU32<FREQ>;
98+
99+
fn enable(&mut self, channel: Self::Channel) {
100+
self.enable(channel)
101+
}
102+
103+
fn disable(&mut self, channel: Self::Channel) {
104+
self.disable(channel)
105+
}
106+
107+
fn get_duty(&self, channel: Self::Channel) -> Self::Duty {
108+
self.get_duty(channel)
109+
}
110+
111+
fn set_duty(&mut self, channel: Self::Channel, duty: Self::Duty) {
112+
self.set_duty(channel, duty)
113+
}
114+
115+
/// If `0` returned means max_duty is 2^16
116+
fn get_max_duty(&self) -> Self::Duty {
117+
self.get_max_duty()
118+
}
119+
120+
fn get_period(&self) -> Self::Time {
121+
self.get_period()
122+
}
123+
124+
fn set_period<T>(&mut self, period: T)
125+
where
126+
T: Into<Self::Time>,
127+
{
128+
self.set_period(period.into())
129+
}
130+
}

src/fugit/pwm.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,44 +135,37 @@ impl<TIM: Instance + WithPwm, const FREQ: u32> Timer<TIM, FREQ> {
135135
}
136136
}
137137

138-
impl<TIM, P, PINS, const FREQ: u32> embedded_hal::Pwm for Pwm<TIM, P, PINS, FREQ>
138+
impl<TIM, P, PINS, const FREQ: u32> Pwm<TIM, P, PINS, FREQ>
139139
where
140140
TIM: Instance + WithPwm,
141141
PINS: Pins<TIM, P>,
142142
{
143-
type Channel = Channel;
144-
type Duty = u16;
145-
type Time = TimerDurationU32<FREQ>;
146-
147-
fn enable(&mut self, channel: Self::Channel) {
143+
pub fn enable(&mut self, channel: Channel) {
148144
TIM::enable_channel(PINS::check_used(channel) as u8, true)
149145
}
150146

151-
fn disable(&mut self, channel: Self::Channel) {
147+
pub fn disable(&mut self, channel: Channel) {
152148
TIM::enable_channel(PINS::check_used(channel) as u8, false)
153149
}
154150

155-
fn get_duty(&self, channel: Self::Channel) -> Self::Duty {
151+
pub fn get_duty(&self, channel: Channel) -> u16 {
156152
TIM::read_cc_value(PINS::check_used(channel) as u8) as u16
157153
}
158154

159-
fn set_duty(&mut self, channel: Self::Channel, duty: Self::Duty) {
155+
pub fn set_duty(&mut self, channel: Channel, duty: u16) {
160156
TIM::set_cc_value(PINS::check_used(channel) as u8, duty.into())
161157
}
162158

163159
/// If `0` returned means max_duty is 2^16
164-
fn get_max_duty(&self) -> Self::Duty {
160+
pub fn get_max_duty(&self) -> u16 {
165161
(TIM::read_auto_reload() as u16).wrapping_add(1)
166162
}
167163

168-
fn get_period(&self) -> Self::Time {
169-
Self::Time::from_ticks(TIM::read_auto_reload() + 1)
164+
pub fn get_period(&self) -> TimerDurationU32<FREQ> {
165+
TimerDurationU32::from_ticks(TIM::read_auto_reload() + 1)
170166
}
171167

172-
fn set_period<T>(&mut self, period: T)
173-
where
174-
T: Into<Self::Time>,
175-
{
176-
self.tim.set_auto_reload(period.into().ticks() - 1).unwrap();
168+
pub fn set_period(&mut self, period: TimerDurationU32<FREQ>) {
169+
self.tim.set_auto_reload(period.ticks() - 1).unwrap();
177170
}
178171
}

src/prelude.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ pub use embedded_hal::watchdog::Watchdog as _embedded_hal_watchdog_Watchdog;
5151
pub use embedded_hal::watchdog::WatchdogDisable as _embedded_hal_watchdog_WatchdogDisable;
5252
pub use embedded_hal::watchdog::WatchdogEnable as _embedded_hal_watchdog_WatchdogEnable;
5353
pub use embedded_hal::Capture as _embedded_hal_Capture;
54-
pub use embedded_hal::Pwm as _embedded_hal_Pwm;
5554
pub use embedded_hal::Qei as _embedded_hal_Qei;
5655
pub use fugit::ExtU32 as _fugit_ExtU32;
5756

src/pwm.rs

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -209,37 +209,33 @@ where
209209
}
210210
}
211211

212-
impl<TIM, P, PINS> embedded_hal::Pwm for Pwm<TIM, P, PINS>
212+
impl<TIM, P, PINS> Pwm<TIM, P, PINS>
213213
where
214214
TIM: Instance + WithPwm,
215215
PINS: Pins<TIM, P>,
216216
{
217-
type Channel = Channel;
218-
type Duty = u16;
219-
type Time = Hertz;
220-
221-
fn enable(&mut self, channel: Self::Channel) {
217+
pub fn enable(&mut self, channel: Channel) {
222218
TIM::enable_channel(PINS::check_used(channel) as u8, true)
223219
}
224220

225-
fn disable(&mut self, channel: Self::Channel) {
221+
pub fn disable(&mut self, channel: Channel) {
226222
TIM::enable_channel(PINS::check_used(channel) as u8, false)
227223
}
228224

229-
fn get_duty(&self, channel: Self::Channel) -> Self::Duty {
225+
pub fn get_duty(&self, channel: Channel) -> u16 {
230226
TIM::read_cc_value(PINS::check_used(channel) as u8) as u16
231227
}
232228

233-
fn set_duty(&mut self, channel: Self::Channel, duty: Self::Duty) {
229+
pub fn set_duty(&mut self, channel: Channel, duty: u16) {
234230
TIM::set_cc_value(PINS::check_used(channel) as u8, duty as u32)
235231
}
236232

237233
/// If `0` returned means max_duty is 2^16
238-
fn get_max_duty(&self) -> Self::Duty {
234+
pub fn get_max_duty(&self) -> u16 {
239235
(TIM::read_auto_reload() as u16).wrapping_add(1)
240236
}
241237

242-
fn get_period(&self) -> Self::Time {
238+
pub fn get_period(&self) -> Hertz {
243239
let clk = self.clk;
244240
let psc = self.tim.read_prescaler() as u32;
245241
let arr = TIM::read_auto_reload();
@@ -248,9 +244,9 @@ where
248244
(clk.0 / (psc * arr)).hz()
249245
}
250246

251-
fn set_period<T>(&mut self, period: T)
247+
pub fn set_period<T>(&mut self, period: T)
252248
where
253-
T: Into<Self::Time>,
249+
T: Into<Hertz>,
254250
{
255251
let clk = self.clk;
256252

@@ -259,3 +255,45 @@ where
259255
self.tim.set_auto_reload(arr).unwrap();
260256
}
261257
}
258+
259+
impl<TIM, P, PINS> embedded_hal::Pwm for Pwm<TIM, P, PINS>
260+
where
261+
TIM: Instance + WithPwm,
262+
PINS: Pins<TIM, P>,
263+
{
264+
type Channel = Channel;
265+
type Duty = u16;
266+
type Time = Hertz;
267+
268+
fn enable(&mut self, channel: Self::Channel) {
269+
self.enable(channel)
270+
}
271+
272+
fn disable(&mut self, channel: Self::Channel) {
273+
self.disable(channel)
274+
}
275+
276+
fn get_duty(&self, channel: Self::Channel) -> Self::Duty {
277+
self.get_duty(channel)
278+
}
279+
280+
fn set_duty(&mut self, channel: Self::Channel, duty: Self::Duty) {
281+
self.set_duty(channel, duty)
282+
}
283+
284+
/// If `0` returned means max_duty is 2^16
285+
fn get_max_duty(&self) -> Self::Duty {
286+
self.get_max_duty()
287+
}
288+
289+
fn get_period(&self) -> Self::Time {
290+
self.get_period()
291+
}
292+
293+
fn set_period<T>(&mut self, period: T)
294+
where
295+
T: Into<Self::Time>,
296+
{
297+
self.set_period(period)
298+
}
299+
}

0 commit comments

Comments
 (0)