@@ -3,6 +3,7 @@ use crate::{
33 hal:: { self , Direction } ,
44 pac:: RCC ,
55 rcc,
6+ timer:: General ,
67} ;
78
89pub trait Pins < TIM > { }
@@ -48,7 +49,7 @@ impl<TIM: Instance, PINS> Qei<TIM, PINS> {
4849}
4950
5051impl < TIM : Instance , PINS > hal:: Qei for Qei < TIM , PINS > {
51- type Count = TIM :: Count ;
52+ type Count = TIM :: Width ;
5253
5354 fn count ( & self ) -> Self :: Count {
5455 self . tim . read_count ( ) as Self :: Count
@@ -63,23 +64,22 @@ impl<TIM: Instance, PINS> hal::Qei for Qei<TIM, PINS> {
6364 }
6465}
6566
66- pub trait Instance : crate :: Sealed + rcc:: Enable + rcc:: Reset {
67- type Count ;
68-
67+ pub trait Instance : crate :: Sealed + rcc:: Enable + rcc:: Reset + General {
6968 fn setup_qei ( & mut self ) ;
70- fn read_count ( & self ) -> Self :: Count ;
69+
7170 fn read_direction ( & self ) -> bool ;
7271}
7372
7473macro_rules! hal {
75- ( $( $TIM: ty: ( $bits : ident ) , ) +) => {
74+ ( $( $TIM: ty, ) +) => {
7675 $(
7776 impl Instance for $TIM {
78- type Count = $bits;
79-
8077 fn setup_qei( & mut self ) {
8178 // Configure TxC1 and TxC2 as captures
82- self . ccmr1_output( )
79+ #[ cfg( not( feature = "stm32f410" ) ) ]
80+ self . ccmr1_input( ) . write( |w| w. cc1s( ) . ti1( ) . cc2s( ) . ti2( ) ) ;
81+ #[ cfg( feature = "stm32f410" ) ]
82+ self . ccmr1_input( )
8383 . write( |w| unsafe { w. cc1s( ) . bits( 0b01 ) . cc2s( ) . bits( 0b01 ) } ) ;
8484 // enable and configure to capture on rising edge
8585 self . ccer. write( |w| {
@@ -92,19 +92,14 @@ macro_rules! hal {
9292 . cc2p( )
9393 . clear_bit( )
9494 } ) ;
95- // configure as quadrature encoder
96- // some chip variants declare `.bits()` as unsafe, some don't
97- #[ allow ( unused_unsafe ) ]
95+ # [ cfg ( not ( feature = "stm32f410" ) ) ]
96+ self . smcr . write ( |w| w . sms ( ) . encoder_mode_3 ( ) ) ;
97+ #[ cfg ( feature = "stm32f410" ) ]
9898 self . smcr. write( |w| unsafe { w. sms( ) . bits( 3 ) } ) ;
99- #[ allow( unused_unsafe) ]
100- self . arr. write( |w| unsafe { w. bits( $bits:: MAX as u32 ) } ) ;
99+ self . set_auto_reload( <$TIM as General >:: Width :: MAX as u32 ) . unwrap( ) ;
101100 self . cr1. write( |w| w. cen( ) . set_bit( ) ) ;
102101 }
103102
104- fn read_count( & self ) -> Self :: Count {
105- self . cnt. read( ) . bits( ) as Self :: Count
106- }
107-
108103 fn read_direction( & self ) -> bool {
109104 self . cr1. read( ) . dir( ) . bit_is_clear( )
110105 }
@@ -114,18 +109,18 @@ macro_rules! hal {
114109}
115110
116111hal ! {
117- crate :: pac:: TIM1 : ( u16 ) ,
118- crate :: pac:: TIM5 : ( u32 ) ,
112+ crate :: pac:: TIM1 ,
113+ crate :: pac:: TIM5 ,
119114}
120115
121116#[ cfg( feature = "tim2" ) ]
122117hal ! {
123- crate :: pac:: TIM2 : ( u32 ) ,
124- crate :: pac:: TIM3 : ( u16 ) ,
125- crate :: pac:: TIM4 : ( u16 ) ,
118+ crate :: pac:: TIM2 ,
119+ crate :: pac:: TIM3 ,
120+ crate :: pac:: TIM4 ,
126121}
127122
128123#[ cfg( feature = "tim8" ) ]
129124hal ! {
130- crate :: pac:: TIM8 : ( u16 ) ,
125+ crate :: pac:: TIM8 ,
131126}
0 commit comments