6
6
*/
7
7
use core:: marker:: PhantomData ;
8
8
9
- #[ cfg( any( feature = "stm32f100" , feature = "stm32f103" , feature = "connectivity" , ) ) ]
10
- use crate :: pac:: TIM1 ;
11
- #[ cfg( feature = "medium" ) ]
12
- use crate :: pac:: TIM4 ;
13
- use crate :: pac:: { TIM2 , TIM3 } ;
9
+ use crate :: pac;
14
10
use embedded_hal_02 as hal;
15
11
pub use hal:: Direction ;
16
12
@@ -70,16 +66,16 @@ pub struct Qei<TIM, REMAP, PINS> {
70
66
_remap : PhantomData < REMAP > ,
71
67
}
72
68
73
- #[ cfg( any( feature = "stm32f100" , feature = "stm32f103" , feature = "connectivity" , ) ) ]
74
- impl Timer < TIM1 > {
69
+ #[ cfg( any( feature = "stm32f100" , feature = "stm32f103" , feature = "connectivity" ) ) ]
70
+ impl Timer < pac :: TIM1 > {
75
71
pub fn qei < REMAP , PINS > (
76
72
self ,
77
73
pins : PINS ,
78
74
mapr : & mut MAPR ,
79
75
options : QeiOptions ,
80
- ) -> Qei < TIM1 , REMAP , PINS >
76
+ ) -> Qei < pac :: TIM1 , REMAP , PINS >
81
77
where
82
- REMAP : Remap < Periph = TIM1 > ,
78
+ REMAP : Remap < Periph = pac :: TIM1 > ,
83
79
PINS : Pins < REMAP > ,
84
80
{
85
81
mapr. modify_mapr ( |_, w| unsafe { w. tim1_remap ( ) . bits ( REMAP :: REMAP ) } ) ;
@@ -89,15 +85,15 @@ impl Timer<TIM1> {
89
85
}
90
86
}
91
87
92
- impl Timer < TIM2 > {
88
+ impl Timer < pac :: TIM2 > {
93
89
pub fn qei < REMAP , PINS > (
94
90
self ,
95
91
pins : PINS ,
96
92
mapr : & mut MAPR ,
97
93
options : QeiOptions ,
98
- ) -> Qei < TIM2 , REMAP , PINS >
94
+ ) -> Qei < pac :: TIM2 , REMAP , PINS >
99
95
where
100
- REMAP : Remap < Periph = TIM2 > ,
96
+ REMAP : Remap < Periph = pac :: TIM2 > ,
101
97
PINS : Pins < REMAP > ,
102
98
{
103
99
mapr. modify_mapr ( |_, w| unsafe { w. tim2_remap ( ) . bits ( REMAP :: REMAP ) } ) ;
@@ -107,15 +103,15 @@ impl Timer<TIM2> {
107
103
}
108
104
}
109
105
110
- impl Timer < TIM3 > {
106
+ impl Timer < pac :: TIM3 > {
111
107
pub fn qei < REMAP , PINS > (
112
108
self ,
113
109
pins : PINS ,
114
110
mapr : & mut MAPR ,
115
111
options : QeiOptions ,
116
- ) -> Qei < TIM3 , REMAP , PINS >
112
+ ) -> Qei < pac :: TIM3 , REMAP , PINS >
117
113
where
118
- REMAP : Remap < Periph = TIM3 > ,
114
+ REMAP : Remap < Periph = pac :: TIM3 > ,
119
115
PINS : Pins < REMAP > ,
120
116
{
121
117
mapr. modify_mapr ( |_, w| unsafe { w. tim3_remap ( ) . bits ( REMAP :: REMAP ) } ) ;
@@ -126,15 +122,15 @@ impl Timer<TIM3> {
126
122
}
127
123
128
124
#[ cfg( feature = "medium" ) ]
129
- impl Timer < TIM4 > {
125
+ impl Timer < pac :: TIM4 > {
130
126
pub fn qei < REMAP , PINS > (
131
127
self ,
132
128
pins : PINS ,
133
129
mapr : & mut MAPR ,
134
130
options : QeiOptions ,
135
- ) -> Qei < TIM4 , REMAP , PINS >
131
+ ) -> Qei < pac :: TIM4 , REMAP , PINS >
136
132
where
137
- REMAP : Remap < Periph = TIM4 > ,
133
+ REMAP : Remap < Periph = pac :: TIM4 > ,
138
134
PINS : Pins < REMAP > ,
139
135
{
140
136
mapr. modify_mapr ( |_, w| w. tim4_remap ( ) . bit ( REMAP :: REMAP == 1 ) ) ;
@@ -145,68 +141,61 @@ impl Timer<TIM4> {
145
141
}
146
142
147
143
macro_rules! hal {
148
- ( $( $TIMX: ident: ( $timX: ident, $timXen: ident, $timXrst: ident) , ) +) => {
149
- $(
150
- impl <REMAP , PINS > Qei <$TIMX, REMAP , PINS > {
151
- fn $timX( tim: $TIMX, pins: PINS , options: QeiOptions ) -> Self {
152
- // Configure TxC1 and TxC2 as captures
153
- tim. ccmr1_input( ) . write( |w| w. cc1s( ) . ti1( ) . cc2s( ) . ti2( ) ) ;
154
-
155
- // enable and configure to capture on rising edge
156
- tim. ccer( ) . write( |w| {
157
- w. cc1e( )
158
- . set_bit( )
159
- . cc1p( )
160
- . clear_bit( )
161
- . cc2e( )
162
- . set_bit( )
163
- . cc2p( )
164
- . clear_bit( )
165
- } ) ;
166
-
167
- // configure as quadrature encoder
168
- tim. smcr( ) . write( |w| w. sms( ) . set( options. slave_mode as u8 ) ) ;
169
-
170
- tim. arr( ) . write( |w| w. arr( ) . set( options. auto_reload_value) ) ;
171
- tim. cr1( ) . write( |w| w. cen( ) . set_bit( ) ) ;
172
-
173
- Qei { tim, pins, _remap: PhantomData }
144
+ ( $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 {
147
+ // Configure TxC1 and TxC2 as captures
148
+ tim. ccmr1_input( ) . write( |w| w. cc1s( ) . ti1( ) . cc2s( ) . ti2( ) ) ;
149
+
150
+ // enable and configure to capture on rising edge
151
+ tim. ccer( ) . write( |w| {
152
+ w. cc1e( ) . set_bit( ) ;
153
+ w. cc1p( ) . clear_bit( ) ;
154
+ w. cc2e( ) . set_bit( ) ;
155
+ w. cc2p( ) . clear_bit( )
156
+ } ) ;
157
+
158
+ // configure as quadrature encoder
159
+ tim. smcr( ) . write( |w| w. sms( ) . set( options. slave_mode as u8 ) ) ;
160
+
161
+ tim. arr( ) . write( |w| w. arr( ) . set( options. auto_reload_value) ) ;
162
+ tim. cr1( ) . write( |w| w. cen( ) . set_bit( ) ) ;
163
+
164
+ Qei {
165
+ tim,
166
+ pins,
167
+ _remap: PhantomData ,
174
168
}
169
+ }
175
170
176
- pub fn release( self ) -> ( $TIMX, PINS ) {
177
- ( self . tim, self . pins)
178
- }
171
+ pub fn release( self ) -> ( $TIMX, PINS ) {
172
+ ( self . tim, self . pins)
179
173
}
174
+ }
180
175
181
- impl <REMAP , PINS > hal:: Qei for Qei <$TIMX, REMAP , PINS > {
182
- type Count = u16 ;
176
+ impl <REMAP , PINS > hal:: Qei for Qei <$TIMX, REMAP , PINS > {
177
+ type Count = u16 ;
183
178
184
- fn count( & self ) -> u16 {
185
- self . tim. cnt( ) . read( ) . cnt( ) . bits( )
186
- }
179
+ fn count( & self ) -> u16 {
180
+ self . tim. cnt( ) . read( ) . cnt( ) . bits( )
181
+ }
187
182
188
- fn direction( & self ) -> Direction {
189
- if self . tim. cr1( ) . read( ) . dir( ) . bit_is_clear( ) {
190
- Direction :: Upcounting
191
- } else {
192
- Direction :: Downcounting
193
- }
183
+ fn direction( & self ) -> Direction {
184
+ if self . tim. cr1( ) . read( ) . dir( ) . bit_is_clear( ) {
185
+ Direction :: Upcounting
186
+ } else {
187
+ Direction :: Downcounting
194
188
}
195
189
}
196
-
197
- ) +
198
- }
190
+ }
191
+ } ;
199
192
}
200
193
201
- #[ cfg( any( feature = "stm32f100" , feature = "stm32f103" , feature = "connectivity" , ) ) ]
202
- hal ! {
203
- TIM1 : ( _tim1, tim1en, tim1rst) ,
204
- }
205
- hal ! {
206
- TIM2 : ( _tim2, tim2en, tim2rst) ,
207
- TIM3 : ( _tim3, tim3en, tim3rst) ,
208
- }
194
+ #[ cfg( any( feature = "stm32f100" , feature = "stm32f103" , feature = "connectivity" ) ) ]
195
+ hal ! ( pac:: TIM1 : _tim1, tim1en, tim1rst) ;
196
+
197
+ hal ! ( pac:: TIM2 : _tim2, tim2en, tim2rst) ;
198
+ hal ! ( pac:: TIM3 : _tim3, tim3en, tim3rst) ;
199
+
209
200
#[ cfg( feature = "medium" ) ]
210
- hal ! {
211
- TIM4 : ( _tim4, tim4en, tim4rst) ,
212
- }
201
+ hal ! ( pac:: TIM4 : _tim4, tim4en, tim4rst) ;
0 commit comments