Skip to content

Commit a339084

Browse files
committed
STM32: pwmout_write: configure channel only when not already enabled
Fix PWM glitch on write(), TARGET_STM/pwmout_api.c, #10734
1 parent 3e6f5eb commit a339084

File tree

12 files changed

+56
-3
lines changed

12 files changed

+56
-3
lines changed

targets/TARGET_STM/TARGET_STM32F0/common_objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "PeripheralNames.h"
3636
#include "PinNames.h"
3737
#include "stm32f0xx_ll_usart.h"
38+
#include "stm32f0xx_ll_tim.h"
3839

3940
#ifdef __cplusplus
4041
extern "C" {

targets/TARGET_STM/TARGET_STM32F1/common_objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "PeripheralNames.h"
3636
#include "PinNames.h"
3737
#include "stm32f1xx_ll_usart.h"
38+
#include "stm32f1xx_ll_tim.h"
3839

3940
#ifdef __cplusplus
4041
extern "C" {

targets/TARGET_STM/TARGET_STM32F2/objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "PeripheralNames.h"
3636
#include "PinNames.h"
3737
#include "stm32f2xx_ll_usart.h"
38+
#include "stm32f2xx_ll_tim.h"
3839

3940
#ifdef __cplusplus
4041
extern "C" {

targets/TARGET_STM/TARGET_STM32F3/common_objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "PeripheralNames.h"
3636
#include "PinNames.h"
3737
#include "stm32f3xx_ll_usart.h"
38+
#include "stm32f3xx_ll_tim.h"
3839

3940
#ifdef __cplusplus
4041
extern "C" {

targets/TARGET_STM/TARGET_STM32F4/common_objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "PeripheralNames.h"
3636
#include "PinNames.h"
3737
#include "stm32f4xx_ll_usart.h"
38+
#include "stm32f4xx_ll_tim.h"
3839

3940
#ifdef __cplusplus
4041
extern "C" {

targets/TARGET_STM/TARGET_STM32F7/common_objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "PeripheralNames.h"
3636
#include "PinNames.h"
3737
#include "stm32f7xx_ll_usart.h"
38+
#include "stm32f7xx_ll_tim.h"
3839

3940
#ifdef __cplusplus
4041
extern "C" {

targets/TARGET_STM/TARGET_STM32H7/objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "PinNames.h"
3737
#include "stm32h7xx_ll_usart.h"
3838
#include "stm32h7xx_ll_rtc.h"
39+
#include "stm32h7xx_ll_tim.h"
3940

4041
#ifdef __cplusplus
4142
extern "C" {

targets/TARGET_STM/TARGET_STM32L0/common_objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "PeripheralNames.h"
3636
#include "PinNames.h"
3737
#include "stm32l0xx_ll_usart.h"
38+
#include "stm32l0xx_ll_tim.h"
3839

3940
#ifdef __cplusplus
4041
extern "C" {

targets/TARGET_STM/TARGET_STM32L1/common_objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "PeripheralNames.h"
3636
#include "PinNames.h"
3737
#include "stm32l1xx_ll_usart.h"
38+
#include "stm32l1xx_ll_tim.h"
3839

3940
#ifdef __cplusplus
4041
extern "C" {

targets/TARGET_STM/TARGET_STM32L4/common_objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "PeripheralNames.h"
3636
#include "PinNames.h"
3737
#include "stm32l4xx_ll_usart.h"
38+
#include "stm32l4xx_ll_tim.h"
3839

3940
#ifdef __cplusplus
4041
extern "C" {

targets/TARGET_STM/TARGET_STM32WB/common_objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "PeripheralNames.h"
3838
#include "PinNames.h"
3939
#include "stm32wbxx_ll_usart.h"
40+
#include "stm32wbxx_ll_tim.h"
4041

4142
#ifdef __cplusplus
4243
extern "C" {

targets/TARGET_STM/pwmout_api.c

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,43 @@
3939

4040
static TIM_HandleTypeDef TimHandle;
4141

42+
/* Convert STM32 Cube HAL channel to LL channel */
43+
uint32_t TIM_ChannelConvert_HAL2LL(uint32_t channel, pwmout_t *obj)
44+
{
45+
#if !defined(PWMOUT_INVERTED_NOT_SUPPORTED)
46+
if (obj->inverted) {
47+
switch (channel) {
48+
case TIM_CHANNEL_1 :
49+
return LL_TIM_CHANNEL_CH1N;
50+
case TIM_CHANNEL_2 :
51+
return LL_TIM_CHANNEL_CH2N;
52+
case TIM_CHANNEL_3 :
53+
return LL_TIM_CHANNEL_CH3N;
54+
#if defined(LL_TIM_CHANNEL_CH4N)
55+
case TIM_CHANNEL_4 :
56+
return LL_TIM_CHANNEL_CH4N;
57+
#endif
58+
default : /* Optional */
59+
return 0;
60+
}
61+
} else
62+
#endif
63+
{
64+
switch (channel) {
65+
case TIM_CHANNEL_1 :
66+
return LL_TIM_CHANNEL_CH1;
67+
case TIM_CHANNEL_2 :
68+
return LL_TIM_CHANNEL_CH2;
69+
case TIM_CHANNEL_3 :
70+
return LL_TIM_CHANNEL_CH3;
71+
case TIM_CHANNEL_4 :
72+
return LL_TIM_CHANNEL_CH4;
73+
default : /* Optional */
74+
return 0;
75+
}
76+
}
77+
}
78+
4279
void pwmout_init(pwmout_t *obj, PinName pin)
4380
{
4481
// Get the peripheral name from the pin and assign it to the object
@@ -214,10 +251,15 @@ void pwmout_write(pwmout_t *obj, float value)
214251
return;
215252
}
216253

217-
if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) {
218-
error("Cannot initialize PWM\n");
254+
if (LL_TIM_CC_IsEnabledChannel(TimHandle.Instance, TIM_ChannelConvert_HAL2LL(channel, obj)) == 0) {
255+
// If channel is not enabled, proceed to channel configuration
256+
if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) {
257+
error("Cannot initialize PWM\n");
258+
}
259+
} else {
260+
// If channel already enabled, only update compare value to avoid glitch
261+
__HAL_TIM_SET_COMPARE(&TimHandle, channel, sConfig.Pulse);
219262
}
220-
221263
#if !defined(PWMOUT_INVERTED_NOT_SUPPORTED)
222264
if (obj->inverted) {
223265
HAL_TIMEx_PWMN_Start(&TimHandle, channel);

0 commit comments

Comments
 (0)