Skip to content

Commit 6c30d95

Browse files
committed
fix(hardwaretimer): avoid glitch when PWM configuration changed
Fixes #2575. Note that the issue is that TIM_OCx_SetConfig() disable in unconditionally the N state output (TIM_CCER_CCxNE). Signed-off-by: Frederic Pillon <[email protected]>
1 parent 1df53a4 commit 6c30d95

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

libraries/SrcWrapper/src/HardwareTimer.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,10 @@ void HardwareTimer::setPWM(uint32_t channel, uint32_t pin, uint32_t frequency, u
925925
*/
926926
void HardwareTimer::setPWM(uint32_t channel, PinName pin, uint32_t frequency, uint32_t dutycycle, callback_function_t PeriodCallback, callback_function_t CompareCallback)
927927
{
928-
setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
928+
TimerModes_t previousMode = getMode(channel);
929+
if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) {
930+
setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
931+
}
929932
setOverflow(frequency, HERTZ_FORMAT);
930933
setCaptureCompare(channel, dutycycle, PERCENT_COMPARE_FORMAT);
931934
if (PeriodCallback) {
@@ -934,7 +937,9 @@ void HardwareTimer::setPWM(uint32_t channel, PinName pin, uint32_t frequency, ui
934937
if (CompareCallback) {
935938
attachInterrupt(channel, CompareCallback);
936939
}
937-
resume();
940+
if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) {
941+
resume();
942+
}
938943
}
939944

940945
/**

0 commit comments

Comments
 (0)