Skip to content

Commit c7a7350

Browse files
authored
Merge pull request #171 from JanekGraff/tim16-pwm
Add PWM standalone output on complementary PWM Channels
2 parents 73b81e6 + 91e287b commit c7a7350

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1616
- Provide getters to serial status flags idle/txe/rxne/tc.
1717
- Provide ability to reset timer UIF interrupt flag
1818
- PWM complementary output capability for TIM1 with new example to demonstrate
19+
- PWM output on complementary channels only for single channel timers (TIM16 + TIM17)
1920

2021
### Fixed
2122

src/pwm.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ pins_impl!(
7575
(P2), (PinC2), (C2);
7676
(P3), (PinC3), (C3);
7777
(P4), (PinC4), (C4);
78+
(P1N), (PinC1N), (C1N);
79+
(P2N), (PinC2N), (C2N);
80+
(P3N), (PinC3N), (C3N);
7881
);
7982

8083
impl<TIM, P1: PinC1<TIM>, P2: PinC1<TIM>> PinC1<TIM> for (P1, P2) {}
@@ -803,7 +806,7 @@ macro_rules! pwm_1_channel_with_complementary_outputs {
803806
rcc.regs.$apbrstr.modify(|_, w| w.$timXrst().set_bit());
804807
rcc.regs.$apbrstr.modify(|_, w| w.$timXrst().clear_bit());
805808

806-
if PINS::C1 {
809+
if PINS::C1 || PINS::C1N {
807810
tim.ccmr1_output().modify(|_, w| w.oc1pe().set_bit().oc1m().bits(6));
808811
}
809812

@@ -868,6 +871,35 @@ macro_rules! pwm_1_channel_with_complementary_outputs {
868871
unsafe { (*$TIMX::ptr()).ccr1.write(|w| w.ccr().bits(duty.into())) }
869872
}
870873
}
874+
875+
impl hal::PwmPin for PwmChannels<$TIMX, C1N> {
876+
type Duty = u16;
877+
878+
//NOTE(unsafe) atomic write with no side effects
879+
fn disable(&mut self) {
880+
unsafe { (*($TIMX::ptr())).ccer.modify(|_, w| w.cc1ne().clear_bit()) };
881+
}
882+
883+
//NOTE(unsafe) atomic write with no side effects
884+
fn enable(&mut self) {
885+
unsafe { (*($TIMX::ptr())).ccer.modify(|_, w| w.cc1ne().set_bit()) };
886+
}
887+
888+
//NOTE(unsafe) atomic read with no side effects
889+
fn get_duty(&self) -> u16 {
890+
unsafe { (*$TIMX::ptr()).ccr1.read().ccr().bits() as u16 }
891+
}
892+
893+
//NOTE(unsafe) atomic read with no side effects
894+
fn get_max_duty(&self) -> u16 {
895+
unsafe { (*$TIMX::ptr()).arr.read().arr().bits() as u16 }
896+
}
897+
898+
//NOTE(unsafe) atomic write with no side effects
899+
fn set_duty(&mut self, duty: u16) {
900+
unsafe { (*$TIMX::ptr()).ccr1.write(|w| w.ccr().bits(duty.into())) }
901+
}
902+
}
871903
)+
872904
};
873905
}

src/timers.rs

+1
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ channel_impl!(
327327

328328
TIM16, PinC1, PA6, Alternate<AF5>;
329329
TIM16, PinC1, PB8, Alternate<AF2>;
330+
TIM16, PinC1N, PB6, Alternate<AF2>;
330331

331332
TIM17, PinC1, PA7, Alternate<AF5>;
332333
TIM17, PinC1, PB9, Alternate<AF2>;

0 commit comments

Comments
 (0)