@@ -21,6 +21,7 @@ use crate::pwm_input::Pins;
21
21
use crate :: timer:: { sealed:: Remap , Timer } ;
22
22
23
23
/// SMS (Slave Mode Selection) register
24
+ #[ derive( Copy , Clone , Debug ) ]
24
25
pub enum SlaveMode {
25
26
/// Counter counts up/down on TI2FP1 edge depending on TI1FP2 level.
26
27
EncoderMode1 = 0b001 ,
@@ -39,14 +40,27 @@ pub enum SlaveMode {
39
40
ExternalClockMode1 = 0b111 ,
40
41
}
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 ) ]
42
48
pub struct QeiOptions {
43
- slave_mode : SlaveMode ,
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 ,
44
57
}
45
58
46
59
impl Default for QeiOptions {
47
60
fn default ( ) -> Self {
48
61
Self {
49
62
slave_mode : SlaveMode :: EncoderMode3 ,
63
+ auto_reload_value : u16:: MAX ,
50
64
}
51
65
}
52
66
}
@@ -154,7 +168,7 @@ macro_rules! hal {
154
168
// configure as quadrature encoder
155
169
tim. smcr. write( |w| w. sms( ) . bits( options. slave_mode as u8 ) ) ;
156
170
157
- tim. arr. write( |w| w. arr( ) . bits( u16 :: MAX ) ) ;
171
+ tim. arr. write( |w| w. arr( ) . bits( options . auto_reload_value ) ) ;
158
172
tim. cr1. write( |w| w. cen( ) . set_bit( ) ) ;
159
173
160
174
Qei { tim, pins, _remap: PhantomData }
0 commit comments