Skip to content

Commit 76c1d71

Browse files
ckhardinnashif
authored andcommitted
drivers: pwm: stm32: add device tree configuration for deadtime
When using an stm32 in a bridge circuit with complementary outputs, the deadtime needs to be configurable to avoid shoot-thru current on the circuit. So, the HAL has the configuration in the BDTR init and use that api access to set the configuration. Signed-off-by: Charles Hardin <[email protected]>
1 parent 52e9064 commit 76c1d71

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

drivers/pwm/pwm_stm32.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ struct pwm_stm32_config {
9090
TIM_TypeDef *timer;
9191
uint32_t prescaler;
9292
uint32_t countermode;
93+
uint32_t deadtime;
9394
const struct stm32_pclken *pclken;
9495
size_t pclk_len;
9596
const struct pinctrl_dev_config *pcfg;
@@ -699,12 +700,29 @@ static int pwm_stm32_init(const struct device *dev)
699700
}
700701
#endif
701702

702-
#if !defined(CONFIG_SOC_SERIES_STM32L0X) && !defined(CONFIG_SOC_SERIES_STM32L1X)
703-
/* enable outputs and counter */
703+
#ifdef IS_TIM_BREAK_INSTANCE
704+
/* Use the macro IS_TIM_BREAK_INSTANCE to check for supporting the
705+
* break instance timers since some socs like L0/L1 will not
706+
* compile and this checks explicitly for the api instead of the soc
707+
*/
704708
if (IS_TIM_BREAK_INSTANCE(timer)) {
709+
/* enable outputs and counter */
705710
LL_TIM_EnableAllOutputs(timer);
711+
712+
/* set the deadtime from the configuration */
713+
LL_TIM_OC_SetDeadTime(timer, cfg->deadtime);
714+
} else if (cfg->deadtime != 0) {
715+
LOG_ERR("Setting deadtime %d on a non-break timer %s",
716+
cfg->deadtime, dev->name);
717+
return -ENOTSUP;
706718
}
707-
#endif
719+
#else
720+
if (cfg->deadtime != 0) {
721+
LOG_ERR("Setting deadtime %d on a non-break timer %s",
722+
cfg->deadtime, dev->name);
723+
return -ENOTSUP;
724+
}
725+
#endif /* IS_TIM_BREAK_INSTANCE */
708726

709727
LL_TIM_EnableCounter(timer);
710728

@@ -766,6 +784,7 @@ static void pwm_stm32_irq_config_func_##index(const struct device *dev) \
766784
.timer = (TIM_TypeDef *)DT_REG_ADDR(PWM(index)), \
767785
.prescaler = DT_PROP(PWM(index), st_prescaler), \
768786
.countermode = DT_PROP(PWM(index), st_countermode), \
787+
.deadtime = DT_PROP(PWM(index), st_deadtime), \
769788
.pclken = pclken_##index, \
770789
.pclk_len = DT_NUM_CLOCKS(PWM(index)), \
771790
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(index), \

dts/bindings/timer/st,stm32-timers.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,19 @@ properties:
4646
down.
4747
4848
If absent, then STM32_TIM_COUNTERMODE_UP is used (reset state).
49+
50+
st,deadtime:
51+
type: int
52+
default: 0
53+
description: |
54+
Sets the dead-time configuration on the associated timer which will add
55+
a delay between the rising edge of the reference signal and the rising
56+
edge of both the OCx and OCxN signals. This is commonly used and required
57+
with bridge circuits to prevent shoot-through currents.
58+
59+
This feature is supported only by certain timer instances. Refer
60+
to your product's Reference Manual for more information about this
61+
feature's availability. (In particular, certain series such as
62+
STM32L0/STM32L1 have no instance supporting this feature).
63+
64+
Valid range [0 ... 255].

0 commit comments

Comments
 (0)