Skip to content

Hardwaretimer analogWrite() no glitch #941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions cores/arduino/HardwareTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,20 @@ void HardwareTimer::setMode(uint32_t channel, TimerModes_t mode, PinName pin)
}
}

/**
* @brief Retrieves channel mode configured
* @param channel: Arduino channel [1..4]
* @retval returns configured mode
*/
TimerModes_t HardwareTimer::getMode(uint32_t channel)
{
if ((1 <= channel) && (channel <= TIMER_CHANNELS)) {
return _ChannelMode[channel - 1];
} else {
return TIMER_DISABLED;
}
}

/**
* @brief Enable or disable preloading for overflow value
* When disabled, changes to the overflow value take effect
Expand Down
2 changes: 2 additions & 0 deletions cores/arduino/HardwareTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ class HardwareTimer {
void setMode(uint32_t channel, TimerModes_t mode, PinName pin = NC); // Configure timer channel with specified mode on specified pin if available
void setMode(uint32_t channel, TimerModes_t mode, uint32_t pin);

TimerModes_t getMode(uint32_t channel); // Retrieve configured mode

void setPreloadEnable(bool value); // Configure overflow preload enable setting

uint32_t getCaptureCompare(uint32_t channel, TimerCompareFormat_t format = TICK_COMPARE_FORMAT); // return Capture/Compare register value of specified channel depending on format provided
Expand Down
10 changes: 8 additions & 2 deletions libraries/SrcWrapper/src/stm32/analog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,7 @@ void pwm_start(PinName pin, uint32_t PWM_freq, uint32_t value, TimerCompareForma
{
TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin, PinMap_PWM);
HardwareTimer *HT;
TimerModes_t previousMode;
uint32_t index = get_timer_index(Instance);
if (HardwareTimer_Handle[index] == NULL) {
HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin, PinMap_PWM));
Expand All @@ -1037,10 +1038,15 @@ void pwm_start(PinName pin, uint32_t PWM_freq, uint32_t value, TimerCompareForma

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

HT->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
previousMode = HT->getMode(channel);
if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) {
HT->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
}
HT->setOverflow(PWM_freq, HERTZ_FORMAT);
HT->setCaptureCompare(channel, value, resolution);
HT->resume();
if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) {
HT->resume();
}
}
/**
* @brief This function will disable the PWM
Expand Down