Skip to content

Commit 866a6c4

Browse files
aq1018burrbull
authored andcommitted
Allow center aligned pwm mode
1 parent e6e42f1 commit 866a6c4

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2121
- IrDA mode for USARTs [#761]
2222
- initial `SAI` support [#248]
2323
- initial `embedded-io` support [#725]
24+
- add `.set_cms()` and `CenterAlignedMode` enum for PWM. [#697]
2425

2526
### Changed
2627

@@ -38,6 +39,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3839

3940
[#248]: https://github.com/stm32-rs/stm32f4xx-hal/pull/248
4041
[#566]: https://github.com/stm32-rs/stm32f4xx-hal/pull/566
42+
[#697]: https://github.com/stm32-rs/stm32f4xx-hal/pull/697
4143
[#706]: https://github.com/stm32-rs/stm32f4xx-hal/pull/706
4244
[#725]: https://github.com/stm32-rs/stm32f4xx-hal/pull/725
4345
[#731]: https://github.com/stm32-rs/stm32f4xx-hal/pull/731

src/timer.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ pub enum Ocm {
306306
PwmMode2 = 7,
307307
}
308308

309+
// Center-aligned mode selection
310+
pub use pac::tim1::cr1::CMS as CenterAlignedMode;
311+
309312
/// Wrapper type that indicates which register of the contained timer to use for DMA.
310313
pub struct CCR<T, const C: u8>(T);
311314
pub type CCR1<T> = CCR<T, 0>;
@@ -317,7 +320,7 @@ pub type CCR4<T> = CCR<T, 3>;
317320
pub struct DMAR<T>(T);
318321

319322
mod sealed {
320-
use super::{BitFlags, Event, Flag, IdleState, Ocm, Polarity};
323+
use super::{BitFlags, CenterAlignedMode, Event, Flag, IdleState, Ocm, Polarity};
321324
pub trait General {
322325
type Width: Into<u32> + From<u16>;
323326
fn max_auto_reload() -> u32;
@@ -361,6 +364,7 @@ mod sealed {
361364
fn set_dtg_value(value: u8);
362365
fn read_dtg_value() -> u8;
363366
fn idle_state(channel: u8, comp: bool, s: IdleState);
367+
fn set_cms(mode: CenterAlignedMode);
364368
}
365369

366370
pub trait WithPwm: WithPwmCommon {
@@ -605,6 +609,11 @@ macro_rules! hal {
605609
}
606610
}
607611
}
612+
#[inline(always)]
613+
fn set_cms(cms: CenterAlignedMode) {
614+
let tim = unsafe { &*<$TIM>::ptr() };
615+
tim.cr1().write(|w| w.cms().variant(cms));
616+
}
608617
}
609618
)?
610619

src/timer/pwm.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
3838
use super::sealed::Split;
3939
use super::{
40-
compute_arr_presc, Advanced, CPin, FTimer, IdleState, Instance, NCPin, Ocm, Polarity, Timer,
41-
WithPwm,
40+
compute_arr_presc, Advanced, CPin, CenterAlignedMode, FTimer, IdleState, Instance, NCPin, Ocm,
41+
Polarity, Timer, WithPwm,
4242
};
4343
pub use super::{Ch, C1, C2, C3, C4};
4444
use crate::gpio::{OpenDrain, PushPull};
@@ -494,6 +494,14 @@ macro_rules! impl_advanced {
494494
pub fn get_dead_time_bits(&self) -> u8 {
495495
TIM::read_dtg_value()
496496
}
497+
498+
/// Sets the alignment mode
499+
#[inline]
500+
pub fn set_cms(&mut self, mode: CenterAlignedMode) {
501+
self.tim.enable_counter(false);
502+
TIM::set_cms(mode);
503+
self.tim.enable_counter(true);
504+
}
497505
};
498506
}
499507

0 commit comments

Comments
 (0)