Skip to content

Commit d006750

Browse files
burrbullTheZoq2
authored andcommitted
Pwm remap: another try (#147)
* implement remap for Timers when pwm is used move Remap to timer.rs, add gpio::Mode
1 parent ffd4663 commit d006750

File tree

7 files changed

+380
-276
lines changed

7 files changed

+380
-276
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Added
1111

12+
- Added `Mode` marker trait for `gpio` pins that correspondent to pin mode.
1213
- RCC `Bus` trait + private `Enable` and `Reset` traits
1314
- Added `micros_since` and `reset` methods to timer
1415
- Added `select_frequency` method to RTC
@@ -19,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1920

2021
### Breaking changes
2122

23+
- Implement more pin combinations for PWM configuration, added PWM for TIM1 (API for custom PWM pins was removed as it's no more needed)
2224
- Bump `stm32f1` dependency (`0.9.0`)
2325
- `void::Void` replaced with `Infallible` where it is possible
2426
- Change timer/pwm init API

examples/pwm_custom.rs

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,13 @@ use panic_halt as _;
88

99
use cortex_m::asm;
1010
use stm32f1xx_hal::{
11-
gpio::gpiob::{PB4, PB5},
12-
gpio::{Alternate, PushPull},
13-
pac::{self,TIM3},
11+
pac,
1412
prelude::*,
15-
pwm::{Pins, Pwm, C1, C2},
1613
timer::Timer,
1714
};
1815

1916
use cortex_m_rt::entry;
2017

21-
// Using PB5 channel for TIM3 PWM output
22-
// struct MyChannels(PB5<Alternate<PushPull>>);
23-
// impl Pins<TIM3> for MyChannels {
24-
// const REMAP: u8 = 0b10;
25-
// const C1: bool = false;
26-
// const C2: bool = true;
27-
// const C3: bool = false;
28-
// const C4: bool = false;
29-
// type Channels = Pwm<TIM3, C2>;
30-
// }
31-
32-
// Using PB4 and PB5 channels for TIM3 PWM output
33-
struct MyChannels(PB4<Alternate<PushPull>>, PB5<Alternate<PushPull>>);
34-
impl Pins<TIM3> for MyChannels {
35-
const REMAP: u8 = 0b10;
36-
const C1: bool = true;
37-
const C2: bool = true;
38-
const C3: bool = false;
39-
const C4: bool = false;
40-
type Channels = (Pwm<TIM3, C1>, Pwm<TIM3, C2>);
41-
}
42-
4318
#[entry]
4419
fn main() -> ! {
4520
let p = pac::Peripherals::take().unwrap();
@@ -59,11 +34,7 @@ fn main() -> ! {
5934
let p1 = gpiob.pb5.into_alternate_push_pull(&mut gpiob.crl);
6035

6136
let mut pwm = Timer::tim3(p.TIM3, &clocks, &mut rcc.apb1)
62-
.pwm(
63-
MyChannels(p0, p1),
64-
&mut afio.mapr,
65-
1.khz(),
66-
);
37+
.pwm((p0, p1), &mut afio.mapr, 1.khz());
6738

6839
let max = pwm.0.get_max_duty();
6940

src/gpio.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ pub trait GpioExt {
2727
fn split(self, apb2: &mut APB2) -> Self::Parts;
2828
}
2929

30+
31+
/// Marker trait for pin mode detection.
32+
pub trait Mode<MODE> {}
33+
3034
/// Marker trait for active states.
3135
pub trait Active {}
3236

@@ -95,7 +99,8 @@ macro_rules! gpio {
9599
State,
96100
Active,
97101
Debugger,
98-
Pxx
102+
Pxx,
103+
Mode,
99104
};
100105

101106
/// GPIO parts
@@ -216,13 +221,17 @@ macro_rules! gpio {
216221
pub type $PXx<MODE> = Pxx<MODE>;
217222

218223

224+
impl<MODE> Mode<MODE> for Generic<MODE> {}
225+
219226

220227
$(
221228
/// Pin
222229
pub struct $PXi<MODE> {
223230
_mode: PhantomData<MODE>,
224231
}
225232

233+
impl<MODE> Mode<MODE> for $PXi<MODE> {}
234+
226235
impl $PXi<Debugger> {
227236
/// Put the pin in an active state. The caller
228237
/// must enforce that the pin is really in this

0 commit comments

Comments
 (0)