44 NOTE: In some cases you need to specify remap you need, especially for TIM2
55 (see [Alternate function remapping](super::timer)):
66*/
7- use core:: marker:: PhantomData ;
8-
97use crate :: pac;
108use embedded_hal_02 as hal;
119pub use hal:: Direction ;
1210
13- use crate :: afio:: MAPR ;
14-
15- use crate :: timer:: pwm_input:: Pins ;
16- use crate :: timer:: { pins:: sealed:: Remap , Timer } ;
11+ use crate :: rcc:: Clocks ;
12+ use crate :: timer:: { InPins , InputPins , Timer } ;
1713
1814/// SMS (Slave Mode Selection) register
1915#[ derive( Copy , Clone , Debug ) ]
@@ -60,90 +56,122 @@ impl Default for QeiOptions {
6056 }
6157}
6258
63- pub struct Qei < TIM , REMAP , PINS > {
59+ pub struct Qei < TIM : InputPins > {
6460 tim : TIM ,
65- pins : PINS ,
66- _remap : PhantomData < REMAP > ,
61+ pins : ( TIM :: InCh1 , TIM :: InCh2 ) ,
62+ }
63+
64+ pub trait QeiExt : Sized + InputPins {
65+ fn qei (
66+ self ,
67+ pins : impl Into < InPins < Self :: InCh1 , Self :: InCh2 > > ,
68+ options : QeiOptions ,
69+ clocks : & Clocks ,
70+ ) -> Qei < Self > ;
6771}
6872
6973#[ cfg( any( feature = "stm32f100" , feature = "stm32f103" , feature = "connectivity" ) ) ]
70- impl Timer < pac:: TIM1 > {
71- pub fn qei < REMAP , PINS > (
74+ impl QeiExt for pac:: TIM1 {
75+ fn qei (
7276 self ,
73- pins : PINS ,
74- mapr : & mut MAPR ,
77+ pins : impl Into < InPins < Self :: InCh1 , Self :: InCh2 > > ,
7578 options : QeiOptions ,
76- ) -> Qei < pac:: TIM1 , REMAP , PINS >
77- where
78- REMAP : Remap < Periph = pac:: TIM1 > ,
79- PINS : Pins < REMAP > ,
80- {
81- mapr. modify_mapr ( |_, w| unsafe { w. tim1_remap ( ) . bits ( REMAP :: REMAP ) } ) ;
79+ clocks : & Clocks ,
80+ ) -> Qei < Self > {
81+ Timer :: new ( self , clocks) . qei ( pins, options)
82+ }
83+ }
8284
85+ #[ cfg( any( feature = "stm32f100" , feature = "stm32f103" , feature = "connectivity" ) ) ]
86+ impl Timer < pac:: TIM1 > {
87+ pub fn qei (
88+ self ,
89+ pins : impl Into < InPins < <pac:: TIM1 as InputPins >:: InCh1 , <pac:: TIM1 as InputPins >:: InCh2 > > ,
90+ options : QeiOptions ,
91+ ) -> Qei < pac:: TIM1 > {
8392 let Self { tim, clk : _ } = self ;
8493 Qei :: _tim1 ( tim, pins, options)
8594 }
8695}
8796
88- impl Timer < pac:: TIM2 > {
89- pub fn qei < REMAP , PINS > (
97+ impl QeiExt for pac:: TIM2 {
98+ fn qei (
9099 self ,
91- pins : PINS ,
92- mapr : & mut MAPR ,
100+ pins : impl Into < InPins < Self :: InCh1 , Self :: InCh2 > > ,
93101 options : QeiOptions ,
94- ) -> Qei < pac:: TIM2 , REMAP , PINS >
95- where
96- REMAP : Remap < Periph = pac:: TIM2 > ,
97- PINS : Pins < REMAP > ,
98- {
99- mapr. modify_mapr ( |_, w| unsafe { w. tim2_remap ( ) . bits ( REMAP :: REMAP ) } ) ;
102+ clocks : & Clocks ,
103+ ) -> Qei < Self > {
104+ Timer :: new ( self , clocks) . qei ( pins, options)
105+ }
106+ }
100107
108+ impl Timer < pac:: TIM2 > {
109+ pub fn qei (
110+ self ,
111+ pins : impl Into < InPins < <pac:: TIM2 as InputPins >:: InCh1 , <pac:: TIM2 as InputPins >:: InCh2 > > ,
112+ options : QeiOptions ,
113+ ) -> Qei < pac:: TIM2 > {
101114 let Self { tim, clk : _ } = self ;
102115 Qei :: _tim2 ( tim, pins, options)
103116 }
104117}
105118
106- impl Timer < pac:: TIM3 > {
107- pub fn qei < REMAP , PINS > (
119+ impl QeiExt for pac:: TIM3 {
120+ fn qei (
108121 self ,
109- pins : PINS ,
110- mapr : & mut MAPR ,
122+ pins : impl Into < InPins < Self :: InCh1 , Self :: InCh2 > > ,
111123 options : QeiOptions ,
112- ) -> Qei < pac:: TIM3 , REMAP , PINS >
113- where
114- REMAP : Remap < Periph = pac:: TIM3 > ,
115- PINS : Pins < REMAP > ,
116- {
117- mapr. modify_mapr ( |_, w| unsafe { w. tim3_remap ( ) . bits ( REMAP :: REMAP ) } ) ;
124+ clocks : & Clocks ,
125+ ) -> Qei < Self > {
126+ Timer :: new ( self , clocks) . qei ( pins, options)
127+ }
128+ }
118129
130+ impl Timer < pac:: TIM3 > {
131+ pub fn qei (
132+ self ,
133+ pins : impl Into < InPins < <pac:: TIM3 as InputPins >:: InCh1 , <pac:: TIM3 as InputPins >:: InCh2 > > ,
134+ options : QeiOptions ,
135+ ) -> Qei < pac:: TIM3 > {
119136 let Self { tim, clk : _ } = self ;
120137 Qei :: _tim3 ( tim, pins, options)
121138 }
122139}
123140
124141#[ cfg( feature = "medium" ) ]
125- impl Timer < pac:: TIM4 > {
126- pub fn qei < REMAP , PINS > (
142+ impl QeiExt for pac:: TIM4 {
143+ fn qei (
127144 self ,
128- pins : PINS ,
129- mapr : & mut MAPR ,
145+ pins : impl Into < InPins < Self :: InCh1 , Self :: InCh2 > > ,
130146 options : QeiOptions ,
131- ) -> Qei < pac:: TIM4 , REMAP , PINS >
132- where
133- REMAP : Remap < Periph = pac:: TIM4 > ,
134- PINS : Pins < REMAP > ,
135- {
136- mapr. modify_mapr ( |_, w| w. tim4_remap ( ) . bit ( REMAP :: REMAP == 1 ) ) ;
147+ clocks : & Clocks ,
148+ ) -> Qei < Self > {
149+ Timer :: new ( self , & clocks) . qei ( pins, options)
150+ }
151+ }
137152
153+ #[ cfg( feature = "medium" ) ]
154+ impl Timer < pac:: TIM4 > {
155+ pub fn qei (
156+ self ,
157+ pins : impl Into < InPins < <pac:: TIM4 as InputPins >:: InCh1 , <pac:: TIM4 as InputPins >:: InCh2 > > ,
158+ options : QeiOptions ,
159+ ) -> Qei < pac:: TIM4 > {
138160 let Self { tim, clk : _ } = self ;
139161 Qei :: _tim4 ( tim, pins, options)
140162 }
141163}
142164
143165macro_rules! hal {
144166 ( $TIMX: ty: $timX: ident, $timXen: ident, $timXrst: ident) => {
145- impl <REMAP , PINS > Qei <$TIMX, REMAP , PINS > {
146- fn $timX( tim: $TIMX, pins: PINS , options: QeiOptions ) -> Self {
167+ impl Qei <$TIMX> {
168+ fn $timX(
169+ tim: $TIMX,
170+ pins: impl Into <InPins <<$TIMX as InputPins >:: InCh1 , <$TIMX as InputPins >:: InCh2 >>,
171+ options: QeiOptions ,
172+ ) -> Self {
173+ let pins = pins. into( ) ;
174+
147175 // Configure TxC1 and TxC2 as captures
148176 tim. ccmr1_input( ) . write( |w| w. cc1s( ) . ti1( ) . cc2s( ) . ti2( ) ) ;
149177
@@ -163,17 +191,21 @@ macro_rules! hal {
163191
164192 Qei {
165193 tim,
166- pins,
167- _remap: PhantomData ,
194+ pins: ( pins. c1, pins. c2) ,
168195 }
169196 }
170197
171- pub fn release( self ) -> ( $TIMX, PINS ) {
198+ pub fn release(
199+ self ,
200+ ) -> (
201+ $TIMX,
202+ ( <$TIMX as InputPins >:: InCh1 , <$TIMX as InputPins >:: InCh2 ) ,
203+ ) {
172204 ( self . tim, self . pins)
173205 }
174206 }
175207
176- impl < REMAP , PINS > hal:: Qei for Qei <$TIMX, REMAP , PINS > {
208+ impl hal:: Qei for Qei <$TIMX> {
177209 type Count = u16 ;
178210
179211 fn count( & self ) -> u16 {
0 commit comments