Skip to content

Commit 4457e6d

Browse files
gautierg-stnashif
authored andcommitted
drivers: counter: stm32: remove init struct
Remove the calls to LL_TIM_StructInit and LL_TIM_Init in the counter driver. This avoids calling functions from stm32xxxx_ll_tim.c. They are replaced by a set of simpler functions from the header file. Also replaces some macros that used constants coming from the HAL. Use an equivalent coming purely from the LL. Signed-off-by: Guillaume Gautier <[email protected]>
1 parent 16c57f6 commit 4457e6d

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

drivers/counter/counter_ll_stm32_timer.c

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ LOG_MODULE_REGISTER(counter_timer_stm32, CONFIG_COUNTER_LOG_LEVEL);
2727
#define TIMER_MAX_CH 4U
2828

2929
/** Number of channels for timer by index. */
30-
#define NUM_CH(timx) \
31-
(IS_TIM_CCX_INSTANCE(timx, TIM_CHANNEL_4) ? 4U : \
32-
(IS_TIM_CCX_INSTANCE(timx, TIM_CHANNEL_3) ? 3U : \
33-
(IS_TIM_CCX_INSTANCE(timx, TIM_CHANNEL_2) ? 2U : \
34-
(IS_TIM_CCX_INSTANCE(timx, TIM_CHANNEL_1) ? 1U : \
35-
0))))
30+
#define NUM_CH(timx) \
31+
(IS_TIM_CC4_INSTANCE(timx) ? 4U : \
32+
(IS_TIM_CC3_INSTANCE(timx) ? 3U : \
33+
(IS_TIM_CC2_INSTANCE(timx) ? 2U : \
34+
(IS_TIM_CC1_INSTANCE(timx) ? 1U : \
35+
0U))))
3636

3737
/** Channel to compare set function mapping. */
3838
static void(*const set_timer_compare[TIMER_MAX_CH])(TIM_TypeDef *,
@@ -369,7 +369,6 @@ static int counter_stm32_init_timer(const struct device *dev)
369369
const struct counter_stm32_config *cfg = dev->config;
370370
struct counter_stm32_data *data = dev->data;
371371
TIM_TypeDef *timer = cfg->timer;
372-
LL_TIM_InitTypeDef init;
373372
const struct device *clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
374373
uint32_t tim_clk;
375374
int r;
@@ -413,17 +412,27 @@ static int counter_stm32_init_timer(const struct device *dev)
413412
cfg->irq_config_func(dev);
414413

415414
/* initialize timer */
416-
LL_TIM_StructInit(&init);
415+
LL_TIM_SetPrescaler(timer, cfg->prescaler);
416+
LL_TIM_SetAutoReload(timer, counter_get_max_top_value(dev));
417417

418-
init.Prescaler = cfg->prescaler;
419-
init.CounterMode = LL_TIM_COUNTERMODE_UP;
420-
init.Autoreload = counter_get_max_top_value(dev);
421-
init.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
418+
if (IS_TIM_COUNTER_MODE_SELECT_INSTANCE(timer)) {
419+
LL_TIM_SetCounterMode(timer, LL_TIM_COUNTERMODE_UP);
420+
}
421+
422+
if (IS_TIM_CLOCK_DIVISION_INSTANCE(timer)) {
423+
LL_TIM_SetClockDivision(timer, LL_TIM_CLOCKDIVISION_DIV1);
424+
}
422425

423-
if (LL_TIM_Init(timer, &init) != SUCCESS) {
424-
LOG_ERR("Could not initialize timer");
425-
return -EIO;
426+
#ifdef IS_TIM_REPETITION_COUNTER_INSTANCE
427+
if (IS_TIM_REPETITION_COUNTER_INSTANCE(timer)) {
428+
LL_TIM_SetRepetitionCounter(timer, 0U);
426429
}
430+
#endif
431+
432+
/* Generate an update event to reload the Prescaler
433+
* and the repetition counter value (if applicable) immediately
434+
*/
435+
LL_TIM_GenerateEvent_UPDATE(timer);
427436

428437
return 0;
429438
}

0 commit comments

Comments
 (0)