Skip to content

Commit 43bf0b3

Browse files
gautierg-stnashif
authored andcommitted
drivers: sensor: st: qdec: remove init struct
Remove the calls to LL_TIM_ENCODER_StructInit and LL_TIM_ENCODER_Init in the QDEC driver. This avoids calling functions from stm32xxxx_ll_tim.c. They are replaced by a set of simpler functions from the header file. Signed-off-by: Guillaume Gautier <[email protected]>
1 parent 24dcfb2 commit 43bf0b3

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

drivers/sensor/st/qdec_stm32/qdec_stm32.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
8194
static 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

Comments
 (0)