Skip to content

Commit f633054

Browse files
committed
init channels after Pwm
1 parent 8563062 commit f633054

File tree

10 files changed

+832
-877
lines changed

10 files changed

+832
-877
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1919
- Temporary replace `stm32f1` with `stm32f1-staging` [#503]
2020
- `Spi` now takes `Option<PIN>` for `SCK`, `MISO`, `MOSI` [#514]
2121
- move `Qei` mod inside `pwm_input` mod [#516]
22+
- move pin connecting to timer channels after `Pwm` initialization
2223

2324
### Changed
2425

examples/pwm.rs

+29-54
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ use panic_halt as _;
99

1010
use cortex_m::asm;
1111
use cortex_m_rt::entry;
12-
use stm32f1xx_hal::{
13-
pac,
14-
prelude::*,
15-
time::ms,
16-
timer::{Channel, Tim2NoRemap},
17-
};
12+
use stm32f1xx_hal::{pac, prelude::*, time::ms};
1813

1914
#[entry]
2015
fn main() -> ! {
@@ -25,90 +20,70 @@ fn main() -> ! {
2520

2621
let clocks = rcc.cfgr.freeze(&mut flash.acr);
2722

28-
let mut afio = p.AFIO.constrain();
29-
30-
let mut gpioa = p.GPIOA.split();
31-
// let mut gpiob = p.GPIOB.split();
23+
let gpioa = p.GPIOA.split();
24+
// let gpiob = p.GPIOB.split();
3225

3326
// TIM2
34-
let c1 = gpioa.pa0.into_alternate_push_pull(&mut gpioa.crl);
35-
let c2 = gpioa.pa1.into_alternate_push_pull(&mut gpioa.crl);
36-
let c3 = gpioa.pa2.into_alternate_push_pull(&mut gpioa.crl);
27+
let c1 = gpioa.pa0;
28+
let c2 = gpioa.pa1;
29+
let c3 = gpioa.pa2;
3730
// If you don't want to use all channels, just leave some out
38-
// let c4 = gpioa.pa3.into_alternate_push_pull(&mut gpioa.crl);
39-
let pins = (c1, c2, c3);
31+
// let c4 = gpioa.pa3;
4032

4133
// TIM3
42-
// let c1 = gpioa.pa6.into_alternate_push_pull(&mut gpioa.crl);
43-
// let c2 = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl);
44-
// let c3 = gpiob.pb0.into_alternate_push_pull(&mut gpiob.crl);
45-
// let c4 = gpiob.pb1.into_alternate_push_pull(&mut gpiob.crl);
34+
// let c1 = gpioa.pa6;
35+
// let c2 = gpioa.pa7;
36+
// let c3 = gpiob.pb0;
37+
// let c4 = gpiob.pb1;
4638

4739
// TIM4 (Only available with the "medium" density feature)
48-
// let c1 = gpiob.pb6.into_alternate_push_pull(&mut gpiob.crl);
49-
// let c2 = gpiob.pb7.into_alternate_push_pull(&mut gpiob.crl);
50-
// let c3 = gpiob.pb8.into_alternate_push_pull(&mut gpiob.crh);
51-
// let c4 = gpiob.pb9.into_alternate_push_pull(&mut gpiob.crh);
40+
// let c1 = gpiob.pb6;
41+
// let c2 = gpiob.pb7;
42+
// let c3 = gpiob.pb8;
43+
// let c4 = gpiob.pb9;
5244

5345
//let mut pwm =
54-
// Timer::new(p.TIM2, &clocks).pwm_hz::<Tim2NoRemap, _, _>(pins, &mut afio.mapr, 1.kHz());
46+
// Timer::new(p.TIM2, &clocks).pwm_hz(pins, 1.kHz());
5547
// or
56-
let mut pwm = p
57-
.TIM2
58-
.pwm_hz::<Tim2NoRemap, _, _>(pins, &mut afio.mapr, 1.kHz(), &clocks);
48+
let (mut pwm_mgr, (pwm_c1, pwm_c2, pwm_c3, ..)) = p.TIM2.pwm_hz(1.kHz(), &clocks);
5949

6050
// Enable clock on each of the channels
61-
pwm.enable(Channel::C1);
62-
pwm.enable(Channel::C2);
63-
pwm.enable(Channel::C3);
51+
let mut c1 = pwm_c1.with(c1);
52+
c1.enable();
53+
let mut c2 = pwm_c2.with(c2);
54+
c2.enable();
55+
let mut c3 = pwm_c3.with(c3);
56+
c3.enable();
6457

6558
//// Operations affecting all defined channels on the Timer
6659

6760
// Adjust period to 0.5 seconds
68-
pwm.set_period(ms(500).into_rate());
61+
pwm_mgr.set_period(ms(500).into_rate());
6962

7063
asm::bkpt();
7164

7265
// Return to the original frequency
73-
pwm.set_period(1.kHz());
66+
pwm_mgr.set_period(1.kHz());
7467

7568
asm::bkpt();
7669

77-
let max = pwm.get_max_duty();
70+
let max = pwm_mgr.get_max_duty();
7871

7972
//// Operations affecting single channels can be accessed through
8073
//// the Pwm object or via dereferencing to the pin.
8174

8275
// Use the Pwm object to set C3 to full strength
83-
pwm.set_duty(Channel::C3, max);
76+
c3.set_duty(max);
8477

8578
asm::bkpt();
8679

8780
// Use the Pwm object to set C3 to be dim
88-
pwm.set_duty(Channel::C3, max / 4);
81+
c3.set_duty(max / 4);
8982

9083
asm::bkpt();
9184

9285
// Use the Pwm object to set C3 to be zero
93-
pwm.set_duty(Channel::C3, 0);
94-
95-
asm::bkpt();
96-
97-
// Extract the PwmChannel for C3
98-
let mut pwm_channel = pwm.split().2;
99-
100-
// Use the PwmChannel object to set C3 to be full strength
101-
pwm_channel.set_duty(max);
102-
103-
asm::bkpt();
104-
105-
// Use the PwmChannel object to set C3 to be dim
106-
pwm_channel.set_duty(max / 4);
107-
108-
asm::bkpt();
109-
110-
// Use the PwmChannel object to set C3 to be zero
111-
pwm_channel.set_duty(0);
86+
c3.set_duty(0);
11287

11388
asm::bkpt();
11489

examples/pwm_custom.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use panic_halt as _;
99

1010
use cortex_m::asm;
11-
use stm32f1xx_hal::{pac, prelude::*, timer::Timer};
11+
use stm32f1xx_hal::{pac, prelude::*};
1212

1313
use cortex_m_rt::entry;
1414

@@ -30,30 +30,31 @@ fn main() -> ! {
3030
let p0 = pb4.into_alternate_push_pull(&mut gpiob.crl);
3131
let p1 = gpiob.pb5.into_alternate_push_pull(&mut gpiob.crl);
3232

33-
let pwm = Timer::new(p.TIM3, &clocks).pwm_hz((p0, p1), &mut afio.mapr, 1.kHz());
33+
let (pwm, pwm_channels) = p.TIM3.remap(&mut afio.mapr).pwm_hz(1.kHz(), &clocks);
3434

3535
let max = pwm.get_max_duty();
3636

37-
let mut pwm_channels = pwm.split();
37+
let mut c1 = pwm_channels.0.with(p0);
38+
let mut c2 = pwm_channels.1.with(p1);
3839

3940
// Enable the individual channels
40-
pwm_channels.0.enable();
41-
pwm_channels.1.enable();
41+
c1.enable();
42+
c2.enable();
4243

4344
// full
44-
pwm_channels.0.set_duty(max);
45-
pwm_channels.1.set_duty(max);
45+
c1.set_duty(max);
46+
c2.set_duty(max);
4647

4748
asm::bkpt();
4849

4950
// dim
50-
pwm_channels.1.set_duty(max / 4);
51+
c2.set_duty(max / 4);
5152

5253
asm::bkpt();
5354

5455
// zero
55-
pwm_channels.0.set_duty(0);
56-
pwm_channels.1.set_duty(0);
56+
c1.set_duty(0);
57+
c2.set_duty(0);
5758

5859
asm::bkpt();
5960

src/lib.rs

+17
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,20 @@ mod sealed {
157157
pub trait Sealed {}
158158
}
159159
use sealed::Sealed;
160+
161+
pub trait Steal {
162+
/// Steal an instance of this peripheral
163+
///
164+
/// # Safety
165+
///
166+
/// Ensure that the new instance of the peripheral cannot be used in a way
167+
/// that may race with any existing instances, for example by only
168+
/// accessing read-only or write-only registers, or by consuming the
169+
/// original peripheral and using critical sections to coordinate
170+
/// access between multiple new instances.
171+
///
172+
/// Additionally the HAL may rely on only one
173+
/// peripheral instance existing to ensure memory safety; ensure
174+
/// no stolen instances are passed to such software.
175+
unsafe fn steal() -> Self;
176+
}

0 commit comments

Comments
 (0)