@@ -288,45 +288,11 @@ static int pwm_stm32_set_cycles(const struct device *dev, uint32_t channel,
288
288
return - ENOTSUP ;
289
289
}
290
290
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 );
329
294
295
+ if (!LL_TIM_CC_IsEnabledChannel (cfg -> timer , current_ll_channel )) {
330
296
#ifdef CONFIG_PWM_CAPTURE
331
297
if (IS_TIM_SLAVE_INSTANCE (cfg -> timer )) {
332
298
LL_TIM_SetSlaveMode (cfg -> timer ,
@@ -336,58 +302,43 @@ static int pwm_stm32_set_cycles(const struct device *dev, uint32_t channel,
336
302
}
337
303
#endif /* CONFIG_PWM_CAPTURE */
338
304
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 );
345
310
LL_TIM_EnableARRPreload (cfg -> timer );
346
311
/* in LL_TIM_OC_EnablePreload, the channel is always the non-complementary */
347
312
LL_TIM_OC_EnablePreload (cfg -> timer , ll_channel );
348
- LL_TIM_SetAutoReload (cfg -> timer , period_cycles );
349
313
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 );
355
314
}
356
315
357
316
return 0 ;
358
317
}
359
318
360
319
#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 ,
362
321
pwm_flags_t flags )
363
322
{
364
323
const struct pwm_stm32_config * cfg = dev -> config ;
365
324
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 ];
367
327
368
- LL_TIM_IC_StructInit (& ic );
369
- ic .ICPrescaler = TIM_ICPSC_DIV1 ;
370
- ic .ICFilter = LL_TIM_IC_FILTER_FDIV1 ;
371
328
372
329
/* 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 );
380
335
381
336
/* 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 );
391
342
}
392
343
393
344
static int pwm_stm32_configure_capture (const struct device * dev ,
@@ -411,7 +362,6 @@ static int pwm_stm32_configure_capture(const struct device *dev,
411
362
const struct pwm_stm32_config * cfg = dev -> config ;
412
363
struct pwm_stm32_data * data = dev -> data ;
413
364
struct pwm_stm32_capture_data * cpt = & data -> capture ;
414
- int ret ;
415
365
416
366
if (!cfg -> four_channel_capture_support ) {
417
367
if ((channel != 1u ) && (channel != 2u )) {
@@ -451,10 +401,7 @@ static int pwm_stm32_configure_capture(const struct device *dev,
451
401
/* Prevents faulty behavior while making changes */
452
402
LL_TIM_SetSlaveMode (cfg -> timer , LL_TIM_SLAVEMODE_DISABLED );
453
403
454
- ret = init_capture_channels (dev , channel , flags );
455
- if (ret < 0 ) {
456
- return ret ;
457
- }
404
+ init_capture_channels (dev , channel , flags );
458
405
459
406
if (!cfg -> four_channel_capture_support ) {
460
407
if (channel == 1u ) {
@@ -681,7 +628,6 @@ static int pwm_stm32_init(const struct device *dev)
681
628
struct pwm_stm32_data * data = dev -> data ;
682
629
const struct pwm_stm32_config * cfg = dev -> config ;
683
630
const struct device * clk = DEVICE_DT_GET (STM32_CLOCK_CONTROL_NODE );
684
- LL_TIM_InitTypeDef init ;
685
631
uint32_t tim_clk ;
686
632
int r ;
687
633
@@ -728,17 +674,22 @@ static int pwm_stm32_init(const struct device *dev)
728
674
}
729
675
730
676
/* 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
+ }
732
683
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
+ }
737
687
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 ) ;
741
691
}
692
+ #endif
742
693
743
694
#if !defined(CONFIG_SOC_SERIES_STM32L0X ) && !defined(CONFIG_SOC_SERIES_STM32L1X )
744
695
/* enable outputs and counter */
0 commit comments