Skip to content

Commit bdcc98e

Browse files
committed
analogWrite: Configure HardwareTimer mode only if not previously done
This avoid glitch when calling analoWrite multiple time. Also don't start again timer otherwise ther is also some glitches Fixes #939
1 parent f28dc09 commit bdcc98e

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

libraries/SrcWrapper/src/stm32/analog.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,7 @@ void pwm_start(PinName pin, uint32_t PWM_freq, uint32_t value, TimerCompareForma
10281028
{
10291029
TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin, PinMap_PWM);
10301030
HardwareTimer *HT;
1031+
TimerModes_t previousMode;
10311032
uint32_t index = get_timer_index(Instance);
10321033
if (HardwareTimer_Handle[index] == NULL) {
10331034
HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin, PinMap_PWM));
@@ -1037,10 +1038,15 @@ void pwm_start(PinName pin, uint32_t PWM_freq, uint32_t value, TimerCompareForma
10371038

10381039
uint32_t channel = STM_PIN_CHANNEL(pinmap_function(pin, PinMap_PWM));
10391040

1040-
HT->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
1041+
previousMode = HT->getMode(channel);
1042+
if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) {
1043+
HT->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
1044+
}
10411045
HT->setOverflow(PWM_freq, HERTZ_FORMAT);
10421046
HT->setCaptureCompare(channel, value, resolution);
1043-
HT->resume();
1047+
if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) {
1048+
HT->resume();
1049+
}
10441050
}
10451051
/**
10461052
* @brief This function will disable the PWM

0 commit comments

Comments
 (0)