Skip to content

Commit 5630d99

Browse files
committed
Update PWM example with latest pwm changes
1 parent 5dd8f0d commit 5630d99

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

examples/pwm.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use cortex_m::asm;
1010
use stm32f1xx_hal::{
1111
prelude::*,
1212
pac,
13-
timer::Timer,
13+
timer::{Tim2NoRemap, Timer},
1414
};
1515
use cortex_m_rt::entry;
1616

@@ -25,14 +25,16 @@ fn main() -> ! {
2525

2626
let mut afio = p.AFIO.constrain(&mut rcc.apb2);
2727

28-
// let mut gpioa = p.GPIOA.split(&mut rcc.apb2);
29-
let mut gpiob = p.GPIOB.split(&mut rcc.apb2);
28+
let mut gpioa = p.GPIOA.split(&mut rcc.apb2);
29+
// let mut gpiob = p.GPIOB.split(&mut rcc.apb2);
3030

3131
// TIM2
3232
let c1 = gpioa.pa0.into_alternate_push_pull(&mut gpioa.crl);
3333
let c2 = gpioa.pa1.into_alternate_push_pull(&mut gpioa.crl);
3434
let c3 = gpioa.pa2.into_alternate_push_pull(&mut gpioa.crl);
35-
let c4 = gpioa.pa3.into_alternate_push_pull(&mut gpioa.crl);
35+
// If you don't want to use all channels, just leave some out
36+
// let c4 = gpioa.pa3.into_alternate_push_pull(&mut gpioa.crl);
37+
let pins = (c1, c2, c3);
3638

3739
// TIM3
3840
// let c1 = gpioa.pa6.into_alternate_push_pull(&mut gpioa.crl);
@@ -46,9 +48,9 @@ fn main() -> ! {
4648
// let c3 = gpiob.pb8.into_alternate_push_pull(&mut gpiob.crh);
4749
// let c4 = gpiob.pb9.into_alternate_push_pull(&mut gpiob.crh);
4850

49-
let mut pwm = Timer::tim4(p.TIM4, &clocks, &mut rcc.apb1)
50-
.pwm((c1, c2, c3, c4), &mut afio.mapr, 1.khz())
51-
.3;
51+
let mut pwm = Timer::tim2(p.TIM2, &clocks, &mut rcc.apb1)
52+
.pwm::<Tim2NoRemap, _, _, _>(pins, &mut afio.mapr, 1.khz())
53+
.2;
5254

5355
let max = pwm.get_max_duty();
5456

src/pwm.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,24 @@
1313
1414
```rust
1515
let gpioa = ..; // Set up and split GPIOA
16+
// Select the pins you want to use
1617
let pins = (
1718
gpioa.pa0.into_alternate_push_pull(&mut gpioa.crl),
1819
gpioa.pa1.into_alternate_push_pull(&mut gpioa.crl),
1920
gpioa.pa2.into_alternate_push_pull(&mut gpioa.crl),
2021
gpioa.pa3.into_alternate_push_pull(&mut gpioa.crl),
2122
);
23+
24+
// Set up the timer as a PWM output. Since there are multiple remap
25+
// options for tim2 that use the same pins, you need to specify
26+
// the remap generic parameter.
27+
let (c1, c2, c3, c4) = Timer::tim2(p.TIM2, &clocks, &mut rcc.apb1)
28+
.pwm::<Tim2NoRemap, _, _, _>(pins, &mut afio.mapr, 1.khz())
29+
.3;
30+
31+
// Start using the channels
32+
c1.set_duty(c1.get_max_duty());
33+
// ...
2234
```
2335
2436
Then call the `pwm` function on the corresponding timer.

src/qei.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
//! # Quadrature Encoder Interface
1+
/**
2+
# Quadrature Encoder Interface
3+
4+
NOTE: In some cases you need to specify remap you need, especially for TIM2
5+
(see [Alternate function remapping](super::timer)):
6+
*/
7+
28
use core::u16;
39

410
use core::marker::PhantomData;

src/timer.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
44
## Alternate function remapping
55
6+
This is a list of the remap settings you can use to assign pins to PWM channels
7+
and the QEI peripherals
8+
69
### TIM1
710
811
Not available on STM32F101.

0 commit comments

Comments
 (0)