Skip to content

Commit b27ec08

Browse files
authored
Merge pull request #137 from korken89/tim15_fix
Fix for TIM15 PWM, has different Alternate Function, prepared for Ch2 when available in PAC
2 parents eb4f436 + 01e3224 commit b27ec08

File tree

1 file changed

+54
-37
lines changed

1 file changed

+54
-37
lines changed

src/pwm.rs

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::stm32::{TIM1, TIM15, TIM2};
88

99
use crate::gpio::gpioa::{PA0, PA1, PA10, PA11, PA2, PA3, PA8, PA9};
1010
use crate::gpio::gpiob::PB14;
11-
use crate::gpio::{Alternate, AlternateOD, Floating, Input, Output, PushPull, AF1};
11+
use crate::gpio::{Alternate, AlternateOD, Floating, Input, Output, PushPull, AF1, AF14};
1212
use crate::rcc::{Clocks, APB1R1, APB2};
1313
use crate::time::Hertz;
1414

@@ -23,17 +23,17 @@ pub trait Pins<TIM> {
2323
}
2424

2525
macro_rules! pins_to_channels_mapping {
26-
( $( $TIMX:ident: ( $($PINX:ident),+ ), ( $($ENCHX:ident),* ); )+ ) => {
26+
( $( $TIMX:ident: ( $($PINX:ident),+ ), ( $($ENCHX:ident),+ ), ( $($AF:ident),+ ); )+ ) => {
2727
$(
2828
#[allow(unused_parens)]
29-
impl Pins<$TIMX> for ($($PINX<Alternate<AF1, Output<PushPull>>>),+)
29+
impl Pins<$TIMX> for ($($PINX<Alternate<$AF, Output<PushPull>>>),+)
3030
{
3131
$(const $ENCHX: bool = true;)+
3232
type Channels = ($(Pwm<$TIMX, $ENCHX>),+);
3333
}
3434

3535
#[allow(unused_parens)]
36-
impl Pins<$TIMX> for ($($PINX<AlternateOD<AF1, Input<Floating>>>),+)
36+
impl Pins<$TIMX> for ($($PINX<AlternateOD<$AF, Input<Floating>>>),+)
3737
{
3838
$(const $ENCHX: bool = true;)+
3939
type Channels = ($(Pwm<$TIMX, $ENCHX>),+);
@@ -44,41 +44,48 @@ macro_rules! pins_to_channels_mapping {
4444

4545
pins_to_channels_mapping! {
4646
// TIM1
47-
TIM1: (PA8, PA9, PA10, PA11), (C1, C2, C3, C4);
48-
TIM1: (PA9, PA10, PA11), (C2, C3, C4);
49-
TIM1: (PA8, PA10, PA11), (C1, C3, C4);
50-
TIM1: (PA8, PA9, PA11), (C1, C2, C4);
51-
TIM1: (PA8, PA9, PA10), (C1, C2, C3);
52-
TIM1: (PA10, PA11), (C3, C4);
53-
TIM1: (PA9, PA11), (C2, C4);
54-
TIM1: (PA9, PA10), (C2, C3);
55-
TIM1: (PA8, PA11), (C1, C4);
56-
TIM1: (PA8, PA10), (C1, C3);
57-
TIM1: (PA8, PA9), (C1, C2);
58-
TIM1: (PA8), (C1);
59-
TIM1: (PA9), (C2);
60-
TIM1: (PA10), (C3);
61-
TIM1: (PA11), (C4);
47+
TIM1: (PA8, PA9, PA10, PA11), (C1, C2, C3, C4), (AF1, AF1, AF1, AF1);
48+
TIM1: (PA9, PA10, PA11), (C2, C3, C4), (AF1, AF1, AF1);
49+
TIM1: (PA8, PA10, PA11), (C1, C3, C4), (AF1, AF1, AF1);
50+
TIM1: (PA8, PA9, PA11), (C1, C2, C4), (AF1, AF1, AF1);
51+
TIM1: (PA8, PA9, PA10), (C1, C2, C3), (AF1, AF1, AF1);
52+
TIM1: (PA10, PA11), (C3, C4), (AF1, AF1);
53+
TIM1: (PA9, PA11), (C2, C4), (AF1, AF1);
54+
TIM1: (PA9, PA10), (C2, C3), (AF1, AF1);
55+
TIM1: (PA8, PA11), (C1, C4), (AF1, AF1);
56+
TIM1: (PA8, PA10), (C1, C3), (AF1, AF1);
57+
TIM1: (PA8, PA9), (C1, C2), (AF1, AF1);
58+
TIM1: (PA8), (C1), (AF1);
59+
TIM1: (PA9), (C2), (AF1);
60+
TIM1: (PA10), (C3), (AF1);
61+
TIM1: (PA11), (C4), (AF1);
6262

6363
// TIM2
64-
TIM2: (PA0, PA1, PA2, PA3), (C1, C2, C3, C4);
65-
TIM2: (PA1, PA2, PA3), (C2, C3, C4);
66-
TIM2: (PA0, PA2, PA3), (C1, C3, C4);
67-
TIM2: (PA0, PA1, PA3), (C1, C2, C4);
68-
TIM2: (PA0, PA1, PA2), (C1, C2, C3);
69-
TIM2: (PA2, PA3), (C3, C4);
70-
TIM2: (PA1, PA3), (C2, C4);
71-
TIM2: (PA1, PA2), (C2, C3);
72-
TIM2: (PA0, PA3), (C1, C4);
73-
TIM2: (PA0, PA2), (C1, C3);
74-
TIM2: (PA0, PA1), (C1, C2);
75-
TIM2: (PA0), (C1);
76-
TIM2: (PA1), (C2);
77-
TIM2: (PA2), (C3);
78-
TIM2: (PA3), (C4);
79-
80-
// TIM15
81-
TIM15: (PB14), (C1);
64+
TIM2: (PA0, PA1, PA2, PA3), (C1, C2, C3, C4), (AF1, AF1, AF1, AF1);
65+
TIM2: (PA1, PA2, PA3), (C2, C3, C4), (AF1, AF1, AF1);
66+
TIM2: (PA0, PA2, PA3), (C1, C3, C4), (AF1, AF1, AF1);
67+
TIM2: (PA0, PA1, PA3), (C1, C2, C4), (AF1, AF1, AF1);
68+
TIM2: (PA0, PA1, PA2), (C1, C2, C3), (AF1, AF1, AF1);
69+
TIM2: (PA2, PA3), (C3, C4), (AF1, AF1);
70+
TIM2: (PA1, PA3), (C2, C4), (AF1, AF1);
71+
TIM2: (PA1, PA2), (C2, C3), (AF1, AF1);
72+
TIM2: (PA0, PA3), (C1, C4), (AF1, AF1);
73+
TIM2: (PA0, PA2), (C1, C3), (AF1, AF1);
74+
TIM2: (PA0, PA1), (C1, C2), (AF1, AF1);
75+
TIM2: (PA0), (C1), (AF1);
76+
TIM2: (PA1), (C2), (AF1);
77+
TIM2: (PA2), (C3), (AF1);
78+
TIM2: (PA3), (C4), (AF1);
79+
80+
// TIM15 - TODO: The uncommented lines are awaiting PAC updates to be valid.
81+
TIM15: (PB14), (C1), (AF14);
82+
// TIM15: (PB15), (C2), (AF14);
83+
TIM15: (PA2), (C1), (AF14);
84+
// TIM15: (PA3), (C2), (AF14);
85+
// TIM15: (PB14, PB15), (C1, C2), (AF14, AF14);
86+
// TIM15: (PB14, PA3), (C1, C2), (AF14, AF14);
87+
// TIM15: (PA2, PB15), (C1, C2), (AF14, AF14);
88+
// TIM15: (PA2, PA3), (C1, C2), (AF14, AF14);
8289
}
8390

8491
pub trait PwmExt1: Sized {
@@ -304,6 +311,11 @@ macro_rules! small_timer {
304311
tim.ccmr1_output().modify(|_, w| unsafe { w.oc1pe().set_bit().oc1m().bits(6) });
305312
}
306313

314+
// TODO: The uncommented lines are awaiting PAC updates to be valid.
315+
// if PINS::C2 {
316+
// tim.ccmr1_output().modify(|_, w| unsafe { w.oc2pe().set_bit().oc2m().bits(6) });
317+
// }
318+
307319
let clk = clocks.pclk1().0;
308320
let freq = freq.0;
309321
let ticks = clk / freq;
@@ -314,6 +326,9 @@ macro_rules! small_timer {
314326
let arr = ticks / (psc + 1);
315327
unsafe { tim.arr.write(|w| { w.arr().bits(arr as $arr_width) }); }
316328

329+
tim.bdtr.write(|w| w.moe().set_bit());
330+
tim.egr.write(|w| w.ug().set_bit());
331+
317332
tim.cr1.write(|w| {
318333
w.opm().clear_bit()
319334
.cen().set_bit()
@@ -325,6 +340,8 @@ macro_rules! small_timer {
325340

326341
pwm_channels! {
327342
$TIMX: (C1, $arr_width, cc1e, ccr1, ccr1),
343+
// TODO: The uncommented line is awaiting PAC updates to be valid.
344+
// (C2, $arr_width, cc2e, ccr2, ccr2),
328345
}
329346

330347
)+

0 commit comments

Comments
 (0)