Skip to content

Commit f1d475a

Browse files
authored
Merge pull request #941 from ABOSTM/HARDWARETIMER_ANALOGWRITE_NO_GLITCH
Hardwaretimer analogWrite() no glitch
2 parents 89e08d2 + bdcc98e commit f1d475a

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

cores/arduino/HardwareTimer.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,20 @@ void HardwareTimer::setMode(uint32_t channel, TimerModes_t mode, PinName pin)
690690
}
691691
}
692692

693+
/**
694+
* @brief Retrieves channel mode configured
695+
* @param channel: Arduino channel [1..4]
696+
* @retval returns configured mode
697+
*/
698+
TimerModes_t HardwareTimer::getMode(uint32_t channel)
699+
{
700+
if ((1 <= channel) && (channel <= TIMER_CHANNELS)) {
701+
return _ChannelMode[channel - 1];
702+
} else {
703+
return TIMER_DISABLED;
704+
}
705+
}
706+
693707
/**
694708
* @brief Enable or disable preloading for overflow value
695709
* When disabled, changes to the overflow value take effect

cores/arduino/HardwareTimer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ class HardwareTimer {
118118
void setMode(uint32_t channel, TimerModes_t mode, PinName pin = NC); // Configure timer channel with specified mode on specified pin if available
119119
void setMode(uint32_t channel, TimerModes_t mode, uint32_t pin);
120120

121+
TimerModes_t getMode(uint32_t channel); // Retrieve configured mode
122+
121123
void setPreloadEnable(bool value); // Configure overflow preload enable setting
122124

123125
uint32_t getCaptureCompare(uint32_t channel, TimerCompareFormat_t format = TICK_COMPARE_FORMAT); // return Capture/Compare register value of specified channel depending on format provided

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)