@@ -78,11 +78,23 @@ static int qdec_stm32_get(const struct device *dev, enum sensor_channel chan,
7878 return 0 ;
7979}
8080
81+ static void qdec_stm32_initialize_channel (const struct device * dev , uint32_t ll_channel )
82+ {
83+ const struct qdec_stm32_dev_cfg * const dev_cfg = dev -> config ;
84+
85+ LL_TIM_IC_SetActiveInput (dev_cfg -> timer_inst , ll_channel , LL_TIM_ACTIVEINPUT_DIRECTTI );
86+ LL_TIM_IC_SetFilter (dev_cfg -> timer_inst , ll_channel ,
87+ dev_cfg -> input_filtering_level * LL_TIM_IC_FILTER_FDIV1_N2 );
88+ LL_TIM_IC_SetPrescaler (dev_cfg -> timer_inst , ll_channel , LL_TIM_ICPSC_DIV1 );
89+ LL_TIM_IC_SetPolarity (dev_cfg -> timer_inst , ll_channel ,
90+ dev_cfg -> is_input_polarity_inverted ? LL_TIM_IC_POLARITY_FALLING :
91+ LL_TIM_IC_POLARITY_RISING );
92+ }
93+
8194static int qdec_stm32_initialize (const struct device * dev )
8295{
8396 const struct qdec_stm32_dev_cfg * const dev_cfg = dev -> config ;
8497 int retval ;
85- LL_TIM_ENCODER_InitTypeDef init_props ;
8698 uint32_t max_counter_value ;
8799
88100 retval = pinctrl_apply_state (dev_cfg -> pin_config , PINCTRL_STATE_DEFAULT );
@@ -108,18 +120,6 @@ static int qdec_stm32_initialize(const struct device *dev)
108120 return - EINVAL ;
109121 }
110122
111- LL_TIM_ENCODER_StructInit (& init_props );
112-
113- init_props .EncoderMode = dev_cfg -> encoder_mode ;
114-
115- if (dev_cfg -> is_input_polarity_inverted ) {
116- init_props .IC1Polarity = LL_TIM_IC_POLARITY_FALLING ;
117- init_props .IC2Polarity = LL_TIM_IC_POLARITY_FALLING ;
118- }
119-
120- init_props .IC1Filter = dev_cfg -> input_filtering_level * LL_TIM_IC_FILTER_FDIV1_N2 ;
121- init_props .IC2Filter = dev_cfg -> input_filtering_level * LL_TIM_IC_FILTER_FDIV1_N2 ;
122-
123123 /* Ensure that the counter will always count up to a multiple of counts_per_revolution */
124124 if (IS_TIM_32B_COUNTER_INSTANCE (dev_cfg -> timer_inst )) {
125125 max_counter_value = UINT32_MAX - (UINT32_MAX % dev_cfg -> counts_per_revolution ) - 1 ;
@@ -128,10 +128,12 @@ static int qdec_stm32_initialize(const struct device *dev)
128128 }
129129 LL_TIM_SetAutoReload (dev_cfg -> timer_inst , max_counter_value );
130130
131- if (LL_TIM_ENCODER_Init (dev_cfg -> timer_inst , & init_props ) != SUCCESS ) {
132- LOG_ERR ("Initalization failed" );
133- return - EIO ;
134- }
131+ LL_TIM_SetClockSource (dev_cfg -> timer_inst , dev_cfg -> encoder_mode );
132+
133+ qdec_stm32_initialize_channel (dev , LL_TIM_CHANNEL_CH1 );
134+ qdec_stm32_initialize_channel (dev , LL_TIM_CHANNEL_CH2 );
135+
136+ LL_TIM_CC_EnableChannel (dev_cfg -> timer_inst , LL_TIM_CHANNEL_CH1 | LL_TIM_CHANNEL_CH2 );
135137
136138 LL_TIM_EnableCounter (dev_cfg -> timer_inst );
137139
0 commit comments