Skip to content

Commit a5531f5

Browse files
gautierg-stnashif
authored andcommitted
drivers: pwm stm32: remove init struct
Remove the calls to LL_TIM_xx_StructInit and LL_TIM_xx_Init in the PWM driver. This avoids calling functions from stm32xxxx_ll_tim.c. They are replaced by a set of simpler functions from the header file. OC Init in particular is much simpler now. The init structure needed to be filled out with the complementary channel (if it existed), even though its configuration didn't change. The new init is much more direct and only touches what needs to be modified. Signed-off-by: Guillaume Gautier <[email protected]>
1 parent 4457e6d commit a5531f5

File tree

1 file changed

+36
-85
lines changed

1 file changed

+36
-85
lines changed

drivers/pwm/pwm_stm32.c

Lines changed: 36 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -288,45 +288,11 @@ static int pwm_stm32_set_cycles(const struct device *dev, uint32_t channel,
288288
return -ENOTSUP;
289289
}
290290

291-
if (!LL_TIM_CC_IsEnabledChannel(cfg->timer, current_ll_channel)) {
292-
LL_TIM_OC_InitTypeDef oc_init;
293-
294-
LL_TIM_OC_StructInit(&oc_init);
295-
296-
oc_init.OCMode = LL_TIM_OCMODE_PWM1;
297-
298-
#if defined(LL_TIM_CHANNEL_CH1N)
299-
/* the flags holds the STM32_PWM_COMPLEMENTARY information */
300-
if ((flags & STM32_PWM_COMPLEMENTARY_MASK) == STM32_PWM_COMPLEMENTARY) {
301-
oc_init.OCNState = LL_TIM_OCSTATE_ENABLE;
302-
oc_init.OCNPolarity = get_polarity(flags);
303-
304-
/* inherit the polarity of the positive output */
305-
oc_init.OCState = LL_TIM_CC_IsEnabledChannel(cfg->timer, ll_channel)
306-
? LL_TIM_OCSTATE_ENABLE
307-
: LL_TIM_OCSTATE_DISABLE;
308-
oc_init.OCPolarity = LL_TIM_OC_GetPolarity(cfg->timer, ll_channel);
309-
} else {
310-
oc_init.OCState = LL_TIM_OCSTATE_ENABLE;
311-
oc_init.OCPolarity = get_polarity(flags);
312-
313-
/* inherit the polarity of the negative output */
314-
if (negative_ll_channel) {
315-
oc_init.OCNState =
316-
LL_TIM_CC_IsEnabledChannel(cfg->timer, negative_ll_channel)
317-
? LL_TIM_OCSTATE_ENABLE
318-
: LL_TIM_OCSTATE_DISABLE;
319-
oc_init.OCNPolarity =
320-
LL_TIM_OC_GetPolarity(cfg->timer, negative_ll_channel);
321-
}
322-
}
323-
#else /* LL_TIM_CHANNEL_CH1N */
324-
325-
oc_init.OCState = LL_TIM_OCSTATE_ENABLE;
326-
oc_init.OCPolarity = get_polarity(flags);
327-
#endif /* LL_TIM_CHANNEL_CH1N */
328-
oc_init.CompareValue = pulse_cycles;
291+
LL_TIM_OC_SetPolarity(cfg->timer, current_ll_channel, get_polarity(flags));
292+
set_timer_compare[channel - 1u](cfg->timer, pulse_cycles);
293+
LL_TIM_SetAutoReload(cfg->timer, period_cycles);
329294

295+
if (!LL_TIM_CC_IsEnabledChannel(cfg->timer, current_ll_channel)) {
330296
#ifdef CONFIG_PWM_CAPTURE
331297
if (IS_TIM_SLAVE_INSTANCE(cfg->timer)) {
332298
LL_TIM_SetSlaveMode(cfg->timer,
@@ -336,58 +302,43 @@ static int pwm_stm32_set_cycles(const struct device *dev, uint32_t channel,
336302
}
337303
#endif /* CONFIG_PWM_CAPTURE */
338304

339-
/* in LL_TIM_OC_Init, the channel is always the non-complementary */
340-
if (LL_TIM_OC_Init(cfg->timer, ll_channel, &oc_init) != SUCCESS) {
341-
LOG_ERR("Could not initialize timer channel output");
342-
return -EIO;
343-
}
344-
305+
LL_TIM_OC_SetMode(cfg->timer, ll_channel, LL_TIM_OCMODE_PWM1);
306+
#ifdef LL_TIM_OCIDLESTATE_LOW
307+
LL_TIM_OC_SetIdleState(cfg->timer, current_ll_channel, LL_TIM_OCIDLESTATE_LOW);
308+
#endif
309+
LL_TIM_CC_EnableChannel(cfg->timer, current_ll_channel);
345310
LL_TIM_EnableARRPreload(cfg->timer);
346311
/* in LL_TIM_OC_EnablePreload, the channel is always the non-complementary */
347312
LL_TIM_OC_EnablePreload(cfg->timer, ll_channel);
348-
LL_TIM_SetAutoReload(cfg->timer, period_cycles);
349313
LL_TIM_GenerateEvent_UPDATE(cfg->timer);
350-
} else {
351-
/* in LL_TIM_OC_SetPolarity, the channel could be the complementary one */
352-
LL_TIM_OC_SetPolarity(cfg->timer, current_ll_channel, get_polarity(flags));
353-
set_timer_compare[channel - 1u](cfg->timer, pulse_cycles);
354-
LL_TIM_SetAutoReload(cfg->timer, period_cycles);
355314
}
356315

357316
return 0;
358317
}
359318

360319
#ifdef CONFIG_PWM_CAPTURE
361-
static int init_capture_channels(const struct device *dev, uint32_t channel,
320+
static void init_capture_channels(const struct device *dev, uint32_t channel,
362321
pwm_flags_t flags)
363322
{
364323
const struct pwm_stm32_config *cfg = dev->config;
365324
bool is_inverted = (flags & PWM_POLARITY_MASK) == PWM_POLARITY_INVERTED;
366-
LL_TIM_IC_InitTypeDef ic;
325+
uint32_t ll_channel = ch2ll[channel - 1];
326+
uint32_t ll_complementary_channel = ch2ll[complimentary_channel[channel] - 1];
367327

368-
LL_TIM_IC_StructInit(&ic);
369-
ic.ICPrescaler = TIM_ICPSC_DIV1;
370-
ic.ICFilter = LL_TIM_IC_FILTER_FDIV1;
371328

372329
/* Setup main channel */
373-
ic.ICActiveInput = LL_TIM_ACTIVEINPUT_DIRECTTI;
374-
ic.ICPolarity = is_inverted ? LL_TIM_IC_POLARITY_FALLING : LL_TIM_IC_POLARITY_RISING;
375-
376-
if (LL_TIM_IC_Init(cfg->timer, ch2ll[channel - 1], &ic) != SUCCESS) {
377-
LOG_ERR("Could not initialize main channel for PWM capture");
378-
return -EIO;
379-
}
330+
LL_TIM_IC_SetPrescaler(cfg->timer, ll_channel, LL_TIM_ICPSC_DIV1);
331+
LL_TIM_IC_SetFilter(cfg->timer, ll_channel, LL_TIM_IC_FILTER_FDIV1);
332+
LL_TIM_IC_SetActiveInput(cfg->timer, ll_channel, LL_TIM_ACTIVEINPUT_DIRECTTI);
333+
LL_TIM_IC_SetPolarity(cfg->timer, ll_channel,
334+
is_inverted ? LL_TIM_IC_POLARITY_FALLING : LL_TIM_IC_POLARITY_RISING);
380335

381336
/* Setup complimentary channel */
382-
ic.ICActiveInput = LL_TIM_ACTIVEINPUT_INDIRECTTI;
383-
ic.ICPolarity = is_inverted ? LL_TIM_IC_POLARITY_RISING : LL_TIM_IC_POLARITY_FALLING;
384-
385-
if (LL_TIM_IC_Init(cfg->timer, ch2ll[complimentary_channel[channel] - 1], &ic) != SUCCESS) {
386-
LOG_ERR("Could not initialize complimentary channel for PWM capture");
387-
return -EIO;
388-
}
389-
390-
return 0;
337+
LL_TIM_IC_SetPrescaler(cfg->timer, ll_complementary_channel, LL_TIM_ICPSC_DIV1);
338+
LL_TIM_IC_SetFilter(cfg->timer, ll_complementary_channel, LL_TIM_IC_FILTER_FDIV1);
339+
LL_TIM_IC_SetActiveInput(cfg->timer, ll_complementary_channel, LL_TIM_ACTIVEINPUT_INDIRECTTI);
340+
LL_TIM_IC_SetPolarity(cfg->timer, ll_complementary_channel,
341+
is_inverted ? LL_TIM_IC_POLARITY_RISING : LL_TIM_IC_POLARITY_FALLING);
391342
}
392343

393344
static int pwm_stm32_configure_capture(const struct device *dev,
@@ -411,7 +362,6 @@ static int pwm_stm32_configure_capture(const struct device *dev,
411362
const struct pwm_stm32_config *cfg = dev->config;
412363
struct pwm_stm32_data *data = dev->data;
413364
struct pwm_stm32_capture_data *cpt = &data->capture;
414-
int ret;
415365

416366
if (!cfg->four_channel_capture_support) {
417367
if ((channel != 1u) && (channel != 2u)) {
@@ -451,10 +401,7 @@ static int pwm_stm32_configure_capture(const struct device *dev,
451401
/* Prevents faulty behavior while making changes */
452402
LL_TIM_SetSlaveMode(cfg->timer, LL_TIM_SLAVEMODE_DISABLED);
453403

454-
ret = init_capture_channels(dev, channel, flags);
455-
if (ret < 0) {
456-
return ret;
457-
}
404+
init_capture_channels(dev, channel, flags);
458405

459406
if (!cfg->four_channel_capture_support) {
460407
if (channel == 1u) {
@@ -681,7 +628,6 @@ static int pwm_stm32_init(const struct device *dev)
681628
struct pwm_stm32_data *data = dev->data;
682629
const struct pwm_stm32_config *cfg = dev->config;
683630
const struct device *clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
684-
LL_TIM_InitTypeDef init;
685631
uint32_t tim_clk;
686632
int r;
687633

@@ -728,17 +674,22 @@ static int pwm_stm32_init(const struct device *dev)
728674
}
729675

730676
/* initialize timer */
731-
LL_TIM_StructInit(&init);
677+
LL_TIM_SetPrescaler(cfg->timer, cfg->prescaler);
678+
LL_TIM_SetAutoReload(cfg->timer, 0U);
679+
680+
if (IS_TIM_COUNTER_MODE_SELECT_INSTANCE(cfg->timer)) {
681+
LL_TIM_SetCounterMode(cfg->timer, cfg->countermode);
682+
}
732683

733-
init.Prescaler = cfg->prescaler;
734-
init.CounterMode = cfg->countermode;
735-
init.Autoreload = 0u;
736-
init.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
684+
if (IS_TIM_CLOCK_DIVISION_INSTANCE(cfg->timer)) {
685+
LL_TIM_SetClockDivision(cfg->timer, LL_TIM_CLOCKDIVISION_DIV1);
686+
}
737687

738-
if (LL_TIM_Init(cfg->timer, &init) != SUCCESS) {
739-
LOG_ERR("Could not initialize timer");
740-
return -EIO;
688+
#ifdef IS_TIM_REPETITION_COUNTER_INSTANCE
689+
if (IS_TIM_REPETITION_COUNTER_INSTANCE(cfg->timer)) {
690+
LL_TIM_SetRepetitionCounter(cfg->timer, 0U);
741691
}
692+
#endif
742693

743694
#if !defined(CONFIG_SOC_SERIES_STM32L0X) && !defined(CONFIG_SOC_SERIES_STM32L1X)
744695
/* enable outputs and counter */

0 commit comments

Comments
 (0)