Skip to content

Commit b2cecee

Browse files
committed
InPins
1 parent 9f903c8 commit b2cecee

File tree

3 files changed

+122
-37
lines changed

3 files changed

+122
-37
lines changed

src/qei.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use embedded_hal_02 as hal;
99
pub use hal::Direction;
1010

1111
use crate::rcc::Clocks;
12-
use crate::timer::{InputPins, Timer};
12+
use crate::timer::{InPins, InputPins, Timer};
1313

1414
/// SMS (Slave Mode Selection) register
1515
#[derive(Copy, Clone, Debug)]
@@ -58,13 +58,13 @@ impl Default for QeiOptions {
5858

5959
pub struct Qei<TIM: InputPins> {
6060
tim: TIM,
61-
pins: TIM::Channels12,
61+
pins: (TIM::InCh1, TIM::InCh2),
6262
}
6363

6464
pub trait QeiExt: Sized + InputPins {
6565
fn qei(
6666
self,
67-
pins: impl Into<<Self as InputPins>::Channels12>,
67+
pins: impl Into<InPins<Self::InCh1, Self::InCh2>>,
6868
options: QeiOptions,
6969
clocks: &Clocks,
7070
) -> Qei<Self>;
@@ -74,7 +74,7 @@ pub trait QeiExt: Sized + InputPins {
7474
impl QeiExt for pac::TIM1 {
7575
fn qei(
7676
self,
77-
pins: impl Into<<Self as InputPins>::Channels12>,
77+
pins: impl Into<InPins<Self::InCh1, Self::InCh2>>,
7878
options: QeiOptions,
7979
clocks: &Clocks,
8080
) -> Qei<Self> {
@@ -86,7 +86,7 @@ impl QeiExt for pac::TIM1 {
8686
impl Timer<pac::TIM1> {
8787
pub fn qei(
8888
self,
89-
pins: impl Into<<pac::TIM1 as InputPins>::Channels12>,
89+
pins: impl Into<InPins<<pac::TIM1 as InputPins>::InCh1, <pac::TIM1 as InputPins>::InCh2>>,
9090
options: QeiOptions,
9191
) -> Qei<pac::TIM1> {
9292
let Self { tim, clk: _ } = self;
@@ -97,7 +97,7 @@ impl Timer<pac::TIM1> {
9797
impl QeiExt for pac::TIM2 {
9898
fn qei(
9999
self,
100-
pins: impl Into<<Self as InputPins>::Channels12>,
100+
pins: impl Into<InPins<Self::InCh1, Self::InCh2>>,
101101
options: QeiOptions,
102102
clocks: &Clocks,
103103
) -> Qei<Self> {
@@ -108,7 +108,7 @@ impl QeiExt for pac::TIM2 {
108108
impl Timer<pac::TIM2> {
109109
pub fn qei(
110110
self,
111-
pins: impl Into<<pac::TIM2 as InputPins>::Channels12>,
111+
pins: impl Into<InPins<<pac::TIM2 as InputPins>::InCh1, <pac::TIM2 as InputPins>::InCh2>>,
112112
options: QeiOptions,
113113
) -> Qei<pac::TIM2> {
114114
let Self { tim, clk: _ } = self;
@@ -119,7 +119,7 @@ impl Timer<pac::TIM2> {
119119
impl QeiExt for pac::TIM3 {
120120
fn qei(
121121
self,
122-
pins: impl Into<<Self as InputPins>::Channels12>,
122+
pins: impl Into<InPins<Self::InCh1, Self::InCh2>>,
123123
options: QeiOptions,
124124
clocks: &Clocks,
125125
) -> Qei<Self> {
@@ -130,7 +130,7 @@ impl QeiExt for pac::TIM3 {
130130
impl Timer<pac::TIM3> {
131131
pub fn qei(
132132
self,
133-
pins: impl Into<<pac::TIM3 as InputPins>::Channels12>,
133+
pins: impl Into<InPins<<pac::TIM3 as InputPins>::InCh1, <pac::TIM3 as InputPins>::InCh2>>,
134134
options: QeiOptions,
135135
) -> Qei<pac::TIM3> {
136136
let Self { tim, clk: _ } = self;
@@ -142,7 +142,7 @@ impl Timer<pac::TIM3> {
142142
impl QeiExt for pac::TIM4 {
143143
fn qei(
144144
self,
145-
pins: impl Into<<Self as InputPins>::Channels12>,
145+
pins: impl Into<InPins<Self::InCh1, Self::InCh2>>,
146146
options: QeiOptions,
147147
clocks: &Clocks,
148148
) -> Qei<Self> {
@@ -154,7 +154,7 @@ impl QeiExt for pac::TIM4 {
154154
impl Timer<pac::TIM4> {
155155
pub fn qei(
156156
self,
157-
pins: impl Into<<pac::TIM4 as InputPins>::Channels12>,
157+
pins: impl Into<InPins<<pac::TIM4 as InputPins>::InCh1, <pac::TIM4 as InputPins>::InCh2>>,
158158
options: QeiOptions,
159159
) -> Qei<pac::TIM4> {
160160
let Self { tim, clk: _ } = self;
@@ -167,7 +167,7 @@ macro_rules! hal {
167167
impl Qei<$TIMX> {
168168
fn $timX(
169169
tim: $TIMX,
170-
pins: impl Into<<$TIMX as InputPins>::Channels12>,
170+
pins: impl Into<InPins<<$TIMX as InputPins>::InCh1, <$TIMX as InputPins>::InCh2>>,
171171
options: QeiOptions,
172172
) -> Self {
173173
let pins = pins.into();
@@ -189,10 +189,18 @@ macro_rules! hal {
189189
tim.arr().write(|w| w.arr().set(options.auto_reload_value));
190190
tim.cr1().write(|w| w.cen().set_bit());
191191

192-
Qei { tim, pins }
192+
Qei {
193+
tim,
194+
pins: (pins.c1, pins.c2),
195+
}
193196
}
194197

195-
pub fn release(self) -> ($TIMX, <$TIMX as InputPins>::Channels12) {
198+
pub fn release(
199+
self,
200+
) -> (
201+
$TIMX,
202+
(<$TIMX as InputPins>::InCh1, <$TIMX as InputPins>::InCh2),
203+
) {
196204
(self.tim, self.pins)
197205
}
198206
}

src/timer/pins.rs

Lines changed: 89 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ pub const C2: u8 = 1;
77
pub const C3: u8 = 2;
88
pub const C4: u8 = 3;
99

10+
pub struct InPins<CH1, CH2> {
11+
pub c1: CH1,
12+
pub c2: CH2,
13+
}
14+
15+
impl<CH1, CH2> From<(CH1, CH2)> for InPins<CH1, CH2> {
16+
fn from(value: (CH1, CH2)) -> Self {
17+
Self {
18+
c1: value.0,
19+
c2: value.1,
20+
}
21+
}
22+
}
23+
1024
#[cfg(any(feature = "stm32f100", feature = "stm32f103", feature = "connectivity"))]
1125
pub mod tim1 {
1226
use super::*;
@@ -35,6 +49,17 @@ pub mod tim1 {
3549
Full, PE13, PE14 => MAPR { |_, w| unsafe { w.tim1_remap().bits(0b11)} };
3650
]
3751
}
52+
53+
out_enums! {
54+
Ch1: PA8, PE9;
55+
Ch2: PA9, PE11;
56+
Ch3: PA10, PE13;
57+
Ch4: PA11, PE14;
58+
}
59+
in_enums! {
60+
InCh1: PA8, PE9;
61+
InCh2: PA9, PE11;
62+
}
3863
}
3964

4065
pub mod tim2 {
@@ -66,6 +91,17 @@ pub mod tim2 {
6691
Partial2, PB10, PB11 => MAPR { |_, w| unsafe { w.tim2_remap().bits(0b10)} };
6792
]
6893
}
94+
95+
out_enums! {
96+
Ch1: PA0, PA15;
97+
Ch2: PA1, PB3;
98+
Ch3: PA2, PB10;
99+
Ch4: PA3, PB11;
100+
}
101+
in_enums! {
102+
InCh1: PA0, PA15;
103+
InCh2: PA1, PB3;
104+
}
69105
}
70106

71107
pub mod tim3 {
@@ -98,6 +134,17 @@ pub mod tim3 {
98134
Full, PC8, PC9 => MAPR { |_, w| unsafe { w.tim3_remap().bits(0b11)} };
99135
]
100136
}
137+
138+
out_enums! {
139+
Ch1: PA6, PB4, PC6;
140+
Ch2: PA7, PB5, PC7;
141+
Ch3: PB0, PC8;
142+
Ch4: PB1, PC9;
143+
}
144+
in_enums! {
145+
InCh1: PA6, PB4, PC6;
146+
InCh2: PA7, PB5, PC7;
147+
}
101148
}
102149

103150
#[cfg(feature = "medium")]
@@ -128,7 +175,43 @@ pub mod tim4 {
128175
Full, PD14, PD15 => MAPR { |_, w| w.tim4_remap().bit(true) };
129176
]
130177
}
178+
179+
out_enums! {
180+
Ch1: PB6, PD12;
181+
Ch2: PB7, PD13;
182+
Ch3: PB8, PD14;
183+
Ch4: PB9, PD15;
184+
}
185+
in_enums! {
186+
InCh1: PB6, PD12;
187+
InCh2: PB7, PD13;
188+
}
189+
}
190+
191+
macro_rules! out_enums {
192+
($($Ch: ident: $($P:ident),+;)+) => {
193+
$(
194+
pub enum $Ch {
195+
$(
196+
$P(gpio::$P<Alternate>),
197+
)+
198+
}
199+
)+
200+
};
201+
}
202+
use out_enums;
203+
macro_rules! in_enums {
204+
($($Ch: ident: $($P:ident),+;)+) => {
205+
$(
206+
pub enum $Ch {
207+
$(
208+
$P(gpio::$P),
209+
)+
210+
}
211+
)+
212+
};
131213
}
214+
use in_enums;
132215

133216
macro_rules! remap4 {
134217
($TIM:ty: [
@@ -412,23 +495,16 @@ macro_rules! remap_input {
412495
($TIM:ty, $name:ident: [
413496
$($rname:ident, $P0:ident, $P1:ident $( => $MAPR:ident { $remapex:expr })?;)+
414497
]) => {
415-
pub enum $name {
416-
$(
417-
$rname(gpio::$P0, gpio::$P1),
418-
)+
419-
}
420-
421498
impl InputPins for $TIM {
422-
type Channels12 = $name;
499+
type InCh1 = InCh1;
500+
type InCh2 = InCh2;
423501
}
424502

425503
$(
426-
impl From<(gpio::$P0, gpio::$P1 $(, &mut $MAPR)?)> for $name {
504+
impl From<(gpio::$P0, gpio::$P1 $(, &mut $MAPR)?)> for InPins<InCh1, InCh2> {
427505
fn from(p: (gpio::$P0, gpio::$P1 $(, &mut $MAPR)?)) -> Self {
428-
let p0 = p.0.into_mode(&mut Cr);
429-
let p1 = p.1.into_mode(&mut Cr);
430506
$(p.2.modify_mapr($remapex);)?
431-
Self::$rname(p0, p1)
507+
Self { c1: InCh1::$P0(p.0), c2: InCh2::$P1(p.1) }
432508
}
433509
}
434510
)+
@@ -437,5 +513,6 @@ macro_rules! remap_input {
437513
use remap_input;
438514

439515
pub trait InputPins {
440-
type Channels12;
516+
type InCh1;
517+
type InCh2;
441518
}

src/timer/pwm_input.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::pac::{self, DBGMCU as DBG};
88

99
use crate::rcc::{BusTimerClock, Clocks};
1010
use crate::time::Hertz;
11-
use crate::timer::{InputPins, Timer};
11+
use crate::timer::{InPins, InputPins, Timer};
1212

1313
/// PWM Input
1414
pub struct PwmInput<TIM> {
@@ -61,7 +61,7 @@ pub enum Configuration {
6161
pub trait PwmInputExt: Sized + InputPins {
6262
fn pwm_input(
6363
self,
64-
pins: impl Into<<Self as InputPins>::Channels12>,
64+
pins: impl Into<InPins<Self::InCh1, Self::InCh2>>,
6565
dbg: &mut DBG,
6666
mode: Configuration,
6767
clocks: &Clocks,
@@ -72,7 +72,7 @@ pub trait PwmInputExt: Sized + InputPins {
7272
impl PwmInputExt for pac::TIM1 {
7373
fn pwm_input(
7474
self,
75-
pins: impl Into<<Self as InputPins>::Channels12>,
75+
pins: impl Into<InPins<Self::InCh1, Self::InCh2>>,
7676
dbg: &mut DBG,
7777
mode: Configuration,
7878
clocks: &Clocks,
@@ -85,7 +85,7 @@ impl PwmInputExt for pac::TIM1 {
8585
impl Timer<pac::TIM1> {
8686
pub fn pwm_input(
8787
mut self,
88-
pins: impl Into<<pac::TIM1 as InputPins>::Channels12>,
88+
pins: impl Into<InPins<<pac::TIM1 as InputPins>::InCh1, <pac::TIM1 as InputPins>::InCh2>>,
8989
dbg: &mut DBG,
9090
mode: Configuration,
9191
) -> PwmInput<pac::TIM1> {
@@ -98,7 +98,7 @@ impl Timer<pac::TIM1> {
9898
impl PwmInputExt for pac::TIM2 {
9999
fn pwm_input(
100100
self,
101-
pins: impl Into<<Self as InputPins>::Channels12>,
101+
pins: impl Into<InPins<Self::InCh1, Self::InCh2>>,
102102
dbg: &mut DBG,
103103
mode: Configuration,
104104
clocks: &Clocks,
@@ -110,7 +110,7 @@ impl PwmInputExt for pac::TIM2 {
110110
impl Timer<pac::TIM2> {
111111
pub fn pwm_input(
112112
mut self,
113-
pins: impl Into<<pac::TIM2 as InputPins>::Channels12>,
113+
pins: impl Into<InPins<<pac::TIM2 as InputPins>::InCh1, <pac::TIM2 as InputPins>::InCh2>>,
114114
dbg: &mut DBG,
115115
mode: Configuration,
116116
) -> PwmInput<pac::TIM2> {
@@ -123,7 +123,7 @@ impl Timer<pac::TIM2> {
123123
impl PwmInputExt for pac::TIM3 {
124124
fn pwm_input(
125125
self,
126-
pins: impl Into<<Self as InputPins>::Channels12>,
126+
pins: impl Into<InPins<Self::InCh1, Self::InCh2>>,
127127
dbg: &mut DBG,
128128
mode: Configuration,
129129
clocks: &Clocks,
@@ -135,7 +135,7 @@ impl PwmInputExt for pac::TIM3 {
135135
impl Timer<pac::TIM3> {
136136
pub fn pwm_input(
137137
mut self,
138-
pins: impl Into<<pac::TIM3 as InputPins>::Channels12>,
138+
pins: impl Into<InPins<<pac::TIM3 as InputPins>::InCh1, <pac::TIM3 as InputPins>::InCh2>>,
139139
dbg: &mut DBG,
140140
mode: Configuration,
141141
) -> PwmInput<pac::TIM3> {
@@ -149,7 +149,7 @@ impl Timer<pac::TIM3> {
149149
impl PwmInputExt for pac::TIM4 {
150150
fn pwm_input(
151151
self,
152-
pins: impl Into<<Self as InputPins>::Channels12>,
152+
pins: impl Into<InPins<Self::InCh1, Self::InCh2>>,
153153
dbg: &mut DBG,
154154
mode: Configuration,
155155
clocks: &Clocks,
@@ -162,7 +162,7 @@ impl PwmInputExt for pac::TIM4 {
162162
impl Timer<pac::TIM4> {
163163
pub fn pwm_input(
164164
mut self,
165-
pins: impl Into<<pac::TIM4 as InputPins>::Channels12>,
165+
pins: impl Into<InPins<<pac::TIM4 as InputPins>::InCh1, <pac::TIM4 as InputPins>::InCh2>>,
166166
dbg: &mut DBG,
167167
mode: Configuration,
168168
) -> PwmInput<pac::TIM4> {
@@ -185,7 +185,7 @@ macro_rules! hal {
185185
($TIMX:ty: $timX:ident) => {
186186
fn $timX(
187187
tim: $TIMX,
188-
pins: impl Into<<$TIMX as InputPins>::Channels12>,
188+
pins: impl Into<InPins<<$TIMX as InputPins>::InCh1, <$TIMX as InputPins>::InCh2>>,
189189
clk: Hertz,
190190
mode: Configuration,
191191
) -> PwmInput<$TIMX> {

0 commit comments

Comments
 (0)