Skip to content

Commit e2c3931

Browse files
savent404fabiobaltieri
authored andcommitted
drivers: pwm: pwm_stm32: Add 6-PWM support
User can use 6-PWM motor driver in dts like this below: ```dts pwms = // ch1,ch2,ch3,ch1n,ch2n,ch3n <&pwm 1 PWM_USEC(50) PWM_POLARITY_NORMAL>, // ch1 <&pwm 2 PWM_USEC(50) PWM_POLARITY_NORMAL>, // ch2 <&pwm 3 PWM_USEC(50) PWM_POLARITY_NORMAL>, // ch3 <&pwm 1 PWM_USEC(50) (PWM_POLARITY_NORMAL|STM32_PWM_COMPLEMENTARY)>, <&pwm 2 PWM_USEC(50) (PWM_POLARITY_NORMAL|STM32_PWM_COMPLEMENTARY)>, <&pwm 3 PWM_USEC(50) (PWM_POLARITY_NORMAL|STM32_PWM_COMPLEMENTARY)>; ``` Signed-off-by: Savent Gate <[email protected]>
1 parent c54b71d commit e2c3931

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

drivers/pwm/pwm_stm32.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ static int pwm_stm32_set_cycles(const struct device *dev, uint32_t channel,
242242

243243
uint32_t ll_channel;
244244
uint32_t current_ll_channel; /* complementary output if used */
245+
uint32_t negative_ll_channel;
245246

246247
if (channel < 1u || channel > TIMER_MAX_CH) {
247248
LOG_ERR("Invalid channel (%d)", channel);
@@ -269,16 +270,22 @@ static int pwm_stm32_set_cycles(const struct device *dev, uint32_t channel,
269270

270271
ll_channel = ch2ll[channel - 1u];
271272

273+
if (channel <= ARRAY_SIZE(ch2ll_n)) {
274+
negative_ll_channel = ch2ll_n[channel - 1u];
275+
} else {
276+
negative_ll_channel = 0;
277+
}
278+
272279
/* in LL_TIM_CC_DisableChannel and LL_TIM_CC_IsEnabledChannel,
273280
* the channel param could be the complementary one
274281
*/
275282
if ((flags & STM32_PWM_COMPLEMENTARY_MASK) == STM32_PWM_COMPLEMENTARY) {
276-
if (channel > ARRAY_SIZE(ch2ll_n)) {
283+
if (!negative_ll_channel) {
277284
/* setting a flag on a channel that has not this capability */
278285
LOG_ERR("Channel %d has NO complementary output", channel);
279286
return -EINVAL;
280287
}
281-
current_ll_channel = ch2ll_n[channel - 1u];
288+
current_ll_channel = negative_ll_channel;
282289
} else {
283290
current_ll_channel = ll_channel;
284291
}
@@ -315,9 +322,25 @@ static int pwm_stm32_set_cycles(const struct device *dev, uint32_t channel,
315322
if ((flags & STM32_PWM_COMPLEMENTARY_MASK) == STM32_PWM_COMPLEMENTARY) {
316323
oc_init.OCNState = LL_TIM_OCSTATE_ENABLE;
317324
oc_init.OCNPolarity = get_polarity(flags);
325+
326+
/* inherit the polarity of the positive output */
327+
oc_init.OCState = LL_TIM_CC_IsEnabledChannel(cfg->timer, ll_channel)
328+
? LL_TIM_OCSTATE_ENABLE
329+
: LL_TIM_OCSTATE_DISABLE;
330+
oc_init.OCPolarity = LL_TIM_OC_GetPolarity(cfg->timer, ll_channel);
318331
} else {
319332
oc_init.OCState = LL_TIM_OCSTATE_ENABLE;
320333
oc_init.OCPolarity = get_polarity(flags);
334+
335+
/* inherit the polarity of the negative output */
336+
if (negative_ll_channel) {
337+
oc_init.OCNState =
338+
LL_TIM_CC_IsEnabledChannel(cfg->timer, negative_ll_channel)
339+
? LL_TIM_OCSTATE_ENABLE
340+
: LL_TIM_OCSTATE_DISABLE;
341+
oc_init.OCNPolarity =
342+
LL_TIM_OC_GetPolarity(cfg->timer, negative_ll_channel);
343+
}
321344
}
322345
#else /* LL_TIM_CHANNEL_CH1N */
323346

0 commit comments

Comments
 (0)