4
4
NOTE: In some cases you need to specify remap you need, especially for TIM2
5
5
(see [Alternate function remapping](super::timer)):
6
6
*/
7
-
8
7
use core:: u16;
9
8
10
9
use core:: marker:: PhantomData ;
11
10
12
11
use crate :: hal:: { self , Direction } ;
13
- #[ cfg( any(
14
- feature = "stm32f100" ,
15
- feature = "stm32f103" ,
16
- feature = "stm32f105" ,
17
- ) ) ]
12
+ #[ cfg( any( feature = "stm32f100" , feature = "stm32f103" , feature = "stm32f105" , ) ) ]
18
13
use crate :: pac:: TIM1 ;
19
- use crate :: pac:: { TIM2 , TIM3 } ;
20
14
#[ cfg( feature = "medium" ) ]
21
15
use crate :: pac:: TIM4 ;
16
+ use crate :: pac:: { TIM2 , TIM3 } ;
22
17
23
18
use crate :: afio:: MAPR ;
24
19
25
- use crate :: timer:: { Timer , sealed:: Remap } ;
26
20
use crate :: pwm_input:: Pins ;
21
+ use crate :: timer:: { sealed:: Remap , Timer } ;
22
+
23
+ /// SMS (Slave Mode Selection) register
24
+ #[ derive( Copy , Clone , Debug ) ]
25
+ pub enum SlaveMode {
26
+ /// Counter counts up/down on TI2FP1 edge depending on TI1FP2 level.
27
+ EncoderMode1 = 0b001 ,
28
+ /// Encoder mode 2 - Counter counts up/down on TI1FP2 edge depending on TI2FP1 level.
29
+ EncoderMode2 = 0b010 ,
30
+ /// Encoder mode 3 - Counter counts up/down on both TI1FP1 and TI2FP2 edges depending on the
31
+ /// level of the other input.
32
+ EncoderMode3 = 0b011 ,
33
+ /// Reset Mode - Rising edge of the selected trigger input (TRGI) reinitializes the counter and
34
+ /// generates an update of the registers.
35
+ ResetMode = 0b100 ,
36
+ /// Trigger Mode - The counter starts at a rising edge of the trigger TRGI (but it is not
37
+ /// reset). Only the start of the counter is controlled.
38
+ TriggerMode = 0b110 ,
39
+ /// External Clock Mode 1 - Rising edges of the selected trigger (TRGI) clock the counter.
40
+ ExternalClockMode1 = 0b111 ,
41
+ }
42
+
43
+ /// Quadrature Encoder Interface (QEI) options
44
+ ///
45
+ /// The `Default` implementation provides a configuration for a 4-count pulse which counts from
46
+ /// 0-65535. The counter wraps back to 0 on overflow.
47
+ #[ derive( Copy , Clone , Debug ) ]
48
+ pub struct QeiOptions {
49
+ /// Encoder slave mode
50
+ pub slave_mode : SlaveMode ,
51
+
52
+ /// Autoreload value
53
+ ///
54
+ /// This value allows the maximum count to be configured, up to 65535. Setting a lower value
55
+ /// will overflow the counter to 0 sooner.
56
+ pub auto_reload_value : u16 ,
57
+ }
58
+
59
+ impl Default for QeiOptions {
60
+ fn default ( ) -> Self {
61
+ Self {
62
+ slave_mode : SlaveMode :: EncoderMode3 ,
63
+ auto_reload_value : u16:: MAX ,
64
+ }
65
+ }
66
+ }
27
67
28
68
pub struct Qei < TIM , REMAP , PINS > {
29
69
tim : TIM ,
30
70
pins : PINS ,
31
71
_remap : PhantomData < REMAP > ,
32
72
}
33
73
34
- #[ cfg( any(
35
- feature = "stm32f100" ,
36
- feature = "stm32f103" ,
37
- feature = "stm32f105" ,
38
- ) ) ]
74
+ #[ cfg( any( feature = "stm32f100" , feature = "stm32f103" , feature = "stm32f105" , ) ) ]
39
75
impl Timer < TIM1 > {
40
- pub fn qei < REMAP , PINS > ( self , pins : PINS , mapr : & mut MAPR ) -> Qei < TIM1 , REMAP , PINS >
76
+ pub fn qei < REMAP , PINS > (
77
+ self ,
78
+ pins : PINS ,
79
+ mapr : & mut MAPR ,
80
+ options : QeiOptions ,
81
+ ) -> Qei < TIM1 , REMAP , PINS >
41
82
where
42
83
REMAP : Remap < Periph = TIM1 > ,
43
84
PINS : Pins < REMAP > ,
44
85
{
45
86
mapr. modify_mapr ( |_, w| unsafe { w. tim1_remap ( ) . bits ( REMAP :: REMAP ) } ) ;
46
87
47
88
let Self { tim, clk : _ } = self ;
48
- Qei :: _tim1 ( tim, pins)
89
+ Qei :: _tim1 ( tim, pins, options )
49
90
}
50
91
}
51
92
52
93
impl Timer < TIM2 > {
53
- pub fn qei < REMAP , PINS > ( self , pins : PINS , mapr : & mut MAPR ) -> Qei < TIM2 , REMAP , PINS >
94
+ pub fn qei < REMAP , PINS > (
95
+ self ,
96
+ pins : PINS ,
97
+ mapr : & mut MAPR ,
98
+ options : QeiOptions ,
99
+ ) -> Qei < TIM2 , REMAP , PINS >
54
100
where
55
101
REMAP : Remap < Periph = TIM2 > ,
56
102
PINS : Pins < REMAP > ,
57
103
{
58
104
mapr. modify_mapr ( |_, w| unsafe { w. tim2_remap ( ) . bits ( REMAP :: REMAP ) } ) ;
59
105
60
106
let Self { tim, clk : _ } = self ;
61
- Qei :: _tim2 ( tim, pins)
107
+ Qei :: _tim2 ( tim, pins, options )
62
108
}
63
109
}
64
110
65
111
impl Timer < TIM3 > {
66
- pub fn qei < REMAP , PINS > ( self , pins : PINS , mapr : & mut MAPR ) -> Qei < TIM3 , REMAP , PINS >
112
+ pub fn qei < REMAP , PINS > (
113
+ self ,
114
+ pins : PINS ,
115
+ mapr : & mut MAPR ,
116
+ options : QeiOptions ,
117
+ ) -> Qei < TIM3 , REMAP , PINS >
67
118
where
68
119
REMAP : Remap < Periph = TIM3 > ,
69
120
PINS : Pins < REMAP > ,
70
121
{
71
122
mapr. modify_mapr ( |_, w| unsafe { w. tim3_remap ( ) . bits ( REMAP :: REMAP ) } ) ;
72
123
73
124
let Self { tim, clk : _ } = self ;
74
- Qei :: _tim3 ( tim, pins)
125
+ Qei :: _tim3 ( tim, pins, options )
75
126
}
76
127
}
77
128
78
129
#[ cfg( feature = "medium" ) ]
79
130
impl Timer < TIM4 > {
80
- pub fn qei < REMAP , PINS > ( self , pins : PINS , mapr : & mut MAPR ) -> Qei < TIM4 , REMAP , PINS >
131
+ pub fn qei < REMAP , PINS > (
132
+ self ,
133
+ pins : PINS ,
134
+ mapr : & mut MAPR ,
135
+ options : QeiOptions ,
136
+ ) -> Qei < TIM4 , REMAP , PINS >
81
137
where
82
138
REMAP : Remap < Periph = TIM4 > ,
83
139
PINS : Pins < REMAP > ,
84
140
{
85
141
mapr. modify_mapr ( |_, w| w. tim4_remap ( ) . bit ( REMAP :: REMAP == 1 ) ) ;
86
142
87
143
let Self { tim, clk : _ } = self ;
88
- Qei :: _tim4 ( tim, pins)
144
+ Qei :: _tim4 ( tim, pins, options )
89
145
}
90
146
}
91
147
92
148
macro_rules! hal {
93
149
( $( $TIMX: ident: ( $timX: ident, $timXen: ident, $timXrst: ident) , ) +) => {
94
150
$(
95
151
impl <REMAP , PINS > Qei <$TIMX, REMAP , PINS > {
96
- fn $timX( tim: $TIMX, pins: PINS ) -> Self {
152
+ fn $timX( tim: $TIMX, pins: PINS , options : QeiOptions ) -> Self {
97
153
// Configure TxC1 and TxC2 as captures
98
154
tim. ccmr1_input( ) . write( |w| w. cc1s( ) . ti1( ) . cc2s( ) . ti2( ) ) ;
99
155
@@ -110,9 +166,9 @@ macro_rules! hal {
110
166
} ) ;
111
167
112
168
// configure as quadrature encoder
113
- tim. smcr. write( |w| w. sms( ) . bits( 3 ) ) ;
169
+ tim. smcr. write( |w| w. sms( ) . bits( options . slave_mode as u8 ) ) ;
114
170
115
- tim. arr. write( |w| w. arr( ) . bits( u16 :: MAX ) ) ;
171
+ tim. arr. write( |w| w. arr( ) . bits( options . auto_reload_value ) ) ;
116
172
tim. cr1. write( |w| w. cen( ) . set_bit( ) ) ;
117
173
118
174
Qei { tim, pins, _remap: PhantomData }
@@ -143,11 +199,7 @@ macro_rules! hal {
143
199
}
144
200
}
145
201
146
- #[ cfg( any(
147
- feature = "stm32f100" ,
148
- feature = "stm32f103" ,
149
- feature = "stm32f105" ,
150
- ) ) ]
202
+ #[ cfg( any( feature = "stm32f100" , feature = "stm32f103" , feature = "stm32f105" , ) ) ]
151
203
hal ! {
152
204
TIM1 : ( _tim1, tim1en, tim1rst) ,
153
205
}
0 commit comments